[FREELDR] Minor code style - use RtlZeroMemory(); shorter member name; use different...
[reactos.git] / boot / freeldr / freeldr / bootmgr.c
1 /*
2 * FreeLoader
3 * Copyright (C) 1998-2003 Brian Palmer <brianp@sginet.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 /* INCLUDES *******************************************************************/
21
22 #include <freeldr.h>
23
24 #include <debug.h>
25 DBG_DEFAULT_CHANNEL(WARNING);
26
27 /* GLOBALS ********************************************************************/
28
29 typedef
30 VOID
31 (*EDIT_OS_ENTRY_PROC)(
32 IN OUT OperatingSystemItem* OperatingSystem);
33
34 static VOID
35 EditCustomBootReactOSSetup(
36 IN OUT OperatingSystemItem* OperatingSystem)
37 {
38 EditCustomBootReactOS(OperatingSystem, TRUE);
39 }
40
41 static VOID
42 EditCustomBootNTOS(
43 IN OUT OperatingSystemItem* OperatingSystem)
44 {
45 EditCustomBootReactOS(OperatingSystem, FALSE);
46 }
47
48 static const struct
49 {
50 PCSTR BootType;
51 EDIT_OS_ENTRY_PROC EditOsEntry;
52 ARC_ENTRY_POINT OsLoader;
53 } OSLoadingMethods[] =
54 {
55 {"ReactOSSetup", EditCustomBootReactOSSetup, LoadReactOSSetup},
56
57 #ifdef _M_IX86
58 {"Drive" , EditCustomBootDisk , LoadAndBootDrive },
59 {"Partition" , EditCustomBootPartition , LoadAndBootPartition },
60 {"BootSector" , EditCustomBootSectorFile, LoadAndBootBootSector},
61
62 {"Linux" , EditCustomBootLinux, LoadAndBootLinux },
63 {"WindowsNT40" , EditCustomBootNTOS , LoadAndBootWindows},
64 #endif
65 {"Windows" , EditCustomBootNTOS , LoadAndBootWindows},
66 {"Windows2003" , EditCustomBootNTOS , LoadAndBootWindows},
67 };
68
69 /* FUNCTIONS ******************************************************************/
70
71 /*
72 * This function converts the list of key=value options in the given operating
73 * system section into an ARC-compatible argument vector, providing in addition
74 * the extra mandatory Software Loading Environment Variables, following the
75 * ARC specification.
76 */
77 PCHAR*
78 BuildArgvForOsLoader(
79 IN PCSTR LoadIdentifier,
80 IN ULONG_PTR SectionId,
81 OUT PULONG pArgc)
82 {
83 SIZE_T Size;
84 ULONG Count;
85 ULONG i;
86 ULONG Argc;
87 PCHAR* Argv;
88 PCHAR* Args;
89 PCHAR SettingName, SettingValue;
90
91 *pArgc = 0;
92
93 ASSERT(SectionId != 0);
94
95 /* Validate the LoadIdentifier (to make tests simpler later) */
96 if (LoadIdentifier && !*LoadIdentifier)
97 LoadIdentifier = NULL;
98
99 /* Count the number of operating systems in the section */
100 Count = IniGetNumSectionItems(SectionId);
101
102 /*
103 * The argument vector contains the program name, the SystemPartition,
104 * the LoadIdentifier (optional), and the items in the OS section.
105 */
106 Argc = 2 + (LoadIdentifier ? 1 : 0) + Count;
107
108 /* Calculate the total size needed for the string buffer of the argument vector */
109 Size = 0;
110 /* i == 0: Program name */
111 /* i == 1: SystemPartition : from where FreeLdr has been started */
112 Size += (strlen("SystemPartition=") + strlen(FrldrBootPath) + 1) * sizeof(CHAR);
113 /* i == 2: LoadIdentifier : ASCII string that may be used to associate an identifier with a set of load parameters */
114 if (LoadIdentifier)
115 {
116 Size += (strlen("LoadIdentifier=") + strlen(LoadIdentifier) + 1) * sizeof(CHAR);
117 }
118 for (i = 0; i < Count; ++i)
119 {
120 Size += IniGetSectionSettingNameSize(SectionId, i); // Counts also the NULL-terminator, that we transform into the '=' sign separator.
121 Size += IniGetSectionSettingValueSize(SectionId, i); // Counts also the NULL-terminator.
122 }
123 Size += sizeof(ANSI_NULL); // Final NULL-terminator.
124
125 /* Allocate memory to hold the argument vector: pointers and string buffer */
126 Argv = FrLdrHeapAlloc(Argc * sizeof(PCHAR) + Size, TAG_STRING);
127 if (!Argv)
128 return NULL;
129
130 /* Initialize the argument vector: loop through the section and copy the key=value options */
131 SettingName = (PCHAR)((ULONG_PTR)Argv + (Argc * sizeof(PCHAR)));
132 Args = Argv;
133 /* i == 0: Program name */
134 *Args++ = NULL;
135 /* i == 1: SystemPartition */
136 {
137 strcpy(SettingName, "SystemPartition=");
138 strcat(SettingName, FrldrBootPath);
139
140 *Args++ = SettingName;
141 SettingName += (strlen(SettingName) + 1);
142 }
143 /* i == 2: LoadIdentifier */
144 if (LoadIdentifier)
145 {
146 strcpy(SettingName, "LoadIdentifier=");
147 strcat(SettingName, LoadIdentifier);
148
149 *Args++ = SettingName;
150 SettingName += (strlen(SettingName) + 1);
151 }
152 for (i = 0; i < Count; ++i)
153 {
154 Size = IniGetSectionSettingNameSize(SectionId, i);
155 SettingValue = SettingName + Size;
156 IniReadSettingByNumber(SectionId, i,
157 SettingName, Size,
158 SettingValue, IniGetSectionSettingValueSize(SectionId, i));
159 SettingName[Size - 1] = '=';
160
161 *Args++ = SettingName;
162 SettingName += (strlen(SettingName) + 1);
163 }
164
165 #if DBG
166 /* Dump the argument vector for debugging */
167 for (i = 0; i < Argc; ++i)
168 {
169 TRACE("Argv[%lu]: '%s'\n", i, Argv[i]);
170 }
171 #endif
172
173 *pArgc = Argc;
174 return Argv;
175 }
176
177 VOID LoadOperatingSystem(IN OperatingSystemItem* OperatingSystem)
178 {
179 ULONG_PTR SectionId = OperatingSystem->SectionId;
180 ULONG i;
181 ULONG Argc;
182 PCHAR* Argv;
183 CHAR BootType[80];
184
185 /* The operating system section has been opened by InitOperatingSystemList() */
186 ASSERT(SectionId != 0);
187
188 /* Try to read the boot type */
189 *BootType = ANSI_NULL;
190 IniReadSettingByName(SectionId, "BootType", BootType, sizeof(BootType));
191
192 /* We must have the "BootType" value (it has been possibly added by InitOperatingSystemList()) */
193 ASSERT(*BootType);
194
195 #ifdef _M_IX86
196 /* Install the drive mapper according to this section drive mappings */
197 DriveMapMapDrivesInSection(SectionId);
198 #endif
199
200 /* Loop through the OS loading method table and find a suitable OS to boot */
201 for (i = 0; i < sizeof(OSLoadingMethods) / sizeof(OSLoadingMethods[0]); ++i)
202 {
203 if (_stricmp(BootType, OSLoadingMethods[i].BootType) == 0)
204 {
205 Argv = BuildArgvForOsLoader(OperatingSystem->LoadIdentifier, SectionId, &Argc);
206 if (Argv)
207 {
208 OSLoadingMethods[i].OsLoader(Argc, Argv, NULL);
209 FrLdrHeapFree(Argv, TAG_STRING);
210 }
211 return;
212 }
213 }
214 }
215
216 #ifdef HAS_OPTION_MENU_EDIT_CMDLINE
217
218 VOID EditOperatingSystemEntry(IN OperatingSystemItem* OperatingSystem)
219 {
220 ULONG_PTR SectionId = OperatingSystem->SectionId;
221 ULONG i;
222 CHAR BootType[80];
223
224 /* The operating system section has been opened by InitOperatingSystemList() */
225 ASSERT(SectionId != 0);
226
227 /* Try to read the boot type */
228 *BootType = ANSI_NULL;
229 IniReadSettingByName(SectionId, "BootType", BootType, sizeof(BootType));
230
231 /* We must have the "BootType" value (it has been possibly added by InitOperatingSystemList()) */
232 ASSERT(*BootType);
233
234 /* Loop through the OS loading method table and find a suitable OS entry editor */
235 for (i = 0; i < sizeof(OSLoadingMethods) / sizeof(OSLoadingMethods[0]); ++i)
236 {
237 if (_stricmp(BootType, OSLoadingMethods[i].BootType) == 0)
238 {
239 OSLoadingMethods[i].EditOsEntry(OperatingSystem);
240 return;
241 }
242 }
243 }
244
245 #endif // HAS_OPTION_MENU_EDIT_CMDLINE
246
247 static LONG
248 GetTimeOut(
249 IN ULONG_PTR FrLdrSectionId)
250 {
251 LONG TimeOut = -1;
252 CHAR TimeOutText[20];
253
254 TimeOut = CmdLineGetTimeOut();
255 if (TimeOut >= 0)
256 return TimeOut;
257
258 TimeOut = -1;
259
260 if ((FrLdrSectionId != 0) &&
261 IniReadSettingByName(FrLdrSectionId, "TimeOut", TimeOutText, sizeof(TimeOutText)))
262 {
263 TimeOut = atoi(TimeOutText);
264 }
265
266 return TimeOut;
267 }
268
269 BOOLEAN
270 MainBootMenuKeyPressFilter(
271 IN ULONG KeyPress,
272 IN ULONG SelectedMenuItem,
273 IN PVOID Context OPTIONAL)
274 {
275 switch (KeyPress)
276 {
277 case KEY_F8:
278 DoOptionsMenu(&((OperatingSystemItem*)Context)[SelectedMenuItem]);
279 return TRUE;
280
281 #ifdef HAS_OPTION_MENU_EDIT_CMDLINE
282 case KEY_F10:
283 EditOperatingSystemEntry(&((OperatingSystemItem*)Context)[SelectedMenuItem]);
284 return TRUE;
285 #endif
286
287 default:
288 /* We didn't handle the key */
289 return FALSE;
290 }
291 }
292
293 VOID RunLoader(VOID)
294 {
295 ULONG_PTR SectionId;
296 LONG TimeOut;
297 ULONG OperatingSystemCount;
298 OperatingSystemItem* OperatingSystemList;
299 PCSTR* OperatingSystemDisplayNames;
300 ULONG DefaultOperatingSystem;
301 ULONG SelectedOperatingSystem;
302 ULONG i;
303
304 if (!MachInitializeBootDevices())
305 {
306 UiMessageBoxCritical("Error when detecting hardware.");
307 return;
308 }
309
310 #ifdef _M_IX86
311 /* Load additional SCSI driver (if any) */
312 if (LoadBootDeviceDriver() != ESUCCESS)
313 {
314 UiMessageBoxCritical("Unable to load additional boot device drivers.");
315 }
316 #endif
317
318 if (!IniFileInitialize())
319 {
320 UiMessageBoxCritical("Error initializing .ini file.");
321 return;
322 }
323
324 /* Open the [FreeLoader] section */
325 if (!IniOpenSection("FreeLoader", &SectionId))
326 {
327 UiMessageBoxCritical("Section [FreeLoader] not found in freeldr.ini.");
328 return;
329 }
330
331 /* Debugger main initialization */
332 DebugInit(SectionId);
333
334 /* Retrieve the default timeout */
335 TimeOut = GetTimeOut(SectionId);
336
337 /* UI main initialization */
338 if (!UiInitialize(TRUE))
339 {
340 UiMessageBoxCritical("Unable to initialize UI.");
341 return;
342 }
343
344 OperatingSystemList = InitOperatingSystemList(SectionId,
345 &OperatingSystemCount,
346 &DefaultOperatingSystem);
347 if (!OperatingSystemList)
348 {
349 UiMessageBox("Unable to read operating systems section in freeldr.ini.\nPress ENTER to reboot.");
350 goto Reboot;
351 }
352 if (OperatingSystemCount == 0)
353 {
354 UiMessageBox("There were no operating systems listed in freeldr.ini.\nPress ENTER to reboot.");
355 goto Reboot;
356 }
357
358 /* Create list of display names */
359 OperatingSystemDisplayNames = FrLdrTempAlloc(sizeof(PCSTR) * OperatingSystemCount, 'mNSO');
360 if (!OperatingSystemDisplayNames)
361 goto Reboot;
362
363 for (i = 0; i < OperatingSystemCount; i++)
364 {
365 OperatingSystemDisplayNames[i] = OperatingSystemList[i].LoadIdentifier;
366 }
367
368 /* Find all the message box settings and run them */
369 UiShowMessageBoxesInSection(SectionId);
370
371 for (;;)
372 {
373 /* Redraw the backdrop */
374 UiDrawBackdrop();
375
376 /* Show the operating system list menu */
377 if (!UiDisplayMenu("Please select the operating system to start:",
378 "For troubleshooting and advanced startup options for "
379 "ReactOS, press F8.",
380 TRUE,
381 OperatingSystemDisplayNames,
382 OperatingSystemCount,
383 DefaultOperatingSystem,
384 TimeOut,
385 &SelectedOperatingSystem,
386 FALSE,
387 MainBootMenuKeyPressFilter,
388 OperatingSystemList))
389 {
390 UiMessageBox("Press ENTER to reboot.");
391 goto Reboot;
392 }
393
394 TimeOut = -1;
395
396 /* Load the chosen operating system */
397 LoadOperatingSystem(&OperatingSystemList[SelectedOperatingSystem]);
398 }
399
400 Reboot:
401 UiUnInitialize("Rebooting...");
402 IniCleanup();
403 return;
404 }