4 * Copyright (C) 2009 Aleksey Bragin <aleksey@reactos.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include <ndk/ldrtypes.h>
27 VOID
AllocateAndInitLPB(PLOADER_PARAMETER_BLOCK
*OutLoaderBlock
);
28 BOOLEAN
WinLdrLoadBootDrivers(PLOADER_PARAMETER_BLOCK LoaderBlock
, LPSTR BootPath
);
29 void WinLdrSetupForNt(PLOADER_PARAMETER_BLOCK LoaderBlock
,
34 WinLdrInitializePhase1(PLOADER_PARAMETER_BLOCK LoaderBlock
,
38 USHORT VersionToBoot
);
40 WinLdrLoadNLSData(IN OUT PLOADER_PARAMETER_BLOCK LoaderBlock
,
41 IN LPCSTR DirectoryPath
,
42 IN LPCSTR AnsiFileName
,
43 IN LPCSTR OemFileName
,
44 IN LPCSTR LanguageFileName
);
46 WinLdrAddDriverToList(LIST_ENTRY
*BootDriverListHead
,
52 //FIXME: Do a better way to retrieve Arc disk information
53 extern ULONG reactos_disk_count
;
54 extern ARC_DISK_SIGNATURE reactos_arc_disk_info
[];
55 extern char reactos_arc_strings
[32][256];
57 extern BOOLEAN UseRealHeap
;
58 extern ULONG LoaderPagesSpanned
;
62 SetupLdrLoadNlsData(PLOADER_PARAMETER_BLOCK LoaderBlock
, HINF InfHandle
, LPCSTR SearchPath
)
64 INFCONTEXT InfContext
;
66 LPCSTR AnsiName
, OemName
, LangName
;
68 /* Get ANSI codepage file */
69 if (!InfFindFirstLine(InfHandle
, "NLS", "AnsiCodepage", &InfContext
))
71 printf("Failed to find 'NLS/AnsiCodepage'\n");
74 if (!InfGetDataField(&InfContext
, 1, &AnsiName
))
76 printf("Failed to get load options\n");
80 /* Get OEM codepage file */
81 if (!InfFindFirstLine(InfHandle
, "NLS", "OemCodepage", &InfContext
))
83 printf("Failed to find 'NLS/AnsiCodepage'\n");
86 if (!InfGetDataField(&InfContext
, 1, &OemName
))
88 printf("Failed to get load options\n");
92 if (!InfFindFirstLine(InfHandle
, "NLS", "UnicodeCasetable", &InfContext
))
94 printf("Failed to find 'NLS/AnsiCodepage'\n");
97 if (!InfGetDataField(&InfContext
, 1, &LangName
))
99 printf("Failed to get load options\n");
103 Status
= WinLdrLoadNLSData(LoaderBlock
, SearchPath
, AnsiName
, OemName
, LangName
);
104 DPRINTM(DPRINT_WINDOWS
, "NLS data loaded with status %d\n", Status
);
108 SetupLdrScanBootDrivers(PLOADER_PARAMETER_BLOCK LoaderBlock
, HINF InfHandle
, LPCSTR SearchPath
)
110 INFCONTEXT InfContext
;
112 LPCSTR Media
, DriverName
;
113 WCHAR ServiceName
[256];
114 WCHAR ImagePath
[256];
116 /* Open inf section */
117 if (!InfFindFirstLine(InfHandle
, "SourceDisksFiles", NULL
, &InfContext
))
120 /* Load all listed boot drivers */
123 if (InfGetDataField(&InfContext
, 7, &Media
) &&
124 InfGetDataField(&InfContext
, 0, &DriverName
))
126 if (strcmp(Media
, "x") == 0)
128 /* Convert name to widechar */
129 swprintf(ServiceName
, L
"%S", DriverName
);
131 /* Remove .sys extension */
132 ServiceName
[wcslen(ServiceName
) - 4] = 0;
134 /* Prepare image path */
135 swprintf(ImagePath
, L
"%S", DriverName
);
137 /* Add it to the list */
138 Status
= WinLdrAddDriverToList(&LoaderBlock
->BootDriverListHead
,
139 L
"\\Registry\\Machine\\System\\CurrentControlSet\\Services\\",
145 DPRINTM(DPRINT_WINDOWS
, "could not add boot driver %s, %s\n", SearchPath
, DriverName
);
150 } while (InfFindNextLine(&InfContext
, &InfContext
));
153 VOID
LoadReactOSSetup2(VOID
)
155 CHAR SystemPath
[512], SearchPath
[512];
159 PVOID NtosBase
= NULL
, HalBase
= NULL
, KdComBase
= NULL
;
164 INFCONTEXT InfContext
;
165 PLOADER_PARAMETER_BLOCK LoaderBlock
, LoaderBlockVA
;
166 KERNEL_ENTRY_POINT KiSystemStartup
;
167 PLDR_DATA_TABLE_ENTRY KernelDTE
, HalDTE
, KdComDTE
= NULL
;
173 LPCSTR SourcePaths
[] =
175 "", /* Only for floppy boot */
178 #elif defined(_M_MPPC)
180 #elif defined(_M_MRX000)
187 /* Get boot device number */
188 MachDiskGetBootDevice(&BootDevice
);
190 /* Open 'txtsetup.sif' from any of source paths */
191 for (i
= MachDiskBootingFromFloppy() ? 0 : 1; ; i
++)
193 SourcePath
= SourcePaths
[i
];
196 printf("Failed to open 'txtsetup.sif'\n");
199 sprintf(FileName
,"%s\\txtsetup.sif", SourcePath
);
200 if (InfOpenFile (&InfHandle
, FileName
, &ErrorLine
))
204 /* If we didn't find it anywhere, then just use root */
209 if (!InfFindFirstLine(InfHandle
,
214 printf("Failed to find 'SetupData/OsLoadOptions'\n");
218 if (!InfGetDataField (&InfContext
, 1, &BootOptions
))
220 printf("Failed to get load options\n");
224 /* Save source path */
225 strcpy(BootPath
, SourcePath
);
228 UiDrawStatusText("");
229 UiDrawStatusText("Detecting Hardware...");
231 /* Let user know we started loading */
232 UiDrawStatusText("Loading...");
234 /* Try to open system drive */
237 /* Append a backslash to the bootpath if needed */
238 if ((strlen(BootPath
)==0) || BootPath
[strlen(BootPath
)] != '\\')
240 strcat(BootPath
, "\\");
243 /* Construct the system path */
244 MachDiskGetBootPath(SystemPath
, sizeof(SystemPath
));
245 strcat(SystemPath
, SourcePath
);
247 DPRINTM(DPRINT_WINDOWS
,"SystemRoot: '%s', SystemPath: '%s'\n", BootPath
, SystemPath
);
249 /* Allocate and minimalistic-initialize LPB */
250 AllocateAndInitLPB(&LoaderBlock
);
252 /* Detect hardware */
254 LoaderBlock
->ConfigurationRoot
= MachHwDetect();
257 strcpy(FileName
, BootPath
);
258 strcat(FileName
, "NTOSKRNL.EXE");
259 Status
= WinLdrLoadImage(FileName
, LoaderSystemCode
, &NtosBase
);
260 DPRINTM(DPRINT_WINDOWS
, "Ntos loaded with status %d at %p\n", Status
, NtosBase
);
263 strcpy(FileName
, BootPath
);
264 strcat(FileName
, "HAL.DLL");
265 Status
= WinLdrLoadImage(FileName
, LoaderHalCode
, &HalBase
);
266 DPRINTM(DPRINT_WINDOWS
, "HAL loaded with status %d at %p\n", Status
, HalBase
);
268 /* Load kernel-debugger support dll */
269 strcpy(FileName
, BootPath
);
270 strcat(FileName
, "KDCOM.DLL");
271 Status
= WinLdrLoadImage(FileName
, LoaderBootDriver
, &KdComBase
);
272 DPRINTM(DPRINT_WINDOWS
, "KdCom loaded with status %d at %p\n", Status
, KdComBase
);
274 /* Allocate data table entries for above-loaded modules */
275 WinLdrAllocateDataTableEntry(LoaderBlock
, "ntoskrnl.exe",
276 "NTOSKRNL.EXE", NtosBase
, &KernelDTE
);
277 WinLdrAllocateDataTableEntry(LoaderBlock
, "hal.dll",
278 "HAL.DLL", HalBase
, &HalDTE
);
279 WinLdrAllocateDataTableEntry(LoaderBlock
, "kdcom.dll",
280 "KDCOM.DLL", KdComBase
, &KdComDTE
);
282 /* Load all referenced DLLs for kernel, HAL and kdcom.dll */
283 strcpy(SearchPath
, BootPath
);
284 WinLdrScanImportDescriptorTable(LoaderBlock
, SearchPath
, KernelDTE
);
285 WinLdrScanImportDescriptorTable(LoaderBlock
, SearchPath
, HalDTE
);
287 WinLdrScanImportDescriptorTable(LoaderBlock
, SearchPath
, KdComDTE
);
290 SetupLdrLoadNlsData(LoaderBlock
, InfHandle
, BootPath
);
292 /* Get a list of boot drivers */
293 SetupLdrScanBootDrivers(LoaderBlock
, InfHandle
, BootPath
);
295 /* Load boot drivers */
296 Status
= WinLdrLoadBootDrivers(LoaderBlock
, BootPath
);
297 DPRINTM(DPRINT_WINDOWS
, "Boot drivers loaded with status %d\n", Status
);
299 /* Alloc PCR, TSS, do magic things with the GDT/IDT */
300 WinLdrSetupForNt(LoaderBlock
, &GdtIdt
, &PcrBasePage
, &TssBasePage
);
302 /* Initialize Phase 1 - no drivers loading anymore */
303 WinLdrInitializePhase1(LoaderBlock
, (PCHAR
)BootOptions
, SystemPath
, BootPath
, _WIN32_WINNT_WS03
);
305 /* Save entry-point pointer and Loader block VAs */
306 KiSystemStartup
= (KERNEL_ENTRY_POINT
)KernelDTE
->EntryPoint
;
307 LoaderBlockVA
= PaToVa(LoaderBlock
);
309 /* "Stop all motors", change videomode */
310 MachPrepareForReactOS(FALSE
);
313 //DumpMemoryAllocMap();
315 /* Turn on paging mode of CPU*/
316 WinLdrTurnOnPaging(LoaderBlock
, PcrBasePage
, TssBasePage
, GdtIdt
);
318 /* Save final value of LoaderPagesSpanned */
319 LoaderBlock
->Extension
->LoaderPagesSpanned
= LoaderPagesSpanned
;
321 DPRINTM(DPRINT_WINDOWS
, "Hello from paged mode, KiSystemStartup %p, LoaderBlockVA %p!\n",
322 KiSystemStartup
, LoaderBlockVA
);
324 //WinLdrpDumpMemoryDescriptors(LoaderBlockVA);
325 //WinLdrpDumpBootDriver(LoaderBlockVA);
326 //WinLdrpDumpArcDisks(LoaderBlockVA);
329 (*KiSystemStartup
)(LoaderBlockVA
);