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