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>
29 //FIXME: Do a better way to retrieve Arc disk information
30 extern ULONG reactos_disk_count
;
31 extern ARC_DISK_SIGNATURE reactos_arc_disk_info
[];
32 extern char reactos_arc_strings
[32][256];
34 ARC_DISK_SIGNATURE BldrDiskInfo
[32];
35 CHAR BldrArcNames
[32][256];
38 WinLdrCheckForLoadedDll(IN OUT PLOADER_PARAMETER_BLOCK WinLdrBlock
,
40 OUT PLDR_DATA_TABLE_ENTRY
*LoadedEntry
);
43 VOID
DumpMemoryAllocMap(VOID
);
44 VOID
WinLdrpDumpMemoryDescriptors(PLOADER_PARAMETER_BLOCK LoaderBlock
);
45 VOID
WinLdrpDumpBootDriver(PLOADER_PARAMETER_BLOCK LoaderBlock
);
46 VOID
WinLdrpDumpArcDisks(PLOADER_PARAMETER_BLOCK LoaderBlock
);
49 void InitializeHWConfig(IN OUT PLOADER_PARAMETER_BLOCK LoaderBlock
)
51 PCONFIGURATION_COMPONENT_DATA ConfigurationRoot
;
52 PCONFIGURATION_COMPONENT Component
;
53 PCONFIGURATION_COMPONENT_DATA
/*CurrentEntry,*/ PreviousEntry
, AdapterEntry
;
54 BOOLEAN IsNextEntryChild
;
56 DbgPrint((DPRINT_WINDOWS
, "InitializeHWConfig()\n"));
58 LoaderBlock
->ConfigurationRoot
= MmAllocateMemory(sizeof(CONFIGURATION_COMPONENT_DATA
));
59 RtlZeroMemory(LoaderBlock
->ConfigurationRoot
, sizeof(CONFIGURATION_COMPONENT_DATA
));
61 /* Fill root == SystemClass */
62 ConfigurationRoot
= LoaderBlock
->ConfigurationRoot
;
63 Component
= &LoaderBlock
->ConfigurationRoot
->ComponentEntry
;
65 Component
->Class
= SystemClass
;
66 Component
->Type
= MaximumType
;
67 Component
->Version
= 0; // FIXME: ?
69 Component
->AffinityMask
= 0;
71 IsNextEntryChild
= TRUE
;
72 PreviousEntry
= ConfigurationRoot
;
74 /* Enumerate all PCI buses */
75 AdapterEntry
= ConfigurationRoot
;
77 /* TODO: Disk Geometry */
80 /* TODO: Serial port */
82 //Config->ConfigurationData = alloc(sizeof(CONFIGURATION_COMPONENT_DATA), EfiLoaderData);
84 /* Convert everything to VA */
85 ConvertConfigToVA(LoaderBlock
->ConfigurationRoot
);
86 LoaderBlock
->ConfigurationRoot
= PaToVa(LoaderBlock
->ConfigurationRoot
);
92 AllocateAndInitLPB(PLOADER_PARAMETER_BLOCK
*OutLoaderBlock
)
94 PLOADER_PARAMETER_BLOCK LoaderBlock
;
96 /* Allocate and zero-init the LPB */
97 LoaderBlock
= MmAllocateMemory(sizeof(LOADER_PARAMETER_BLOCK
));
98 RtlZeroMemory(LoaderBlock
, sizeof(LOADER_PARAMETER_BLOCK
));
100 /* Init three critical lists, used right away */
101 InitializeListHead(&LoaderBlock
->LoadOrderListHead
);
102 InitializeListHead(&LoaderBlock
->MemoryDescriptorListHead
);
103 InitializeListHead(&LoaderBlock
->BootDriverListHead
);
105 /* Alloc space for NLS (it will be converted to VA in WinLdrLoadNLS) */
106 LoaderBlock
->NlsData
= MmAllocateMemory(sizeof(NLS_DATA_BLOCK
));
107 if (LoaderBlock
->NlsData
== NULL
)
109 UiMessageBox("Failed to allocate memory for NLS table data!");
112 RtlZeroMemory(LoaderBlock
->NlsData
, sizeof(NLS_DATA_BLOCK
));
114 *OutLoaderBlock
= LoaderBlock
;
119 WinLdrInitializePhase1(PLOADER_PARAMETER_BLOCK LoaderBlock
)
121 //CHAR Options[] = "/CRASHDEBUG /DEBUGPORT=COM1 /BAUDRATE=115200";
122 CHAR Options
[] = "/NODEBUG";
123 CHAR SystemRoot
[] = "\\WINNT\\";
124 CHAR HalPath
[] = "\\";
125 CHAR ArcBoot
[] = "multi(0)disk(0)rdisk(1)partition(1)";
126 CHAR ArcHal
[] = "multi(0)disk(0)rdisk(1)partition(1)";
129 PLOADER_PARAMETER_EXTENSION Extension
;
131 LoaderBlock
->u
.I386
.CommonDataArea
= NULL
; // Force No ABIOS support
133 /* Fill Arc BootDevice */
134 LoaderBlock
->ArcBootDeviceName
= MmAllocateMemory(strlen(ArcBoot
)+1);
135 strcpy(LoaderBlock
->ArcBootDeviceName
, ArcBoot
);
136 LoaderBlock
->ArcBootDeviceName
= PaToVa(LoaderBlock
->ArcBootDeviceName
);
138 /* Fill Arc HalDevice */
139 LoaderBlock
->ArcHalDeviceName
= MmAllocateMemory(strlen(ArcHal
)+1);
140 strcpy(LoaderBlock
->ArcHalDeviceName
, ArcHal
);
141 LoaderBlock
->ArcHalDeviceName
= PaToVa(LoaderBlock
->ArcHalDeviceName
);
143 /* Fill SystemRoot */
144 LoaderBlock
->NtBootPathName
= MmAllocateMemory(strlen(SystemRoot
)+1);
145 strcpy(LoaderBlock
->NtBootPathName
, SystemRoot
);
146 LoaderBlock
->NtBootPathName
= PaToVa(LoaderBlock
->NtBootPathName
);
148 /* Fill NtHalPathName */
149 LoaderBlock
->NtHalPathName
= MmAllocateMemory(strlen(HalPath
)+1);
150 strcpy(LoaderBlock
->NtHalPathName
, HalPath
);
151 LoaderBlock
->NtHalPathName
= PaToVa(LoaderBlock
->NtHalPathName
);
153 /* Fill load options */
154 LoaderBlock
->LoadOptions
= MmAllocateMemory(strlen(Options
)+1);
155 strcpy(LoaderBlock
->LoadOptions
, Options
);
156 LoaderBlock
->LoadOptions
= PaToVa(LoaderBlock
->LoadOptions
);
159 LoaderBlock
->ArcDiskInformation
= (PARC_DISK_INFORMATION
)MmAllocateMemory(sizeof(ARC_DISK_INFORMATION
));
160 InitializeListHead(&LoaderBlock
->ArcDiskInformation
->DiskSignatureListHead
);
162 /* Convert ARC disk information from freeldr to a correct format */
163 for (i
= 0; i
< reactos_disk_count
; i
++)
165 PARC_DISK_SIGNATURE ArcDiskInfo
;
167 /* Get the ARC structure */
168 ArcDiskInfo
= &BldrDiskInfo
[i
];
170 /* Copy the data over */
171 ArcDiskInfo
->Signature
= reactos_arc_disk_info
[i
].Signature
;
172 ArcDiskInfo
->CheckSum
= reactos_arc_disk_info
[i
].CheckSum
;
174 /* Copy the ARC Name */
175 strcpy(BldrArcNames
[i
], reactos_arc_disk_info
[i
].ArcName
);
176 ArcDiskInfo
->ArcName
= BldrArcNames
[i
];
178 /* Insert into the list */
179 InsertTailList(&LoaderBlock
->ArcDiskInformation
->DiskSignatureListHead
,
180 &ArcDiskInfo
->ListEntry
);
183 /* Convert the list to virtual address */
184 List_PaToVa(&LoaderBlock
->ArcDiskInformation
->DiskSignatureListHead
);
185 LoaderBlock
->ArcDiskInformation
= PaToVa(LoaderBlock
->ArcDiskInformation
);
187 /* Create configuration entries */
188 InitializeHWConfig(LoaderBlock
);
190 /* Convert all DTE into virtual addresses */
193 /* Convert all list's to Virtual address */
194 List_PaToVa(&LoaderBlock
->LoadOrderListHead
);
196 /* this one will be converted right before switching to
197 virtual paging mode */
198 //List_PaToVa(&LoaderBlock->MemoryDescriptorListHead);
200 List_PaToVa(&LoaderBlock
->BootDriverListHead
);
202 /* Initialize Extension now */
203 Extension
= MmAllocateMemory(sizeof(LOADER_PARAMETER_EXTENSION
));
204 if (Extension
== NULL
)
206 UiMessageBox("Failed to allocate LPB Extension!");
209 RtlZeroMemory(Extension
, sizeof(LOADER_PARAMETER_EXTENSION
));
211 Extension
->Size
= sizeof(LOADER_PARAMETER_EXTENSION
);
212 Extension
->MajorVersion
= 4;
213 Extension
->MinorVersion
= 0;
216 LoaderBlock
->Extension
= PaToVa(Extension
);
219 // Last step before going virtual
220 void WinLdrSetupForNt(PLOADER_PARAMETER_BLOCK LoaderBlock
,
229 ULONG BlockSize
, NumPages
;
231 LoaderBlock
->u
.I386
.CommonDataArea
= NULL
;//CommonDataArea;
232 //LoaderBlock->u.I386.MachineType = MachineType; //FIXME: MachineType?
234 /* Allocate 2 pages for PCR */
235 Pcr
= (ULONG_PTR
)MmAllocateMemory(2 * MM_PAGE_SIZE
);
236 *PcrBasePage
= Pcr
>> MM_PAGE_SHIFT
;
240 UiMessageBox("Can't allocate PCR\n");
245 TssSize
= (sizeof(KTSS
) + MM_PAGE_SIZE
) & ~(MM_PAGE_SIZE
- 1);
246 TssPages
= TssSize
/ MM_PAGE_SIZE
;
248 Tss
= (ULONG_PTR
)MmAllocateMemory(TssSize
);
250 *TssBasePage
= Tss
>> MM_PAGE_SHIFT
;
252 /* Allocate space for new GDT + IDT */
253 BlockSize
= NUM_GDT
*sizeof(KGDTENTRY
) + NUM_IDT
*sizeof(KIDTENTRY
);//FIXME: Use GDT/IDT limits here?
254 NumPages
= (BlockSize
+ MM_PAGE_SIZE
- 1) >> MM_PAGE_SHIFT
;
255 *GdtIdt
= (PKGDTENTRY
)MmAllocateMemory(NumPages
* MM_PAGE_SIZE
);
259 UiMessageBox("Can't allocate pages for GDT+IDT!\n");
263 /* Zero newly prepared GDT+IDT */
264 RtlZeroMemory(*GdtIdt
, NumPages
<< MM_PAGE_SHIFT
);
268 WinLdrLoadDeviceDriver(PLOADER_PARAMETER_BLOCK LoaderBlock
,
270 PUNICODE_STRING FilePath
,
272 PLDR_DATA_TABLE_ENTRY
*DriverDTE
)
275 CHAR DriverPath
[1024];
281 // Separate the path to file name and directory path
282 sprintf(DriverPath
, "%wZ", FilePath
);
283 DriverNamePos
= strrchr(DriverPath
, '\\');
284 if (DriverNamePos
!= NULL
)
287 strcpy(DllName
, DriverNamePos
+1);
289 // Cut out the name from the path
290 *(DriverNamePos
+1) = 0;
293 DbgPrint((DPRINT_WINDOWS
, "DriverPath: %s, DllName: %s, LPB %p\n", DriverPath
, DllName
, LoaderBlock
));
296 // Check if driver is already loaded
297 Status
= WinLdrCheckForLoadedDll(LoaderBlock
, DllName
, DriverDTE
);
300 // We've got the pointer to its DTE, just return success
304 // It's not loaded, we have to load it
305 sprintf(FullPath
,"%s%wZ", BootPath
, FilePath
);
306 Status
= WinLdrLoadImage(FullPath
, &DriverBase
);
310 // Allocate a DTE for it
311 Status
= WinLdrAllocateDataTableEntry(LoaderBlock
, DllName
, DllName
, DriverBase
, DriverDTE
);
314 DbgPrint((DPRINT_WINDOWS
, "WinLdrAllocateDataTableEntry() failed\n"));
318 // Modify any flags, if needed
319 (*DriverDTE
)->Flags
|= Flags
;
321 // Look for any dependencies it may have, and load them too
322 sprintf(FullPath
,"%s%s", BootPath
, DriverPath
);
323 Status
= WinLdrScanImportDescriptorTable(LoaderBlock
, FullPath
, *DriverDTE
);
326 DbgPrint((DPRINT_WINDOWS
, "WinLdrScanImportDescriptorTable() failed for %s\n",
335 WinLdrLoadBootDrivers(PLOADER_PARAMETER_BLOCK LoaderBlock
,
339 PBOOT_DRIVER_LIST_ENTRY BootDriver
;
342 // Walk through the boot drivers list
343 NextBd
= LoaderBlock
->BootDriverListHead
.Flink
;
345 while (NextBd
!= &LoaderBlock
->BootDriverListHead
)
347 BootDriver
= CONTAINING_RECORD(NextBd
, BOOT_DRIVER_LIST_ENTRY
, ListEntry
);
349 //DbgPrint((DPRINT_WINDOWS, "BootDriver %wZ DTE %08X RegPath: %wZ\n", &BootDriver->FilePath,
350 // BootDriver->DataTableEntry, &BootDriver->RegistryPath));
352 // Paths are relative (FIXME: Are they always relative?)
355 Status
= WinLdrLoadDeviceDriver(LoaderBlock
, BootPath
, &BootDriver
->FilePath
,
356 0, &BootDriver
->DataTableEntry
);
358 // If loading failed - cry loudly
359 //FIXME: Maybe remove it from the list and try to continue?
362 UiMessageBox("Can't load boot driver!");
366 NextBd
= BootDriver
->ListEntry
.Flink
;
373 LoadAndBootWindows(PCSTR OperatingSystemName
, WORD OperatingSystemVersion
)
376 CHAR SystemPath
[1024], SearchPath
[1024];
379 PVOID NtosBase
= NULL
, HalBase
= NULL
, KdComBase
= NULL
;
383 PLOADER_PARAMETER_BLOCK LoaderBlock
, LoaderBlockVA
;
384 KERNEL_ENTRY_POINT KiSystemStartup
;
385 PLDR_DATA_TABLE_ENTRY KernelDTE
, HalDTE
, KdComDTE
= NULL
;
391 //sprintf(MsgBuffer,"Booting Microsoft(R) Windows(R) OS version '%04x' is not implemented yet", OperatingSystemVersion);
392 //UiMessageBox(MsgBuffer);
394 // Open the operating system section
395 // specified in the .ini file
396 if (!IniOpenSection(OperatingSystemName
, &SectionId
))
398 sprintf(MsgBuffer
,"Operating System section '%s' not found in freeldr.ini", OperatingSystemName
);
399 UiMessageBox(MsgBuffer
);
404 UiDrawStatusText("Detecting Hardware...");
405 UiDrawProgressBarCenter(1, 100, "Loading Windows...");
407 //FIXME: This is needed only for MachHwDetect() which performs registry operations!
408 RegInitializeRegistry();
410 /* Make sure the system path is set in the .ini file */
411 if (!IniReadSettingByName(SectionId
, "SystemPath", SystemPath
, sizeof(SystemPath
)))
413 UiMessageBox("System path not specified for selected operating system.");
417 if (!MachDiskNormalizeSystemPath(SystemPath
,
420 UiMessageBox("Invalid system path");
424 /* Detect hardware */
427 UiDrawStatusText("Loading...");
429 /* Try to open system drive */
430 BootDevice
= 0xffffffff;
431 if (!FsOpenSystemVolume(SystemPath
, BootPath
, &BootDevice
))
433 UiMessageBox("Failed to open boot drive.");
437 /* append a backslash */
438 if ((strlen(BootPath
)==0) ||
439 BootPath
[strlen(BootPath
)] != '\\')
440 strcat(BootPath
, "\\");
442 DbgPrint((DPRINT_WINDOWS
,"SystemRoot: '%s'\n", BootPath
));
444 // Allocate and minimalistic-initialize LPB
445 AllocateAndInitLPB(&LoaderBlock
);
448 strcpy(FileName
, BootPath
);
449 strcat(FileName
, "SYSTEM32\\NTOSKRNL.EXE");
450 Status
= WinLdrLoadImage(FileName
, &NtosBase
);
451 DbgPrint((DPRINT_WINDOWS
, "Ntos loaded with status %d at %p\n", Status
, NtosBase
));
454 strcpy(FileName
, BootPath
);
455 strcat(FileName
, "SYSTEM32\\HAL.DLL");
456 Status
= WinLdrLoadImage(FileName
, &HalBase
);
457 DbgPrint((DPRINT_WINDOWS
, "HAL loaded with status %d at %p\n", Status
, HalBase
));
459 // Load kernel-debugger support dll
460 if (OperatingSystemVersion
> _WIN32_WINNT_NT4
)
462 strcpy(FileName
, BootPath
);
463 strcat(FileName
, "SYSTEM32\\KDCOM.DLL");
464 Status
= WinLdrLoadImage(FileName
, &KdComBase
);
465 DbgPrint((DPRINT_WINDOWS
, "KdCom loaded with status %d at %p\n", Status
, KdComBase
));
468 // Allocate data table entries for above-loaded modules
469 WinLdrAllocateDataTableEntry(LoaderBlock
, "ntoskrnl.exe",
470 "WINNT\\SYSTEM32\\NTOSKRNL.EXE", NtosBase
, &KernelDTE
);
471 WinLdrAllocateDataTableEntry(LoaderBlock
, "hal.dll",
472 "WINNT\\SYSTEM32\\HAL.DLL", HalBase
, &HalDTE
);
473 if (OperatingSystemVersion
> _WIN32_WINNT_NT4
)
475 WinLdrAllocateDataTableEntry(LoaderBlock
, "kdcom.dll",
476 "WINNT\\SYSTEM32\\KDCOM.DLL", KdComBase
, &KdComDTE
);
479 /* Load all referenced DLLs for kernel, HAL and kdcom.dll */
480 strcpy(SearchPath
, BootPath
);
481 strcat(SearchPath
, "SYSTEM32\\");
482 WinLdrScanImportDescriptorTable(LoaderBlock
, SearchPath
, KernelDTE
);
483 WinLdrScanImportDescriptorTable(LoaderBlock
, SearchPath
, HalDTE
);
485 WinLdrScanImportDescriptorTable(LoaderBlock
, SearchPath
, KdComDTE
);
487 /* Load Hive, and then NLS data, OEM font, and prepare boot drivers list */
488 Status
= WinLdrLoadAndScanSystemHive(LoaderBlock
, BootPath
);
489 DbgPrint((DPRINT_WINDOWS
, "SYSTEM hive loaded and scanned with status %d\n", Status
));
491 /* Load boot drivers */
492 Status
= WinLdrLoadBootDrivers(LoaderBlock
, BootPath
);
493 DbgPrint((DPRINT_WINDOWS
, "Boot drivers loaded with status %d\n", Status
));
495 /* Initialize Phase 1 - no drivers loading anymore */
496 WinLdrInitializePhase1(LoaderBlock
);
498 /* Alloc PCR, TSS, do magic things with the GDT/IDT */
499 WinLdrSetupForNt(LoaderBlock
, &GdtIdt
, &PcrBasePage
, &TssBasePage
);
501 /* Save entry-point pointer (VA) */
502 KiSystemStartup
= (KERNEL_ENTRY_POINT
)KernelDTE
->EntryPoint
;
504 LoaderBlockVA
= PaToVa(LoaderBlock
);
507 //DumpMemoryAllocMap();
509 /* Turn on paging mode of CPU*/
510 WinLdrTurnOnPaging(LoaderBlock
, PcrBasePage
, TssBasePage
, GdtIdt
);
512 DbgPrint((DPRINT_WINDOWS
, "Hello from paged mode, KiSystemStartup %p, LoaderBlockVA %p!\n",
513 KiSystemStartup
, LoaderBlockVA
));
515 WinLdrpDumpMemoryDescriptors(LoaderBlockVA
);
516 WinLdrpDumpBootDriver(LoaderBlockVA
);
517 WinLdrpDumpArcDisks(LoaderBlockVA
);
519 //FIXME: If I substitute this debugging checkpoint, GCC will "optimize away" the code below
521 /*asm(".intel_syntax noprefix\n");
524 asm(".att_syntax\n");*/
527 (*KiSystemStartup
)(LoaderBlockVA
);
533 WinLdrpDumpMemoryDescriptors(PLOADER_PARAMETER_BLOCK LoaderBlock
)
536 PMEMORY_ALLOCATION_DESCRIPTOR MemoryDescriptor
;
538 NextMd
= LoaderBlock
->MemoryDescriptorListHead
.Flink
;
540 while (NextMd
!= &LoaderBlock
->MemoryDescriptorListHead
)
542 MemoryDescriptor
= CONTAINING_RECORD(NextMd
, MEMORY_ALLOCATION_DESCRIPTOR
, ListEntry
);
545 DbgPrint((DPRINT_WINDOWS
, "BP %08X PC %04X MT %d\n", MemoryDescriptor
->BasePage
,
546 MemoryDescriptor
->PageCount
, MemoryDescriptor
->MemoryType
));
548 NextMd
= MemoryDescriptor
->ListEntry
.Flink
;
553 WinLdrpDumpBootDriver(PLOADER_PARAMETER_BLOCK LoaderBlock
)
556 PBOOT_DRIVER_LIST_ENTRY BootDriver
;
558 NextBd
= LoaderBlock
->BootDriverListHead
.Flink
;
560 while (NextBd
!= &LoaderBlock
->BootDriverListHead
)
562 BootDriver
= CONTAINING_RECORD(NextBd
, BOOT_DRIVER_LIST_ENTRY
, ListEntry
);
564 DbgPrint((DPRINT_WINDOWS
, "BootDriver %wZ DTE %08X RegPath: %wZ\n", &BootDriver
->FilePath
,
565 BootDriver
->DataTableEntry
, &BootDriver
->RegistryPath
));
567 NextBd
= BootDriver
->ListEntry
.Flink
;
572 WinLdrpDumpArcDisks(PLOADER_PARAMETER_BLOCK LoaderBlock
)
575 PARC_DISK_SIGNATURE ArcDisk
;
577 NextBd
= LoaderBlock
->ArcDiskInformation
->DiskSignatureListHead
.Flink
;
579 while (NextBd
!= &LoaderBlock
->ArcDiskInformation
->DiskSignatureListHead
)
581 ArcDisk
= CONTAINING_RECORD(NextBd
, ARC_DISK_SIGNATURE
, ListEntry
);
583 DbgPrint((DPRINT_WINDOWS
, "ArcDisk %s checksum: 0x%X, signature: 0x%X\n",
584 ArcDisk
->ArcName
, ArcDisk
->CheckSum
, ArcDisk
->Signature
));
586 NextBd
= ArcDisk
->ListEntry
.Flink
;