[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 extern void WinLdrSetupEms(IN PCHAR BootOptions);
45
46 BOOLEAN
47 WinLdrCheckForLoadedDll(IN OUT PLOADER_PARAMETER_BLOCK WinLdrBlock,
48 IN PCH DllName,
49 OUT PLDR_DATA_TABLE_ENTRY *LoadedEntry);
50
51 // debug stuff
52 VOID DumpMemoryAllocMap(VOID);
53 VOID WinLdrpDumpMemoryDescriptors(PLOADER_PARAMETER_BLOCK LoaderBlock);
54 VOID WinLdrpDumpBootDriver(PLOADER_PARAMETER_BLOCK LoaderBlock);
55 VOID WinLdrpDumpArcDisks(PLOADER_PARAMETER_BLOCK LoaderBlock);
56
57
58 // Init "phase 0"
59 VOID
60 AllocateAndInitLPB(PLOADER_PARAMETER_BLOCK *OutLoaderBlock)
61 {
62 PLOADER_PARAMETER_BLOCK LoaderBlock;
63
64 /* Allocate and zero-init the LPB */
65 LoaderBlock = MmHeapAlloc(sizeof(LOADER_PARAMETER_BLOCK));
66 RtlZeroMemory(LoaderBlock, sizeof(LOADER_PARAMETER_BLOCK));
67
68 /* Init three critical lists, used right away */
69 InitializeListHead(&LoaderBlock->LoadOrderListHead);
70 InitializeListHead(&LoaderBlock->MemoryDescriptorListHead);
71 InitializeListHead(&LoaderBlock->BootDriverListHead);
72
73 /* Alloc space for NLS (it will be converted to VA in WinLdrLoadNLS) */
74 LoaderBlock->NlsData = MmHeapAlloc(sizeof(NLS_DATA_BLOCK));
75 if (LoaderBlock->NlsData == NULL)
76 {
77 UiMessageBox("Failed to allocate memory for NLS table data!");
78 return;
79 }
80 RtlZeroMemory(LoaderBlock->NlsData, sizeof(NLS_DATA_BLOCK));
81
82 *OutLoaderBlock = LoaderBlock;
83 }
84
85 // Init "phase 1"
86 VOID
87 WinLdrInitializePhase1(PLOADER_PARAMETER_BLOCK LoaderBlock,
88 PCHAR Options,
89 PCHAR SystemRoot,
90 PCHAR BootPath,
91 USHORT VersionToBoot)
92 {
93 /* Examples of correct options and paths */
94 //CHAR Options[] = "/DEBUGPORT=COM1 /BAUDRATE=115200";
95 //CHAR Options[] = "/NODEBUG";
96 //CHAR SystemRoot[] = "\\WINNT\\";
97 //CHAR ArcBoot[] = "multi(0)disk(0)rdisk(0)partition(1)";
98
99 CHAR HalPath[] = "\\";
100 CHAR ArcBoot[256];
101 CHAR MiscFiles[256];
102 ULONG i, PathSeparator;
103 PLOADER_PARAMETER_EXTENSION Extension;
104
105 /* Construct SystemRoot and ArcBoot from SystemPath */
106 PathSeparator = strstr(BootPath, "\\") - BootPath;
107 strncpy(ArcBoot, BootPath, PathSeparator);
108 ArcBoot[PathSeparator] = 0;
109
110 DPRINTM(DPRINT_WINDOWS, "ArcBoot: %s\n", ArcBoot);
111 DPRINTM(DPRINT_WINDOWS, "SystemRoot: %s\n", SystemRoot);
112 DPRINTM(DPRINT_WINDOWS, "Options: %s\n", Options);
113
114 /* Fill Arc BootDevice */
115 LoaderBlock->ArcBootDeviceName = MmHeapAlloc(strlen(ArcBoot)+1);
116 strcpy(LoaderBlock->ArcBootDeviceName, ArcBoot);
117 LoaderBlock->ArcBootDeviceName = PaToVa(LoaderBlock->ArcBootDeviceName);
118
119 /* Fill Arc HalDevice, it matches ArcBoot path */
120 LoaderBlock->ArcHalDeviceName = MmHeapAlloc(strlen(ArcBoot)+1);
121 strcpy(LoaderBlock->ArcHalDeviceName, ArcBoot);
122 LoaderBlock->ArcHalDeviceName = PaToVa(LoaderBlock->ArcHalDeviceName);
123
124 /* Fill SystemRoot */
125 LoaderBlock->NtBootPathName = MmHeapAlloc(strlen(SystemRoot)+1);
126 strcpy(LoaderBlock->NtBootPathName, SystemRoot);
127 LoaderBlock->NtBootPathName = PaToVa(LoaderBlock->NtBootPathName);
128
129 /* Fill NtHalPathName */
130 LoaderBlock->NtHalPathName = MmHeapAlloc(strlen(HalPath)+1);
131 strcpy(LoaderBlock->NtHalPathName, HalPath);
132 LoaderBlock->NtHalPathName = PaToVa(LoaderBlock->NtHalPathName);
133
134 /* Fill load options */
135 LoaderBlock->LoadOptions = MmHeapAlloc(strlen(Options)+1);
136 strcpy(LoaderBlock->LoadOptions, Options);
137 LoaderBlock->LoadOptions = PaToVa(LoaderBlock->LoadOptions);
138
139 /* Arc devices */
140 LoaderBlock->ArcDiskInformation = (PARC_DISK_INFORMATION)MmHeapAlloc(sizeof(ARC_DISK_INFORMATION));
141 InitializeListHead(&LoaderBlock->ArcDiskInformation->DiskSignatureListHead);
142
143 /* Convert ARC disk information from freeldr to a correct format */
144 for (i = 0; i < reactos_disk_count; i++)
145 {
146 PARC_DISK_SIGNATURE ArcDiskInfo;
147
148 /* Get the ARC structure */
149 ArcDiskInfo = (PARC_DISK_SIGNATURE)MmHeapAlloc(sizeof(ARC_DISK_SIGNATURE));
150 RtlZeroMemory(ArcDiskInfo, sizeof(ARC_DISK_SIGNATURE));
151
152 /* Copy the data over */
153 ArcDiskInfo->Signature = reactos_arc_disk_info[i].Signature;
154 ArcDiskInfo->CheckSum = reactos_arc_disk_info[i].CheckSum;
155
156 /* Copy the ARC Name */
157 ArcDiskInfo->ArcName = (PCHAR)MmHeapAlloc(sizeof(CHAR)*256);
158 strcpy(ArcDiskInfo->ArcName, reactos_arc_disk_info[i].ArcName);
159 ArcDiskInfo->ArcName = (PCHAR)PaToVa(ArcDiskInfo->ArcName);
160
161 /* Mark partition table as valid */
162 ArcDiskInfo->ValidPartitionTable = TRUE;
163
164 /* Insert into the list */
165 InsertTailList(&LoaderBlock->ArcDiskInformation->DiskSignatureListHead,
166 &ArcDiskInfo->ListEntry);
167 }
168
169 /* Convert all list's to Virtual address */
170
171 /* Convert the ArcDisks list to virtual address */
172 List_PaToVa(&LoaderBlock->ArcDiskInformation->DiskSignatureListHead);
173 LoaderBlock->ArcDiskInformation = PaToVa(LoaderBlock->ArcDiskInformation);
174
175 /* Convert configuration entries to VA */
176 ConvertConfigToVA(LoaderBlock->ConfigurationRoot);
177 LoaderBlock->ConfigurationRoot = PaToVa(LoaderBlock->ConfigurationRoot);
178
179 /* Convert all DTE into virtual addresses */
180 List_PaToVa(&LoaderBlock->LoadOrderListHead);
181
182 /* this one will be converted right before switching to
183 virtual paging mode */
184 //List_PaToVa(&LoaderBlock->MemoryDescriptorListHead);
185
186 /* Convert list of boot drivers */
187 List_PaToVa(&LoaderBlock->BootDriverListHead);
188
189 /* Initialize Extension now */
190 Extension = MmHeapAlloc(sizeof(LOADER_PARAMETER_EXTENSION));
191 if (Extension == NULL)
192 {
193 UiMessageBox("Failed to allocate LPB Extension!");
194 return;
195 }
196 RtlZeroMemory(Extension, sizeof(LOADER_PARAMETER_EXTENSION));
197
198 /* Fill LPB extension */
199 Extension->Size = sizeof(LOADER_PARAMETER_EXTENSION);
200 Extension->MajorVersion = (VersionToBoot & 0xFF00) >> 8;
201 Extension->MinorVersion = VersionToBoot & 0xFF;
202 Extension->Profile.Status = 2;
203
204 /* Check if ACPI is present */
205 if (AcpiPresent)
206 {
207 /* See KiRosFrldrLpbToNtLpb for details */
208 Extension->AcpiTable = (PVOID)1;
209 }
210
211 #ifdef _M_IX86
212 /* Set headless block pointer */
213 if (WinLdrTerminalConnected)
214 {
215 Extension->HeadlessLoaderBlock = MmHeapAlloc(sizeof(HEADLESS_LOADER_BLOCK));
216 if (Extension->HeadlessLoaderBlock == NULL)
217 {
218 UiMessageBox("Failed to allocate HLB Extension!");
219 while (TRUE);
220 return;
221 }
222 RtlCopyMemory(
223 Extension->HeadlessLoaderBlock,
224 &LoaderRedirectionInformation,
225 sizeof(HEADLESS_LOADER_BLOCK));
226 Extension->HeadlessLoaderBlock = PaToVa(Extension->HeadlessLoaderBlock);
227 }
228 #endif
229 /* Load drivers database */
230 strcpy(MiscFiles, BootPath);
231 strcat(MiscFiles, "AppPatch\\drvmain.sdb");
232 Extension->DrvDBImage = PaToVa(WinLdrLoadModule(MiscFiles,
233 &Extension->DrvDBSize, LoaderRegistryData));
234
235 /* Convert extension and setup block pointers */
236 LoaderBlock->Extension = PaToVa(Extension);
237
238 if (LoaderBlock->SetupLdrBlock)
239 LoaderBlock->SetupLdrBlock = PaToVa(LoaderBlock->SetupLdrBlock);
240
241 }
242
243 BOOLEAN
244 WinLdrLoadDeviceDriver(PLOADER_PARAMETER_BLOCK LoaderBlock,
245 LPSTR BootPath,
246 PUNICODE_STRING FilePath,
247 ULONG Flags,
248 PLDR_DATA_TABLE_ENTRY *DriverDTE)
249 {
250 CHAR FullPath[1024];
251 CHAR DriverPath[1024];
252 CHAR DllName[1024];
253 PCHAR DriverNamePos;
254 BOOLEAN Status;
255 PVOID DriverBase;
256
257 // Separate the path to file name and directory path
258 _snprintf(DriverPath, sizeof(DriverPath), "%wZ", FilePath);
259 DriverNamePos = strrchr(DriverPath, '\\');
260 if (DriverNamePos != NULL)
261 {
262 // Copy the name
263 strcpy(DllName, DriverNamePos+1);
264
265 // Cut out the name from the path
266 *(DriverNamePos+1) = 0;
267 }
268 else
269 {
270 // There is no directory in the path
271 strcpy(DllName, DriverPath);
272 DriverPath[0] = 0;
273 }
274
275 DPRINTM(DPRINT_WINDOWS, "DriverPath: %s, DllName: %s, LPB %p\n", DriverPath, DllName, LoaderBlock);
276
277
278 // Check if driver is already loaded
279 Status = WinLdrCheckForLoadedDll(LoaderBlock, DllName, DriverDTE);
280 if (Status)
281 {
282 // We've got the pointer to its DTE, just return success
283 return TRUE;
284 }
285
286 // It's not loaded, we have to load it
287 strcpy(FullPath, "\\ArcName\\");
288 _snprintf(FullPath+strlen("\\ArcName\\"), sizeof(FullPath), "%s%wZ", BootPath, FilePath);
289 Status = WinLdrLoadImage(FullPath+strlen("\\ArcName\\"), LoaderBootDriver, &DriverBase);
290 if (!Status)
291 return FALSE;
292
293 // Allocate a DTE for it
294 Status = WinLdrAllocateDataTableEntry(LoaderBlock, DllName, FullPath, 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 #ifdef _M_IX86
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 strcpy(FileName, "\\ArcName\\");
557
558 /* Load kernel */
559 strcpy(FileName+strlen("\\ArcName\\"), BootPath);
560 strcat(FileName, "SYSTEM32\\NTOSKRNL.EXE");
561 Status = WinLdrLoadImage(FileName+strlen("\\ArcName\\"), LoaderSystemCode, &NtosBase);
562 DPRINTM(DPRINT_WINDOWS, "Ntos loaded with status %d at %p\n", Status, NtosBase);
563 WinLdrAllocateDataTableEntry(LoaderBlock, "ntoskrnl.exe",
564 FileName, NtosBase, &KernelDTE);
565
566 /* Load HAL */
567 strcpy(FileName+strlen("\\ArcName\\"), BootPath);
568 strcat(FileName, "SYSTEM32\\HAL.DLL");
569 Status = WinLdrLoadImage(FileName+strlen("\\ArcName\\"), LoaderHalCode, &HalBase);
570 DPRINTM(DPRINT_WINDOWS, "HAL loaded with status %d at %p\n", Status, HalBase);
571 WinLdrAllocateDataTableEntry(LoaderBlock, "hal.dll",
572 FileName, HalBase, &HalDTE);
573
574 /* Load kernel-debugger support dll */
575 if (OperatingSystemVersion > _WIN32_WINNT_WIN2K)
576 {
577 strcpy(FileName+strlen("\\ArcName\\"), BootPath);
578 strcat(FileName, "SYSTEM32\\KDCOM.DLL");
579 Status = WinLdrLoadImage(FileName+strlen("\\ArcName\\"), LoaderBootDriver, &KdComBase);
580 DPRINTM(DPRINT_WINDOWS, "KdCom loaded with status %d at %p\n", Status, KdComBase);
581 WinLdrAllocateDataTableEntry(LoaderBlock, "kdcom.dll",
582 FileName, KdComBase, &KdComDTE);
583 }
584
585 /* Load all referenced DLLs for kernel, HAL and kdcom.dll */
586 strcpy(FileName, BootPath);
587 strcat(FileName, "SYSTEM32\\");
588 WinLdrScanImportDescriptorTable(LoaderBlock, FileName, KernelDTE);
589 WinLdrScanImportDescriptorTable(LoaderBlock, FileName, HalDTE);
590 if (KdComDTE)
591 WinLdrScanImportDescriptorTable(LoaderBlock, FileName, KdComDTE);
592
593 /* Load NLS data, OEM font, and prepare boot drivers list */
594 Status = WinLdrScanSystemHive(LoaderBlock, BootPath);
595 DPRINTM(DPRINT_WINDOWS, "SYSTEM hive scanned with status %d\n", Status);
596
597 /* Load boot drivers */
598 Status = WinLdrLoadBootDrivers(LoaderBlock, BootPath);
599 DPRINTM(DPRINT_WINDOWS, "Boot drivers loaded with status %d\n", Status);
600
601 /* Alloc PCR, TSS, do magic things with the GDT/IDT */
602 WinLdrSetupForNt(LoaderBlock, &GdtIdt, &PcrBasePage, &TssBasePage);
603
604 /* Initialize Phase 1 - no drivers loading anymore */
605 WinLdrInitializePhase1(LoaderBlock, BootOptions, SystemRoot, BootPath, OperatingSystemVersion);
606
607 /* Save entry-point pointer and Loader block VAs */
608 KiSystemStartup = (KERNEL_ENTRY_POINT)KernelDTE->EntryPoint;
609 LoaderBlockVA = PaToVa(LoaderBlock);
610
611 /* "Stop all motors", change videomode */
612 if (OperatingSystemVersion < _WIN32_WINNT_WIN2K)
613 MachPrepareForReactOS(TRUE);
614 else
615 MachPrepareForReactOS(FALSE);
616
617 /* Debugging... */
618 //DumpMemoryAllocMap();
619
620 /* Turn on paging mode of CPU*/
621 WinLdrTurnOnPaging(LoaderBlock, PcrBasePage, TssBasePage, GdtIdt);
622
623 /* Save final value of LoaderPagesSpanned */
624 LoaderBlockVA->Extension->LoaderPagesSpanned = LoaderPagesSpanned;
625
626 DPRINTM(DPRINT_WINDOWS, "Hello from paged mode, KiSystemStartup %p, LoaderBlockVA %p!\n",
627 KiSystemStartup, LoaderBlockVA);
628
629 WinLdrpDumpMemoryDescriptors(LoaderBlockVA);
630 WinLdrpDumpBootDriver(LoaderBlockVA);
631 WinLdrpDumpArcDisks(LoaderBlockVA);
632
633 //FIXME: If I substitute this debugging checkpoint, GCC will "optimize away" the code below
634 //while (1) {};
635 /*asm(".intel_syntax noprefix\n");
636 asm("test1:\n");
637 asm("jmp test1\n");
638 asm(".att_syntax\n");*/
639
640 /* Pass control */
641 (*KiSystemStartup)(LoaderBlockVA);
642
643 return;
644 }
645
646 VOID
647 WinLdrpDumpMemoryDescriptors(PLOADER_PARAMETER_BLOCK LoaderBlock)
648 {
649 PLIST_ENTRY NextMd;
650 PMEMORY_ALLOCATION_DESCRIPTOR MemoryDescriptor;
651
652 NextMd = LoaderBlock->MemoryDescriptorListHead.Flink;
653
654 while (NextMd != &LoaderBlock->MemoryDescriptorListHead)
655 {
656 MemoryDescriptor = CONTAINING_RECORD(NextMd, MEMORY_ALLOCATION_DESCRIPTOR, ListEntry);
657
658 DPRINTM(DPRINT_WINDOWS, "BP %08X PC %04X MT %d\n", MemoryDescriptor->BasePage,
659 MemoryDescriptor->PageCount, MemoryDescriptor->MemoryType);
660
661 NextMd = MemoryDescriptor->ListEntry.Flink;
662 }
663 }
664
665 VOID
666 WinLdrpDumpBootDriver(PLOADER_PARAMETER_BLOCK LoaderBlock)
667 {
668 PLIST_ENTRY NextBd;
669 PBOOT_DRIVER_LIST_ENTRY BootDriver;
670
671 NextBd = LoaderBlock->BootDriverListHead.Flink;
672
673 while (NextBd != &LoaderBlock->BootDriverListHead)
674 {
675 BootDriver = CONTAINING_RECORD(NextBd, BOOT_DRIVER_LIST_ENTRY, Link);
676
677 DPRINTM(DPRINT_WINDOWS, "BootDriver %wZ DTE %08X RegPath: %wZ\n", &BootDriver->FilePath,
678 BootDriver->LdrEntry, &BootDriver->RegistryPath);
679
680 NextBd = BootDriver->Link.Flink;
681 }
682 }
683
684 VOID
685 WinLdrpDumpArcDisks(PLOADER_PARAMETER_BLOCK LoaderBlock)
686 {
687 PLIST_ENTRY NextBd;
688 PARC_DISK_SIGNATURE ArcDisk;
689
690 NextBd = LoaderBlock->ArcDiskInformation->DiskSignatureListHead.Flink;
691
692 while (NextBd != &LoaderBlock->ArcDiskInformation->DiskSignatureListHead)
693 {
694 ArcDisk = CONTAINING_RECORD(NextBd, ARC_DISK_SIGNATURE, ListEntry);
695
696 DPRINTM(DPRINT_WINDOWS, "ArcDisk %s checksum: 0x%X, signature: 0x%X\n",
697 ArcDisk->ArcName, ArcDisk->CheckSum, ArcDisk->Signature);
698
699 NextBd = ArcDisk->ListEntry.Flink;
700 }
701 }
702
703