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 along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include <ndk/ldrtypes.h>
24 #include <arc/setupblk.h>
29 VOID
AllocateAndInitLPB(PLOADER_PARAMETER_BLOCK
*OutLoaderBlock
);
30 BOOLEAN
WinLdrLoadBootDrivers(PLOADER_PARAMETER_BLOCK LoaderBlock
, LPSTR BootPath
);
31 void WinLdrSetupForNt(PLOADER_PARAMETER_BLOCK LoaderBlock
,
36 WinLdrInitializePhase1(PLOADER_PARAMETER_BLOCK LoaderBlock
,
40 USHORT VersionToBoot
);
42 WinLdrLoadNLSData(IN OUT PLOADER_PARAMETER_BLOCK LoaderBlock
,
43 IN LPCSTR DirectoryPath
,
44 IN LPCSTR AnsiFileName
,
45 IN LPCSTR OemFileName
,
46 IN LPCSTR LanguageFileName
);
48 WinLdrAddDriverToList(LIST_ENTRY
*BootDriverListHead
,
54 //FIXME: Do a better way to retrieve Arc disk information
55 extern ULONG reactos_disk_count
;
56 extern ARC_DISK_SIGNATURE reactos_arc_disk_info
[];
57 extern char reactos_arc_strings
[32][256];
59 extern BOOLEAN UseRealHeap
;
60 extern ULONG LoaderPagesSpanned
;
64 SetupLdrLoadNlsData(PLOADER_PARAMETER_BLOCK LoaderBlock
, HINF InfHandle
, LPCSTR SearchPath
)
66 INFCONTEXT InfContext
;
68 LPCSTR AnsiName
, OemName
, LangName
;
70 /* Get ANSI codepage file */
71 if (!InfFindFirstLine(InfHandle
, "NLS", "AnsiCodepage", &InfContext
))
73 printf("Failed to find 'NLS/AnsiCodepage'\n");
76 if (!InfGetDataField(&InfContext
, 1, &AnsiName
))
78 printf("Failed to get load options\n");
82 /* Get OEM codepage file */
83 if (!InfFindFirstLine(InfHandle
, "NLS", "OemCodepage", &InfContext
))
85 printf("Failed to find 'NLS/AnsiCodepage'\n");
88 if (!InfGetDataField(&InfContext
, 1, &OemName
))
90 printf("Failed to get load options\n");
94 if (!InfFindFirstLine(InfHandle
, "NLS", "UnicodeCasetable", &InfContext
))
96 printf("Failed to find 'NLS/AnsiCodepage'\n");
99 if (!InfGetDataField(&InfContext
, 1, &LangName
))
101 printf("Failed to get load options\n");
105 Status
= WinLdrLoadNLSData(LoaderBlock
, SearchPath
, AnsiName
, OemName
, LangName
);
106 DPRINTM(DPRINT_WINDOWS
, "NLS data loaded with status %d\n", Status
);
110 SetupLdrScanBootDrivers(PLOADER_PARAMETER_BLOCK LoaderBlock
, HINF InfHandle
, LPCSTR SearchPath
)
112 INFCONTEXT InfContext
;
114 LPCSTR Media
, DriverName
;
115 WCHAR ServiceName
[256];
116 WCHAR ImagePath
[256];
118 /* Open inf section */
119 if (!InfFindFirstLine(InfHandle
, "SourceDisksFiles", NULL
, &InfContext
))
122 /* Load all listed boot drivers */
125 if (InfGetDataField(&InfContext
, 7, &Media
) &&
126 InfGetDataField(&InfContext
, 0, &DriverName
))
128 if (strcmp(Media
, "x") == 0)
130 /* Convert name to widechar */
131 swprintf(ServiceName
, L
"%S", DriverName
);
133 /* Remove .sys extension */
134 ServiceName
[wcslen(ServiceName
) - 4] = 0;
136 /* Prepare image path */
137 swprintf(ImagePath
, L
"%S", DriverName
);
139 /* Add it to the list */
140 Status
= WinLdrAddDriverToList(&LoaderBlock
->BootDriverListHead
,
141 L
"\\Registry\\Machine\\System\\CurrentControlSet\\Services\\",
147 DPRINTM(DPRINT_WINDOWS
, "could not add boot driver %s, %s\n", SearchPath
, DriverName
);
152 } while (InfFindNextLine(&InfContext
, &InfContext
));
155 VOID
LoadReactOSSetup2(VOID
)
157 CHAR SystemPath
[512], SearchPath
[512];
160 LPCSTR LoadOptions
, BootOptions
;
161 BOOLEAN BootFromFloppy
;
165 PVOID NtosBase
= NULL
, HalBase
= NULL
, KdComBase
= NULL
;
169 INFCONTEXT InfContext
;
170 PLOADER_PARAMETER_BLOCK LoaderBlock
, LoaderBlockVA
;
171 PSETUP_LOADER_BLOCK SetupBlock
;
172 KERNEL_ENTRY_POINT KiSystemStartup
;
173 PLDR_DATA_TABLE_ENTRY KernelDTE
, HalDTE
, KdComDTE
= NULL
;
179 LPCSTR SourcePaths
[] =
181 "", /* Only for floppy boot */
184 #elif defined(_M_MPPC)
186 #elif defined(_M_MRX000)
194 MachDiskGetBootPath(SystemPath
, sizeof(SystemPath
));
196 /* And check if we booted from floppy */
197 BootFromFloppy
= strstr(SystemPath
, "fdisk") != NULL
;
199 /* Open 'txtsetup.sif' from any of source paths */
200 for (i
= BootFromFloppy
? 0 : 1; ; i
++)
202 SourcePath
= SourcePaths
[i
];
205 printf("Failed to open 'txtsetup.sif'\n");
208 sprintf(FileName
, "%s\\txtsetup.sif", SourcePath
);
209 if (InfOpenFile (&InfHandle
, FileName
, &ErrorLine
))
211 sprintf(BootPath
, "%s%s\\", SystemPath
, SourcePath
);
216 /* Get Load options - debug and non-debug */
217 if (!InfFindFirstLine(InfHandle
,
222 printf("Failed to find 'SetupData/OsLoadOptions'\n");
226 if (!InfGetDataField (&InfContext
, 1, &LoadOptions
))
228 printf("Failed to get load options\n");
232 BootOptions
= LoadOptions
;
235 /* Get debug load options and use them */
236 if (InfFindFirstLine(InfHandle
,
241 if (!InfGetDataField(&InfContext
, 1, &DbgOptions
))
244 BootOptions
= DbgOptions
;
248 DPRINTM(DPRINT_WINDOWS
,"BootOptions: '%s'\n", BootOptions
);
251 UiDrawStatusText("");
252 UiDrawStatusText("Detecting Hardware...");
254 /* Let user know we started loading */
255 UiDrawStatusText("Loading...");
257 /* Construct the system path */
258 sprintf(SystemPath
, "%s\\", SourcePath
);
260 DPRINTM(DPRINT_WINDOWS
,"BootPath: '%s', SystemPath: '%s'\n", BootPath
, SystemPath
);
262 /* Allocate and minimalistic-initialize LPB */
263 AllocateAndInitLPB(&LoaderBlock
);
265 /* Allocate and initialize setup loader block */
266 SetupBlock
= MmHeapAlloc(sizeof(SETUP_LOADER_BLOCK
));
267 RtlZeroMemory(SetupBlock
, sizeof(SETUP_LOADER_BLOCK
));
268 LoaderBlock
->SetupLdrBlock
= SetupBlock
;
270 /* Set textmode setup flag */
271 SetupBlock
->Flags
= SETUPLDR_TEXT_MODE
;
273 /* Detect hardware */
275 LoaderBlock
->ConfigurationRoot
= MachHwDetect();
277 strcpy(FileName
, "\\ArcName\\");
280 strcpy(FileName
+strlen("\\ArcName\\"), BootPath
);
281 strcat(FileName
, "NTOSKRNL.EXE");
282 Status
= WinLdrLoadImage(FileName
+strlen("\\ArcName\\"), LoaderSystemCode
, &NtosBase
);
283 DPRINTM(DPRINT_WINDOWS
, "Ntos loaded with status %d at %p\n", Status
, NtosBase
);
284 WinLdrAllocateDataTableEntry(LoaderBlock
, "ntoskrnl.exe",
285 FileName
, NtosBase
, &KernelDTE
);
288 strcpy(FileName
+strlen("\\ArcName\\"), BootPath
);
289 strcat(FileName
, "HAL.DLL");
290 Status
= WinLdrLoadImage(FileName
+strlen("\\ArcName\\"), LoaderHalCode
, &HalBase
);
291 DPRINTM(DPRINT_WINDOWS
, "HAL loaded with status %d at %p\n", Status
, HalBase
);
292 WinLdrAllocateDataTableEntry(LoaderBlock
, "hal.dll",
293 FileName
, HalBase
, &HalDTE
);
295 /* Load kernel-debugger support dll */
296 strcpy(FileName
+strlen("\\ArcName\\"), BootPath
);
297 strcat(FileName
, "KDCOM.DLL");
298 Status
= WinLdrLoadImage(FileName
+strlen("\\ArcName\\"), LoaderBootDriver
, &KdComBase
);
299 DPRINTM(DPRINT_WINDOWS
, "KdCom loaded with status %d at %p\n", Status
, KdComBase
);
300 WinLdrAllocateDataTableEntry(LoaderBlock
, "kdcom.dll",
301 FileName
, KdComBase
, &KdComDTE
);
303 /* Load all referenced DLLs for kernel, HAL and kdcom.dll */
304 strcpy(SearchPath
, BootPath
);
305 WinLdrScanImportDescriptorTable(LoaderBlock
, SearchPath
, KernelDTE
);
306 WinLdrScanImportDescriptorTable(LoaderBlock
, SearchPath
, HalDTE
);
308 WinLdrScanImportDescriptorTable(LoaderBlock
, SearchPath
, KdComDTE
);
311 SetupLdrLoadNlsData(LoaderBlock
, InfHandle
, BootPath
);
313 /* Get a list of boot drivers */
314 SetupLdrScanBootDrivers(LoaderBlock
, InfHandle
, BootPath
);
316 /* Load boot drivers */
317 Status
= WinLdrLoadBootDrivers(LoaderBlock
, BootPath
);
318 DPRINTM(DPRINT_WINDOWS
, "Boot drivers loaded with status %d\n", Status
);
320 /* Alloc PCR, TSS, do magic things with the GDT/IDT */
321 WinLdrSetupForNt(LoaderBlock
, &GdtIdt
, &PcrBasePage
, &TssBasePage
);
323 /* Initialize Phase 1 - no drivers loading anymore */
324 WinLdrInitializePhase1(LoaderBlock
, (PCHAR
)BootOptions
, SystemPath
, BootPath
, _WIN32_WINNT_WS03
);
326 /* Save entry-point pointer and Loader block VAs */
327 KiSystemStartup
= (KERNEL_ENTRY_POINT
)KernelDTE
->EntryPoint
;
328 LoaderBlockVA
= PaToVa(LoaderBlock
);
330 /* "Stop all motors", change videomode */
331 MachPrepareForReactOS(TRUE
);
334 //DumpMemoryAllocMap();
336 /* Turn on paging mode of CPU*/
337 WinLdrTurnOnPaging(LoaderBlock
, PcrBasePage
, TssBasePage
, GdtIdt
);
339 /* Save final value of LoaderPagesSpanned */
340 LoaderBlock
->Extension
->LoaderPagesSpanned
= LoaderPagesSpanned
;
342 DPRINTM(DPRINT_WINDOWS
, "Hello from paged mode, KiSystemStartup %p, LoaderBlockVA %p!\n",
343 KiSystemStartup
, LoaderBlockVA
);
345 //WinLdrpDumpMemoryDescriptors(LoaderBlockVA);
346 //WinLdrpDumpBootDriver(LoaderBlockVA);
347 //WinLdrpDumpArcDisks(LoaderBlockVA);
349 /*asm(".intel_syntax noprefix\n");
352 asm(".att_syntax\n");*/
355 (*KiSystemStartup
)(LoaderBlockVA
);