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