Fix a couple of problems with FreeLDR portability.
[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
22 VOID RunLoader(VOID)
23 {
24 CHAR SettingName[80];
25 CHAR SettingValue[80];
26 ULONG SectionId;
27 ULONG OperatingSystemCount;
28 PCSTR *OperatingSystemSectionNames;
29 PCSTR *OperatingSystemDisplayNames;
30 ULONG DefaultOperatingSystem;
31 LONG TimeOut;
32 ULONG SelectedOperatingSystem;
33
34 if (!FsOpenBootVolume())
35 {
36 UiMessageBoxCritical("Error opening boot partition for file access.");
37 return;
38 }
39
40 //
41 // Check if we have a virtual RAM disk
42 // This is for x86 emulation -- on real hardware, the RAM disk will be
43 // located in one of the hardware memory descriptors as well as on the
44 // freeldr command-line
45 //
46 RamDiskCheckForVirtualFile();
47
48 if (!IniFileInitialize())
49 {
50 UiMessageBoxCritical("Error initializing .ini file");
51 return;
52 }
53
54 if (!IniOpenSection("FreeLoader", &SectionId))
55 {
56 UiMessageBoxCritical("Section [FreeLoader] not found in freeldr.ini.");
57 return;
58 }
59 TimeOut = GetTimeOut();
60
61 if (!UiInitialize(TimeOut))
62 {
63 UiMessageBoxCritical("Unable to initialize UI.");
64 return;
65 }
66
67
68 if (!InitOperatingSystemList(&OperatingSystemSectionNames, &OperatingSystemDisplayNames, &OperatingSystemCount))
69 {
70 UiMessageBox("Press ENTER to reboot.");
71 goto reboot;
72 }
73
74 if (OperatingSystemCount == 0)
75 {
76 UiMessageBox("There were no operating systems listed in freeldr.ini.\nPress ENTER to reboot.");
77 goto reboot;
78 }
79
80 DefaultOperatingSystem = GetDefaultOperatingSystem(OperatingSystemSectionNames, OperatingSystemCount);
81
82 //
83 // Find all the message box settings and run them
84 //
85 UiShowMessageBoxesInSection("FreeLoader");
86
87 for (;;)
88 {
89
90 // Redraw the backdrop
91 UiDrawBackdrop();
92
93 // Show the operating system list menu
94 if (!UiDisplayMenu(OperatingSystemDisplayNames, OperatingSystemCount, DefaultOperatingSystem, TimeOut, &SelectedOperatingSystem, FALSE, MainBootMenuKeyPressFilter))
95 {
96 UiMessageBox("Press ENTER to reboot.");
97 goto reboot;
98 }
99
100 TimeOut = -1;
101
102 // Try to open the operating system section in the .ini file
103 if (!IniOpenSection(OperatingSystemSectionNames[SelectedOperatingSystem], &SectionId))
104 {
105 sprintf(SettingName, "Section [%s] not found in freeldr.ini.", OperatingSystemSectionNames[SelectedOperatingSystem]);
106 UiMessageBox(SettingName);
107 continue;
108 }
109
110 // Try to read the boot type
111 if (!IniReadSettingByName(SectionId, "BootType", SettingValue, sizeof(SettingValue)))
112 {
113 sprintf(SettingName, "BootType= line not found in section [%s] in freeldr.ini.", OperatingSystemSectionNames[SelectedOperatingSystem]);
114 UiMessageBox(SettingName);
115 continue;
116 }
117
118 // Install the drive mapper according to this sections drive mappings
119 #ifdef __i386__
120 DriveMapMapDrivesInSection(OperatingSystemSectionNames[SelectedOperatingSystem]);
121 #endif
122 if (_stricmp(SettingValue, "ReactOS") == 0)
123 {
124 LoadAndBootReactOS(OperatingSystemSectionNames[SelectedOperatingSystem]);
125 }
126 #ifdef __i386__
127 else if (_stricmp(SettingValue, "WindowsNT40") == 0)
128 {
129 LoadAndBootWindows(OperatingSystemSectionNames[SelectedOperatingSystem], _WIN32_WINNT_NT4);
130 }
131 else if (_stricmp(SettingValue, "Windows2000") == 0)
132 {
133 LoadAndBootWindows(OperatingSystemSectionNames[SelectedOperatingSystem], _WIN32_WINNT_WIN2K);
134 }
135 else if (_stricmp(SettingValue, "WindowsXP") == 0)
136 {
137 LoadAndBootWindows(OperatingSystemSectionNames[SelectedOperatingSystem], _WIN32_WINNT_WINXP);
138 }
139 else if (_stricmp(SettingValue, "Windows2003") == 0)
140 {
141 LoadAndBootWindows(OperatingSystemSectionNames[SelectedOperatingSystem], _WIN32_WINNT_WS03);
142 }
143 else if (_stricmp(SettingValue, "Linux") == 0)
144 {
145 LoadAndBootLinux(OperatingSystemSectionNames[SelectedOperatingSystem], OperatingSystemDisplayNames[SelectedOperatingSystem]);
146 }
147 else if (_stricmp(SettingValue, "BootSector") == 0)
148 {
149 LoadAndBootBootSector(OperatingSystemSectionNames[SelectedOperatingSystem]);
150 }
151 else if (_stricmp(SettingValue, "Partition") == 0)
152 {
153 LoadAndBootPartition(OperatingSystemSectionNames[SelectedOperatingSystem]);
154 }
155 else if (_stricmp(SettingValue, "Drive") == 0)
156 {
157 LoadAndBootDrive(OperatingSystemSectionNames[SelectedOperatingSystem]);
158 }
159 #endif
160 }
161
162
163 reboot:
164 UiUnInitialize("Rebooting...");
165 return;
166 }
167
168 ULONG GetDefaultOperatingSystem(PCSTR OperatingSystemList[], ULONG OperatingSystemCount)
169 {
170 CHAR DefaultOSText[80];
171 PCSTR DefaultOSName;
172 ULONG SectionId;
173 ULONG DefaultOS = 0;
174 ULONG Idx;
175
176 if (!IniOpenSection("FreeLoader", &SectionId))
177 {
178 return 0;
179 }
180
181 DefaultOSName = CmdLineGetDefaultOS();
182 if (NULL == DefaultOSName)
183 {
184 if (IniReadSettingByName(SectionId, "DefaultOS", DefaultOSText, sizeof(DefaultOSText)))
185 {
186 DefaultOSName = DefaultOSText;
187 }
188 }
189
190 if (NULL != DefaultOSName)
191 {
192 for (Idx=0; Idx<OperatingSystemCount; Idx++)
193 {
194 if (_stricmp(DefaultOSName, OperatingSystemList[Idx]) == 0)
195 {
196 DefaultOS = Idx;
197 break;
198 }
199 }
200 }
201
202 return DefaultOS;
203 }
204
205 LONG GetTimeOut(VOID)
206 {
207 CHAR TimeOutText[20];
208 LONG TimeOut;
209 ULONG SectionId;
210
211 TimeOut = CmdLineGetTimeOut();
212 if (0 <= TimeOut)
213 {
214 return TimeOut;
215 }
216
217 if (!IniOpenSection("FreeLoader", &SectionId))
218 {
219 return -1;
220 }
221
222 if (IniReadSettingByName(SectionId, "TimeOut", TimeOutText, sizeof(TimeOutText)))
223 {
224 TimeOut = atoi(TimeOutText);
225 }
226 else
227 {
228 TimeOut = -1;
229 }
230
231 return TimeOut;
232 }
233
234 BOOLEAN MainBootMenuKeyPressFilter(ULONG KeyPress)
235 {
236 if (KeyPress == KEY_F8)
237 {
238 DoOptionsMenu();
239
240 return TRUE;
241 }
242
243 // We didn't handle the key
244 return FALSE;
245 }