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