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 VOID
DumpMemoryAllocMap(VOID
);
30 VOID
WinLdrpDumpMemoryDescriptors(PLOADER_PARAMETER_BLOCK LoaderBlock
);
32 void InitializeHWConfig(IN OUT PLOADER_PARAMETER_BLOCK LoaderBlock
)
34 PCONFIGURATION_COMPONENT_DATA ConfigurationRoot
;
35 PCONFIGURATION_COMPONENT Component
;
36 PCONFIGURATION_COMPONENT_DATA
/*CurrentEntry,*/ PreviousEntry
, AdapterEntry
;
37 BOOLEAN IsNextEntryChild
;
39 DbgPrint((DPRINT_WINDOWS
, "InitializeHWConfig()\n"));
41 LoaderBlock
->ConfigurationRoot
= MmAllocateMemory(sizeof(CONFIGURATION_COMPONENT_DATA
));
42 RtlZeroMemory(LoaderBlock
->ConfigurationRoot
, sizeof(CONFIGURATION_COMPONENT_DATA
));
44 /* Fill root == SystemClass */
45 ConfigurationRoot
= LoaderBlock
->ConfigurationRoot
;
46 Component
= &LoaderBlock
->ConfigurationRoot
->ComponentEntry
;
48 Component
->Class
= SystemClass
;
49 Component
->Type
= MaximumType
;
50 Component
->Version
= 0; // FIXME: ?
52 Component
->AffinityMask
= 0;
54 IsNextEntryChild
= TRUE
;
55 PreviousEntry
= ConfigurationRoot
;
57 /* Enumerate all PCI buses */
58 AdapterEntry
= ConfigurationRoot
;
60 /* TODO: Disk Geometry */
63 /* TODO: Serial port */
65 //Config->ConfigurationData = alloc(sizeof(CONFIGURATION_COMPONENT_DATA), EfiLoaderData);
67 /* Convert everything to VA */
68 ConvertConfigToVA(LoaderBlock
->ConfigurationRoot
);
69 LoaderBlock
->ConfigurationRoot
= PaToVa(LoaderBlock
->ConfigurationRoot
);
75 AllocateAndInitLPB(PLOADER_PARAMETER_BLOCK
*OutLoaderBlock
)
77 PLOADER_PARAMETER_BLOCK LoaderBlock
;
79 /* Allocate and zero-init the LPB */
80 LoaderBlock
= MmAllocateMemory(sizeof(LOADER_PARAMETER_BLOCK
));
81 RtlZeroMemory(LoaderBlock
, sizeof(LOADER_PARAMETER_BLOCK
));
83 /* Init three critical lists, used right away */
84 InitializeListHead(&LoaderBlock
->LoadOrderListHead
);
85 InitializeListHead(&LoaderBlock
->MemoryDescriptorListHead
);
86 InitializeListHead(&LoaderBlock
->BootDriverListHead
);
89 *OutLoaderBlock
= LoaderBlock
;
94 WinLdrInitializePhase1(PLOADER_PARAMETER_BLOCK LoaderBlock
)
96 //CHAR Options[] = "/CRASHDEBUG /DEBUGPORT=COM1 /BAUDRATE=115200";
97 CHAR Options
[] = "/NODEBUG";
98 CHAR SystemRoot
[] = "\\WINNT";
99 CHAR HalPath
[] = "\\";
100 CHAR ArcBoot
[] = "multi(0)disk(0)rdisk(1)partition(1)";
101 CHAR ArcHal
[] = "multi(0)disk(0)rdisk(1)partition(1)";
103 PLOADER_PARAMETER_EXTENSION Extension
;
105 LoaderBlock
->u
.I386
.CommonDataArea
= NULL
; // Force No ABIOS support
107 /* Fill Arc BootDevice */
108 LoaderBlock
->ArcBootDeviceName
= MmAllocateMemory(strlen(ArcBoot
)+1);
109 strcpy(LoaderBlock
->ArcBootDeviceName
, ArcBoot
);
110 LoaderBlock
->ArcBootDeviceName
= PaToVa(LoaderBlock
->ArcBootDeviceName
);
112 /* Fill Arc HalDevice */
113 LoaderBlock
->ArcHalDeviceName
= MmAllocateMemory(strlen(ArcHal
)+1);
114 strcpy(LoaderBlock
->ArcHalDeviceName
, ArcHal
);
115 LoaderBlock
->ArcHalDeviceName
= PaToVa(LoaderBlock
->ArcHalDeviceName
);
117 /* Fill SystemRoot */
118 LoaderBlock
->NtBootPathName
= MmAllocateMemory(strlen(SystemRoot
)+1);
119 strcpy(LoaderBlock
->NtBootPathName
, SystemRoot
);
120 LoaderBlock
->NtBootPathName
= PaToVa(LoaderBlock
->NtBootPathName
);
122 /* Fill NtHalPathName */
123 LoaderBlock
->NtHalPathName
= MmAllocateMemory(strlen(HalPath
)+1);
124 strcpy(LoaderBlock
->NtHalPathName
, HalPath
);
125 LoaderBlock
->NtHalPathName
= PaToVa(LoaderBlock
->NtHalPathName
);
127 /* Fill load options */
128 LoaderBlock
->LoadOptions
= MmAllocateMemory(strlen(Options
)+1);
129 strcpy(LoaderBlock
->LoadOptions
, Options
);
130 LoaderBlock
->LoadOptions
= PaToVa(LoaderBlock
->LoadOptions
);
133 LoaderBlock
->ArcDiskInformation
= (PARC_DISK_INFORMATION
)MmAllocateMemory(sizeof(ARC_DISK_INFORMATION
));
134 InitializeListHead(&LoaderBlock
->ArcDiskInformation
->DiskSignatureListHead
);
135 List_PaToVa(&LoaderBlock
->ArcDiskInformation
->DiskSignatureListHead
);
136 LoaderBlock
->ArcDiskInformation
= PaToVa(LoaderBlock
->ArcDiskInformation
);
138 /* Alloc space for NLS (it will be converted to VA in WinLdrLoadNLS) */
139 LoaderBlock
->NlsData
= MmAllocateMemory(sizeof(NLS_DATA_BLOCK
));
140 if (LoaderBlock
->NlsData
== NULL
)
142 UiMessageBox("Failed to allocate memory for NLS table data!");
145 RtlZeroMemory(LoaderBlock
->NlsData
, sizeof(NLS_DATA_BLOCK
));
147 /* Create configuration entries */
148 InitializeHWConfig(LoaderBlock
);
150 /* Convert all DTE into virtual addresses */
153 /* Convert all list's to Virtual address */
154 List_PaToVa(&LoaderBlock
->LoadOrderListHead
);
156 /* this one will be converted right before switching to
157 virtual paging mode */
158 //List_PaToVa(&LoaderBlock->MemoryDescriptorListHead);
160 List_PaToVa(&LoaderBlock
->BootDriverListHead
);
162 /* Initialize Extension now */
163 Extension
= MmAllocateMemory(sizeof(LOADER_PARAMETER_EXTENSION
));
164 if (Extension
== NULL
)
166 UiMessageBox("Failed to allocate LPB Extension!");
169 RtlZeroMemory(Extension
, sizeof(LOADER_PARAMETER_EXTENSION
));
171 Extension
->Size
= sizeof(LOADER_PARAMETER_EXTENSION
);
172 Extension
->MajorVersion
= 4;
173 Extension
->MinorVersion
= 0;
176 LoaderBlock
->Extension
= PaToVa(Extension
);
179 // Last step before going virtual
180 void WinLdrSetupForNt(PLOADER_PARAMETER_BLOCK LoaderBlock
,
189 ULONG BlockSize
, NumPages
;
191 LoaderBlock
->u
.I386
.CommonDataArea
= NULL
;//CommonDataArea;
192 //LoaderBlock->u.I386.MachineType = MachineType; //FIXME: MachineType?
194 /* Allocate 2 pages for PCR */
195 Pcr
= (ULONG_PTR
)MmAllocateMemory(2 * MM_PAGE_SIZE
);
196 *PcrBasePage
= Pcr
>> MM_PAGE_SHIFT
;
200 UiMessageBox("Can't allocate PCR\n");
205 TssSize
= (sizeof(KTSS
) + MM_PAGE_SIZE
) & ~(MM_PAGE_SIZE
- 1);
206 TssPages
= TssSize
/ MM_PAGE_SIZE
;
208 Tss
= (ULONG_PTR
)MmAllocateMemory(TssSize
);
210 *TssBasePage
= Tss
>> MM_PAGE_SHIFT
;
212 /* Allocate space for new GDT + IDT */
213 BlockSize
= NUM_GDT
*sizeof(KGDTENTRY
) + NUM_IDT
*sizeof(KIDTENTRY
);//FIXME: Use GDT/IDT limits here?
214 NumPages
= (BlockSize
+ MM_PAGE_SIZE
- 1) >> MM_PAGE_SHIFT
;
215 *GdtIdt
= (PKGDTENTRY
)MmAllocateMemory(NumPages
* MM_PAGE_SIZE
);
219 UiMessageBox("Can't allocate pages for GDT+IDT!\n");
223 /* Zero newly prepared GDT+IDT */
224 RtlZeroMemory(*GdtIdt
, NumPages
<< MM_PAGE_SHIFT
);
228 LoadAndBootWindows(PCSTR OperatingSystemName
, WORD OperatingSystemVersion
)
231 CHAR SystemPath
[1024], SearchPath
[1024];
234 PVOID NtosBase
= NULL
, HalBase
= NULL
, KdComBase
= NULL
;
238 PLOADER_PARAMETER_BLOCK LoaderBlock
, LoaderBlockVA
;
239 KERNEL_ENTRY_POINT KiSystemStartup
;
240 PLDR_DATA_TABLE_ENTRY KernelDTE
, HalDTE
, KdComDTE
= NULL
;
248 //sprintf(MsgBuffer,"Booting Microsoft(R) Windows(R) OS version '%04x' is not implemented yet", OperatingSystemVersion);
249 //UiMessageBox(MsgBuffer);
251 // Open the operating system section
252 // specified in the .ini file
253 if (!IniOpenSection(OperatingSystemName
, &SectionId
))
255 sprintf(MsgBuffer
,"Operating System section '%s' not found in freeldr.ini", OperatingSystemName
);
256 UiMessageBox(MsgBuffer
);
260 /* Make sure the system path is set in the .ini file */
261 if (!IniReadSettingByName(SectionId
, "SystemPath", SystemPath
, sizeof(SystemPath
)))
263 UiMessageBox("System path not specified for selected operating system.");
267 if (!MachDiskNormalizeSystemPath(SystemPath
,
270 UiMessageBox("Invalid system path");
274 UiDrawStatusText("Loading...");
276 /* Try to open system drive */
277 BootDevice
= 0xffffffff;
278 if (!FsOpenSystemVolume(SystemPath
, BootPath
, &BootDevice
))
280 UiMessageBox("Failed to open boot drive.");
284 /* append a backslash */
285 if ((strlen(BootPath
)==0) ||
286 BootPath
[strlen(BootPath
)] != '\\')
287 strcat(BootPath
, "\\");
289 DbgPrint((DPRINT_WINDOWS
,"SystemRoot: '%s'\n", BootPath
));
291 // Allocate and minimalistic-initialize LPB
292 AllocateAndInitLPB(&LoaderBlock
);
295 strcpy(FileName
, BootPath
);
296 strcat(FileName
, "SYSTEM32\\NTOSKRNL.EXE");
297 Status
= WinLdrLoadImage(FileName
, &NtosBase
);
298 DbgPrint((DPRINT_WINDOWS
, "Ntos loaded with status %d at %p\n", Status
, NtosBase
));
301 strcpy(FileName
, BootPath
);
302 strcat(FileName
, "SYSTEM32\\HAL.DLL");
303 Status
= WinLdrLoadImage(FileName
, &HalBase
);
304 DbgPrint((DPRINT_WINDOWS
, "HAL loaded with status %d at %p\n", Status
, HalBase
));
306 // Load kernel-debugger support dll
307 if (OperatingSystemVersion
> _WIN32_WINNT_NT4
)
309 strcpy(FileName
, BootPath
);
310 strcat(FileName
, "SYSTEM32\\KDCOM.DLL");
311 Status
= WinLdrLoadImage(FileName
, &KdComBase
);
312 DbgPrint((DPRINT_WINDOWS
, "KdCom loaded with status %d at %p\n", Status
, KdComBase
));
315 // Allocate data table entries for above-loaded modules
316 WinLdrAllocateDataTableEntry(LoaderBlock
, "ntoskrnl.exe",
317 "WINNT\\SYSTEM32\\NTOSKRNL.EXE", NtosBase
, &KernelDTE
);
318 WinLdrAllocateDataTableEntry(LoaderBlock
, "hal.dll",
319 "WINNT\\SYSTEM32\\HAL.DLL", HalBase
, &HalDTE
);
320 if (OperatingSystemVersion
> _WIN32_WINNT_NT4
)
322 WinLdrAllocateDataTableEntry(LoaderBlock
, "kdcom.dll",
323 "WINNT\\SYSTEM32\\KDCOM.DLL", KdComBase
, &KdComDTE
);
326 /* Load all referenced DLLs for kernel, HAL and kdcom.dll */
327 strcpy(SearchPath
, BootPath
);
328 strcat(SearchPath
, "SYSTEM32\\");
329 WinLdrScanImportDescriptorTable(LoaderBlock
, SearchPath
, KernelDTE
);
330 WinLdrScanImportDescriptorTable(LoaderBlock
, SearchPath
, HalDTE
);
332 WinLdrScanImportDescriptorTable(LoaderBlock
, SearchPath
, KdComDTE
);
334 /* Initialize Phase 1 - before NLS */
335 WinLdrInitializePhase1(LoaderBlock
);
337 /* Load Hive, and then NLS data, OEM font, and prepare boot drivers list */
338 Status
= WinLdrLoadAndScanSystemHive(LoaderBlock
, BootPath
);
339 DbgPrint((DPRINT_WINDOWS
, "SYSTEM hive loaded and scanned with status %d\n", Status
));
341 /* FIXME: Load OEM HAL font, should be moved to WinLdrLoadAndScanSystemHive() */
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
);
365 //FIXME: If I substitute this debugging checkpoint, GCC will "optimize away" the code below
367 /*asm(".intel_syntax noprefix\n");
370 asm(".att_syntax\n");*/
373 (*KiSystemStartup
)(LoaderBlockVA
);
379 WinLdrpDumpMemoryDescriptors(PLOADER_PARAMETER_BLOCK LoaderBlock
)
382 PMEMORY_ALLOCATION_DESCRIPTOR MemoryDescriptor
;
384 NextMd
= LoaderBlock
->MemoryDescriptorListHead
.Flink
;
386 while (NextMd
!= &LoaderBlock
->MemoryDescriptorListHead
)
388 MemoryDescriptor
= CONTAINING_RECORD(NextMd
, MEMORY_ALLOCATION_DESCRIPTOR
, ListEntry
);
391 DbgPrint((DPRINT_WINDOWS
, "BP %08X PC %04X MT %d\n", MemoryDescriptor
->BasePage
,
392 MemoryDescriptor
->PageCount
, MemoryDescriptor
->MemoryType
));
394 NextMd
= MemoryDescriptor
->ListEntry
.Flink
;