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 **************************************************************/
142 CHAR BootOptions2
[256];
145 BOOLEAN BootFromFloppy
;
149 INFCONTEXT InfContext
;
150 PLOADER_PARAMETER_BLOCK LoaderBlock
;
151 PSETUP_LOADER_BLOCK SetupBlock
;
154 static PCSTR SourcePaths
[] =
156 "", /* Only for floppy boot */
159 #elif defined(_M_MPPC)
161 #elif defined(_M_MRX000)
168 UiDrawStatusText("Setup is loading...");
171 UiDrawProgressBarCenter(1, 100, "Loading ReactOS Setup...");
173 /* Retrieve the system path */
174 *BootPath
= ANSI_NULL
;
175 ArgValue
= GetArgumentValue(Argc
, Argv
, "SystemPath");
178 RtlStringCbCopyA(BootPath
, sizeof(BootPath
), ArgValue
);
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.
188 // MachDiskGetBootPath(BootPath, sizeof(BootPath));
189 // RtlStringCbCopyA(BootPath, sizeof(BootPath), ArgValue);
190 *BootPath
= ANSI_NULL
;
194 * Check whether BootPath is a full path
195 * and if not, create a full boot path.
197 * See FsOpenFile for the technique used.
199 if (strrchr(BootPath
, ')') == NULL
)
201 /* Temporarily save the boot path */
202 RtlStringCbCopyA(FileName
, sizeof(FileName
), BootPath
);
204 /* This is not a full path. Use the current (i.e. boot) device. */
205 MachDiskGetBootPath(BootPath
, sizeof(BootPath
));
207 /* Append a path separator if needed */
208 if (*FileName
!= '\\' && *FileName
!= '/')
209 RtlStringCbCatA(BootPath
, sizeof(BootPath
), "\\");
211 /* Append the remaining path */
212 RtlStringCbCatA(BootPath
, sizeof(BootPath
), FileName
);
215 /* Append a backslash if needed */
216 if (!*BootPath
|| BootPath
[strlen(BootPath
) - 1] != '\\')
217 RtlStringCbCatA(BootPath
, sizeof(BootPath
), "\\");
219 TRACE("BootPath: '%s'\n", BootPath
);
221 /* Retrieve the boot options */
222 *BootOptions2
= ANSI_NULL
;
223 ArgValue
= GetArgumentValue(Argc
, Argv
, "Options");
225 RtlStringCbCopyA(BootOptions2
, sizeof(BootOptions2
), ArgValue
);
227 TRACE("BootOptions: '%s'\n", BootOptions2
);
229 /* Check if a ramdisk file was given */
230 File
= strstr(BootOptions2
, "/RDPATH=");
233 /* Copy the file name and everything else after it */
234 RtlStringCbCopyA(FileName
, sizeof(FileName
), File
+ 8);
237 *strstr(FileName
, " ") = ANSI_NULL
;
239 /* Load the ramdisk */
240 if (!RamDiskLoadVirtualFile(FileName
))
242 UiMessageBox("Failed to load RAM disk file %s", FileName
);
247 /* Check if we booted from floppy */
248 BootFromFloppy
= strstr(BootPath
, "fdisk") != NULL
;
250 /* Open 'txtsetup.sif' from any of source paths */
251 File
= BootPath
+ strlen(BootPath
);
252 for (i
= BootFromFloppy
? 0 : 1; ; i
++)
254 SystemPath
= SourcePaths
[i
];
257 UiMessageBox("Failed to open txtsetup.sif");
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
))
269 TRACE("BootPath: '%s', SystemPath: '%s'\n", BootPath
, SystemPath
);
271 /* Get Load options - debug and non-debug */
272 if (!InfFindFirstLine(InfHandle
, "SetupData", "OsLoadOptions", &InfContext
))
274 ERR("Failed to find 'SetupData/OsLoadOptions'\n");
278 if (!InfGetDataField(&InfContext
, 1, &LoadOptions
))
280 ERR("Failed to get load options\n");
285 /* Get debug load options and use them */
286 if (InfFindFirstLine(InfHandle
, "SetupData", "DbgOsLoadOptions", &InfContext
))
288 PCSTR DbgLoadOptions
;
290 if (InfGetDataField(&InfContext
, 1, &DbgLoadOptions
))
291 LoadOptions
= DbgLoadOptions
;
295 /* Copy loadoptions (original string will be freed) */
296 BootOptions
= FrLdrTempAlloc(strlen(LoadOptions
) + 1, TAG_BOOT_OPTIONS
);
298 strcpy(BootOptions
, LoadOptions
);
300 TRACE("BootOptions: '%s'\n", BootOptions
);
302 /* Allocate and minimalist-initialize LPB */
303 AllocateAndInitLPB(&LoaderBlock
);
305 /* Allocate and initialize setup loader block */
306 SetupBlock
= &WinLdrSystemBlock
->SetupBlock
;
307 LoaderBlock
->SetupLdrBlock
= SetupBlock
;
309 /* Set textmode setup flag */
310 SetupBlock
->Flags
= SETUPLDR_TEXT_MODE
;
312 /* Load the system hive "setupreg.hiv" for setup */
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 */
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
);
326 // UiDrawStatusText("Press F6 if you need to install a 3rd-party SCSI or RAID driver...");
328 /* Get a list of boot drivers */
329 SetupLdrScanBootDrivers(&LoaderBlock
->BootDriverListHead
, InfHandle
, BootPath
);
331 /* Close the inf file */
332 InfCloseFile(InfHandle
);
334 UiDrawStatusText("The Setup program is starting...");
336 /* Load ReactOS Setup */
337 return LoadAndBootWindowsCommon(_WIN32_WINNT_WS03
,