5b4828cfbc8e1ee1326b4b16cb649d4f6ee4fb35
[reactos.git] / freeldr / freeldr / miscboot.c
1 /*
2 * FreeLoader
3 * Copyright (C) 1998-2003 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 <inifile.h>
28 #include <disk.h>
29 #include <drivemap.h>
30 #include <machine.h>
31
32 VOID LoadAndBootBootSector(PUCHAR OperatingSystemName)
33 {
34 PFILE FilePointer;
35 UCHAR SettingName[80];
36 UCHAR SettingValue[80];
37 U32 SectionId;
38 UCHAR FileName[260];
39 U32 BytesRead;
40
41 // Find all the message box settings and run them
42 UiShowMessageBoxesInSection(OperatingSystemName);
43
44 // Try to open the operating system section in the .ini file
45 if (!IniOpenSection(OperatingSystemName, &SectionId))
46 {
47 sprintf(SettingName, "Section [%s] not found in freeldr.ini.\n", OperatingSystemName);
48 UiMessageBox(SettingName);
49 return;
50 }
51
52 if (!IniReadSettingByName(SectionId, "BootDrive", SettingValue, 80))
53 {
54 UiMessageBox("Boot drive not specified for selected OS!");
55 return;
56 }
57
58 BootDrive = DriveMapGetBiosDriveNumber(SettingValue);
59
60 BootPartition = 0;
61 if (IniReadSettingByName(SectionId, "BootPartition", SettingValue, 80))
62 {
63 BootPartition = atoi(SettingValue);
64 }
65
66 if (!IniReadSettingByName(SectionId, "BootSectorFile", FileName, 260))
67 {
68 UiMessageBox("Boot sector file not specified for selected OS!");
69 return;
70 }
71
72 if (!FsOpenVolume(BootDrive, BootPartition))
73 {
74 UiMessageBox("Failed to open boot drive.");
75 return;
76 }
77
78 FilePointer = FsOpenFile(FileName);
79 if (FilePointer == NULL)
80 {
81 strcat(FileName, " not found.");
82 UiMessageBox(FileName);
83 return;
84 }
85
86 // Read boot sector
87 if (!FsReadFile(FilePointer, 512, &BytesRead, (void*)0x7c00) || (BytesRead != 512))
88 {
89 return;
90 }
91
92 // Check for validity
93 if (*((U16*)(0x7c00 + 0x1fe)) != 0xaa55)
94 {
95 UiMessageBox("Invalid boot sector magic (0xaa55)");
96 return;
97 }
98
99 UiUnInitialize("Booting...");
100 // Don't stop the floppy drive motor when we
101 // are just booting a bootsector, or drive, or partition.
102 // If we were to stop the floppy motor then
103 // the BIOS wouldn't be informed and if the
104 // next read is to a floppy then the BIOS will
105 // still think the motor is on and this will
106 // result in a read error.
107 //DiskStopFloppyMotor();
108 //DisableA20();
109 ChainLoadBiosBootSectorCode();
110 }
111
112 VOID LoadAndBootPartition(PUCHAR OperatingSystemName)
113 {
114 UCHAR SettingName[80];
115 UCHAR SettingValue[80];
116 U32 SectionId;
117 PARTITION_TABLE_ENTRY PartitionTableEntry;
118
119 // Find all the message box settings and run them
120 UiShowMessageBoxesInSection(OperatingSystemName);
121
122 // Try to open the operating system section in the .ini file
123 if (!IniOpenSection(OperatingSystemName, &SectionId))
124 {
125 sprintf(SettingName, "Section [%s] not found in freeldr.ini.\n", OperatingSystemName);
126 UiMessageBox(SettingName);
127 return;
128 }
129
130 // Read the boot drive
131 if (!IniReadSettingByName(SectionId, "BootDrive", SettingValue, 80))
132 {
133 UiMessageBox("Boot drive not specified for selected OS!");
134 return;
135 }
136
137 BootDrive = DriveMapGetBiosDriveNumber(SettingValue);
138
139 // Read the boot partition
140 if (!IniReadSettingByName(SectionId, "BootPartition", SettingValue, 80))
141 {
142 UiMessageBox("Boot partition not specified for selected OS!");
143 return;
144 }
145
146 BootPartition = atoi(SettingValue);
147
148 // Get the partition table entry
149 if (!DiskGetPartitionEntry(BootDrive, BootPartition, &PartitionTableEntry))
150 {
151 return;
152 }
153
154 // Now try to read the partition boot sector
155 // If this fails then abort
156 if (!MachDiskReadLogicalSectors(BootDrive, PartitionTableEntry.SectorCountBeforePartition, 1, (PVOID)0x7C00))
157 {
158 return;
159 }
160
161 // Check for validity
162 if (*((U16*)(0x7c00 + 0x1fe)) != 0xaa55)
163 {
164 UiMessageBox("Invalid boot sector magic (0xaa55)");
165 return;
166 }
167
168 UiUnInitialize("Booting...");
169 // Don't stop the floppy drive motor when we
170 // are just booting a bootsector, or drive, or partition.
171 // If we were to stop the floppy motor then
172 // the BIOS wouldn't be informed and if the
173 // next read is to a floppy then the BIOS will
174 // still think the motor is on and this will
175 // result in a read error.
176 //DiskStopFloppyMotor();
177 //DisableA20();
178 ChainLoadBiosBootSectorCode();
179 }
180
181 VOID LoadAndBootDrive(PUCHAR OperatingSystemName)
182 {
183 UCHAR SettingName[80];
184 UCHAR SettingValue[80];
185 U32 SectionId;
186
187 // Find all the message box settings and run them
188 UiShowMessageBoxesInSection(OperatingSystemName);
189
190 // Try to open the operating system section in the .ini file
191 if (!IniOpenSection(OperatingSystemName, &SectionId))
192 {
193 sprintf(SettingName, "Section [%s] not found in freeldr.ini.\n", OperatingSystemName);
194 UiMessageBox(SettingName);
195 return;
196 }
197
198 if (!IniReadSettingByName(SectionId, "BootDrive", SettingValue, 80))
199 {
200 UiMessageBox("Boot drive not specified for selected OS!");
201 return;
202 }
203
204 BootDrive = DriveMapGetBiosDriveNumber(SettingValue);
205
206 // Now try to read the boot sector (or mbr)
207 // If this fails then abort
208 if (!MachDiskReadLogicalSectors(BootDrive, 0, 1, (PVOID)0x7C00))
209 {
210 return;
211 }
212
213 // Check for validity
214 if (*((U16*)(0x7c00 + 0x1fe)) != 0xaa55)
215 {
216 UiMessageBox("Invalid boot sector magic (0xaa55)");
217 return;
218 }
219
220 UiUnInitialize("Booting...");
221 // Don't stop the floppy drive motor when we
222 // are just booting a bootsector, or drive, or partition.
223 // If we were to stop the floppy motor then
224 // the BIOS wouldn't be informed and if the
225 // next read is to a floppy then the BIOS will
226 // still think the motor is on and this will
227 // result in a read error.
228 //DiskStopFloppyMotor();
229 //DisableA20();
230 ChainLoadBiosBootSectorCode();
231 }