Forgot to add a semi-colon at end of statement
[reactos.git] / freeldr / freeldr / miscboot.c
1 /*
2 * FreeLoader
3 * Copyright (C) 1999, 2000 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
21 #include "freeldr.h"
22 #include "asmcode.h"
23 #include "miscboot.h"
24 #include "stdlib.h"
25 #include "fs.h"
26 #include "tui.h"
27
28 void LoadAndBootBootSector(int nOSToBoot)
29 {
30 FILE file;
31 char name[260];
32 char value[260];
33 char szFileName[1024];
34 int i;
35
36 // Find all the message box settings and run them
37 for (i=1; i<=GetNumSectionItems(OSList[nOSToBoot].name); i++)
38 {
39 ReadSectionSettingByNumber(OSList[nOSToBoot].name, i, name, value);
40 if (stricmp(name, "MessageBox") == 0)
41 MessageBox(value);
42 if (stricmp(name, "MessageLine") == 0)
43 MessageLine(value);
44 }
45
46 if (!ReadSectionSettingByName(OSList[nOSToBoot].name, "BootDrive", name, value))
47 {
48 MessageBox("Boot drive not specified for selected OS!");
49 return;
50 }
51
52 BootDrive = atoi(value);
53
54 BootPartition = 0;
55 if (ReadSectionSettingByName(OSList[nOSToBoot].name, "BootPartition", name, value))
56 BootPartition = atoi(value);
57
58 if (!ReadSectionSettingByName(OSList[nOSToBoot].name, "BootSector", name, value))
59 {
60 MessageBox("Boot sector file not specified for selected OS!");
61 return;
62 }
63
64 if (!OpenDiskDrive(BootDrive, BootPartition))
65 {
66 MessageBox("Failed to open boot drive.");
67 return;
68 }
69
70 strcpy(szFileName, value);
71 if (!OpenFile(szFileName, &file))
72 {
73 strcat(value, " not found.");
74 MessageBox(value);
75 return;
76 }
77
78 // Read boot sector
79 if (ReadFile(&file, 512, (void*)0x7c00) != 512)
80 {
81 MessageBox("Disk Read Error");
82 return;
83 }
84
85 // Check for validity
86 if (*((WORD*)(0x7c00 + 0x1fe)) != 0xaa55)
87 {
88 MessageBox("Invalid boot sector magic (0xaa55)");
89 return;
90 }
91
92 RestoreScreen(pScreenBuffer);
93 showcursor();
94 gotoxy(nCursorXPos, nCursorYPos);
95
96 stop_floppy();
97 JumpToBootCode();
98 }
99
100 void LoadAndBootPartition(int nOSToBoot)
101 {
102 char name[260];
103 char value[260];
104 int head, sector, cylinder;
105 int offset;
106 int i;
107
108 // Find all the message box settings and run them
109 for (i=1; i<=GetNumSectionItems(OSList[nOSToBoot].name); i++)
110 {
111 ReadSectionSettingByNumber(OSList[nOSToBoot].name, i, name, value);
112 if (stricmp(name, "MessageBox") == 0)
113 MessageBox(value);
114 if (stricmp(name, "MessageLine") == 0)
115 MessageLine(value);
116 }
117
118 if (!ReadSectionSettingByName(OSList[nOSToBoot].name, "BootDrive", name, value))
119 {
120 MessageBox("Boot drive not specified for selected OS!");
121 return;
122 }
123
124 BootDrive = atoi(value);
125
126 if (!ReadSectionSettingByName(OSList[nOSToBoot].name, "BootPartition", name, value))
127 {
128 MessageBox("Boot partition not specified for selected OS!");
129 return;
130 }
131
132 BootPartition = atoi(value);
133
134 if (!biosdisk(_DISK_READ, BootDrive, 0, 0, 1, 1, SectorBuffer))
135 {
136 MessageBox("Disk Read Error");
137 return;
138 }
139
140 // Check for validity
141 if (*((WORD*)(SectorBuffer + 0x1fe)) != 0xaa55)
142 {
143 MessageBox("Invalid partition table magic (0xaa55)");
144 return;
145 }
146
147 offset = 0x1BE + ((BootPartition-1) * 0x10);
148
149 // Check for valid partition
150 if (SectorBuffer[offset + 4] == 0)
151 {
152 MessageBox("Invalid boot partition");
153 return;
154 }
155
156 head = SectorBuffer[offset + 1];
157 sector = (SectorBuffer[offset + 2] & 0x3F);
158 cylinder = SectorBuffer[offset + 3];
159 if (SectorBuffer[offset + 2] & 0x80)
160 cylinder += 0x200;
161 if (SectorBuffer[offset + 2] & 0x40)
162 cylinder += 0x100;
163
164 // Read partition boot sector
165 if (!biosdisk(_DISK_READ, BootDrive, head, cylinder, sector, 1, (void*)0x7c00))
166 {
167 MessageBox("Disk Read Error");
168 return;
169 }
170
171 // Check for validity
172 if (*((WORD*)(0x7c00 + 0x1fe)) != 0xaa55)
173 {
174 MessageBox("Invalid boot sector magic (0xaa55)");
175 return;
176 }
177
178 RestoreScreen(pScreenBuffer);
179 showcursor();
180 gotoxy(nCursorXPos, nCursorYPos);
181
182 stop_floppy();
183 JumpToBootCode();
184 }
185
186 void LoadAndBootDrive(int nOSToBoot)
187 {
188 char name[260];
189 char value[260];
190 int i;
191
192 // Find all the message box settings and run them
193 for (i=1; i<=GetNumSectionItems(OSList[nOSToBoot].name); i++)
194 {
195 ReadSectionSettingByNumber(OSList[nOSToBoot].name, i, name, value);
196 if (stricmp(name, "MessageBox") == 0)
197 MessageBox(value);
198 if (stricmp(name, "MessageLine") == 0)
199 MessageLine(value);
200 }
201
202 if (!ReadSectionSettingByName(OSList[nOSToBoot].name, "BootDrive", name, value))
203 {
204 MessageBox("Boot drive not specified for selected OS!");
205 return;
206 }
207
208 BootDrive = atoi(value);
209
210 if (!biosdisk(_DISK_READ, BootDrive, 0, 0, 1, 1, (void*)0x7c00))
211 {
212 MessageBox("Disk Read Error");
213 return;
214 }
215
216 // Check for validity
217 if (*((WORD*)(0x7c00 + 0x1fe)) != 0xaa55)
218 {
219 MessageBox("Invalid boot sector magic (0xaa55)");
220 return;
221 }
222
223 RestoreScreen(pScreenBuffer);
224 showcursor();
225 gotoxy(nCursorXPos, nCursorYPos);
226
227 stop_floppy();
228 JumpToBootCode();
229 }