- Initialize MmLowestPhysicalPage to -1, otherwise setting this value will never...
[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_PTR 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 if (!IniFileInitialize())
41 {
42 UiMessageBoxCritical("Error initializing .ini file");
43 return;
44 }
45
46 if (!IniOpenSection("FreeLoader", &SectionId))
47 {
48 UiMessageBoxCritical("Section [FreeLoader] not found in freeldr.ini.");
49 return;
50 }
51 TimeOut = GetTimeOut();
52
53 if (!UiInitialize(TimeOut))
54 {
55 UiMessageBoxCritical("Unable to initialize UI.");
56 return;
57 }
58
59
60 if (!InitOperatingSystemList(&OperatingSystemSectionNames, &OperatingSystemDisplayNames, &OperatingSystemCount))
61 {
62 UiMessageBox("Press ENTER to reboot.");
63 goto reboot;
64 }
65
66 if (OperatingSystemCount == 0)
67 {
68 UiMessageBox("There were no operating systems listed in freeldr.ini.\nPress ENTER to reboot.");
69 goto reboot;
70 }
71
72 DefaultOperatingSystem = GetDefaultOperatingSystem(OperatingSystemSectionNames, OperatingSystemCount);
73
74 //
75 // Find all the message box settings and run them
76 //
77 UiShowMessageBoxesInSection("FreeLoader");
78
79 for (;;)
80 {
81
82 // Redraw the backdrop
83 UiDrawBackdrop();
84
85 // Show the operating system list menu
86 if (!UiDisplayMenu(OperatingSystemDisplayNames, OperatingSystemCount, DefaultOperatingSystem, TimeOut, &SelectedOperatingSystem, FALSE, MainBootMenuKeyPressFilter))
87 {
88 UiMessageBox("Press ENTER to reboot.");
89 goto reboot;
90 }
91
92 TimeOut = -1;
93
94 // Try to open the operating system section in the .ini file
95 if (!IniOpenSection(OperatingSystemSectionNames[SelectedOperatingSystem], &SectionId))
96 {
97 sprintf(SettingName, "Section [%s] not found in freeldr.ini.", OperatingSystemSectionNames[SelectedOperatingSystem]);
98 UiMessageBox(SettingName);
99 continue;
100 }
101
102 // Try to read the boot type
103 if (!IniReadSettingByName(SectionId, "BootType", SettingValue, sizeof(SettingValue)))
104 {
105 sprintf(SettingName, "BootType= line not found in section [%s] in freeldr.ini.", OperatingSystemSectionNames[SelectedOperatingSystem]);
106 UiMessageBox(SettingName);
107 continue;
108 }
109
110 // Install the drive mapper according to this sections drive mappings
111 #ifdef __i386__
112 DriveMapMapDrivesInSection(OperatingSystemSectionNames[SelectedOperatingSystem]);
113 #endif
114 if (_stricmp(SettingValue, "ReactOS") == 0)
115 {
116 LoadAndBootReactOS(OperatingSystemSectionNames[SelectedOperatingSystem]);
117 }
118 #ifdef FREELDR_REACTOS_SETUP
119 else if (_stricmp(SettingValue, "ReactOSSetup") == 0)
120 {
121 // In future we could pass the selected OS details through this
122 // to have different install methods, etc.
123 LoadReactOSSetup();
124 }
125 #ifdef __i386__
126 else if (_stricmp(SettingValue, "ReactOSSetup2") == 0)
127 {
128 // WinLdr-style boot
129 LoadReactOSSetup2();
130 }
131 #endif
132 #endif
133 #ifdef __i386__
134 else if (_stricmp(SettingValue, "WindowsNT40") == 0)
135 {
136 LoadAndBootWindows(OperatingSystemSectionNames[SelectedOperatingSystem], _WIN32_WINNT_NT4);
137 }
138 else if (_stricmp(SettingValue, "Windows2003") == 0)
139 {
140 LoadAndBootWindows(OperatingSystemSectionNames[SelectedOperatingSystem], _WIN32_WINNT_WS03);
141 }
142 else if (_stricmp(SettingValue, "Linux") == 0)
143 {
144 LoadAndBootLinux(OperatingSystemSectionNames[SelectedOperatingSystem], OperatingSystemDisplayNames[SelectedOperatingSystem]);
145 }
146 else if (_stricmp(SettingValue, "BootSector") == 0)
147 {
148 LoadAndBootBootSector(OperatingSystemSectionNames[SelectedOperatingSystem]);
149 }
150 else if (_stricmp(SettingValue, "Partition") == 0)
151 {
152 LoadAndBootPartition(OperatingSystemSectionNames[SelectedOperatingSystem]);
153 }
154 else if (_stricmp(SettingValue, "Drive") == 0)
155 {
156 LoadAndBootDrive(OperatingSystemSectionNames[SelectedOperatingSystem]);
157 }
158 #endif
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_PTR 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_PTR 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 }