c327f23bdf193926c4ddc25c2c9b8dc853162191
[reactos.git] / boot / freeldr / freeldr / windows / winldr.c
1 /*
2 * FreeLoader
3 *
4 * Copyright (C) 1998-2003 Brian Palmer <brianp@sginet.com>
5 * Copyright (C) 2006 Aleksey Bragin <aleksey@reactos.org>
6 *
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.
11 *
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.
16 *
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.
20 */
21
22 #include <freeldr.h>
23
24 #include <ndk/ldrtypes.h>
25 #include <debug.h>
26
27 DBG_DEFAULT_CHANNEL(WINDOWS);
28
29 //FIXME: Do a better way to retrieve Arc disk information
30 extern ULONG reactos_disk_count;
31 extern ARC_DISK_SIGNATURE reactos_arc_disk_info[];
32
33 extern ULONG LoaderPagesSpanned;
34 extern BOOLEAN AcpiPresent;
35
36 extern HEADLESS_LOADER_BLOCK LoaderRedirectionInformation;
37 extern BOOLEAN WinLdrTerminalConnected;
38 extern void WinLdrSetupEms(IN PCHAR BootOptions);
39
40 PLOADER_SYSTEM_BLOCK WinLdrSystemBlock;
41
42 // debug stuff
43 VOID DumpMemoryAllocMap(VOID);
44
45 // Init "phase 0"
46 VOID
47 AllocateAndInitLPB(PLOADER_PARAMETER_BLOCK *OutLoaderBlock)
48 {
49 PLOADER_PARAMETER_BLOCK LoaderBlock;
50
51 /* Allocate and zero-init the LPB */
52 WinLdrSystemBlock = MmAllocateMemoryWithType(sizeof(LOADER_SYSTEM_BLOCK),
53 LoaderSystemBlock);
54 if (WinLdrSystemBlock == NULL)
55 {
56 UiMessageBox("Failed to allocate memory for system block!");
57 return;
58 }
59
60 RtlZeroMemory(WinLdrSystemBlock, sizeof(LOADER_SYSTEM_BLOCK));
61
62 LoaderBlock = &WinLdrSystemBlock->LoaderBlock;
63 LoaderBlock->NlsData = &WinLdrSystemBlock->NlsDataBlock;
64
65 /* Init three critical lists, used right away */
66 InitializeListHead(&LoaderBlock->LoadOrderListHead);
67 InitializeListHead(&LoaderBlock->MemoryDescriptorListHead);
68 InitializeListHead(&LoaderBlock->BootDriverListHead);
69
70 *OutLoaderBlock = LoaderBlock;
71 }
72
73 // Init "phase 1"
74 VOID
75 WinLdrInitializePhase1(PLOADER_PARAMETER_BLOCK LoaderBlock,
76 LPCSTR Options,
77 LPCSTR SystemRoot,
78 LPCSTR BootPath,
79 USHORT VersionToBoot)
80 {
81 /* Examples of correct options and paths */
82 //CHAR Options[] = "/DEBUGPORT=COM1 /BAUDRATE=115200";
83 //CHAR Options[] = "/NODEBUG";
84 //CHAR SystemRoot[] = "\\WINNT\\";
85 //CHAR ArcBoot[] = "multi(0)disk(0)rdisk(0)partition(1)";
86
87 LPSTR LoadOptions, NewLoadOptions;
88 CHAR HalPath[] = "\\";
89 CHAR ArcBoot[256];
90 CHAR MiscFiles[256];
91 ULONG i;
92 ULONG_PTR PathSeparator;
93 PLOADER_PARAMETER_EXTENSION Extension;
94
95 /* Construct SystemRoot and ArcBoot from SystemPath */
96 PathSeparator = strstr(BootPath, "\\") - BootPath;
97 strncpy(ArcBoot, BootPath, PathSeparator);
98 ArcBoot[PathSeparator] = 0;
99
100 TRACE("ArcBoot: %s\n", ArcBoot);
101 TRACE("SystemRoot: %s\n", SystemRoot);
102 TRACE("Options: %s\n", Options);
103
104 /* Fill Arc BootDevice */
105 LoaderBlock->ArcBootDeviceName = WinLdrSystemBlock->ArcBootDeviceName;
106 strncpy(LoaderBlock->ArcBootDeviceName, ArcBoot, MAX_PATH);
107 LoaderBlock->ArcBootDeviceName = PaToVa(LoaderBlock->ArcBootDeviceName);
108
109 /* Fill Arc HalDevice, it matches ArcBoot path */
110 LoaderBlock->ArcHalDeviceName = WinLdrSystemBlock->ArcBootDeviceName;
111 LoaderBlock->ArcHalDeviceName = PaToVa(LoaderBlock->ArcHalDeviceName);
112
113 /* Fill SystemRoot */
114 LoaderBlock->NtBootPathName = WinLdrSystemBlock->NtBootPathName;
115 strncpy(LoaderBlock->NtBootPathName, SystemRoot, MAX_PATH);
116 LoaderBlock->NtBootPathName = PaToVa(LoaderBlock->NtBootPathName);
117
118 /* Fill NtHalPathName */
119 LoaderBlock->NtHalPathName = WinLdrSystemBlock->NtHalPathName;
120 strncpy(LoaderBlock->NtHalPathName, HalPath, MAX_PATH);
121 LoaderBlock->NtHalPathName = PaToVa(LoaderBlock->NtHalPathName);
122
123 /* Fill LoadOptions and strip the '/' commutator symbol in front of each option */
124 NewLoadOptions = LoadOptions = LoaderBlock->LoadOptions = WinLdrSystemBlock->LoadOptions;
125 strncpy(LoaderBlock->LoadOptions, Options, MAX_OPTIONS_LENGTH);
126
127 do
128 {
129 while (*LoadOptions == '/') ++LoadOptions;
130 *NewLoadOptions++ = *LoadOptions;
131 } while (*LoadOptions++);
132
133 LoaderBlock->LoadOptions = PaToVa(LoaderBlock->LoadOptions);
134
135 /* Arc devices */
136 LoaderBlock->ArcDiskInformation = &WinLdrSystemBlock->ArcDiskInformation;
137 InitializeListHead(&LoaderBlock->ArcDiskInformation->DiskSignatureListHead);
138
139 /* Convert ARC disk information from freeldr to a correct format */
140 for (i = 0; i < reactos_disk_count; i++)
141 {
142 PARC_DISK_SIGNATURE_EX ArcDiskSig;
143
144 /* Allocate the ARC structure */
145 ArcDiskSig = HeapAllocate(FrLdrDefaultHeap,
146 sizeof(ARC_DISK_SIGNATURE_EX),
147 'giSD');
148
149 /* Copy the data over */
150 ArcDiskSig->DiskSignature.Signature = reactos_arc_disk_info[i].Signature;
151 ArcDiskSig->DiskSignature.CheckSum = reactos_arc_disk_info[i].CheckSum;
152
153 /* Copy the ARC Name */
154 strncpy(ArcDiskSig->ArcName, reactos_arc_disk_info[i].ArcName, MAX_PATH);
155 ArcDiskSig->DiskSignature.ArcName = PaToVa(ArcDiskSig->ArcName);
156
157 /* Mark partition table as valid */
158 ArcDiskSig->DiskSignature.ValidPartitionTable = TRUE;
159
160 /* Insert into the list */
161 InsertTailList(&LoaderBlock->ArcDiskInformation->DiskSignatureListHead,
162 &ArcDiskSig->DiskSignature.ListEntry);
163 }
164
165 /* Convert all list's to Virtual address */
166
167 /* Convert the ArcDisks list to virtual address */
168 List_PaToVa(&LoaderBlock->ArcDiskInformation->DiskSignatureListHead);
169 LoaderBlock->ArcDiskInformation = PaToVa(LoaderBlock->ArcDiskInformation);
170
171 /* Convert configuration entries to VA */
172 ConvertConfigToVA(LoaderBlock->ConfigurationRoot);
173 LoaderBlock->ConfigurationRoot = PaToVa(LoaderBlock->ConfigurationRoot);
174
175 /* Convert all DTE into virtual addresses */
176 List_PaToVa(&LoaderBlock->LoadOrderListHead);
177
178 /* this one will be converted right before switching to
179 virtual paging mode */
180 //List_PaToVa(&LoaderBlock->MemoryDescriptorListHead);
181
182 /* Convert list of boot drivers */
183 List_PaToVa(&LoaderBlock->BootDriverListHead);
184
185 /* Initialize Extension now */
186 Extension = &WinLdrSystemBlock->Extension;
187 Extension->Size = sizeof(LOADER_PARAMETER_EXTENSION);
188 Extension->MajorVersion = (VersionToBoot & 0xFF00) >> 8;
189 Extension->MinorVersion = VersionToBoot & 0xFF;
190 Extension->Profile.Status = 2;
191
192 /* Check if ACPI is present */
193 if (AcpiPresent)
194 {
195 /* See KiRosFrldrLpbToNtLpb for details */
196 Extension->AcpiTable = (PVOID)1;
197 }
198
199 #ifdef _M_IX86
200 /* Set headless block pointer */
201 if (WinLdrTerminalConnected)
202 {
203 Extension->HeadlessLoaderBlock = &WinLdrSystemBlock->HeadlessLoaderBlock;
204 RtlCopyMemory(Extension->HeadlessLoaderBlock,
205 &LoaderRedirectionInformation,
206 sizeof(HEADLESS_LOADER_BLOCK));
207 Extension->HeadlessLoaderBlock = PaToVa(Extension->HeadlessLoaderBlock);
208 }
209 #endif
210 /* Load drivers database */
211 strcpy(MiscFiles, BootPath);
212 strcat(MiscFiles, "AppPatch\\drvmain.sdb");
213 Extension->DrvDBImage = PaToVa(WinLdrLoadModule(MiscFiles,
214 &Extension->DrvDBSize,
215 LoaderRegistryData));
216
217 /* Convert extension and setup block pointers */
218 LoaderBlock->Extension = PaToVa(Extension);
219
220 if (LoaderBlock->SetupLdrBlock)
221 LoaderBlock->SetupLdrBlock = PaToVa(LoaderBlock->SetupLdrBlock);
222
223 TRACE("WinLdrInitializePhase1() completed\n");
224 }
225
226 static BOOLEAN
227 WinLdrLoadDeviceDriver(PLIST_ENTRY LoadOrderListHead,
228 LPCSTR BootPath,
229 PUNICODE_STRING FilePath,
230 ULONG Flags,
231 PLDR_DATA_TABLE_ENTRY *DriverDTE)
232 {
233 CHAR FullPath[1024];
234 CHAR DriverPath[1024];
235 CHAR DllName[1024];
236 PCHAR DriverNamePos;
237 BOOLEAN Status;
238 PVOID DriverBase;
239
240 // Separate the path to file name and directory path
241 _snprintf(DriverPath, sizeof(DriverPath), "%wZ", FilePath);
242 DriverNamePos = strrchr(DriverPath, '\\');
243 if (DriverNamePos != NULL)
244 {
245 // Copy the name
246 strcpy(DllName, DriverNamePos+1);
247
248 // Cut out the name from the path
249 *(DriverNamePos+1) = 0;
250 }
251 else
252 {
253 // There is no directory in the path
254 strcpy(DllName, DriverPath);
255 DriverPath[0] = 0;
256 }
257
258 TRACE("DriverPath: %s, DllName: %s, LPB\n", DriverPath, DllName);
259
260
261 // Check if driver is already loaded
262 Status = WinLdrCheckForLoadedDll(LoadOrderListHead, DllName, DriverDTE);
263 if (Status)
264 {
265 // We've got the pointer to its DTE, just return success
266 return TRUE;
267 }
268
269 // It's not loaded, we have to load it
270 _snprintf(FullPath, sizeof(FullPath), "%s%wZ", BootPath, FilePath);
271 Status = WinLdrLoadImage(FullPath, LoaderBootDriver, &DriverBase);
272 if (!Status)
273 return FALSE;
274
275 // Allocate a DTE for it
276 Status = WinLdrAllocateDataTableEntry(LoadOrderListHead, DllName, DllName, DriverBase, DriverDTE);
277 if (!Status)
278 {
279 ERR("WinLdrAllocateDataTableEntry() failed\n");
280 return FALSE;
281 }
282
283 // Modify any flags, if needed
284 (*DriverDTE)->Flags |= Flags;
285
286 // Look for any dependencies it may have, and load them too
287 sprintf(FullPath,"%s%s", BootPath, DriverPath);
288 Status = WinLdrScanImportDescriptorTable(LoadOrderListHead, FullPath, *DriverDTE);
289 if (!Status)
290 {
291 ERR("WinLdrScanImportDescriptorTable() failed for %s\n", FullPath);
292 return FALSE;
293 }
294
295 return TRUE;
296 }
297
298 BOOLEAN
299 WinLdrLoadBootDrivers(PLOADER_PARAMETER_BLOCK LoaderBlock,
300 LPCSTR BootPath)
301 {
302 PLIST_ENTRY NextBd;
303 PBOOT_DRIVER_LIST_ENTRY BootDriver;
304 BOOLEAN Status;
305
306 // Walk through the boot drivers list
307 NextBd = LoaderBlock->BootDriverListHead.Flink;
308
309 while (NextBd != &LoaderBlock->BootDriverListHead)
310 {
311 BootDriver = CONTAINING_RECORD(NextBd, BOOT_DRIVER_LIST_ENTRY, Link);
312
313 TRACE("BootDriver %wZ DTE %08X RegPath: %wZ\n", &BootDriver->FilePath,
314 BootDriver->LdrEntry, &BootDriver->RegistryPath);
315
316 // Paths are relative (FIXME: Are they always relative?)
317
318 // Load it
319 Status = WinLdrLoadDeviceDriver(&LoaderBlock->LoadOrderListHead,
320 BootPath,
321 &BootDriver->FilePath,
322 0,
323 &BootDriver->LdrEntry);
324
325 // If loading failed - cry loudly
326 //FIXME: Maybe remove it from the list and try to continue?
327 if (!Status)
328 {
329 UiMessageBox("Can't load boot driver!");
330 return FALSE;
331 }
332
333 // Convert the RegistryPath and DTE addresses to VA since we are not going to use it anymore
334 BootDriver->RegistryPath.Buffer = PaToVa(BootDriver->RegistryPath.Buffer);
335 BootDriver->FilePath.Buffer = PaToVa(BootDriver->FilePath.Buffer);
336 BootDriver->LdrEntry = PaToVa(BootDriver->LdrEntry);
337
338 NextBd = BootDriver->Link.Flink;
339 }
340
341 return TRUE;
342 }
343
344 PVOID
345 WinLdrLoadModule(PCSTR ModuleName,
346 ULONG *Size,
347 TYPE_OF_MEMORY MemoryType)
348 {
349 ULONG FileId;
350 PVOID PhysicalBase;
351 FILEINFORMATION FileInfo;
352 ULONG FileSize;
353 ULONG Status;
354 ULONG BytesRead;
355
356 //CHAR ProgressString[256];
357
358 /* Inform user we are loading files */
359 //sprintf(ProgressString, "Loading %s...", FileName);
360 //UiDrawProgressBarCenter(1, 100, ProgressString);
361
362 TRACE("Loading module %s\n", ModuleName);
363 *Size = 0;
364
365 /* Open the image file */
366 Status = ArcOpen((PCHAR)ModuleName, OpenReadOnly, &FileId);
367 if (Status != ESUCCESS)
368 {
369 /* In case of errors, we just return, without complaining to the user */
370 return NULL;
371 }
372
373 /* Get this file's size */
374 Status = ArcGetFileInformation(FileId, &FileInfo);
375 if (Status != ESUCCESS)
376 {
377 ArcClose(FileId);
378 return NULL;
379 }
380 FileSize = FileInfo.EndingAddress.LowPart;
381 *Size = FileSize;
382
383 /* Allocate memory */
384 PhysicalBase = MmAllocateMemoryWithType(FileSize, MemoryType);
385 if (PhysicalBase == NULL)
386 {
387 ArcClose(FileId);
388 return NULL;
389 }
390
391 /* Load whole file */
392 Status = ArcRead(FileId, PhysicalBase, FileSize, &BytesRead);
393 ArcClose(FileId);
394 if (Status != ESUCCESS)
395 {
396 return NULL;
397 }
398
399 TRACE("Loaded %s at 0x%x with size 0x%x\n", ModuleName, PhysicalBase, FileSize);
400
401 return PhysicalBase;
402 }
403
404 USHORT
405 WinLdrDetectVersion()
406 {
407 LONG rc;
408 FRLDRHKEY hKey;
409
410 rc = RegOpenKey(
411 NULL,
412 L"\\Registry\\Machine\\SYSTEM\\CurrentControlSet\\Control\\Terminal Server",
413 &hKey);
414 if (rc != ERROR_SUCCESS)
415 {
416 // Key doesn't exist; assume NT 4.0
417 return _WIN32_WINNT_NT4;
418 }
419
420 // We may here want to read the value of ProductVersion
421 return _WIN32_WINNT_WS03;
422 }
423
424 static
425 PVOID
426 LoadModule(
427 PLOADER_PARAMETER_BLOCK LoaderBlock,
428 PCCH Path,
429 PCCH File,
430 TYPE_OF_MEMORY MemoryType,
431 PLDR_DATA_TABLE_ENTRY *Dte,
432 BOOLEAN IsKdTransportDll,
433 ULONG Percentage)
434 {
435 CHAR FullFileName[MAX_PATH];
436 CHAR ProgressString[256];
437 NTSTATUS Status;
438 PVOID BaseAdress;
439
440 UiDrawBackdrop();
441 sprintf(ProgressString, "Loading %s...", File);
442 UiDrawProgressBarCenter(Percentage, 100, ProgressString);
443
444 strcpy(FullFileName, Path);
445 strcat(FullFileName, "SYSTEM32\\");
446 strcat(FullFileName, File);
447
448 Status = WinLdrLoadImage(FullFileName, MemoryType, &BaseAdress);
449 TRACE("%s loaded with status %d at %p\n",
450 File, Status, BaseAdress);
451
452 strcpy(FullFileName, "WINDOWS\\SYSTEM32\\");
453 strcat(FullFileName, File);
454 /*
455 * Cheat about the base DLL name if we are loading
456 * the Kernel Debugger Transport DLL, to make the
457 * PE loader happy.
458 */
459 WinLdrAllocateDataTableEntry(&LoaderBlock->LoadOrderListHead,
460 (IsKdTransportDll ? "KDCOM.DLL" : File),
461 FullFileName,
462 BaseAdress,
463 Dte);
464
465 return BaseAdress;
466 }
467
468 static
469 BOOLEAN
470 LoadWindowsCore(IN USHORT OperatingSystemVersion,
471 IN OUT PLOADER_PARAMETER_BLOCK LoaderBlock,
472 IN LPCSTR BootOptions,
473 IN LPCSTR BootPath,
474 IN OUT PLDR_DATA_TABLE_ENTRY* KernelDTE)
475 {
476 BOOLEAN Status;
477 CHAR DirPath[MAX_PATH];
478 CHAR KdTransportDllName[MAX_PATH];
479 PLDR_DATA_TABLE_ENTRY HalDTE, KdComDTE = NULL;
480
481 if (!KernelDTE) return FALSE;
482
483 /* Load the Kernel */
484 LoadModule(LoaderBlock, BootPath, "NTOSKRNL.EXE", LoaderSystemCode, KernelDTE, FALSE, 30);
485
486 /* Load the HAL */
487 LoadModule(LoaderBlock, BootPath, "HAL.DLL", LoaderHalCode, &HalDTE, FALSE, 45);
488
489 /* Load the Kernel Debugger Transport DLL */
490 if (OperatingSystemVersion > _WIN32_WINNT_WIN2K)
491 {
492 /*
493 * According to http://www.nynaeve.net/?p=173 :
494 * "[...] Another enhancement that could be done Microsoft-side would be
495 * a better interface for replacing KD transport modules. Right now, due
496 * to the fact that ntoskrnl is static linked to KDCOM.DLL, the OS loader
497 * has a hardcoded hack that interprets the KD type in the OS loader options,
498 * loads one of the (hardcoded filenames) "kdcom.dll", "kd1394.dll", or
499 * "kdusb2.dll" modules, and inserts them into the loaded module list under
500 * the name "kdcom.dll". [...]"
501 */
502
503 /*
504 * This loop replaces a dumb call to strstr(..., "DEBUGPORT=").
505 * Indeed I want it to be case-insensitive to allow "debugport="
506 * or "DeBuGpOrT=" or... , and I don't want it to match malformed
507 * command-line options, such as:
508 *
509 * "...foo DEBUGPORT=xxx bar..."
510 * "...foo/DEBUGPORT=xxx bar..."
511 * "...foo/DEBUGPORT=bar..."
512 *
513 * i.e. the "DEBUGPORT=" switch must start with a slash and be separated
514 * from the rest by whitespace, unless it begins the command-line, e.g.:
515 *
516 * "/DEBUGPORT=COM1 foo...bar..."
517 * "...foo /DEBUGPORT=USB bar..."
518 * or:
519 * "...foo /DEBUGPORT= bar..."
520 * (in that case, we default the port to COM).
521 */
522 while (BootOptions)
523 {
524 /* Skip possible initial whitespace */
525 BootOptions += strspn(BootOptions, " \t");
526
527 /* Check whether a new commutator starts and it is the DEBUGPORT one */
528 if (*BootOptions != '/' || _strnicmp(++BootOptions, "DEBUGPORT=", 10) != 0)
529 {
530 /* Search for another whitespace */
531 BootOptions = strpbrk(BootOptions, " \t");
532 continue;
533 }
534 else
535 {
536 /* We found the DEBUGPORT commutator. Move to the port name. */
537 BootOptions += 10;
538 break;
539 }
540 }
541
542 if (BootOptions)
543 {
544 /*
545 * We have found the DEBUGPORT commutator. Parse the port name.
546 * Format: /DEBUGPORT=COM1 or /DEBUGPORT=FILE:\Device\HarddiskX\PartitionY\debug.log or /DEBUGPORT=FOO
547 * If we only have /DEBUGPORT= (i.e. without any port name), defaults it to "COM".
548 */
549 strcpy(KdTransportDllName, "KD");
550 if (_strnicmp(BootOptions, "COM", 3) == 0 && '0' <= BootOptions[3] && BootOptions[3] <= '9')
551 {
552 strncat(KdTransportDllName, BootOptions, 3);
553 }
554 else
555 {
556 size_t i = strcspn(BootOptions, " \t:"); /* Skip valid separators: whitespace or colon */
557 if (i == 0)
558 strcat(KdTransportDllName, "COM");
559 else
560 strncat(KdTransportDllName, BootOptions, i);
561 }
562 strcat(KdTransportDllName, ".DLL");
563 _strupr(KdTransportDllName);
564
565 /*
566 * Load the transport DLL. Specify it to LoadModule so that it can
567 * change the base DLL name of the loaded transport DLL to the default
568 * "KDCOM.DLL" name, to make the PE loader happy.
569 */
570 LoadModule(LoaderBlock, BootPath, KdTransportDllName, LoaderSystemCode, &KdComDTE, TRUE, 60);
571 }
572 }
573
574 /* Load all referenced DLLs for Kernel, HAL and Kernel Debugger Transport DLL */
575 strcpy(DirPath, BootPath);
576 strcat(DirPath, "system32\\");
577 Status = WinLdrScanImportDescriptorTable(&LoaderBlock->LoadOrderListHead, DirPath, *KernelDTE);
578 Status &= WinLdrScanImportDescriptorTable(&LoaderBlock->LoadOrderListHead, DirPath, HalDTE);
579 if (KdComDTE)
580 {
581 Status &= WinLdrScanImportDescriptorTable(&LoaderBlock->LoadOrderListHead, DirPath, KdComDTE);
582 }
583
584 return Status;
585 }
586
587 VOID
588 LoadAndBootWindows(IN OperatingSystemItem* OperatingSystem,
589 IN USHORT OperatingSystemVersion)
590 {
591 ULONG_PTR SectionId;
592 PCSTR SectionName = OperatingSystem->SystemPartition;
593 CHAR SettingsValue[80];
594 BOOLEAN HasSection;
595 CHAR BootPath[MAX_PATH];
596 CHAR FileName[MAX_PATH];
597 CHAR BootOptions[256];
598 PCHAR File;
599 BOOLEAN Status;
600 PLOADER_PARAMETER_BLOCK LoaderBlock;
601
602 // Get OS setting value
603 SettingsValue[0] = ANSI_NULL;
604 IniOpenSection("Operating Systems", &SectionId);
605 IniReadSettingByName(SectionId, SectionName, SettingsValue, sizeof(SettingsValue));
606
607 // Open the operating system section
608 // specified in the .ini file
609 HasSection = IniOpenSection(SectionName, &SectionId);
610
611 UiDrawBackdrop();
612 UiDrawProgressBarCenter(1, 100, "Loading NT...");
613
614 /* Read the system path is set in the .ini file */
615 if (!HasSection ||
616 !IniReadSettingByName(SectionId, "SystemPath", BootPath, sizeof(BootPath)))
617 {
618 strcpy(BootPath, SectionName);
619 }
620
621 /* Special case for LiveCD */
622 if (!_strnicmp(BootPath, "LiveCD", strlen("LiveCD")))
623 {
624 strcpy(FileName, BootPath + strlen("LiveCD"));
625 MachDiskGetBootPath(BootPath, sizeof(BootPath));
626 strcat(BootPath, FileName);
627 }
628
629 /* Append a backslash */
630 if ((strlen(BootPath)==0) || BootPath[strlen(BootPath)] != '\\')
631 strcat(BootPath, "\\");
632
633 /* Read booting options */
634 if (!HasSection || !IniReadSettingByName(SectionId, "Options", BootOptions, sizeof(BootOptions)))
635 {
636 /* Get options after the title */
637 PCSTR p = SettingsValue;
638 while (*p == ' ' || *p == '"')
639 p++;
640 while (*p != '\0' && *p != '"')
641 p++;
642 strcpy(BootOptions, p);
643 TRACE("BootOptions: '%s'\n", BootOptions);
644 }
645
646 /* Append boot-time options */
647 AppendBootTimeOptions(BootOptions);
648
649 /* Check if a ramdisk file was given */
650 File = strstr(BootOptions, "/RDPATH=");
651 if (File)
652 {
653 /* Copy the file name and everything else after it */
654 strcpy(FileName, File + 8);
655
656 /* Null-terminate */
657 *strstr(FileName, " ") = ANSI_NULL;
658
659 /* Load the ramdisk */
660 RamDiskLoadVirtualFile(FileName);
661 }
662
663 /* Let user know we started loading */
664 //UiDrawStatusText("Loading...");
665
666 TRACE("BootPath: '%s'\n", BootPath);
667
668 /* Allocate and minimalistic-initialize LPB */
669 AllocateAndInitLPB(&LoaderBlock);
670
671 #ifdef _M_IX86
672 /* Setup redirection support */
673 WinLdrSetupEms(BootOptions);
674 #endif
675
676 /* Load Hive */
677 UiDrawBackdrop();
678 UiDrawProgressBarCenter(15, 100, "Loading system hive...");
679 Status = WinLdrInitSystemHive(LoaderBlock, BootPath);
680 TRACE("SYSTEM hive loaded with status %d\n", Status);
681
682 /* Load NLS data, OEM font, and prepare boot drivers list */
683 Status = WinLdrScanSystemHive(LoaderBlock, BootPath);
684 TRACE("SYSTEM hive scanned with status %d\n", Status);
685
686 /* Finish loading */
687 LoadAndBootWindowsCommon(OperatingSystemVersion,
688 LoaderBlock,
689 BootOptions,
690 BootPath,
691 0);
692 }
693
694 VOID
695 LoadAndBootWindowsCommon(
696 USHORT OperatingSystemVersion,
697 PLOADER_PARAMETER_BLOCK LoaderBlock,
698 LPCSTR BootOptions,
699 LPCSTR BootPath,
700 BOOLEAN Setup)
701 {
702 PLOADER_PARAMETER_BLOCK LoaderBlockVA;
703 BOOLEAN Status;
704 PLDR_DATA_TABLE_ENTRY KernelDTE;
705 KERNEL_ENTRY_POINT KiSystemStartup;
706 LPCSTR SystemRoot;
707 TRACE("LoadAndBootWindowsCommon()\n");
708
709 /* Convert BootPath to SystemRoot */
710 SystemRoot = strstr(BootPath, "\\");
711
712 /* Detect hardware */
713 LoaderBlock->ConfigurationRoot = MachHwDetect();
714
715 if (OperatingSystemVersion == 0)
716 OperatingSystemVersion = WinLdrDetectVersion();
717
718 /* Load the operating system core: the Kernel, the HAL and the Kernel Debugger Transport DLL */
719 Status = LoadWindowsCore(OperatingSystemVersion,
720 LoaderBlock,
721 BootOptions,
722 BootPath,
723 &KernelDTE);
724 if (!Status)
725 {
726 UiMessageBox("Error loading NTOS core.");
727 return;
728 }
729
730 /* Load boot drivers */
731 UiDrawBackdrop();
732 UiDrawProgressBarCenter(100, 100, "Loading boot drivers...");
733 Status = WinLdrLoadBootDrivers(LoaderBlock, BootPath);
734 TRACE("Boot drivers loaded with status %d\n", Status);
735
736 /* Initialize Phase 1 - no drivers loading anymore */
737 WinLdrInitializePhase1(LoaderBlock,
738 BootOptions,
739 SystemRoot,
740 BootPath,
741 OperatingSystemVersion);
742
743 /* Save entry-point pointer and Loader block VAs */
744 KiSystemStartup = (KERNEL_ENTRY_POINT)KernelDTE->EntryPoint;
745 LoaderBlockVA = PaToVa(LoaderBlock);
746
747 /* "Stop all motors", change videomode */
748 MachPrepareForReactOS(Setup);
749
750 /* Debugging... */
751 //DumpMemoryAllocMap();
752
753 /* Do the machine specific initialization */
754 WinLdrSetupMachineDependent(LoaderBlock);
755
756 /* Map pages and create memory descriptors */
757 WinLdrSetupMemoryLayout(LoaderBlock);
758
759 /* Set processor context */
760 WinLdrSetProcessorContext();
761
762 /* Save final value of LoaderPagesSpanned */
763 LoaderBlock->Extension->LoaderPagesSpanned = LoaderPagesSpanned;
764
765 TRACE("Hello from paged mode, KiSystemStartup %p, LoaderBlockVA %p!\n",
766 KiSystemStartup, LoaderBlockVA);
767
768 // Zero KI_USER_SHARED_DATA page
769 memset((PVOID)KI_USER_SHARED_DATA, 0, MM_PAGE_SIZE);
770
771 WinLdrpDumpMemoryDescriptors(LoaderBlockVA);
772 WinLdrpDumpBootDriver(LoaderBlockVA);
773 #ifndef _M_AMD64
774 WinLdrpDumpArcDisks(LoaderBlockVA);
775 #endif
776
777 //FIXME: If I substitute this debugging checkpoint, GCC will "optimize away" the code below
778 //while (1) {};
779 /*asm(".intel_syntax noprefix\n");
780 asm("test1:\n");
781 asm("jmp test1\n");
782 asm(".att_syntax\n");*/
783
784 /* Pass control */
785 (*KiSystemStartup)(LoaderBlockVA);
786 }
787
788 VOID
789 WinLdrpDumpMemoryDescriptors(PLOADER_PARAMETER_BLOCK LoaderBlock)
790 {
791 PLIST_ENTRY NextMd;
792 PMEMORY_ALLOCATION_DESCRIPTOR MemoryDescriptor;
793
794 NextMd = LoaderBlock->MemoryDescriptorListHead.Flink;
795
796 while (NextMd != &LoaderBlock->MemoryDescriptorListHead)
797 {
798 MemoryDescriptor = CONTAINING_RECORD(NextMd, MEMORY_ALLOCATION_DESCRIPTOR, ListEntry);
799
800 TRACE("BP %08X PC %04X MT %d\n", MemoryDescriptor->BasePage,
801 MemoryDescriptor->PageCount, MemoryDescriptor->MemoryType);
802
803 NextMd = MemoryDescriptor->ListEntry.Flink;
804 }
805 }
806
807 VOID
808 WinLdrpDumpBootDriver(PLOADER_PARAMETER_BLOCK LoaderBlock)
809 {
810 PLIST_ENTRY NextBd;
811 PBOOT_DRIVER_LIST_ENTRY BootDriver;
812
813 NextBd = LoaderBlock->BootDriverListHead.Flink;
814
815 while (NextBd != &LoaderBlock->BootDriverListHead)
816 {
817 BootDriver = CONTAINING_RECORD(NextBd, BOOT_DRIVER_LIST_ENTRY, Link);
818
819 TRACE("BootDriver %wZ DTE %08X RegPath: %wZ\n", &BootDriver->FilePath,
820 BootDriver->LdrEntry, &BootDriver->RegistryPath);
821
822 NextBd = BootDriver->Link.Flink;
823 }
824 }
825
826 VOID
827 WinLdrpDumpArcDisks(PLOADER_PARAMETER_BLOCK LoaderBlock)
828 {
829 PLIST_ENTRY NextBd;
830 PARC_DISK_SIGNATURE ArcDisk;
831
832 NextBd = LoaderBlock->ArcDiskInformation->DiskSignatureListHead.Flink;
833
834 while (NextBd != &LoaderBlock->ArcDiskInformation->DiskSignatureListHead)
835 {
836 ArcDisk = CONTAINING_RECORD(NextBd, ARC_DISK_SIGNATURE, ListEntry);
837
838 TRACE("ArcDisk %s checksum: 0x%X, signature: 0x%X\n",
839 ArcDisk->ArcName, ArcDisk->CheckSum, ArcDisk->Signature);
840
841 NextBd = ArcDisk->ListEntry.Flink;
842 }
843 }