1 /* $OpenBSD: boot.c,v 1.12 2006/07/19 10:44:23 tom Exp $ */
2 /* $NetBSD: boot.c,v 1.5 1997/10/17 11:19:23 ws Exp $ */
3
4 /*
5 * Copyright (C) 1995, 1997 Wolfgang Solfrank
6 * Copyright (c) 1995 Martin Husemann
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Martin Husemann
19 * and Wolfgang Solfrank.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35
36
37 #ifndef lint
38 static char rcsid[] = "$OpenBSD: boot.c,v 1.12 2006/07/19 10:44:23 tom Exp $";
39 #endif /* not lint */
40
41 #include <stdlib.h>
42 #include <string.h>
43 #include <ctype.h>
44 #include <stdio.h>
45 #include <unistd.h>
46
47 #include "ext.h"
48
49 int
readboot(int dosfs,struct bootblock * boot)50 readboot(int dosfs, struct bootblock *boot)
51 {
52 u_char block[DOSBOOTBLOCKSIZE];
53 u_char fsinfo[2 * DOSBOOTBLOCKSIZE];
54 u_char backup[DOSBOOTBLOCKSIZE];
55 int ret = FSOK;
56
57 if (read(dosfs, block, sizeof block) < sizeof block) {
58 xperror("could not read boot block");
59 return (FSFATAL);
60 }
61
62 if (block[510] != 0x55 || block[511] != 0xaa) {
63 pfatal("Invalid signature in boot block: %02x%02x\n", block[511], block[510]);
64 return FSFATAL;
65 }
66
67 memset(boot, 0, sizeof *boot);
68 boot->ValidFat = -1;
69
70 /* decode bios parameter block */
71 boot->BytesPerSec = block[11] + (block[12] << 8);
72 boot->SecPerClust = block[13];
73 boot->ResSectors = block[14] + (block[15] << 8);
74 boot->FATs = block[16];
75 boot->RootDirEnts = block[17] + (block[18] << 8);
76 boot->Sectors = block[19] + (block[20] << 8);
77 boot->Media = block[21];
78 boot->FATsmall = block[22] + (block[23] << 8);
79 boot->SecPerTrack = block[24] + (block[25] << 8);
80 boot->Heads = block[26] + (block[27] << 8);
81 boot->HiddenSecs = block[28] + (block[29] << 8) + (block[30] << 16) + (block[31] << 24);
82 boot->HugeSectors = block[32] + (block[33] << 8) + (block[34] << 16) + (block[35] << 24);
83
84 boot->FATsecs = boot->FATsmall;
85
86 if (!boot->RootDirEnts)
87 boot->flags |= FAT32;
88 if (boot->flags & FAT32) {
89 boot->FATsecs = block[36] + (block[37] << 8)
90 + (block[38] << 16) + (block[39] << 24);
91 if (block[40] & 0x80)
92 boot->ValidFat = block[40] & 0x0f;
93
94 /* check version number: */
95 if (block[42] || block[43]) {
96 /* Correct? XXX */
97 pfatal("Unknown filesystem version: %x.%x\n",
98 block[43], block[42]);
99 return FSFATAL;
100 }
101 boot->RootCl = block[44] + (block[45] << 8)
102 + (block[46] << 16) + (block[47] << 24);
103 boot->FSInfo = block[48] + (block[49] << 8);
104 boot->Backup = block[50] + (block[51] << 8);
105
106 if (lseek(dosfs, boot->FSInfo * boot->BytesPerSec, SEEK_SET)
107 != boot->FSInfo * boot->BytesPerSec
108 || read(dosfs, fsinfo, sizeof fsinfo)
109 != sizeof fsinfo) {
110 xperror("could not read fsinfo block");
111 return FSFATAL;
112 }
113 if (memcmp(fsinfo, "RRaA", 4)
114 || memcmp(fsinfo + 0x1e4, "rrAa", 4)
115 || fsinfo[0x1fc]
116 || fsinfo[0x1fd]
117 || fsinfo[0x1fe] != 0x55
118 || fsinfo[0x1ff] != 0xaa
119 || fsinfo[0x3fc]
120 || fsinfo[0x3fd]
121 || fsinfo[0x3fe] != 0x55
122 || fsinfo[0x3ff] != 0xaa) {
123 pwarn("Invalid signature in fsinfo block");
124 if (ask(0, "fix")) {
125 memcpy(fsinfo, "RRaA", 4);
126 memcpy(fsinfo + 0x1e4, "rrAa", 4);
127 fsinfo[0x1fc] = fsinfo[0x1fd] = 0;
128 fsinfo[0x1fe] = 0x55;
129 fsinfo[0x1ff] = 0xaa;
130 fsinfo[0x3fc] = fsinfo[0x3fd] = 0;
131 fsinfo[0x3fe] = 0x55;
132 fsinfo[0x3ff] = 0xaa;
133 if (lseek(dosfs, boot->FSInfo * boot->BytesPerSec, SEEK_SET)
134 != boot->FSInfo * boot->BytesPerSec
135 || write(dosfs, fsinfo, sizeof fsinfo)
136 != sizeof fsinfo) {
137 xperror("Unable to write FSInfo");
138 return FSFATAL;
139 }
140 ret = FSBOOTMOD;
141 } else
142 boot->FSInfo = 0;
143 }
144 if (boot->FSInfo) {
145 boot->FSFree = fsinfo[0x1e8] + (fsinfo[0x1e9] << 8)
146 + (fsinfo[0x1ea] << 16)
147 + (fsinfo[0x1eb] << 24);
148 boot->FSNext = fsinfo[0x1ec] + (fsinfo[0x1ed] << 8)
149 + (fsinfo[0x1ee] << 16)
150 + (fsinfo[0x1ef] << 24);
151 }
152
153 if (lseek(dosfs, boot->Backup * boot->BytesPerSec, SEEK_SET)
154 != boot->Backup * boot->BytesPerSec
155 || read(dosfs, backup, sizeof backup) != sizeof backup) {
156 xperror("could not read backup bootblock");
157 return FSFATAL;
158 }
159
160 /*
161 * Check that the backup boot block matches the primary one.
162 * We don't check every byte, since some vendor utilities
163 * seem to overwrite the boot code when they feel like it,
164 * without changing the backup block. Specifically, we check
165 * the two-byte signature at the end, the BIOS parameter
166 * block (which starts after the 3-byte JMP and the 8-byte
167 * OEM name/version) and the filesystem information that
168 * follows the BPB (bsPBP[53] and bsExt[26] for FAT32, so we
169 * check 79 bytes).
170 */
171 if (backup[510] != 0x55 || backup[511] != 0xaa) {
172 pfatal("Invalid signature in backup boot block: %02x%02x\n", backup[511], backup[510]);
173 return FSFATAL;
174 }
175 if (memcmp(block + 11, backup + 11, 79)) {
176 pfatal("backup doesn't compare to primary bootblock\n");
177 return FSFATAL;
178 }
179 /* Check backup FSInfo? XXX */
180 }
181
182 if (boot->BytesPerSec == 0 || boot->BytesPerSec % DOSBOOTBLOCKSIZE
183 != 0) {
184 pfatal("Invalid sector size: %u\n", boot->BytesPerSec);
185 return (FSFATAL);
186 }
187 if (boot->SecPerClust == 0) {
188 pfatal("Invalid cluster size: %u\n", boot->SecPerClust);
189 return (FSFATAL);
190 }
191
192 boot->ClusterOffset = (boot->RootDirEnts * 32 + boot->BytesPerSec - 1)
193 / boot->BytesPerSec
194 + boot->ResSectors
195 + boot->FATs * boot->FATsecs
196 - CLUST_FIRST * boot->SecPerClust;
197
198 if (boot->Sectors) {
199 boot->HugeSectors = 0;
200 boot->NumSectors = boot->Sectors;
201 } else
202 boot->NumSectors = boot->HugeSectors;
203 boot->NumClusters = (boot->NumSectors - boot->ClusterOffset) / boot->SecPerClust;
204
205 if (boot->flags&FAT32)
206 boot->ClustMask = CLUST32_MASK;
207 else if (boot->NumClusters < (CLUST_RSRVD&CLUST12_MASK))
208 boot->ClustMask = CLUST12_MASK;
209 else if (boot->NumClusters < (CLUST_RSRVD&CLUST16_MASK))
210 boot->ClustMask = CLUST16_MASK;
211 else {
212 pfatal("Filesystem too big (%u clusters) for non-FAT32 partition\n",
213 boot->NumClusters);
214 return FSFATAL;
215 }
216
217 switch (boot->ClustMask) {
218 case CLUST32_MASK:
219 boot->NumFatEntries = (boot->FATsecs * boot->BytesPerSec) / 4;
220 break;
221 case CLUST16_MASK:
222 boot->NumFatEntries = (boot->FATsecs * boot->BytesPerSec) / 2;
223 break;
224 default:
225 boot->NumFatEntries = (boot->FATsecs * boot->BytesPerSec * 2) / 3;
226 break;
227 }
228
229 if (boot->NumFatEntries < boot->NumClusters) {
230 pfatal("FAT size too small, %u entries won't fit into %u sectors\n",
231 boot->NumClusters, boot->FATsecs);
232 return (FSFATAL);
233 }
234 boot->ClusterSize = boot->BytesPerSec * boot->SecPerClust;
235
236 boot->NumFiles = 1;
237 boot->NumFree = 0;
238
239 return ret;
240 }
241
242 int
writefsinfo(int dosfs,struct bootblock * boot)243 writefsinfo(int dosfs, struct bootblock *boot)
244 {
245 u_char fsinfo[2 * DOSBOOTBLOCKSIZE];
246
247 if (lseek(dosfs, boot->FSInfo * boot->BytesPerSec, SEEK_SET)
248 != boot->FSInfo * boot->BytesPerSec
249 || read(dosfs, fsinfo, sizeof fsinfo) != sizeof fsinfo) {
250 xperror("could not read fsinfo block");
251 return FSFATAL;
252 }
253 fsinfo[0x1e8] = (u_char)boot->FSFree;
254 fsinfo[0x1e9] = (u_char)(boot->FSFree >> 8);
255 fsinfo[0x1ea] = (u_char)(boot->FSFree >> 16);
256 fsinfo[0x1eb] = (u_char)(boot->FSFree >> 24);
257 fsinfo[0x1ec] = (u_char)boot->FSNext;
258 fsinfo[0x1ed] = (u_char)(boot->FSNext >> 8);
259 fsinfo[0x1ee] = (u_char)(boot->FSNext >> 16);
260 fsinfo[0x1ef] = (u_char)(boot->FSNext >> 24);
261 if (lseek(dosfs, boot->FSInfo * boot->BytesPerSec, SEEK_SET)
262 != boot->FSInfo * boot->BytesPerSec
263 || write(dosfs, fsinfo, sizeof fsinfo)
264 != sizeof fsinfo) {
265 xperror("Unable to write FSInfo");
266 return FSFATAL;
267 }
268 /*
269 * Technically, we should return FSBOOTMOD here.
270 *
271 * However, since Win95 OSR2 (the first M$ OS that has
272 * support for FAT32) doesn't maintain the FSINFO block
273 * correctly, it has to be fixed pretty often.
274 *
275 * Therefor, we handle the FSINFO block only informally,
276 * fixing it if necessary, but otherwise ignoring the
277 * fact that it was incorrect.
278 */
279 return 0;
280 }
281