xref: /freebsd/sbin/fsck_msdosfs/boot.c (revision f9bc005b8ef3b507a5ecbc2d7fe4411ef70eff38)
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 #endif /* not lint */
33 
34 #include <sys/param.h>
35 #include <sys/endian.h>
36 
37 #include <stdint.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <stdio.h>
41 #include <unistd.h>
42 
43 #include "ext.h"
44 #include "fsutil.h"
45 
46 int
47 readboot(int dosfs, struct bootblock *boot)
48 {
49 	u_char block[DOSBOOTBLOCKSIZE];
50 	u_char fsinfo[2 * DOSBOOTBLOCKSIZE];
51 	int ret = FSOK;
52 
53 	if ((size_t)read(dosfs, block, sizeof block) != sizeof block) {
54 		perr("could not read boot block");
55 		return FSFATAL;
56 	}
57 
58 	if (block[510] != 0x55 || block[511] != 0xaa) {
59 		pfatal("Invalid signature in boot block: %02x%02x",
60 		    block[511], block[510]);
61 		return FSFATAL;
62 	}
63 
64 	memset(boot, 0, sizeof *boot);
65 	boot->ValidFat = -1;
66 
67 	/* Decode BIOS Parameter Block */
68 
69 	/* Bytes per sector: can only be  512, 1024, 2048 and 4096. */
70 	boot->bpbBytesPerSec = block[11] + (block[12] << 8);
71 	if (boot->bpbBytesPerSec < DOSBOOTBLOCKSIZE_REAL ||
72 	    boot->bpbBytesPerSec > DOSBOOTBLOCKSIZE ||
73 	    !powerof2(boot->bpbBytesPerSec)) {
74 		pfatal("Invalid sector size: %u", boot->bpbBytesPerSec);
75 		return FSFATAL;
76 	}
77 
78 	/* Sectors per cluster: can only be: 1, 2, 4, 8, 16, 32, 64, 128. */
79 	boot->bpbSecPerClust = block[13];
80 	if (boot->bpbSecPerClust == 0 || !powerof2(boot->bpbSecPerClust)) {
81 		pfatal("Invalid cluster size: %u", boot->bpbSecPerClust);
82 		return FSFATAL;
83 	}
84 
85 	/* Reserved sectors: must be non-zero */
86 	boot->bpbResSectors = block[14] + (block[15] << 8);
87 	if (boot->bpbResSectors < 1) {
88 		pfatal("Invalid reserved sectors: %u",
89 		    boot->bpbResSectors);
90 		return FSFATAL;
91 	}
92 
93 	/* Number of FATs */
94 	boot->bpbFATs = block[16];
95 	if (boot->bpbFATs == 0) {
96 		pfatal("Invalid number of FATs: %u", boot->bpbFATs);
97 		return FSFATAL;
98 	}
99 
100 	/* Root directory entries for FAT12 and FAT16 */
101 	boot->bpbRootDirEnts = block[17] + (block[18] << 8);
102 	if (!boot->bpbRootDirEnts) {
103 		/* bpbRootDirEnts = 0 suggests that we are FAT32 */
104 		boot->flags |= FAT32;
105 	}
106 
107 	/* Total sectors (16 bits) */
108 	boot->bpbSectors = block[19] + (block[20] << 8);
109 	if (boot->bpbSectors != 0 && (boot->flags & FAT32)) {
110 		pfatal("Invalid 16-bit total sector count on FAT32: %u",
111 		    boot->bpbSectors);
112 		return FSFATAL;
113 	}
114 
115 	/* Media type: ignored */
116 	boot->bpbMedia = block[21];
117 
118 	/* FAT12/FAT16: 16-bit count of sectors per FAT */
119 	boot->bpbFATsmall = block[22] + (block[23] << 8);
120 	if (boot->bpbFATsmall != 0 && (boot->flags & FAT32)) {
121 		pfatal("Invalid 16-bit FAT sector count on FAT32: %u",
122 		    boot->bpbFATsmall);
123 		return FSFATAL;
124 	}
125 
126 	/* Legacy CHS geometry numbers: ignored */
127 	boot->SecPerTrack = block[24] + (block[25] << 8);
128 	boot->bpbHeads = block[26] + (block[27] << 8);
129 
130 	/* Hidden sectors: ignored */
131 	boot->bpbHiddenSecs = le32dec(&block[28]);
132 
133 	/* Total sectors (32 bits) */
134 	boot->bpbHugeSectors = le32dec(&block[32]);
135 	if (boot->bpbHugeSectors == 0) {
136 		if (boot->flags & FAT32) {
137 			pfatal("FAT32 with sector count of zero");
138 			return FSFATAL;
139 		} else if (boot->bpbSectors == 0) {
140 			pfatal("FAT with sector count of zero");
141 			return FSFATAL;
142 		}
143 		boot->NumSectors = boot->bpbSectors;
144 	} else {
145 		if (boot->bpbSectors != 0) {
146 			pfatal("Invalid FAT sector count");
147 			return FSFATAL;
148 		}
149 		boot->NumSectors = boot->bpbHugeSectors;
150 	}
151 
152 	if (boot->flags & FAT32) {
153 		/* If the OEM Name field is EXFAT, it's not FAT32, so bail */
154 		if (!memcmp(&block[3], "EXFAT   ", 8)) {
155 			pfatal("exFAT filesystem is not supported.");
156 			return FSFATAL;
157 		}
158 
159 		/* 32-bit count of sectors per FAT */
160 		boot->FATsecs = le32dec(&block[36]);
161 
162 		if (block[40] & 0x80)
163 			boot->ValidFat = block[40] & 0x0f;
164 
165 		/* FAT32 version, bail out if not 0.0 */
166 		if (block[42] || block[43]) {
167 			pfatal("Unknown file system version: %x.%x",
168 			       block[43], block[42]);
169 			return FSFATAL;
170 		}
171 
172 		/*
173 		 * Cluster number of the first cluster of root directory.
174 		 *
175 		 * Should be 2 but do not require it.
176 		 */
177 		boot->bpbRootClust = le32dec(&block[44]);
178 
179 		/* Sector number of the FSInfo structure, usually 1 */
180 		boot->bpbFSInfo = block[48] + (block[49] << 8);
181 
182 		/* Sector number of the backup boot block, ignored */
183 		boot->bpbBackup = block[50] + (block[51] << 8);
184 
185 		/* Check basic parameters */
186 		if (boot->bpbFSInfo == 0) {
187 			/*
188 			 * Either the BIOS Parameter Block has been corrupted,
189 			 * or this is not a FAT32 filesystem, most likely an
190 			 * exFAT filesystem.
191 			 */
192 			pfatal("Invalid FAT32 Extended BIOS Parameter Block");
193 			return FSFATAL;
194 		}
195 
196 		/* Read in and verify the FSInfo block */
197 		if (lseek(dosfs, boot->bpbFSInfo * boot->bpbBytesPerSec,
198 		    SEEK_SET) != boot->bpbFSInfo * boot->bpbBytesPerSec
199 		    || read(dosfs, fsinfo, sizeof fsinfo) != sizeof fsinfo) {
200 			perr("could not read fsinfo block");
201 			return FSFATAL;
202 		}
203 		if (memcmp(fsinfo, "RRaA", 4)
204 		    || memcmp(fsinfo + 0x1e4, "rrAa", 4)
205 		    || fsinfo[0x1fc]
206 		    || fsinfo[0x1fd]
207 		    || fsinfo[0x1fe] != 0x55
208 		    || fsinfo[0x1ff] != 0xaa
209 		    || fsinfo[0x3fc]
210 		    || fsinfo[0x3fd]
211 		    || fsinfo[0x3fe] != 0x55
212 		    || fsinfo[0x3ff] != 0xaa) {
213 			pwarn("Invalid signature in fsinfo block\n");
214 			if (ask(0, "Fix")) {
215 				memcpy(fsinfo, "RRaA", 4);
216 				memcpy(fsinfo + 0x1e4, "rrAa", 4);
217 				fsinfo[0x1fc] = fsinfo[0x1fd] = 0;
218 				fsinfo[0x1fe] = 0x55;
219 				fsinfo[0x1ff] = 0xaa;
220 				fsinfo[0x3fc] = fsinfo[0x3fd] = 0;
221 				fsinfo[0x3fe] = 0x55;
222 				fsinfo[0x3ff] = 0xaa;
223 				if (lseek(dosfs, boot->bpbFSInfo *
224 				    boot->bpbBytesPerSec, SEEK_SET)
225 				    != boot->bpbFSInfo * boot->bpbBytesPerSec
226 				    || write(dosfs, fsinfo, sizeof fsinfo)
227 				    != sizeof fsinfo) {
228 					perr("Unable to write bpbFSInfo");
229 					return FSFATAL;
230 				}
231 				ret = FSBOOTMOD;
232 			} else
233 				boot->bpbFSInfo = 0;
234 		} else {
235 			/* We appear to have a valid FSInfo block, decode */
236 			boot->FSFree = le32dec(&fsinfo[0x1e8]);
237 			boot->FSNext = le32dec(&fsinfo[0x1ec]);
238 		}
239 	} else {
240 		/* !FAT32: FAT12/FAT16 */
241 		boot->FATsecs = boot->bpbFATsmall;
242 	}
243 
244 	if (boot->FATsecs < 1 || boot->FATsecs > UINT32_MAX / boot->bpbFATs) {
245 		pfatal("Invalid FATs(%u) with FATsecs(%zu)",
246 			boot->bpbFATs, (size_t)boot->FATsecs);
247 		return FSFATAL;
248 	}
249 
250 	boot->FirstCluster = (boot->bpbRootDirEnts * 32 +
251 	    boot->bpbBytesPerSec - 1) / boot->bpbBytesPerSec +
252 	    boot->bpbResSectors + boot->bpbFATs * boot->FATsecs;
253 
254 	if (boot->FirstCluster + boot->bpbSecPerClust > boot->NumSectors) {
255 		pfatal("Cluster offset too large (%u clusters)\n",
256 		    boot->FirstCluster);
257 		return FSFATAL;
258 	}
259 
260 	/*
261 	 * The number of clusters is derived from available data sectors,
262 	 * divided by sectors per cluster.
263 	 */
264 	boot->NumClusters =
265 	    (boot->NumSectors - boot->FirstCluster) / boot->bpbSecPerClust;
266 
267 	if (boot->flags & FAT32) {
268 		if (boot->NumClusters > (CLUST_RSRVD & CLUST32_MASK)) {
269 			pfatal("Filesystem too big (%u clusters) for FAT32 partition",
270 			    boot->NumClusters);
271 			return FSFATAL;
272 		}
273 		if (boot->NumClusters < (CLUST_RSRVD & CLUST16_MASK)) {
274 			pfatal("Filesystem too small (%u clusters) for FAT32 partition",
275 			    boot->NumClusters);
276 			return FSFATAL;
277 		}
278 		boot->ClustMask = CLUST32_MASK;
279 
280 		if (boot->bpbRootClust < CLUST_FIRST ||
281 		    boot->bpbRootClust >= boot->NumClusters) {
282 			pfatal("Root directory starts with cluster out of range(%u)",
283 			       boot->bpbRootClust);
284 			return FSFATAL;
285 		}
286 	} else if (boot->NumClusters < (CLUST_RSRVD&CLUST12_MASK)) {
287 		boot->ClustMask = CLUST12_MASK;
288 	} else if (boot->NumClusters < (CLUST_RSRVD&CLUST16_MASK)) {
289 		boot->ClustMask = CLUST16_MASK;
290 	} else {
291 		pfatal("Filesystem too big (%u clusters) for non-FAT32 partition",
292 		       boot->NumClusters);
293 		return FSFATAL;
294 	}
295 
296 	switch (boot->ClustMask) {
297 	case CLUST32_MASK:
298 		boot->NumFatEntries = (boot->FATsecs * boot->bpbBytesPerSec) / 4;
299 		break;
300 	case CLUST16_MASK:
301 		boot->NumFatEntries = (boot->FATsecs * boot->bpbBytesPerSec) / 2;
302 		break;
303 	default:
304 		boot->NumFatEntries = (boot->FATsecs * boot->bpbBytesPerSec * 2) / 3;
305 		break;
306 	}
307 
308 	if (boot->NumFatEntries < boot->NumClusters) {
309 		pfatal("FAT size too small, %u entries won't fit into %u sectors\n",
310 		       boot->NumClusters, boot->FATsecs);
311 		return FSFATAL;
312 	}
313 
314 	/*
315 	 * There are two reserved clusters. To avoid adding CLUST_FIRST every
316 	 * time we perform boundary checks, we increment the NumClusters by 2,
317 	 * which is CLUST_FIRST to denote the first out-of-range cluster number.
318 	 */
319 	boot->NumClusters += CLUST_FIRST;
320 
321 	boot->ClusterSize = boot->bpbBytesPerSec * boot->bpbSecPerClust;
322 
323 	boot->NumFiles = 1;
324 	boot->NumFree = 0;
325 
326 	return ret;
327 }
328 
329 int
330 writefsinfo(int dosfs, struct bootblock *boot)
331 {
332 	u_char fsinfo[2 * DOSBOOTBLOCKSIZE];
333 
334 	if (lseek(dosfs, boot->bpbFSInfo * boot->bpbBytesPerSec, SEEK_SET)
335 	    != boot->bpbFSInfo * boot->bpbBytesPerSec
336 	    || read(dosfs, fsinfo, sizeof fsinfo) != sizeof fsinfo) {
337 		perr("could not read fsinfo block");
338 		return FSFATAL;
339 	}
340 	fsinfo[0x1e8] = (u_char)boot->FSFree;
341 	fsinfo[0x1e9] = (u_char)(boot->FSFree >> 8);
342 	fsinfo[0x1ea] = (u_char)(boot->FSFree >> 16);
343 	fsinfo[0x1eb] = (u_char)(boot->FSFree >> 24);
344 	fsinfo[0x1ec] = (u_char)boot->FSNext;
345 	fsinfo[0x1ed] = (u_char)(boot->FSNext >> 8);
346 	fsinfo[0x1ee] = (u_char)(boot->FSNext >> 16);
347 	fsinfo[0x1ef] = (u_char)(boot->FSNext >> 24);
348 	if (lseek(dosfs, boot->bpbFSInfo * boot->bpbBytesPerSec, SEEK_SET)
349 	    != boot->bpbFSInfo * boot->bpbBytesPerSec
350 	    || write(dosfs, fsinfo, sizeof fsinfo)
351 	    != sizeof fsinfo) {
352 		perr("Unable to write bpbFSInfo");
353 		return FSFATAL;
354 	}
355 	/*
356 	 * Technically, we should return FSBOOTMOD here.
357 	 *
358 	 * However, since Win95 OSR2 (the first M$ OS that has
359 	 * support for FAT32) doesn't maintain the FSINFO block
360 	 * correctly, it has to be fixed pretty often.
361 	 *
362 	 * Therefore, we handle the FSINFO block only informally,
363 	 * fixing it if necessary, but otherwise ignoring the
364 	 * fact that it was incorrect.
365 	 */
366 	return 0;
367 }
368