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