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