4 * Copyright (C) 1998-2003 Brian Palmer <brianp@sginet.com>
5 * Copyright (C) 2006 Aleksey Bragin <aleksey@reactos.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <ndk/ldrtypes.h>
27 //FIXME: Do a better way to retrieve Arc disk information
28 extern ULONG reactos_disk_count
;
29 extern ARC_DISK_SIGNATURE reactos_arc_disk_info
[];
30 extern char reactos_arc_strings
[32][256];
33 WinLdrCheckForLoadedDll(IN OUT PLOADER_PARAMETER_BLOCK WinLdrBlock
,
35 OUT PLDR_DATA_TABLE_ENTRY
*LoadedEntry
);
38 VOID
DumpMemoryAllocMap(VOID
);
39 VOID
WinLdrpDumpMemoryDescriptors(PLOADER_PARAMETER_BLOCK LoaderBlock
);
40 VOID
WinLdrpDumpBootDriver(PLOADER_PARAMETER_BLOCK LoaderBlock
);
41 VOID
WinLdrpDumpArcDisks(PLOADER_PARAMETER_BLOCK LoaderBlock
);
46 AllocateAndInitLPB(PLOADER_PARAMETER_BLOCK
*OutLoaderBlock
)
48 PLOADER_PARAMETER_BLOCK LoaderBlock
;
50 /* Allocate and zero-init the LPB */
51 LoaderBlock
= MmHeapAlloc(sizeof(LOADER_PARAMETER_BLOCK
));
52 RtlZeroMemory(LoaderBlock
, sizeof(LOADER_PARAMETER_BLOCK
));
54 /* Init three critical lists, used right away */
55 InitializeListHead(&LoaderBlock
->LoadOrderListHead
);
56 InitializeListHead(&LoaderBlock
->MemoryDescriptorListHead
);
57 InitializeListHead(&LoaderBlock
->BootDriverListHead
);
59 /* Alloc space for NLS (it will be converted to VA in WinLdrLoadNLS) */
60 LoaderBlock
->NlsData
= MmHeapAlloc(sizeof(NLS_DATA_BLOCK
));
61 if (LoaderBlock
->NlsData
== NULL
)
63 UiMessageBox("Failed to allocate memory for NLS table data!");
66 RtlZeroMemory(LoaderBlock
->NlsData
, sizeof(NLS_DATA_BLOCK
));
68 *OutLoaderBlock
= LoaderBlock
;
73 WinLdrInitializePhase1(PLOADER_PARAMETER_BLOCK LoaderBlock
,
78 /* Examples of correct options and paths */
79 //CHAR Options[] = "/DEBUGPORT=COM1 /BAUDRATE=115200";
80 //CHAR Options[] = "/NODEBUG";
81 //CHAR SystemRoot[] = "\\WINNT\\";
82 //CHAR ArcBoot[] = "multi(0)disk(0)rdisk(0)partition(1)";
84 CHAR HalPath
[] = "\\";
87 ULONG i
, PathSeparator
;
88 PLOADER_PARAMETER_EXTENSION Extension
;
90 LoaderBlock
->u
.I386
.CommonDataArea
= NULL
; // Force No ABIOS support
92 /* Construct SystemRoot and ArcBoot from SystemPath */
93 PathSeparator
= strstr(SystemPath
, "\\") - SystemPath
;
94 strncpy(ArcBoot
, SystemPath
, PathSeparator
);
95 ArcBoot
[PathSeparator
] = 0;
96 strcpy(SystemRoot
, &SystemPath
[PathSeparator
]);
97 strcat(SystemRoot
, "\\");
99 DbgPrint((DPRINT_WINDOWS
, "ArcBoot: %s\n", ArcBoot
));
100 DbgPrint((DPRINT_WINDOWS
, "SystemRoot: %s\n", SystemRoot
));
101 DbgPrint((DPRINT_WINDOWS
, "Options: %s\n", Options
));
103 /* Fill Arc BootDevice */
104 LoaderBlock
->ArcBootDeviceName
= MmHeapAlloc(strlen(ArcBoot
)+1);
105 strcpy(LoaderBlock
->ArcBootDeviceName
, ArcBoot
);
106 LoaderBlock
->ArcBootDeviceName
= PaToVa(LoaderBlock
->ArcBootDeviceName
);
108 /* Fill Arc HalDevice, it matches ArcBoot path */
109 LoaderBlock
->ArcHalDeviceName
= MmHeapAlloc(strlen(ArcBoot
)+1);
110 strcpy(LoaderBlock
->ArcHalDeviceName
, ArcBoot
);
111 LoaderBlock
->ArcHalDeviceName
= PaToVa(LoaderBlock
->ArcHalDeviceName
);
113 /* Fill SystemRoot */
114 LoaderBlock
->NtBootPathName
= MmHeapAlloc(strlen(SystemRoot
)+1);
115 strcpy(LoaderBlock
->NtBootPathName
, SystemRoot
);
116 LoaderBlock
->NtBootPathName
= PaToVa(LoaderBlock
->NtBootPathName
);
118 /* Fill NtHalPathName */
119 LoaderBlock
->NtHalPathName
= MmHeapAlloc(strlen(HalPath
)+1);
120 strcpy(LoaderBlock
->NtHalPathName
, HalPath
);
121 LoaderBlock
->NtHalPathName
= PaToVa(LoaderBlock
->NtHalPathName
);
123 /* Fill load options */
124 LoaderBlock
->LoadOptions
= MmHeapAlloc(strlen(Options
)+1);
125 strcpy(LoaderBlock
->LoadOptions
, Options
);
126 LoaderBlock
->LoadOptions
= PaToVa(LoaderBlock
->LoadOptions
);
129 LoaderBlock
->ArcDiskInformation
= (PARC_DISK_INFORMATION
)MmHeapAlloc(sizeof(ARC_DISK_INFORMATION
));
130 InitializeListHead(&LoaderBlock
->ArcDiskInformation
->DiskSignatureListHead
);
132 /* Convert ARC disk information from freeldr to a correct format */
133 for (i
= 0; i
< reactos_disk_count
; i
++)
135 PARC_DISK_SIGNATURE ArcDiskInfo
;
137 /* Get the ARC structure */
138 ArcDiskInfo
= (PARC_DISK_SIGNATURE
)MmHeapAlloc(sizeof(ARC_DISK_SIGNATURE
));
139 RtlZeroMemory(ArcDiskInfo
, sizeof(ARC_DISK_SIGNATURE
));
141 /* Copy the data over */
142 ArcDiskInfo
->Signature
= reactos_arc_disk_info
[i
].Signature
;
143 ArcDiskInfo
->CheckSum
= reactos_arc_disk_info
[i
].CheckSum
;
145 /* Copy the ARC Name */
146 ArcDiskInfo
->ArcName
= (PCHAR
)MmHeapAlloc(sizeof(CHAR
)*256);
147 strcpy(ArcDiskInfo
->ArcName
, reactos_arc_disk_info
[i
].ArcName
);
148 ArcDiskInfo
->ArcName
= (PCHAR
)PaToVa(ArcDiskInfo
->ArcName
);
150 /* Mark partition table as valid */
151 ArcDiskInfo
->ValidPartitionTable
= TRUE
;
153 /* Insert into the list */
154 InsertTailList(&LoaderBlock
->ArcDiskInformation
->DiskSignatureListHead
,
155 &ArcDiskInfo
->ListEntry
);
158 /* Convert all list's to Virtual address */
160 /* Convert the ArcDisks list to virtual address */
161 List_PaToVa(&LoaderBlock
->ArcDiskInformation
->DiskSignatureListHead
);
162 LoaderBlock
->ArcDiskInformation
= PaToVa(LoaderBlock
->ArcDiskInformation
);
164 /* Convert configuration entries to VA */
165 ConvertConfigToVA(LoaderBlock
->ConfigurationRoot
);
166 LoaderBlock
->ConfigurationRoot
= PaToVa(LoaderBlock
->ConfigurationRoot
);
168 /* Convert all DTE into virtual addresses */
169 List_PaToVa(&LoaderBlock
->LoadOrderListHead
);
171 /* this one will be converted right before switching to
172 virtual paging mode */
173 //List_PaToVa(&LoaderBlock->MemoryDescriptorListHead);
175 /* Convert list of boot drivers */
176 List_PaToVa(&LoaderBlock
->BootDriverListHead
);
178 /* Initialize Extension now */
179 Extension
= MmHeapAlloc(sizeof(LOADER_PARAMETER_EXTENSION
));
180 if (Extension
== NULL
)
182 UiMessageBox("Failed to allocate LPB Extension!");
185 RtlZeroMemory(Extension
, sizeof(LOADER_PARAMETER_EXTENSION
));
187 /* Save size and version information */
188 Extension
->Size
= sizeof(LOADER_PARAMETER_EXTENSION
);
189 Extension
->MajorVersion
= (VersionToBoot
& 0xFF00) >> 8;
190 Extension
->MinorVersion
= VersionToBoot
& 0xFF;
193 LoaderBlock
->Extension
= PaToVa(Extension
);
196 // Last step before going virtual
197 void WinLdrSetupForNt(PLOADER_PARAMETER_BLOCK LoaderBlock
,
206 ULONG BlockSize
, NumPages
;
208 LoaderBlock
->u
.I386
.CommonDataArea
= NULL
;//CommonDataArea;
209 //LoaderBlock->u.I386.MachineType = MachineType; //FIXME: MachineType?
211 /* Allocate 2 pages for PCR */
212 Pcr
= (ULONG_PTR
)MmAllocateMemoryWithType(2 * MM_PAGE_SIZE
, LoaderStartupPcrPage
);
213 *PcrBasePage
= Pcr
>> MM_PAGE_SHIFT
;
217 UiMessageBox("Can't allocate PCR\n");
222 TssSize
= (sizeof(KTSS
) + MM_PAGE_SIZE
) & ~(MM_PAGE_SIZE
- 1);
223 TssPages
= TssSize
/ MM_PAGE_SIZE
;
225 Tss
= (ULONG_PTR
)MmAllocateMemoryWithType(TssSize
, LoaderMemoryData
);
227 *TssBasePage
= Tss
>> MM_PAGE_SHIFT
;
229 /* Allocate space for new GDT + IDT */
230 BlockSize
= NUM_GDT
*sizeof(KGDTENTRY
) + NUM_IDT
*sizeof(KIDTENTRY
);//FIXME: Use GDT/IDT limits here?
231 NumPages
= (BlockSize
+ MM_PAGE_SIZE
- 1) >> MM_PAGE_SHIFT
;
232 *GdtIdt
= (PKGDTENTRY
)MmAllocateMemoryWithType(NumPages
* MM_PAGE_SIZE
, LoaderMemoryData
);
236 UiMessageBox("Can't allocate pages for GDT+IDT!\n");
240 /* Zero newly prepared GDT+IDT */
241 RtlZeroMemory(*GdtIdt
, NumPages
<< MM_PAGE_SHIFT
);
245 WinLdrLoadDeviceDriver(PLOADER_PARAMETER_BLOCK LoaderBlock
,
247 PUNICODE_STRING FilePath
,
249 PLDR_DATA_TABLE_ENTRY
*DriverDTE
)
252 CHAR DriverPath
[1024];
258 // Separate the path to file name and directory path
259 sprintf(DriverPath
, "%wZ", FilePath
);
260 DriverNamePos
= strrchr(DriverPath
, '\\');
261 if (DriverNamePos
!= NULL
)
264 strcpy(DllName
, DriverNamePos
+1);
266 // Cut out the name from the path
267 *(DriverNamePos
+1) = 0;
270 DbgPrint((DPRINT_WINDOWS
, "DriverPath: %s, DllName: %s, LPB %p\n", DriverPath
, DllName
, LoaderBlock
));
273 // Check if driver is already loaded
274 Status
= WinLdrCheckForLoadedDll(LoaderBlock
, DllName
, DriverDTE
);
277 // We've got the pointer to its DTE, just return success
281 // It's not loaded, we have to load it
282 sprintf(FullPath
,"%s%wZ", BootPath
, FilePath
);
283 Status
= WinLdrLoadImage(FullPath
, LoaderBootDriver
, &DriverBase
);
287 // Allocate a DTE for it
288 Status
= WinLdrAllocateDataTableEntry(LoaderBlock
, DllName
, DllName
, DriverBase
, DriverDTE
);
291 DbgPrint((DPRINT_WINDOWS
, "WinLdrAllocateDataTableEntry() failed\n"));
295 // Modify any flags, if needed
296 (*DriverDTE
)->Flags
|= Flags
;
298 // Look for any dependencies it may have, and load them too
299 sprintf(FullPath
,"%s%s", BootPath
, DriverPath
);
300 Status
= WinLdrScanImportDescriptorTable(LoaderBlock
, FullPath
, *DriverDTE
);
303 DbgPrint((DPRINT_WINDOWS
, "WinLdrScanImportDescriptorTable() failed for %s\n",
312 WinLdrLoadBootDrivers(PLOADER_PARAMETER_BLOCK LoaderBlock
,
316 PBOOT_DRIVER_LIST_ENTRY BootDriver
;
319 // Walk through the boot drivers list
320 NextBd
= LoaderBlock
->BootDriverListHead
.Flink
;
322 while (NextBd
!= &LoaderBlock
->BootDriverListHead
)
324 BootDriver
= CONTAINING_RECORD(NextBd
, BOOT_DRIVER_LIST_ENTRY
, ListEntry
);
326 DbgPrint((DPRINT_WINDOWS
, "BootDriver %wZ DTE %08X RegPath: %wZ\n", &BootDriver
->FilePath
,
327 BootDriver
->DataTableEntry
, &BootDriver
->RegistryPath
));
329 // Paths are relative (FIXME: Are they always relative?)
332 Status
= WinLdrLoadDeviceDriver(LoaderBlock
, BootPath
, &BootDriver
->FilePath
,
333 0, &BootDriver
->DataTableEntry
);
335 // If loading failed - cry loudly
336 //FIXME: Maybe remove it from the list and try to continue?
339 UiMessageBox("Can't load boot driver!");
343 // Convert the RegistryPath and DTE addresses to VA since we are not going to use it anymore
344 BootDriver
->RegistryPath
.Buffer
= PaToVa(BootDriver
->RegistryPath
.Buffer
);
345 BootDriver
->DataTableEntry
= PaToVa(BootDriver
->DataTableEntry
);
347 NextBd
= BootDriver
->ListEntry
.Flink
;
354 LoadAndBootWindows(PCSTR OperatingSystemName
, WORD OperatingSystemVersion
)
357 CHAR SystemPath
[512], SearchPath
[512];
360 CHAR BootOptions
[256];
361 PVOID NtosBase
= NULL
, HalBase
= NULL
, KdComBase
= NULL
;
365 PLOADER_PARAMETER_BLOCK LoaderBlock
, LoaderBlockVA
;
366 KERNEL_ENTRY_POINT KiSystemStartup
;
367 PLDR_DATA_TABLE_ENTRY KernelDTE
, HalDTE
, KdComDTE
= NULL
;
373 //sprintf(MsgBuffer,"Booting Microsoft(R) Windows(R) OS version '%04x' is not implemented yet", OperatingSystemVersion);
374 //UiMessageBox(MsgBuffer);
376 // Open the operating system section
377 // specified in the .ini file
378 if (!IniOpenSection(OperatingSystemName
, &SectionId
))
380 sprintf(MsgBuffer
,"Operating System section '%s' not found in freeldr.ini", OperatingSystemName
);
381 UiMessageBox(MsgBuffer
);
386 UiDrawStatusText("Detecting Hardware...");
387 UiDrawProgressBarCenter(1, 100, "Loading Windows...");
389 /* Make sure the system path is set in the .ini file */
390 if (!IniReadSettingByName(SectionId
, "SystemPath", SystemPath
, sizeof(SystemPath
)))
392 UiMessageBox("System path not specified for selected operating system.");
396 /* Read booting options */
397 if (!IniReadSettingByName(SectionId
, "Options", BootOptions
, sizeof(BootOptions
)))
399 /* Nothing read, make the string empty */
400 strcpy(BootOptions
, "");
403 /* Normalize system path */
404 if (!MachDiskNormalizeSystemPath(SystemPath
, sizeof(SystemPath
)))
406 UiMessageBox("Invalid system path");
410 /* Let user know we started loading */
411 UiDrawStatusText("Loading...");
413 /* Try to open system drive */
414 BootDevice
= 0xffffffff;
415 if (!FsOpenSystemVolume(SystemPath
, BootPath
, &BootDevice
))
417 UiMessageBox("Failed to open boot drive.");
421 /* append a backslash */
422 if ((strlen(BootPath
)==0) ||
423 BootPath
[strlen(BootPath
)] != '\\')
424 strcat(BootPath
, "\\");
426 DbgPrint((DPRINT_WINDOWS
,"SystemRoot: '%s'\n", BootPath
));
428 /* Allocate and minimalistic-initialize LPB */
429 AllocateAndInitLPB(&LoaderBlock
);
431 /* Detect hardware */
432 LoaderBlock
->ConfigurationRoot
= MachHwDetect();
435 strcpy(FileName
, BootPath
);
436 strcat(FileName
, "SYSTEM32\\NTOSKRNL.EXE");
437 Status
= WinLdrLoadImage(FileName
, LoaderSystemCode
, &NtosBase
);
438 DbgPrint((DPRINT_WINDOWS
, "Ntos loaded with status %d at %p\n", Status
, NtosBase
));
441 strcpy(FileName
, BootPath
);
442 strcat(FileName
, "SYSTEM32\\HAL.DLL");
443 Status
= WinLdrLoadImage(FileName
, LoaderHalCode
, &HalBase
);
444 DbgPrint((DPRINT_WINDOWS
, "HAL loaded with status %d at %p\n", Status
, HalBase
));
446 /* Load kernel-debugger support dll */
447 if (OperatingSystemVersion
> _WIN32_WINNT_WIN2K
)
449 strcpy(FileName
, BootPath
);
450 strcat(FileName
, "SYSTEM32\\KDCOM.DLL");
451 Status
= WinLdrLoadImage(FileName
, LoaderBootDriver
, &KdComBase
);
452 DbgPrint((DPRINT_WINDOWS
, "KdCom loaded with status %d at %p\n", Status
, KdComBase
));
455 /* Allocate data table entries for above-loaded modules */
456 WinLdrAllocateDataTableEntry(LoaderBlock
, "ntoskrnl.exe",
457 "WINNT\\SYSTEM32\\NTOSKRNL.EXE", NtosBase
, &KernelDTE
);
458 WinLdrAllocateDataTableEntry(LoaderBlock
, "hal.dll",
459 "WINNT\\SYSTEM32\\HAL.DLL", HalBase
, &HalDTE
);
460 if (OperatingSystemVersion
> _WIN32_WINNT_WIN2K
)
462 WinLdrAllocateDataTableEntry(LoaderBlock
, "kdcom.dll",
463 "WINNT\\SYSTEM32\\KDCOM.DLL", KdComBase
, &KdComDTE
);
466 /* Load all referenced DLLs for kernel, HAL and kdcom.dll */
467 strcpy(SearchPath
, BootPath
);
468 strcat(SearchPath
, "SYSTEM32\\");
469 WinLdrScanImportDescriptorTable(LoaderBlock
, SearchPath
, KernelDTE
);
470 WinLdrScanImportDescriptorTable(LoaderBlock
, SearchPath
, HalDTE
);
472 WinLdrScanImportDescriptorTable(LoaderBlock
, SearchPath
, KdComDTE
);
474 /* Load Hive, and then NLS data, OEM font, and prepare boot drivers list */
475 Status
= WinLdrLoadAndScanSystemHive(LoaderBlock
, BootPath
);
476 DbgPrint((DPRINT_WINDOWS
, "SYSTEM hive loaded and scanned with status %d\n", Status
));
478 /* Load boot drivers */
479 Status
= WinLdrLoadBootDrivers(LoaderBlock
, BootPath
);
480 DbgPrint((DPRINT_WINDOWS
, "Boot drivers loaded with status %d\n", Status
));
482 /* Initialize Phase 1 - no drivers loading anymore */
483 WinLdrInitializePhase1(LoaderBlock
, BootOptions
, SystemPath
, OperatingSystemVersion
);
485 /* Alloc PCR, TSS, do magic things with the GDT/IDT */
486 WinLdrSetupForNt(LoaderBlock
, &GdtIdt
, &PcrBasePage
, &TssBasePage
);
488 /* Save entry-point pointer and Loader block VAs */
489 KiSystemStartup
= (KERNEL_ENTRY_POINT
)KernelDTE
->EntryPoint
;
490 LoaderBlockVA
= PaToVa(LoaderBlock
);
492 /* "Stop all motors", change videomode */
493 DiskStopFloppyMotor();
494 if (OperatingSystemVersion
< _WIN32_WINNT_WIN2K
)
495 MachVideoPrepareForReactOS(TRUE
);
497 MachVideoPrepareForReactOS(FALSE
);
500 //DumpMemoryAllocMap();
502 /* Turn on paging mode of CPU*/
503 WinLdrTurnOnPaging(LoaderBlock
, PcrBasePage
, TssBasePage
, GdtIdt
);
505 DbgPrint((DPRINT_WINDOWS
, "Hello from paged mode, KiSystemStartup %p, LoaderBlockVA %p!\n",
506 KiSystemStartup
, LoaderBlockVA
));
508 WinLdrpDumpMemoryDescriptors(LoaderBlockVA
);
509 WinLdrpDumpBootDriver(LoaderBlockVA
);
510 WinLdrpDumpArcDisks(LoaderBlockVA
);
512 //FIXME: If I substitute this debugging checkpoint, GCC will "optimize away" the code below
514 /*asm(".intel_syntax noprefix\n");
517 asm(".att_syntax\n");*/
520 (*KiSystemStartup
)(LoaderBlockVA
);
526 WinLdrpDumpMemoryDescriptors(PLOADER_PARAMETER_BLOCK LoaderBlock
)
529 PMEMORY_ALLOCATION_DESCRIPTOR MemoryDescriptor
;
531 NextMd
= LoaderBlock
->MemoryDescriptorListHead
.Flink
;
533 while (NextMd
!= &LoaderBlock
->MemoryDescriptorListHead
)
535 MemoryDescriptor
= CONTAINING_RECORD(NextMd
, MEMORY_ALLOCATION_DESCRIPTOR
, ListEntry
);
537 DbgPrint((DPRINT_WINDOWS
, "BP %08X PC %04X MT %d\n", MemoryDescriptor
->BasePage
,
538 MemoryDescriptor
->PageCount
, MemoryDescriptor
->MemoryType
));
540 NextMd
= MemoryDescriptor
->ListEntry
.Flink
;
545 WinLdrpDumpBootDriver(PLOADER_PARAMETER_BLOCK LoaderBlock
)
548 PBOOT_DRIVER_LIST_ENTRY BootDriver
;
550 NextBd
= LoaderBlock
->BootDriverListHead
.Flink
;
552 while (NextBd
!= &LoaderBlock
->BootDriverListHead
)
554 BootDriver
= CONTAINING_RECORD(NextBd
, BOOT_DRIVER_LIST_ENTRY
, ListEntry
);
556 DbgPrint((DPRINT_WINDOWS
, "BootDriver %wZ DTE %08X RegPath: %wZ\n", &BootDriver
->FilePath
,
557 BootDriver
->DataTableEntry
, &BootDriver
->RegistryPath
));
559 NextBd
= BootDriver
->ListEntry
.Flink
;
564 WinLdrpDumpArcDisks(PLOADER_PARAMETER_BLOCK LoaderBlock
)
567 PARC_DISK_SIGNATURE ArcDisk
;
569 NextBd
= LoaderBlock
->ArcDiskInformation
->DiskSignatureListHead
.Flink
;
571 while (NextBd
!= &LoaderBlock
->ArcDiskInformation
->DiskSignatureListHead
)
573 ArcDisk
= CONTAINING_RECORD(NextBd
, ARC_DISK_SIGNATURE
, ListEntry
);
575 DbgPrint((DPRINT_WINDOWS
, "ArcDisk %s checksum: 0x%X, signature: 0x%X\n",
576 ArcDisk
->ArcName
, ArcDisk
->CheckSum
, ArcDisk
->Signature
));
578 NextBd
= ArcDisk
->ListEntry
.Flink
;