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];
32 ARC_DISK_SIGNATURE BldrDiskInfo
[32];
33 CHAR BldrArcNames
[32][256];
36 WinLdrCheckForLoadedDll(IN OUT PLOADER_PARAMETER_BLOCK WinLdrBlock
,
38 OUT PLDR_DATA_TABLE_ENTRY
*LoadedEntry
);
41 VOID
DumpMemoryAllocMap(VOID
);
42 VOID
WinLdrpDumpMemoryDescriptors(PLOADER_PARAMETER_BLOCK LoaderBlock
);
43 VOID
WinLdrpDumpBootDriver(PLOADER_PARAMETER_BLOCK LoaderBlock
);
44 VOID
WinLdrpDumpArcDisks(PLOADER_PARAMETER_BLOCK LoaderBlock
);
47 void InitializeHWConfig(IN OUT PLOADER_PARAMETER_BLOCK LoaderBlock
)
49 PCONFIGURATION_COMPONENT_DATA ConfigurationRoot
;
50 PCONFIGURATION_COMPONENT Component
;
51 PCONFIGURATION_COMPONENT_DATA
/*CurrentEntry,*/ PreviousEntry
, AdapterEntry
;
52 BOOLEAN IsNextEntryChild
;
54 DbgPrint((DPRINT_WINDOWS
, "InitializeHWConfig()\n"));
56 LoaderBlock
->ConfigurationRoot
= MmAllocateMemory(sizeof(CONFIGURATION_COMPONENT_DATA
));
57 RtlZeroMemory(LoaderBlock
->ConfigurationRoot
, sizeof(CONFIGURATION_COMPONENT_DATA
));
59 /* Fill root == SystemClass */
60 ConfigurationRoot
= LoaderBlock
->ConfigurationRoot
;
61 Component
= &LoaderBlock
->ConfigurationRoot
->ComponentEntry
;
63 Component
->Class
= SystemClass
;
64 Component
->Type
= MaximumType
;
65 Component
->Version
= 0; // FIXME: ?
67 Component
->AffinityMask
= 0;
69 IsNextEntryChild
= TRUE
;
70 PreviousEntry
= ConfigurationRoot
;
72 /* Enumerate all PCI buses */
73 AdapterEntry
= ConfigurationRoot
;
75 /* TODO: Disk Geometry */
78 /* TODO: Serial port */
80 //Config->ConfigurationData = alloc(sizeof(CONFIGURATION_COMPONENT_DATA), EfiLoaderData);
82 /* Convert everything to VA */
83 ConvertConfigToVA(LoaderBlock
->ConfigurationRoot
);
84 LoaderBlock
->ConfigurationRoot
= PaToVa(LoaderBlock
->ConfigurationRoot
);
90 AllocateAndInitLPB(PLOADER_PARAMETER_BLOCK
*OutLoaderBlock
)
92 PLOADER_PARAMETER_BLOCK LoaderBlock
;
94 /* Allocate and zero-init the LPB */
95 LoaderBlock
= MmAllocateMemory(sizeof(LOADER_PARAMETER_BLOCK
));
96 RtlZeroMemory(LoaderBlock
, sizeof(LOADER_PARAMETER_BLOCK
));
98 /* Init three critical lists, used right away */
99 InitializeListHead(&LoaderBlock
->LoadOrderListHead
);
100 InitializeListHead(&LoaderBlock
->MemoryDescriptorListHead
);
101 InitializeListHead(&LoaderBlock
->BootDriverListHead
);
103 /* Alloc space for NLS (it will be converted to VA in WinLdrLoadNLS) */
104 LoaderBlock
->NlsData
= MmAllocateMemory(sizeof(NLS_DATA_BLOCK
));
105 if (LoaderBlock
->NlsData
== NULL
)
107 UiMessageBox("Failed to allocate memory for NLS table data!");
110 RtlZeroMemory(LoaderBlock
->NlsData
, sizeof(NLS_DATA_BLOCK
));
112 *OutLoaderBlock
= LoaderBlock
;
117 WinLdrInitializePhase1(PLOADER_PARAMETER_BLOCK LoaderBlock
)
119 //CHAR Options[] = "/CRASHDEBUG /DEBUGPORT=COM1 /BAUDRATE=115200";
120 CHAR Options
[] = "/NODEBUG";
121 CHAR SystemRoot
[] = "\\WINNT\\";
122 CHAR HalPath
[] = "\\";
123 CHAR ArcBoot
[] = "multi(0)disk(0)rdisk(1)partition(1)";
124 CHAR ArcHal
[] = "multi(0)disk(0)rdisk(1)partition(1)";
127 PLOADER_PARAMETER_EXTENSION Extension
;
129 LoaderBlock
->u
.I386
.CommonDataArea
= NULL
; // Force No ABIOS support
131 /* Fill Arc BootDevice */
132 LoaderBlock
->ArcBootDeviceName
= MmAllocateMemory(strlen(ArcBoot
)+1);
133 strcpy(LoaderBlock
->ArcBootDeviceName
, ArcBoot
);
134 LoaderBlock
->ArcBootDeviceName
= PaToVa(LoaderBlock
->ArcBootDeviceName
);
136 /* Fill Arc HalDevice */
137 LoaderBlock
->ArcHalDeviceName
= MmAllocateMemory(strlen(ArcHal
)+1);
138 strcpy(LoaderBlock
->ArcHalDeviceName
, ArcHal
);
139 LoaderBlock
->ArcHalDeviceName
= PaToVa(LoaderBlock
->ArcHalDeviceName
);
141 /* Fill SystemRoot */
142 LoaderBlock
->NtBootPathName
= MmAllocateMemory(strlen(SystemRoot
)+1);
143 strcpy(LoaderBlock
->NtBootPathName
, SystemRoot
);
144 LoaderBlock
->NtBootPathName
= PaToVa(LoaderBlock
->NtBootPathName
);
146 /* Fill NtHalPathName */
147 LoaderBlock
->NtHalPathName
= MmAllocateMemory(strlen(HalPath
)+1);
148 strcpy(LoaderBlock
->NtHalPathName
, HalPath
);
149 LoaderBlock
->NtHalPathName
= PaToVa(LoaderBlock
->NtHalPathName
);
151 /* Fill load options */
152 LoaderBlock
->LoadOptions
= MmAllocateMemory(strlen(Options
)+1);
153 strcpy(LoaderBlock
->LoadOptions
, Options
);
154 LoaderBlock
->LoadOptions
= PaToVa(LoaderBlock
->LoadOptions
);
157 LoaderBlock
->ArcDiskInformation
= (PARC_DISK_INFORMATION
)MmAllocateMemory(sizeof(ARC_DISK_INFORMATION
));
158 InitializeListHead(&LoaderBlock
->ArcDiskInformation
->DiskSignatureListHead
);
160 /* Convert ARC disk information from freeldr to a correct format */
161 for (i
= 0; i
< reactos_disk_count
; i
++)
163 PARC_DISK_SIGNATURE ArcDiskInfo
;
165 /* Get the ARC structure */
166 ArcDiskInfo
= &BldrDiskInfo
[i
];
168 /* Copy the data over */
169 ArcDiskInfo
->Signature
= reactos_arc_disk_info
[i
].Signature
;
170 ArcDiskInfo
->CheckSum
= reactos_arc_disk_info
[i
].CheckSum
;
172 /* Copy the ARC Name */
173 strcpy(BldrArcNames
[i
], reactos_arc_disk_info
[i
].ArcName
);
174 ArcDiskInfo
->ArcName
= BldrArcNames
[i
];
176 /* Insert into the list */
177 InsertTailList(&LoaderBlock
->ArcDiskInformation
->DiskSignatureListHead
,
178 &ArcDiskInfo
->ListEntry
);
181 /* Convert the list to virtual address */
182 List_PaToVa(&LoaderBlock
->ArcDiskInformation
->DiskSignatureListHead
);
183 LoaderBlock
->ArcDiskInformation
= PaToVa(LoaderBlock
->ArcDiskInformation
);
185 /* Create configuration entries */
186 InitializeHWConfig(LoaderBlock
);
188 /* Convert all DTE into virtual addresses */
191 /* Convert all list's to Virtual address */
192 List_PaToVa(&LoaderBlock
->LoadOrderListHead
);
194 /* this one will be converted right before switching to
195 virtual paging mode */
196 //List_PaToVa(&LoaderBlock->MemoryDescriptorListHead);
198 List_PaToVa(&LoaderBlock
->BootDriverListHead
);
200 /* Initialize Extension now */
201 Extension
= MmAllocateMemory(sizeof(LOADER_PARAMETER_EXTENSION
));
202 if (Extension
== NULL
)
204 UiMessageBox("Failed to allocate LPB Extension!");
207 RtlZeroMemory(Extension
, sizeof(LOADER_PARAMETER_EXTENSION
));
209 Extension
->Size
= sizeof(LOADER_PARAMETER_EXTENSION
);
210 Extension
->MajorVersion
= 4;
211 Extension
->MinorVersion
= 0;
214 LoaderBlock
->Extension
= PaToVa(Extension
);
217 // Last step before going virtual
218 void WinLdrSetupForNt(PLOADER_PARAMETER_BLOCK LoaderBlock
,
227 ULONG BlockSize
, NumPages
;
229 LoaderBlock
->u
.I386
.CommonDataArea
= NULL
;//CommonDataArea;
230 //LoaderBlock->u.I386.MachineType = MachineType; //FIXME: MachineType?
232 /* Allocate 2 pages for PCR */
233 Pcr
= (ULONG_PTR
)MmAllocateMemory(2 * MM_PAGE_SIZE
);
234 *PcrBasePage
= Pcr
>> MM_PAGE_SHIFT
;
238 UiMessageBox("Can't allocate PCR\n");
243 TssSize
= (sizeof(KTSS
) + MM_PAGE_SIZE
) & ~(MM_PAGE_SIZE
- 1);
244 TssPages
= TssSize
/ MM_PAGE_SIZE
;
246 Tss
= (ULONG_PTR
)MmAllocateMemory(TssSize
);
248 *TssBasePage
= Tss
>> MM_PAGE_SHIFT
;
250 /* Allocate space for new GDT + IDT */
251 BlockSize
= NUM_GDT
*sizeof(KGDTENTRY
) + NUM_IDT
*sizeof(KIDTENTRY
);//FIXME: Use GDT/IDT limits here?
252 NumPages
= (BlockSize
+ MM_PAGE_SIZE
- 1) >> MM_PAGE_SHIFT
;
253 *GdtIdt
= (PKGDTENTRY
)MmAllocateMemory(NumPages
* MM_PAGE_SIZE
);
257 UiMessageBox("Can't allocate pages for GDT+IDT!\n");
261 /* Zero newly prepared GDT+IDT */
262 RtlZeroMemory(*GdtIdt
, NumPages
<< MM_PAGE_SHIFT
);
266 WinLdrLoadDeviceDriver(PLOADER_PARAMETER_BLOCK LoaderBlock
,
268 PUNICODE_STRING FilePath
,
270 PLDR_DATA_TABLE_ENTRY
*DriverDTE
)
273 CHAR DriverPath
[1024];
279 // Separate the path to file name and directory path
280 sprintf(DriverPath
, "%wZ", FilePath
);
281 DriverNamePos
= strrchr(DriverPath
, '\\');
282 if (DriverNamePos
!= NULL
)
285 strcpy(DllName
, DriverNamePos
+1);
287 // Cut out the name from the path
288 *(DriverNamePos
+1) = 0;
291 DbgPrint((DPRINT_WINDOWS
, "DriverPath: %s, DllName: %s, LPB %p\n", DriverPath
, DllName
, LoaderBlock
));
294 // Check if driver is already loaded
295 Status
= WinLdrCheckForLoadedDll(LoaderBlock
, DllName
, DriverDTE
);
298 // We've got the pointer to its DTE, just return success
302 // It's not loaded, we have to load it
303 sprintf(FullPath
,"%s%wZ", BootPath
, FilePath
);
304 Status
= WinLdrLoadImage(FullPath
, &DriverBase
);
308 // Allocate a DTE for it
309 Status
= WinLdrAllocateDataTableEntry(LoaderBlock
, DllName
, DllName
, DriverBase
, DriverDTE
);
312 DbgPrint((DPRINT_WINDOWS
, "WinLdrAllocateDataTableEntry() failed\n"));
316 // Modify any flags, if needed
317 (*DriverDTE
)->Flags
|= Flags
;
319 // Look for any dependencies it may have, and load them too
320 sprintf(FullPath
,"%s%s", BootPath
, DriverPath
);
321 Status
= WinLdrScanImportDescriptorTable(LoaderBlock
, FullPath
, *DriverDTE
);
324 DbgPrint((DPRINT_WINDOWS
, "WinLdrScanImportDescriptorTable() failed for %s\n",
333 WinLdrLoadBootDrivers(PLOADER_PARAMETER_BLOCK LoaderBlock
,
337 PBOOT_DRIVER_LIST_ENTRY BootDriver
;
340 // Walk through the boot drivers list
341 NextBd
= LoaderBlock
->BootDriverListHead
.Flink
;
343 while (NextBd
!= &LoaderBlock
->BootDriverListHead
)
345 BootDriver
= CONTAINING_RECORD(NextBd
, BOOT_DRIVER_LIST_ENTRY
, ListEntry
);
347 //DbgPrint((DPRINT_WINDOWS, "BootDriver %wZ DTE %08X RegPath: %wZ\n", &BootDriver->FilePath,
348 // BootDriver->DataTableEntry, &BootDriver->RegistryPath));
350 // Paths are relative (FIXME: Are they always relative?)
353 Status
= WinLdrLoadDeviceDriver(LoaderBlock
, BootPath
, &BootDriver
->FilePath
,
354 0, &BootDriver
->DataTableEntry
);
356 // If loading failed - cry loudly
357 //FIXME: Maybe remove it from the list and try to continue?
360 UiMessageBox("Can't load boot driver!");
364 NextBd
= BootDriver
->ListEntry
.Flink
;
371 LoadAndBootWindows(PCSTR OperatingSystemName
, WORD OperatingSystemVersion
)
374 CHAR SystemPath
[1024], SearchPath
[1024];
377 PVOID NtosBase
= NULL
, HalBase
= NULL
, KdComBase
= NULL
;
381 PLOADER_PARAMETER_BLOCK LoaderBlock
, LoaderBlockVA
;
382 KERNEL_ENTRY_POINT KiSystemStartup
;
383 PLDR_DATA_TABLE_ENTRY KernelDTE
, HalDTE
, KdComDTE
= NULL
;
389 //sprintf(MsgBuffer,"Booting Microsoft(R) Windows(R) OS version '%04x' is not implemented yet", OperatingSystemVersion);
390 //UiMessageBox(MsgBuffer);
392 // Open the operating system section
393 // specified in the .ini file
394 if (!IniOpenSection(OperatingSystemName
, &SectionId
))
396 sprintf(MsgBuffer
,"Operating System section '%s' not found in freeldr.ini", OperatingSystemName
);
397 UiMessageBox(MsgBuffer
);
402 UiDrawStatusText("Detecting Hardware...");
403 UiDrawProgressBarCenter(1, 100, "Loading Windows...");
405 //FIXME: This is needed only for MachHwDetect() which performs registry operations!
406 RegInitializeRegistry();
408 /* Make sure the system path is set in the .ini file */
409 if (!IniReadSettingByName(SectionId
, "SystemPath", SystemPath
, sizeof(SystemPath
)))
411 UiMessageBox("System path not specified for selected operating system.");
415 if (!MachDiskNormalizeSystemPath(SystemPath
,
418 UiMessageBox("Invalid system path");
422 /* Detect hardware */
425 UiDrawStatusText("Loading...");
427 /* Try to open system drive */
428 BootDevice
= 0xffffffff;
429 if (!FsOpenSystemVolume(SystemPath
, BootPath
, &BootDevice
))
431 UiMessageBox("Failed to open boot drive.");
435 /* append a backslash */
436 if ((strlen(BootPath
)==0) ||
437 BootPath
[strlen(BootPath
)] != '\\')
438 strcat(BootPath
, "\\");
440 DbgPrint((DPRINT_WINDOWS
,"SystemRoot: '%s'\n", BootPath
));
442 // Allocate and minimalistic-initialize LPB
443 AllocateAndInitLPB(&LoaderBlock
);
446 strcpy(FileName
, BootPath
);
447 strcat(FileName
, "SYSTEM32\\NTOSKRNL.EXE");
448 Status
= WinLdrLoadImage(FileName
, &NtosBase
);
449 DbgPrint((DPRINT_WINDOWS
, "Ntos loaded with status %d at %p\n", Status
, NtosBase
));
452 strcpy(FileName
, BootPath
);
453 strcat(FileName
, "SYSTEM32\\HAL.DLL");
454 Status
= WinLdrLoadImage(FileName
, &HalBase
);
455 DbgPrint((DPRINT_WINDOWS
, "HAL loaded with status %d at %p\n", Status
, HalBase
));
457 // Load kernel-debugger support dll
458 if (OperatingSystemVersion
> _WIN32_WINNT_NT4
)
460 strcpy(FileName
, BootPath
);
461 strcat(FileName
, "SYSTEM32\\KDCOM.DLL");
462 Status
= WinLdrLoadImage(FileName
, &KdComBase
);
463 DbgPrint((DPRINT_WINDOWS
, "KdCom loaded with status %d at %p\n", Status
, KdComBase
));
466 // Allocate data table entries for above-loaded modules
467 WinLdrAllocateDataTableEntry(LoaderBlock
, "ntoskrnl.exe",
468 "WINNT\\SYSTEM32\\NTOSKRNL.EXE", NtosBase
, &KernelDTE
);
469 WinLdrAllocateDataTableEntry(LoaderBlock
, "hal.dll",
470 "WINNT\\SYSTEM32\\HAL.DLL", HalBase
, &HalDTE
);
471 if (OperatingSystemVersion
> _WIN32_WINNT_NT4
)
473 WinLdrAllocateDataTableEntry(LoaderBlock
, "kdcom.dll",
474 "WINNT\\SYSTEM32\\KDCOM.DLL", KdComBase
, &KdComDTE
);
477 /* Load all referenced DLLs for kernel, HAL and kdcom.dll */
478 strcpy(SearchPath
, BootPath
);
479 strcat(SearchPath
, "SYSTEM32\\");
480 WinLdrScanImportDescriptorTable(LoaderBlock
, SearchPath
, KernelDTE
);
481 WinLdrScanImportDescriptorTable(LoaderBlock
, SearchPath
, HalDTE
);
483 WinLdrScanImportDescriptorTable(LoaderBlock
, SearchPath
, KdComDTE
);
485 /* Load Hive, and then NLS data, OEM font, and prepare boot drivers list */
486 Status
= WinLdrLoadAndScanSystemHive(LoaderBlock
, BootPath
);
487 DbgPrint((DPRINT_WINDOWS
, "SYSTEM hive loaded and scanned with status %d\n", Status
));
489 /* Load boot drivers */
490 Status
= WinLdrLoadBootDrivers(LoaderBlock
, BootPath
);
491 DbgPrint((DPRINT_WINDOWS
, "Boot drivers loaded with status %d\n", Status
));
493 /* Initialize Phase 1 - no drivers loading anymore */
494 WinLdrInitializePhase1(LoaderBlock
);
496 /* Alloc PCR, TSS, do magic things with the GDT/IDT */
497 WinLdrSetupForNt(LoaderBlock
, &GdtIdt
, &PcrBasePage
, &TssBasePage
);
499 /* Save entry-point pointer (VA) */
500 KiSystemStartup
= (KERNEL_ENTRY_POINT
)KernelDTE
->EntryPoint
;
502 LoaderBlockVA
= PaToVa(LoaderBlock
);
505 //DumpMemoryAllocMap();
507 /* Turn on paging mode of CPU*/
508 WinLdrTurnOnPaging(LoaderBlock
, PcrBasePage
, TssBasePage
, GdtIdt
);
510 DbgPrint((DPRINT_WINDOWS
, "Hello from paged mode, KiSystemStartup %p, LoaderBlockVA %p!\n",
511 KiSystemStartup
, LoaderBlockVA
));
513 WinLdrpDumpMemoryDescriptors(LoaderBlockVA
);
514 WinLdrpDumpBootDriver(LoaderBlockVA
);
515 WinLdrpDumpArcDisks(LoaderBlockVA
);
517 //FIXME: If I substitute this debugging checkpoint, GCC will "optimize away" the code below
519 /*asm(".intel_syntax noprefix\n");
522 asm(".att_syntax\n");*/
525 (*KiSystemStartup
)(LoaderBlockVA
);
531 WinLdrpDumpMemoryDescriptors(PLOADER_PARAMETER_BLOCK LoaderBlock
)
534 PMEMORY_ALLOCATION_DESCRIPTOR MemoryDescriptor
;
536 NextMd
= LoaderBlock
->MemoryDescriptorListHead
.Flink
;
538 while (NextMd
!= &LoaderBlock
->MemoryDescriptorListHead
)
540 MemoryDescriptor
= CONTAINING_RECORD(NextMd
, MEMORY_ALLOCATION_DESCRIPTOR
, ListEntry
);
543 DbgPrint((DPRINT_WINDOWS
, "BP %08X PC %04X MT %d\n", MemoryDescriptor
->BasePage
,
544 MemoryDescriptor
->PageCount
, MemoryDescriptor
->MemoryType
));
546 NextMd
= MemoryDescriptor
->ListEntry
.Flink
;
551 WinLdrpDumpBootDriver(PLOADER_PARAMETER_BLOCK LoaderBlock
)
554 PBOOT_DRIVER_LIST_ENTRY BootDriver
;
556 NextBd
= LoaderBlock
->BootDriverListHead
.Flink
;
558 while (NextBd
!= &LoaderBlock
->BootDriverListHead
)
560 BootDriver
= CONTAINING_RECORD(NextBd
, BOOT_DRIVER_LIST_ENTRY
, ListEntry
);
562 DbgPrint((DPRINT_WINDOWS
, "BootDriver %wZ DTE %08X RegPath: %wZ\n", &BootDriver
->FilePath
,
563 BootDriver
->DataTableEntry
, &BootDriver
->RegistryPath
));
565 NextBd
= BootDriver
->ListEntry
.Flink
;
570 WinLdrpDumpArcDisks(PLOADER_PARAMETER_BLOCK LoaderBlock
)
573 PARC_DISK_SIGNATURE ArcDisk
;
575 NextBd
= LoaderBlock
->ArcDiskInformation
->DiskSignatureListHead
.Flink
;
577 while (NextBd
!= &LoaderBlock
->ArcDiskInformation
->DiskSignatureListHead
)
579 ArcDisk
= CONTAINING_RECORD(NextBd
, ARC_DISK_SIGNATURE
, ListEntry
);
581 DbgPrint((DPRINT_WINDOWS
, "ArcDisk %s checksum: 0x%X, signature: 0x%X\n",
582 ArcDisk
->ArcName
, ArcDisk
->CheckSum
, ArcDisk
->Signature
));
584 NextBd
= ArcDisk
->ListEntry
.Flink
;