* Sync to trunk HEAD (r53298).
[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 _snprintf(FullPath, sizeof(FullPath), "%s%wZ", BootPath, FilePath);
288 Status = WinLdrLoadImage(FullPath, LoaderBootDriver, &DriverBase);
289 if (!Status)
290 return FALSE;
291
292 // Allocate a DTE for it
293 Status = WinLdrAllocateDataTableEntry(LoaderBlock, DllName, DllName, DriverBase, DriverDTE);
294 if (!Status)
295 {
296 DPRINTM(DPRINT_WINDOWS, "WinLdrAllocateDataTableEntry() failed\n");
297 return FALSE;
298 }
299
300 // Modify any flags, if needed
301 (*DriverDTE)->Flags |= Flags;
302
303 // Look for any dependencies it may have, and load them too
304 sprintf(FullPath,"%s%s", BootPath, DriverPath);
305 Status = WinLdrScanImportDescriptorTable(LoaderBlock, FullPath, *DriverDTE);
306 if (!Status)
307 {
308 DPRINTM(DPRINT_WINDOWS, "WinLdrScanImportDescriptorTable() failed for %s\n",
309 FullPath);
310 return FALSE;
311 }
312
313 return TRUE;
314 }
315
316 BOOLEAN
317 WinLdrLoadBootDrivers(PLOADER_PARAMETER_BLOCK LoaderBlock,
318 LPSTR BootPath)
319 {
320 PLIST_ENTRY NextBd;
321 PBOOT_DRIVER_LIST_ENTRY BootDriver;
322 BOOLEAN Status;
323
324 // Walk through the boot drivers list
325 NextBd = LoaderBlock->BootDriverListHead.Flink;
326
327 while (NextBd != &LoaderBlock->BootDriverListHead)
328 {
329 BootDriver = CONTAINING_RECORD(NextBd, BOOT_DRIVER_LIST_ENTRY, Link);
330
331 DPRINTM(DPRINT_WINDOWS, "BootDriver %wZ DTE %08X RegPath: %wZ\n", &BootDriver->FilePath,
332 BootDriver->LdrEntry, &BootDriver->RegistryPath);
333
334 // Paths are relative (FIXME: Are they always relative?)
335
336 // Load it
337 Status = WinLdrLoadDeviceDriver(LoaderBlock, BootPath, &BootDriver->FilePath,
338 0, &BootDriver->LdrEntry);
339
340 // If loading failed - cry loudly
341 //FIXME: Maybe remove it from the list and try to continue?
342 if (!Status)
343 {
344 UiMessageBox("Can't load boot driver!");
345 return FALSE;
346 }
347
348 // Convert the RegistryPath and DTE addresses to VA since we are not going to use it anymore
349 BootDriver->RegistryPath.Buffer = PaToVa(BootDriver->RegistryPath.Buffer);
350 BootDriver->FilePath.Buffer = PaToVa(BootDriver->FilePath.Buffer);
351 BootDriver->LdrEntry = PaToVa(BootDriver->LdrEntry);
352
353 NextBd = BootDriver->Link.Flink;
354 }
355
356 return TRUE;
357 }
358
359 PVOID WinLdrLoadModule(PCSTR ModuleName, ULONG *Size,
360 TYPE_OF_MEMORY MemoryType)
361 {
362 ULONG FileId;
363 PVOID PhysicalBase;
364 FILEINFORMATION FileInfo;
365 ULONG FileSize;
366 ULONG Status;
367 ULONG BytesRead;
368
369 //CHAR ProgressString[256];
370
371 /* Inform user we are loading files */
372 //sprintf(ProgressString, "Loading %s...", FileName);
373 //UiDrawProgressBarCenter(1, 100, ProgressString);
374
375 DPRINTM(DPRINT_WINDOWS, "Loading module %s\n", ModuleName);
376 *Size = 0;
377
378 /* Open the image file */
379 Status = ArcOpen((PCHAR)ModuleName, OpenReadOnly, &FileId);
380 if (Status != ESUCCESS)
381 {
382 /* In case of errors, we just return, without complaining to the user */
383 return NULL;
384 }
385
386 /* Get this file's size */
387 Status = ArcGetFileInformation(FileId, &FileInfo);
388 if (Status != ESUCCESS)
389 {
390 ArcClose(FileId);
391 return NULL;
392 }
393 FileSize = FileInfo.EndingAddress.LowPart;
394 *Size = FileSize;
395
396 /* Allocate memory */
397 PhysicalBase = MmAllocateMemoryWithType(FileSize, MemoryType);
398 if (PhysicalBase == NULL)
399 {
400 ArcClose(FileId);
401 return NULL;
402 }
403
404 /* Load whole file */
405 Status = ArcRead(FileId, PhysicalBase, FileSize, &BytesRead);
406 ArcClose(FileId);
407 if (Status != ESUCCESS)
408 {
409 return NULL;
410 }
411
412 DPRINTM(DPRINT_WINDOWS, "Loaded %s at 0x%x with size 0x%x\n", ModuleName, PhysicalBase, FileSize);
413
414 return PhysicalBase;
415 }
416
417
418 USHORT
419 WinLdrDetectVersion()
420 {
421 LONG rc;
422 FRLDRHKEY hKey;
423
424 rc = RegOpenKey(
425 NULL,
426 L"\\Registry\\Machine\\SYSTEM\\CurrentControlSet\\Control\\Terminal Server",
427 &hKey);
428 if (rc != ERROR_SUCCESS)
429 {
430 // Key doesn't exist; assume NT 4.0
431 return _WIN32_WINNT_NT4;
432 }
433
434 // We may here want to read the value of ProductVersion
435 return _WIN32_WINNT_WS03;
436 }
437
438
439 VOID
440 LoadAndBootWindows(PCSTR OperatingSystemName,
441 PSTR SettingsValue,
442 USHORT OperatingSystemVersion)
443 {
444 BOOLEAN HasSection;
445 char FullPath[MAX_PATH], SystemRoot[MAX_PATH], BootPath[MAX_PATH];
446 CHAR FileName[MAX_PATH];
447 CHAR BootOptions[256];
448 PCHAR File;
449 PCHAR PathSeparator;
450 PVOID NtosBase = NULL, HalBase = NULL, KdComBase = NULL;
451 BOOLEAN Status;
452 ULONG_PTR SectionId;
453 PLOADER_PARAMETER_BLOCK LoaderBlock, LoaderBlockVA;
454 KERNEL_ENTRY_POINT KiSystemStartup;
455 PLDR_DATA_TABLE_ENTRY KernelDTE, HalDTE, KdComDTE = NULL;
456 // Mm-related things
457 PVOID GdtIdt;
458 ULONG PcrBasePage=0;
459 ULONG TssBasePage=0;
460 // Progress bar
461 CHAR ProgressString[256];
462
463 // Open the operating system section
464 // specified in the .ini file
465 HasSection = IniOpenSection(OperatingSystemName, &SectionId);
466
467 UiDrawBackdrop();
468 UiDrawStatusText("Detecting Hardware...");
469 sprintf(ProgressString, "Loading NT...");
470 UiDrawProgressBarCenter(1, 100, ProgressString);
471
472 /* Read the system path is set in the .ini file */
473 if (!HasSection || !IniReadSettingByName(SectionId, "SystemPath", FullPath, sizeof(FullPath)))
474 {
475 strcpy(FullPath, OperatingSystemName);
476 }
477
478 /* Special case for LiveCD */
479 if (!_strnicmp(FullPath, "LiveCD", strlen("LiveCD")))
480 {
481 strcpy(BootPath, FullPath + strlen("LiveCD"));
482 MachDiskGetBootPath(FullPath, sizeof(FullPath));
483 strcat(FullPath, BootPath);
484 }
485
486 /* Convert FullPath to SystemRoot */
487 PathSeparator = strstr(FullPath, "\\");
488 strcpy(SystemRoot, PathSeparator);
489 strcat(SystemRoot, "\\");
490
491 /* Read booting options */
492 if (!HasSection || !IniReadSettingByName(SectionId, "Options", BootOptions, sizeof(BootOptions)))
493 {
494 /* Get options after the title */
495 const CHAR*p = SettingsValue;
496 while (*p == ' ' || *p == '"')
497 p++;
498 while (*p != '\0' && *p != '"')
499 p++;
500 strcpy(BootOptions, p);
501 DPRINTM(DPRINT_WINDOWS,"BootOptions: '%s'\n", BootOptions);
502 }
503
504 /* Append boot-time options */
505 AppendBootTimeOptions(BootOptions);
506
507 //
508 // Check if a ramdisk file was given
509 //
510 File = strstr(BootOptions, "/RDPATH=");
511 if (File)
512 {
513 //
514 // Copy the file name and everything else after it
515 //
516 strcpy(FileName, File + 8);
517
518 //
519 // Null-terminate
520 //
521 *strstr(FileName, " ") = ANSI_NULL;
522
523 //
524 // Load the ramdisk
525 //
526 RamDiskLoadVirtualFile(FileName);
527 }
528
529 /* Let user know we started loading */
530 //UiDrawStatusText("Loading...");
531
532 /* append a backslash */
533 strcpy(BootPath, FullPath);
534 if ((strlen(BootPath)==0) ||
535 BootPath[strlen(BootPath)] != '\\')
536 strcat(BootPath, "\\");
537
538 DPRINTM(DPRINT_WINDOWS,"BootPath: '%s'\n", BootPath);
539
540 /* Allocate and minimalistic-initialize LPB */
541 AllocateAndInitLPB(&LoaderBlock);
542
543 #ifdef _M_IX86
544 /* Setup redirection support */
545 WinLdrSetupEms(BootOptions);
546 #endif
547 /* Detect hardware */
548 UseRealHeap = TRUE;
549 LoaderBlock->ConfigurationRoot = MachHwDetect();
550
551 sprintf(ProgressString, "Loading system hive...");
552 UiDrawBackdrop();
553 UiDrawProgressBarCenter(15, 100, ProgressString);
554
555 /* Load Hive */
556 Status = WinLdrInitSystemHive(LoaderBlock, BootPath);
557 DPRINTM(DPRINT_WINDOWS, "SYSTEM hive loaded with status %d\n", Status);
558
559 if (OperatingSystemVersion == 0)
560 OperatingSystemVersion = WinLdrDetectVersion();
561
562 /* Load kernel */
563 strcpy(FileName, BootPath);
564 strcat(FileName, "SYSTEM32\\NTOSKRNL.EXE");
565 sprintf(ProgressString, "Loading %s...", strchr(FileName, '\\') + 1);
566 UiDrawBackdrop();
567 UiDrawProgressBarCenter(30, 100, ProgressString);
568 Status = WinLdrLoadImage(FileName, LoaderSystemCode, &NtosBase);
569 DPRINTM(DPRINT_WINDOWS, "Ntos loaded with status %d at %p\n", Status, NtosBase);
570
571 /* Load HAL */
572 strcpy(FileName, BootPath);
573 strcat(FileName, "SYSTEM32\\HAL.DLL");
574 sprintf(ProgressString, "Loading %s...", strchr(FileName, '\\') + 1);
575 UiDrawBackdrop();
576 UiDrawProgressBarCenter(45, 100, ProgressString);
577 Status = WinLdrLoadImage(FileName, LoaderHalCode, &HalBase);
578 DPRINTM(DPRINT_WINDOWS, "HAL loaded with status %d at %p\n", Status, HalBase);
579
580 /* Load kernel-debugger support dll */
581 if (OperatingSystemVersion > _WIN32_WINNT_WIN2K)
582 {
583 strcpy(FileName, BootPath);
584 strcat(FileName, "SYSTEM32\\KDCOM.DLL");
585 sprintf(ProgressString, "Loading %s...", strchr(FileName, '\\') + 1);
586 UiDrawBackdrop();
587 UiDrawProgressBarCenter(60, 100, ProgressString);
588 Status = WinLdrLoadImage(FileName, LoaderBootDriver, &KdComBase);
589 DPRINTM(DPRINT_WINDOWS, "KdCom loaded with status %d at %p\n", Status, KdComBase);
590 }
591
592 /* Allocate data table entries for above-loaded modules */
593 WinLdrAllocateDataTableEntry(LoaderBlock, "ntoskrnl.exe",
594 "WINDOWS\\SYSTEM32\\NTOSKRNL.EXE", NtosBase, &KernelDTE);
595 WinLdrAllocateDataTableEntry(LoaderBlock, "hal.dll",
596 "WINDOWS\\SYSTEM32\\HAL.DLL", HalBase, &HalDTE);
597 if (OperatingSystemVersion > _WIN32_WINNT_WIN2K)
598 {
599 WinLdrAllocateDataTableEntry(LoaderBlock, "kdcom.dll",
600 "WINDOWS\\SYSTEM32\\KDCOM.DLL", KdComBase, &KdComDTE);
601 }
602
603 /* Load all referenced DLLs for kernel, HAL and kdcom.dll */
604 strcpy(FileName, BootPath);
605 strcat(FileName, "SYSTEM32\\");
606 WinLdrScanImportDescriptorTable(LoaderBlock, FileName, KernelDTE);
607 WinLdrScanImportDescriptorTable(LoaderBlock, FileName, HalDTE);
608 if (KdComDTE)
609 WinLdrScanImportDescriptorTable(LoaderBlock, FileName, KdComDTE);
610
611 sprintf(ProgressString, "Loading NLS and OEM fonts...");
612 UiDrawBackdrop();
613 UiDrawProgressBarCenter(80, 100, ProgressString);
614 /* Load NLS data, OEM font, and prepare boot drivers list */
615 Status = WinLdrScanSystemHive(LoaderBlock, BootPath);
616 DPRINTM(DPRINT_WINDOWS, "SYSTEM hive scanned with status %d\n", Status);
617
618 /* Load boot drivers */
619 sprintf(ProgressString, "Loading boot drivers...");
620 UiDrawBackdrop();
621 UiDrawProgressBarCenter(100, 100, ProgressString);
622 Status = WinLdrLoadBootDrivers(LoaderBlock, BootPath);
623 DPRINTM(DPRINT_WINDOWS, "Boot drivers loaded with status %d\n", Status);
624
625 /* Alloc PCR, TSS, do magic things with the GDT/IDT */
626 WinLdrSetupForNt(LoaderBlock, &GdtIdt, &PcrBasePage, &TssBasePage);
627
628 /* Initialize Phase 1 - no drivers loading anymore */
629 WinLdrInitializePhase1(LoaderBlock, BootOptions, SystemRoot, BootPath, OperatingSystemVersion);
630
631 /* Save entry-point pointer and Loader block VAs */
632 KiSystemStartup = (KERNEL_ENTRY_POINT)KernelDTE->EntryPoint;
633 LoaderBlockVA = PaToVa(LoaderBlock);
634
635 /* "Stop all motors", change videomode */
636 if (OperatingSystemVersion < _WIN32_WINNT_WIN2K)
637 MachPrepareForReactOS(TRUE);
638 else
639 MachPrepareForReactOS(FALSE);
640
641 /* Debugging... */
642 //DumpMemoryAllocMap();
643
644 /* Turn on paging mode of CPU*/
645 WinLdrTurnOnPaging(LoaderBlock, PcrBasePage, TssBasePage, GdtIdt);
646
647 /* Save final value of LoaderPagesSpanned */
648 LoaderBlockVA->Extension->LoaderPagesSpanned = LoaderPagesSpanned;
649
650 DPRINTM(DPRINT_WINDOWS, "Hello from paged mode, KiSystemStartup %p, LoaderBlockVA %p!\n",
651 KiSystemStartup, LoaderBlockVA);
652
653 WinLdrpDumpMemoryDescriptors(LoaderBlockVA);
654 WinLdrpDumpBootDriver(LoaderBlockVA);
655 WinLdrpDumpArcDisks(LoaderBlockVA);
656
657 //FIXME: If I substitute this debugging checkpoint, GCC will "optimize away" the code below
658 //while (1) {};
659 /*asm(".intel_syntax noprefix\n");
660 asm("test1:\n");
661 asm("jmp test1\n");
662 asm(".att_syntax\n");*/
663
664 /* Pass control */
665 (*KiSystemStartup)(LoaderBlockVA);
666
667 return;
668 }
669
670 VOID
671 WinLdrpDumpMemoryDescriptors(PLOADER_PARAMETER_BLOCK LoaderBlock)
672 {
673 PLIST_ENTRY NextMd;
674 PMEMORY_ALLOCATION_DESCRIPTOR MemoryDescriptor;
675
676 NextMd = LoaderBlock->MemoryDescriptorListHead.Flink;
677
678 while (NextMd != &LoaderBlock->MemoryDescriptorListHead)
679 {
680 MemoryDescriptor = CONTAINING_RECORD(NextMd, MEMORY_ALLOCATION_DESCRIPTOR, ListEntry);
681
682 DPRINTM(DPRINT_WINDOWS, "BP %08X PC %04X MT %d\n", MemoryDescriptor->BasePage,
683 MemoryDescriptor->PageCount, MemoryDescriptor->MemoryType);
684
685 NextMd = MemoryDescriptor->ListEntry.Flink;
686 }
687 }
688
689 VOID
690 WinLdrpDumpBootDriver(PLOADER_PARAMETER_BLOCK LoaderBlock)
691 {
692 PLIST_ENTRY NextBd;
693 PBOOT_DRIVER_LIST_ENTRY BootDriver;
694
695 NextBd = LoaderBlock->BootDriverListHead.Flink;
696
697 while (NextBd != &LoaderBlock->BootDriverListHead)
698 {
699 BootDriver = CONTAINING_RECORD(NextBd, BOOT_DRIVER_LIST_ENTRY, Link);
700
701 DPRINTM(DPRINT_WINDOWS, "BootDriver %wZ DTE %08X RegPath: %wZ\n", &BootDriver->FilePath,
702 BootDriver->LdrEntry, &BootDriver->RegistryPath);
703
704 NextBd = BootDriver->Link.Flink;
705 }
706 }
707
708 VOID
709 WinLdrpDumpArcDisks(PLOADER_PARAMETER_BLOCK LoaderBlock)
710 {
711 PLIST_ENTRY NextBd;
712 PARC_DISK_SIGNATURE ArcDisk;
713
714 NextBd = LoaderBlock->ArcDiskInformation->DiskSignatureListHead.Flink;
715
716 while (NextBd != &LoaderBlock->ArcDiskInformation->DiskSignatureListHead)
717 {
718 ArcDisk = CONTAINING_RECORD(NextBd, ARC_DISK_SIGNATURE, ListEntry);
719
720 DPRINTM(DPRINT_WINDOWS, "ArcDisk %s checksum: 0x%X, signature: 0x%X\n",
721 ArcDisk->ArcName, ArcDisk->CheckSum, ArcDisk->Signature);
722
723 NextBd = ArcDisk->ListEntry.Flink;
724 }
725 }
726
727