Created new subtree for groups of related test programs.
[reactos.git] / freeldr / freeldr / miscboot.c
1 /*
2 * FreeLoader
3 * Copyright (C) 1998-2002 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 <video.h>
30
31 VOID LoadAndBootBootSector(PUCHAR OperatingSystemName)
32 {
33 PFILE FilePointer;
34 UCHAR SettingName[80];
35 UCHAR SettingValue[80];
36 U32 SectionId;
37 UCHAR FileName[260];
38 U32 BytesRead;
39
40 // Find all the message box settings and run them
41 UiShowMessageBoxesInSection(OperatingSystemName);
42
43 // Try to open the operating system section in the .ini file
44 if (!IniOpenSection(OperatingSystemName, &SectionId))
45 {
46 sprintf(SettingName, "Section [%s] not found in freeldr.ini.\n", OperatingSystemName);
47 UiMessageBox(SettingName);
48 return;
49 }
50
51 if (!IniReadSettingByName(SectionId, "BootDrive", SettingValue, 80))
52 {
53 UiMessageBox("Boot drive not specified for selected OS!");
54 return;
55 }
56
57 BootDrive = atoi(SettingValue);
58
59 BootPartition = 0;
60 if (IniReadSettingByName(SectionId, "BootPartition", SettingValue, 80))
61 {
62 BootPartition = atoi(SettingValue);
63 }
64
65 if (!IniReadSettingByName(SectionId, "BootSectorFile", FileName, 260))
66 {
67 UiMessageBox("Boot sector file not specified for selected OS!");
68 return;
69 }
70
71 if (!OpenDiskDrive(BootDrive, BootPartition))
72 {
73 UiMessageBox("Failed to open boot drive.");
74 return;
75 }
76
77 FilePointer = OpenFile(FileName);
78 if (FilePointer == NULL)
79 {
80 strcat(FileName, " not found.");
81 UiMessageBox(FileName);
82 return;
83 }
84
85 // Read boot sector
86 if (!ReadFile(FilePointer, 512, &BytesRead, (void*)0x7c00) || (BytesRead != 512))
87 {
88 return;
89 }
90
91 // Check for validity
92 if (*((U16*)(0x7c00 + 0x1fe)) != 0xaa55)
93 {
94 UiMessageBox("Invalid boot sector magic (0xaa55)");
95 return;
96 }
97
98 VideoClearScreen();
99 VideoShowTextCursor();
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 //StopFloppyMotor();
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 = atoi(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 (!DiskReadLogicalSectors(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 VideoClearScreen();
169 VideoShowTextCursor();
170 // Don't stop the floppy drive motor when we
171 // are just booting a bootsector, or drive, or partition.
172 // If we were to stop the floppy motor then
173 // the BIOS wouldn't be informed and if the
174 // next read is to a floppy then the BIOS will
175 // still think the motor is on and this will
176 // result in a read error.
177 //StopFloppyMotor();
178 //DisableA20();
179 ChainLoadBiosBootSectorCode();
180 }
181
182 VOID LoadAndBootDrive(PUCHAR OperatingSystemName)
183 {
184 UCHAR SettingName[80];
185 UCHAR SettingValue[80];
186 U32 SectionId;
187
188 // Find all the message box settings and run them
189 UiShowMessageBoxesInSection(OperatingSystemName);
190
191 // Try to open the operating system section in the .ini file
192 if (!IniOpenSection(OperatingSystemName, &SectionId))
193 {
194 sprintf(SettingName, "Section [%s] not found in freeldr.ini.\n", OperatingSystemName);
195 UiMessageBox(SettingName);
196 return;
197 }
198
199 if (!IniReadSettingByName(SectionId, "BootDrive", SettingValue, 80))
200 {
201 UiMessageBox("Boot drive not specified for selected OS!");
202 return;
203 }
204
205 BootDrive = atoi(SettingValue);
206
207 // Now try to read the boot sector (or mbr)
208 // If this fails then abort
209 if (!DiskReadLogicalSectors(BootDrive, 0, 1, (PVOID)0x7C00))
210 {
211 return;
212 }
213
214 // Check for validity
215 if (*((U16*)(0x7c00 + 0x1fe)) != 0xaa55)
216 {
217 UiMessageBox("Invalid boot sector magic (0xaa55)");
218 return;
219 }
220
221 VideoClearScreen();
222 VideoShowTextCursor();
223 // Don't stop the floppy drive motor when we
224 // are just booting a bootsector, or drive, or partition.
225 // If we were to stop the floppy motor then
226 // the BIOS wouldn't be informed and if the
227 // next read is to a floppy then the BIOS will
228 // still think the motor is on and this will
229 // result in a read error.
230 //StopFloppyMotor();
231 //DisableA20();
232 ChainLoadBiosBootSectorCode();
233 }