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 along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include <ndk/ldrtypes.h>
29 DBG_DEFAULT_CHANNEL(WINDOWS
);
31 // FIXME: Find a better way to retrieve ARC disk information
32 extern ULONG reactos_disk_count
;
33 extern ARC_DISK_SIGNATURE_EX reactos_arc_disk_info
[];
35 extern ULONG LoaderPagesSpanned
;
36 extern BOOLEAN AcpiPresent
;
38 extern HEADLESS_LOADER_BLOCK LoaderRedirectionInformation
;
39 extern BOOLEAN WinLdrTerminalConnected
;
40 extern void WinLdrSetupEms(IN PCHAR BootOptions
);
42 PLOADER_SYSTEM_BLOCK WinLdrSystemBlock
;
45 VOID
DumpMemoryAllocMap(VOID
);
49 AllocateAndInitLPB(PLOADER_PARAMETER_BLOCK
*OutLoaderBlock
)
51 PLOADER_PARAMETER_BLOCK LoaderBlock
;
53 /* Allocate and zero-init the LPB */
54 WinLdrSystemBlock
= MmAllocateMemoryWithType(sizeof(LOADER_SYSTEM_BLOCK
),
56 if (WinLdrSystemBlock
== NULL
)
58 UiMessageBox("Failed to allocate memory for system block!");
62 RtlZeroMemory(WinLdrSystemBlock
, sizeof(LOADER_SYSTEM_BLOCK
));
64 LoaderBlock
= &WinLdrSystemBlock
->LoaderBlock
;
65 LoaderBlock
->NlsData
= &WinLdrSystemBlock
->NlsDataBlock
;
67 /* Init three critical lists, used right away */
68 InitializeListHead(&LoaderBlock
->LoadOrderListHead
);
69 InitializeListHead(&LoaderBlock
->MemoryDescriptorListHead
);
70 InitializeListHead(&LoaderBlock
->BootDriverListHead
);
72 *OutLoaderBlock
= LoaderBlock
;
77 WinLdrInitializePhase1(PLOADER_PARAMETER_BLOCK LoaderBlock
,
83 /* Examples of correct options and paths */
84 //CHAR Options[] = "/DEBUGPORT=COM1 /BAUDRATE=115200";
85 //CHAR Options[] = "/NODEBUG";
86 //CHAR SystemRoot[] = "\\WINNT\\";
87 //CHAR ArcBoot[] = "multi(0)disk(0)rdisk(0)partition(1)";
89 LPSTR LoadOptions
, NewLoadOptions
;
90 CHAR HalPath
[] = "\\";
91 CHAR ArcBoot
[MAX_PATH
+1];
92 CHAR MiscFiles
[MAX_PATH
+1];
94 ULONG_PTR PathSeparator
;
95 PLOADER_PARAMETER_EXTENSION Extension
;
97 /* Construct SystemRoot and ArcBoot from SystemPath */
98 PathSeparator
= strstr(BootPath
, "\\") - BootPath
;
99 RtlStringCbCopyNA(ArcBoot
, sizeof(ArcBoot
), BootPath
, PathSeparator
);
101 TRACE("ArcBoot: '%s'\n", ArcBoot
);
102 TRACE("SystemRoot: '%s'\n", SystemRoot
);
103 TRACE("Options: '%s'\n", Options
);
105 /* Fill ARC BootDevice */
106 LoaderBlock
->ArcBootDeviceName
= WinLdrSystemBlock
->ArcBootDeviceName
;
107 RtlStringCbCopyA(LoaderBlock
->ArcBootDeviceName
, sizeof(WinLdrSystemBlock
->ArcBootDeviceName
), ArcBoot
);
108 LoaderBlock
->ArcBootDeviceName
= PaToVa(LoaderBlock
->ArcBootDeviceName
);
110 /* Fill ARC HalDevice, it matches ArcBoot path */
111 LoaderBlock
->ArcHalDeviceName
= WinLdrSystemBlock
->ArcBootDeviceName
;
112 LoaderBlock
->ArcHalDeviceName
= PaToVa(LoaderBlock
->ArcHalDeviceName
);
114 /* Fill SystemRoot */
115 LoaderBlock
->NtBootPathName
= WinLdrSystemBlock
->NtBootPathName
;
116 RtlStringCbCopyA(LoaderBlock
->NtBootPathName
, sizeof(WinLdrSystemBlock
->NtBootPathName
), SystemRoot
);
117 LoaderBlock
->NtBootPathName
= PaToVa(LoaderBlock
->NtBootPathName
);
119 /* Fill NtHalPathName */
120 LoaderBlock
->NtHalPathName
= WinLdrSystemBlock
->NtHalPathName
;
121 RtlStringCbCopyA(LoaderBlock
->NtHalPathName
, sizeof(WinLdrSystemBlock
->NtHalPathName
), HalPath
);
122 LoaderBlock
->NtHalPathName
= PaToVa(LoaderBlock
->NtHalPathName
);
124 /* Fill LoadOptions and strip the '/' switch symbol in front of each option */
125 NewLoadOptions
= LoadOptions
= LoaderBlock
->LoadOptions
= WinLdrSystemBlock
->LoadOptions
;
126 RtlStringCbCopyA(LoaderBlock
->LoadOptions
, sizeof(WinLdrSystemBlock
->LoadOptions
), Options
);
130 while (*LoadOptions
== '/')
133 *NewLoadOptions
++ = *LoadOptions
;
134 } while (*LoadOptions
++);
136 LoaderBlock
->LoadOptions
= PaToVa(LoaderBlock
->LoadOptions
);
139 LoaderBlock
->ArcDiskInformation
= &WinLdrSystemBlock
->ArcDiskInformation
;
140 InitializeListHead(&LoaderBlock
->ArcDiskInformation
->DiskSignatureListHead
);
142 /* Convert ARC disk information from freeldr to a correct format */
143 for (i
= 0; i
< reactos_disk_count
; i
++)
145 PARC_DISK_SIGNATURE_EX ArcDiskSig
;
147 /* Allocate the ARC structure */
148 ArcDiskSig
= FrLdrHeapAlloc(sizeof(ARC_DISK_SIGNATURE_EX
), 'giSD');
150 /* Copy the data over */
151 RtlCopyMemory(ArcDiskSig
, &reactos_arc_disk_info
[i
], sizeof(ARC_DISK_SIGNATURE_EX
));
153 /* Set the ARC Name pointer */
154 ArcDiskSig
->DiskSignature
.ArcName
= PaToVa(ArcDiskSig
->ArcName
);
156 /* Insert into the list */
157 InsertTailList(&LoaderBlock
->ArcDiskInformation
->DiskSignatureListHead
,
158 &ArcDiskSig
->DiskSignature
.ListEntry
);
161 /* Convert all list's to Virtual address */
163 /* Convert the ArcDisks list to virtual address */
164 List_PaToVa(&LoaderBlock
->ArcDiskInformation
->DiskSignatureListHead
);
165 LoaderBlock
->ArcDiskInformation
= PaToVa(LoaderBlock
->ArcDiskInformation
);
167 /* Convert configuration entries to VA */
168 ConvertConfigToVA(LoaderBlock
->ConfigurationRoot
);
169 LoaderBlock
->ConfigurationRoot
= PaToVa(LoaderBlock
->ConfigurationRoot
);
171 /* Convert all DTE into virtual addresses */
172 List_PaToVa(&LoaderBlock
->LoadOrderListHead
);
174 /* This one will be converted right before switching to virtual paging mode */
175 //List_PaToVa(&LoaderBlock->MemoryDescriptorListHead);
177 /* Convert list of boot drivers */
178 List_PaToVa(&LoaderBlock
->BootDriverListHead
);
180 /* Initialize Extension now */
181 Extension
= &WinLdrSystemBlock
->Extension
;
182 Extension
->Size
= sizeof(LOADER_PARAMETER_EXTENSION
);
183 Extension
->MajorVersion
= (VersionToBoot
& 0xFF00) >> 8;
184 Extension
->MinorVersion
= VersionToBoot
& 0xFF;
185 Extension
->Profile
.Status
= 2;
187 /* Check if FreeLdr detected a ACPI table */
190 /* Set the pointer to something for compatibility */
191 Extension
->AcpiTable
= (PVOID
)1;
192 // FIXME: Extension->AcpiTableSize;
196 /* Set headless block pointer */
197 if (WinLdrTerminalConnected
)
199 Extension
->HeadlessLoaderBlock
= &WinLdrSystemBlock
->HeadlessLoaderBlock
;
200 RtlCopyMemory(Extension
->HeadlessLoaderBlock
,
201 &LoaderRedirectionInformation
,
202 sizeof(HEADLESS_LOADER_BLOCK
));
203 Extension
->HeadlessLoaderBlock
= PaToVa(Extension
->HeadlessLoaderBlock
);
206 /* Load drivers database */
207 RtlStringCbCopyA(MiscFiles
, sizeof(MiscFiles
), BootPath
);
208 RtlStringCbCatA(MiscFiles
, sizeof(MiscFiles
), "AppPatch\\drvmain.sdb");
209 Extension
->DrvDBImage
= PaToVa(WinLdrLoadModule(MiscFiles
,
210 &Extension
->DrvDBSize
,
211 LoaderRegistryData
));
213 /* Convert extension and setup block pointers */
214 LoaderBlock
->Extension
= PaToVa(Extension
);
216 if (LoaderBlock
->SetupLdrBlock
)
217 LoaderBlock
->SetupLdrBlock
= PaToVa(LoaderBlock
->SetupLdrBlock
);
219 TRACE("WinLdrInitializePhase1() completed\n");
223 WinLdrLoadDeviceDriver(PLIST_ENTRY LoadOrderListHead
,
225 PUNICODE_STRING FilePath
,
227 PLDR_DATA_TABLE_ENTRY
*DriverDTE
)
230 CHAR DriverPath
[1024];
234 PVOID DriverBase
= NULL
;
236 // Separate the path to file name and directory path
237 RtlStringCbPrintfA(DriverPath
, sizeof(DriverPath
), "%wZ", FilePath
);
238 DriverNamePos
= strrchr(DriverPath
, '\\');
239 if (DriverNamePos
!= NULL
)
242 RtlStringCbCopyA(DllName
, sizeof(DllName
), DriverNamePos
+1);
244 // Cut out the name from the path
245 *(DriverNamePos
+1) = ANSI_NULL
;
249 // There is no directory in the path
250 RtlStringCbCopyA(DllName
, sizeof(DllName
), DriverPath
);
251 *DriverPath
= ANSI_NULL
;
254 TRACE("DriverPath: '%s', DllName: '%s', LPB\n", DriverPath
, DllName
);
256 // Check if driver is already loaded
257 Success
= WinLdrCheckForLoadedDll(LoadOrderListHead
, DllName
, DriverDTE
);
260 // We've got the pointer to its DTE, just return success
264 // It's not loaded, we have to load it
265 RtlStringCbPrintfA(FullPath
, sizeof(FullPath
), "%s%wZ", BootPath
, FilePath
);
266 Success
= WinLdrLoadImage(FullPath
, LoaderBootDriver
, &DriverBase
);
270 // Allocate a DTE for it
271 Success
= WinLdrAllocateDataTableEntry(LoadOrderListHead
, DllName
, DllName
, DriverBase
, DriverDTE
);
274 ERR("WinLdrAllocateDataTableEntry() failed\n");
278 // Modify any flags, if needed
279 (*DriverDTE
)->Flags
|= Flags
;
281 // Look for any dependencies it may have, and load them too
282 RtlStringCbPrintfA(FullPath
, sizeof(FullPath
), "%s%s", BootPath
, DriverPath
);
283 Success
= WinLdrScanImportDescriptorTable(LoadOrderListHead
, FullPath
, *DriverDTE
);
286 ERR("WinLdrScanImportDescriptorTable() failed for %s\n", FullPath
);
294 WinLdrLoadBootDrivers(PLOADER_PARAMETER_BLOCK LoaderBlock
,
298 PBOOT_DRIVER_LIST_ENTRY BootDriver
;
302 // Walk through the boot drivers list
303 NextBd
= LoaderBlock
->BootDriverListHead
.Flink
;
305 while (NextBd
!= &LoaderBlock
->BootDriverListHead
)
307 BootDriver
= CONTAINING_RECORD(NextBd
, BOOT_DRIVER_LIST_ENTRY
, Link
);
309 TRACE("BootDriver %wZ DTE %08X RegPath: %wZ\n", &BootDriver
->FilePath
,
310 BootDriver
->LdrEntry
, &BootDriver
->RegistryPath
);
312 // Paths are relative (FIXME: Are they always relative?)
315 Success
= WinLdrLoadDeviceDriver(&LoaderBlock
->LoadOrderListHead
,
317 &BootDriver
->FilePath
,
319 &BootDriver
->LdrEntry
);
323 // Convert the RegistryPath and DTE addresses to VA since we are not going to use it anymore
324 BootDriver
->RegistryPath
.Buffer
= PaToVa(BootDriver
->RegistryPath
.Buffer
);
325 BootDriver
->FilePath
.Buffer
= PaToVa(BootDriver
->FilePath
.Buffer
);
326 BootDriver
->LdrEntry
= PaToVa(BootDriver
->LdrEntry
);
330 // Loading failed - cry loudly
331 ERR("Can't load boot driver '%wZ'!\n", &BootDriver
->FilePath
);
332 UiMessageBox("Can't load boot driver '%wZ'!", &BootDriver
->FilePath
);
335 // Remove it from the list and try to continue
336 RemoveEntryList(NextBd
);
339 NextBd
= BootDriver
->Link
.Flink
;
346 WinLdrLoadModule(PCSTR ModuleName
,
348 TYPE_OF_MEMORY MemoryType
)
352 FILEINFORMATION FileInfo
;
357 //CHAR ProgressString[256];
359 /* Inform user we are loading files */
361 //RtlStringCbPrintfA(ProgressString, sizeof(ProgressString), "Loading %s...", FileName);
362 //UiDrawProgressBarCenter(1, 100, ProgressString);
364 TRACE("Loading module %s\n", ModuleName
);
367 /* Open the image file */
368 Status
= ArcOpen((PCHAR
)ModuleName
, OpenReadOnly
, &FileId
);
369 if (Status
!= ESUCCESS
)
371 /* In case of errors, we just return, without complaining to the user */
372 WARN("Error while opening '%s', Status: %u\n", ModuleName
, Status
);
376 /* Get this file's size */
377 Status
= ArcGetFileInformation(FileId
, &FileInfo
);
378 if (Status
!= ESUCCESS
)
383 FileSize
= FileInfo
.EndingAddress
.LowPart
;
386 /* Allocate memory */
387 PhysicalBase
= MmAllocateMemoryWithType(FileSize
, MemoryType
);
388 if (PhysicalBase
== NULL
)
394 /* Load whole file */
395 Status
= ArcRead(FileId
, PhysicalBase
, FileSize
, &BytesRead
);
397 if (Status
!= ESUCCESS
)
399 WARN("Error while reading '%s', Status: %u\n", ModuleName
, Status
);
403 TRACE("Loaded %s at 0x%x with size 0x%x\n", ModuleName
, PhysicalBase
, FileSize
);
409 WinLdrDetectVersion(VOID
)
416 L
"\\Registry\\Machine\\SYSTEM\\CurrentControlSet\\Control\\Terminal Server",
418 if (rc
!= ERROR_SUCCESS
)
420 // Key doesn't exist; assume NT 4.0
421 return _WIN32_WINNT_NT4
;
424 // We may here want to read the value of ProductVersion
425 return _WIN32_WINNT_WS03
;
431 IN OUT PLOADER_PARAMETER_BLOCK LoaderBlock
,
434 IN PCCH ImportName
, // BaseDllName
435 IN TYPE_OF_MEMORY MemoryType
,
436 OUT PLDR_DATA_TABLE_ENTRY
*Dte
,
440 CHAR FullFileName
[MAX_PATH
];
441 CHAR ProgressString
[256];
442 PVOID BaseAddress
= NULL
;
445 RtlStringCbPrintfA(ProgressString
, sizeof(ProgressString
), "Loading %s...", File
);
446 UiDrawProgressBarCenter(Percentage
, 100, ProgressString
);
448 RtlStringCbCopyA(FullFileName
, sizeof(FullFileName
), Path
);
449 RtlStringCbCatA(FullFileName
, sizeof(FullFileName
), File
);
451 Success
= WinLdrLoadImage(FullFileName
, MemoryType
, &BaseAddress
);
454 TRACE("Loading %s failed\n", File
);
457 TRACE("%s loaded successfully at %p\n", File
, BaseAddress
);
460 * Cheat about the base DLL name if we are loading
461 * the Kernel Debugger Transport DLL, to make the
464 Success
= WinLdrAllocateDataTableEntry(&LoaderBlock
->LoadOrderListHead
,
475 LoadWindowsCore(IN USHORT OperatingSystemVersion
,
476 IN OUT PLOADER_PARAMETER_BLOCK LoaderBlock
,
477 IN PCSTR BootOptions
,
479 IN OUT PLDR_DATA_TABLE_ENTRY
* KernelDTE
)
483 CHAR DirPath
[MAX_PATH
];
484 CHAR KernelFileName
[MAX_PATH
];
485 CHAR HalFileName
[MAX_PATH
];
486 CHAR KdTransportDllName
[MAX_PATH
];
487 PLDR_DATA_TABLE_ENTRY HalDTE
, KdComDTE
= NULL
;
489 if (!KernelDTE
) return FALSE
;
491 /* Initialize SystemRoot\System32 path */
492 RtlStringCbCopyA(DirPath
, sizeof(DirPath
), BootPath
);
493 RtlStringCbCatA(DirPath
, sizeof(DirPath
), "system32\\");
496 // TODO: Parse also the separate INI values "Kernel=" and "Hal="
499 /* Default KERNEL and HAL file names */
500 RtlStringCbCopyA(KernelFileName
, sizeof(KernelFileName
), "ntoskrnl.exe");
501 RtlStringCbCopyA(HalFileName
, sizeof(HalFileName
) , "hal.dll");
503 /* Find any /KERNEL= or /HAL= switch in the boot options */
504 Options
= BootOptions
;
507 /* Skip possible initial whitespace */
508 Options
+= strspn(Options
, " \t");
510 /* Check whether a new option starts and it is either KERNEL or HAL */
511 if (*Options
!= '/' || (++Options
,
512 !(_strnicmp(Options
, "KERNEL=", 7) == 0 ||
513 _strnicmp(Options
, "HAL=", 4) == 0)) )
515 /* Search for another whitespace */
516 Options
= strpbrk(Options
, " \t");
521 size_t i
= strcspn(Options
, " \t"); /* Skip whitespace */
524 /* Use the default values */
528 /* We have found either KERNEL or HAL options */
529 if (_strnicmp(Options
, "KERNEL=", 7) == 0)
531 Options
+= 7; i
-= 7;
532 RtlStringCbCopyNA(KernelFileName
, sizeof(KernelFileName
), Options
, i
);
533 _strupr(KernelFileName
);
535 else if (_strnicmp(Options
, "HAL=", 4) == 0)
537 Options
+= 4; i
-= 4;
538 RtlStringCbCopyNA(HalFileName
, sizeof(HalFileName
), Options
, i
);
539 _strupr(HalFileName
);
544 TRACE("Kernel file = '%s' ; HAL file = '%s'\n", KernelFileName
, HalFileName
);
546 /* Load the Kernel */
547 LoadModule(LoaderBlock
, DirPath
, KernelFileName
, "ntoskrnl.exe", LoaderSystemCode
, KernelDTE
, 30);
550 LoadModule(LoaderBlock
, DirPath
, HalFileName
, "hal.dll", LoaderHalCode
, &HalDTE
, 45);
552 /* Load the Kernel Debugger Transport DLL */
553 if (OperatingSystemVersion
> _WIN32_WINNT_WIN2K
)
556 * According to http://www.nynaeve.net/?p=173 :
557 * "[...] Another enhancement that could be done Microsoft-side would be
558 * a better interface for replacing KD transport modules. Right now, due
559 * to the fact that ntoskrnl is static linked to KDCOM.DLL, the OS loader
560 * has a hardcoded hack that interprets the KD type in the OS loader options,
561 * loads one of the (hardcoded filenames) "kdcom.dll", "kd1394.dll", or
562 * "kdusb2.dll" modules, and inserts them into the loaded module list under
563 * the name "kdcom.dll". [...]"
567 * This loop replaces a dumb call to strstr(..., "DEBUGPORT=").
568 * Indeed I want it to be case-insensitive to allow "debugport="
569 * or "DeBuGpOrT=" or... , and I don't want it to match malformed
570 * command-line options, such as:
572 * "...foo DEBUGPORT=xxx bar..."
573 * "...foo/DEBUGPORT=xxx bar..."
574 * "...foo/DEBUGPORT=bar..."
576 * i.e. the "DEBUGPORT=" switch must start with a slash and be separated
577 * from the rest by whitespace, unless it begins the command-line, e.g.:
579 * "/DEBUGPORT=COM1 foo...bar..."
580 * "...foo /DEBUGPORT=USB bar..."
582 * "...foo /DEBUGPORT= bar..."
583 * (in that case, we default the port to COM).
585 Options
= BootOptions
;
588 /* Skip possible initial whitespace */
589 Options
+= strspn(Options
, " \t");
591 /* Check whether a new option starts and it is the DEBUGPORT one */
592 if (*Options
!= '/' || _strnicmp(++Options
, "DEBUGPORT=", 10) != 0)
594 /* Search for another whitespace */
595 Options
= strpbrk(Options
, " \t");
600 /* We found the DEBUGPORT option. Move to the port name. */
609 * We have found the DEBUGPORT option. Parse the port name.
610 * Format: /DEBUGPORT=COM1 or /DEBUGPORT=FILE:\Device\HarddiskX\PartitionY\debug.log or /DEBUGPORT=FOO
611 * If we only have /DEBUGPORT= (i.e. without any port name), defaults it to "COM".
613 RtlStringCbCopyA(KdTransportDllName
, sizeof(KdTransportDllName
), "KD");
614 if (_strnicmp(Options
, "COM", 3) == 0 && '0' <= Options
[3] && Options
[3] <= '9')
616 RtlStringCbCatNA(KdTransportDllName
, sizeof(KdTransportDllName
), Options
, 3);
620 size_t i
= strcspn(Options
, " \t:"); /* Skip valid separators: whitespace or colon */
622 RtlStringCbCatA(KdTransportDllName
, sizeof(KdTransportDllName
), "COM");
624 RtlStringCbCatNA(KdTransportDllName
, sizeof(KdTransportDllName
), Options
, i
);
626 RtlStringCbCatA(KdTransportDllName
, sizeof(KdTransportDllName
), ".DLL");
627 _strupr(KdTransportDllName
);
630 * Load the transport DLL. Override the base DLL name of the
631 * loaded transport DLL to the default "KDCOM.DLL" name.
633 LoadModule(LoaderBlock
, DirPath
, KdTransportDllName
, "kdcom.dll", LoaderSystemCode
, &KdComDTE
, 60);
637 /* Load all referenced DLLs for Kernel, HAL and Kernel Debugger Transport DLL */
638 Success
= WinLdrScanImportDescriptorTable(&LoaderBlock
->LoadOrderListHead
, DirPath
, *KernelDTE
);
639 Success
&= WinLdrScanImportDescriptorTable(&LoaderBlock
->LoadOrderListHead
, DirPath
, HalDTE
);
642 Success
&= WinLdrScanImportDescriptorTable(&LoaderBlock
->LoadOrderListHead
, DirPath
, KdComDTE
);
657 USHORT OperatingSystemVersion
;
658 PLOADER_PARAMETER_BLOCK LoaderBlock
;
659 CHAR BootPath
[MAX_PATH
];
660 CHAR FileName
[MAX_PATH
];
661 CHAR BootOptions
[256];
663 ArgValue
= GetArgumentValue(Argc
, Argv
, "BootType");
666 ERR("No 'BootType' value, aborting!\n");
670 if (_stricmp(ArgValue
, "Windows") == 0 ||
671 _stricmp(ArgValue
, "Windows2003") == 0)
673 OperatingSystemVersion
= _WIN32_WINNT_WS03
;
675 else if (_stricmp(ArgValue
, "WindowsNT40") == 0)
677 OperatingSystemVersion
= _WIN32_WINNT_NT4
;
681 ERR("Unknown 'BootType' value '%s', aborting!\n", ArgValue
);
686 UiDrawProgressBarCenter(1, 100, "Loading NT...");
688 /* Retrieve the system path */
689 *BootPath
= ANSI_NULL
;
690 ArgValue
= GetArgumentValue(Argc
, Argv
, "SystemPath");
692 RtlStringCbCopyA(BootPath
, sizeof(BootPath
), ArgValue
);
695 * Check whether BootPath is a full path
696 * and if not, create a full boot path.
698 * See FsOpenFile for the technique used.
700 if (strrchr(BootPath
, ')') == NULL
)
702 /* Temporarily save the boot path */
703 RtlStringCbCopyA(FileName
, sizeof(FileName
), BootPath
);
705 /* This is not a full path. Use the current (i.e. boot) device. */
706 MachDiskGetBootPath(BootPath
, sizeof(BootPath
));
708 /* Append a path separator if needed */
709 if (*FileName
!= '\\' && *FileName
!= '/')
710 RtlStringCbCatA(BootPath
, sizeof(BootPath
), "\\");
712 /* Append the remaining path */
713 RtlStringCbCatA(BootPath
, sizeof(BootPath
), FileName
);
716 /* Append a backslash if needed */
717 if (!*BootPath
|| BootPath
[strlen(BootPath
) - 1] != '\\')
718 RtlStringCbCatA(BootPath
, sizeof(BootPath
), "\\");
720 TRACE("BootPath: '%s'\n", BootPath
);
722 /* Retrieve the boot options */
723 *BootOptions
= ANSI_NULL
;
724 ArgValue
= GetArgumentValue(Argc
, Argv
, "Options");
726 RtlStringCbCopyA(BootOptions
, sizeof(BootOptions
), ArgValue
);
728 /* Append boot-time options */
729 AppendBootTimeOptions(BootOptions
);
731 TRACE("BootOptions: '%s'\n", BootOptions
);
733 /* Check if a ramdisk file was given */
734 File
= strstr(BootOptions
, "/RDPATH=");
737 /* Copy the file name and everything else after it */
738 RtlStringCbCopyA(FileName
, sizeof(FileName
), File
+ 8);
741 *strstr(FileName
, " ") = ANSI_NULL
;
743 /* Load the ramdisk */
744 if (!RamDiskLoadVirtualFile(FileName
))
746 UiMessageBox("Failed to load RAM disk file %s", FileName
);
751 /* Let user know we started loading */
752 //UiDrawStatusText("Loading...");
754 /* Allocate and minimalist-initialize LPB */
755 AllocateAndInitLPB(&LoaderBlock
);
757 /* Load the system hive */
759 UiDrawProgressBarCenter(15, 100, "Loading system hive...");
760 Success
= WinLdrInitSystemHive(LoaderBlock
, BootPath
, FALSE
);
761 TRACE("SYSTEM hive %s\n", (Success
? "loaded" : "not loaded"));
762 /* Bail out if failure */
766 /* Load NLS data, OEM font, and prepare boot drivers list */
767 Success
= WinLdrScanSystemHive(LoaderBlock
, BootPath
);
768 TRACE("SYSTEM hive %s\n", (Success
? "scanned" : "not scanned"));
769 /* Bail out if failure */
774 return LoadAndBootWindowsCommon(OperatingSystemVersion
,
782 LoadAndBootWindowsCommon(
783 USHORT OperatingSystemVersion
,
784 PLOADER_PARAMETER_BLOCK LoaderBlock
,
789 PLOADER_PARAMETER_BLOCK LoaderBlockVA
;
791 PLDR_DATA_TABLE_ENTRY KernelDTE
;
792 KERNEL_ENTRY_POINT KiSystemStartup
;
795 TRACE("LoadAndBootWindowsCommon()\n");
798 /* Setup redirection support */
799 WinLdrSetupEms((PCHAR
)BootOptions
);
802 /* Convert BootPath to SystemRoot */
803 SystemRoot
= strstr(BootPath
, "\\");
805 /* Detect hardware */
807 UiDrawProgressBarCenter(20, 100, "Detecting hardware...");
808 LoaderBlock
->ConfigurationRoot
= MachHwDetect();
810 if (OperatingSystemVersion
== 0)
811 OperatingSystemVersion
= WinLdrDetectVersion();
813 /* Load the operating system core: the Kernel, the HAL and the Kernel Debugger Transport DLL */
814 Success
= LoadWindowsCore(OperatingSystemVersion
,
821 UiMessageBox("Error loading NTOS core.");
825 /* Load boot drivers */
827 UiDrawProgressBarCenter(100, 100, "Loading boot drivers...");
828 Success
= WinLdrLoadBootDrivers(LoaderBlock
, BootPath
);
829 TRACE("Boot drivers loading %s\n", Success
? "successful" : "failed");
831 /* Cleanup ini file */
834 /* Initialize Phase 1 - no drivers loading anymore */
835 WinLdrInitializePhase1(LoaderBlock
,
839 OperatingSystemVersion
);
841 /* Save entry-point pointer and Loader block VAs */
842 KiSystemStartup
= (KERNEL_ENTRY_POINT
)KernelDTE
->EntryPoint
;
843 LoaderBlockVA
= PaToVa(LoaderBlock
);
845 /* "Stop all motors", change videomode */
846 MachPrepareForReactOS();
849 //DumpMemoryAllocMap();
851 /* Do the machine specific initialization */
852 WinLdrSetupMachineDependent(LoaderBlock
);
854 /* Map pages and create memory descriptors */
855 WinLdrSetupMemoryLayout(LoaderBlock
);
857 /* Set processor context */
858 WinLdrSetProcessorContext();
860 /* Save final value of LoaderPagesSpanned */
861 LoaderBlock
->Extension
->LoaderPagesSpanned
= LoaderPagesSpanned
;
863 TRACE("Hello from paged mode, KiSystemStartup %p, LoaderBlockVA %p!\n",
864 KiSystemStartup
, LoaderBlockVA
);
866 // Zero KI_USER_SHARED_DATA page
867 memset((PVOID
)KI_USER_SHARED_DATA
, 0, MM_PAGE_SIZE
);
869 WinLdrpDumpMemoryDescriptors(LoaderBlockVA
);
870 WinLdrpDumpBootDriver(LoaderBlockVA
);
872 WinLdrpDumpArcDisks(LoaderBlockVA
);
876 (*KiSystemStartup
)(LoaderBlockVA
);
881 WinLdrpDumpMemoryDescriptors(PLOADER_PARAMETER_BLOCK LoaderBlock
)
884 PMEMORY_ALLOCATION_DESCRIPTOR MemoryDescriptor
;
886 NextMd
= LoaderBlock
->MemoryDescriptorListHead
.Flink
;
888 while (NextMd
!= &LoaderBlock
->MemoryDescriptorListHead
)
890 MemoryDescriptor
= CONTAINING_RECORD(NextMd
, MEMORY_ALLOCATION_DESCRIPTOR
, ListEntry
);
892 TRACE("BP %08X PC %04X MT %d\n", MemoryDescriptor
->BasePage
,
893 MemoryDescriptor
->PageCount
, MemoryDescriptor
->MemoryType
);
895 NextMd
= MemoryDescriptor
->ListEntry
.Flink
;
900 WinLdrpDumpBootDriver(PLOADER_PARAMETER_BLOCK LoaderBlock
)
903 PBOOT_DRIVER_LIST_ENTRY BootDriver
;
905 NextBd
= LoaderBlock
->BootDriverListHead
.Flink
;
907 while (NextBd
!= &LoaderBlock
->BootDriverListHead
)
909 BootDriver
= CONTAINING_RECORD(NextBd
, BOOT_DRIVER_LIST_ENTRY
, Link
);
911 TRACE("BootDriver %wZ DTE %08X RegPath: %wZ\n", &BootDriver
->FilePath
,
912 BootDriver
->LdrEntry
, &BootDriver
->RegistryPath
);
914 NextBd
= BootDriver
->Link
.Flink
;
919 WinLdrpDumpArcDisks(PLOADER_PARAMETER_BLOCK LoaderBlock
)
922 PARC_DISK_SIGNATURE ArcDisk
;
924 NextBd
= LoaderBlock
->ArcDiskInformation
->DiskSignatureListHead
.Flink
;
926 while (NextBd
!= &LoaderBlock
->ArcDiskInformation
->DiskSignatureListHead
)
928 ArcDisk
= CONTAINING_RECORD(NextBd
, ARC_DISK_SIGNATURE
, ListEntry
);
930 TRACE("ArcDisk %s checksum: 0x%X, signature: 0x%X\n",
931 ArcDisk
->ArcName
, ArcDisk
->CheckSum
, ArcDisk
->Signature
);
933 NextBd
= ArcDisk
->ListEntry
.Flink
;