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 */
375 /* Get this file's size */
376 Status
= ArcGetFileInformation(FileId
, &FileInfo
);
377 if (Status
!= ESUCCESS
)
382 FileSize
= FileInfo
.EndingAddress
.LowPart
;
385 /* Allocate memory */
386 PhysicalBase
= MmAllocateMemoryWithType(FileSize
, MemoryType
);
387 if (PhysicalBase
== NULL
)
393 /* Load whole file */
394 Status
= ArcRead(FileId
, PhysicalBase
, FileSize
, &BytesRead
);
396 if (Status
!= ESUCCESS
)
401 TRACE("Loaded %s at 0x%x with size 0x%x\n", ModuleName
, PhysicalBase
, FileSize
);
407 WinLdrDetectVersion(VOID
)
414 L
"\\Registry\\Machine\\SYSTEM\\CurrentControlSet\\Control\\Terminal Server",
416 if (rc
!= ERROR_SUCCESS
)
418 // Key doesn't exist; assume NT 4.0
419 return _WIN32_WINNT_NT4
;
422 // We may here want to read the value of ProductVersion
423 return _WIN32_WINNT_WS03
;
429 IN OUT PLOADER_PARAMETER_BLOCK LoaderBlock
,
432 IN PCCH ImportName
, // BaseDllName
433 IN TYPE_OF_MEMORY MemoryType
,
434 OUT PLDR_DATA_TABLE_ENTRY
*Dte
,
438 CHAR FullFileName
[MAX_PATH
];
439 CHAR ProgressString
[256];
440 PVOID BaseAddress
= NULL
;
443 RtlStringCbPrintfA(ProgressString
, sizeof(ProgressString
), "Loading %s...", File
);
444 UiDrawProgressBarCenter(Percentage
, 100, ProgressString
);
446 RtlStringCbCopyA(FullFileName
, sizeof(FullFileName
), Path
);
447 RtlStringCbCatA(FullFileName
, sizeof(FullFileName
), File
);
449 Success
= WinLdrLoadImage(FullFileName
, MemoryType
, &BaseAddress
);
452 TRACE("Loading %s failed\n", File
);
455 TRACE("%s loaded successfully at %p\n", File
, BaseAddress
);
458 * Cheat about the base DLL name if we are loading
459 * the Kernel Debugger Transport DLL, to make the
462 Success
= WinLdrAllocateDataTableEntry(&LoaderBlock
->LoadOrderListHead
,
473 LoadWindowsCore(IN USHORT OperatingSystemVersion
,
474 IN OUT PLOADER_PARAMETER_BLOCK LoaderBlock
,
475 IN PCSTR BootOptions
,
477 IN OUT PLDR_DATA_TABLE_ENTRY
* KernelDTE
)
481 CHAR DirPath
[MAX_PATH
];
482 CHAR KernelFileName
[MAX_PATH
];
483 CHAR HalFileName
[MAX_PATH
];
484 CHAR KdTransportDllName
[MAX_PATH
];
485 PLDR_DATA_TABLE_ENTRY HalDTE
, KdComDTE
= NULL
;
487 if (!KernelDTE
) return FALSE
;
489 /* Initialize SystemRoot\System32 path */
490 RtlStringCbCopyA(DirPath
, sizeof(DirPath
), BootPath
);
491 RtlStringCbCatA(DirPath
, sizeof(DirPath
), "system32\\");
494 // TODO: Parse also the separate INI values "Kernel=" and "Hal="
497 /* Default KERNEL and HAL file names */
498 RtlStringCbCopyA(KernelFileName
, sizeof(KernelFileName
), "ntoskrnl.exe");
499 RtlStringCbCopyA(HalFileName
, sizeof(HalFileName
) , "hal.dll");
501 /* Find any /KERNEL= or /HAL= switch in the boot options */
502 Options
= BootOptions
;
505 /* Skip possible initial whitespace */
506 Options
+= strspn(Options
, " \t");
508 /* Check whether a new option starts and it is either KERNEL or HAL */
509 if (*Options
!= '/' || (++Options
,
510 !(_strnicmp(Options
, "KERNEL=", 7) == 0 ||
511 _strnicmp(Options
, "HAL=", 4) == 0)) )
513 /* Search for another whitespace */
514 Options
= strpbrk(Options
, " \t");
519 size_t i
= strcspn(Options
, " \t"); /* Skip whitespace */
522 /* Use the default values */
526 /* We have found either KERNEL or HAL options */
527 if (_strnicmp(Options
, "KERNEL=", 7) == 0)
529 Options
+= 7; i
-= 7;
530 RtlStringCbCopyNA(KernelFileName
, sizeof(KernelFileName
), Options
, i
);
531 _strupr(KernelFileName
);
533 else if (_strnicmp(Options
, "HAL=", 4) == 0)
535 Options
+= 4; i
-= 4;
536 RtlStringCbCopyNA(HalFileName
, sizeof(HalFileName
), Options
, i
);
537 _strupr(HalFileName
);
542 TRACE("Kernel file = '%s' ; HAL file = '%s'\n", KernelFileName
, HalFileName
);
544 /* Load the Kernel */
545 LoadModule(LoaderBlock
, DirPath
, KernelFileName
, "ntoskrnl.exe", LoaderSystemCode
, KernelDTE
, 30);
548 LoadModule(LoaderBlock
, DirPath
, HalFileName
, "hal.dll", LoaderHalCode
, &HalDTE
, 45);
550 /* Load the Kernel Debugger Transport DLL */
551 if (OperatingSystemVersion
> _WIN32_WINNT_WIN2K
)
554 * According to http://www.nynaeve.net/?p=173 :
555 * "[...] Another enhancement that could be done Microsoft-side would be
556 * a better interface for replacing KD transport modules. Right now, due
557 * to the fact that ntoskrnl is static linked to KDCOM.DLL, the OS loader
558 * has a hardcoded hack that interprets the KD type in the OS loader options,
559 * loads one of the (hardcoded filenames) "kdcom.dll", "kd1394.dll", or
560 * "kdusb2.dll" modules, and inserts them into the loaded module list under
561 * the name "kdcom.dll". [...]"
565 * This loop replaces a dumb call to strstr(..., "DEBUGPORT=").
566 * Indeed I want it to be case-insensitive to allow "debugport="
567 * or "DeBuGpOrT=" or... , and I don't want it to match malformed
568 * command-line options, such as:
570 * "...foo DEBUGPORT=xxx bar..."
571 * "...foo/DEBUGPORT=xxx bar..."
572 * "...foo/DEBUGPORT=bar..."
574 * i.e. the "DEBUGPORT=" switch must start with a slash and be separated
575 * from the rest by whitespace, unless it begins the command-line, e.g.:
577 * "/DEBUGPORT=COM1 foo...bar..."
578 * "...foo /DEBUGPORT=USB bar..."
580 * "...foo /DEBUGPORT= bar..."
581 * (in that case, we default the port to COM).
583 Options
= BootOptions
;
586 /* Skip possible initial whitespace */
587 Options
+= strspn(Options
, " \t");
589 /* Check whether a new option starts and it is the DEBUGPORT one */
590 if (*Options
!= '/' || _strnicmp(++Options
, "DEBUGPORT=", 10) != 0)
592 /* Search for another whitespace */
593 Options
= strpbrk(Options
, " \t");
598 /* We found the DEBUGPORT option. Move to the port name. */
607 * We have found the DEBUGPORT option. Parse the port name.
608 * Format: /DEBUGPORT=COM1 or /DEBUGPORT=FILE:\Device\HarddiskX\PartitionY\debug.log or /DEBUGPORT=FOO
609 * If we only have /DEBUGPORT= (i.e. without any port name), defaults it to "COM".
611 RtlStringCbCopyA(KdTransportDllName
, sizeof(KdTransportDllName
), "KD");
612 if (_strnicmp(Options
, "COM", 3) == 0 && '0' <= Options
[3] && Options
[3] <= '9')
614 RtlStringCbCatNA(KdTransportDllName
, sizeof(KdTransportDllName
), Options
, 3);
618 size_t i
= strcspn(Options
, " \t:"); /* Skip valid separators: whitespace or colon */
620 RtlStringCbCatA(KdTransportDllName
, sizeof(KdTransportDllName
), "COM");
622 RtlStringCbCatNA(KdTransportDllName
, sizeof(KdTransportDllName
), Options
, i
);
624 RtlStringCbCatA(KdTransportDllName
, sizeof(KdTransportDllName
), ".DLL");
625 _strupr(KdTransportDllName
);
628 * Load the transport DLL. Override the base DLL name of the
629 * loaded transport DLL to the default "KDCOM.DLL" name.
631 LoadModule(LoaderBlock
, DirPath
, KdTransportDllName
, "kdcom.dll", LoaderSystemCode
, &KdComDTE
, 60);
635 /* Load all referenced DLLs for Kernel, HAL and Kernel Debugger Transport DLL */
636 Success
= WinLdrScanImportDescriptorTable(&LoaderBlock
->LoadOrderListHead
, DirPath
, *KernelDTE
);
637 Success
&= WinLdrScanImportDescriptorTable(&LoaderBlock
->LoadOrderListHead
, DirPath
, HalDTE
);
640 Success
&= WinLdrScanImportDescriptorTable(&LoaderBlock
->LoadOrderListHead
, DirPath
, KdComDTE
);
655 USHORT OperatingSystemVersion
;
656 PLOADER_PARAMETER_BLOCK LoaderBlock
;
657 CHAR BootPath
[MAX_PATH
];
658 CHAR FileName
[MAX_PATH
];
659 CHAR BootOptions
[256];
661 ArgValue
= GetArgumentValue(Argc
, Argv
, "BootType");
664 ERR("No 'BootType' value, aborting!\n");
668 if (_stricmp(ArgValue
, "Windows") == 0 ||
669 _stricmp(ArgValue
, "Windows2003") == 0)
671 OperatingSystemVersion
= _WIN32_WINNT_WS03
;
673 else if (_stricmp(ArgValue
, "WindowsNT40") == 0)
675 OperatingSystemVersion
= _WIN32_WINNT_NT4
;
679 ERR("Unknown 'BootType' value '%s', aborting!\n", ArgValue
);
684 UiDrawProgressBarCenter(1, 100, "Loading NT...");
686 /* Retrieve the system path */
687 *BootPath
= ANSI_NULL
;
688 ArgValue
= GetArgumentValue(Argc
, Argv
, "SystemPath");
690 RtlStringCbCopyA(BootPath
, sizeof(BootPath
), ArgValue
);
693 * Check whether BootPath is a full path
694 * and if not, create a full boot path.
696 * See FsOpenFile for the technique used.
698 if (strrchr(BootPath
, ')') == NULL
)
700 /* Temporarily save the boot path */
701 RtlStringCbCopyA(FileName
, sizeof(FileName
), BootPath
);
703 /* This is not a full path. Use the current (i.e. boot) device. */
704 MachDiskGetBootPath(BootPath
, sizeof(BootPath
));
706 /* Append a path separator if needed */
707 if (*FileName
!= '\\' && *FileName
!= '/')
708 RtlStringCbCatA(BootPath
, sizeof(BootPath
), "\\");
710 /* Append the remaining path */
711 RtlStringCbCatA(BootPath
, sizeof(BootPath
), FileName
);
714 /* Append a backslash if needed */
715 if (!*BootPath
|| BootPath
[strlen(BootPath
) - 1] != '\\')
716 RtlStringCbCatA(BootPath
, sizeof(BootPath
), "\\");
718 TRACE("BootPath: '%s'\n", BootPath
);
720 /* Retrieve the boot options */
721 *BootOptions
= ANSI_NULL
;
722 ArgValue
= GetArgumentValue(Argc
, Argv
, "Options");
724 RtlStringCbCopyA(BootOptions
, sizeof(BootOptions
), ArgValue
);
726 /* Append boot-time options */
727 AppendBootTimeOptions(BootOptions
);
729 TRACE("BootOptions: '%s'\n", BootOptions
);
731 /* Check if a ramdisk file was given */
732 File
= strstr(BootOptions
, "/RDPATH=");
735 /* Copy the file name and everything else after it */
736 RtlStringCbCopyA(FileName
, sizeof(FileName
), File
+ 8);
739 *strstr(FileName
, " ") = ANSI_NULL
;
741 /* Load the ramdisk */
742 if (!RamDiskLoadVirtualFile(FileName
))
744 UiMessageBox("Failed to load RAM disk file %s", FileName
);
749 /* Let user know we started loading */
750 //UiDrawStatusText("Loading...");
752 /* Allocate and minimalist-initialize LPB */
753 AllocateAndInitLPB(&LoaderBlock
);
755 /* Load the system hive */
757 UiDrawProgressBarCenter(15, 100, "Loading system hive...");
758 Success
= WinLdrInitSystemHive(LoaderBlock
, BootPath
, FALSE
);
759 TRACE("SYSTEM hive %s\n", (Success
? "loaded" : "not loaded"));
760 /* Bail out if failure */
764 /* Load NLS data, OEM font, and prepare boot drivers list */
765 Success
= WinLdrScanSystemHive(LoaderBlock
, BootPath
);
766 TRACE("SYSTEM hive %s\n", (Success
? "scanned" : "not scanned"));
767 /* Bail out if failure */
772 return LoadAndBootWindowsCommon(OperatingSystemVersion
,
780 LoadAndBootWindowsCommon(
781 USHORT OperatingSystemVersion
,
782 PLOADER_PARAMETER_BLOCK LoaderBlock
,
787 PLOADER_PARAMETER_BLOCK LoaderBlockVA
;
789 PLDR_DATA_TABLE_ENTRY KernelDTE
;
790 KERNEL_ENTRY_POINT KiSystemStartup
;
793 TRACE("LoadAndBootWindowsCommon()\n");
796 /* Setup redirection support */
797 WinLdrSetupEms((PCHAR
)BootOptions
);
800 /* Convert BootPath to SystemRoot */
801 SystemRoot
= strstr(BootPath
, "\\");
803 /* Detect hardware */
805 UiDrawProgressBarCenter(20, 100, "Detecting hardware...");
806 LoaderBlock
->ConfigurationRoot
= MachHwDetect();
808 if (OperatingSystemVersion
== 0)
809 OperatingSystemVersion
= WinLdrDetectVersion();
811 /* Load the operating system core: the Kernel, the HAL and the Kernel Debugger Transport DLL */
812 Success
= LoadWindowsCore(OperatingSystemVersion
,
819 UiMessageBox("Error loading NTOS core.");
823 /* Load boot drivers */
825 UiDrawProgressBarCenter(100, 100, "Loading boot drivers...");
826 Success
= WinLdrLoadBootDrivers(LoaderBlock
, BootPath
);
827 TRACE("Boot drivers loading %s\n", Success
? "successful" : "failed");
829 /* Cleanup ini file */
832 /* Initialize Phase 1 - no drivers loading anymore */
833 WinLdrInitializePhase1(LoaderBlock
,
837 OperatingSystemVersion
);
839 /* Save entry-point pointer and Loader block VAs */
840 KiSystemStartup
= (KERNEL_ENTRY_POINT
)KernelDTE
->EntryPoint
;
841 LoaderBlockVA
= PaToVa(LoaderBlock
);
843 /* "Stop all motors", change videomode */
844 MachPrepareForReactOS();
847 //DumpMemoryAllocMap();
849 /* Do the machine specific initialization */
850 WinLdrSetupMachineDependent(LoaderBlock
);
852 /* Map pages and create memory descriptors */
853 WinLdrSetupMemoryLayout(LoaderBlock
);
855 /* Set processor context */
856 WinLdrSetProcessorContext();
858 /* Save final value of LoaderPagesSpanned */
859 LoaderBlock
->Extension
->LoaderPagesSpanned
= LoaderPagesSpanned
;
861 TRACE("Hello from paged mode, KiSystemStartup %p, LoaderBlockVA %p!\n",
862 KiSystemStartup
, LoaderBlockVA
);
864 // Zero KI_USER_SHARED_DATA page
865 memset((PVOID
)KI_USER_SHARED_DATA
, 0, MM_PAGE_SIZE
);
867 WinLdrpDumpMemoryDescriptors(LoaderBlockVA
);
868 WinLdrpDumpBootDriver(LoaderBlockVA
);
870 WinLdrpDumpArcDisks(LoaderBlockVA
);
874 (*KiSystemStartup
)(LoaderBlockVA
);
879 WinLdrpDumpMemoryDescriptors(PLOADER_PARAMETER_BLOCK LoaderBlock
)
882 PMEMORY_ALLOCATION_DESCRIPTOR MemoryDescriptor
;
884 NextMd
= LoaderBlock
->MemoryDescriptorListHead
.Flink
;
886 while (NextMd
!= &LoaderBlock
->MemoryDescriptorListHead
)
888 MemoryDescriptor
= CONTAINING_RECORD(NextMd
, MEMORY_ALLOCATION_DESCRIPTOR
, ListEntry
);
890 TRACE("BP %08X PC %04X MT %d\n", MemoryDescriptor
->BasePage
,
891 MemoryDescriptor
->PageCount
, MemoryDescriptor
->MemoryType
);
893 NextMd
= MemoryDescriptor
->ListEntry
.Flink
;
898 WinLdrpDumpBootDriver(PLOADER_PARAMETER_BLOCK LoaderBlock
)
901 PBOOT_DRIVER_LIST_ENTRY BootDriver
;
903 NextBd
= LoaderBlock
->BootDriverListHead
.Flink
;
905 while (NextBd
!= &LoaderBlock
->BootDriverListHead
)
907 BootDriver
= CONTAINING_RECORD(NextBd
, BOOT_DRIVER_LIST_ENTRY
, Link
);
909 TRACE("BootDriver %wZ DTE %08X RegPath: %wZ\n", &BootDriver
->FilePath
,
910 BootDriver
->LdrEntry
, &BootDriver
->RegistryPath
);
912 NextBd
= BootDriver
->Link
.Flink
;
917 WinLdrpDumpArcDisks(PLOADER_PARAMETER_BLOCK LoaderBlock
)
920 PARC_DISK_SIGNATURE ArcDisk
;
922 NextBd
= LoaderBlock
->ArcDiskInformation
->DiskSignatureListHead
.Flink
;
924 while (NextBd
!= &LoaderBlock
->ArcDiskInformation
->DiskSignatureListHead
)
926 ArcDisk
= CONTAINING_RECORD(NextBd
, ARC_DISK_SIGNATURE
, ListEntry
);
928 TRACE("ArcDisk %s checksum: 0x%X, signature: 0x%X\n",
929 ArcDisk
->ArcName
, ArcDisk
->CheckSum
, ArcDisk
->Signature
);
931 NextBd
= ArcDisk
->ListEntry
.Flink
;