e8d26cbf7f665b3c07fd65210cf0797bdd10a1f2
[reactos.git] / freeldr / freeldr / freeldr.c
1 /*
2 * FreeLoader
3 * Copyright (C) 1998-2002 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 <oslist.h>
32 #include <video.h>
33
34 // Variable BootDrive moved to asmcode.S
35 //ULONG BootDrive = 0; // BIOS boot drive, 0-A:, 1-B:, 0x80-C:, 0x81-D:, etc.
36 ULONG BootPartition = 0; // Boot Partition, 1-4
37
38 ULONG GetDefaultOperatingSystem(PUCHAR OperatingSystemList[], ULONG OperatingSystemCount);
39 LONG GetTimeOut(VOID);
40
41 VOID BootMain(VOID)
42 {
43 UCHAR SettingName[80];
44 UCHAR SettingValue[80];
45 ULONG SectionId;
46 ULONG OperatingSystemCount;
47 PUCHAR *OperatingSystemSectionNames;
48 PUCHAR *OperatingSystemDisplayNames;
49 ULONG DefaultOperatingSystem;
50 LONG TimeOut;
51 ULONG SelectedOperatingSystem;
52
53 EnableA20();
54
55 #ifdef DEBUG
56 DebugInit();
57 #endif
58
59 if (!MmInitializeMemoryManager())
60 {
61 printf("Press any key to reboot.\n");
62 getch();
63 return;
64 }
65
66 if (!IniFileInitialize())
67 {
68 printf("Press any key to reboot.\n");
69 getch();
70 return;
71 }
72
73 if (!IniOpenSection("FreeLoader", &SectionId))
74 {
75 printf("Section [FreeLoader] not found in freeldr.ini.\n");
76 getch();
77 return;
78 }
79
80 if (!UiInitialize())
81 {
82 printf("Press any key to reboot.\n");
83 getch();
84 return;
85 }
86
87 if (!InitOperatingSystemList(&OperatingSystemSectionNames, &OperatingSystemDisplayNames, &OperatingSystemCount))
88 {
89 UiMessageBox("Press ENTER to reboot.\n");
90 goto reboot;
91 }
92
93 if (OperatingSystemCount == 0)
94 {
95 UiMessageBox("There were no operating systems listed in freeldr.ini.\nPress ENTER to reboot.");
96 goto reboot;
97 }
98
99 DefaultOperatingSystem = GetDefaultOperatingSystem(OperatingSystemSectionNames, OperatingSystemCount);
100 TimeOut = GetTimeOut();
101
102 //
103 // Find all the message box settings and run them
104 //
105 UiShowMessageBoxesInSection("FreeLoader");
106
107 for (;;)
108 {
109 // Redraw the backdrop
110 UiDrawBackdrop();
111
112 // Show the operating system list menu
113 if (!UiDisplayMenu(OperatingSystemDisplayNames, OperatingSystemCount, DefaultOperatingSystem, TimeOut, &SelectedOperatingSystem))
114 {
115 UiMessageBox("Press ENTER to reboot.\n");
116 goto reboot;
117 }
118 TimeOut = -1;
119 DefaultOperatingSystem = SelectedOperatingSystem;
120
121 // Try to open the operating system section in the .ini file
122 if (!IniOpenSection(OperatingSystemSectionNames[SelectedOperatingSystem], &SectionId))
123 {
124 sprintf(SettingName, "Section [%s] not found in freeldr.ini.\n", OperatingSystemSectionNames[SelectedOperatingSystem]);
125 UiMessageBox(SettingName);
126 continue;
127 }
128
129 // Try to read the boot type
130 if (!IniReadSettingByName(SectionId, "BootType", SettingValue, 80))
131 {
132 sprintf(SettingName, "BootType= line not found in section [%s] in freeldr.ini.\n", OperatingSystemSectionNames[SelectedOperatingSystem]);
133 UiMessageBox(SettingName);
134 continue;
135 }
136
137 if (stricmp(SettingValue, "ReactOS") == 0)
138 {
139 LoadAndBootReactOS(OperatingSystemSectionNames[SelectedOperatingSystem]);
140 }
141 else if (stricmp(SettingValue, "Linux") == 0)
142 {
143 LoadAndBootLinux(OperatingSystemSectionNames[SelectedOperatingSystem]);
144 }
145 else if (stricmp(SettingValue, "BootSector") == 0)
146 {
147 LoadAndBootBootSector(OperatingSystemSectionNames[SelectedOperatingSystem]);
148 }
149 else if (stricmp(SettingValue, "Partition") == 0)
150 {
151 LoadAndBootPartition(OperatingSystemSectionNames[SelectedOperatingSystem]);
152 }
153 else if (stricmp(SettingValue, "Drive") == 0)
154 {
155 LoadAndBootDrive(OperatingSystemSectionNames[SelectedOperatingSystem]);
156 }
157 }
158
159
160 reboot:
161 VideoClearScreen();
162 VideoShowTextCursor();
163 return;
164 }
165
166 ULONG GetDefaultOperatingSystem(PUCHAR OperatingSystemList[], ULONG OperatingSystemCount)
167 {
168 UCHAR DefaultOSText[80];
169 ULONG SectionId;
170 ULONG DefaultOS = 0;
171 ULONG Idx;
172
173 if (!IniOpenSection("FreeLoader", &SectionId))
174 {
175 return 0;
176 }
177
178 if (IniReadSettingByName(SectionId, "DefaultOS", DefaultOSText, 80))
179 {
180 for (Idx=0; Idx<OperatingSystemCount; Idx++)
181 {
182 if (stricmp(DefaultOSText, OperatingSystemList[Idx]) == 0)
183 {
184 DefaultOS = Idx;
185 break;
186 }
187 }
188 }
189
190 return DefaultOS;
191 }
192
193 LONG GetTimeOut(VOID)
194 {
195 UCHAR TimeOutText[20];
196 ULONG TimeOut;
197 ULONG SectionId;
198
199 if (!IniOpenSection("FreeLoader", &SectionId))
200 {
201 return -1;
202 }
203
204 if (IniReadSettingByName(SectionId, "TimeOut", TimeOutText, 20))
205 {
206 TimeOut = atoi(TimeOutText);
207 }
208 else
209 {
210 TimeOut = -1;
211 }
212
213 return TimeOut;
214 }