[FREELDR] Use RtlStringCbPrintfA instead of sprintf
[reactos.git] / boot / freeldr / freeldr / ntldr / setupldr.c
1 /*
2 * FreeLoader
3 *
4 * Copyright (C) 2009 Aleksey Bragin <aleksey@reactos.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include <freeldr.h>
22 #include "winldr.h"
23
24 #include <ndk/ldrtypes.h>
25 #include <arc/setupblk.h>
26
27 #include <debug.h>
28
29 DBG_DEFAULT_CHANNEL(WINDOWS);
30 #define TAG_BOOT_OPTIONS 'pOtB'
31
32 // TODO: Move to .h
33 VOID AllocateAndInitLPB(PLOADER_PARAMETER_BLOCK *OutLoaderBlock);
34
35 static VOID
36 SetupLdrLoadNlsData(PLOADER_PARAMETER_BLOCK LoaderBlock, HINF InfHandle, LPCSTR SearchPath)
37 {
38 INFCONTEXT InfContext;
39 LPCSTR AnsiName, OemName, LangName;
40
41 /* Get ANSI codepage file */
42 if (!InfFindFirstLine(InfHandle, "NLS", "AnsiCodepage", &InfContext))
43 {
44 ERR("Failed to find 'NLS/AnsiCodepage'\n");
45 return;
46 }
47 if (!InfGetDataField(&InfContext, 1, &AnsiName))
48 {
49 ERR("Failed to get load options\n");
50 return;
51 }
52
53 /* Get OEM codepage file */
54 if (!InfFindFirstLine(InfHandle, "NLS", "OemCodepage", &InfContext))
55 {
56 ERR("Failed to find 'NLS/AnsiCodepage'\n");
57 return;
58 }
59 if (!InfGetDataField(&InfContext, 1, &OemName))
60 {
61 ERR("Failed to get load options\n");
62 return;
63 }
64
65 if (!InfFindFirstLine(InfHandle, "NLS", "UnicodeCasetable", &InfContext))
66 {
67 ERR("Failed to find 'NLS/AnsiCodepage'\n");
68 return;
69 }
70 if (!InfGetDataField(&InfContext, 1, &LangName))
71 {
72 ERR("Failed to get load options\n");
73 return;
74 }
75
76 TRACE("NLS data %s %s %s\n", AnsiName, OemName, LangName);
77
78 #if DBG
79 {
80 BOOLEAN Success = WinLdrLoadNLSData(LoaderBlock, SearchPath, AnsiName, OemName, LangName);
81 TRACE("NLS data loading %s\n", Success ? "successful" : "failed");
82 }
83 #else
84 WinLdrLoadNLSData(LoaderBlock, SearchPath, AnsiName, OemName, LangName);
85 #endif
86
87 /* TODO: Load OEM HAL font */
88 // Value "OemHalFont"
89 }
90
91 static VOID
92 SetupLdrScanBootDrivers(PLIST_ENTRY BootDriverListHead, HINF InfHandle, LPCSTR SearchPath)
93 {
94 INFCONTEXT InfContext, dirContext;
95 BOOLEAN Success;
96 LPCSTR Media, DriverName, dirIndex, ImagePath;
97 WCHAR ServiceName[256];
98 WCHAR ImagePathW[256];
99
100 /* Open inf section */
101 if (!InfFindFirstLine(InfHandle, "SourceDisksFiles", NULL, &InfContext))
102 return;
103
104 /* Load all listed boot drivers */
105 do
106 {
107 if (InfGetDataField(&InfContext, 7, &Media) &&
108 InfGetDataField(&InfContext, 0, &DriverName) &&
109 InfGetDataField(&InfContext, 13, &dirIndex))
110 {
111 if ((strcmp(Media, "x") == 0) &&
112 InfFindFirstLine(InfHandle, "Directories", dirIndex, &dirContext) &&
113 InfGetDataField(&dirContext, 1, &ImagePath))
114 {
115 /* Convert name to widechar */
116 swprintf(ServiceName, L"%S", DriverName);
117
118 /* Prepare image path */
119 swprintf(ImagePathW, L"%S", ImagePath);
120 wcscat(ImagePathW, L"\\");
121 wcscat(ImagePathW, ServiceName);
122
123 /* Remove .sys extension */
124 ServiceName[wcslen(ServiceName) - 4] = 0;
125
126 /* Add it to the list */
127 Success = WinLdrAddDriverToList(BootDriverListHead,
128 L"\\Registry\\Machine\\System\\CurrentControlSet\\Services\\",
129 ImagePathW,
130 ServiceName);
131 if (!Success)
132 {
133 ERR("could not add boot driver %s, %s\n", SearchPath, DriverName);
134 return;
135 }
136 }
137 }
138 } while (InfFindNextLine(&InfContext, &InfContext));
139 }
140
141
142 /* SETUP STARTER **************************************************************/
143
144 VOID
145 LoadReactOSSetup(IN OperatingSystemItem* OperatingSystem,
146 IN USHORT OperatingSystemVersion)
147 {
148 ULONG_PTR SectionId;
149 PCSTR SectionName = OperatingSystem->SystemPartition;
150 CHAR SettingsValue[80];
151 BOOLEAN HasSection;
152 CHAR BootOptions2[256];
153 PCHAR File;
154 CHAR FileName[512];
155 CHAR BootPath[512];
156 LPCSTR LoadOptions;
157 LPSTR BootOptions;
158 BOOLEAN BootFromFloppy;
159 BOOLEAN Success;
160 ULONG i, ErrorLine;
161 HINF InfHandle;
162 INFCONTEXT InfContext;
163 PLOADER_PARAMETER_BLOCK LoaderBlock;
164 PSETUP_LOADER_BLOCK SetupBlock;
165 LPCSTR SystemPath;
166 LPCSTR SourcePaths[] =
167 {
168 "", /* Only for floppy boot */
169 #if defined(_M_IX86)
170 "I386\\",
171 #elif defined(_M_MPPC)
172 "PPC\\",
173 #elif defined(_M_MRX000)
174 "MIPS\\",
175 #endif
176 "reactos\\",
177 NULL
178 };
179
180 UiDrawStatusText("Setup is loading...");
181
182 /* Get OS setting value */
183 SettingsValue[0] = ANSI_NULL;
184 IniOpenSection("Operating Systems", &SectionId);
185 IniReadSettingByName(SectionId, SectionName, SettingsValue, sizeof(SettingsValue));
186
187 /* Open the operating system section specified in the .ini file */
188 HasSection = IniOpenSection(SectionName, &SectionId);
189
190 UiDrawBackdrop();
191 UiDrawProgressBarCenter(1, 100, "Loading ReactOS Setup...");
192
193 /* Read the system path is set in the .ini file */
194 if (!HasSection ||
195 !IniReadSettingByName(SectionId, "SystemPath", BootPath, sizeof(BootPath)))
196 {
197 /*
198 * IMPROVE: I don't want to call MachDiskGetBootPath here as a
199 * default choice because I can call it after (see few lines below).
200 * Also doing the strcpy call as it is done in winldr.c is not
201 * really what we want. Instead I reset BootPath here so that
202 * we can build the full path using the general code from below.
203 */
204 // MachDiskGetBootPath(BootPath, sizeof(BootPath));
205 // strcpy(BootPath, SectionName);
206 BootPath[0] = '\0';
207 }
208
209 /*
210 * Check whether BootPath is a full path
211 * and if not, create a full boot path.
212 *
213 * See FsOpenFile for the technique used.
214 */
215 if (strrchr(BootPath, ')') == NULL)
216 {
217 /* Temporarily save the boot path */
218 strcpy(FileName, BootPath);
219
220 /* This is not a full path. Use the current (i.e. boot) device. */
221 MachDiskGetBootPath(BootPath, sizeof(BootPath));
222
223 /* Append a path separator if needed */
224 if (FileName[0] != '\\' && FileName[0] != '/')
225 strcat(BootPath, "\\");
226
227 /* Append the remaining path */
228 strcat(BootPath, FileName);
229 }
230
231 /* Append a backslash if needed */
232 if ((strlen(BootPath) == 0) || BootPath[strlen(BootPath) - 1] != '\\')
233 strcat(BootPath, "\\");
234
235 /* Read booting options */
236 if (!HasSection || !IniReadSettingByName(SectionId, "Options", BootOptions2, sizeof(BootOptions2)))
237 {
238 /* Get options after the title */
239 PCSTR p = SettingsValue;
240 while (*p == ' ' || *p == '"')
241 p++;
242 while (*p != '\0' && *p != '"')
243 p++;
244 strcpy(BootOptions2, p);
245 TRACE("BootOptions: '%s'\n", BootOptions2);
246 }
247
248 /* Check if a ramdisk file was given */
249 File = strstr(BootOptions2, "/RDPATH=");
250 if (File)
251 {
252 /* Copy the file name and everything else after it */
253 strcpy(FileName, File + 8);
254
255 /* Null-terminate */
256 *strstr(FileName, " ") = ANSI_NULL;
257
258 /* Load the ramdisk */
259 if (!RamDiskLoadVirtualFile(FileName))
260 {
261 UiMessageBox("Failed to load RAM disk file %s", FileName);
262 return;
263 }
264 }
265
266 TRACE("BootPath: '%s'\n", BootPath);
267
268 /* And check if we booted from floppy */
269 BootFromFloppy = strstr(BootPath, "fdisk") != NULL;
270
271 /* Open 'txtsetup.sif' from any of source paths */
272 File = BootPath + strlen(BootPath);
273 for (i = BootFromFloppy ? 0 : 1; ; i++)
274 {
275 SystemPath = SourcePaths[i];
276 if (!SystemPath)
277 {
278 UiMessageBox("Failed to open txtsetup.sif");
279 return;
280 }
281 strcpy(File, SystemPath);
282 strcpy(FileName, BootPath);
283 strcat(FileName, "txtsetup.sif");
284 if (InfOpenFile(&InfHandle, FileName, &ErrorLine))
285 {
286 break;
287 }
288 }
289
290 TRACE("BootPath: '%s', SystemPath: '%s'\n", BootPath, SystemPath);
291
292 /* Get Load options - debug and non-debug */
293 if (!InfFindFirstLine(InfHandle, "SetupData", "OsLoadOptions", &InfContext))
294 {
295 ERR("Failed to find 'SetupData/OsLoadOptions'\n");
296 return;
297 }
298
299 if (!InfGetDataField(&InfContext, 1, &LoadOptions))
300 {
301 ERR("Failed to get load options\n");
302 return;
303 }
304
305 #if DBG
306 /* Get debug load options and use them */
307 if (InfFindFirstLine(InfHandle, "SetupData", "DbgOsLoadOptions", &InfContext))
308 {
309 LPCSTR DbgLoadOptions;
310
311 if (InfGetDataField(&InfContext, 1, &DbgLoadOptions))
312 LoadOptions = DbgLoadOptions;
313 }
314 #endif
315
316 /* Copy loadoptions (original string will be freed) */
317 BootOptions = FrLdrTempAlloc(strlen(LoadOptions) + 1, TAG_BOOT_OPTIONS);
318 strcpy(BootOptions, LoadOptions);
319
320 TRACE("BootOptions: '%s'\n", BootOptions);
321
322 /* Allocate and minimalist-initialize LPB */
323 AllocateAndInitLPB(&LoaderBlock);
324
325 /* Allocate and initialize setup loader block */
326 SetupBlock = &WinLdrSystemBlock->SetupBlock;
327 LoaderBlock->SetupLdrBlock = SetupBlock;
328
329 /* Set textmode setup flag */
330 SetupBlock->Flags = SETUPLDR_TEXT_MODE;
331
332 /* Load the system hive "setupreg.hiv" for setup */
333 UiDrawBackdrop();
334 UiDrawProgressBarCenter(15, 100, "Loading setup system hive...");
335 Success = WinLdrInitSystemHive(LoaderBlock, BootPath, TRUE);
336 TRACE("Setup SYSTEM hive %s\n", (Success ? "loaded" : "not loaded"));
337 /* Bail out if failure */
338 if (!Success)
339 return;
340
341 /* Load NLS data, they are in the System32 directory of the installation medium */
342 strcpy(FileName, BootPath);
343 strcat(FileName, "system32\\");
344 SetupLdrLoadNlsData(LoaderBlock, InfHandle, FileName);
345
346 // UiDrawStatusText("Press F6 if you need to install a 3rd-party SCSI or RAID driver...");
347
348 /* Get a list of boot drivers */
349 SetupLdrScanBootDrivers(&LoaderBlock->BootDriverListHead, InfHandle, BootPath);
350
351 /* Close the inf file */
352 InfCloseFile(InfHandle);
353
354 UiDrawStatusText("The Setup program is starting...");
355
356 /* Load ReactOS Setup */
357 LoadAndBootWindowsCommon(_WIN32_WINNT_WS03,
358 LoaderBlock,
359 BootOptions,
360 BootPath,
361 TRUE);
362 }