Create a place for work on creating a bootloader capable of booting Windows NT (from...
[reactos.git] / 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 printf("Error opening boot partition for file access.\n");
37 MachConsGetCh();
38 return;
39 }
40
41 if (!IniFileInitialize())
42 {
43 printf("Press any key to reboot.\n");
44 MachConsGetCh();
45 return;
46 }
47
48 if (!IniOpenSection("FreeLoader", &SectionId))
49 {
50 printf("Section [FreeLoader] not found in freeldr.ini.\n");
51 MachConsGetCh();
52 return;
53 }
54 TimeOut = GetTimeOut();
55
56 if (!UiInitialize(TimeOut))
57 {
58 printf("Press any key to reboot.\n");
59 MachConsGetCh();
60 return;
61 }
62
63
64 if (!InitOperatingSystemList(&OperatingSystemSectionNames, &OperatingSystemDisplayNames, &OperatingSystemCount))
65 {
66 UiMessageBox("Press ENTER to reboot.\n");
67 goto reboot;
68 }
69
70 if (OperatingSystemCount == 0)
71 {
72 UiMessageBox("There were no operating systems listed in freeldr.ini.\nPress ENTER to reboot.");
73 goto reboot;
74 }
75
76 DefaultOperatingSystem = GetDefaultOperatingSystem(OperatingSystemSectionNames, OperatingSystemCount);
77
78 //
79 // Find all the message box settings and run them
80 //
81 UiShowMessageBoxesInSection("FreeLoader");
82
83 for (;;)
84 {
85
86 /* If Timeout is 0, don't even bother loading any gui */
87 if (!UserInterfaceUp) {
88 SelectedOperatingSystem = DefaultOperatingSystem;
89 goto NoGui;
90 }
91
92 // Redraw the backdrop
93 UiDrawBackdrop();
94
95 // Show the operating system list menu
96 if (!UiDisplayMenu(OperatingSystemDisplayNames, OperatingSystemCount, DefaultOperatingSystem, TimeOut, &SelectedOperatingSystem, FALSE, MainBootMenuKeyPressFilter))
97 {
98 UiMessageBox("Press ENTER to reboot.\n");
99 goto reboot;
100 }
101
102 NoGui:
103 TimeOut = -1;
104
105 // Try to open the operating system section in the .ini file
106 if (!IniOpenSection(OperatingSystemSectionNames[SelectedOperatingSystem], &SectionId))
107 {
108 sprintf(SettingName, "Section [%s] not found in freeldr.ini.\n", OperatingSystemSectionNames[SelectedOperatingSystem]);
109 UiMessageBox(SettingName);
110 continue;
111 }
112
113 // Try to read the boot type
114 if (!IniReadSettingByName(SectionId, "BootType", SettingValue, sizeof(SettingValue)))
115 {
116 sprintf(SettingName, "BootType= line not found in section [%s] in freeldr.ini.\n", OperatingSystemSectionNames[SelectedOperatingSystem]);
117 UiMessageBox(SettingName);
118 continue;
119 }
120
121 // Install the drive mapper according to this sections drive mappings
122 DriveMapMapDrivesInSection(OperatingSystemSectionNames[SelectedOperatingSystem]);
123 if (_stricmp(SettingValue, "ReactOS") == 0)
124 {
125 LoadAndBootReactOS(OperatingSystemSectionNames[SelectedOperatingSystem]);
126 }
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 }
160
161
162 reboot:
163 UiUnInitialize("Rebooting...");
164 return;
165 }
166
167 ULONG GetDefaultOperatingSystem(PCSTR OperatingSystemList[], ULONG OperatingSystemCount)
168 {
169 CHAR DefaultOSText[80];
170 PCSTR DefaultOSName;
171 ULONG SectionId;
172 ULONG DefaultOS = 0;
173 ULONG Idx;
174
175 if (!IniOpenSection("FreeLoader", &SectionId))
176 {
177 return 0;
178 }
179
180 DefaultOSName = CmdLineGetDefaultOS();
181 if (NULL == DefaultOSName)
182 {
183 if (IniReadSettingByName(SectionId, "DefaultOS", DefaultOSText, sizeof(DefaultOSText)))
184 {
185 DefaultOSName = DefaultOSText;
186 }
187 }
188
189 if (NULL != DefaultOSName)
190 {
191 for (Idx=0; Idx<OperatingSystemCount; Idx++)
192 {
193 if (_stricmp(DefaultOSName, OperatingSystemList[Idx]) == 0)
194 {
195 DefaultOS = Idx;
196 break;
197 }
198 }
199 }
200
201 return DefaultOS;
202 }
203
204 LONG GetTimeOut(VOID)
205 {
206 CHAR TimeOutText[20];
207 LONG TimeOut;
208 ULONG SectionId;
209
210 TimeOut = CmdLineGetTimeOut();
211 if (0 <= TimeOut)
212 {
213 return TimeOut;
214 }
215
216 if (!IniOpenSection("FreeLoader", &SectionId))
217 {
218 return -1;
219 }
220
221 if (IniReadSettingByName(SectionId, "TimeOut", TimeOutText, sizeof(TimeOutText)))
222 {
223 TimeOut = atoi(TimeOutText);
224 }
225 else
226 {
227 TimeOut = -1;
228 }
229
230 return TimeOut;
231 }
232
233 BOOLEAN MainBootMenuKeyPressFilter(ULONG KeyPress)
234 {
235 if (KeyPress == KEY_F8)
236 {
237 DoOptionsMenu();
238
239 return TRUE;
240 }
241
242 // We didn't handle the key
243 return FALSE;
244 }