Reworked the boot sector & boot drive code
[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 #include "disk.h"
29
30 VOID LoadAndBootBootSector(PUCHAR OperatingSystemName)
31 {
32 PFILE FilePointer;
33 UCHAR SettingName[80];
34 UCHAR SettingValue[80];
35 ULONG SectionId;
36 UCHAR FileName[260];
37 ULONG BytesRead;
38
39 // Find all the message box settings and run them
40 ShowMessageBoxesInSection(OperatingSystemName);
41
42 // Try to open the operating system section in the .ini file
43 if (!OpenSection(OperatingSystemName, &SectionId))
44 {
45 sprintf(SettingName, "Section [%s] not found in freeldr.ini.\n", OperatingSystemName);
46 MessageBox(SettingName);
47 return;
48 }
49
50 if (!ReadSectionSettingByName(SectionId, "BootDrive", SettingValue, 80))
51 {
52 MessageBox("Boot drive not specified for selected OS!");
53 return;
54 }
55
56 BootDrive = atoi(SettingValue);
57
58 BootPartition = 0;
59 if (ReadSectionSettingByName(SectionId, "BootPartition", SettingValue, 80))
60 {
61 BootPartition = atoi(SettingValue);
62 }
63
64 if (!ReadSectionSettingByName(SectionId, "BootSectorFile", FileName, 260))
65 {
66 MessageBox("Boot sector file not specified for selected OS!");
67 return;
68 }
69
70 if (!OpenDiskDrive(BootDrive, BootPartition))
71 {
72 MessageBox("Failed to open boot drive.");
73 return;
74 }
75
76 FilePointer = OpenFile(FileName);
77 if (FilePointer == NULL)
78 {
79 strcat(FileName, " not found.");
80 MessageBox(FileName);
81 return;
82 }
83
84 // Read boot sector
85 if (!ReadFile(FilePointer, 512, &BytesRead, (void*)0x7c00) || (BytesRead != 512))
86 {
87 DiskError("Disk read error.");
88 return;
89 }
90
91 // Check for validity
92 if (*((WORD*)(0x7c00 + 0x1fe)) != 0xaa55)
93 {
94 MessageBox("Invalid boot sector magic (0xaa55)");
95 return;
96 }
97
98 clrscr();
99 showcursor();
100 stop_floppy();
101 JumpToBootCode();
102 }
103
104 VOID LoadAndBootPartition(PUCHAR OperatingSystemName)
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(PUCHAR OperatingSystemName)
191 {
192 UCHAR SettingName[80];
193 UCHAR SettingValue[80];
194 ULONG SectionId;
195
196 // Find all the message box settings and run them
197 ShowMessageBoxesInSection(OperatingSystemName);
198
199 // Try to open the operating system section in the .ini file
200 if (!OpenSection(OperatingSystemName, &SectionId))
201 {
202 sprintf(SettingName, "Section [%s] not found in freeldr.ini.\n", OperatingSystemName);
203 MessageBox(SettingName);
204 return;
205 }
206
207 if (!ReadSectionSettingByName(SectionId, "BootDrive", SettingValue, 80))
208 {
209 MessageBox("Boot drive not specified for selected OS!");
210 return;
211 }
212
213 BootDrive = atoi(SettingValue);
214
215 if (!BiosInt13Read(BootDrive, 0, 0, 1, 1, (PVOID)0x7C00))
216 {
217 DiskError("Disk read error.");
218 return;
219 }
220
221 // Check for validity
222 if (*((WORD*)(0x7c00 + 0x1fe)) != 0xaa55)
223 {
224 MessageBox("Invalid boot sector magic (0xaa55)");
225 return;
226 }
227
228 clrscr();
229 showcursor();
230 stop_floppy();
231 JumpToBootCode();
232 }