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