81e76feca2c60d8fa79e16312e4603bdb00b681c
[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 ARC_STATUS Status;
139 PCSTR ArgValue;
140 PCSTR SystemPartition;
141 PCHAR File;
142 CHAR FileName[MAX_PATH];
143 CHAR BootPath[MAX_PATH];
144 CHAR BootOptions2[256];
145 PCSTR LoadOptions;
146 PSTR BootOptions;
147 BOOLEAN BootFromFloppy;
148 BOOLEAN Success;
149 ULONG i, ErrorLine;
150 HINF InfHandle;
151 INFCONTEXT InfContext;
152 PLOADER_PARAMETER_BLOCK LoaderBlock;
153 PSETUP_LOADER_BLOCK SetupBlock;
154 PCSTR SystemPath;
155
156 static PCSTR SourcePaths[] =
157 {
158 "", /* Only for floppy boot */
159 #if defined(_M_IX86)
160 "I386\\",
161 #elif defined(_M_MPPC)
162 "PPC\\",
163 #elif defined(_M_MRX000)
164 "MIPS\\",
165 #endif
166 "reactos\\",
167 NULL
168 };
169
170 /* Retrieve the (mandatory) system partition */
171 SystemPartition = GetArgumentValue(Argc, Argv, "SystemPartition");
172 if (!SystemPartition || !*SystemPartition)
173 {
174 ERR("No 'SystemPartition' specified, aborting!\n");
175 return EINVAL;
176 }
177
178 UiDrawStatusText("Setup is loading...");
179
180 UiDrawBackdrop();
181 UiDrawProgressBarCenter(1, 100, "Loading ReactOS Setup...");
182
183 /* Retrieve the system path */
184 *BootPath = ANSI_NULL;
185 ArgValue = GetArgumentValue(Argc, Argv, "SystemPath");
186 if (ArgValue)
187 {
188 RtlStringCbCopyA(BootPath, sizeof(BootPath), ArgValue);
189 }
190 else
191 {
192 /*
193 * IMPROVE: I don't want to use the SystemPartition here as a
194 * default choice because I can do it after (see few lines below).
195 * Instead I reset BootPath here so that we can build the full path
196 * using the general code from below.
197 */
198 // RtlStringCbCopyA(BootPath, sizeof(BootPath), SystemPartition);
199 *BootPath = ANSI_NULL;
200 }
201
202 /*
203 * Check whether BootPath is a full path
204 * and if not, create a full boot path.
205 *
206 * See FsOpenFile for the technique used.
207 */
208 if (strrchr(BootPath, ')') == NULL)
209 {
210 /* Temporarily save the boot path */
211 RtlStringCbCopyA(FileName, sizeof(FileName), BootPath);
212
213 /* This is not a full path: prepend the SystemPartition */
214 RtlStringCbCopyA(BootPath, sizeof(BootPath), SystemPartition);
215
216 /* Append a path separator if needed */
217 if (*FileName != '\\' && *FileName != '/')
218 RtlStringCbCatA(BootPath, sizeof(BootPath), "\\");
219
220 /* Append the remaining path */
221 RtlStringCbCatA(BootPath, sizeof(BootPath), FileName);
222 }
223
224 /* Append a path separator if needed */
225 if (!*BootPath || BootPath[strlen(BootPath) - 1] != '\\')
226 RtlStringCbCatA(BootPath, sizeof(BootPath), "\\");
227
228 TRACE("BootPath: '%s'\n", BootPath);
229
230 /* Retrieve the boot options */
231 *BootOptions2 = ANSI_NULL;
232 ArgValue = GetArgumentValue(Argc, Argv, "Options");
233 if (ArgValue && *ArgValue)
234 RtlStringCbCopyA(BootOptions2, sizeof(BootOptions2), ArgValue);
235
236 TRACE("BootOptions: '%s'\n", BootOptions2);
237
238 /* Check if a ramdisk file was given */
239 File = strstr(BootOptions2, "/RDPATH=");
240 if (File)
241 {
242 /* Copy the file name and everything else after it */
243 RtlStringCbCopyA(FileName, sizeof(FileName), File + 8);
244
245 /* Null-terminate */
246 *strstr(FileName, " ") = ANSI_NULL;
247
248 /* Load the ramdisk */
249 Status = RamDiskLoadVirtualFile(FileName, SystemPartition);
250 if (Status != ESUCCESS)
251 {
252 UiMessageBox("Failed to load RAM disk file %s", FileName);
253 return Status;
254 }
255 }
256
257 /* Check if we booted from floppy */
258 BootFromFloppy = strstr(BootPath, "fdisk") != NULL;
259
260 /* Open 'txtsetup.sif' from any of source paths */
261 File = BootPath + strlen(BootPath);
262 for (i = BootFromFloppy ? 0 : 1; ; i++)
263 {
264 SystemPath = SourcePaths[i];
265 if (!SystemPath)
266 {
267 UiMessageBox("Failed to open txtsetup.sif");
268 return ENOENT;
269 }
270 RtlStringCbCopyA(File, sizeof(BootPath) - (File - BootPath)*sizeof(CHAR), SystemPath);
271 RtlStringCbCopyA(FileName, sizeof(FileName), BootPath);
272 RtlStringCbCatA(FileName, sizeof(FileName), "txtsetup.sif");
273 if (InfOpenFile(&InfHandle, FileName, &ErrorLine))
274 {
275 break;
276 }
277 }
278
279 TRACE("BootPath: '%s', SystemPath: '%s'\n", BootPath, SystemPath);
280
281 /* Get Load options - debug and non-debug */
282 if (!InfFindFirstLine(InfHandle, "SetupData", "OsLoadOptions", &InfContext))
283 {
284 ERR("Failed to find 'SetupData/OsLoadOptions'\n");
285 return EINVAL;
286 }
287
288 if (!InfGetDataField(&InfContext, 1, &LoadOptions))
289 {
290 ERR("Failed to get load options\n");
291 return EINVAL;
292 }
293
294 #if DBG
295 /* Get debug load options and use them */
296 if (InfFindFirstLine(InfHandle, "SetupData", "DbgOsLoadOptions", &InfContext))
297 {
298 PCSTR DbgLoadOptions;
299
300 if (InfGetDataField(&InfContext, 1, &DbgLoadOptions))
301 LoadOptions = DbgLoadOptions;
302 }
303 #endif
304
305 /* Copy LoadOptions (original string will be freed) */
306 BootOptions = FrLdrTempAlloc(strlen(LoadOptions) + 1, TAG_BOOT_OPTIONS);
307 ASSERT(BootOptions);
308 strcpy(BootOptions, LoadOptions);
309
310 TRACE("BootOptions: '%s'\n", BootOptions);
311
312 /* Allocate and minimalist-initialize LPB */
313 AllocateAndInitLPB(&LoaderBlock);
314
315 /* Allocate and initialize setup loader block */
316 SetupBlock = &WinLdrSystemBlock->SetupBlock;
317 LoaderBlock->SetupLdrBlock = SetupBlock;
318
319 /* Set textmode setup flag */
320 SetupBlock->Flags = SETUPLDR_TEXT_MODE;
321
322 /* Load the system hive "setupreg.hiv" for setup */
323 UiDrawBackdrop();
324 UiDrawProgressBarCenter(15, 100, "Loading setup system hive...");
325 Success = WinLdrInitSystemHive(LoaderBlock, BootPath, TRUE);
326 TRACE("Setup SYSTEM hive %s\n", (Success ? "loaded" : "not loaded"));
327 /* Bail out if failure */
328 if (!Success)
329 return ENOEXEC;
330
331 /* Load NLS data, they are in the System32 directory of the installation medium */
332 RtlStringCbCopyA(FileName, sizeof(FileName), BootPath);
333 RtlStringCbCatA(FileName, sizeof(FileName), "system32\\");
334 SetupLdrLoadNlsData(LoaderBlock, InfHandle, FileName);
335
336 // UiDrawStatusText("Press F6 if you need to install a 3rd-party SCSI or RAID driver...");
337
338 /* Get a list of boot drivers */
339 SetupLdrScanBootDrivers(&LoaderBlock->BootDriverListHead, InfHandle, BootPath);
340
341 /* Close the inf file */
342 InfCloseFile(InfHandle);
343
344 UiDrawStatusText("The Setup program is starting...");
345
346 /* Load ReactOS Setup */
347 return LoadAndBootWindowsCommon(_WIN32_WINNT_WS03,
348 LoaderBlock,
349 BootOptions,
350 BootPath,
351 TRUE);
352 }