Fix compilation of freeldr. (a number of ULONG / ULONG_PTR issues, remove LoadReactOS...
[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 else
281 {
282 // There is no directory in the path
283 strcpy(DllName, DriverPath);
284 DriverPath[0] = 0;
285 }
286
287 DPRINTM(DPRINT_WINDOWS, "DriverPath: %s, DllName: %s, LPB %p\n", DriverPath, DllName, LoaderBlock);
288
289
290 // Check if driver is already loaded
291 Status = WinLdrCheckForLoadedDll(LoaderBlock, DllName, DriverDTE);
292 if (Status)
293 {
294 // We've got the pointer to its DTE, just return success
295 return TRUE;
296 }
297
298 // It's not loaded, we have to load it
299 sprintf(FullPath,"%s%S", BootPath, FilePath->Buffer);
300 Status = WinLdrLoadImage(FullPath, LoaderBootDriver, &DriverBase);
301 if (!Status)
302 return FALSE;
303
304 // Allocate a DTE for it
305 Status = WinLdrAllocateDataTableEntry(LoaderBlock, DllName, DllName, DriverBase, DriverDTE);
306 if (!Status)
307 {
308 DPRINTM(DPRINT_WINDOWS, "WinLdrAllocateDataTableEntry() failed\n");
309 return FALSE;
310 }
311
312 // Modify any flags, if needed
313 (*DriverDTE)->Flags |= Flags;
314
315 // Look for any dependencies it may have, and load them too
316 sprintf(FullPath,"%s%s", BootPath, DriverPath);
317 Status = WinLdrScanImportDescriptorTable(LoaderBlock, FullPath, *DriverDTE);
318 if (!Status)
319 {
320 DPRINTM(DPRINT_WINDOWS, "WinLdrScanImportDescriptorTable() failed for %s\n",
321 FullPath);
322 return FALSE;
323 }
324
325 return TRUE;
326 }
327
328 BOOLEAN
329 WinLdrLoadBootDrivers(PLOADER_PARAMETER_BLOCK LoaderBlock,
330 LPSTR BootPath)
331 {
332 PLIST_ENTRY NextBd;
333 PBOOT_DRIVER_LIST_ENTRY BootDriver;
334 BOOLEAN Status;
335
336 // Walk through the boot drivers list
337 NextBd = LoaderBlock->BootDriverListHead.Flink;
338
339 while (NextBd != &LoaderBlock->BootDriverListHead)
340 {
341 BootDriver = CONTAINING_RECORD(NextBd, BOOT_DRIVER_LIST_ENTRY, Link);
342
343 DPRINTM(DPRINT_WINDOWS, "BootDriver %wZ DTE %08X RegPath: %wZ\n", &BootDriver->FilePath,
344 BootDriver->LdrEntry, &BootDriver->RegistryPath);
345
346 // Paths are relative (FIXME: Are they always relative?)
347
348 // Load it
349 Status = WinLdrLoadDeviceDriver(LoaderBlock, BootPath, &BootDriver->FilePath,
350 0, &BootDriver->LdrEntry);
351
352 // If loading failed - cry loudly
353 //FIXME: Maybe remove it from the list and try to continue?
354 if (!Status)
355 {
356 UiMessageBox("Can't load boot driver!");
357 return FALSE;
358 }
359
360 // Convert the RegistryPath and DTE addresses to VA since we are not going to use it anymore
361 BootDriver->RegistryPath.Buffer = PaToVa(BootDriver->RegistryPath.Buffer);
362 BootDriver->LdrEntry = PaToVa(BootDriver->LdrEntry);
363
364 NextBd = BootDriver->Link.Flink;
365 }
366
367 return TRUE;
368 }
369
370 PVOID WinLdrLoadModule(PCSTR ModuleName, ULONG *Size,
371 TYPE_OF_MEMORY MemoryType)
372 {
373 PFILE FileHandle;
374 PVOID PhysicalBase;
375 ULONG FileSize;
376 BOOLEAN Status;
377
378 //CHAR ProgressString[256];
379
380 /* Inform user we are loading files */
381 //sprintf(ProgressString, "Loading %s...", FileName);
382 //UiDrawProgressBarCenter(1, 100, ProgressString);
383
384 DPRINTM(DPRINT_WINDOWS, "Loading module %s\n", ModuleName);
385 *Size = 0;
386
387 /* Open the image file */
388 FileHandle = FsOpenFile(ModuleName);
389
390 if (FileHandle == NULL)
391 {
392 /* In case of errors, we just return, without complaining to the user */
393 return NULL;
394 }
395
396 /* Get this file's size */
397 FileSize = FsGetFileSize(FileHandle);
398 *Size = FileSize;
399
400 /* Allocate memory */
401 PhysicalBase = MmAllocateMemoryWithType(FileSize, MemoryType);
402 if (PhysicalBase == NULL)
403 {
404 FsCloseFile(FileHandle);
405 return NULL;
406 }
407
408 /* Load whole file */
409 Status = FsReadFile(FileHandle, FileSize, NULL, PhysicalBase);
410 if (!Status)
411 {
412 FsCloseFile(FileHandle);
413 return NULL;
414 }
415
416 DPRINTM(DPRINT_WINDOWS, "Loaded %s at 0x%x with size 0x%x\n", ModuleName, PhysicalBase, FileSize);
417
418 /* We are done with the file - close it */
419 FsCloseFile(FileHandle);
420
421 return PhysicalBase;
422 }
423
424
425 VOID
426 LoadAndBootWindows(PCSTR OperatingSystemName, USHORT OperatingSystemVersion)
427 {
428 CHAR MsgBuffer[256];
429 CHAR SystemPath[512], SearchPath[512];
430 CHAR FileName[512];
431 CHAR BootPath[512];
432 CHAR BootOptions[256];
433 PVOID NtosBase = NULL, HalBase = NULL, KdComBase = NULL;
434 BOOLEAN Status;
435 ULONG_PTR SectionId;
436 ULONG BootDevice;
437 PLOADER_PARAMETER_BLOCK LoaderBlock, LoaderBlockVA;
438 KERNEL_ENTRY_POINT KiSystemStartup;
439 PLDR_DATA_TABLE_ENTRY KernelDTE, HalDTE, KdComDTE = NULL;
440 // Mm-related things
441 PVOID GdtIdt;
442 ULONG PcrBasePage=0;
443 ULONG TssBasePage=0;
444
445 //sprintf(MsgBuffer,"Booting Microsoft(R) Windows(R) OS version '%04x' is not implemented yet", OperatingSystemVersion);
446 //UiMessageBox(MsgBuffer);
447
448 // Open the operating system section
449 // specified in the .ini file
450 if (!IniOpenSection(OperatingSystemName, &SectionId))
451 {
452 sprintf(MsgBuffer,"Operating System section '%s' not found in freeldr.ini", OperatingSystemName);
453 UiMessageBox(MsgBuffer);
454 return;
455 }
456
457 UiDrawBackdrop();
458 UiDrawStatusText("Detecting Hardware...");
459 UiDrawProgressBarCenter(1, 100, "Loading Windows...");
460
461 /* Make sure the system path is set in the .ini file */
462 if (!IniReadSettingByName(SectionId, "SystemPath", SystemPath, sizeof(SystemPath)))
463 {
464 UiMessageBox("System path not specified for selected operating system.");
465 return;
466 }
467
468 /* Read booting options */
469 if (!IniReadSettingByName(SectionId, "Options", BootOptions, sizeof(BootOptions)))
470 {
471 /* Nothing read, make the string empty */
472 strcpy(BootOptions, "");
473 }
474
475 /* Normalize system path */
476 if (!MachDiskNormalizeSystemPath(SystemPath, sizeof(SystemPath)))
477 {
478 UiMessageBox("Invalid system path");
479 return;
480 }
481
482 /* Let user know we started loading */
483 UiDrawStatusText("Loading...");
484
485 /* Try to open system drive */
486 BootDevice = 0xffffffff;
487 if (!FsOpenSystemVolume(SystemPath, BootPath, &BootDevice))
488 {
489 UiMessageBox("Failed to open boot drive.");
490 return;
491 }
492
493 /* append a backslash */
494 if ((strlen(BootPath)==0) ||
495 BootPath[strlen(BootPath)] != '\\')
496 strcat(BootPath, "\\");
497
498 DPRINTM(DPRINT_WINDOWS,"SystemRoot: '%s'\n", BootPath);
499
500 /* Allocate and minimalistic-initialize LPB */
501 AllocateAndInitLPB(&LoaderBlock);
502
503 /* Detect hardware */
504 UseRealHeap = TRUE;
505 LoaderBlock->ConfigurationRoot = MachHwDetect();
506
507 /* Load kernel */
508 strcpy(FileName, BootPath);
509 strcat(FileName, "SYSTEM32\\NTOSKRNL.EXE");
510 Status = WinLdrLoadImage(FileName, LoaderSystemCode, &NtosBase);
511 DPRINTM(DPRINT_WINDOWS, "Ntos loaded with status %d at %p\n", Status, NtosBase);
512
513 /* Load HAL */
514 strcpy(FileName, BootPath);
515 strcat(FileName, "SYSTEM32\\HAL.DLL");
516 Status = WinLdrLoadImage(FileName, LoaderHalCode, &HalBase);
517 DPRINTM(DPRINT_WINDOWS, "HAL loaded with status %d at %p\n", Status, HalBase);
518
519 /* Load kernel-debugger support dll */
520 if (OperatingSystemVersion > _WIN32_WINNT_WIN2K)
521 {
522 strcpy(FileName, BootPath);
523 strcat(FileName, "SYSTEM32\\KDCOM.DLL");
524 Status = WinLdrLoadImage(FileName, LoaderBootDriver, &KdComBase);
525 DPRINTM(DPRINT_WINDOWS, "KdCom loaded with status %d at %p\n", Status, KdComBase);
526 }
527
528 /* Allocate data table entries for above-loaded modules */
529 WinLdrAllocateDataTableEntry(LoaderBlock, "ntoskrnl.exe",
530 "WINDOWS\\SYSTEM32\\NTOSKRNL.EXE", NtosBase, &KernelDTE);
531 WinLdrAllocateDataTableEntry(LoaderBlock, "hal.dll",
532 "WINDOWS\\SYSTEM32\\HAL.DLL", HalBase, &HalDTE);
533 if (OperatingSystemVersion > _WIN32_WINNT_WIN2K)
534 {
535 WinLdrAllocateDataTableEntry(LoaderBlock, "kdcom.dll",
536 "WINDOWS\\SYSTEM32\\KDCOM.DLL", KdComBase, &KdComDTE);
537 }
538
539 /* Load all referenced DLLs for kernel, HAL and kdcom.dll */
540 strcpy(SearchPath, BootPath);
541 strcat(SearchPath, "SYSTEM32\\");
542 WinLdrScanImportDescriptorTable(LoaderBlock, SearchPath, KernelDTE);
543 WinLdrScanImportDescriptorTable(LoaderBlock, SearchPath, HalDTE);
544 if (KdComDTE)
545 WinLdrScanImportDescriptorTable(LoaderBlock, SearchPath, KdComDTE);
546
547 /* Load Hive, and then NLS data, OEM font, and prepare boot drivers list */
548 Status = WinLdrLoadAndScanSystemHive(LoaderBlock, BootPath);
549 DPRINTM(DPRINT_WINDOWS, "SYSTEM hive loaded and scanned with status %d\n", Status);
550
551 /* Load boot drivers */
552 Status = WinLdrLoadBootDrivers(LoaderBlock, BootPath);
553 DPRINTM(DPRINT_WINDOWS, "Boot drivers loaded with status %d\n", Status);
554
555 /* Alloc PCR, TSS, do magic things with the GDT/IDT */
556 WinLdrSetupForNt(LoaderBlock, &GdtIdt, &PcrBasePage, &TssBasePage);
557
558 /* Initialize Phase 1 - no drivers loading anymore */
559 WinLdrInitializePhase1(LoaderBlock, BootOptions, SystemPath, BootPath, OperatingSystemVersion);
560
561 /* Save entry-point pointer and Loader block VAs */
562 KiSystemStartup = (KERNEL_ENTRY_POINT)KernelDTE->EntryPoint;
563 LoaderBlockVA = PaToVa(LoaderBlock);
564
565 /* "Stop all motors", change videomode */
566 if (OperatingSystemVersion < _WIN32_WINNT_WIN2K)
567 MachPrepareForReactOS(TRUE);
568 else
569 MachPrepareForReactOS(FALSE);
570
571 /* Debugging... */
572 //DumpMemoryAllocMap();
573
574 /* Turn on paging mode of CPU*/
575 WinLdrTurnOnPaging(LoaderBlock, PcrBasePage, TssBasePage, GdtIdt);
576
577 /* Save final value of LoaderPagesSpanned */
578 LoaderBlock->Extension->LoaderPagesSpanned = LoaderPagesSpanned;
579
580 DPRINTM(DPRINT_WINDOWS, "Hello from paged mode, KiSystemStartup %p, LoaderBlockVA %p!\n",
581 KiSystemStartup, LoaderBlockVA);
582
583 WinLdrpDumpMemoryDescriptors(LoaderBlockVA);
584 WinLdrpDumpBootDriver(LoaderBlockVA);
585 WinLdrpDumpArcDisks(LoaderBlockVA);
586
587 //FIXME: If I substitute this debugging checkpoint, GCC will "optimize away" the code below
588 //while (1) {};
589 /*asm(".intel_syntax noprefix\n");
590 asm("test1:\n");
591 asm("jmp test1\n");
592 asm(".att_syntax\n");*/
593
594 /* Pass control */
595 (*KiSystemStartup)(LoaderBlockVA);
596
597 return;
598 }
599
600 VOID
601 WinLdrpDumpMemoryDescriptors(PLOADER_PARAMETER_BLOCK LoaderBlock)
602 {
603 PLIST_ENTRY NextMd;
604 PMEMORY_ALLOCATION_DESCRIPTOR MemoryDescriptor;
605
606 NextMd = LoaderBlock->MemoryDescriptorListHead.Flink;
607
608 while (NextMd != &LoaderBlock->MemoryDescriptorListHead)
609 {
610 MemoryDescriptor = CONTAINING_RECORD(NextMd, MEMORY_ALLOCATION_DESCRIPTOR, ListEntry);
611
612 DPRINTM(DPRINT_WINDOWS, "BP %08X PC %04X MT %d\n", MemoryDescriptor->BasePage,
613 MemoryDescriptor->PageCount, MemoryDescriptor->MemoryType);
614
615 NextMd = MemoryDescriptor->ListEntry.Flink;
616 }
617 }
618
619 VOID
620 WinLdrpDumpBootDriver(PLOADER_PARAMETER_BLOCK LoaderBlock)
621 {
622 PLIST_ENTRY NextBd;
623 PBOOT_DRIVER_LIST_ENTRY BootDriver;
624
625 NextBd = LoaderBlock->BootDriverListHead.Flink;
626
627 while (NextBd != &LoaderBlock->BootDriverListHead)
628 {
629 BootDriver = CONTAINING_RECORD(NextBd, BOOT_DRIVER_LIST_ENTRY, Link);
630
631 DPRINTM(DPRINT_WINDOWS, "BootDriver %wZ DTE %08X RegPath: %wZ\n", &BootDriver->FilePath,
632 BootDriver->LdrEntry, &BootDriver->RegistryPath);
633
634 NextBd = BootDriver->Link.Flink;
635 }
636 }
637
638 VOID
639 WinLdrpDumpArcDisks(PLOADER_PARAMETER_BLOCK LoaderBlock)
640 {
641 PLIST_ENTRY NextBd;
642 PARC_DISK_SIGNATURE ArcDisk;
643
644 NextBd = LoaderBlock->ArcDiskInformation->DiskSignatureListHead.Flink;
645
646 while (NextBd != &LoaderBlock->ArcDiskInformation->DiskSignatureListHead)
647 {
648 ArcDisk = CONTAINING_RECORD(NextBd, ARC_DISK_SIGNATURE, ListEntry);
649
650 DPRINTM(DPRINT_WINDOWS, "ArcDisk %s checksum: 0x%X, signature: 0x%X\n",
651 ArcDisk->ArcName, ArcDisk->CheckSum, ArcDisk->Signature);
652
653 NextBd = ArcDisk->ListEntry.Flink;
654 }
655 }
656
657