7ed45be6a8086278871d6550b6d30af86b63c5e2
[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 "parseini.h"
30 #include "debug.h"
31 #include "oslist.h"
32 #include "cache.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 enable_a20();
54
55 #ifdef DEBUG
56 DebugInit();
57 #endif
58
59 InitMemoryManager();
60
61 if (!ParseIniFile())
62 {
63 printf("Press any key to reboot.\n");
64 getch();
65 return;
66 }
67
68 if (!OpenSection("FreeLoader", &SectionId))
69 {
70 printf("Section [FreeLoader] not found in freeldr.ini.\n");
71 getch();
72 return;
73 }
74
75 if (!InitUserInterface())
76 {
77 printf("Press any key to reboot.\n");
78 getch();
79 return;
80 }
81
82 if (!InitOperatingSystemList(&OperatingSystemSectionNames, &OperatingSystemDisplayNames, &OperatingSystemCount))
83 {
84 MessageBox("Press ENTER to reboot.\n");
85 goto reboot;
86 }
87
88 if (OperatingSystemCount == 0)
89 {
90 MessageBox("There were no operating systems listed in freeldr.ini.\nPress ENTER to reboot.");
91 goto reboot;
92 }
93
94 DefaultOperatingSystem = GetDefaultOperatingSystem(OperatingSystemSectionNames, OperatingSystemCount);
95 TimeOut = GetTimeOut();
96
97 //
98 // Find all the message box settings and run them
99 //
100 ShowMessageBoxesInSection("FreeLoader");
101
102 for (;;)
103 {
104 // Show the operating system list menu
105 if (!DisplayMenu(OperatingSystemDisplayNames, OperatingSystemCount, DefaultOperatingSystem, TimeOut, &SelectedOperatingSystem))
106 {
107 MessageBox("Press ENTER to reboot.\n");
108 goto reboot;
109 }
110
111 // Try to open the operating system section in the .ini file
112 if (!OpenSection(OperatingSystemSectionNames[SelectedOperatingSystem], &SectionId))
113 {
114 sprintf(SettingName, "Section [%s] not found in freeldr.ini.\n", OperatingSystemSectionNames[SelectedOperatingSystem]);
115 MessageBox(SettingName);
116 continue;
117 }
118
119 // Try to read the boot type
120 if (!ReadSectionSettingByName(SectionId, "BootType", SettingValue, 80))
121 {
122 sprintf(SettingName, "BootType= line not found in section [%s] in freeldr.ini.\n", OperatingSystemSectionNames[SelectedOperatingSystem]);
123 MessageBox(SettingName);
124 continue;
125 }
126
127 if (stricmp(SettingValue, "ReactOS") == 0)
128 {
129 LoadAndBootReactOS(OperatingSystemSectionNames[SelectedOperatingSystem]);
130 }
131 else if (stricmp(SettingValue, "Linux") == 0)
132 {
133 MessageBox("Cannot boot this OS type yet!");
134 }
135 else if (stricmp(SettingValue, "BootSector") == 0)
136 {
137 LoadAndBootBootSector(OperatingSystemSectionNames[SelectedOperatingSystem]);
138 }
139 else if (stricmp(SettingValue, "Partition") == 0)
140 {
141 LoadAndBootPartition(OperatingSystemSectionNames[SelectedOperatingSystem]);
142 }
143 else if (stricmp(SettingValue, "Drive") == 0)
144 {
145 LoadAndBootDrive(OperatingSystemSectionNames[SelectedOperatingSystem]);
146 }
147 }
148
149
150 reboot:
151 clrscr();
152 showcursor();
153 return;
154 }
155
156 ULONG GetDefaultOperatingSystem(PUCHAR OperatingSystemList[], ULONG OperatingSystemCount)
157 {
158 UCHAR DefaultOSText[80];
159 ULONG SectionId;
160 ULONG DefaultOS = 0;
161 ULONG Idx;
162
163 if (!OpenSection("FreeLoader", &SectionId))
164 {
165 return 0;
166 }
167
168 if (ReadSectionSettingByName(SectionId, "DefaultOS", DefaultOSText, 80))
169 {
170 for (Idx=0; Idx<OperatingSystemCount; Idx++)
171 {
172 if (stricmp(DefaultOSText, OperatingSystemList[Idx]) == 0)
173 {
174 DefaultOS = Idx;
175 break;
176 }
177 }
178 }
179
180 return DefaultOS;
181 }
182
183 LONG GetTimeOut(VOID)
184 {
185 UCHAR TimeOutText[20];
186 ULONG TimeOut;
187 ULONG SectionId;
188
189 if (!OpenSection("FreeLoader", &SectionId))
190 {
191 return -1;
192 }
193
194 if (ReadSectionSettingByName(SectionId, "TimeOut", TimeOutText, 20))
195 {
196 TimeOut = atoi(TimeOutText);
197 }
198 else
199 {
200 TimeOut = -1;
201 }
202
203 return TimeOut;
204 }