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