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 */
80 SetupLdrScanBootDrivers(PLIST_ENTRY BootDriverListHead
, HINF InfHandle
, PCSTR SearchPath
)
82 INFCONTEXT InfContext
, dirContext
;
84 PCSTR Media
, DriverName
, dirIndex
, ImagePath
;
85 WCHAR ServiceName
[256];
86 WCHAR ImagePathW
[256];
88 /* Open inf section */
89 if (!InfFindFirstLine(InfHandle
, "SourceDisksFiles", NULL
, &InfContext
))
92 /* Load all listed boot drivers */
95 if (InfGetDataField(&InfContext
, 7, &Media
) &&
96 InfGetDataField(&InfContext
, 0, &DriverName
) &&
97 InfGetDataField(&InfContext
, 13, &dirIndex
))
99 if ((strcmp(Media
, "x") == 0) &&
100 InfFindFirstLine(InfHandle
, "Directories", dirIndex
, &dirContext
) &&
101 InfGetDataField(&dirContext
, 1, &ImagePath
))
103 /* Convert name to widechar */
104 swprintf(ServiceName
, L
"%S", DriverName
);
106 /* Prepare image path */
107 swprintf(ImagePathW
, L
"%S", ImagePath
);
108 wcscat(ImagePathW
, L
"\\");
109 wcscat(ImagePathW
, ServiceName
);
111 /* Remove .sys extension */
112 ServiceName
[wcslen(ServiceName
) - 4] = 0;
114 /* Add it to the list */
115 Success
= WinLdrAddDriverToList(BootDriverListHead
,
116 L
"\\Registry\\Machine\\System\\CurrentControlSet\\Services\\",
121 ERR("Could not add boot driver '%s', '%s'\n", SearchPath
, DriverName
);
126 } while (InfFindNextLine(&InfContext
, &InfContext
));
130 /* SETUP STARTER **************************************************************/
140 PCSTR SystemPartition
;
142 CHAR FileName
[MAX_PATH
];
143 CHAR BootPath
[MAX_PATH
];
144 CHAR BootOptions2
[256];
147 BOOLEAN BootFromFloppy
;
151 INFCONTEXT InfContext
;
152 PLOADER_PARAMETER_BLOCK LoaderBlock
;
153 PSETUP_LOADER_BLOCK SetupBlock
;
156 static PCSTR SourcePaths
[] =
158 "", /* Only for floppy boot */
161 #elif defined(_M_MPPC)
163 #elif defined(_M_MRX000)
170 /* Retrieve the (mandatory) system partition */
171 SystemPartition
= GetArgumentValue(Argc
, Argv
, "SystemPartition");
172 if (!SystemPartition
|| !*SystemPartition
)
174 ERR("No 'SystemPartition' specified, aborting!\n");
178 UiDrawStatusText("Setup is loading...");
181 UiDrawProgressBarCenter(1, 100, "Loading ReactOS Setup...");
183 /* Retrieve the system path */
184 *BootPath
= ANSI_NULL
;
185 ArgValue
= GetArgumentValue(Argc
, Argv
, "SystemPath");
188 RtlStringCbCopyA(BootPath
, sizeof(BootPath
), ArgValue
);
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.
198 // RtlStringCbCopyA(BootPath, sizeof(BootPath), SystemPartition);
199 *BootPath
= ANSI_NULL
;
203 * Check whether BootPath is a full path
204 * and if not, create a full boot path.
206 * See FsOpenFile for the technique used.
208 if (strrchr(BootPath
, ')') == NULL
)
210 /* Temporarily save the boot path */
211 RtlStringCbCopyA(FileName
, sizeof(FileName
), BootPath
);
213 /* This is not a full path: prepend the SystemPartition */
214 RtlStringCbCopyA(BootPath
, sizeof(BootPath
), SystemPartition
);
216 /* Append a path separator if needed */
217 if (*FileName
!= '\\' && *FileName
!= '/')
218 RtlStringCbCatA(BootPath
, sizeof(BootPath
), "\\");
220 /* Append the remaining path */
221 RtlStringCbCatA(BootPath
, sizeof(BootPath
), FileName
);
224 /* Append a path separator if needed */
225 if (!*BootPath
|| BootPath
[strlen(BootPath
) - 1] != '\\')
226 RtlStringCbCatA(BootPath
, sizeof(BootPath
), "\\");
228 TRACE("BootPath: '%s'\n", BootPath
);
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
);
236 TRACE("BootOptions: '%s'\n", BootOptions2
);
238 /* Check if a ramdisk file was given */
239 File
= strstr(BootOptions2
, "/RDPATH=");
242 /* Copy the file name and everything else after it */
243 RtlStringCbCopyA(FileName
, sizeof(FileName
), File
+ 8);
246 *strstr(FileName
, " ") = ANSI_NULL
;
248 /* Load the ramdisk */
249 Status
= RamDiskLoadVirtualFile(FileName
, SystemPartition
);
250 if (Status
!= ESUCCESS
)
252 UiMessageBox("Failed to load RAM disk file %s", FileName
);
257 /* Check if we booted from floppy */
258 BootFromFloppy
= strstr(BootPath
, "fdisk") != NULL
;
260 /* Open 'txtsetup.sif' from any of source paths */
261 File
= BootPath
+ strlen(BootPath
);
262 for (i
= BootFromFloppy
? 0 : 1; ; i
++)
264 SystemPath
= SourcePaths
[i
];
267 UiMessageBox("Failed to open txtsetup.sif");
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
))
279 TRACE("BootPath: '%s', SystemPath: '%s'\n", BootPath
, SystemPath
);
281 /* Get Load options - debug and non-debug */
282 if (!InfFindFirstLine(InfHandle
, "SetupData", "OsLoadOptions", &InfContext
))
284 ERR("Failed to find 'SetupData/OsLoadOptions'\n");
288 if (!InfGetDataField(&InfContext
, 1, &LoadOptions
))
290 ERR("Failed to get load options\n");
295 /* Get debug load options and use them */
296 if (InfFindFirstLine(InfHandle
, "SetupData", "DbgOsLoadOptions", &InfContext
))
298 PCSTR DbgLoadOptions
;
300 if (InfGetDataField(&InfContext
, 1, &DbgLoadOptions
))
301 LoadOptions
= DbgLoadOptions
;
305 /* Copy LoadOptions (original string will be freed) */
306 BootOptions
= FrLdrTempAlloc(strlen(LoadOptions
) + 1, TAG_BOOT_OPTIONS
);
308 strcpy(BootOptions
, LoadOptions
);
310 TRACE("BootOptions: '%s'\n", BootOptions
);
312 /* Allocate and minimalist-initialize LPB */
313 AllocateAndInitLPB(&LoaderBlock
);
315 /* Allocate and initialize setup loader block */
316 SetupBlock
= &WinLdrSystemBlock
->SetupBlock
;
317 LoaderBlock
->SetupLdrBlock
= SetupBlock
;
319 /* Set textmode setup flag */
320 SetupBlock
->Flags
= SETUPLDR_TEXT_MODE
;
322 /* Load the system hive "setupreg.hiv" for setup */
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 */
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
);
336 // UiDrawStatusText("Press F6 if you need to install a 3rd-party SCSI or RAID driver...");
338 /* Get a list of boot drivers */
339 SetupLdrScanBootDrivers(&LoaderBlock
->BootDriverListHead
, InfHandle
, BootPath
);
341 /* Close the inf file */
342 InfCloseFile(InfHandle
);
344 UiDrawStatusText("The Setup program is starting...");
346 /* Load ReactOS Setup */
347 return LoadAndBootWindowsCommon(_WIN32_WINNT_WS03
,