[freeldr] Never suppose that buffer in UNICODE_STRING is null terminated. Fixes some...
[reactos.git] / reactos / 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 // TODO: Move to .h
28 void WinLdrSetupForNt(PLOADER_PARAMETER_BLOCK LoaderBlock,
29 PVOID *GdtIdt,
30 ULONG *PcrBasePage,
31 ULONG *TssBasePage);
32
33 //FIXME: Do a better way to retrieve Arc disk information
34 extern ULONG reactos_disk_count;
35 extern ARC_DISK_SIGNATURE reactos_arc_disk_info[];
36 extern char reactos_arc_strings[32][256];
37
38 extern BOOLEAN UseRealHeap;
39 extern ULONG LoaderPagesSpanned;
40 extern BOOLEAN AcpiPresent;
41
42 BOOLEAN
43 WinLdrCheckForLoadedDll(IN OUT PLOADER_PARAMETER_BLOCK WinLdrBlock,
44 IN PCH DllName,
45 OUT PLDR_DATA_TABLE_ENTRY *LoadedEntry);
46
47 // debug stuff
48 VOID DumpMemoryAllocMap(VOID);
49 VOID WinLdrpDumpMemoryDescriptors(PLOADER_PARAMETER_BLOCK LoaderBlock);
50 VOID WinLdrpDumpBootDriver(PLOADER_PARAMETER_BLOCK LoaderBlock);
51 VOID WinLdrpDumpArcDisks(PLOADER_PARAMETER_BLOCK LoaderBlock);
52
53
54 // Init "phase 0"
55 VOID
56 AllocateAndInitLPB(PLOADER_PARAMETER_BLOCK *OutLoaderBlock)
57 {
58 PLOADER_PARAMETER_BLOCK LoaderBlock;
59
60 /* Allocate and zero-init the LPB */
61 LoaderBlock = MmHeapAlloc(sizeof(LOADER_PARAMETER_BLOCK));
62 RtlZeroMemory(LoaderBlock, sizeof(LOADER_PARAMETER_BLOCK));
63
64 /* Init three critical lists, used right away */
65 InitializeListHead(&LoaderBlock->LoadOrderListHead);
66 InitializeListHead(&LoaderBlock->MemoryDescriptorListHead);
67 InitializeListHead(&LoaderBlock->BootDriverListHead);
68
69 /* Alloc space for NLS (it will be converted to VA in WinLdrLoadNLS) */
70 LoaderBlock->NlsData = MmHeapAlloc(sizeof(NLS_DATA_BLOCK));
71 if (LoaderBlock->NlsData == NULL)
72 {
73 UiMessageBox("Failed to allocate memory for NLS table data!");
74 return;
75 }
76 RtlZeroMemory(LoaderBlock->NlsData, sizeof(NLS_DATA_BLOCK));
77
78 *OutLoaderBlock = LoaderBlock;
79 }
80
81 // Init "phase 1"
82 VOID
83 WinLdrInitializePhase1(PLOADER_PARAMETER_BLOCK LoaderBlock,
84 PCHAR Options,
85 PCHAR SystemRoot,
86 PCHAR BootPath,
87 USHORT VersionToBoot)
88 {
89 /* Examples of correct options and paths */
90 //CHAR Options[] = "/DEBUGPORT=COM1 /BAUDRATE=115200";
91 //CHAR Options[] = "/NODEBUG";
92 //CHAR SystemRoot[] = "\\WINNT\\";
93 //CHAR ArcBoot[] = "multi(0)disk(0)rdisk(0)partition(1)";
94
95 CHAR HalPath[] = "\\";
96 CHAR ArcBoot[256];
97 CHAR MiscFiles[256];
98 ULONG i, PathSeparator;
99 PLOADER_PARAMETER_EXTENSION Extension;
100
101 /* Construct SystemRoot and ArcBoot from SystemPath */
102 PathSeparator = strstr(BootPath, "\\") - BootPath;
103 strncpy(ArcBoot, BootPath, PathSeparator);
104 ArcBoot[PathSeparator] = 0;
105
106 DPRINTM(DPRINT_WINDOWS, "ArcBoot: %s\n", ArcBoot);
107 DPRINTM(DPRINT_WINDOWS, "SystemRoot: %s\n", SystemRoot);
108 DPRINTM(DPRINT_WINDOWS, "Options: %s\n", Options);
109
110 /* Fill Arc BootDevice */
111 LoaderBlock->ArcBootDeviceName = MmHeapAlloc(strlen(ArcBoot)+1);
112 strcpy(LoaderBlock->ArcBootDeviceName, ArcBoot);
113 LoaderBlock->ArcBootDeviceName = PaToVa(LoaderBlock->ArcBootDeviceName);
114
115 /* Fill Arc HalDevice, it matches ArcBoot path */
116 LoaderBlock->ArcHalDeviceName = MmHeapAlloc(strlen(ArcBoot)+1);
117 strcpy(LoaderBlock->ArcHalDeviceName, ArcBoot);
118 LoaderBlock->ArcHalDeviceName = PaToVa(LoaderBlock->ArcHalDeviceName);
119
120 /* Fill SystemRoot */
121 LoaderBlock->NtBootPathName = MmHeapAlloc(strlen(SystemRoot)+1);
122 strcpy(LoaderBlock->NtBootPathName, SystemRoot);
123 LoaderBlock->NtBootPathName = PaToVa(LoaderBlock->NtBootPathName);
124
125 /* Fill NtHalPathName */
126 LoaderBlock->NtHalPathName = MmHeapAlloc(strlen(HalPath)+1);
127 strcpy(LoaderBlock->NtHalPathName, HalPath);
128 LoaderBlock->NtHalPathName = PaToVa(LoaderBlock->NtHalPathName);
129
130 /* Fill load options */
131 LoaderBlock->LoadOptions = MmHeapAlloc(strlen(Options)+1);
132 strcpy(LoaderBlock->LoadOptions, Options);
133 LoaderBlock->LoadOptions = PaToVa(LoaderBlock->LoadOptions);
134
135 /* Arc devices */
136 LoaderBlock->ArcDiskInformation = (PARC_DISK_INFORMATION)MmHeapAlloc(sizeof(ARC_DISK_INFORMATION));
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 ArcDiskInfo;
143
144 /* Get the ARC structure */
145 ArcDiskInfo = (PARC_DISK_SIGNATURE)MmHeapAlloc(sizeof(ARC_DISK_SIGNATURE));
146 RtlZeroMemory(ArcDiskInfo, sizeof(ARC_DISK_SIGNATURE));
147
148 /* Copy the data over */
149 ArcDiskInfo->Signature = reactos_arc_disk_info[i].Signature;
150 ArcDiskInfo->CheckSum = reactos_arc_disk_info[i].CheckSum;
151
152 /* Copy the ARC Name */
153 ArcDiskInfo->ArcName = (PCHAR)MmHeapAlloc(sizeof(CHAR)*256);
154 strcpy(ArcDiskInfo->ArcName, reactos_arc_disk_info[i].ArcName);
155 ArcDiskInfo->ArcName = (PCHAR)PaToVa(ArcDiskInfo->ArcName);
156
157 /* Mark partition table as valid */
158 ArcDiskInfo->ValidPartitionTable = TRUE;
159
160 /* Insert into the list */
161 InsertTailList(&LoaderBlock->ArcDiskInformation->DiskSignatureListHead,
162 &ArcDiskInfo->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 = MmHeapAlloc(sizeof(LOADER_PARAMETER_EXTENSION));
187 if (Extension == NULL)
188 {
189 UiMessageBox("Failed to allocate LPB Extension!");
190 return;
191 }
192 RtlZeroMemory(Extension, sizeof(LOADER_PARAMETER_EXTENSION));
193
194 /* Fill LPB extension */
195 Extension->Size = sizeof(LOADER_PARAMETER_EXTENSION);
196 Extension->MajorVersion = (VersionToBoot & 0xFF00) >> 8;
197 Extension->MinorVersion = VersionToBoot & 0xFF;
198 Extension->Profile.Status = 2;
199
200 /* Check if ACPI is present */
201 if (AcpiPresent)
202 {
203 /* See KiRosFrldrLpbToNtLpb for details */
204 Extension->AcpiTable = (PVOID)1;
205 }
206
207 /* Load drivers database */
208 strcpy(MiscFiles, BootPath);
209 strcat(MiscFiles, "AppPatch\\drvmain.sdb");
210 Extension->DrvDBImage = PaToVa(WinLdrLoadModule(MiscFiles,
211 &Extension->DrvDBSize, LoaderRegistryData));
212
213 /* Convert extension and setup block pointers */
214 LoaderBlock->Extension = PaToVa(Extension);
215
216 if (LoaderBlock->SetupLdrBlock)
217 LoaderBlock->SetupLdrBlock = PaToVa(LoaderBlock->SetupLdrBlock);
218 }
219
220 BOOLEAN
221 WinLdrLoadDeviceDriver(PLOADER_PARAMETER_BLOCK LoaderBlock,
222 LPSTR BootPath,
223 PUNICODE_STRING FilePath,
224 ULONG Flags,
225 PLDR_DATA_TABLE_ENTRY *DriverDTE)
226 {
227 CHAR FullPath[1024];
228 CHAR DriverPath[1024];
229 CHAR DllName[1024];
230 PCHAR DriverNamePos;
231 BOOLEAN Status;
232 PVOID DriverBase;
233
234 // Separate the path to file name and directory path
235 snprintf(DriverPath, sizeof(DriverPath), "%wZ", FilePath);
236 DriverNamePos = strrchr(DriverPath, '\\');
237 if (DriverNamePos != NULL)
238 {
239 // Copy the name
240 strcpy(DllName, DriverNamePos+1);
241
242 // Cut out the name from the path
243 *(DriverNamePos+1) = 0;
244 }
245 else
246 {
247 // There is no directory in the path
248 strcpy(DllName, DriverPath);
249 DriverPath[0] = 0;
250 }
251
252 DPRINTM(DPRINT_WINDOWS, "DriverPath: %s, DllName: %s, LPB %p\n", DriverPath, DllName, LoaderBlock);
253
254
255 // Check if driver is already loaded
256 Status = WinLdrCheckForLoadedDll(LoaderBlock, DllName, DriverDTE);
257 if (Status)
258 {
259 // We've got the pointer to its DTE, just return success
260 return TRUE;
261 }
262
263 // It's not loaded, we have to load it
264 snprintf(FullPath, sizeof(FullPath), "%s%wZ", BootPath, FilePath);
265 Status = WinLdrLoadImage(FullPath, LoaderBootDriver, &DriverBase);
266 if (!Status)
267 return FALSE;
268
269 // Allocate a DTE for it
270 Status = WinLdrAllocateDataTableEntry(LoaderBlock, DllName, DllName, DriverBase, DriverDTE);
271 if (!Status)
272 {
273 DPRINTM(DPRINT_WINDOWS, "WinLdrAllocateDataTableEntry() failed\n");
274 return FALSE;
275 }
276
277 // Modify any flags, if needed
278 (*DriverDTE)->Flags |= Flags;
279
280 // Look for any dependencies it may have, and load them too
281 sprintf(FullPath,"%s%s", BootPath, DriverPath);
282 Status = WinLdrScanImportDescriptorTable(LoaderBlock, FullPath, *DriverDTE);
283 if (!Status)
284 {
285 DPRINTM(DPRINT_WINDOWS, "WinLdrScanImportDescriptorTable() failed for %s\n",
286 FullPath);
287 return FALSE;
288 }
289
290 return TRUE;
291 }
292
293 BOOLEAN
294 WinLdrLoadBootDrivers(PLOADER_PARAMETER_BLOCK LoaderBlock,
295 LPSTR BootPath)
296 {
297 PLIST_ENTRY NextBd;
298 PBOOT_DRIVER_LIST_ENTRY BootDriver;
299 BOOLEAN Status;
300
301 // Walk through the boot drivers list
302 NextBd = LoaderBlock->BootDriverListHead.Flink;
303
304 while (NextBd != &LoaderBlock->BootDriverListHead)
305 {
306 BootDriver = CONTAINING_RECORD(NextBd, BOOT_DRIVER_LIST_ENTRY, Link);
307
308 DPRINTM(DPRINT_WINDOWS, "BootDriver %wZ DTE %08X RegPath: %wZ\n", &BootDriver->FilePath,
309 BootDriver->LdrEntry, &BootDriver->RegistryPath);
310
311 // Paths are relative (FIXME: Are they always relative?)
312
313 // Load it
314 Status = WinLdrLoadDeviceDriver(LoaderBlock, BootPath, &BootDriver->FilePath,
315 0, &BootDriver->LdrEntry);
316
317 // If loading failed - cry loudly
318 //FIXME: Maybe remove it from the list and try to continue?
319 if (!Status)
320 {
321 UiMessageBox("Can't load boot driver!");
322 return FALSE;
323 }
324
325 // Convert the RegistryPath and DTE addresses to VA since we are not going to use it anymore
326 BootDriver->RegistryPath.Buffer = PaToVa(BootDriver->RegistryPath.Buffer);
327 BootDriver->FilePath.Buffer = PaToVa(BootDriver->FilePath.Buffer);
328 BootDriver->LdrEntry = PaToVa(BootDriver->LdrEntry);
329
330 NextBd = BootDriver->Link.Flink;
331 }
332
333 return TRUE;
334 }
335
336 PVOID WinLdrLoadModule(PCSTR ModuleName, ULONG *Size,
337 TYPE_OF_MEMORY MemoryType)
338 {
339 ULONG FileId;
340 PVOID PhysicalBase;
341 FILEINFORMATION FileInfo;
342 ULONG FileSize;
343 ULONG Status;
344 ULONG BytesRead;
345
346 //CHAR ProgressString[256];
347
348 /* Inform user we are loading files */
349 //sprintf(ProgressString, "Loading %s...", FileName);
350 //UiDrawProgressBarCenter(1, 100, ProgressString);
351
352 DPRINTM(DPRINT_WINDOWS, "Loading module %s\n", ModuleName);
353 *Size = 0;
354
355 /* Open the image file */
356 Status = ArcOpen((PCHAR)ModuleName, OpenReadOnly, &FileId);
357 if (Status != ESUCCESS)
358 {
359 /* In case of errors, we just return, without complaining to the user */
360 return NULL;
361 }
362
363 /* Get this file's size */
364 Status = ArcGetFileInformation(FileId, &FileInfo);
365 if (Status != ESUCCESS)
366 {
367 ArcClose(FileId);
368 return NULL;
369 }
370 FileSize = FileInfo.EndingAddress.LowPart;
371 *Size = FileSize;
372
373 /* Allocate memory */
374 PhysicalBase = MmAllocateMemoryWithType(FileSize, MemoryType);
375 if (PhysicalBase == NULL)
376 {
377 ArcClose(FileId);
378 return NULL;
379 }
380
381 /* Load whole file */
382 Status = ArcRead(FileId, PhysicalBase, FileSize, &BytesRead);
383 ArcClose(FileId);
384 if (Status != ESUCCESS)
385 {
386 return NULL;
387 }
388
389 DPRINTM(DPRINT_WINDOWS, "Loaded %s at 0x%x with size 0x%x\n", ModuleName, PhysicalBase, FileSize);
390
391 return PhysicalBase;
392 }
393
394
395 USHORT
396 WinLdrDetectVersion()
397 {
398 LONG rc;
399 FRLDRHKEY hKey;
400
401 rc = RegOpenKey(
402 NULL,
403 L"\\Registry\\Machine\\SYSTEM\\CurrentControlSet\\Control\\Terminal Server",
404 &hKey);
405 if (rc != ERROR_SUCCESS)
406 {
407 // Key doesn't exist; assume NT 4.0
408 return _WIN32_WINNT_NT4;
409 }
410
411 // We may here want to read the value of ProductVersion
412 return _WIN32_WINNT_WS03;
413 }
414
415
416 VOID
417 LoadAndBootWindows(PCSTR OperatingSystemName,
418 PSTR SettingsValue,
419 USHORT OperatingSystemVersion)
420 {
421 BOOLEAN HasSection;
422 char FullPath[MAX_PATH], SystemRoot[MAX_PATH], BootPath[MAX_PATH];
423 CHAR FileName[MAX_PATH];
424 CHAR BootOptions[256];
425 PCHAR File;
426 PCHAR PathSeparator;
427 PVOID NtosBase = NULL, HalBase = NULL, KdComBase = NULL;
428 BOOLEAN Status;
429 ULONG_PTR SectionId;
430 PLOADER_PARAMETER_BLOCK LoaderBlock, LoaderBlockVA;
431 KERNEL_ENTRY_POINT KiSystemStartup;
432 PLDR_DATA_TABLE_ENTRY KernelDTE, HalDTE, KdComDTE = NULL;
433 // Mm-related things
434 PVOID GdtIdt;
435 ULONG PcrBasePage=0;
436 ULONG TssBasePage=0;
437
438 // Open the operating system section
439 // specified in the .ini file
440 HasSection = IniOpenSection(OperatingSystemName, &SectionId);
441
442 UiDrawBackdrop();
443 UiDrawStatusText("Detecting Hardware...");
444 UiDrawProgressBarCenter(1, 100, "Loading NT...");
445
446 /* Read the system path is set in the .ini file */
447 if (!HasSection || !IniReadSettingByName(SectionId, "SystemPath", FullPath, sizeof(FullPath)))
448 {
449 strcpy(FullPath, OperatingSystemName);
450 }
451
452 /* Special case for LiveCD */
453 if (!_strnicmp(FullPath, "LiveCD", strlen("LiveCD")))
454 {
455 strcpy(BootPath, FullPath + strlen("LiveCD"));
456 MachDiskGetBootPath(FullPath, sizeof(FullPath));
457 strcat(FullPath, BootPath);
458 }
459
460 /* Convert FullPath to SystemRoot */
461 PathSeparator = strstr(FullPath, "\\");
462 strcpy(SystemRoot, PathSeparator);
463 strcat(SystemRoot, "\\");
464
465 /* Read booting options */
466 if (!HasSection || !IniReadSettingByName(SectionId, "Options", BootOptions, sizeof(BootOptions)))
467 {
468 /* Get options after the title */
469 const CHAR*p = SettingsValue;
470 while (*p == ' ' || *p == '"')
471 p++;
472 while (*p != '\0' && *p != '"')
473 p++;
474 strcpy(BootOptions, p);
475 DPRINTM(DPRINT_WINDOWS,"BootOptions: '%s'\n", BootOptions);
476 }
477
478 //
479 // Check if a ramdisk file was given
480 //
481 File = strstr(BootOptions, "/RDPATH=");
482 if (File)
483 {
484 //
485 // Copy the file name and everything else after it
486 //
487 strcpy(FileName, File + 8);
488
489 //
490 // Null-terminate
491 //
492 *strstr(FileName, " ") = ANSI_NULL;
493
494 //
495 // Load the ramdisk
496 //
497 RamDiskLoadVirtualFile(FileName);
498 }
499
500 /* Let user know we started loading */
501 UiDrawStatusText("Loading...");
502
503 /* append a backslash */
504 strcpy(BootPath, FullPath);
505 if ((strlen(BootPath)==0) ||
506 BootPath[strlen(BootPath)] != '\\')
507 strcat(BootPath, "\\");
508
509 DPRINTM(DPRINT_WINDOWS,"BootPath: '%s'\n", BootPath);
510
511 /* Allocate and minimalistic-initialize LPB */
512 AllocateAndInitLPB(&LoaderBlock);
513
514 /* Detect hardware */
515 UseRealHeap = TRUE;
516 LoaderBlock->ConfigurationRoot = MachHwDetect();
517
518 /* Load Hive */
519 Status = WinLdrInitSystemHive(LoaderBlock, BootPath);
520 DPRINTM(DPRINT_WINDOWS, "SYSTEM hive loaded with status %d\n", Status);
521
522 if (OperatingSystemVersion == 0)
523 OperatingSystemVersion = WinLdrDetectVersion();
524
525 /* Load kernel */
526 strcpy(FileName, BootPath);
527 strcat(FileName, "SYSTEM32\\NTOSKRNL.EXE");
528 Status = WinLdrLoadImage(FileName, LoaderSystemCode, &NtosBase);
529 DPRINTM(DPRINT_WINDOWS, "Ntos loaded with status %d at %p\n", Status, NtosBase);
530
531 /* Load HAL */
532 strcpy(FileName, BootPath);
533 strcat(FileName, "SYSTEM32\\HAL.DLL");
534 Status = WinLdrLoadImage(FileName, LoaderHalCode, &HalBase);
535 DPRINTM(DPRINT_WINDOWS, "HAL loaded with status %d at %p\n", Status, HalBase);
536
537 /* Load kernel-debugger support dll */
538 if (OperatingSystemVersion > _WIN32_WINNT_WIN2K)
539 {
540 strcpy(FileName, BootPath);
541 strcat(FileName, "SYSTEM32\\KDCOM.DLL");
542 Status = WinLdrLoadImage(FileName, LoaderBootDriver, &KdComBase);
543 DPRINTM(DPRINT_WINDOWS, "KdCom loaded with status %d at %p\n", Status, KdComBase);
544 }
545
546 /* Allocate data table entries for above-loaded modules */
547 WinLdrAllocateDataTableEntry(LoaderBlock, "ntoskrnl.exe",
548 "WINDOWS\\SYSTEM32\\NTOSKRNL.EXE", NtosBase, &KernelDTE);
549 WinLdrAllocateDataTableEntry(LoaderBlock, "hal.dll",
550 "WINDOWS\\SYSTEM32\\HAL.DLL", HalBase, &HalDTE);
551 if (OperatingSystemVersion > _WIN32_WINNT_WIN2K)
552 {
553 WinLdrAllocateDataTableEntry(LoaderBlock, "kdcom.dll",
554 "WINDOWS\\SYSTEM32\\KDCOM.DLL", KdComBase, &KdComDTE);
555 }
556
557 /* Load all referenced DLLs for kernel, HAL and kdcom.dll */
558 strcpy(FileName, BootPath);
559 strcat(FileName, "SYSTEM32\\");
560 WinLdrScanImportDescriptorTable(LoaderBlock, FileName, KernelDTE);
561 WinLdrScanImportDescriptorTable(LoaderBlock, FileName, HalDTE);
562 if (KdComDTE)
563 WinLdrScanImportDescriptorTable(LoaderBlock, FileName, KdComDTE);
564
565 /* Load NLS data, OEM font, and prepare boot drivers list */
566 Status = WinLdrScanSystemHive(LoaderBlock, BootPath);
567 DPRINTM(DPRINT_WINDOWS, "SYSTEM hive scanned with status %d\n", Status);
568
569 /* Load boot drivers */
570 Status = WinLdrLoadBootDrivers(LoaderBlock, BootPath);
571 DPRINTM(DPRINT_WINDOWS, "Boot drivers loaded with status %d\n", Status);
572
573 /* Alloc PCR, TSS, do magic things with the GDT/IDT */
574 WinLdrSetupForNt(LoaderBlock, &GdtIdt, &PcrBasePage, &TssBasePage);
575
576 /* Initialize Phase 1 - no drivers loading anymore */
577 WinLdrInitializePhase1(LoaderBlock, BootOptions, SystemRoot, BootPath, OperatingSystemVersion);
578
579 /* Save entry-point pointer and Loader block VAs */
580 KiSystemStartup = (KERNEL_ENTRY_POINT)KernelDTE->EntryPoint;
581 LoaderBlockVA = PaToVa(LoaderBlock);
582
583 /* "Stop all motors", change videomode */
584 if (OperatingSystemVersion < _WIN32_WINNT_WIN2K)
585 MachPrepareForReactOS(TRUE);
586 else
587 MachPrepareForReactOS(FALSE);
588
589 /* Debugging... */
590 //DumpMemoryAllocMap();
591
592 /* Turn on paging mode of CPU*/
593 WinLdrTurnOnPaging(LoaderBlock, PcrBasePage, TssBasePage, GdtIdt);
594
595 /* Save final value of LoaderPagesSpanned */
596 LoaderBlockVA->Extension->LoaderPagesSpanned = LoaderPagesSpanned;
597
598 DPRINTM(DPRINT_WINDOWS, "Hello from paged mode, KiSystemStartup %p, LoaderBlockVA %p!\n",
599 KiSystemStartup, LoaderBlockVA);
600
601 WinLdrpDumpMemoryDescriptors(LoaderBlockVA);
602 WinLdrpDumpBootDriver(LoaderBlockVA);
603 WinLdrpDumpArcDisks(LoaderBlockVA);
604
605 //FIXME: If I substitute this debugging checkpoint, GCC will "optimize away" the code below
606 //while (1) {};
607 /*asm(".intel_syntax noprefix\n");
608 asm("test1:\n");
609 asm("jmp test1\n");
610 asm(".att_syntax\n");*/
611
612 /* Pass control */
613 (*KiSystemStartup)(LoaderBlockVA);
614
615 return;
616 }
617
618 VOID
619 WinLdrpDumpMemoryDescriptors(PLOADER_PARAMETER_BLOCK LoaderBlock)
620 {
621 PLIST_ENTRY NextMd;
622 PMEMORY_ALLOCATION_DESCRIPTOR MemoryDescriptor;
623
624 NextMd = LoaderBlock->MemoryDescriptorListHead.Flink;
625
626 while (NextMd != &LoaderBlock->MemoryDescriptorListHead)
627 {
628 MemoryDescriptor = CONTAINING_RECORD(NextMd, MEMORY_ALLOCATION_DESCRIPTOR, ListEntry);
629
630 DPRINTM(DPRINT_WINDOWS, "BP %08X PC %04X MT %d\n", MemoryDescriptor->BasePage,
631 MemoryDescriptor->PageCount, MemoryDescriptor->MemoryType);
632
633 NextMd = MemoryDescriptor->ListEntry.Flink;
634 }
635 }
636
637 VOID
638 WinLdrpDumpBootDriver(PLOADER_PARAMETER_BLOCK LoaderBlock)
639 {
640 PLIST_ENTRY NextBd;
641 PBOOT_DRIVER_LIST_ENTRY BootDriver;
642
643 NextBd = LoaderBlock->BootDriverListHead.Flink;
644
645 while (NextBd != &LoaderBlock->BootDriverListHead)
646 {
647 BootDriver = CONTAINING_RECORD(NextBd, BOOT_DRIVER_LIST_ENTRY, Link);
648
649 DPRINTM(DPRINT_WINDOWS, "BootDriver %wZ DTE %08X RegPath: %wZ\n", &BootDriver->FilePath,
650 BootDriver->LdrEntry, &BootDriver->RegistryPath);
651
652 NextBd = BootDriver->Link.Flink;
653 }
654 }
655
656 VOID
657 WinLdrpDumpArcDisks(PLOADER_PARAMETER_BLOCK LoaderBlock)
658 {
659 PLIST_ENTRY NextBd;
660 PARC_DISK_SIGNATURE ArcDisk;
661
662 NextBd = LoaderBlock->ArcDiskInformation->DiskSignatureListHead.Flink;
663
664 while (NextBd != &LoaderBlock->ArcDiskInformation->DiskSignatureListHead)
665 {
666 ArcDisk = CONTAINING_RECORD(NextBd, ARC_DISK_SIGNATURE, ListEntry);
667
668 DPRINTM(DPRINT_WINDOWS, "ArcDisk %s checksum: 0x%X, signature: 0x%X\n",
669 ArcDisk->ArcName, ArcDisk->CheckSum, ArcDisk->Signature);
670
671 NextBd = ArcDisk->ListEntry.Flink;
672 }
673 }
674
675