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