Make disk partition handling architecture dependent, as not
[reactos.git] / reactos / 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
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include <freeldr.h>
21 #include <rtl.h>
22 #include <fs.h>
23 #include <reactos.h>
24 #include <ui.h>
25 #include <arch.h>
26 #include <miscboot.h>
27 #include <linux.h>
28 #include <mm.h>
29 #include <inifile.h>
30 #include <debug.h>
31 #include <options.h>
32 #include <oslist.h>
33 #include <video.h>
34 #include <bootmgr.h>
35 #include <drivemap.h>
36 #include <keycodes.h>
37 #include <cmdline.h>
38 #include <machine.h>
39
40 VOID RunLoader(VOID)
41 {
42 UCHAR SettingName[80];
43 UCHAR SettingValue[80];
44 ULONG SectionId;
45 ULONG OperatingSystemCount;
46 PUCHAR *OperatingSystemSectionNames;
47 PUCHAR *OperatingSystemDisplayNames;
48 ULONG DefaultOperatingSystem;
49 LONG TimeOut;
50 ULONG SelectedOperatingSystem;
51
52 if (!FsOpenBootVolume())
53 {
54 printf("Error opening boot partition for file access.\n");
55 MachConsGetCh();
56 return;
57 }
58
59 if (!IniFileInitialize())
60 {
61 printf("Press any key to reboot.\n");
62 MachConsGetCh();
63 return;
64 }
65
66 if (!IniOpenSection("FreeLoader", &SectionId))
67 {
68 printf("Section [FreeLoader] not found in freeldr.ini.\n");
69 MachConsGetCh();
70 return;
71 }
72 TimeOut = GetTimeOut();
73
74 if (!UiInitialize(TimeOut))
75 {
76 printf("Press any key to reboot.\n");
77 MachConsGetCh();
78 return;
79 }
80
81
82 if (!InitOperatingSystemList(&OperatingSystemSectionNames, &OperatingSystemDisplayNames, &OperatingSystemCount))
83 {
84 UiMessageBox("Press ENTER to reboot.\n");
85 goto reboot;
86 }
87
88 if (OperatingSystemCount == 0)
89 {
90 UiMessageBox("There were no operating systems listed in freeldr.ini.\nPress ENTER to reboot.");
91 goto reboot;
92 }
93
94 DefaultOperatingSystem = GetDefaultOperatingSystem(OperatingSystemSectionNames, OperatingSystemCount);
95
96 //
97 // Find all the message box settings and run them
98 //
99 UiShowMessageBoxesInSection("FreeLoader");
100
101 for (;;)
102 {
103
104 /* If Timeout is 0, don't even bother loading any gui */
105 if (!UserInterfaceUp) {
106 SelectedOperatingSystem = DefaultOperatingSystem;
107 goto NoGui;
108 }
109
110 // Redraw the backdrop
111 UiDrawBackdrop();
112
113 // Show the operating system list menu
114 if (!UiDisplayMenu(OperatingSystemDisplayNames, OperatingSystemCount, DefaultOperatingSystem, TimeOut, &SelectedOperatingSystem, FALSE, MainBootMenuKeyPressFilter))
115 {
116 UiMessageBox("Press ENTER to reboot.\n");
117 goto reboot;
118 }
119
120 NoGui:
121 TimeOut = -1;
122
123 // Try to open the operating system section in the .ini file
124 if (!IniOpenSection(OperatingSystemSectionNames[SelectedOperatingSystem], &SectionId))
125 {
126 sprintf(SettingName, "Section [%s] not found in freeldr.ini.\n", OperatingSystemSectionNames[SelectedOperatingSystem]);
127 UiMessageBox(SettingName);
128 continue;
129 }
130
131 // Try to read the boot type
132 if (!IniReadSettingByName(SectionId, "BootType", SettingValue, 80))
133 {
134 sprintf(SettingName, "BootType= line not found in section [%s] in freeldr.ini.\n", OperatingSystemSectionNames[SelectedOperatingSystem]);
135 UiMessageBox(SettingName);
136 continue;
137 }
138
139 // Install the drive mapper according to this sections drive mappings
140 DriveMapMapDrivesInSection(OperatingSystemSectionNames[SelectedOperatingSystem]);
141 if (stricmp(SettingValue, "ReactOS") == 0)
142 {
143 LoadAndBootReactOS(OperatingSystemSectionNames[SelectedOperatingSystem]);
144 }
145 else if (stricmp(SettingValue, "Linux") == 0)
146 {
147 LoadAndBootLinux(OperatingSystemSectionNames[SelectedOperatingSystem], OperatingSystemDisplayNames[SelectedOperatingSystem]);
148 }
149 else if (stricmp(SettingValue, "BootSector") == 0)
150 {
151 LoadAndBootBootSector(OperatingSystemSectionNames[SelectedOperatingSystem]);
152 }
153 else if (stricmp(SettingValue, "Partition") == 0)
154 {
155 LoadAndBootPartition(OperatingSystemSectionNames[SelectedOperatingSystem]);
156 }
157 else if (stricmp(SettingValue, "Drive") == 0)
158 {
159 LoadAndBootDrive(OperatingSystemSectionNames[SelectedOperatingSystem]);
160 }
161 }
162
163
164 reboot:
165 UiUnInitialize("Rebooting...");
166 return;
167 }
168
169 ULONG GetDefaultOperatingSystem(PUCHAR OperatingSystemList[], ULONG OperatingSystemCount)
170 {
171 UCHAR DefaultOSText[80];
172 char* DefaultOSName;
173 ULONG SectionId;
174 ULONG DefaultOS = 0;
175 ULONG Idx;
176
177 if (!IniOpenSection("FreeLoader", &SectionId))
178 {
179 return 0;
180 }
181
182 DefaultOSName = CmdLineGetDefaultOS();
183 if (NULL == DefaultOSName)
184 {
185 if (IniReadSettingByName(SectionId, "DefaultOS", DefaultOSText, 80))
186 {
187 DefaultOSName = DefaultOSText;
188 }
189 }
190
191 if (NULL != DefaultOSName)
192 {
193 for (Idx=0; Idx<OperatingSystemCount; Idx++)
194 {
195 if (stricmp(DefaultOSName, OperatingSystemList[Idx]) == 0)
196 {
197 DefaultOS = Idx;
198 break;
199 }
200 }
201 }
202
203 return DefaultOS;
204 }
205
206 LONG GetTimeOut(VOID)
207 {
208 UCHAR TimeOutText[20];
209 LONG TimeOut;
210 ULONG SectionId;
211
212 TimeOut = CmdLineGetTimeOut();
213 if (0 <= TimeOut)
214 {
215 return TimeOut;
216 }
217
218 if (!IniOpenSection("FreeLoader", &SectionId))
219 {
220 return -1;
221 }
222
223 if (IniReadSettingByName(SectionId, "TimeOut", TimeOutText, 20))
224 {
225 TimeOut = atoi(TimeOutText);
226 }
227 else
228 {
229 TimeOut = -1;
230 }
231
232 return TimeOut;
233 }
234
235 BOOL MainBootMenuKeyPressFilter(ULONG KeyPress)
236 {
237 if (KeyPress == KEY_F8)
238 {
239 DoOptionsMenu();
240
241 return TRUE;
242 }
243
244 // We didn't handle the key
245 return FALSE;
246 }