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