3 * LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
4 * PURPOSE: Windows-compatible NT OS Setup Loader.
5 * COPYRIGHT: Copyright 2009-2019 Aleksey Bragin <aleksey@reactos.org>
9 #include <ndk/ldrtypes.h>
10 #include <arc/setupblk.h>
15 DBG_DEFAULT_CHANNEL(WINDOWS
);
17 #define TAG_BOOT_OPTIONS 'pOtB'
20 VOID
AllocateAndInitLPB(PLOADER_PARAMETER_BLOCK
*OutLoaderBlock
);
23 SetupLdrLoadNlsData(PLOADER_PARAMETER_BLOCK LoaderBlock
, HINF InfHandle
, PCSTR SearchPath
)
25 INFCONTEXT InfContext
;
26 PCSTR AnsiName
, OemName
, LangName
;
28 /* Get ANSI codepage file */
29 if (!InfFindFirstLine(InfHandle
, "NLS", "AnsiCodepage", &InfContext
))
31 ERR("Failed to find 'NLS/AnsiCodepage'\n");
34 if (!InfGetDataField(&InfContext
, 1, &AnsiName
))
36 ERR("Failed to get load options\n");
40 /* Get OEM codepage file */
41 if (!InfFindFirstLine(InfHandle
, "NLS", "OemCodepage", &InfContext
))
43 ERR("Failed to find 'NLS/AnsiCodepage'\n");
46 if (!InfGetDataField(&InfContext
, 1, &OemName
))
48 ERR("Failed to get load options\n");
52 if (!InfFindFirstLine(InfHandle
, "NLS", "UnicodeCasetable", &InfContext
))
54 ERR("Failed to find 'NLS/AnsiCodepage'\n");
57 if (!InfGetDataField(&InfContext
, 1, &LangName
))
59 ERR("Failed to get load options\n");
63 TRACE("NLS data '%s' '%s' '%s'\n", AnsiName
, OemName
, LangName
);
67 BOOLEAN Success
= WinLdrLoadNLSData(LoaderBlock
, SearchPath
, AnsiName
, OemName
, LangName
);
69 TRACE("NLS data loading %s\n", Success
? "successful" : "failed");
72 WinLdrLoadNLSData(LoaderBlock
, SearchPath
, AnsiName
, OemName
, LangName
);
75 /* TODO: Load OEM HAL font */
81 SetupLdrInitErrataInf(
85 INFCONTEXT InfContext
;
89 CHAR ErrataFilePath
[MAX_PATH
];
91 /* Retrieve the INF file name value */
92 if (!InfFindFirstLine(InfHandle
, "BiosInfo", "InfName", &InfContext
))
94 WARN("Failed to find 'BiosInfo/InfName'\n");
97 if (!InfGetDataField(&InfContext
, 1, &FileName
))
99 WARN("Failed to read 'InfName' value\n");
103 RtlStringCbCopyA(ErrataFilePath
, sizeof(ErrataFilePath
), SystemRoot
);
104 RtlStringCbCatA(ErrataFilePath
, sizeof(ErrataFilePath
), FileName
);
106 /* Load the INF file */
107 PhysicalBase
= WinLdrLoadModule(ErrataFilePath
, &FileSize
, LoaderRegistryData
);
110 WARN("Could not load '%s'\n", ErrataFilePath
);
114 WinLdrSystemBlock
->Extension
.EmInfFileImage
= PaToVa(PhysicalBase
);
115 WinLdrSystemBlock
->Extension
.EmInfFileSize
= FileSize
;
121 SetupLdrScanBootDrivers(PLIST_ENTRY BootDriverListHead
, HINF InfHandle
, PCSTR SearchPath
)
123 INFCONTEXT InfContext
, dirContext
;
125 PCSTR Media
, DriverName
, dirIndex
, ImagePath
;
126 WCHAR ServiceName
[256];
127 WCHAR ImagePathW
[256];
129 /* Open inf section */
130 if (!InfFindFirstLine(InfHandle
, "SourceDisksFiles", NULL
, &InfContext
))
133 /* Load all listed boot drivers */
136 if (InfGetDataField(&InfContext
, 7, &Media
) &&
137 InfGetDataField(&InfContext
, 0, &DriverName
) &&
138 InfGetDataField(&InfContext
, 13, &dirIndex
))
140 if ((strcmp(Media
, "x") == 0) &&
141 InfFindFirstLine(InfHandle
, "Directories", dirIndex
, &dirContext
) &&
142 InfGetDataField(&dirContext
, 1, &ImagePath
))
144 /* Convert name to widechar */
145 swprintf(ServiceName
, L
"%S", DriverName
);
147 /* Prepare image path */
148 swprintf(ImagePathW
, L
"%S", ImagePath
);
149 wcscat(ImagePathW
, L
"\\");
150 wcscat(ImagePathW
, ServiceName
);
152 /* Remove .sys extension */
153 ServiceName
[wcslen(ServiceName
) - 4] = 0;
155 /* Add it to the list */
156 Success
= WinLdrAddDriverToList(BootDriverListHead
,
157 L
"\\Registry\\Machine\\System\\CurrentControlSet\\Services\\",
162 ERR("Could not add boot driver '%s', '%s'\n", SearchPath
, DriverName
);
167 } while (InfFindNextLine(&InfContext
, &InfContext
));
171 /* SETUP STARTER **************************************************************/
181 PCSTR SystemPartition
;
183 CHAR FileName
[MAX_PATH
];
184 CHAR BootPath
[MAX_PATH
];
185 CHAR BootOptions2
[256];
188 BOOLEAN BootFromFloppy
;
192 INFCONTEXT InfContext
;
193 PLOADER_PARAMETER_BLOCK LoaderBlock
;
194 PSETUP_LOADER_BLOCK SetupBlock
;
197 static PCSTR SourcePaths
[] =
199 "", /* Only for floppy boot */
202 #elif defined(_M_MPPC)
204 #elif defined(_M_MRX000)
211 /* Retrieve the (mandatory) system partition */
212 SystemPartition
= GetArgumentValue(Argc
, Argv
, "SystemPartition");
213 if (!SystemPartition
|| !*SystemPartition
)
215 ERR("No 'SystemPartition' specified, aborting!\n");
219 UiDrawStatusText("Setup is loading...");
222 UiDrawProgressBarCenter(1, 100, "Loading ReactOS Setup...");
224 /* Retrieve the system path */
225 *BootPath
= ANSI_NULL
;
226 ArgValue
= GetArgumentValue(Argc
, Argv
, "SystemPath");
229 RtlStringCbCopyA(BootPath
, sizeof(BootPath
), ArgValue
);
234 * IMPROVE: I don't want to use the SystemPartition here as a
235 * default choice because I can do it after (see few lines below).
236 * Instead I reset BootPath here so that we can build the full path
237 * using the general code from below.
239 // RtlStringCbCopyA(BootPath, sizeof(BootPath), SystemPartition);
240 *BootPath
= ANSI_NULL
;
244 * Check whether BootPath is a full path
245 * and if not, create a full boot path.
247 * See FsOpenFile for the technique used.
249 if (strrchr(BootPath
, ')') == NULL
)
251 /* Temporarily save the boot path */
252 RtlStringCbCopyA(FileName
, sizeof(FileName
), BootPath
);
254 /* This is not a full path: prepend the SystemPartition */
255 RtlStringCbCopyA(BootPath
, sizeof(BootPath
), SystemPartition
);
257 /* Append a path separator if needed */
258 if (*FileName
!= '\\' && *FileName
!= '/')
259 RtlStringCbCatA(BootPath
, sizeof(BootPath
), "\\");
261 /* Append the remaining path */
262 RtlStringCbCatA(BootPath
, sizeof(BootPath
), FileName
);
265 /* Append a path separator if needed */
266 if (!*BootPath
|| BootPath
[strlen(BootPath
) - 1] != '\\')
267 RtlStringCbCatA(BootPath
, sizeof(BootPath
), "\\");
269 TRACE("BootPath: '%s'\n", BootPath
);
271 /* Retrieve the boot options */
272 *BootOptions2
= ANSI_NULL
;
273 ArgValue
= GetArgumentValue(Argc
, Argv
, "Options");
274 if (ArgValue
&& *ArgValue
)
275 RtlStringCbCopyA(BootOptions2
, sizeof(BootOptions2
), ArgValue
);
277 TRACE("BootOptions: '%s'\n", BootOptions2
);
279 /* Check if a ramdisk file was given */
280 File
= strstr(BootOptions2
, "/RDPATH=");
283 /* Copy the file name and everything else after it */
284 RtlStringCbCopyA(FileName
, sizeof(FileName
), File
+ 8);
287 *strstr(FileName
, " ") = ANSI_NULL
;
289 /* Load the ramdisk */
290 Status
= RamDiskLoadVirtualFile(FileName
, SystemPartition
);
291 if (Status
!= ESUCCESS
)
293 UiMessageBox("Failed to load RAM disk file %s", FileName
);
298 /* Check if we booted from floppy */
299 BootFromFloppy
= strstr(BootPath
, "fdisk") != NULL
;
301 /* Open 'txtsetup.sif' from any of source paths */
302 File
= BootPath
+ strlen(BootPath
);
303 for (i
= BootFromFloppy
? 0 : 1; ; i
++)
305 SystemPath
= SourcePaths
[i
];
308 UiMessageBox("Failed to open txtsetup.sif");
311 RtlStringCbCopyA(File
, sizeof(BootPath
) - (File
- BootPath
)*sizeof(CHAR
), SystemPath
);
312 RtlStringCbCopyA(FileName
, sizeof(FileName
), BootPath
);
313 RtlStringCbCatA(FileName
, sizeof(FileName
), "txtsetup.sif");
314 if (InfOpenFile(&InfHandle
, FileName
, &ErrorLine
))
320 TRACE("BootPath: '%s', SystemPath: '%s'\n", BootPath
, SystemPath
);
322 /* Get Load options - debug and non-debug */
323 if (!InfFindFirstLine(InfHandle
, "SetupData", "OsLoadOptions", &InfContext
))
325 ERR("Failed to find 'SetupData/OsLoadOptions'\n");
329 if (!InfGetDataField(&InfContext
, 1, &LoadOptions
))
331 ERR("Failed to get load options\n");
336 /* Get debug load options and use them */
337 if (InfFindFirstLine(InfHandle
, "SetupData", "DbgOsLoadOptions", &InfContext
))
339 PCSTR DbgLoadOptions
;
341 if (InfGetDataField(&InfContext
, 1, &DbgLoadOptions
))
342 LoadOptions
= DbgLoadOptions
;
346 /* Copy LoadOptions (original string will be freed) */
347 BootOptions
= FrLdrTempAlloc(strlen(LoadOptions
) + 1, TAG_BOOT_OPTIONS
);
349 strcpy(BootOptions
, LoadOptions
);
351 TRACE("BootOptions: '%s'\n", BootOptions
);
353 /* Allocate and minimalist-initialize LPB */
354 AllocateAndInitLPB(&LoaderBlock
);
356 /* Allocate and initialize setup loader block */
357 SetupBlock
= &WinLdrSystemBlock
->SetupBlock
;
358 LoaderBlock
->SetupLdrBlock
= SetupBlock
;
360 /* Set textmode setup flag */
361 SetupBlock
->Flags
= SETUPLDR_TEXT_MODE
;
363 /* Load the system hive "setupreg.hiv" for setup */
365 UiDrawProgressBarCenter(15, 100, "Loading setup system hive...");
366 Success
= WinLdrInitSystemHive(LoaderBlock
, BootPath
, TRUE
);
367 TRACE("Setup SYSTEM hive %s\n", (Success
? "loaded" : "not loaded"));
368 /* Bail out if failure */
372 /* Load NLS data, they are in the System32 directory of the installation medium */
373 RtlStringCbCopyA(FileName
, sizeof(FileName
), BootPath
);
374 RtlStringCbCatA(FileName
, sizeof(FileName
), "system32\\");
375 SetupLdrLoadNlsData(LoaderBlock
, InfHandle
, FileName
);
377 /* Load the Firmware Errata file from the installation medium */
378 Success
= SetupLdrInitErrataInf(InfHandle
, BootPath
);
379 TRACE("Firmware Errata file %s\n", (Success
? "loaded" : "not loaded"));
380 /* Not necessarily fatal if not found - carry on going */
382 // UiDrawStatusText("Press F6 if you need to install a 3rd-party SCSI or RAID driver...");
384 /* Get a list of boot drivers */
385 SetupLdrScanBootDrivers(&LoaderBlock
->BootDriverListHead
, InfHandle
, BootPath
);
387 /* Close the inf file */
388 InfCloseFile(InfHandle
);
390 UiDrawStatusText("The Setup program is starting...");
392 /* Load ReactOS Setup */
393 return LoadAndBootWindowsCommon(_WIN32_WINNT_WS03
,