[FREELDR] Verbose error output for FS errors
[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 ARC_STATUS
29 LoadAndBootBootSector(
30 IN ULONG Argc,
31 IN PCHAR Argv[],
32 IN PCHAR Envp[])
33 {
34 PCSTR FileName;
35 PFILE FilePointer;
36 ULONG BytesRead;
37
38 /* Find all the message box settings and run them */
39 UiShowMessageBoxesInArgv(Argc, Argv);
40
41 /* Read the file name */
42 FileName = GetArgumentValue(Argc, Argv, "BootSectorFile");
43 if (!FileName)
44 {
45 UiMessageBox("Boot sector file not specified for selected OS!");
46 return EINVAL;
47 }
48
49 FilePointer = FsOpenFile(FileName);
50 if (!FilePointer)
51 {
52 UiMessageBox("%s not found.", FileName);
53 return ENOENT;
54 }
55
56 /* Read boot sector */
57 if (!FsReadFile(FilePointer, 512, &BytesRead, (void*)0x7c00) || (BytesRead != 512))
58 {
59 UiMessageBox("Unable to read boot sector.");
60 return EIO;
61 }
62
63 /* Check for validity */
64 if (*((USHORT*)(0x7c00 + 0x1fe)) != 0xaa55)
65 {
66 UiMessageBox("Invalid boot sector magic (0xaa55)");
67 return ENOEXEC;
68 }
69
70 UiUnInitialize("Booting...");
71 IniCleanup();
72
73 /*
74 * Don't stop the floppy drive motor when we
75 * are just booting a bootsector, or drive, or partition.
76 * If we were to stop the floppy motor then
77 * the BIOS wouldn't be informed and if the
78 * next read is to a floppy then the BIOS will
79 * still think the motor is on and this will
80 * result in a read error.
81 */
82 // DiskStopFloppyMotor();
83 // DisableA20();
84 ChainLoadBiosBootSectorCode();
85 return ESUCCESS;
86 }
87
88 ARC_STATUS
89 LoadAndBootPartition(
90 IN ULONG Argc,
91 IN PCHAR Argv[],
92 IN PCHAR Envp[])
93 {
94 PCSTR ArgValue;
95 PARTITION_TABLE_ENTRY PartitionTableEntry;
96 UCHAR DriveNumber;
97 ULONG PartitionNumber;
98
99 /* Find all the message box settings and run them */
100 UiShowMessageBoxesInArgv(Argc, Argv);
101
102 /* Read the boot drive */
103 ArgValue = GetArgumentValue(Argc, Argv, "BootDrive");
104 if (!ArgValue)
105 {
106 UiMessageBox("Boot drive not specified for selected OS!");
107 return EINVAL;
108 }
109 DriveNumber = DriveMapGetBiosDriveNumber(ArgValue);
110
111 /* Read the boot partition */
112 ArgValue = GetArgumentValue(Argc, Argv, "BootPartition");
113 if (!ArgValue)
114 {
115 UiMessageBox("Boot partition not specified for selected OS!");
116 return EINVAL;
117 }
118 PartitionNumber = atoi(ArgValue);
119
120 /* Get the partition table entry */
121 if (!DiskGetPartitionEntry(DriveNumber, PartitionNumber, &PartitionTableEntry))
122 {
123 return ENOENT;
124 }
125
126 /* Now try to read the partition boot sector. If this fails then abort. */
127 if (!MachDiskReadLogicalSectors(DriveNumber, PartitionTableEntry.SectorCountBeforePartition, 1, (PVOID)0x7C00))
128 {
129 UiMessageBox("Unable to read partition's boot sector.");
130 return EIO;
131 }
132
133 /* Check for validity */
134 if (*((USHORT*)(0x7c00 + 0x1fe)) != 0xaa55)
135 {
136 UiMessageBox("Invalid boot sector magic (0xaa55)");
137 return ENOEXEC;
138 }
139
140 UiUnInitialize("Booting...");
141 IniCleanup();
142
143 /*
144 * Don't stop the floppy drive motor when we
145 * are just booting a bootsector, or drive, or partition.
146 * If we were to stop the floppy motor then
147 * the BIOS wouldn't be informed and if the
148 * next read is to a floppy then the BIOS will
149 * still think the motor is on and this will
150 * result in a read error.
151 */
152 // DiskStopFloppyMotor();
153 // DisableA20();
154 FrldrBootDrive = DriveNumber;
155 ChainLoadBiosBootSectorCode();
156 return ESUCCESS;
157 }
158
159 ARC_STATUS
160 LoadAndBootDrive(
161 IN ULONG Argc,
162 IN PCHAR Argv[],
163 IN PCHAR Envp[])
164 {
165 PCSTR ArgValue;
166 UCHAR DriveNumber;
167
168 /* Find all the message box settings and run them */
169 UiShowMessageBoxesInArgv(Argc, Argv);
170
171 /* Read the boot drive */
172 ArgValue = GetArgumentValue(Argc, Argv, "BootDrive");
173 if (!ArgValue)
174 {
175 UiMessageBox("Boot drive not specified for selected OS!");
176 return EINVAL;
177 }
178 DriveNumber = DriveMapGetBiosDriveNumber(ArgValue);
179
180 /* Now try to read the boot sector (or mbr). If this fails then abort. */
181 if (!MachDiskReadLogicalSectors(DriveNumber, 0, 1, (PVOID)0x7C00))
182 {
183 UiMessageBox("Unable to read boot sector");
184 return EIO;
185 }
186
187 /* Check for validity */
188 if (*((USHORT*)(0x7c00 + 0x1fe)) != 0xaa55)
189 {
190 UiMessageBox("Invalid boot sector magic (0xaa55)");
191 return ENOEXEC;
192 }
193
194 UiUnInitialize("Booting...");
195 IniCleanup();
196
197 /*
198 * Don't stop the floppy drive motor when we
199 * are just booting a bootsector, or drive, or partition.
200 * If we were to stop the floppy motor then
201 * the BIOS wouldn't be informed and if the
202 * next read is to a floppy then the BIOS will
203 * still think the motor is on and this will
204 * result in a read error.
205 */
206 // DiskStopFloppyMotor();
207 // DisableA20();
208 FrldrBootDrive = DriveNumber;
209 ChainLoadBiosBootSectorCode();
210 return ESUCCESS;
211 }
212
213 #endif // _M_IX86