xref: /freebsd/sbin/fsck_msdosfs/boot.c (revision 1a498d2e689f9e8220e2ad64b018eb1f0d11127e)
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 <stdlib.h>
37 #include <string.h>
38 #include <stdio.h>
39 #include <unistd.h>
40 
41 #include "ext.h"
42 #include "fsutil.h"
43 
44 int
45 readboot(int dosfs, struct bootblock *boot)
46 {
47 	u_char block[DOSBOOTBLOCKSIZE];
48 	u_char fsinfo[2 * DOSBOOTBLOCKSIZE];
49 	u_char backup[DOSBOOTBLOCKSIZE];
50 	int ret = FSOK;
51 	int i;
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 	boot->bpbBytesPerSec = block[11] + (block[12] << 8);
69 	boot->bpbSecPerClust = block[13];
70 	boot->bpbResSectors = block[14] + (block[15] << 8);
71 	boot->bpbFATs = block[16];
72 	boot->bpbRootDirEnts = block[17] + (block[18] << 8);
73 	boot->bpbSectors = block[19] + (block[20] << 8);
74 	boot->bpbMedia = block[21];
75 	boot->bpbFATsmall = block[22] + (block[23] << 8);
76 	boot->SecPerTrack = block[24] + (block[25] << 8);
77 	boot->bpbHeads = block[26] + (block[27] << 8);
78 	boot->bpbHiddenSecs = block[28] + (block[29] << 8) +
79 	    (block[30] << 16) + (block[31] << 24);
80 	boot->bpbHugeSectors = block[32] + (block[33] << 8) +
81 	    (block[34] << 16) + (block[35] << 24);
82 
83 	boot->FATsecs = boot->bpbFATsmall;
84 
85 	if (boot->bpbBytesPerSec % DOSBOOTBLOCKSIZE_REAL != 0 ||
86 	    boot->bpbBytesPerSec / DOSBOOTBLOCKSIZE_REAL == 0) {
87 		pfatal("Invalid sector size: %u", boot->bpbBytesPerSec);
88 		return FSFATAL;
89 	}
90 	if (boot->bpbFATs == 0) {
91 		pfatal("Invalid number of FATs: %u", boot->bpbFATs);
92 		return FSFATAL;
93 	}
94 	if (!boot->bpbRootDirEnts)
95 		boot->flags |= FAT32;
96 	if (boot->flags & FAT32) {
97 		boot->FATsecs = block[36] + (block[37] << 8)
98 				+ (block[38] << 16) + (block[39] << 24);
99 		if (block[40] & 0x80)
100 			boot->ValidFat = block[40] & 0x0f;
101 
102 		/* check version number: */
103 		if (block[42] || block[43]) {
104 			/* Correct?				XXX */
105 			pfatal("Unknown file system version: %x.%x",
106 			       block[43], block[42]);
107 			return FSFATAL;
108 		}
109 		boot->bpbRootClust = block[44] + (block[45] << 8)
110 			       + (block[46] << 16) + (block[47] << 24);
111 		boot->bpbFSInfo = block[48] + (block[49] << 8);
112 		boot->bpbBackup = block[50] + (block[51] << 8);
113 
114 		/* If the OEM Name field is EXFAT, it's not FAT32, so bail */
115 		if (!memcmp(&block[3], "EXFAT   ", 8)) {
116 			pfatal("exFAT filesystem is not supported.");
117 			return FSFATAL;
118 		}
119 
120 		/* check basic parameters */
121 		if ((boot->bpbFSInfo == 0) || (boot->bpbSecPerClust == 0)) {
122 			/*
123 			 * Either the BIOS Parameter Block has been corrupted,
124 			 * or this is not a FAT32 filesystem, most likely an
125 			 * exFAT filesystem.
126 			 */
127 			pfatal("Invalid FAT32 Extended BIOS Parameter Block");
128 			return FSFATAL;
129 		}
130 		if (lseek(dosfs, boot->bpbFSInfo * boot->bpbBytesPerSec,
131 		    SEEK_SET) != boot->bpbFSInfo * boot->bpbBytesPerSec
132 		    || read(dosfs, fsinfo, sizeof fsinfo) != sizeof fsinfo) {
133 			perr("could not read fsinfo block");
134 			return FSFATAL;
135 		}
136 		if (memcmp(fsinfo, "RRaA", 4)
137 		    || memcmp(fsinfo + 0x1e4, "rrAa", 4)
138 		    || fsinfo[0x1fc]
139 		    || fsinfo[0x1fd]
140 		    || fsinfo[0x1fe] != 0x55
141 		    || fsinfo[0x1ff] != 0xaa
142 		    || fsinfo[0x3fc]
143 		    || fsinfo[0x3fd]
144 		    || fsinfo[0x3fe] != 0x55
145 		    || fsinfo[0x3ff] != 0xaa) {
146 			pwarn("Invalid signature in fsinfo block\n");
147 			if (ask(0, "Fix")) {
148 				memcpy(fsinfo, "RRaA", 4);
149 				memcpy(fsinfo + 0x1e4, "rrAa", 4);
150 				fsinfo[0x1fc] = fsinfo[0x1fd] = 0;
151 				fsinfo[0x1fe] = 0x55;
152 				fsinfo[0x1ff] = 0xaa;
153 				fsinfo[0x3fc] = fsinfo[0x3fd] = 0;
154 				fsinfo[0x3fe] = 0x55;
155 				fsinfo[0x3ff] = 0xaa;
156 				if (lseek(dosfs, boot->bpbFSInfo *
157 				    boot->bpbBytesPerSec, SEEK_SET)
158 				    != boot->bpbFSInfo * boot->bpbBytesPerSec
159 				    || write(dosfs, fsinfo, sizeof fsinfo)
160 				    != sizeof fsinfo) {
161 					perr("Unable to write bpbFSInfo");
162 					return FSFATAL;
163 				}
164 				ret = FSBOOTMOD;
165 			} else
166 				boot->bpbFSInfo = 0;
167 		}
168 		if (boot->bpbFSInfo) {
169 			boot->FSFree = fsinfo[0x1e8] + (fsinfo[0x1e9] << 8)
170 				       + (fsinfo[0x1ea] << 16)
171 				       + (fsinfo[0x1eb] << 24);
172 			boot->FSNext = fsinfo[0x1ec] + (fsinfo[0x1ed] << 8)
173 				       + (fsinfo[0x1ee] << 16)
174 				       + (fsinfo[0x1ef] << 24);
175 		}
176 
177 		if (lseek(dosfs, boot->bpbBackup * boot->bpbBytesPerSec,
178 		    SEEK_SET)
179 		    != boot->bpbBackup * boot->bpbBytesPerSec
180 		    || read(dosfs, backup, sizeof backup) != sizeof  backup) {
181 			perr("could not read backup bootblock");
182 			return FSFATAL;
183 		}
184 		backup[65] = block[65];				/* XXX */
185 		if (memcmp(block + 11, backup + 11, 79)) {
186 			/*
187 			 * XXX We require a reference that explains
188 			 * that these bytes need to match, or should
189 			 * drop the check.  gdt@NetBSD has observed
190 			 * filesystems that work fine under Windows XP
191 			 * and NetBSD that do not match, so the
192 			 * requirement is suspect.  For now, just
193 			 * print out useful information and continue.
194 			 */
195 			pwarn("backup (block %d) mismatch with primary bootblock:\n",
196 			        boot->bpbBackup);
197 			for (i = 11; i < 11 + 90; i++) {
198 				if (block[i] != backup[i])
199 					pwarn("\ti=%d\tprimary 0x%02x\tbackup 0x%02x\n",
200 					       i, block[i], backup[i]);
201 			}
202 		}
203 		/* Check backup bpbFSInfo?					XXX */
204 	}
205 
206 	if (boot->bpbSecPerClust == 0) {
207 		pfatal("Invalid cluster size: %u", boot->bpbSecPerClust);
208 		return FSFATAL;
209 	}
210 	if (boot->bpbSectors) {
211 		boot->bpbHugeSectors = 0;
212 		boot->NumSectors = boot->bpbSectors;
213 	} else
214 		boot->NumSectors = boot->bpbHugeSectors;
215 	boot->ClusterOffset = (boot->bpbRootDirEnts * 32 +
216 	    boot->bpbBytesPerSec - 1) / boot->bpbBytesPerSec +
217 	    boot->bpbResSectors + boot->bpbFATs * boot->FATsecs -
218 	    CLUST_FIRST * boot->bpbSecPerClust;
219 	boot->NumClusters = (boot->NumSectors - boot->ClusterOffset) /
220 	    boot->bpbSecPerClust;
221 
222 	if (boot->flags&FAT32)
223 		boot->ClustMask = CLUST32_MASK;
224 	else if (boot->NumClusters < (CLUST_RSRVD&CLUST12_MASK))
225 		boot->ClustMask = CLUST12_MASK;
226 	else if (boot->NumClusters < (CLUST_RSRVD&CLUST16_MASK))
227 		boot->ClustMask = CLUST16_MASK;
228 	else {
229 		pfatal("Filesystem too big (%u clusters) for non-FAT32 partition",
230 		       boot->NumClusters);
231 		return FSFATAL;
232 	}
233 
234 	switch (boot->ClustMask) {
235 	case CLUST32_MASK:
236 		boot->NumFatEntries = (boot->FATsecs * boot->bpbBytesPerSec) / 4;
237 		break;
238 	case CLUST16_MASK:
239 		boot->NumFatEntries = (boot->FATsecs * boot->bpbBytesPerSec) / 2;
240 		break;
241 	default:
242 		boot->NumFatEntries = (boot->FATsecs * boot->bpbBytesPerSec * 2) / 3;
243 		break;
244 	}
245 
246 	if (boot->NumFatEntries < boot->NumClusters - CLUST_FIRST) {
247 		pfatal("FAT size too small, %u entries won't fit into %u sectors\n",
248 		       boot->NumClusters, boot->FATsecs);
249 		return FSFATAL;
250 	}
251 	boot->ClusterSize = boot->bpbBytesPerSec * boot->bpbSecPerClust;
252 
253 	boot->NumFiles = 1;
254 	boot->NumFree = 0;
255 
256 	return ret;
257 }
258 
259 int
260 writefsinfo(int dosfs, struct bootblock *boot)
261 {
262 	u_char fsinfo[2 * DOSBOOTBLOCKSIZE];
263 
264 	if (lseek(dosfs, boot->bpbFSInfo * boot->bpbBytesPerSec, SEEK_SET)
265 	    != boot->bpbFSInfo * boot->bpbBytesPerSec
266 	    || read(dosfs, fsinfo, sizeof fsinfo) != sizeof fsinfo) {
267 		perr("could not read fsinfo block");
268 		return FSFATAL;
269 	}
270 	fsinfo[0x1e8] = (u_char)boot->FSFree;
271 	fsinfo[0x1e9] = (u_char)(boot->FSFree >> 8);
272 	fsinfo[0x1ea] = (u_char)(boot->FSFree >> 16);
273 	fsinfo[0x1eb] = (u_char)(boot->FSFree >> 24);
274 	fsinfo[0x1ec] = (u_char)boot->FSNext;
275 	fsinfo[0x1ed] = (u_char)(boot->FSNext >> 8);
276 	fsinfo[0x1ee] = (u_char)(boot->FSNext >> 16);
277 	fsinfo[0x1ef] = (u_char)(boot->FSNext >> 24);
278 	if (lseek(dosfs, boot->bpbFSInfo * boot->bpbBytesPerSec, SEEK_SET)
279 	    != boot->bpbFSInfo * boot->bpbBytesPerSec
280 	    || write(dosfs, fsinfo, sizeof fsinfo)
281 	    != sizeof fsinfo) {
282 		perr("Unable to write bpbFSInfo");
283 		return FSFATAL;
284 	}
285 	/*
286 	 * Technically, we should return FSBOOTMOD here.
287 	 *
288 	 * However, since Win95 OSR2 (the first M$ OS that has
289 	 * support for FAT32) doesn't maintain the FSINFO block
290 	 * correctly, it has to be fixed pretty often.
291 	 *
292 	 * Therefor, we handle the FSINFO block only informally,
293 	 * fixing it if necessary, but otherwise ignoring the
294 	 * fact that it was incorrect.
295 	 */
296 	return 0;
297 }
298