xref: /freebsd/sbin/fsck_msdosfs/boot.c (revision 58983e4b0253ad38a3e1ef2166fedd3133fdb552)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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.22 2020/01/11 16:29:07 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 	if (boot->flags & FAT32) {
156 		/* If the OEM Name field is EXFAT, it's not FAT32, so bail */
157 		if (!memcmp(&block[3], "EXFAT   ", 8)) {
158 			pfatal("exFAT filesystem is not supported.");
159 			return FSFATAL;
160 		}
161 
162 		/* 32-bit count of sectors per FAT */
163 		boot->FATsecs = block[36] + (block[37] << 8)
164 				+ (block[38] << 16) + (block[39] << 24);
165 
166 		if (block[40] & 0x80)
167 			boot->ValidFat = block[40] & 0x0f;
168 
169 		/* FAT32 version, bail out if not 0.0 */
170 		if (block[42] || block[43]) {
171 			pfatal("Unknown file system version: %x.%x",
172 			       block[43], block[42]);
173 			return FSFATAL;
174 		}
175 
176 		/*
177 		 * Cluster number of the first cluster of root directory.
178 		 *
179 		 * Should be 2 but do not require it.
180 		 */
181 		boot->bpbRootClust = block[44] + (block[45] << 8)
182 			       + (block[46] << 16) + (block[47] << 24);
183 
184 		/* Sector number of the FSInfo structure, usually 1 */
185 		boot->bpbFSInfo = block[48] + (block[49] << 8);
186 
187 		/* Sector number of the backup boot block, ignored */
188 		boot->bpbBackup = block[50] + (block[51] << 8);
189 
190 		/* Check basic parameters */
191 		if (boot->bpbFSInfo == 0) {
192 			/*
193 			 * Either the BIOS Parameter Block has been corrupted,
194 			 * or this is not a FAT32 filesystem, most likely an
195 			 * exFAT filesystem.
196 			 */
197 			pfatal("Invalid FAT32 Extended BIOS Parameter Block");
198 			return FSFATAL;
199 		}
200 
201 		/* Read in and verify the FSInfo block */
202 		if (lseek(dosfs, boot->bpbFSInfo * boot->bpbBytesPerSec,
203 		    SEEK_SET) != boot->bpbFSInfo * boot->bpbBytesPerSec
204 		    || read(dosfs, fsinfo, sizeof fsinfo) != sizeof fsinfo) {
205 			perr("could not read fsinfo block");
206 			return FSFATAL;
207 		}
208 		if (memcmp(fsinfo, "RRaA", 4)
209 		    || memcmp(fsinfo + 0x1e4, "rrAa", 4)
210 		    || fsinfo[0x1fc]
211 		    || fsinfo[0x1fd]
212 		    || fsinfo[0x1fe] != 0x55
213 		    || fsinfo[0x1ff] != 0xaa
214 		    || fsinfo[0x3fc]
215 		    || fsinfo[0x3fd]
216 		    || fsinfo[0x3fe] != 0x55
217 		    || fsinfo[0x3ff] != 0xaa) {
218 			pwarn("Invalid signature in fsinfo block\n");
219 			if (ask(0, "Fix")) {
220 				memcpy(fsinfo, "RRaA", 4);
221 				memcpy(fsinfo + 0x1e4, "rrAa", 4);
222 				fsinfo[0x1fc] = fsinfo[0x1fd] = 0;
223 				fsinfo[0x1fe] = 0x55;
224 				fsinfo[0x1ff] = 0xaa;
225 				fsinfo[0x3fc] = fsinfo[0x3fd] = 0;
226 				fsinfo[0x3fe] = 0x55;
227 				fsinfo[0x3ff] = 0xaa;
228 				if (lseek(dosfs, boot->bpbFSInfo *
229 				    boot->bpbBytesPerSec, SEEK_SET)
230 				    != boot->bpbFSInfo * boot->bpbBytesPerSec
231 				    || write(dosfs, fsinfo, sizeof fsinfo)
232 				    != sizeof fsinfo) {
233 					perr("Unable to write bpbFSInfo");
234 					return FSFATAL;
235 				}
236 				ret = FSBOOTMOD;
237 			} else
238 				boot->bpbFSInfo = 0;
239 		} else {
240 			/* We appear to have a valid FSInfo block, decode */
241 			boot->FSFree = fsinfo[0x1e8] + (fsinfo[0x1e9] << 8)
242 				       + (fsinfo[0x1ea] << 16)
243 				       + (fsinfo[0x1eb] << 24);
244 			boot->FSNext = fsinfo[0x1ec] + (fsinfo[0x1ed] << 8)
245 				       + (fsinfo[0x1ee] << 16)
246 				       + (fsinfo[0x1ef] << 24);
247 		}
248 	} else {
249 		/* !FAT32: FAT12/FAT16 */
250 		boot->FATsecs = boot->bpbFATsmall;
251 	}
252 
253 	if (boot->FATsecs < 1 || boot->FATsecs > UINT32_MAX / boot->bpbFATs) {
254 		pfatal("Invalid FATs(%u) with FATsecs(%zu)",
255 			boot->bpbFATs, (size_t)boot->FATsecs);
256 		return FSFATAL;
257 	}
258 
259 	boot->FirstCluster = (boot->bpbRootDirEnts * 32 +
260 	    boot->bpbBytesPerSec - 1) / boot->bpbBytesPerSec +
261 	    boot->bpbResSectors + boot->bpbFATs * boot->FATsecs;
262 
263 	if (boot->FirstCluster + boot->bpbSecPerClust > boot->NumSectors) {
264 		pfatal("Cluster offset too large (%u clusters)\n",
265 		    boot->FirstCluster);
266 		return FSFATAL;
267 	}
268 
269 	/*
270 	 * The number of clusters is derived from available data sectors,
271 	 * divided by sectors per cluster.
272 	 */
273 	boot->NumClusters =
274 	    (boot->NumSectors - boot->FirstCluster) / boot->bpbSecPerClust;
275 
276 	if (boot->flags & FAT32) {
277 		if (boot->NumClusters > (CLUST_RSRVD & CLUST32_MASK)) {
278 			pfatal("Filesystem too big (%u clusters) for FAT32 partition",
279 			    boot->NumClusters);
280 			return FSFATAL;
281 		}
282 		if (boot->NumClusters < (CLUST_RSRVD & CLUST16_MASK)) {
283 			pfatal("Filesystem too small (%u clusters) for FAT32 partition",
284 			    boot->NumClusters);
285 			return FSFATAL;
286 		}
287 		boot->ClustMask = CLUST32_MASK;
288 
289 		if (boot->bpbRootClust < CLUST_FIRST ||
290 		    boot->bpbRootClust >= boot->NumClusters) {
291 			pfatal("Root directory starts with cluster out of range(%u)",
292 			       boot->bpbRootClust);
293 			return FSFATAL;
294 		}
295 	} else if (boot->NumClusters < (CLUST_RSRVD&CLUST12_MASK)) {
296 		boot->ClustMask = CLUST12_MASK;
297 	} else if (boot->NumClusters < (CLUST_RSRVD&CLUST16_MASK)) {
298 		boot->ClustMask = CLUST16_MASK;
299 	} else {
300 		pfatal("Filesystem too big (%u clusters) for non-FAT32 partition",
301 		       boot->NumClusters);
302 		return FSFATAL;
303 	}
304 
305 	switch (boot->ClustMask) {
306 	case CLUST32_MASK:
307 		boot->NumFatEntries = (boot->FATsecs * boot->bpbBytesPerSec) / 4;
308 		break;
309 	case CLUST16_MASK:
310 		boot->NumFatEntries = (boot->FATsecs * boot->bpbBytesPerSec) / 2;
311 		break;
312 	default:
313 		boot->NumFatEntries = (boot->FATsecs * boot->bpbBytesPerSec * 2) / 3;
314 		break;
315 	}
316 
317 	if (boot->NumFatEntries < boot->NumClusters) {
318 		pfatal("FAT size too small, %u entries won't fit into %u sectors\n",
319 		       boot->NumClusters, boot->FATsecs);
320 		return FSFATAL;
321 	}
322 
323 	/*
324 	 * There are two reserved clusters. To avoid adding CLUST_FIRST every
325 	 * time we perform boundary checks, we increment the NumClusters by 2,
326 	 * which is CLUST_FIRST to denote the first out-of-range cluster number.
327 	 */
328 	boot->NumClusters += CLUST_FIRST;
329 
330 	boot->ClusterSize = boot->bpbBytesPerSec * boot->bpbSecPerClust;
331 
332 	boot->NumFiles = 1;
333 	boot->NumFree = 0;
334 
335 	return ret;
336 }
337 
338 int
339 writefsinfo(int dosfs, struct bootblock *boot)
340 {
341 	u_char fsinfo[2 * DOSBOOTBLOCKSIZE];
342 
343 	if (lseek(dosfs, boot->bpbFSInfo * boot->bpbBytesPerSec, SEEK_SET)
344 	    != boot->bpbFSInfo * boot->bpbBytesPerSec
345 	    || read(dosfs, fsinfo, sizeof fsinfo) != sizeof fsinfo) {
346 		perr("could not read fsinfo block");
347 		return FSFATAL;
348 	}
349 	fsinfo[0x1e8] = (u_char)boot->FSFree;
350 	fsinfo[0x1e9] = (u_char)(boot->FSFree >> 8);
351 	fsinfo[0x1ea] = (u_char)(boot->FSFree >> 16);
352 	fsinfo[0x1eb] = (u_char)(boot->FSFree >> 24);
353 	fsinfo[0x1ec] = (u_char)boot->FSNext;
354 	fsinfo[0x1ed] = (u_char)(boot->FSNext >> 8);
355 	fsinfo[0x1ee] = (u_char)(boot->FSNext >> 16);
356 	fsinfo[0x1ef] = (u_char)(boot->FSNext >> 24);
357 	if (lseek(dosfs, boot->bpbFSInfo * boot->bpbBytesPerSec, SEEK_SET)
358 	    != boot->bpbFSInfo * boot->bpbBytesPerSec
359 	    || write(dosfs, fsinfo, sizeof fsinfo)
360 	    != sizeof fsinfo) {
361 		perr("Unable to write bpbFSInfo");
362 		return FSFATAL;
363 	}
364 	/*
365 	 * Technically, we should return FSBOOTMOD here.
366 	 *
367 	 * However, since Win95 OSR2 (the first M$ OS that has
368 	 * support for FAT32) doesn't maintain the FSINFO block
369 	 * correctly, it has to be fixed pretty often.
370 	 *
371 	 * Therefore, we handle the FSINFO block only informally,
372 	 * fixing it if necessary, but otherwise ignoring the
373 	 * fact that it was incorrect.
374 	 */
375 	return 0;
376 }
377