Fix some Rtl Prototype inconsistencies, more are in ntifs/winddk but i have fixed...
[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 U32 SectionId;
45 U32 OperatingSystemCount;
46 PUCHAR *OperatingSystemSectionNames;
47 PUCHAR *OperatingSystemDisplayNames;
48 U32 DefaultOperatingSystem;
49 S32 TimeOut;
50 U32 SelectedOperatingSystem;
51
52 if (!IniFileInitialize())
53 {
54 printf("Press any key to reboot.\n");
55 MachConsGetCh();
56 return;
57 }
58
59 if (!IniOpenSection("FreeLoader", &SectionId))
60 {
61 printf("Section [FreeLoader] not found in freeldr.ini.\n");
62 MachConsGetCh();
63 return;
64 }
65 TimeOut = GetTimeOut();
66
67 if (!UiInitialize(TimeOut))
68 {
69 printf("Press any key to reboot.\n");
70 MachConsGetCh();
71 return;
72 }
73
74
75 if (!InitOperatingSystemList(&OperatingSystemSectionNames, &OperatingSystemDisplayNames, &OperatingSystemCount))
76 {
77 UiMessageBox("Press ENTER to reboot.\n");
78 goto reboot;
79 }
80
81 if (OperatingSystemCount == 0)
82 {
83 UiMessageBox("There were no operating systems listed in freeldr.ini.\nPress ENTER to reboot.");
84 goto reboot;
85 }
86
87 DefaultOperatingSystem = GetDefaultOperatingSystem(OperatingSystemSectionNames, OperatingSystemCount);
88
89 //
90 // Find all the message box settings and run them
91 //
92 UiShowMessageBoxesInSection("FreeLoader");
93
94 for (;;)
95 {
96
97 /* If Timeout is 0, don't even bother loading any gui */
98 if (!UserInterfaceUp) {
99 goto NoGui;
100 }
101
102 // Redraw the backdrop
103 UiDrawBackdrop();
104
105 // Show the operating system list menu
106 if (!UiDisplayMenu(OperatingSystemDisplayNames, OperatingSystemCount, DefaultOperatingSystem, TimeOut, &SelectedOperatingSystem, FALSE, MainBootMenuKeyPressFilter))
107 {
108 UiMessageBox("Press ENTER to reboot.\n");
109 goto reboot;
110 }
111
112 NoGui:
113 TimeOut = -1;
114 DefaultOperatingSystem = SelectedOperatingSystem;
115
116 // Try to open the operating system section in the .ini file
117 if (!IniOpenSection(OperatingSystemSectionNames[SelectedOperatingSystem], &SectionId))
118 {
119 sprintf(SettingName, "Section [%s] not found in freeldr.ini.\n", OperatingSystemSectionNames[SelectedOperatingSystem]);
120 UiMessageBox(SettingName);
121 continue;
122 }
123
124 // Try to read the boot type
125 if (!IniReadSettingByName(SectionId, "BootType", SettingValue, 80))
126 {
127 sprintf(SettingName, "BootType= line not found in section [%s] in freeldr.ini.\n", OperatingSystemSectionNames[SelectedOperatingSystem]);
128 UiMessageBox(SettingName);
129 continue;
130 }
131
132 // Install the drive mapper according to this sections drive mappings
133 DriveMapMapDrivesInSection(OperatingSystemSectionNames[SelectedOperatingSystem]);
134 if (stricmp(SettingValue, "ReactOS") == 0)
135 {
136 LoadAndBootReactOS(OperatingSystemSectionNames[SelectedOperatingSystem]);
137 }
138 else if (stricmp(SettingValue, "Linux") == 0)
139 {
140 LoadAndBootLinux(OperatingSystemSectionNames[SelectedOperatingSystem], OperatingSystemDisplayNames[SelectedOperatingSystem]);
141 }
142 else if (stricmp(SettingValue, "BootSector") == 0)
143 {
144 LoadAndBootBootSector(OperatingSystemSectionNames[SelectedOperatingSystem]);
145 }
146 else if (stricmp(SettingValue, "Partition") == 0)
147 {
148 LoadAndBootPartition(OperatingSystemSectionNames[SelectedOperatingSystem]);
149 }
150 else if (stricmp(SettingValue, "Drive") == 0)
151 {
152 LoadAndBootDrive(OperatingSystemSectionNames[SelectedOperatingSystem]);
153 }
154 }
155
156
157 reboot:
158 UiUnInitialize("Rebooting...");
159 return;
160 }
161
162 U32 GetDefaultOperatingSystem(PUCHAR OperatingSystemList[], U32 OperatingSystemCount)
163 {
164 UCHAR DefaultOSText[80];
165 char* DefaultOSName;
166 U32 SectionId;
167 U32 DefaultOS = 0;
168 U32 Idx;
169
170 if (!IniOpenSection("FreeLoader", &SectionId))
171 {
172 return 0;
173 }
174
175 DefaultOSName = CmdLineGetDefaultOS();
176 if (NULL == DefaultOSName)
177 {
178 if (IniReadSettingByName(SectionId, "DefaultOS", DefaultOSText, 80))
179 {
180 DefaultOSName = DefaultOSText;
181 }
182 }
183
184 if (NULL != DefaultOSName)
185 {
186 for (Idx=0; Idx<OperatingSystemCount; Idx++)
187 {
188 if (stricmp(DefaultOSName, OperatingSystemList[Idx]) == 0)
189 {
190 DefaultOS = Idx;
191 break;
192 }
193 }
194 }
195
196 return DefaultOS;
197 }
198
199 S32 GetTimeOut(VOID)
200 {
201 UCHAR TimeOutText[20];
202 S32 TimeOut;
203 U32 SectionId;
204
205 TimeOut = CmdLineGetTimeOut();
206 if (0 <= TimeOut)
207 {
208 return TimeOut;
209 }
210
211 if (!IniOpenSection("FreeLoader", &SectionId))
212 {
213 return -1;
214 }
215
216 if (IniReadSettingByName(SectionId, "TimeOut", TimeOutText, 20))
217 {
218 TimeOut = atoi(TimeOutText);
219 }
220 else
221 {
222 TimeOut = -1;
223 }
224
225 return TimeOut;
226 }
227
228 BOOL MainBootMenuKeyPressFilter(U32 KeyPress)
229 {
230 if (KeyPress == KEY_F8)
231 {
232 DoOptionsMenu();
233
234 return TRUE;
235 }
236
237 // We didn't handle the key
238 return FALSE;
239 }