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