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