Create the AHCI branch for Aman's work
[reactos.git] / boot / 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 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 #ifdef _M_IX86
21
22 /* INCLUDES *******************************************************************/
23
24 #include <freeldr.h>
25
26 /* FUNCTIONS ******************************************************************/
27
28 VOID
29 LoadAndBootBootSector(IN OperatingSystemItem* OperatingSystem,
30 IN USHORT OperatingSystemVersion)
31 {
32 ULONG_PTR SectionId;
33 PCSTR SectionName = OperatingSystem->SystemPartition;
34 CHAR FileName[260];
35 PFILE FilePointer;
36 ULONG BytesRead;
37
38 /* Find all the message box settings and run them */
39 UiShowMessageBoxesInSection(SectionName);
40
41 /* Try to open the operating system section in the .ini file */
42 if (!IniOpenSection(SectionName, &SectionId))
43 {
44 UiMessageBox("Section [%s] not found in freeldr.ini.", SectionName);
45 return;
46 }
47
48 if (!IniReadSettingByName(SectionId, "BootSectorFile", FileName, sizeof(FileName)))
49 {
50 UiMessageBox("Boot sector file not specified for selected OS!");
51 return;
52 }
53
54 FilePointer = FsOpenFile(FileName);
55 if (!FilePointer)
56 {
57 UiMessageBox("%s not found.", 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
77 /*
78 * Don't stop the floppy drive motor when we
79 * are just booting a bootsector, or drive, or partition.
80 * If we were to stop the floppy motor then
81 * the BIOS wouldn't be informed and if the
82 * next read is to a floppy then the BIOS will
83 * still think the motor is on and this will
84 * result in a read error.
85 */
86 // DiskStopFloppyMotor();
87 // DisableA20();
88 ChainLoadBiosBootSectorCode();
89 }
90
91 VOID
92 LoadAndBootPartition(IN OperatingSystemItem* OperatingSystem,
93 IN USHORT OperatingSystemVersion)
94 {
95 ULONG_PTR SectionId;
96 PCSTR SectionName = OperatingSystem->SystemPartition;
97 CHAR SettingValue[80];
98 PARTITION_TABLE_ENTRY PartitionTableEntry;
99 UCHAR DriveNumber;
100 ULONG PartitionNumber;
101
102 /* Find all the message box settings and run them */
103 UiShowMessageBoxesInSection(SectionName);
104
105 /* Try to open the operating system section in the .ini file */
106 if (!IniOpenSection(SectionName, &SectionId))
107 {
108 UiMessageBox("Section [%s] not found in freeldr.ini.", SectionName);
109 return;
110 }
111
112 /* Read the boot drive */
113 if (!IniReadSettingByName(SectionId, "BootDrive", SettingValue, sizeof(SettingValue)))
114 {
115 UiMessageBox("Boot drive not specified for selected OS!");
116 return;
117 }
118
119 DriveNumber = DriveMapGetBiosDriveNumber(SettingValue);
120
121 /* Read the boot partition */
122 if (!IniReadSettingByName(SectionId, "BootPartition", SettingValue, sizeof(SettingValue)))
123 {
124 UiMessageBox("Boot partition not specified for selected OS!");
125 return;
126 }
127
128 PartitionNumber = atoi(SettingValue);
129
130 /* Get the partition table entry */
131 if (!DiskGetPartitionEntry(DriveNumber, PartitionNumber, &PartitionTableEntry))
132 {
133 return;
134 }
135
136 /* Now try to read the partition boot sector. If this fails then abort. */
137 if (!MachDiskReadLogicalSectors(DriveNumber, PartitionTableEntry.SectorCountBeforePartition, 1, (PVOID)0x7C00))
138 {
139 UiMessageBox("Unable to read partition's boot sector.");
140 return;
141 }
142
143 /* Check for validity */
144 if (*((USHORT*)(0x7c00 + 0x1fe)) != 0xaa55)
145 {
146 UiMessageBox("Invalid boot sector magic (0xaa55)");
147 return;
148 }
149
150 UiUnInitialize("Booting...");
151
152 /*
153 * Don't stop the floppy drive motor when we
154 * are just booting a bootsector, or drive, or partition.
155 * If we were to stop the floppy motor then
156 * the BIOS wouldn't be informed and if the
157 * next read is to a floppy then the BIOS will
158 * still think the motor is on and this will
159 * result in a read error.
160 */
161 // DiskStopFloppyMotor();
162 // DisableA20();
163 FrldrBootDrive = DriveNumber;
164 ChainLoadBiosBootSectorCode();
165 }
166
167 VOID
168 LoadAndBootDrive(IN OperatingSystemItem* OperatingSystem,
169 IN USHORT OperatingSystemVersion)
170 {
171 ULONG_PTR SectionId;
172 PCSTR SectionName = OperatingSystem->SystemPartition;
173 CHAR SettingValue[80];
174 UCHAR DriveNumber;
175
176 /* Find all the message box settings and run them */
177 UiShowMessageBoxesInSection(SectionName);
178
179 /* Try to open the operating system section in the .ini file */
180 if (!IniOpenSection(SectionName, &SectionId))
181 {
182 UiMessageBox("Section [%s] not found in freeldr.ini.", SectionName);
183 return;
184 }
185
186 if (!IniReadSettingByName(SectionId, "BootDrive", SettingValue, sizeof(SettingValue)))
187 {
188 UiMessageBox("Boot drive not specified for selected OS!");
189 return;
190 }
191
192 DriveNumber = DriveMapGetBiosDriveNumber(SettingValue);
193
194 /* Now try to read the boot sector (or mbr). If this fails then abort. */
195 if (!MachDiskReadLogicalSectors(DriveNumber, 0, 1, (PVOID)0x7C00))
196 {
197 UiMessageBox("Unable to read boot sector");
198 return;
199 }
200
201 /* Check for validity */
202 if (*((USHORT*)(0x7c00 + 0x1fe)) != 0xaa55)
203 {
204 UiMessageBox("Invalid boot sector magic (0xaa55)");
205 return;
206 }
207
208 UiUnInitialize("Booting...");
209
210 /*
211 * Don't stop the floppy drive motor when we
212 * are just booting a bootsector, or drive, or partition.
213 * If we were to stop the floppy motor then
214 * the BIOS wouldn't be informed and if the
215 * next read is to a floppy then the BIOS will
216 * still think the motor is on and this will
217 * result in a read error.
218 */
219 // DiskStopFloppyMotor();
220 // DisableA20();
221 FrldrBootDrive = DriveNumber;
222 ChainLoadBiosBootSectorCode();
223 }
224
225 #endif // _M_IX86