[FREELDR][ARM] Suppress error about unused var
[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 VOID
80 SetupLdrScanBootDrivers(PLIST_ENTRY BootDriverListHead, HINF InfHandle, PCSTR SearchPath)
81 {
82 INFCONTEXT InfContext, dirContext;
83 BOOLEAN Success;
84 PCSTR Media, DriverName, dirIndex, ImagePath;
85 WCHAR ServiceName[256];
86 WCHAR ImagePathW[256];
87
88 /* Open inf section */
89 if (!InfFindFirstLine(InfHandle, "SourceDisksFiles", NULL, &InfContext))
90 return;
91
92 /* Load all listed boot drivers */
93 do
94 {
95 if (InfGetDataField(&InfContext, 7, &Media) &&
96 InfGetDataField(&InfContext, 0, &DriverName) &&
97 InfGetDataField(&InfContext, 13, &dirIndex))
98 {
99 if ((strcmp(Media, "x") == 0) &&
100 InfFindFirstLine(InfHandle, "Directories", dirIndex, &dirContext) &&
101 InfGetDataField(&dirContext, 1, &ImagePath))
102 {
103 /* Convert name to widechar */
104 swprintf(ServiceName, L"%S", DriverName);
105
106 /* Prepare image path */
107 swprintf(ImagePathW, L"%S", ImagePath);
108 wcscat(ImagePathW, L"\\");
109 wcscat(ImagePathW, ServiceName);
110
111 /* Remove .sys extension */
112 ServiceName[wcslen(ServiceName) - 4] = 0;
113
114 /* Add it to the list */
115 Success = WinLdrAddDriverToList(BootDriverListHead,
116 L"\\Registry\\Machine\\System\\CurrentControlSet\\Services\\",
117 ImagePathW,
118 ServiceName);
119 if (!Success)
120 {
121 ERR("Could not add boot driver '%s', '%s'\n", SearchPath, DriverName);
122 return;
123 }
124 }
125 }
126 } while (InfFindNextLine(&InfContext, &InfContext));
127 }
128
129
130 /* SETUP STARTER **************************************************************/
131
132 ARC_STATUS
133 LoadReactOSSetup(
134 IN ULONG Argc,
135 IN PCHAR Argv[],
136 IN PCHAR Envp[])
137 {
138 PCSTR ArgValue;
139 PCHAR File;
140 CHAR FileName[512];
141 CHAR BootPath[512];
142 CHAR BootOptions2[256];
143 PCSTR LoadOptions;
144 PSTR BootOptions;
145 BOOLEAN BootFromFloppy;
146 BOOLEAN Success;
147 ULONG i, ErrorLine;
148 HINF InfHandle;
149 INFCONTEXT InfContext;
150 PLOADER_PARAMETER_BLOCK LoaderBlock;
151 PSETUP_LOADER_BLOCK SetupBlock;
152 PCSTR SystemPath;
153
154 static PCSTR SourcePaths[] =
155 {
156 "", /* Only for floppy boot */
157 #if defined(_M_IX86)
158 "I386\\",
159 #elif defined(_M_MPPC)
160 "PPC\\",
161 #elif defined(_M_MRX000)
162 "MIPS\\",
163 #endif
164 "reactos\\",
165 NULL
166 };
167
168 UiDrawStatusText("Setup is loading...");
169
170 UiDrawBackdrop();
171 UiDrawProgressBarCenter(1, 100, "Loading ReactOS Setup...");
172
173 /* Retrieve the system path */
174 *BootPath = ANSI_NULL;
175 ArgValue = GetArgumentValue(Argc, Argv, "SystemPath");
176 if (ArgValue)
177 {
178 RtlStringCbCopyA(BootPath, sizeof(BootPath), ArgValue);
179 }
180 else
181 {
182 /*
183 * IMPROVE: I don't want to call MachDiskGetBootPath here as a
184 * default choice because I can call it after (see few lines below).
185 * Instead I reset BootPath here so that we can build the full path
186 * using the general code from below.
187 */
188 // MachDiskGetBootPath(BootPath, sizeof(BootPath));
189 // RtlStringCbCopyA(BootPath, sizeof(BootPath), ArgValue);
190 *BootPath = ANSI_NULL;
191 }
192
193 /*
194 * Check whether BootPath is a full path
195 * and if not, create a full boot path.
196 *
197 * See FsOpenFile for the technique used.
198 */
199 if (strrchr(BootPath, ')') == NULL)
200 {
201 /* Temporarily save the boot path */
202 RtlStringCbCopyA(FileName, sizeof(FileName), BootPath);
203
204 /* This is not a full path. Use the current (i.e. boot) device. */
205 MachDiskGetBootPath(BootPath, sizeof(BootPath));
206
207 /* Append a path separator if needed */
208 if (*FileName != '\\' && *FileName != '/')
209 RtlStringCbCatA(BootPath, sizeof(BootPath), "\\");
210
211 /* Append the remaining path */
212 RtlStringCbCatA(BootPath, sizeof(BootPath), FileName);
213 }
214
215 /* Append a backslash if needed */
216 if (!*BootPath || BootPath[strlen(BootPath) - 1] != '\\')
217 RtlStringCbCatA(BootPath, sizeof(BootPath), "\\");
218
219 TRACE("BootPath: '%s'\n", BootPath);
220
221 /* Retrieve the boot options */
222 *BootOptions2 = ANSI_NULL;
223 ArgValue = GetArgumentValue(Argc, Argv, "Options");
224 if (ArgValue)
225 RtlStringCbCopyA(BootOptions2, sizeof(BootOptions2), ArgValue);
226
227 TRACE("BootOptions: '%s'\n", BootOptions2);
228
229 /* Check if a ramdisk file was given */
230 File = strstr(BootOptions2, "/RDPATH=");
231 if (File)
232 {
233 /* Copy the file name and everything else after it */
234 RtlStringCbCopyA(FileName, sizeof(FileName), File + 8);
235
236 /* Null-terminate */
237 *strstr(FileName, " ") = ANSI_NULL;
238
239 /* Load the ramdisk */
240 if (!RamDiskLoadVirtualFile(FileName))
241 {
242 UiMessageBox("Failed to load RAM disk file %s", FileName);
243 return ENOENT;
244 }
245 }
246
247 /* Check if we booted from floppy */
248 BootFromFloppy = strstr(BootPath, "fdisk") != NULL;
249
250 /* Open 'txtsetup.sif' from any of source paths */
251 File = BootPath + strlen(BootPath);
252 for (i = BootFromFloppy ? 0 : 1; ; i++)
253 {
254 SystemPath = SourcePaths[i];
255 if (!SystemPath)
256 {
257 UiMessageBox("Failed to open txtsetup.sif");
258 return ENOENT;
259 }
260 RtlStringCbCopyA(File, sizeof(BootPath) - (File - BootPath)*sizeof(CHAR), SystemPath);
261 RtlStringCbCopyA(FileName, sizeof(FileName), BootPath);
262 RtlStringCbCatA(FileName, sizeof(FileName), "txtsetup.sif");
263 if (InfOpenFile(&InfHandle, FileName, &ErrorLine))
264 {
265 break;
266 }
267 }
268
269 TRACE("BootPath: '%s', SystemPath: '%s'\n", BootPath, SystemPath);
270
271 /* Get Load options - debug and non-debug */
272 if (!InfFindFirstLine(InfHandle, "SetupData", "OsLoadOptions", &InfContext))
273 {
274 ERR("Failed to find 'SetupData/OsLoadOptions'\n");
275 return EINVAL;
276 }
277
278 if (!InfGetDataField(&InfContext, 1, &LoadOptions))
279 {
280 ERR("Failed to get load options\n");
281 return EINVAL;
282 }
283
284 #if DBG
285 /* Get debug load options and use them */
286 if (InfFindFirstLine(InfHandle, "SetupData", "DbgOsLoadOptions", &InfContext))
287 {
288 PCSTR DbgLoadOptions;
289
290 if (InfGetDataField(&InfContext, 1, &DbgLoadOptions))
291 LoadOptions = DbgLoadOptions;
292 }
293 #endif
294
295 /* Copy loadoptions (original string will be freed) */
296 BootOptions = FrLdrTempAlloc(strlen(LoadOptions) + 1, TAG_BOOT_OPTIONS);
297 ASSERT(BootOptions);
298 strcpy(BootOptions, LoadOptions);
299
300 TRACE("BootOptions: '%s'\n", BootOptions);
301
302 /* Allocate and minimalist-initialize LPB */
303 AllocateAndInitLPB(&LoaderBlock);
304
305 /* Allocate and initialize setup loader block */
306 SetupBlock = &WinLdrSystemBlock->SetupBlock;
307 LoaderBlock->SetupLdrBlock = SetupBlock;
308
309 /* Set textmode setup flag */
310 SetupBlock->Flags = SETUPLDR_TEXT_MODE;
311
312 /* Load the system hive "setupreg.hiv" for setup */
313 UiDrawBackdrop();
314 UiDrawProgressBarCenter(15, 100, "Loading setup system hive...");
315 Success = WinLdrInitSystemHive(LoaderBlock, BootPath, TRUE);
316 TRACE("Setup SYSTEM hive %s\n", (Success ? "loaded" : "not loaded"));
317 /* Bail out if failure */
318 if (!Success)
319 return ENOEXEC;
320
321 /* Load NLS data, they are in the System32 directory of the installation medium */
322 RtlStringCbCopyA(FileName, sizeof(FileName), BootPath);
323 RtlStringCbCatA(FileName, sizeof(FileName), "system32\\");
324 SetupLdrLoadNlsData(LoaderBlock, InfHandle, FileName);
325
326 // UiDrawStatusText("Press F6 if you need to install a 3rd-party SCSI or RAID driver...");
327
328 /* Get a list of boot drivers */
329 SetupLdrScanBootDrivers(&LoaderBlock->BootDriverListHead, InfHandle, BootPath);
330
331 /* Close the inf file */
332 InfCloseFile(InfHandle);
333
334 UiDrawStatusText("The Setup program is starting...");
335
336 /* Load ReactOS Setup */
337 return LoadAndBootWindowsCommon(_WIN32_WINNT_WS03,
338 LoaderBlock,
339 BootOptions,
340 BootPath,
341 TRUE);
342 }