xref: /freebsd/sbin/fsck_msdosfs/boot.c (revision 42249ef2)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (C) 1995, 1997 Wolfgang Solfrank
5  * Copyright (c) 1995 Martin Husemann
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 
29 #include <sys/cdefs.h>
30 #ifndef lint
31 __RCSID("$NetBSD: boot.c,v 1.11 2006/06/05 16:51:18 christos Exp ");
32 static const char rcsid[] =
33   "$FreeBSD$";
34 #endif /* not lint */
35 
36 #include <sys/param.h>
37 
38 #include <stdint.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <stdio.h>
42 #include <unistd.h>
43 
44 #include "ext.h"
45 #include "fsutil.h"
46 
47 int
48 readboot(int dosfs, struct bootblock *boot)
49 {
50 	u_char block[DOSBOOTBLOCKSIZE];
51 	u_char fsinfo[2 * DOSBOOTBLOCKSIZE];
52 	int ret = FSOK;
53 
54 	if ((size_t)read(dosfs, block, sizeof block) != sizeof block) {
55 		perr("could not read boot block");
56 		return FSFATAL;
57 	}
58 
59 	if (block[510] != 0x55 || block[511] != 0xaa) {
60 		pfatal("Invalid signature in boot block: %02x%02x",
61 		    block[511], block[510]);
62 		return FSFATAL;
63 	}
64 
65 	memset(boot, 0, sizeof *boot);
66 	boot->ValidFat = -1;
67 
68 	/* Decode BIOS Parameter Block */
69 
70 	/* Bytes per sector: can only be  512, 1024, 2048 and 4096. */
71 	boot->bpbBytesPerSec = block[11] + (block[12] << 8);
72 	if (boot->bpbBytesPerSec < DOSBOOTBLOCKSIZE_REAL ||
73 	    boot->bpbBytesPerSec > DOSBOOTBLOCKSIZE ||
74 	    !powerof2(boot->bpbBytesPerSec)) {
75 		pfatal("Invalid sector size: %u", boot->bpbBytesPerSec);
76 		return FSFATAL;
77 	}
78 
79 	/* Sectors per cluster: can only be: 1, 2, 4, 8, 16, 32, 64, 128. */
80 	boot->bpbSecPerClust = block[13];
81 	if (boot->bpbSecPerClust == 0 || !powerof2(boot->bpbSecPerClust)) {
82 		pfatal("Invalid cluster size: %u", boot->bpbSecPerClust);
83 		return FSFATAL;
84 	}
85 
86 	/* Reserved sectors: must be non-zero */
87 	boot->bpbResSectors = block[14] + (block[15] << 8);
88 	if (boot->bpbResSectors < 1) {
89 		pfatal("Invalid reserved sectors: %u",
90 		    boot->bpbResSectors);
91 		return FSFATAL;
92 	}
93 
94 	/* Number of FATs */
95 	boot->bpbFATs = block[16];
96 	if (boot->bpbFATs == 0) {
97 		pfatal("Invalid number of FATs: %u", boot->bpbFATs);
98 		return FSFATAL;
99 	}
100 
101 	/* Root directory entries for FAT12 and FAT16 */
102 	boot->bpbRootDirEnts = block[17] + (block[18] << 8);
103 	if (!boot->bpbRootDirEnts) {
104 		/* bpbRootDirEnts = 0 suggests that we are FAT32 */
105 		boot->flags |= FAT32;
106 	}
107 
108 	/* Total sectors (16 bits) */
109 	boot->bpbSectors = block[19] + (block[20] << 8);
110 	if (boot->bpbSectors != 0 && (boot->flags & FAT32)) {
111 		pfatal("Invalid 16-bit total sector count on FAT32: %u",
112 		    boot->bpbSectors);
113 		return FSFATAL;
114 	}
115 
116 	/* Media type: ignored */
117 	boot->bpbMedia = block[21];
118 
119 	/* FAT12/FAT16: 16-bit count of sectors per FAT */
120 	boot->bpbFATsmall = block[22] + (block[23] << 8);
121 	if (boot->bpbFATsmall != 0 && (boot->flags & FAT32)) {
122 		pfatal("Invalid 16-bit FAT sector count on FAT32: %u",
123 		    boot->bpbFATsmall);
124 		return FSFATAL;
125 	}
126 
127 	/* Legacy CHS geometry numbers: ignored */
128 	boot->SecPerTrack = block[24] + (block[25] << 8);
129 	boot->bpbHeads = block[26] + (block[27] << 8);
130 
131 	/* Hidden sectors: ignored */
132 	boot->bpbHiddenSecs = block[28] + (block[29] << 8) +
133 	    (block[30] << 16) + (block[31] << 24);
134 
135 	/* Total sectors (32 bits) */
136 	boot->bpbHugeSectors = block[32] + (block[33] << 8) +
137 	    (block[34] << 16) + (block[35] << 24);
138 	if (boot->bpbHugeSectors == 0) {
139 		if (boot->flags & FAT32) {
140 			pfatal("FAT32 with sector count of zero");
141 			return FSFATAL;
142 		} else if (boot->bpbSectors == 0) {
143 			pfatal("FAT with sector count of zero");
144 			return FSFATAL;
145 		}
146 		boot->NumSectors = boot->bpbSectors;
147 	} else {
148 		if (boot->bpbSectors != 0) {
149 			pfatal("Invalid FAT sector count");
150 			return FSFATAL;
151 		}
152 		boot->NumSectors = boot->bpbHugeSectors;
153 	}
154 
155 
156 
157 
158 	if (boot->flags & FAT32) {
159 		/* If the OEM Name field is EXFAT, it's not FAT32, so bail */
160 		if (!memcmp(&block[3], "EXFAT   ", 8)) {
161 			pfatal("exFAT filesystem is not supported.");
162 			return FSFATAL;
163 		}
164 
165 		/* 32-bit count of sectors per FAT */
166 		boot->FATsecs = block[36] + (block[37] << 8)
167 				+ (block[38] << 16) + (block[39] << 24);
168 
169 		if (block[40] & 0x80)
170 			boot->ValidFat = block[40] & 0x0f;
171 
172 		/* FAT32 version, bail out if not 0.0 */
173 		if (block[42] || block[43]) {
174 			pfatal("Unknown file system version: %x.%x",
175 			       block[43], block[42]);
176 			return FSFATAL;
177 		}
178 
179 		/*
180 		 * Cluster number of the first cluster of root directory.
181 		 *
182 		 * Should be 2 but do not require it.
183 		 */
184 		boot->bpbRootClust = block[44] + (block[45] << 8)
185 			       + (block[46] << 16) + (block[47] << 24);
186 
187 		/* Sector number of the FSInfo structure, usually 1 */
188 		boot->bpbFSInfo = block[48] + (block[49] << 8);
189 
190 		/* Sector number of the backup boot block, ignored */
191 		boot->bpbBackup = block[50] + (block[51] << 8);
192 
193 		/* Check basic parameters */
194 		if (boot->bpbFSInfo == 0) {
195 			/*
196 			 * Either the BIOS Parameter Block has been corrupted,
197 			 * or this is not a FAT32 filesystem, most likely an
198 			 * exFAT filesystem.
199 			 */
200 			pfatal("Invalid FAT32 Extended BIOS Parameter Block");
201 			return FSFATAL;
202 		}
203 
204 		/* Read in and verify the FSInfo block */
205 		if (lseek(dosfs, boot->bpbFSInfo * boot->bpbBytesPerSec,
206 		    SEEK_SET) != boot->bpbFSInfo * boot->bpbBytesPerSec
207 		    || read(dosfs, fsinfo, sizeof fsinfo) != sizeof fsinfo) {
208 			perr("could not read fsinfo block");
209 			return FSFATAL;
210 		}
211 		if (memcmp(fsinfo, "RRaA", 4)
212 		    || memcmp(fsinfo + 0x1e4, "rrAa", 4)
213 		    || fsinfo[0x1fc]
214 		    || fsinfo[0x1fd]
215 		    || fsinfo[0x1fe] != 0x55
216 		    || fsinfo[0x1ff] != 0xaa
217 		    || fsinfo[0x3fc]
218 		    || fsinfo[0x3fd]
219 		    || fsinfo[0x3fe] != 0x55
220 		    || fsinfo[0x3ff] != 0xaa) {
221 			pwarn("Invalid signature in fsinfo block\n");
222 			if (ask(0, "Fix")) {
223 				memcpy(fsinfo, "RRaA", 4);
224 				memcpy(fsinfo + 0x1e4, "rrAa", 4);
225 				fsinfo[0x1fc] = fsinfo[0x1fd] = 0;
226 				fsinfo[0x1fe] = 0x55;
227 				fsinfo[0x1ff] = 0xaa;
228 				fsinfo[0x3fc] = fsinfo[0x3fd] = 0;
229 				fsinfo[0x3fe] = 0x55;
230 				fsinfo[0x3ff] = 0xaa;
231 				if (lseek(dosfs, boot->bpbFSInfo *
232 				    boot->bpbBytesPerSec, SEEK_SET)
233 				    != boot->bpbFSInfo * boot->bpbBytesPerSec
234 				    || write(dosfs, fsinfo, sizeof fsinfo)
235 				    != sizeof fsinfo) {
236 					perr("Unable to write bpbFSInfo");
237 					return FSFATAL;
238 				}
239 				ret = FSBOOTMOD;
240 			} else
241 				boot->bpbFSInfo = 0;
242 		} else {
243 			/* We appear to have a valid FSInfo block, decode */
244 			boot->FSFree = fsinfo[0x1e8] + (fsinfo[0x1e9] << 8)
245 				       + (fsinfo[0x1ea] << 16)
246 				       + (fsinfo[0x1eb] << 24);
247 			boot->FSNext = fsinfo[0x1ec] + (fsinfo[0x1ed] << 8)
248 				       + (fsinfo[0x1ee] << 16)
249 				       + (fsinfo[0x1ef] << 24);
250 		}
251 	} else {
252 		/* !FAT32: FAT12/FAT16 */
253 		boot->FATsecs = boot->bpbFATsmall;
254 	}
255 
256 	if (boot->FATsecs > UINT32_MAX / boot->bpbFATs) {
257 		pfatal("Invalid FATs(%u) with FATsecs(%zu)",
258 			boot->bpbFATs, (size_t)boot->FATsecs);
259 		return FSFATAL;
260 	}
261 
262 	boot->ClusterOffset = (boot->bpbRootDirEnts * 32 +
263 	    boot->bpbBytesPerSec - 1) / boot->bpbBytesPerSec +
264 	    boot->bpbResSectors + boot->bpbFATs * boot->FATsecs -
265 	    CLUST_FIRST * boot->bpbSecPerClust;
266 	boot->NumClusters = (boot->NumSectors - boot->ClusterOffset) /
267 	    boot->bpbSecPerClust;
268 
269 	if (boot->flags & FAT32)
270 		boot->ClustMask = CLUST32_MASK;
271 	else if (boot->NumClusters < (CLUST_RSRVD&CLUST12_MASK))
272 		boot->ClustMask = CLUST12_MASK;
273 	else if (boot->NumClusters < (CLUST_RSRVD&CLUST16_MASK))
274 		boot->ClustMask = CLUST16_MASK;
275 	else {
276 		pfatal("Filesystem too big (%u clusters) for non-FAT32 partition",
277 		       boot->NumClusters);
278 		return FSFATAL;
279 	}
280 
281 	switch (boot->ClustMask) {
282 	case CLUST32_MASK:
283 		boot->NumFatEntries = (boot->FATsecs * boot->bpbBytesPerSec) / 4;
284 		break;
285 	case CLUST16_MASK:
286 		boot->NumFatEntries = (boot->FATsecs * boot->bpbBytesPerSec) / 2;
287 		break;
288 	default:
289 		boot->NumFatEntries = (boot->FATsecs * boot->bpbBytesPerSec * 2) / 3;
290 		break;
291 	}
292 
293 	if (boot->NumFatEntries < boot->NumClusters - CLUST_FIRST) {
294 		pfatal("FAT size too small, %u entries won't fit into %u sectors\n",
295 		       boot->NumClusters, boot->FATsecs);
296 		return FSFATAL;
297 	}
298 	boot->ClusterSize = boot->bpbBytesPerSec * boot->bpbSecPerClust;
299 
300 	boot->NumFiles = 1;
301 	boot->NumFree = 0;
302 
303 	return ret;
304 }
305 
306 int
307 writefsinfo(int dosfs, struct bootblock *boot)
308 {
309 	u_char fsinfo[2 * DOSBOOTBLOCKSIZE];
310 
311 	if (lseek(dosfs, boot->bpbFSInfo * boot->bpbBytesPerSec, SEEK_SET)
312 	    != boot->bpbFSInfo * boot->bpbBytesPerSec
313 	    || read(dosfs, fsinfo, sizeof fsinfo) != sizeof fsinfo) {
314 		perr("could not read fsinfo block");
315 		return FSFATAL;
316 	}
317 	fsinfo[0x1e8] = (u_char)boot->FSFree;
318 	fsinfo[0x1e9] = (u_char)(boot->FSFree >> 8);
319 	fsinfo[0x1ea] = (u_char)(boot->FSFree >> 16);
320 	fsinfo[0x1eb] = (u_char)(boot->FSFree >> 24);
321 	fsinfo[0x1ec] = (u_char)boot->FSNext;
322 	fsinfo[0x1ed] = (u_char)(boot->FSNext >> 8);
323 	fsinfo[0x1ee] = (u_char)(boot->FSNext >> 16);
324 	fsinfo[0x1ef] = (u_char)(boot->FSNext >> 24);
325 	if (lseek(dosfs, boot->bpbFSInfo * boot->bpbBytesPerSec, SEEK_SET)
326 	    != boot->bpbFSInfo * boot->bpbBytesPerSec
327 	    || write(dosfs, fsinfo, sizeof fsinfo)
328 	    != sizeof fsinfo) {
329 		perr("Unable to write bpbFSInfo");
330 		return FSFATAL;
331 	}
332 	/*
333 	 * Technically, we should return FSBOOTMOD here.
334 	 *
335 	 * However, since Win95 OSR2 (the first M$ OS that has
336 	 * support for FAT32) doesn't maintain the FSINFO block
337 	 * correctly, it has to be fixed pretty often.
338 	 *
339 	 * Therefor, we handle the FSINFO block only informally,
340 	 * fixing it if necessary, but otherwise ignoring the
341 	 * fact that it was incorrect.
342 	 */
343 	return 0;
344 }
345