Fix some registry structures to match the Windows format.
[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 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, 80))
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, "Linux") == 0)
128 {
129 LoadAndBootLinux(OperatingSystemSectionNames[SelectedOperatingSystem], OperatingSystemDisplayNames[SelectedOperatingSystem]);
130 }
131 else if (_stricmp(SettingValue, "BootSector") == 0)
132 {
133 LoadAndBootBootSector(OperatingSystemSectionNames[SelectedOperatingSystem]);
134 }
135 else if (_stricmp(SettingValue, "Partition") == 0)
136 {
137 LoadAndBootPartition(OperatingSystemSectionNames[SelectedOperatingSystem]);
138 }
139 else if (_stricmp(SettingValue, "Drive") == 0)
140 {
141 LoadAndBootDrive(OperatingSystemSectionNames[SelectedOperatingSystem]);
142 }
143 }
144
145
146 reboot:
147 UiUnInitialize("Rebooting...");
148 return;
149 }
150
151 ULONG GetDefaultOperatingSystem(PCSTR OperatingSystemList[], ULONG OperatingSystemCount)
152 {
153 CHAR DefaultOSText[80];
154 PCSTR DefaultOSName;
155 ULONG SectionId;
156 ULONG DefaultOS = 0;
157 ULONG Idx;
158
159 if (!IniOpenSection("FreeLoader", &SectionId))
160 {
161 return 0;
162 }
163
164 DefaultOSName = CmdLineGetDefaultOS();
165 if (NULL == DefaultOSName)
166 {
167 if (IniReadSettingByName(SectionId, "DefaultOS", DefaultOSText, 80))
168 {
169 DefaultOSName = DefaultOSText;
170 }
171 }
172
173 if (NULL != DefaultOSName)
174 {
175 for (Idx=0; Idx<OperatingSystemCount; Idx++)
176 {
177 if (_stricmp(DefaultOSName, OperatingSystemList[Idx]) == 0)
178 {
179 DefaultOS = Idx;
180 break;
181 }
182 }
183 }
184
185 return DefaultOS;
186 }
187
188 LONG GetTimeOut(VOID)
189 {
190 CHAR TimeOutText[20];
191 LONG TimeOut;
192 ULONG SectionId;
193
194 TimeOut = CmdLineGetTimeOut();
195 if (0 <= TimeOut)
196 {
197 return TimeOut;
198 }
199
200 if (!IniOpenSection("FreeLoader", &SectionId))
201 {
202 return -1;
203 }
204
205 if (IniReadSettingByName(SectionId, "TimeOut", TimeOutText, 20))
206 {
207 TimeOut = atoi(TimeOutText);
208 }
209 else
210 {
211 TimeOut = -1;
212 }
213
214 return TimeOut;
215 }
216
217 BOOL MainBootMenuKeyPressFilter(ULONG KeyPress)
218 {
219 if (KeyPress == KEY_F8)
220 {
221 DoOptionsMenu();
222
223 return TRUE;
224 }
225
226 // We didn't handle the key
227 return FALSE;
228 }