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.
27 VOID
DumpMemoryAllocMap(VOID
);
28 VOID
WinLdrpDumpMemoryDescriptors(PLOADER_PARAMETER_BLOCK LoaderBlock
);
31 WinLdrLoadNLSData(IN OUT PLOADER_PARAMETER_BLOCK LoaderBlock
,
32 IN LPCSTR DirectoryPath
,
33 IN LPCSTR AnsiFileName
,
34 IN LPCSTR OemFileName
,
35 IN LPCSTR LanguageFileName
)
40 PFILE LanguageFileHandle
;
41 ULONG AnsiFileSize
, OemFileSize
, LanguageFileSize
;
43 ULONG_PTR NlsDataBase
;
45 BOOLEAN Status
, AnsiEqualsOem
= FALSE
;
47 /* There may be a case, when OEM and ANSI page coincide */
48 if (!strcmp(AnsiFileName
, OemFileName
))
51 /* Open file with ANSI and store its size */
52 //Print(L"Loading %s...\n", Filename);
53 strcpy(FileName
, DirectoryPath
);
54 strcat(FileName
, AnsiFileName
);
55 AnsiFileHandle
= FsOpenFile(FileName
);
57 if (AnsiFileHandle
== NULL
)
60 AnsiFileSize
= FsGetFileSize(AnsiFileHandle
);
61 DbgPrint((DPRINT_WINDOWS
, "AnsiFileSize: %d\n", AnsiFileSize
));
62 FsCloseFile(AnsiFileHandle
);
64 /* Open OEM file and store its length */
71 //Print(L"Loading %s...\n", Filename);
72 strcpy(FileName
, DirectoryPath
);
73 strcat(FileName
, OemFileName
);
74 OemFileHandle
= FsOpenFile(FileName
);
76 if (OemFileHandle
== NULL
)
79 OemFileSize
= FsGetFileSize(OemFileHandle
);
80 FsCloseFile(OemFileHandle
);
82 DbgPrint((DPRINT_WINDOWS
, "OemFileSize: %d\n", OemFileSize
));
84 /* And finally open the language codepage file and store its length */
85 //Print(L"Loading %s...\n", Filename);
86 strcpy(FileName
, DirectoryPath
);
87 strcat(FileName
, LanguageFileName
);
88 LanguageFileHandle
= FsOpenFile(FileName
);
90 if (LanguageFileHandle
== NULL
)
93 LanguageFileSize
= FsGetFileSize(LanguageFileHandle
);
94 FsCloseFile(LanguageFileHandle
);
95 DbgPrint((DPRINT_WINDOWS
, "LanguageFileSize: %d\n", LanguageFileSize
));
97 /* Sum up all three length, having in mind that every one of them
98 must start at a page boundary => thus round up each file to a page */
99 TotalSize
= MM_SIZE_TO_PAGES(AnsiFileSize
) +
100 MM_SIZE_TO_PAGES(OemFileSize
) +
101 MM_SIZE_TO_PAGES(LanguageFileSize
);
103 NlsDataBase
= (ULONG_PTR
)MmAllocateMemory(TotalSize
*MM_PAGE_SIZE
);
105 if (NlsDataBase
== 0)
108 NlsVirtual
= (PVOID
)(KSEG0_BASE
| NlsDataBase
);
109 LoaderBlock
->NlsData
->AnsiCodePageData
= NlsVirtual
;
110 LoaderBlock
->NlsData
->OemCodePageData
= (PVOID
)((PUCHAR
)NlsVirtual
+
111 (MM_SIZE_TO_PAGES(AnsiFileSize
) << MM_PAGE_SHIFT
));
112 LoaderBlock
->NlsData
->UnicodeCodePageData
= (PVOID
)((PUCHAR
)NlsVirtual
+
113 (MM_SIZE_TO_PAGES(AnsiFileSize
) << MM_PAGE_SHIFT
) +
114 (MM_SIZE_TO_PAGES(OemFileSize
) << MM_PAGE_SHIFT
));
116 /* Ansi and OEM data are the same - just set pointers to the same area */
118 LoaderBlock
->NlsData
->OemCodePageData
= LoaderBlock
->NlsData
->AnsiCodePageData
;
121 /* Now actually read the data into memory, starting with Ansi file */
122 strcpy(FileName
, DirectoryPath
);
123 strcat(FileName
, AnsiFileName
);
124 AnsiFileHandle
= FsOpenFile(FileName
);
126 if (AnsiFileHandle
== NULL
)
129 Status
= FsReadFile(AnsiFileHandle
, AnsiFileSize
, NULL
, VaToPa(LoaderBlock
->NlsData
->AnsiCodePageData
));
134 FsCloseFile(AnsiFileHandle
);
136 /* OEM now, if it doesn't equal Ansi of course */
139 strcpy(FileName
, DirectoryPath
);
140 strcat(FileName
, OemFileName
);
141 OemFileHandle
= FsOpenFile(FileName
);
143 if (OemFileHandle
== NULL
)
146 Status
= FsReadFile(OemFileHandle
, OemFileSize
, NULL
, VaToPa(LoaderBlock
->NlsData
->OemCodePageData
));
151 FsCloseFile(AnsiFileHandle
);
154 /* finally the language file */
155 strcpy(FileName
, DirectoryPath
);
156 strcat(FileName
, LanguageFileName
);
157 LanguageFileHandle
= FsOpenFile(FileName
);
159 if (LanguageFileHandle
== NULL
)
162 Status
= FsReadFile(LanguageFileHandle
, LanguageFileSize
, NULL
, VaToPa(LoaderBlock
->NlsData
->UnicodeCodePageData
));
167 FsCloseFile(LanguageFileHandle
);
170 // THIS IS HAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACK
171 // Should go to WinLdrLoadOemHalFont(), when it will be implemented
173 LoaderBlock
->OemFontFile
= VaToPa(LoaderBlock
->NlsData
->UnicodeCodePageData
);
175 /* Convert NlsTables address to VA */
176 LoaderBlock
->NlsData
= PaToVa(LoaderBlock
->NlsData
);
181 //UiMessageBox("Error reading NLS file %s\n", Filename);
182 UiMessageBox("Error reading NLS file!");
187 WinLdrLoadSystemHive(IN OUT PLOADER_PARAMETER_BLOCK LoaderBlock
,
188 IN LPCSTR DirectoryPath
,
192 CHAR FullHiveName
[256];
195 ULONG_PTR HiveDataPhysical
;
196 PVOID HiveDataVirtual
;
198 /* Concatenate path and filename to get the full name */
199 strcpy(FullHiveName
, DirectoryPath
);
200 strcat(FullHiveName
, HiveName
);
201 //Print(L"Loading %s...\n", FullHiveName);
202 FileHandle
= FsOpenFile(FullHiveName
);
204 if (FileHandle
== NULL
)
206 UiMessageBox("Opening hive file failed!");
210 /* Get the file length */
211 HiveFileSize
= FsGetFileSize(FileHandle
);
213 if (HiveFileSize
== 0)
215 FsCloseFile(FileHandle
);
216 UiMessageBox("Hive file has 0 size!");
220 /* Round up the size to page boundary and alloc memory */
221 HiveDataPhysical
= (ULONG_PTR
)MmAllocateMemory(
222 MM_SIZE_TO_PAGES(HiveFileSize
+ MM_PAGE_SIZE
- 1) << MM_PAGE_SHIFT
);
224 if (HiveDataPhysical
== 0)
226 FsCloseFile(FileHandle
);
227 UiMessageBox("Unable to alloc memory for a hive!");
231 /* Convert address to virtual */
232 HiveDataVirtual
= (PVOID
)(KSEG0_BASE
| HiveDataPhysical
);
234 /* Fill LoaderBlock's entries */
235 LoaderBlock
->RegistryLength
= HiveFileSize
;
236 LoaderBlock
->RegistryBase
= HiveDataVirtual
;
238 /* Finally read from file to the memory */
239 Status
= FsReadFile(FileHandle
, HiveFileSize
, NULL
, (PVOID
)HiveDataPhysical
);
240 FsCloseFile(FileHandle
);
243 UiMessageBox("Unable to read from hive file!");
250 void InitializeHWConfig(IN OUT PLOADER_PARAMETER_BLOCK LoaderBlock
)
252 PCONFIGURATION_COMPONENT_DATA ConfigurationRoot
;
253 PCONFIGURATION_COMPONENT Component
;
254 PCONFIGURATION_COMPONENT_DATA
/*CurrentEntry,*/ PreviousEntry
, AdapterEntry
;
255 BOOLEAN IsNextEntryChild
;
257 DbgPrint((DPRINT_WINDOWS
, "InitializeHWConfig()\n"));
259 LoaderBlock
->ConfigurationRoot
= MmAllocateMemory(sizeof(CONFIGURATION_COMPONENT_DATA
));
260 RtlZeroMemory(LoaderBlock
->ConfigurationRoot
, sizeof(CONFIGURATION_COMPONENT_DATA
));
262 /* Fill root == SystemClass */
263 ConfigurationRoot
= LoaderBlock
->ConfigurationRoot
;
264 Component
= &LoaderBlock
->ConfigurationRoot
->ComponentEntry
;
266 Component
->Class
= SystemClass
;
267 Component
->Type
= MaximumType
;
268 Component
->Version
= 0; // FIXME: ?
270 Component
->AffinityMask
= 0;
272 IsNextEntryChild
= TRUE
;
273 PreviousEntry
= ConfigurationRoot
;
275 /* Enumerate all PCI buses */
276 AdapterEntry
= ConfigurationRoot
;
278 /* TODO: Disk Geometry */
281 /* TODO: Serial port */
283 //Config->ConfigurationData = alloc(sizeof(CONFIGURATION_COMPONENT_DATA), EfiLoaderData);
285 /* Convert everything to VA */
286 ConvertConfigToVA(LoaderBlock
->ConfigurationRoot
);
287 LoaderBlock
->ConfigurationRoot
= PaToVa(LoaderBlock
->ConfigurationRoot
);
293 AllocateAndInitLPB(PLOADER_PARAMETER_BLOCK
*OutLoaderBlock
)
295 PLOADER_PARAMETER_BLOCK LoaderBlock
;
297 /* Allocate and zero-init the LPB */
298 LoaderBlock
= MmAllocateMemory(sizeof(LOADER_PARAMETER_BLOCK
));
299 RtlZeroMemory(LoaderBlock
, sizeof(LOADER_PARAMETER_BLOCK
));
301 /* Init three critical lists, used right away */
302 InitializeListHead(&LoaderBlock
->LoadOrderListHead
);
303 InitializeListHead(&LoaderBlock
->MemoryDescriptorListHead
);
304 InitializeListHead(&LoaderBlock
->BootDriverListHead
);
307 *OutLoaderBlock
= LoaderBlock
;
312 WinLdrInitializePhase1(PLOADER_PARAMETER_BLOCK LoaderBlock
)
314 //CHAR Options[] = "/DEBUG /DEBUGPORT=COM1 /BAUDRATE=115200";
315 CHAR Options
[] = "/NODEBUG";
316 CHAR SystemRoot
[] = "\\WINNT";
317 CHAR HalPath
[] = "\\";
318 CHAR ArcBoot
[] = "multi(0)";
319 CHAR ArcHal
[] = "multi(0)";
321 PLOADER_PARAMETER_EXTENSION Extension
;
323 LoaderBlock
->u
.I386
.CommonDataArea
= NULL
; // Force No ABIOS support
325 /* Fill Arc BootDevice */
326 LoaderBlock
->ArcBootDeviceName
= MmAllocateMemory(strlen(ArcBoot
)+1);
327 strcpy(LoaderBlock
->ArcBootDeviceName
, ArcBoot
);
328 LoaderBlock
->ArcBootDeviceName
= PaToVa(LoaderBlock
->ArcBootDeviceName
);
330 /* Fill Arc HalDevice */
331 LoaderBlock
->ArcHalDeviceName
= MmAllocateMemory(strlen(ArcHal
)+1);
332 strcpy(LoaderBlock
->ArcHalDeviceName
, ArcHal
);
333 LoaderBlock
->ArcHalDeviceName
= PaToVa(LoaderBlock
->ArcHalDeviceName
);
335 /* Fill SystemRoot */
336 LoaderBlock
->NtBootPathName
= MmAllocateMemory(strlen(SystemRoot
)+1);
337 strcpy(LoaderBlock
->NtBootPathName
, SystemRoot
);
338 LoaderBlock
->NtBootPathName
= PaToVa(LoaderBlock
->NtBootPathName
);
340 /* Fill NtHalPathName */
341 LoaderBlock
->NtHalPathName
= MmAllocateMemory(strlen(HalPath
)+1);
342 strcpy(LoaderBlock
->NtHalPathName
, HalPath
);
343 LoaderBlock
->NtHalPathName
= PaToVa(LoaderBlock
->NtHalPathName
);
345 /* Fill load options */
346 LoaderBlock
->LoadOptions
= MmAllocateMemory(strlen(Options
)+1);
347 strcpy(LoaderBlock
->LoadOptions
, Options
);
348 LoaderBlock
->LoadOptions
= PaToVa(LoaderBlock
->LoadOptions
);
351 LoaderBlock
->ArcDiskInformation
= (PARC_DISK_INFORMATION
)MmAllocateMemory(sizeof(ARC_DISK_INFORMATION
));
352 InitializeListHead(&LoaderBlock
->ArcDiskInformation
->DiskSignatureListHead
);
353 List_PaToVa(&LoaderBlock
->ArcDiskInformation
->DiskSignatureListHead
);
354 LoaderBlock
->ArcDiskInformation
= PaToVa(LoaderBlock
->ArcDiskInformation
);
356 /* Alloc space for NLS (it will be converted to VA in WinLdrLoadNLS) */
357 LoaderBlock
->NlsData
= MmAllocateMemory(sizeof(NLS_DATA_BLOCK
));
358 if (LoaderBlock
->NlsData
== NULL
)
360 UiMessageBox("Failed to allocate memory for NLS table data!");
363 RtlZeroMemory(LoaderBlock
->NlsData
, sizeof(NLS_DATA_BLOCK
));
365 /* Create configuration entries */
366 InitializeHWConfig(LoaderBlock
);
368 /* Convert all DTE into virtual addresses */
371 /* Convert all list's to Virtual address */
372 List_PaToVa(&LoaderBlock
->LoadOrderListHead
);
374 /* this one will be converted right before switching to
375 virtual paging mode */
376 //List_PaToVa(&LoaderBlock->MemoryDescriptorListHead);
378 List_PaToVa(&LoaderBlock
->BootDriverListHead
);
380 /* Initialize Extension now */
381 Extension
= MmAllocateMemory(sizeof(LOADER_PARAMETER_EXTENSION
));
382 if (Extension
== NULL
)
384 UiMessageBox("Failed to allocate LPB Extension!");
387 RtlZeroMemory(Extension
, sizeof(LOADER_PARAMETER_EXTENSION
));
389 Extension
->Size
= sizeof(LOADER_PARAMETER_EXTENSION
);
390 Extension
->MajorVersion
= 4;
391 Extension
->MinorVersion
= 0;
394 LoaderBlock
->Extension
= PaToVa(Extension
);
397 // Last step before going virtual
398 void WinLdrSetupForNt(PLOADER_PARAMETER_BLOCK LoaderBlock
,
407 ULONG BlockSize
, NumPages
;
409 LoaderBlock
->u
.I386
.CommonDataArea
= NULL
;//CommonDataArea;
410 //LoaderBlock->u.I386.MachineType = MachineType; //FIXME: MachineType?
412 /* Allocate 2 pages for PCR */
413 Pcr
= (ULONG_PTR
)MmAllocateMemory(2 * MM_PAGE_SIZE
);
414 *PcrBasePage
= Pcr
>> MM_PAGE_SHIFT
;
418 UiMessageBox("Can't allocate PCR\n");
423 TssSize
= (sizeof(KTSS
) + MM_PAGE_SIZE
) & ~(MM_PAGE_SIZE
- 1);
424 TssPages
= TssSize
/ MM_PAGE_SIZE
;
426 Tss
= (ULONG_PTR
)MmAllocateMemory(TssSize
);
428 *TssBasePage
= Tss
>> MM_PAGE_SHIFT
;
430 /* Allocate space for new GDT + IDT */
431 BlockSize
= NUM_GDT
*sizeof(KGDTENTRY
) + NUM_IDT
*sizeof(KIDTENTRY
);//FIXME: Use GDT/IDT limits here?
432 NumPages
= (BlockSize
+ MM_PAGE_SIZE
- 1) >> MM_PAGE_SHIFT
;
433 *GdtIdt
= (PKGDTENTRY
)MmAllocateMemory(NumPages
* MM_PAGE_SIZE
);
437 UiMessageBox("Can't allocate pages for GDT+IDT!\n");
441 /* Zero newly prepared GDT+IDT */
442 RtlZeroMemory(*GdtIdt
, NumPages
<< MM_PAGE_SHIFT
);
447 LoadAndBootWindows(PCSTR OperatingSystemName
, WORD OperatingSystemVersion
)
450 CHAR SystemPath
[1024], SearchPath
[1024];
453 PVOID NtosBase
= NULL
, HalBase
= NULL
;
457 PLOADER_PARAMETER_BLOCK LoaderBlock
=NULL
, LoaderBlockVA
;
458 PLDR_DATA_TABLE_ENTRY KernelDTE
, HalDTE
;
459 KERNEL_ENTRY_POINT KiSystemStartup
;
465 //sprintf(MsgBuffer,"Booting Microsoft(R) Windows(R) OS version '%04x' is not implemented yet", OperatingSystemVersion);
466 //UiMessageBox(MsgBuffer);
469 // Open the operating system section
470 // specified in the .ini file
472 if (!IniOpenSection(OperatingSystemName
, &SectionId
))
474 sprintf(MsgBuffer
,"Operating System section '%s' not found in freeldr.ini", OperatingSystemName
);
475 UiMessageBox(MsgBuffer
);
480 * Make sure the system path is set in the .ini file
482 if (!IniReadSettingByName(SectionId
, "SystemPath", SystemPath
, sizeof(SystemPath
)))
484 UiMessageBox("System path not specified for selected operating system.");
488 if (!MachDiskNormalizeSystemPath(SystemPath
,
491 UiMessageBox("Invalid system path");
495 UiDrawStatusText("Loading...");
498 * Try to open system drive
500 BootDevice
= 0xffffffff;
501 if (!FsOpenSystemVolume(SystemPath
, BootPath
, &BootDevice
))
503 UiMessageBox("Failed to open boot drive.");
507 /* append a backslash */
508 if ((strlen(BootPath
)==0) ||
509 BootPath
[strlen(BootPath
)] != '\\')
510 strcat(BootPath
, "\\");
512 DbgPrint((DPRINT_WINDOWS
,"SystemRoot: '%s'\n", BootPath
));
514 /* Allocate and minimalistic-initialize LPB */
515 AllocateAndInitLPB(&LoaderBlock
);
518 strcpy(FileName
, BootPath
);
519 strcat(FileName
, "SYSTEM32\\NTOSKRNL.EXE");
520 Status
= WinLdrLoadImage(FileName
, &NtosBase
);
521 DbgPrint((DPRINT_WINDOWS
, "Ntos loaded with status %d\n", Status
));
524 strcpy(FileName
, BootPath
);
525 strcat(FileName
, "SYSTEM32\\HAL.DLL");
526 Status
= WinLdrLoadImage(FileName
, &HalBase
);
527 DbgPrint((DPRINT_WINDOWS
, "HAL loaded with status %d\n", Status
));
529 WinLdrAllocateDataTableEntry(LoaderBlock
, "ntoskrnl.exe",
530 "WINNT\\SYSTEM32\\NTOSKRNL.EXE", NtosBase
, &KernelDTE
);
531 WinLdrAllocateDataTableEntry(LoaderBlock
, "hal.dll",
532 "WINNT\\SYSTEM32\\HAL.EXE", HalBase
, &HalDTE
);
534 /* Load all referenced DLLs for kernel and HAL */
535 strcpy(SearchPath
, BootPath
);
536 strcat(SearchPath
, "SYSTEM32\\");
537 WinLdrScanImportDescriptorTable(LoaderBlock
, SearchPath
, KernelDTE
);
538 WinLdrScanImportDescriptorTable(LoaderBlock
, SearchPath
, HalDTE
);
540 /* Initialize Phase 1 - before NLS */
541 WinLdrInitializePhase1(LoaderBlock
);
543 /* Load SYSTEM hive and its LOG file */
544 strcpy(SearchPath
, BootPath
);
545 strcat(SearchPath
, "SYSTEM32\\CONFIG\\");
546 Status
= WinLdrLoadSystemHive(LoaderBlock
, SearchPath
, "SYSTEM");
547 DbgPrint((DPRINT_WINDOWS
, "SYSTEM hive loaded with status %d\n", Status
));
550 strcpy(SearchPath
, BootPath
);
551 strcat(SearchPath
, "SYSTEM32\\");
552 Status
= WinLdrLoadNLSData(LoaderBlock
, SearchPath
,
553 "c_1252.nls", "c_437.nls", "l_intl.nls");
554 DbgPrint((DPRINT_WINDOWS
, "NLS data loaded with status %d\n", Status
));
556 /* Load OEM HAL font */
558 /* Load boot drivers */
560 /* Alloc PCR, TSS, do magic things with the GDT/IDT */
561 WinLdrSetupForNt(LoaderBlock
, &GdtIdt
, &PcrBasePage
, &TssBasePage
);
563 /* Save entry-point pointer (VA) */
564 KiSystemStartup
= (KERNEL_ENTRY_POINT
)KernelDTE
->EntryPoint
;
565 LoaderBlockVA
= PaToVa(LoaderBlock
);
568 //DumpMemoryAllocMap();
570 /* Turn on paging mode of CPU*/
571 WinLdrTurnOnPaging(LoaderBlock
, PcrBasePage
, TssBasePage
, GdtIdt
);
573 DbgPrint((DPRINT_WINDOWS
, "Hello from paged mode, KiSystemStartup %p, LoaderBlockVA %p!\n",
574 KiSystemStartup
, LoaderBlockVA
));
576 WinLdrpDumpMemoryDescriptors(LoaderBlockVA
);
586 ULONG *trrr = (ULONG *)(512*1024*1024);
594 (KiSystemStartup
)(LoaderBlockVA
);
600 WinLdrpDumpMemoryDescriptors(PLOADER_PARAMETER_BLOCK LoaderBlock
)
603 PMEMORY_ALLOCATION_DESCRIPTOR MemoryDescriptor
;
605 NextMd
= LoaderBlock
->MemoryDescriptorListHead
.Flink
;
607 while (NextMd
!= &LoaderBlock
->MemoryDescriptorListHead
)
609 MemoryDescriptor
= CONTAINING_RECORD(NextMd
, MEMORY_ALLOCATION_DESCRIPTOR
, ListEntry
);
612 DbgPrint((DPRINT_WINDOWS
, "BP %08X PC %04X MT %d\n", MemoryDescriptor
->BasePage
,
613 MemoryDescriptor
->PageCount
, MemoryDescriptor
->MemoryType
));
615 NextMd
= MemoryDescriptor
->ListEntry
.Flink
;