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
);
33 WinLdrLoadNLSData(IN OUT PLOADER_PARAMETER_BLOCK LoaderBlock
,
34 IN LPCSTR DirectoryPath
,
35 IN LPCSTR AnsiFileName
,
36 IN LPCSTR OemFileName
,
37 IN LPCSTR LanguageFileName
)
42 PFILE LanguageFileHandle
;
43 ULONG AnsiFileSize
, OemFileSize
, LanguageFileSize
;
45 ULONG_PTR NlsDataBase
;
47 BOOLEAN Status
, AnsiEqualsOem
= FALSE
;
49 /* There may be a case, when OEM and ANSI page coincide */
50 if (!strcmp(AnsiFileName
, OemFileName
))
53 /* Open file with ANSI and store its size */
54 //Print(L"Loading %s...\n", Filename);
55 strcpy(FileName
, DirectoryPath
);
56 strcat(FileName
, AnsiFileName
);
57 AnsiFileHandle
= FsOpenFile(FileName
);
59 if (AnsiFileHandle
== NULL
)
62 AnsiFileSize
= FsGetFileSize(AnsiFileHandle
);
63 DbgPrint((DPRINT_WINDOWS
, "AnsiFileSize: %d\n", AnsiFileSize
));
64 FsCloseFile(AnsiFileHandle
);
66 /* Open OEM file and store its length */
73 //Print(L"Loading %s...\n", Filename);
74 strcpy(FileName
, DirectoryPath
);
75 strcat(FileName
, OemFileName
);
76 OemFileHandle
= FsOpenFile(FileName
);
78 if (OemFileHandle
== NULL
)
81 OemFileSize
= FsGetFileSize(OemFileHandle
);
82 FsCloseFile(OemFileHandle
);
84 DbgPrint((DPRINT_WINDOWS
, "OemFileSize: %d\n", OemFileSize
));
86 /* And finally open the language codepage file and store its length */
87 //Print(L"Loading %s...\n", Filename);
88 strcpy(FileName
, DirectoryPath
);
89 strcat(FileName
, LanguageFileName
);
90 LanguageFileHandle
= FsOpenFile(FileName
);
92 if (LanguageFileHandle
== NULL
)
95 LanguageFileSize
= FsGetFileSize(LanguageFileHandle
);
96 FsCloseFile(LanguageFileHandle
);
97 DbgPrint((DPRINT_WINDOWS
, "LanguageFileSize: %d\n", LanguageFileSize
));
99 /* Sum up all three length, having in mind that every one of them
100 must start at a page boundary => thus round up each file to a page */
101 TotalSize
= MM_SIZE_TO_PAGES(AnsiFileSize
) +
102 MM_SIZE_TO_PAGES(OemFileSize
) +
103 MM_SIZE_TO_PAGES(LanguageFileSize
);
105 NlsDataBase
= (ULONG_PTR
)MmAllocateMemory(TotalSize
*MM_PAGE_SIZE
);
107 if (NlsDataBase
== 0)
110 NlsVirtual
= (PVOID
)(KSEG0_BASE
| NlsDataBase
);
111 LoaderBlock
->NlsData
->AnsiCodePageData
= NlsVirtual
;
112 LoaderBlock
->NlsData
->OemCodePageData
= (PVOID
)((PUCHAR
)NlsVirtual
+
113 (MM_SIZE_TO_PAGES(AnsiFileSize
) << MM_PAGE_SHIFT
));
114 LoaderBlock
->NlsData
->UnicodeCodePageData
= (PVOID
)((PUCHAR
)NlsVirtual
+
115 (MM_SIZE_TO_PAGES(AnsiFileSize
) << MM_PAGE_SHIFT
) +
116 (MM_SIZE_TO_PAGES(OemFileSize
) << MM_PAGE_SHIFT
));
118 /* Ansi and OEM data are the same - just set pointers to the same area */
120 LoaderBlock
->NlsData
->OemCodePageData
= LoaderBlock
->NlsData
->AnsiCodePageData
;
123 /* Now actually read the data into memory, starting with Ansi file */
124 strcpy(FileName
, DirectoryPath
);
125 strcat(FileName
, AnsiFileName
);
126 AnsiFileHandle
= FsOpenFile(FileName
);
128 if (AnsiFileHandle
== NULL
)
131 Status
= FsReadFile(AnsiFileHandle
, AnsiFileSize
, NULL
, VaToPa(LoaderBlock
->NlsData
->AnsiCodePageData
));
136 FsCloseFile(AnsiFileHandle
);
138 /* OEM now, if it doesn't equal Ansi of course */
141 strcpy(FileName
, DirectoryPath
);
142 strcat(FileName
, OemFileName
);
143 OemFileHandle
= FsOpenFile(FileName
);
145 if (OemFileHandle
== NULL
)
148 Status
= FsReadFile(OemFileHandle
, OemFileSize
, NULL
, VaToPa(LoaderBlock
->NlsData
->OemCodePageData
));
153 FsCloseFile(AnsiFileHandle
);
156 /* finally the language file */
157 strcpy(FileName
, DirectoryPath
);
158 strcat(FileName
, LanguageFileName
);
159 LanguageFileHandle
= FsOpenFile(FileName
);
161 if (LanguageFileHandle
== NULL
)
164 Status
= FsReadFile(LanguageFileHandle
, LanguageFileSize
, NULL
, VaToPa(LoaderBlock
->NlsData
->UnicodeCodePageData
));
169 FsCloseFile(LanguageFileHandle
);
172 // THIS IS HAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACK
173 // Should go to WinLdrLoadOemHalFont(), when it will be implemented
175 LoaderBlock
->OemFontFile
= VaToPa(LoaderBlock
->NlsData
->UnicodeCodePageData
);
177 /* Convert NlsTables address to VA */
178 LoaderBlock
->NlsData
= PaToVa(LoaderBlock
->NlsData
);
183 //UiMessageBox("Error reading NLS file %s\n", Filename);
184 UiMessageBox("Error reading NLS file!");
188 void InitializeHWConfig(IN OUT PLOADER_PARAMETER_BLOCK LoaderBlock
)
190 PCONFIGURATION_COMPONENT_DATA ConfigurationRoot
;
191 PCONFIGURATION_COMPONENT Component
;
192 PCONFIGURATION_COMPONENT_DATA
/*CurrentEntry,*/ PreviousEntry
, AdapterEntry
;
193 BOOLEAN IsNextEntryChild
;
195 DbgPrint((DPRINT_WINDOWS
, "InitializeHWConfig()\n"));
197 LoaderBlock
->ConfigurationRoot
= MmAllocateMemory(sizeof(CONFIGURATION_COMPONENT_DATA
));
198 RtlZeroMemory(LoaderBlock
->ConfigurationRoot
, sizeof(CONFIGURATION_COMPONENT_DATA
));
200 /* Fill root == SystemClass */
201 ConfigurationRoot
= LoaderBlock
->ConfigurationRoot
;
202 Component
= &LoaderBlock
->ConfigurationRoot
->ComponentEntry
;
204 Component
->Class
= SystemClass
;
205 Component
->Type
= MaximumType
;
206 Component
->Version
= 0; // FIXME: ?
208 Component
->AffinityMask
= 0;
210 IsNextEntryChild
= TRUE
;
211 PreviousEntry
= ConfigurationRoot
;
213 /* Enumerate all PCI buses */
214 AdapterEntry
= ConfigurationRoot
;
216 /* TODO: Disk Geometry */
219 /* TODO: Serial port */
221 //Config->ConfigurationData = alloc(sizeof(CONFIGURATION_COMPONENT_DATA), EfiLoaderData);
223 /* Convert everything to VA */
224 ConvertConfigToVA(LoaderBlock
->ConfigurationRoot
);
225 LoaderBlock
->ConfigurationRoot
= PaToVa(LoaderBlock
->ConfigurationRoot
);
231 AllocateAndInitLPB(PLOADER_PARAMETER_BLOCK
*OutLoaderBlock
)
233 PLOADER_PARAMETER_BLOCK LoaderBlock
;
235 /* Allocate and zero-init the LPB */
236 LoaderBlock
= MmAllocateMemory(sizeof(LOADER_PARAMETER_BLOCK
));
237 RtlZeroMemory(LoaderBlock
, sizeof(LOADER_PARAMETER_BLOCK
));
239 /* Init three critical lists, used right away */
240 InitializeListHead(&LoaderBlock
->LoadOrderListHead
);
241 InitializeListHead(&LoaderBlock
->MemoryDescriptorListHead
);
242 InitializeListHead(&LoaderBlock
->BootDriverListHead
);
245 *OutLoaderBlock
= LoaderBlock
;
250 WinLdrInitializePhase1(PLOADER_PARAMETER_BLOCK LoaderBlock
)
252 //CHAR Options[] = "/CRASHDEBUG /DEBUGPORT=COM1 /BAUDRATE=115200";
253 CHAR Options
[] = "/NODEBUG";
254 CHAR SystemRoot
[] = "\\WINNT";
255 CHAR HalPath
[] = "\\";
256 CHAR ArcBoot
[] = "multi(0)disk(0)rdisk(1)partition(1)";
257 CHAR ArcHal
[] = "multi(0)disk(0)rdisk(1)partition(1)";
259 PLOADER_PARAMETER_EXTENSION Extension
;
261 LoaderBlock
->u
.I386
.CommonDataArea
= NULL
; // Force No ABIOS support
263 /* Fill Arc BootDevice */
264 LoaderBlock
->ArcBootDeviceName
= MmAllocateMemory(strlen(ArcBoot
)+1);
265 strcpy(LoaderBlock
->ArcBootDeviceName
, ArcBoot
);
266 LoaderBlock
->ArcBootDeviceName
= PaToVa(LoaderBlock
->ArcBootDeviceName
);
268 /* Fill Arc HalDevice */
269 LoaderBlock
->ArcHalDeviceName
= MmAllocateMemory(strlen(ArcHal
)+1);
270 strcpy(LoaderBlock
->ArcHalDeviceName
, ArcHal
);
271 LoaderBlock
->ArcHalDeviceName
= PaToVa(LoaderBlock
->ArcHalDeviceName
);
273 /* Fill SystemRoot */
274 LoaderBlock
->NtBootPathName
= MmAllocateMemory(strlen(SystemRoot
)+1);
275 strcpy(LoaderBlock
->NtBootPathName
, SystemRoot
);
276 LoaderBlock
->NtBootPathName
= PaToVa(LoaderBlock
->NtBootPathName
);
278 /* Fill NtHalPathName */
279 LoaderBlock
->NtHalPathName
= MmAllocateMemory(strlen(HalPath
)+1);
280 strcpy(LoaderBlock
->NtHalPathName
, HalPath
);
281 LoaderBlock
->NtHalPathName
= PaToVa(LoaderBlock
->NtHalPathName
);
283 /* Fill load options */
284 LoaderBlock
->LoadOptions
= MmAllocateMemory(strlen(Options
)+1);
285 strcpy(LoaderBlock
->LoadOptions
, Options
);
286 LoaderBlock
->LoadOptions
= PaToVa(LoaderBlock
->LoadOptions
);
289 LoaderBlock
->ArcDiskInformation
= (PARC_DISK_INFORMATION
)MmAllocateMemory(sizeof(ARC_DISK_INFORMATION
));
290 InitializeListHead(&LoaderBlock
->ArcDiskInformation
->DiskSignatureListHead
);
291 List_PaToVa(&LoaderBlock
->ArcDiskInformation
->DiskSignatureListHead
);
292 LoaderBlock
->ArcDiskInformation
= PaToVa(LoaderBlock
->ArcDiskInformation
);
294 /* Alloc space for NLS (it will be converted to VA in WinLdrLoadNLS) */
295 LoaderBlock
->NlsData
= MmAllocateMemory(sizeof(NLS_DATA_BLOCK
));
296 if (LoaderBlock
->NlsData
== NULL
)
298 UiMessageBox("Failed to allocate memory for NLS table data!");
301 RtlZeroMemory(LoaderBlock
->NlsData
, sizeof(NLS_DATA_BLOCK
));
303 /* Create configuration entries */
304 InitializeHWConfig(LoaderBlock
);
306 /* Convert all DTE into virtual addresses */
309 /* Convert all list's to Virtual address */
310 List_PaToVa(&LoaderBlock
->LoadOrderListHead
);
312 /* this one will be converted right before switching to
313 virtual paging mode */
314 //List_PaToVa(&LoaderBlock->MemoryDescriptorListHead);
316 List_PaToVa(&LoaderBlock
->BootDriverListHead
);
318 /* Initialize Extension now */
319 Extension
= MmAllocateMemory(sizeof(LOADER_PARAMETER_EXTENSION
));
320 if (Extension
== NULL
)
322 UiMessageBox("Failed to allocate LPB Extension!");
325 RtlZeroMemory(Extension
, sizeof(LOADER_PARAMETER_EXTENSION
));
327 Extension
->Size
= sizeof(LOADER_PARAMETER_EXTENSION
);
328 Extension
->MajorVersion
= 4;
329 Extension
->MinorVersion
= 0;
332 LoaderBlock
->Extension
= PaToVa(Extension
);
335 // Last step before going virtual
336 void WinLdrSetupForNt(PLOADER_PARAMETER_BLOCK LoaderBlock
,
345 ULONG BlockSize
, NumPages
;
347 LoaderBlock
->u
.I386
.CommonDataArea
= NULL
;//CommonDataArea;
348 //LoaderBlock->u.I386.MachineType = MachineType; //FIXME: MachineType?
350 /* Allocate 2 pages for PCR */
351 Pcr
= (ULONG_PTR
)MmAllocateMemory(2 * MM_PAGE_SIZE
);
352 *PcrBasePage
= Pcr
>> MM_PAGE_SHIFT
;
356 UiMessageBox("Can't allocate PCR\n");
361 TssSize
= (sizeof(KTSS
) + MM_PAGE_SIZE
) & ~(MM_PAGE_SIZE
- 1);
362 TssPages
= TssSize
/ MM_PAGE_SIZE
;
364 Tss
= (ULONG_PTR
)MmAllocateMemory(TssSize
);
366 *TssBasePage
= Tss
>> MM_PAGE_SHIFT
;
368 /* Allocate space for new GDT + IDT */
369 BlockSize
= NUM_GDT
*sizeof(KGDTENTRY
) + NUM_IDT
*sizeof(KIDTENTRY
);//FIXME: Use GDT/IDT limits here?
370 NumPages
= (BlockSize
+ MM_PAGE_SIZE
- 1) >> MM_PAGE_SHIFT
;
371 *GdtIdt
= (PKGDTENTRY
)MmAllocateMemory(NumPages
* MM_PAGE_SIZE
);
375 UiMessageBox("Can't allocate pages for GDT+IDT!\n");
379 /* Zero newly prepared GDT+IDT */
380 RtlZeroMemory(*GdtIdt
, NumPages
<< MM_PAGE_SHIFT
);
384 LoadAndBootWindows(PCSTR OperatingSystemName
, WORD OperatingSystemVersion
)
387 CHAR SystemPath
[1024], SearchPath
[1024];
390 PVOID NtosBase
= NULL
, HalBase
= NULL
, KdComBase
= NULL
;
394 PLOADER_PARAMETER_BLOCK LoaderBlock
, LoaderBlockVA
;
395 KERNEL_ENTRY_POINT KiSystemStartup
;
396 PLDR_DATA_TABLE_ENTRY KernelDTE
, HalDTE
, KdComDTE
;
404 //sprintf(MsgBuffer,"Booting Microsoft(R) Windows(R) OS version '%04x' is not implemented yet", OperatingSystemVersion);
405 //UiMessageBox(MsgBuffer);
407 // Open the operating system section
408 // specified in the .ini file
409 if (!IniOpenSection(OperatingSystemName
, &SectionId
))
411 sprintf(MsgBuffer
,"Operating System section '%s' not found in freeldr.ini", OperatingSystemName
);
412 UiMessageBox(MsgBuffer
);
416 /* Make sure the system path is set in the .ini file */
417 if (!IniReadSettingByName(SectionId
, "SystemPath", SystemPath
, sizeof(SystemPath
)))
419 UiMessageBox("System path not specified for selected operating system.");
423 if (!MachDiskNormalizeSystemPath(SystemPath
,
426 UiMessageBox("Invalid system path");
430 UiDrawStatusText("Loading...");
432 /* Try to open system drive */
433 BootDevice
= 0xffffffff;
434 if (!FsOpenSystemVolume(SystemPath
, BootPath
, &BootDevice
))
436 UiMessageBox("Failed to open boot drive.");
440 /* append a backslash */
441 if ((strlen(BootPath
)==0) ||
442 BootPath
[strlen(BootPath
)] != '\\')
443 strcat(BootPath
, "\\");
445 DbgPrint((DPRINT_WINDOWS
,"SystemRoot: '%s'\n", BootPath
));
447 // Allocate and minimalistic-initialize LPB
448 AllocateAndInitLPB(&LoaderBlock
);
451 strcpy(FileName
, BootPath
);
452 strcat(FileName
, "SYSTEM32\\NTOSKRNL.EXE");
453 Status
= WinLdrLoadImage(FileName
, &NtosBase
);
454 DbgPrint((DPRINT_WINDOWS
, "Ntos loaded with status %d at %p\n", Status
, NtosBase
));
457 strcpy(FileName
, BootPath
);
458 strcat(FileName
, "SYSTEM32\\HAL.DLL");
459 Status
= WinLdrLoadImage(FileName
, &HalBase
);
460 DbgPrint((DPRINT_WINDOWS
, "HAL loaded with status %d at %p\n", Status
, HalBase
));
462 // Load kernel-debugger support dll
463 if (OperatingSystemVersion
> _WIN32_WINNT_NT4
)
465 strcpy(FileName
, BootPath
);
466 strcat(FileName
, "SYSTEM32\\KDCOM.DLL");
467 Status
= WinLdrLoadImage(FileName
, &KdComBase
);
468 DbgPrint((DPRINT_WINDOWS
, "KdCom loaded with status %d at %p\n", Status
, KdComBase
));
471 // Allocate data table entries for above-loaded modules
472 WinLdrAllocateDataTableEntry(LoaderBlock
, "ntoskrnl.exe",
473 "WINNT\\SYSTEM32\\NTOSKRNL.EXE", NtosBase
, &KernelDTE
);
474 WinLdrAllocateDataTableEntry(LoaderBlock
, "hal.dll",
475 "WINNT\\SYSTEM32\\HAL.DLL", HalBase
, &HalDTE
);
476 if (OperatingSystemVersion
> _WIN32_WINNT_NT4
)
478 WinLdrAllocateDataTableEntry(LoaderBlock
, "kdcom.dll",
479 "WINNT\\SYSTEM32\\KDCOM.DLL", KdComBase
, &KdComDTE
);
482 /* Load all referenced DLLs for kernel, HAL and kdcom.dll */
483 strcpy(SearchPath
, BootPath
);
484 strcat(SearchPath
, "SYSTEM32\\");
485 WinLdrScanImportDescriptorTable(LoaderBlock
, SearchPath
, KernelDTE
);
486 WinLdrScanImportDescriptorTable(LoaderBlock
, SearchPath
, HalDTE
);
488 WinLdrScanImportDescriptorTable(LoaderBlock
, SearchPath
, KdComDTE
);
490 /* Initialize Phase 1 - before NLS */
491 WinLdrInitializePhase1(LoaderBlock
);
493 /* Load Hive, and then NLS data, OEM font, and prepare boot drivers list */
494 Status
= WinLdrLoadAndScanSystemHive(LoaderBlock
, BootPath
);
495 DbgPrint((DPRINT_WINDOWS
, "SYSTEM hive loaded and scanned with status %d\n", Status
));
497 /* FIXME: Load NLS data, should be moved to WinLdrLoadAndScanSystemHive() */
498 strcpy(SearchPath
, BootPath
);
499 strcat(SearchPath
, "SYSTEM32\\");
500 Status
= WinLdrLoadNLSData(LoaderBlock
, SearchPath
,
501 "c_1252.nls", "c_437.nls", "l_intl.nls");
502 DbgPrint((DPRINT_WINDOWS
, "NLS data loaded with status %d\n", Status
));
504 /* FIXME: Load OEM HAL font, should be moved to WinLdrLoadAndScanSystemHive() */
506 /* Load boot drivers */
507 //WinLdrLoadBootDrivers();
509 /* Alloc PCR, TSS, do magic things with the GDT/IDT */
510 WinLdrSetupForNt(LoaderBlock
, &GdtIdt
, &PcrBasePage
, &TssBasePage
);
512 /* Save entry-point pointer (VA) */
513 KiSystemStartup
= (KERNEL_ENTRY_POINT
)KernelDTE
->EntryPoint
;
515 LoaderBlockVA
= PaToVa(LoaderBlock
);
518 //DumpMemoryAllocMap();
520 /* Turn on paging mode of CPU*/
521 WinLdrTurnOnPaging(LoaderBlock
, PcrBasePage
, TssBasePage
, GdtIdt
);
523 DbgPrint((DPRINT_WINDOWS
, "Hello from paged mode, KiSystemStartup %p, LoaderBlockVA %p!\n",
524 KiSystemStartup
, LoaderBlockVA
));
526 WinLdrpDumpMemoryDescriptors(LoaderBlockVA
);
528 //FIXME: If I substitute this debugging checkpoint, GCC will "optimize away" the code below
530 /*asm(".intel_syntax noprefix\n");
533 asm(".att_syntax\n");*/
536 (*KiSystemStartup
)(LoaderBlockVA
);
542 WinLdrpDumpMemoryDescriptors(PLOADER_PARAMETER_BLOCK LoaderBlock
)
545 PMEMORY_ALLOCATION_DESCRIPTOR MemoryDescriptor
;
547 NextMd
= LoaderBlock
->MemoryDescriptorListHead
.Flink
;
549 while (NextMd
!= &LoaderBlock
->MemoryDescriptorListHead
)
551 MemoryDescriptor
= CONTAINING_RECORD(NextMd
, MEMORY_ALLOCATION_DESCRIPTOR
, ListEntry
);
554 DbgPrint((DPRINT_WINDOWS
, "BP %08X PC %04X MT %d\n", MemoryDescriptor
->BasePage
,
555 MemoryDescriptor
->PageCount
, MemoryDescriptor
->MemoryType
));
557 NextMd
= MemoryDescriptor
->ListEntry
.Flink
;