Sync trunk head (r41026)
[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 FullPath[MAX_PATH], SystemRoot[MAX_PATH], BootPath[MAX_PATH];
435 CHAR FileName[MAX_PATH];
436 CHAR BootOptions[256];
437 PCHAR PathSeparator;
438 PVOID NtosBase = NULL, HalBase = NULL, KdComBase = NULL;
439 BOOLEAN Status;
440 ULONG_PTR 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", FullPath, sizeof(FullPath)))
467 {
468 UiMessageBox("System path not specified for selected operating system.");
469 return;
470 }
471
472 /* Special case for LiveCD */
473 if (!_strnicmp(FullPath, "LiveCD", strlen("LiveCD")))
474 {
475 strcpy(BootPath, FullPath + strlen("LiveCD"));
476 MachDiskGetBootPath(FullPath, sizeof(FullPath));
477 strcat(FullPath, BootPath);
478 }
479
480 /* Convert FullPath to SystemRoot */
481 PathSeparator = strstr(FullPath, "\\");
482 strcpy(SystemRoot, PathSeparator);
483 strcat(SystemRoot, "\\");
484
485 /* Read booting options */
486 if (!IniReadSettingByName(SectionId, "Options", BootOptions, sizeof(BootOptions)))
487 {
488 /* Nothing read, make the string empty */
489 strcpy(BootOptions, "");
490 }
491
492 /* Let user know we started loading */
493 UiDrawStatusText("Loading...");
494
495 /* append a backslash */
496 strcpy(BootPath, FullPath);
497 if ((strlen(BootPath)==0) ||
498 BootPath[strlen(BootPath)] != '\\')
499 strcat(BootPath, "\\");
500
501 DPRINTM(DPRINT_WINDOWS,"BootPath: '%s'\n", BootPath);
502
503 /* Allocate and minimalistic-initialize LPB */
504 AllocateAndInitLPB(&LoaderBlock);
505
506 /* Detect hardware */
507 UseRealHeap = TRUE;
508 LoaderBlock->ConfigurationRoot = MachHwDetect();
509
510 /* Load kernel */
511 strcpy(FileName, BootPath);
512 strcat(FileName, "SYSTEM32\\NTOSKRNL.EXE");
513 Status = WinLdrLoadImage(FileName, LoaderSystemCode, &NtosBase);
514 DPRINTM(DPRINT_WINDOWS, "Ntos loaded with status %d at %p\n", Status, NtosBase);
515
516 /* Load HAL */
517 strcpy(FileName, BootPath);
518 strcat(FileName, "SYSTEM32\\HAL.DLL");
519 Status = WinLdrLoadImage(FileName, LoaderHalCode, &HalBase);
520 DPRINTM(DPRINT_WINDOWS, "HAL loaded with status %d at %p\n", Status, HalBase);
521
522 /* Load kernel-debugger support dll */
523 if (OperatingSystemVersion > _WIN32_WINNT_WIN2K)
524 {
525 strcpy(FileName, BootPath);
526 strcat(FileName, "SYSTEM32\\KDCOM.DLL");
527 Status = WinLdrLoadImage(FileName, LoaderBootDriver, &KdComBase);
528 DPRINTM(DPRINT_WINDOWS, "KdCom loaded with status %d at %p\n", Status, KdComBase);
529 }
530
531 /* Allocate data table entries for above-loaded modules */
532 WinLdrAllocateDataTableEntry(LoaderBlock, "ntoskrnl.exe",
533 "WINDOWS\\SYSTEM32\\NTOSKRNL.EXE", NtosBase, &KernelDTE);
534 WinLdrAllocateDataTableEntry(LoaderBlock, "hal.dll",
535 "WINDOWS\\SYSTEM32\\HAL.DLL", HalBase, &HalDTE);
536 if (OperatingSystemVersion > _WIN32_WINNT_WIN2K)
537 {
538 WinLdrAllocateDataTableEntry(LoaderBlock, "kdcom.dll",
539 "WINDOWS\\SYSTEM32\\KDCOM.DLL", KdComBase, &KdComDTE);
540 }
541
542 /* Load all referenced DLLs for kernel, HAL and kdcom.dll */
543 strcpy(FileName, BootPath);
544 strcat(FileName, "SYSTEM32\\");
545 WinLdrScanImportDescriptorTable(LoaderBlock, FileName, KernelDTE);
546 WinLdrScanImportDescriptorTable(LoaderBlock, FileName, HalDTE);
547 if (KdComDTE)
548 WinLdrScanImportDescriptorTable(LoaderBlock, FileName, KdComDTE);
549
550 /* Load Hive, and then NLS data, OEM font, and prepare boot drivers list */
551 Status = WinLdrLoadAndScanSystemHive(LoaderBlock, BootPath);
552 DPRINTM(DPRINT_WINDOWS, "SYSTEM hive loaded and scanned with status %d\n", Status);
553
554 /* Load boot drivers */
555 Status = WinLdrLoadBootDrivers(LoaderBlock, BootPath);
556 DPRINTM(DPRINT_WINDOWS, "Boot drivers loaded with status %d\n", Status);
557
558 /* Alloc PCR, TSS, do magic things with the GDT/IDT */
559 WinLdrSetupForNt(LoaderBlock, &GdtIdt, &PcrBasePage, &TssBasePage);
560
561 /* Initialize Phase 1 - no drivers loading anymore */
562 WinLdrInitializePhase1(LoaderBlock, BootOptions, SystemRoot, BootPath, OperatingSystemVersion);
563
564 /* Save entry-point pointer and Loader block VAs */
565 KiSystemStartup = (KERNEL_ENTRY_POINT)KernelDTE->EntryPoint;
566 LoaderBlockVA = PaToVa(LoaderBlock);
567
568 /* "Stop all motors", change videomode */
569 if (OperatingSystemVersion < _WIN32_WINNT_WIN2K)
570 MachPrepareForReactOS(TRUE);
571 else
572 MachPrepareForReactOS(FALSE);
573
574 /* Debugging... */
575 //DumpMemoryAllocMap();
576
577 /* Turn on paging mode of CPU*/
578 WinLdrTurnOnPaging(LoaderBlock, PcrBasePage, TssBasePage, GdtIdt);
579
580 /* Save final value of LoaderPagesSpanned */
581 LoaderBlock->Extension->LoaderPagesSpanned = LoaderPagesSpanned;
582
583 DPRINTM(DPRINT_WINDOWS, "Hello from paged mode, KiSystemStartup %p, LoaderBlockVA %p!\n",
584 KiSystemStartup, LoaderBlockVA);
585
586 WinLdrpDumpMemoryDescriptors(LoaderBlockVA);
587 WinLdrpDumpBootDriver(LoaderBlockVA);
588 WinLdrpDumpArcDisks(LoaderBlockVA);
589
590 //FIXME: If I substitute this debugging checkpoint, GCC will "optimize away" the code below
591 //while (1) {};
592 /*asm(".intel_syntax noprefix\n");
593 asm("test1:\n");
594 asm("jmp test1\n");
595 asm(".att_syntax\n");*/
596
597 /* Pass control */
598 (*KiSystemStartup)(LoaderBlockVA);
599
600 return;
601 }
602
603 VOID
604 WinLdrpDumpMemoryDescriptors(PLOADER_PARAMETER_BLOCK LoaderBlock)
605 {
606 PLIST_ENTRY NextMd;
607 PMEMORY_ALLOCATION_DESCRIPTOR MemoryDescriptor;
608
609 NextMd = LoaderBlock->MemoryDescriptorListHead.Flink;
610
611 while (NextMd != &LoaderBlock->MemoryDescriptorListHead)
612 {
613 MemoryDescriptor = CONTAINING_RECORD(NextMd, MEMORY_ALLOCATION_DESCRIPTOR, ListEntry);
614
615 DPRINTM(DPRINT_WINDOWS, "BP %08X PC %04X MT %d\n", MemoryDescriptor->BasePage,
616 MemoryDescriptor->PageCount, MemoryDescriptor->MemoryType);
617
618 NextMd = MemoryDescriptor->ListEntry.Flink;
619 }
620 }
621
622 VOID
623 WinLdrpDumpBootDriver(PLOADER_PARAMETER_BLOCK LoaderBlock)
624 {
625 PLIST_ENTRY NextBd;
626 PBOOT_DRIVER_LIST_ENTRY BootDriver;
627
628 NextBd = LoaderBlock->BootDriverListHead.Flink;
629
630 while (NextBd != &LoaderBlock->BootDriverListHead)
631 {
632 BootDriver = CONTAINING_RECORD(NextBd, BOOT_DRIVER_LIST_ENTRY, Link);
633
634 DPRINTM(DPRINT_WINDOWS, "BootDriver %wZ DTE %08X RegPath: %wZ\n", &BootDriver->FilePath,
635 BootDriver->LdrEntry, &BootDriver->RegistryPath);
636
637 NextBd = BootDriver->Link.Flink;
638 }
639 }
640
641 VOID
642 WinLdrpDumpArcDisks(PLOADER_PARAMETER_BLOCK LoaderBlock)
643 {
644 PLIST_ENTRY NextBd;
645 PARC_DISK_SIGNATURE ArcDisk;
646
647 NextBd = LoaderBlock->ArcDiskInformation->DiskSignatureListHead.Flink;
648
649 while (NextBd != &LoaderBlock->ArcDiskInformation->DiskSignatureListHead)
650 {
651 ArcDisk = CONTAINING_RECORD(NextBd, ARC_DISK_SIGNATURE, ListEntry);
652
653 DPRINTM(DPRINT_WINDOWS, "ArcDisk %s checksum: 0x%X, signature: 0x%X\n",
654 ArcDisk->ArcName, ArcDisk->CheckSum, ArcDisk->Signature);
655
656 NextBd = ArcDisk->ListEntry.Flink;
657 }
658 }
659
660