72e69f889cf6eeb59ff11bf4832c95117507ae55
[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 BOOLEAN Success;
40 LPCSTR AnsiName, OemName, LangName;
41
42 /* Get ANSI codepage file */
43 if (!InfFindFirstLine(InfHandle, "NLS", "AnsiCodepage", &InfContext))
44 {
45 ERR("Failed to find 'NLS/AnsiCodepage'\n");
46 return;
47 }
48 if (!InfGetDataField(&InfContext, 1, &AnsiName))
49 {
50 ERR("Failed to get load options\n");
51 return;
52 }
53
54 /* Get OEM codepage file */
55 if (!InfFindFirstLine(InfHandle, "NLS", "OemCodepage", &InfContext))
56 {
57 ERR("Failed to find 'NLS/AnsiCodepage'\n");
58 return;
59 }
60 if (!InfGetDataField(&InfContext, 1, &OemName))
61 {
62 ERR("Failed to get load options\n");
63 return;
64 }
65
66 if (!InfFindFirstLine(InfHandle, "NLS", "UnicodeCasetable", &InfContext))
67 {
68 ERR("Failed to find 'NLS/AnsiCodepage'\n");
69 return;
70 }
71 if (!InfGetDataField(&InfContext, 1, &LangName))
72 {
73 ERR("Failed to get load options\n");
74 return;
75 }
76
77 Success = WinLdrLoadNLSData(LoaderBlock, SearchPath, AnsiName, OemName, LangName);
78 TRACE("NLS data loading %s\n", Success ? "successful" : "failed");
79 }
80
81 static VOID
82 SetupLdrScanBootDrivers(PLIST_ENTRY BootDriverListHead, HINF InfHandle, LPCSTR SearchPath)
83 {
84 INFCONTEXT InfContext, dirContext;
85 BOOLEAN Success;
86 LPCSTR Media, DriverName, dirIndex, ImagePath;
87 WCHAR ServiceName[256];
88 WCHAR ImagePathW[256];
89
90 /* Open inf section */
91 if (!InfFindFirstLine(InfHandle, "SourceDisksFiles", NULL, &InfContext))
92 return;
93
94 /* Load all listed boot drivers */
95 do
96 {
97 if (InfGetDataField(&InfContext, 7, &Media) &&
98 InfGetDataField(&InfContext, 0, &DriverName) &&
99 InfGetDataField(&InfContext, 13, &dirIndex))
100 {
101 if ((strcmp(Media, "x") == 0) &&
102 InfFindFirstLine(InfHandle, "Directories", dirIndex, &dirContext) &&
103 InfGetDataField(&dirContext, 1, &ImagePath))
104 {
105 /* Convert name to widechar */
106 swprintf(ServiceName, L"%S", DriverName);
107
108 /* Prepare image path */
109 swprintf(ImagePathW, L"%S", ImagePath);
110 wcscat(ImagePathW, L"\\");
111 wcscat(ImagePathW, ServiceName);
112
113 /* Remove .sys extension */
114 ServiceName[wcslen(ServiceName) - 4] = 0;
115
116 /* Add it to the list */
117 Success = WinLdrAddDriverToList(BootDriverListHead,
118 L"\\Registry\\Machine\\System\\CurrentControlSet\\Services\\",
119 ImagePathW,
120 ServiceName);
121 if (!Success)
122 {
123 ERR("could not add boot driver %s, %s\n", SearchPath, DriverName);
124 return;
125 }
126 }
127 }
128 } while (InfFindNextLine(&InfContext, &InfContext));
129 }
130
131 VOID
132 LoadReactOSSetup(IN OperatingSystemItem* OperatingSystem,
133 IN USHORT OperatingSystemVersion)
134 {
135 ULONG_PTR SectionId;
136 PCSTR SectionName = OperatingSystem->SystemPartition;
137 CHAR SettingsValue[80];
138 BOOLEAN HasSection;
139 CHAR BootOptions2[256];
140 PCHAR File;
141 CHAR FileName[512];
142 CHAR BootPath[512];
143 LPCSTR LoadOptions;
144 LPSTR BootOptions;
145 BOOLEAN BootFromFloppy;
146 ULONG i, ErrorLine;
147 HINF InfHandle;
148 INFCONTEXT InfContext;
149 PLOADER_PARAMETER_BLOCK LoaderBlock;
150 PSETUP_LOADER_BLOCK SetupBlock;
151 LPCSTR SystemPath;
152 LPCSTR SourcePaths[] =
153 {
154 "", /* Only for floppy boot */
155 #if defined(_M_IX86)
156 "I386\\",
157 #elif defined(_M_MPPC)
158 "PPC\\",
159 #elif defined(_M_MRX000)
160 "MIPS\\",
161 #endif
162 "reactos\\",
163 NULL
164 };
165
166 /* Get OS setting value */
167 SettingsValue[0] = ANSI_NULL;
168 IniOpenSection("Operating Systems", &SectionId);
169 IniReadSettingByName(SectionId, SectionName, SettingsValue, sizeof(SettingsValue));
170
171 /* Open the operating system section specified in the .ini file */
172 HasSection = IniOpenSection(SectionName, &SectionId);
173
174 UiDrawBackdrop();
175 UiDrawProgressBarCenter(1, 100, "Loading ReactOS Setup...");
176
177 /* Read the system path is set in the .ini file */
178 if (!HasSection ||
179 !IniReadSettingByName(SectionId, "SystemPath", BootPath, sizeof(BootPath)))
180 {
181 /*
182 * IMPROVE: I don't want to call MachDiskGetBootPath here as a
183 * default choice because I can call it after (see few lines below).
184 * Also doing the strcpy call as it is done in winldr.c is not
185 * really what we want. Instead I reset BootPath here so that
186 * we can build the full path using the general code from below.
187 */
188 // MachDiskGetBootPath(BootPath, sizeof(BootPath));
189 // strcpy(BootPath, SectionName);
190 BootPath[0] = '\0';
191 }
192
193 /*
194 * Check whether BootPath is a full path
195 * and if not, create a full boot path.
196 *
197 * See FsOpenFile for the technique used.
198 */
199 if (strrchr(BootPath, ')') == NULL)
200 {
201 /* Temporarily save the boot path */
202 strcpy(FileName, BootPath);
203
204 /* This is not a full path. Use the current (i.e. boot) device. */
205 MachDiskGetBootPath(BootPath, sizeof(BootPath));
206
207 /* Append a path separator if needed */
208 if (FileName[0] != '\\' && FileName[0] != '/')
209 strcat(BootPath, "\\");
210
211 /* Append the remaining path */
212 strcat(BootPath, FileName);
213 }
214
215 /* Append a backslash if needed */
216 if ((strlen(BootPath) == 0) || BootPath[strlen(BootPath) - 1] != '\\')
217 strcat(BootPath, "\\");
218
219 /* Read booting options */
220 if (!HasSection || !IniReadSettingByName(SectionId, "Options", BootOptions2, sizeof(BootOptions2)))
221 {
222 /* Get options after the title */
223 PCSTR p = SettingsValue;
224 while (*p == ' ' || *p == '"')
225 p++;
226 while (*p != '\0' && *p != '"')
227 p++;
228 strcpy(BootOptions2, p);
229 TRACE("BootOptions: '%s'\n", BootOptions2);
230 }
231
232 /* Check if a ramdisk file was given */
233 File = strstr(BootOptions2, "/RDPATH=");
234 if (File)
235 {
236 /* Copy the file name and everything else after it */
237 strcpy(FileName, File + 8);
238
239 /* Null-terminate */
240 *strstr(FileName, " ") = ANSI_NULL;
241
242 /* Load the ramdisk */
243 if (!RamDiskLoadVirtualFile(FileName))
244 {
245 UiMessageBox("Failed to load RAM disk file %s", FileName);
246 return;
247 }
248 }
249
250 TRACE("BootPath: '%s'\n", BootPath);
251
252 /* And check if we booted from floppy */
253 BootFromFloppy = strstr(BootPath, "fdisk") != NULL;
254
255 /* Open 'txtsetup.sif' from any of source paths */
256 File = BootPath + strlen(BootPath);
257 for (i = BootFromFloppy ? 0 : 1; ; i++)
258 {
259 SystemPath = SourcePaths[i];
260 if (!SystemPath)
261 {
262 UiMessageBox("Failed to open txtsetup.sif");
263 return;
264 }
265 strcpy(File, SystemPath);
266 strcpy(FileName, BootPath);
267 strcat(FileName, "txtsetup.sif");
268 if (InfOpenFile(&InfHandle, FileName, &ErrorLine))
269 {
270 break;
271 }
272 }
273
274 TRACE("BootPath: '%s', SystemPath: '%s'\n", BootPath, SystemPath);
275
276 /* Get Load options - debug and non-debug */
277 if (!InfFindFirstLine(InfHandle, "SetupData", "OsLoadOptions", &InfContext))
278 {
279 ERR("Failed to find 'SetupData/OsLoadOptions'\n");
280 return;
281 }
282
283 if (!InfGetDataField(&InfContext, 1, &LoadOptions))
284 {
285 ERR("Failed to get load options\n");
286 return;
287 }
288
289 #if DBG
290 /* Get debug load options and use them */
291 if (InfFindFirstLine(InfHandle, "SetupData", "DbgOsLoadOptions", &InfContext))
292 {
293 LPCSTR DbgLoadOptions;
294
295 if (InfGetDataField(&InfContext, 1, &DbgLoadOptions))
296 LoadOptions = DbgLoadOptions;
297 }
298 #endif
299
300 /* Copy loadoptions (original string will be freed) */
301 BootOptions = FrLdrTempAlloc(strlen(LoadOptions) + 1, TAG_BOOT_OPTIONS);
302 strcpy(BootOptions, LoadOptions);
303
304 TRACE("BootOptions: '%s'\n", BootOptions);
305
306 UiDrawStatusText("Setup is loading...");
307
308 /* Allocate and minimalist-initialize LPB */
309 AllocateAndInitLPB(&LoaderBlock);
310
311 /* Allocate and initialize setup loader block */
312 SetupBlock = &WinLdrSystemBlock->SetupBlock;
313 LoaderBlock->SetupLdrBlock = SetupBlock;
314
315 /* Set textmode setup flag */
316 SetupBlock->Flags = SETUPLDR_TEXT_MODE;
317
318 /* Load NLS data, they are in system32 */
319 strcpy(FileName, BootPath);
320 strcat(FileName, "system32\\");
321 SetupLdrLoadNlsData(LoaderBlock, InfHandle, FileName);
322
323 /* Get a list of boot drivers */
324 SetupLdrScanBootDrivers(&LoaderBlock->BootDriverListHead, InfHandle, BootPath);
325
326 /* Close the inf file */
327 InfCloseFile(InfHandle);
328
329 /* Load ReactOS Setup */
330 LoadAndBootWindowsCommon(_WIN32_WINNT_WS03,
331 LoaderBlock,
332 BootOptions,
333 BootPath,
334 TRUE);
335 }