[FREELDR] Load the Firmware Errata file specified in the registry. (#1951)
[reactos.git] / boot / freeldr / freeldr / ntldr / setupldr.c
1 /*
2 * PROJECT: FreeLoader
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>
6 */
7
8 #include <freeldr.h>
9 #include <ndk/ldrtypes.h>
10 #include <arc/setupblk.h>
11 #include "winldr.h"
12 #include "inffile.h"
13
14 #include <debug.h>
15 DBG_DEFAULT_CHANNEL(WINDOWS);
16
17 #define TAG_BOOT_OPTIONS 'pOtB'
18
19 // TODO: Move to .h
20 VOID AllocateAndInitLPB(PLOADER_PARAMETER_BLOCK *OutLoaderBlock);
21
22 static VOID
23 SetupLdrLoadNlsData(PLOADER_PARAMETER_BLOCK LoaderBlock, HINF InfHandle, PCSTR SearchPath)
24 {
25 INFCONTEXT InfContext;
26 PCSTR AnsiName, OemName, LangName;
27
28 /* Get ANSI codepage file */
29 if (!InfFindFirstLine(InfHandle, "NLS", "AnsiCodepage", &InfContext))
30 {
31 ERR("Failed to find 'NLS/AnsiCodepage'\n");
32 return;
33 }
34 if (!InfGetDataField(&InfContext, 1, &AnsiName))
35 {
36 ERR("Failed to get load options\n");
37 return;
38 }
39
40 /* Get OEM codepage file */
41 if (!InfFindFirstLine(InfHandle, "NLS", "OemCodepage", &InfContext))
42 {
43 ERR("Failed to find 'NLS/AnsiCodepage'\n");
44 return;
45 }
46 if (!InfGetDataField(&InfContext, 1, &OemName))
47 {
48 ERR("Failed to get load options\n");
49 return;
50 }
51
52 if (!InfFindFirstLine(InfHandle, "NLS", "UnicodeCasetable", &InfContext))
53 {
54 ERR("Failed to find 'NLS/AnsiCodepage'\n");
55 return;
56 }
57 if (!InfGetDataField(&InfContext, 1, &LangName))
58 {
59 ERR("Failed to get load options\n");
60 return;
61 }
62
63 TRACE("NLS data '%s' '%s' '%s'\n", AnsiName, OemName, LangName);
64
65 #if DBG
66 {
67 BOOLEAN Success = WinLdrLoadNLSData(LoaderBlock, SearchPath, AnsiName, OemName, LangName);
68 (VOID)Success;
69 TRACE("NLS data loading %s\n", Success ? "successful" : "failed");
70 }
71 #else
72 WinLdrLoadNLSData(LoaderBlock, SearchPath, AnsiName, OemName, LangName);
73 #endif
74
75 /* TODO: Load OEM HAL font */
76 // Value "OemHalFont"
77 }
78
79 static
80 BOOLEAN
81 SetupLdrInitErrataInf(
82 IN HINF InfHandle,
83 IN PCSTR SystemRoot)
84 {
85 INFCONTEXT InfContext;
86 PCSTR FileName;
87 ULONG FileSize;
88 PVOID PhysicalBase;
89 CHAR ErrataFilePath[MAX_PATH];
90
91 /* Retrieve the INF file name value */
92 if (!InfFindFirstLine(InfHandle, "BiosInfo", "InfName", &InfContext))
93 {
94 WARN("Failed to find 'BiosInfo/InfName'\n");
95 return FALSE;
96 }
97 if (!InfGetDataField(&InfContext, 1, &FileName))
98 {
99 WARN("Failed to read 'InfName' value\n");
100 return FALSE;
101 }
102
103 RtlStringCbCopyA(ErrataFilePath, sizeof(ErrataFilePath), SystemRoot);
104 RtlStringCbCatA(ErrataFilePath, sizeof(ErrataFilePath), FileName);
105
106 /* Load the INF file */
107 PhysicalBase = WinLdrLoadModule(ErrataFilePath, &FileSize, LoaderRegistryData);
108 if (!PhysicalBase)
109 {
110 WARN("Could not load '%s'\n", ErrataFilePath);
111 return FALSE;
112 }
113
114 WinLdrSystemBlock->Extension.EmInfFileImage = PaToVa(PhysicalBase);
115 WinLdrSystemBlock->Extension.EmInfFileSize = FileSize;
116
117 return TRUE;
118 }
119
120 static VOID
121 SetupLdrScanBootDrivers(PLIST_ENTRY BootDriverListHead, HINF InfHandle, PCSTR SearchPath)
122 {
123 INFCONTEXT InfContext, dirContext;
124 BOOLEAN Success;
125 PCSTR Media, DriverName, dirIndex, ImagePath;
126 WCHAR ServiceName[256];
127 WCHAR ImagePathW[256];
128
129 /* Open inf section */
130 if (!InfFindFirstLine(InfHandle, "SourceDisksFiles", NULL, &InfContext))
131 return;
132
133 /* Load all listed boot drivers */
134 do
135 {
136 if (InfGetDataField(&InfContext, 7, &Media) &&
137 InfGetDataField(&InfContext, 0, &DriverName) &&
138 InfGetDataField(&InfContext, 13, &dirIndex))
139 {
140 if ((strcmp(Media, "x") == 0) &&
141 InfFindFirstLine(InfHandle, "Directories", dirIndex, &dirContext) &&
142 InfGetDataField(&dirContext, 1, &ImagePath))
143 {
144 /* Convert name to widechar */
145 swprintf(ServiceName, L"%S", DriverName);
146
147 /* Prepare image path */
148 swprintf(ImagePathW, L"%S", ImagePath);
149 wcscat(ImagePathW, L"\\");
150 wcscat(ImagePathW, ServiceName);
151
152 /* Remove .sys extension */
153 ServiceName[wcslen(ServiceName) - 4] = 0;
154
155 /* Add it to the list */
156 Success = WinLdrAddDriverToList(BootDriverListHead,
157 L"\\Registry\\Machine\\System\\CurrentControlSet\\Services\\",
158 ImagePathW,
159 ServiceName);
160 if (!Success)
161 {
162 ERR("Could not add boot driver '%s', '%s'\n", SearchPath, DriverName);
163 return;
164 }
165 }
166 }
167 } while (InfFindNextLine(&InfContext, &InfContext));
168 }
169
170
171 /* SETUP STARTER **************************************************************/
172
173 ARC_STATUS
174 LoadReactOSSetup(
175 IN ULONG Argc,
176 IN PCHAR Argv[],
177 IN PCHAR Envp[])
178 {
179 ARC_STATUS Status;
180 PCSTR ArgValue;
181 PCSTR SystemPartition;
182 PCHAR File;
183 CHAR FileName[MAX_PATH];
184 CHAR BootPath[MAX_PATH];
185 CHAR BootOptions2[256];
186 PCSTR LoadOptions;
187 PSTR BootOptions;
188 BOOLEAN BootFromFloppy;
189 BOOLEAN Success;
190 ULONG i, ErrorLine;
191 HINF InfHandle;
192 INFCONTEXT InfContext;
193 PLOADER_PARAMETER_BLOCK LoaderBlock;
194 PSETUP_LOADER_BLOCK SetupBlock;
195 PCSTR SystemPath;
196
197 static PCSTR SourcePaths[] =
198 {
199 "", /* Only for floppy boot */
200 #if defined(_M_IX86)
201 "I386\\",
202 #elif defined(_M_MPPC)
203 "PPC\\",
204 #elif defined(_M_MRX000)
205 "MIPS\\",
206 #endif
207 "reactos\\",
208 NULL
209 };
210
211 /* Retrieve the (mandatory) system partition */
212 SystemPartition = GetArgumentValue(Argc, Argv, "SystemPartition");
213 if (!SystemPartition || !*SystemPartition)
214 {
215 ERR("No 'SystemPartition' specified, aborting!\n");
216 return EINVAL;
217 }
218
219 UiDrawStatusText("Setup is loading...");
220
221 UiDrawBackdrop();
222 UiDrawProgressBarCenter(1, 100, "Loading ReactOS Setup...");
223
224 /* Retrieve the system path */
225 *BootPath = ANSI_NULL;
226 ArgValue = GetArgumentValue(Argc, Argv, "SystemPath");
227 if (ArgValue)
228 {
229 RtlStringCbCopyA(BootPath, sizeof(BootPath), ArgValue);
230 }
231 else
232 {
233 /*
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.
238 */
239 // RtlStringCbCopyA(BootPath, sizeof(BootPath), SystemPartition);
240 *BootPath = ANSI_NULL;
241 }
242
243 /*
244 * Check whether BootPath is a full path
245 * and if not, create a full boot path.
246 *
247 * See FsOpenFile for the technique used.
248 */
249 if (strrchr(BootPath, ')') == NULL)
250 {
251 /* Temporarily save the boot path */
252 RtlStringCbCopyA(FileName, sizeof(FileName), BootPath);
253
254 /* This is not a full path: prepend the SystemPartition */
255 RtlStringCbCopyA(BootPath, sizeof(BootPath), SystemPartition);
256
257 /* Append a path separator if needed */
258 if (*FileName != '\\' && *FileName != '/')
259 RtlStringCbCatA(BootPath, sizeof(BootPath), "\\");
260
261 /* Append the remaining path */
262 RtlStringCbCatA(BootPath, sizeof(BootPath), FileName);
263 }
264
265 /* Append a path separator if needed */
266 if (!*BootPath || BootPath[strlen(BootPath) - 1] != '\\')
267 RtlStringCbCatA(BootPath, sizeof(BootPath), "\\");
268
269 TRACE("BootPath: '%s'\n", BootPath);
270
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);
276
277 TRACE("BootOptions: '%s'\n", BootOptions2);
278
279 /* Check if a ramdisk file was given */
280 File = strstr(BootOptions2, "/RDPATH=");
281 if (File)
282 {
283 /* Copy the file name and everything else after it */
284 RtlStringCbCopyA(FileName, sizeof(FileName), File + 8);
285
286 /* Null-terminate */
287 *strstr(FileName, " ") = ANSI_NULL;
288
289 /* Load the ramdisk */
290 Status = RamDiskLoadVirtualFile(FileName, SystemPartition);
291 if (Status != ESUCCESS)
292 {
293 UiMessageBox("Failed to load RAM disk file %s", FileName);
294 return Status;
295 }
296 }
297
298 /* Check if we booted from floppy */
299 BootFromFloppy = strstr(BootPath, "fdisk") != NULL;
300
301 /* Open 'txtsetup.sif' from any of source paths */
302 File = BootPath + strlen(BootPath);
303 for (i = BootFromFloppy ? 0 : 1; ; i++)
304 {
305 SystemPath = SourcePaths[i];
306 if (!SystemPath)
307 {
308 UiMessageBox("Failed to open txtsetup.sif");
309 return ENOENT;
310 }
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))
315 {
316 break;
317 }
318 }
319
320 TRACE("BootPath: '%s', SystemPath: '%s'\n", BootPath, SystemPath);
321
322 /* Get Load options - debug and non-debug */
323 if (!InfFindFirstLine(InfHandle, "SetupData", "OsLoadOptions", &InfContext))
324 {
325 ERR("Failed to find 'SetupData/OsLoadOptions'\n");
326 return EINVAL;
327 }
328
329 if (!InfGetDataField(&InfContext, 1, &LoadOptions))
330 {
331 ERR("Failed to get load options\n");
332 return EINVAL;
333 }
334
335 #if DBG
336 /* Get debug load options and use them */
337 if (InfFindFirstLine(InfHandle, "SetupData", "DbgOsLoadOptions", &InfContext))
338 {
339 PCSTR DbgLoadOptions;
340
341 if (InfGetDataField(&InfContext, 1, &DbgLoadOptions))
342 LoadOptions = DbgLoadOptions;
343 }
344 #endif
345
346 /* Copy LoadOptions (original string will be freed) */
347 BootOptions = FrLdrTempAlloc(strlen(LoadOptions) + 1, TAG_BOOT_OPTIONS);
348 ASSERT(BootOptions);
349 strcpy(BootOptions, LoadOptions);
350
351 TRACE("BootOptions: '%s'\n", BootOptions);
352
353 /* Allocate and minimalist-initialize LPB */
354 AllocateAndInitLPB(&LoaderBlock);
355
356 /* Allocate and initialize setup loader block */
357 SetupBlock = &WinLdrSystemBlock->SetupBlock;
358 LoaderBlock->SetupLdrBlock = SetupBlock;
359
360 /* Set textmode setup flag */
361 SetupBlock->Flags = SETUPLDR_TEXT_MODE;
362
363 /* Load the system hive "setupreg.hiv" for setup */
364 UiDrawBackdrop();
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 */
369 if (!Success)
370 return ENOEXEC;
371
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);
376
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 */
381
382 // UiDrawStatusText("Press F6 if you need to install a 3rd-party SCSI or RAID driver...");
383
384 /* Get a list of boot drivers */
385 SetupLdrScanBootDrivers(&LoaderBlock->BootDriverListHead, InfHandle, BootPath);
386
387 /* Close the inf file */
388 InfCloseFile(InfHandle);
389
390 UiDrawStatusText("The Setup program is starting...");
391
392 /* Load ReactOS Setup */
393 return LoadAndBootWindowsCommon(_WIN32_WINNT_WS03,
394 LoaderBlock,
395 BootOptions,
396 BootPath,
397 TRUE);
398 }