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!");
189 WinLdrLoadSystemHive(IN OUT PLOADER_PARAMETER_BLOCK LoaderBlock
,
190 IN LPCSTR DirectoryPath
,
194 CHAR FullHiveName
[256];
197 ULONG_PTR HiveDataPhysical
;
198 PVOID HiveDataVirtual
;
200 /* Concatenate path and filename to get the full name */
201 strcpy(FullHiveName
, DirectoryPath
);
202 strcat(FullHiveName
, HiveName
);
203 //Print(L"Loading %s...\n", FullHiveName);
204 FileHandle
= FsOpenFile(FullHiveName
);
206 if (FileHandle
== NULL
)
208 UiMessageBox("Opening hive file failed!");
212 /* Get the file length */
213 HiveFileSize
= FsGetFileSize(FileHandle
);
215 if (HiveFileSize
== 0)
217 FsCloseFile(FileHandle
);
218 UiMessageBox("Hive file has 0 size!");
222 /* Round up the size to page boundary and alloc memory */
223 HiveDataPhysical
= (ULONG_PTR
)MmAllocateMemory(
224 MM_SIZE_TO_PAGES(HiveFileSize
+ MM_PAGE_SIZE
- 1) << MM_PAGE_SHIFT
);
226 if (HiveDataPhysical
== 0)
228 FsCloseFile(FileHandle
);
229 UiMessageBox("Unable to alloc memory for a hive!");
233 /* Convert address to virtual */
234 HiveDataVirtual
= (PVOID
)(KSEG0_BASE
| HiveDataPhysical
);
236 /* Fill LoaderBlock's entries */
237 LoaderBlock
->RegistryLength
= HiveFileSize
;
238 LoaderBlock
->RegistryBase
= HiveDataVirtual
;
240 /* Finally read from file to the memory */
241 Status
= FsReadFile(FileHandle
, HiveFileSize
, NULL
, (PVOID
)HiveDataPhysical
);
242 FsCloseFile(FileHandle
);
245 UiMessageBox("Unable to read from hive file!");
252 void InitializeHWConfig(IN OUT PLOADER_PARAMETER_BLOCK LoaderBlock
)
254 PCONFIGURATION_COMPONENT_DATA ConfigurationRoot
;
255 PCONFIGURATION_COMPONENT Component
;
256 PCONFIGURATION_COMPONENT_DATA
/*CurrentEntry,*/ PreviousEntry
, AdapterEntry
;
257 BOOLEAN IsNextEntryChild
;
259 DbgPrint((DPRINT_WINDOWS
, "InitializeHWConfig()\n"));
261 LoaderBlock
->ConfigurationRoot
= MmAllocateMemory(sizeof(CONFIGURATION_COMPONENT_DATA
));
262 RtlZeroMemory(LoaderBlock
->ConfigurationRoot
, sizeof(CONFIGURATION_COMPONENT_DATA
));
264 /* Fill root == SystemClass */
265 ConfigurationRoot
= LoaderBlock
->ConfigurationRoot
;
266 Component
= &LoaderBlock
->ConfigurationRoot
->ComponentEntry
;
268 Component
->Class
= SystemClass
;
269 Component
->Type
= MaximumType
;
270 Component
->Version
= 0; // FIXME: ?
272 Component
->AffinityMask
= 0;
274 IsNextEntryChild
= TRUE
;
275 PreviousEntry
= ConfigurationRoot
;
277 /* Enumerate all PCI buses */
278 AdapterEntry
= ConfigurationRoot
;
280 /* TODO: Disk Geometry */
283 /* TODO: Serial port */
285 //Config->ConfigurationData = alloc(sizeof(CONFIGURATION_COMPONENT_DATA), EfiLoaderData);
287 /* Convert everything to VA */
288 ConvertConfigToVA(LoaderBlock
->ConfigurationRoot
);
289 LoaderBlock
->ConfigurationRoot
= PaToVa(LoaderBlock
->ConfigurationRoot
);
295 AllocateAndInitLPB(PLOADER_PARAMETER_BLOCK
*OutLoaderBlock
)
297 PLOADER_PARAMETER_BLOCK LoaderBlock
;
299 /* Allocate and zero-init the LPB */
300 LoaderBlock
= MmAllocateMemory(sizeof(LOADER_PARAMETER_BLOCK
));
301 RtlZeroMemory(LoaderBlock
, sizeof(LOADER_PARAMETER_BLOCK
));
303 /* Init three critical lists, used right away */
304 InitializeListHead(&LoaderBlock
->LoadOrderListHead
);
305 InitializeListHead(&LoaderBlock
->MemoryDescriptorListHead
);
306 InitializeListHead(&LoaderBlock
->BootDriverListHead
);
309 *OutLoaderBlock
= LoaderBlock
;
314 WinLdrInitializePhase1(PLOADER_PARAMETER_BLOCK LoaderBlock
)
316 //CHAR Options[] = "/DEBUG /DEBUGPORT=COM1 /BAUDRATE=115200";
317 CHAR Options
[] = "/NODEBUG";
318 CHAR SystemRoot
[] = "\\WINNT";
319 CHAR HalPath
[] = "\\";
320 CHAR ArcBoot
[] = "multi(0)";
321 CHAR ArcHal
[] = "multi(0)";
323 PLOADER_PARAMETER_EXTENSION Extension
;
325 LoaderBlock
->u
.I386
.CommonDataArea
= NULL
; // Force No ABIOS support
327 /* Fill Arc BootDevice */
328 LoaderBlock
->ArcBootDeviceName
= MmAllocateMemory(strlen(ArcBoot
)+1);
329 strcpy(LoaderBlock
->ArcBootDeviceName
, ArcBoot
);
330 LoaderBlock
->ArcBootDeviceName
= PaToVa(LoaderBlock
->ArcBootDeviceName
);
332 /* Fill Arc HalDevice */
333 LoaderBlock
->ArcHalDeviceName
= MmAllocateMemory(strlen(ArcHal
)+1);
334 strcpy(LoaderBlock
->ArcHalDeviceName
, ArcHal
);
335 LoaderBlock
->ArcHalDeviceName
= PaToVa(LoaderBlock
->ArcHalDeviceName
);
337 /* Fill SystemRoot */
338 LoaderBlock
->NtBootPathName
= MmAllocateMemory(strlen(SystemRoot
)+1);
339 strcpy(LoaderBlock
->NtBootPathName
, SystemRoot
);
340 LoaderBlock
->NtBootPathName
= PaToVa(LoaderBlock
->NtBootPathName
);
342 /* Fill NtHalPathName */
343 LoaderBlock
->NtHalPathName
= MmAllocateMemory(strlen(HalPath
)+1);
344 strcpy(LoaderBlock
->NtHalPathName
, HalPath
);
345 LoaderBlock
->NtHalPathName
= PaToVa(LoaderBlock
->NtHalPathName
);
347 /* Fill load options */
348 LoaderBlock
->LoadOptions
= MmAllocateMemory(strlen(Options
)+1);
349 strcpy(LoaderBlock
->LoadOptions
, Options
);
350 LoaderBlock
->LoadOptions
= PaToVa(LoaderBlock
->LoadOptions
);
353 LoaderBlock
->ArcDiskInformation
= (PARC_DISK_INFORMATION
)MmAllocateMemory(sizeof(ARC_DISK_INFORMATION
));
354 InitializeListHead(&LoaderBlock
->ArcDiskInformation
->DiskSignatureListHead
);
355 List_PaToVa(&LoaderBlock
->ArcDiskInformation
->DiskSignatureListHead
);
356 LoaderBlock
->ArcDiskInformation
= PaToVa(LoaderBlock
->ArcDiskInformation
);
358 /* Alloc space for NLS (it will be converted to VA in WinLdrLoadNLS) */
359 LoaderBlock
->NlsData
= MmAllocateMemory(sizeof(NLS_DATA_BLOCK
));
360 if (LoaderBlock
->NlsData
== NULL
)
362 UiMessageBox("Failed to allocate memory for NLS table data!");
365 RtlZeroMemory(LoaderBlock
->NlsData
, sizeof(NLS_DATA_BLOCK
));
367 /* Create configuration entries */
368 InitializeHWConfig(LoaderBlock
);
370 /* Convert all DTE into virtual addresses */
373 /* Convert all list's to Virtual address */
374 List_PaToVa(&LoaderBlock
->LoadOrderListHead
);
376 /* this one will be converted right before switching to
377 virtual paging mode */
378 //List_PaToVa(&LoaderBlock->MemoryDescriptorListHead);
380 List_PaToVa(&LoaderBlock
->BootDriverListHead
);
382 /* Initialize Extension now */
383 Extension
= MmAllocateMemory(sizeof(LOADER_PARAMETER_EXTENSION
));
384 if (Extension
== NULL
)
386 UiMessageBox("Failed to allocate LPB Extension!");
389 RtlZeroMemory(Extension
, sizeof(LOADER_PARAMETER_EXTENSION
));
391 Extension
->Size
= sizeof(LOADER_PARAMETER_EXTENSION
);
392 Extension
->MajorVersion
= 4;
393 Extension
->MinorVersion
= 0;
396 LoaderBlock
->Extension
= PaToVa(Extension
);
399 // Last step before going virtual
400 void WinLdrSetupForNt(PLOADER_PARAMETER_BLOCK LoaderBlock
,
409 ULONG BlockSize
, NumPages
;
411 LoaderBlock
->u
.I386
.CommonDataArea
= NULL
;//CommonDataArea;
412 //LoaderBlock->u.I386.MachineType = MachineType; //FIXME: MachineType?
414 /* Allocate 2 pages for PCR */
415 Pcr
= (ULONG_PTR
)MmAllocateMemory(2 * MM_PAGE_SIZE
);
416 *PcrBasePage
= Pcr
>> MM_PAGE_SHIFT
;
420 UiMessageBox("Can't allocate PCR\n");
425 TssSize
= (sizeof(KTSS
) + MM_PAGE_SIZE
) & ~(MM_PAGE_SIZE
- 1);
426 TssPages
= TssSize
/ MM_PAGE_SIZE
;
428 Tss
= (ULONG_PTR
)MmAllocateMemory(TssSize
);
430 *TssBasePage
= Tss
>> MM_PAGE_SHIFT
;
432 /* Allocate space for new GDT + IDT */
433 BlockSize
= NUM_GDT
*sizeof(KGDTENTRY
) + NUM_IDT
*sizeof(KIDTENTRY
);//FIXME: Use GDT/IDT limits here?
434 NumPages
= (BlockSize
+ MM_PAGE_SIZE
- 1) >> MM_PAGE_SHIFT
;
435 *GdtIdt
= (PKGDTENTRY
)MmAllocateMemory(NumPages
* MM_PAGE_SIZE
);
439 UiMessageBox("Can't allocate pages for GDT+IDT!\n");
443 /* Zero newly prepared GDT+IDT */
444 RtlZeroMemory(*GdtIdt
, NumPages
<< MM_PAGE_SHIFT
);
448 LoadAndBootWindows(PCSTR OperatingSystemName
, WORD OperatingSystemVersion
)
451 CHAR SystemPath
[1024], SearchPath
[1024];
454 PVOID NtosBase
= NULL
, HalBase
= NULL
;
458 PLOADER_PARAMETER_BLOCK LoaderBlock
, LoaderBlockVA
;
459 KERNEL_ENTRY_POINT KiSystemStartup
;
460 PLDR_DATA_TABLE_ENTRY KernelDTE
, HalDTE
;
468 //sprintf(MsgBuffer,"Booting Microsoft(R) Windows(R) OS version '%04x' is not implemented yet", OperatingSystemVersion);
469 //UiMessageBox(MsgBuffer);
472 // Open the operating system section
473 // specified in the .ini file
475 if (!IniOpenSection(OperatingSystemName
, &SectionId
))
477 sprintf(MsgBuffer
,"Operating System section '%s' not found in freeldr.ini", OperatingSystemName
);
478 UiMessageBox(MsgBuffer
);
483 * Make sure the system path is set in the .ini file
485 if (!IniReadSettingByName(SectionId
, "SystemPath", SystemPath
, sizeof(SystemPath
)))
487 UiMessageBox("System path not specified for selected operating system.");
491 if (!MachDiskNormalizeSystemPath(SystemPath
,
494 UiMessageBox("Invalid system path");
498 UiDrawStatusText("Loading...");
501 * Try to open system drive
503 BootDevice
= 0xffffffff;
504 if (!FsOpenSystemVolume(SystemPath
, BootPath
, &BootDevice
))
506 UiMessageBox("Failed to open boot drive.");
510 /* append a backslash */
511 if ((strlen(BootPath
)==0) ||
512 BootPath
[strlen(BootPath
)] != '\\')
513 strcat(BootPath
, "\\");
515 DbgPrint((DPRINT_WINDOWS
,"SystemRoot: '%s'\n", BootPath
));
517 /* Allocate and minimalistic-initialize LPB */
518 AllocateAndInitLPB(&LoaderBlock
);
521 strcpy(FileName
, BootPath
);
522 strcat(FileName
, "SYSTEM32\\NTOSKRNL.EXE");
523 Status
= WinLdrLoadImage(FileName
, &NtosBase
);
524 DbgPrint((DPRINT_WINDOWS
, "Ntos loaded with status %d\n", Status
));
527 strcpy(FileName
, BootPath
);
528 strcat(FileName
, "SYSTEM32\\HAL.DLL");
529 Status
= WinLdrLoadImage(FileName
, &HalBase
);
530 DbgPrint((DPRINT_WINDOWS
, "HAL loaded with status %d\n", Status
));
532 WinLdrAllocateDataTableEntry(LoaderBlock
, "ntoskrnl.exe",
533 "WINNT\\SYSTEM32\\NTOSKRNL.EXE", NtosBase
, &KernelDTE
);
534 WinLdrAllocateDataTableEntry(LoaderBlock
, "hal.dll",
535 "WINNT\\SYSTEM32\\HAL.EXE", HalBase
, &HalDTE
);
537 /* Load all referenced DLLs for kernel and HAL */
538 strcpy(SearchPath
, BootPath
);
539 strcat(SearchPath
, "SYSTEM32\\");
540 WinLdrScanImportDescriptorTable(LoaderBlock
, SearchPath
, KernelDTE
);
541 WinLdrScanImportDescriptorTable(LoaderBlock
, SearchPath
, HalDTE
);
543 /* Initialize Phase 1 - before NLS */
544 WinLdrInitializePhase1(LoaderBlock
);
546 /* Load SYSTEM hive and its LOG file */
547 strcpy(SearchPath
, BootPath
);
548 strcat(SearchPath
, "SYSTEM32\\CONFIG\\");
549 Status
= WinLdrLoadSystemHive(LoaderBlock
, SearchPath
, "SYSTEM");
550 DbgPrint((DPRINT_WINDOWS
, "SYSTEM hive loaded with status %d\n", Status
));
553 strcpy(SearchPath
, BootPath
);
554 strcat(SearchPath
, "SYSTEM32\\");
555 Status
= WinLdrLoadNLSData(LoaderBlock
, SearchPath
,
556 "c_1252.nls", "c_437.nls", "l_intl.nls");
557 DbgPrint((DPRINT_WINDOWS
, "NLS data loaded with status %d\n", Status
));
559 /* Load OEM HAL font */
561 /* Load boot drivers */
563 /* Alloc PCR, TSS, do magic things with the GDT/IDT */
564 WinLdrSetupForNt(LoaderBlock
, &GdtIdt
, &PcrBasePage
, &TssBasePage
);
566 /* Save entry-point pointer (VA) */
567 KiSystemStartup
= (KERNEL_ENTRY_POINT
)KernelDTE
->EntryPoint
;
569 LoaderBlockVA
= PaToVa(LoaderBlock
);
572 //DumpMemoryAllocMap();
574 /* Turn on paging mode of CPU*/
575 WinLdrTurnOnPaging(LoaderBlock
, PcrBasePage
, TssBasePage
, GdtIdt
);
577 DbgPrint((DPRINT_WINDOWS
, "Hello from paged mode, KiSystemStartup %p, LoaderBlockVA %p!\n",
578 KiSystemStartup
, LoaderBlockVA
));
580 WinLdrpDumpMemoryDescriptors(LoaderBlockVA
);
582 // temp: offset C9000
584 //FIXME: If I substitute this debugging checkpoint, GCC will "optimize away" the code below
586 asm(".intel_syntax noprefix\n");
589 asm(".att_syntax\n");
592 (*KiSystemStartup
)(LoaderBlockVA
);
598 WinLdrpDumpMemoryDescriptors(PLOADER_PARAMETER_BLOCK LoaderBlock
)
601 PMEMORY_ALLOCATION_DESCRIPTOR MemoryDescriptor
;
603 NextMd
= LoaderBlock
->MemoryDescriptorListHead
.Flink
;
605 while (NextMd
!= &LoaderBlock
->MemoryDescriptorListHead
)
607 MemoryDescriptor
= CONTAINING_RECORD(NextMd
, MEMORY_ALLOCATION_DESCRIPTOR
, ListEntry
);
610 DbgPrint((DPRINT_WINDOWS
, "BP %08X PC %04X MT %d\n", MemoryDescriptor
->BasePage
,
611 MemoryDescriptor
->PageCount
, MemoryDescriptor
->MemoryType
));
613 NextMd
= MemoryDescriptor
->ListEntry
.Flink
;