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>
30 VOID
DumpMemoryAllocMap(VOID
);
31 VOID
WinLdrpDumpMemoryDescriptors(PLOADER_PARAMETER_BLOCK LoaderBlock
);
32 VOID
WinLdrpDumpBootDriver(PLOADER_PARAMETER_BLOCK LoaderBlock
);
34 void InitializeHWConfig(IN OUT PLOADER_PARAMETER_BLOCK LoaderBlock
)
36 PCONFIGURATION_COMPONENT_DATA ConfigurationRoot
;
37 PCONFIGURATION_COMPONENT Component
;
38 PCONFIGURATION_COMPONENT_DATA
/*CurrentEntry,*/ PreviousEntry
, AdapterEntry
;
39 BOOLEAN IsNextEntryChild
;
41 DbgPrint((DPRINT_WINDOWS
, "InitializeHWConfig()\n"));
43 LoaderBlock
->ConfigurationRoot
= MmAllocateMemory(sizeof(CONFIGURATION_COMPONENT_DATA
));
44 RtlZeroMemory(LoaderBlock
->ConfigurationRoot
, sizeof(CONFIGURATION_COMPONENT_DATA
));
46 /* Fill root == SystemClass */
47 ConfigurationRoot
= LoaderBlock
->ConfigurationRoot
;
48 Component
= &LoaderBlock
->ConfigurationRoot
->ComponentEntry
;
50 Component
->Class
= SystemClass
;
51 Component
->Type
= MaximumType
;
52 Component
->Version
= 0; // FIXME: ?
54 Component
->AffinityMask
= 0;
56 IsNextEntryChild
= TRUE
;
57 PreviousEntry
= ConfigurationRoot
;
59 /* Enumerate all PCI buses */
60 AdapterEntry
= ConfigurationRoot
;
62 /* TODO: Disk Geometry */
65 /* TODO: Serial port */
67 //Config->ConfigurationData = alloc(sizeof(CONFIGURATION_COMPONENT_DATA), EfiLoaderData);
69 /* Convert everything to VA */
70 ConvertConfigToVA(LoaderBlock
->ConfigurationRoot
);
71 LoaderBlock
->ConfigurationRoot
= PaToVa(LoaderBlock
->ConfigurationRoot
);
77 AllocateAndInitLPB(PLOADER_PARAMETER_BLOCK
*OutLoaderBlock
)
79 PLOADER_PARAMETER_BLOCK LoaderBlock
;
81 /* Allocate and zero-init the LPB */
82 LoaderBlock
= MmAllocateMemory(sizeof(LOADER_PARAMETER_BLOCK
));
83 RtlZeroMemory(LoaderBlock
, sizeof(LOADER_PARAMETER_BLOCK
));
85 /* Init three critical lists, used right away */
86 InitializeListHead(&LoaderBlock
->LoadOrderListHead
);
87 InitializeListHead(&LoaderBlock
->MemoryDescriptorListHead
);
88 InitializeListHead(&LoaderBlock
->BootDriverListHead
);
91 *OutLoaderBlock
= LoaderBlock
;
96 WinLdrInitializePhase1(PLOADER_PARAMETER_BLOCK LoaderBlock
)
98 //CHAR Options[] = "/CRASHDEBUG /DEBUGPORT=COM1 /BAUDRATE=115200";
99 CHAR Options
[] = "/NODEBUG";
100 CHAR SystemRoot
[] = "\\WINNT";
101 CHAR HalPath
[] = "\\";
102 CHAR ArcBoot
[] = "multi(0)disk(0)rdisk(1)partition(1)";
103 CHAR ArcHal
[] = "multi(0)disk(0)rdisk(1)partition(1)";
105 PLOADER_PARAMETER_EXTENSION Extension
;
107 LoaderBlock
->u
.I386
.CommonDataArea
= NULL
; // Force No ABIOS support
109 /* Fill Arc BootDevice */
110 LoaderBlock
->ArcBootDeviceName
= MmAllocateMemory(strlen(ArcBoot
)+1);
111 strcpy(LoaderBlock
->ArcBootDeviceName
, ArcBoot
);
112 LoaderBlock
->ArcBootDeviceName
= PaToVa(LoaderBlock
->ArcBootDeviceName
);
114 /* Fill Arc HalDevice */
115 LoaderBlock
->ArcHalDeviceName
= MmAllocateMemory(strlen(ArcHal
)+1);
116 strcpy(LoaderBlock
->ArcHalDeviceName
, ArcHal
);
117 LoaderBlock
->ArcHalDeviceName
= PaToVa(LoaderBlock
->ArcHalDeviceName
);
119 /* Fill SystemRoot */
120 LoaderBlock
->NtBootPathName
= MmAllocateMemory(strlen(SystemRoot
)+1);
121 strcpy(LoaderBlock
->NtBootPathName
, SystemRoot
);
122 LoaderBlock
->NtBootPathName
= PaToVa(LoaderBlock
->NtBootPathName
);
124 /* Fill NtHalPathName */
125 LoaderBlock
->NtHalPathName
= MmAllocateMemory(strlen(HalPath
)+1);
126 strcpy(LoaderBlock
->NtHalPathName
, HalPath
);
127 LoaderBlock
->NtHalPathName
= PaToVa(LoaderBlock
->NtHalPathName
);
129 /* Fill load options */
130 LoaderBlock
->LoadOptions
= MmAllocateMemory(strlen(Options
)+1);
131 strcpy(LoaderBlock
->LoadOptions
, Options
);
132 LoaderBlock
->LoadOptions
= PaToVa(LoaderBlock
->LoadOptions
);
135 LoaderBlock
->ArcDiskInformation
= (PARC_DISK_INFORMATION
)MmAllocateMemory(sizeof(ARC_DISK_INFORMATION
));
136 InitializeListHead(&LoaderBlock
->ArcDiskInformation
->DiskSignatureListHead
);
137 List_PaToVa(&LoaderBlock
->ArcDiskInformation
->DiskSignatureListHead
);
138 LoaderBlock
->ArcDiskInformation
= PaToVa(LoaderBlock
->ArcDiskInformation
);
140 /* Alloc space for NLS (it will be converted to VA in WinLdrLoadNLS) */
141 LoaderBlock
->NlsData
= MmAllocateMemory(sizeof(NLS_DATA_BLOCK
));
142 if (LoaderBlock
->NlsData
== NULL
)
144 UiMessageBox("Failed to allocate memory for NLS table data!");
147 RtlZeroMemory(LoaderBlock
->NlsData
, sizeof(NLS_DATA_BLOCK
));
149 /* Create configuration entries */
150 InitializeHWConfig(LoaderBlock
);
152 /* Convert all DTE into virtual addresses */
155 /* Convert all list's to Virtual address */
156 List_PaToVa(&LoaderBlock
->LoadOrderListHead
);
158 /* this one will be converted right before switching to
159 virtual paging mode */
160 //List_PaToVa(&LoaderBlock->MemoryDescriptorListHead);
162 List_PaToVa(&LoaderBlock
->BootDriverListHead
);
164 /* Initialize Extension now */
165 Extension
= MmAllocateMemory(sizeof(LOADER_PARAMETER_EXTENSION
));
166 if (Extension
== NULL
)
168 UiMessageBox("Failed to allocate LPB Extension!");
171 RtlZeroMemory(Extension
, sizeof(LOADER_PARAMETER_EXTENSION
));
173 Extension
->Size
= sizeof(LOADER_PARAMETER_EXTENSION
);
174 Extension
->MajorVersion
= 4;
175 Extension
->MinorVersion
= 0;
178 LoaderBlock
->Extension
= PaToVa(Extension
);
181 // Last step before going virtual
182 void WinLdrSetupForNt(PLOADER_PARAMETER_BLOCK LoaderBlock
,
191 ULONG BlockSize
, NumPages
;
193 LoaderBlock
->u
.I386
.CommonDataArea
= NULL
;//CommonDataArea;
194 //LoaderBlock->u.I386.MachineType = MachineType; //FIXME: MachineType?
196 /* Allocate 2 pages for PCR */
197 Pcr
= (ULONG_PTR
)MmAllocateMemory(2 * MM_PAGE_SIZE
);
198 *PcrBasePage
= Pcr
>> MM_PAGE_SHIFT
;
202 UiMessageBox("Can't allocate PCR\n");
207 TssSize
= (sizeof(KTSS
) + MM_PAGE_SIZE
) & ~(MM_PAGE_SIZE
- 1);
208 TssPages
= TssSize
/ MM_PAGE_SIZE
;
210 Tss
= (ULONG_PTR
)MmAllocateMemory(TssSize
);
212 *TssBasePage
= Tss
>> MM_PAGE_SHIFT
;
214 /* Allocate space for new GDT + IDT */
215 BlockSize
= NUM_GDT
*sizeof(KGDTENTRY
) + NUM_IDT
*sizeof(KIDTENTRY
);//FIXME: Use GDT/IDT limits here?
216 NumPages
= (BlockSize
+ MM_PAGE_SIZE
- 1) >> MM_PAGE_SHIFT
;
217 *GdtIdt
= (PKGDTENTRY
)MmAllocateMemory(NumPages
* MM_PAGE_SIZE
);
221 UiMessageBox("Can't allocate pages for GDT+IDT!\n");
225 /* Zero newly prepared GDT+IDT */
226 RtlZeroMemory(*GdtIdt
, NumPages
<< MM_PAGE_SHIFT
);
230 LoadAndBootWindows(PCSTR OperatingSystemName
, WORD OperatingSystemVersion
)
233 CHAR SystemPath
[1024], SearchPath
[1024];
236 PVOID NtosBase
= NULL
, HalBase
= NULL
, KdComBase
= NULL
;
240 PLOADER_PARAMETER_BLOCK LoaderBlock
, LoaderBlockVA
;
241 KERNEL_ENTRY_POINT KiSystemStartup
;
242 PLDR_DATA_TABLE_ENTRY KernelDTE
, HalDTE
, KdComDTE
= NULL
;
250 //sprintf(MsgBuffer,"Booting Microsoft(R) Windows(R) OS version '%04x' is not implemented yet", OperatingSystemVersion);
251 //UiMessageBox(MsgBuffer);
253 // Open the operating system section
254 // specified in the .ini file
255 if (!IniOpenSection(OperatingSystemName
, &SectionId
))
257 sprintf(MsgBuffer
,"Operating System section '%s' not found in freeldr.ini", OperatingSystemName
);
258 UiMessageBox(MsgBuffer
);
262 /* Make sure the system path is set in the .ini file */
263 if (!IniReadSettingByName(SectionId
, "SystemPath", SystemPath
, sizeof(SystemPath
)))
265 UiMessageBox("System path not specified for selected operating system.");
269 if (!MachDiskNormalizeSystemPath(SystemPath
,
272 UiMessageBox("Invalid system path");
276 UiDrawStatusText("Loading...");
278 /* Try to open system drive */
279 BootDevice
= 0xffffffff;
280 if (!FsOpenSystemVolume(SystemPath
, BootPath
, &BootDevice
))
282 UiMessageBox("Failed to open boot drive.");
286 /* append a backslash */
287 if ((strlen(BootPath
)==0) ||
288 BootPath
[strlen(BootPath
)] != '\\')
289 strcat(BootPath
, "\\");
291 DbgPrint((DPRINT_WINDOWS
,"SystemRoot: '%s'\n", BootPath
));
293 // Allocate and minimalistic-initialize LPB
294 AllocateAndInitLPB(&LoaderBlock
);
297 strcpy(FileName
, BootPath
);
298 strcat(FileName
, "SYSTEM32\\NTOSKRNL.EXE");
299 Status
= WinLdrLoadImage(FileName
, &NtosBase
);
300 DbgPrint((DPRINT_WINDOWS
, "Ntos loaded with status %d at %p\n", Status
, NtosBase
));
303 strcpy(FileName
, BootPath
);
304 strcat(FileName
, "SYSTEM32\\HAL.DLL");
305 Status
= WinLdrLoadImage(FileName
, &HalBase
);
306 DbgPrint((DPRINT_WINDOWS
, "HAL loaded with status %d at %p\n", Status
, HalBase
));
308 // Load kernel-debugger support dll
309 if (OperatingSystemVersion
> _WIN32_WINNT_NT4
)
311 strcpy(FileName
, BootPath
);
312 strcat(FileName
, "SYSTEM32\\KDCOM.DLL");
313 Status
= WinLdrLoadImage(FileName
, &KdComBase
);
314 DbgPrint((DPRINT_WINDOWS
, "KdCom loaded with status %d at %p\n", Status
, KdComBase
));
317 // Allocate data table entries for above-loaded modules
318 WinLdrAllocateDataTableEntry(LoaderBlock
, "ntoskrnl.exe",
319 "WINNT\\SYSTEM32\\NTOSKRNL.EXE", NtosBase
, &KernelDTE
);
320 WinLdrAllocateDataTableEntry(LoaderBlock
, "hal.dll",
321 "WINNT\\SYSTEM32\\HAL.DLL", HalBase
, &HalDTE
);
322 if (OperatingSystemVersion
> _WIN32_WINNT_NT4
)
324 WinLdrAllocateDataTableEntry(LoaderBlock
, "kdcom.dll",
325 "WINNT\\SYSTEM32\\KDCOM.DLL", KdComBase
, &KdComDTE
);
328 /* Load all referenced DLLs for kernel, HAL and kdcom.dll */
329 strcpy(SearchPath
, BootPath
);
330 strcat(SearchPath
, "SYSTEM32\\");
331 WinLdrScanImportDescriptorTable(LoaderBlock
, SearchPath
, KernelDTE
);
332 WinLdrScanImportDescriptorTable(LoaderBlock
, SearchPath
, HalDTE
);
334 WinLdrScanImportDescriptorTable(LoaderBlock
, SearchPath
, KdComDTE
);
336 /* Initialize Phase 1 - before NLS */
337 WinLdrInitializePhase1(LoaderBlock
);
339 /* Load Hive, and then NLS data, OEM font, and prepare boot drivers list */
340 Status
= WinLdrLoadAndScanSystemHive(LoaderBlock
, BootPath
);
341 DbgPrint((DPRINT_WINDOWS
, "SYSTEM hive loaded and scanned with status %d\n", Status
));
343 /* Load boot drivers */
344 //WinLdrLoadBootDrivers();
346 /* Alloc PCR, TSS, do magic things with the GDT/IDT */
347 WinLdrSetupForNt(LoaderBlock
, &GdtIdt
, &PcrBasePage
, &TssBasePage
);
349 /* Save entry-point pointer (VA) */
350 KiSystemStartup
= (KERNEL_ENTRY_POINT
)KernelDTE
->EntryPoint
;
352 LoaderBlockVA
= PaToVa(LoaderBlock
);
355 //DumpMemoryAllocMap();
357 /* Turn on paging mode of CPU*/
358 WinLdrTurnOnPaging(LoaderBlock
, PcrBasePage
, TssBasePage
, GdtIdt
);
360 DbgPrint((DPRINT_WINDOWS
, "Hello from paged mode, KiSystemStartup %p, LoaderBlockVA %p!\n",
361 KiSystemStartup
, LoaderBlockVA
));
363 WinLdrpDumpMemoryDescriptors(LoaderBlockVA
);
364 WinLdrpDumpBootDriver(LoaderBlockVA
);
366 //FIXME: If I substitute this debugging checkpoint, GCC will "optimize away" the code below
368 /*asm(".intel_syntax noprefix\n");
371 asm(".att_syntax\n");*/
374 (*KiSystemStartup
)(LoaderBlockVA
);
380 WinLdrpDumpMemoryDescriptors(PLOADER_PARAMETER_BLOCK LoaderBlock
)
383 PMEMORY_ALLOCATION_DESCRIPTOR MemoryDescriptor
;
385 NextMd
= LoaderBlock
->MemoryDescriptorListHead
.Flink
;
387 while (NextMd
!= &LoaderBlock
->MemoryDescriptorListHead
)
389 MemoryDescriptor
= CONTAINING_RECORD(NextMd
, MEMORY_ALLOCATION_DESCRIPTOR
, ListEntry
);
392 DbgPrint((DPRINT_WINDOWS
, "BP %08X PC %04X MT %d\n", MemoryDescriptor
->BasePage
,
393 MemoryDescriptor
->PageCount
, MemoryDescriptor
->MemoryType
));
395 NextMd
= MemoryDescriptor
->ListEntry
.Flink
;
400 WinLdrpDumpBootDriver(PLOADER_PARAMETER_BLOCK LoaderBlock
)
403 PBOOT_DRIVER_LIST_ENTRY BootDriver
;
405 NextBd
= LoaderBlock
->BootDriverListHead
.Flink
;
407 while (NextBd
!= &LoaderBlock
->BootDriverListHead
)
409 BootDriver
= CONTAINING_RECORD(NextBd
, BOOT_DRIVER_LIST_ENTRY
, ListEntry
);
411 DbgPrint((DPRINT_WINDOWS
, "BootDriver %wZ DTE %08X RegPath: %wZ\n", &BootDriver
->FilePath
,
412 BootDriver
->DataTableEntry
, &BootDriver
->RegistryPath
));
414 NextBd
= BootDriver
->ListEntry
.Flink
;