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