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