xref: /freebsd/sbin/fsck_msdosfs/boot.c (revision ff0ba87247820afbdfdc1b307c803f7923d0e4d3)
1 /*
2  * Copyright (C) 1995, 1997 Wolfgang Solfrank
3  * Copyright (c) 1995 Martin Husemann
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 
27 #include <sys/cdefs.h>
28 #ifndef lint
29 __RCSID("$NetBSD: boot.c,v 1.11 2006/06/05 16:51:18 christos Exp ");
30 static const char rcsid[] =
31   "$FreeBSD$";
32 #endif /* not lint */
33 
34 #include <stdlib.h>
35 #include <string.h>
36 #include <stdio.h>
37 #include <unistd.h>
38 
39 #include "ext.h"
40 #include "fsutil.h"
41 
42 int
43 readboot(int dosfs, struct bootblock *boot)
44 {
45 	u_char block[DOSBOOTBLOCKSIZE];
46 	u_char fsinfo[2 * DOSBOOTBLOCKSIZE];
47 	u_char backup[DOSBOOTBLOCKSIZE];
48 	int ret = FSOK;
49 	int i;
50 
51 	if ((size_t)read(dosfs, block, sizeof block) != sizeof block) {
52 		perr("could not read boot block");
53 		return FSFATAL;
54 	}
55 
56 	if (block[510] != 0x55 || block[511] != 0xaa) {
57 		pfatal("Invalid signature in boot block: %02x%02x",
58 		    block[511], block[510]);
59 		return FSFATAL;
60 	}
61 
62 	memset(boot, 0, sizeof *boot);
63 	boot->ValidFat = -1;
64 
65 	/* decode bios parameter block */
66 	boot->bpbBytesPerSec = block[11] + (block[12] << 8);
67 	boot->bpbSecPerClust = block[13];
68 	boot->bpbResSectors = block[14] + (block[15] << 8);
69 	boot->bpbFATs = block[16];
70 	boot->bpbRootDirEnts = block[17] + (block[18] << 8);
71 	boot->bpbSectors = block[19] + (block[20] << 8);
72 	boot->bpbMedia = block[21];
73 	boot->bpbFATsmall = block[22] + (block[23] << 8);
74 	boot->SecPerTrack = block[24] + (block[25] << 8);
75 	boot->bpbHeads = block[26] + (block[27] << 8);
76 	boot->bpbHiddenSecs = block[28] + (block[29] << 8) +
77 	    (block[30] << 16) + (block[31] << 24);
78 	boot->bpbHugeSectors = block[32] + (block[33] << 8) +
79 	    (block[34] << 16) + (block[35] << 24);
80 
81 	boot->FATsecs = boot->bpbFATsmall;
82 
83 	if (!boot->bpbRootDirEnts)
84 		boot->flags |= FAT32;
85 	if (boot->flags & FAT32) {
86 		boot->FATsecs = block[36] + (block[37] << 8)
87 				+ (block[38] << 16) + (block[39] << 24);
88 		if (block[40] & 0x80)
89 			boot->ValidFat = block[40] & 0x0f;
90 
91 		/* check version number: */
92 		if (block[42] || block[43]) {
93 			/* Correct?				XXX */
94 			pfatal("Unknown file system version: %x.%x",
95 			       block[43], block[42]);
96 			return FSFATAL;
97 		}
98 		boot->bpbRootClust = block[44] + (block[45] << 8)
99 			       + (block[46] << 16) + (block[47] << 24);
100 		boot->bpbFSInfo = block[48] + (block[49] << 8);
101 		boot->bpbBackup = block[50] + (block[51] << 8);
102 
103 		if (lseek(dosfs, boot->bpbFSInfo * boot->bpbBytesPerSec,
104 		    SEEK_SET) != boot->bpbFSInfo * boot->bpbBytesPerSec
105 		    || read(dosfs, fsinfo, sizeof fsinfo) != sizeof fsinfo) {
106 			perr("could not read fsinfo block");
107 			return FSFATAL;
108 		}
109 		if (memcmp(fsinfo, "RRaA", 4)
110 		    || memcmp(fsinfo + 0x1e4, "rrAa", 4)
111 		    || fsinfo[0x1fc]
112 		    || fsinfo[0x1fd]
113 		    || fsinfo[0x1fe] != 0x55
114 		    || fsinfo[0x1ff] != 0xaa
115 		    || fsinfo[0x3fc]
116 		    || fsinfo[0x3fd]
117 		    || fsinfo[0x3fe] != 0x55
118 		    || fsinfo[0x3ff] != 0xaa) {
119 			pwarn("Invalid signature in fsinfo block\n");
120 			if (ask(0, "Fix")) {
121 				memcpy(fsinfo, "RRaA", 4);
122 				memcpy(fsinfo + 0x1e4, "rrAa", 4);
123 				fsinfo[0x1fc] = fsinfo[0x1fd] = 0;
124 				fsinfo[0x1fe] = 0x55;
125 				fsinfo[0x1ff] = 0xaa;
126 				fsinfo[0x3fc] = fsinfo[0x3fd] = 0;
127 				fsinfo[0x3fe] = 0x55;
128 				fsinfo[0x3ff] = 0xaa;
129 				if (lseek(dosfs, boot->bpbFSInfo *
130 				    boot->bpbBytesPerSec, SEEK_SET)
131 				    != boot->bpbFSInfo * boot->bpbBytesPerSec
132 				    || write(dosfs, fsinfo, sizeof fsinfo)
133 				    != sizeof fsinfo) {
134 					perr("Unable to write bpbFSInfo");
135 					return FSFATAL;
136 				}
137 				ret = FSBOOTMOD;
138 			} else
139 				boot->bpbFSInfo = 0;
140 		}
141 		if (boot->bpbFSInfo) {
142 			boot->FSFree = fsinfo[0x1e8] + (fsinfo[0x1e9] << 8)
143 				       + (fsinfo[0x1ea] << 16)
144 				       + (fsinfo[0x1eb] << 24);
145 			boot->FSNext = fsinfo[0x1ec] + (fsinfo[0x1ed] << 8)
146 				       + (fsinfo[0x1ee] << 16)
147 				       + (fsinfo[0x1ef] << 24);
148 		}
149 
150 		if (lseek(dosfs, boot->bpbBackup * boot->bpbBytesPerSec,
151 		    SEEK_SET)
152 		    != boot->bpbBackup * boot->bpbBytesPerSec
153 		    || read(dosfs, backup, sizeof backup) != sizeof  backup) {
154 			perr("could not read backup bootblock");
155 			return FSFATAL;
156 		}
157 		backup[65] = block[65];				/* XXX */
158 		if (memcmp(block + 11, backup + 11, 79)) {
159 			/*
160 			 * XXX We require a reference that explains
161 			 * that these bytes need to match, or should
162 			 * drop the check.  gdt@NetBSD has observed
163 			 * filesystems that work fine under Windows XP
164 			 * and NetBSD that do not match, so the
165 			 * requirement is suspect.  For now, just
166 			 * print out useful information and continue.
167 			 */
168 			pfatal("backup (block %d) mismatch with primary bootblock:\n",
169 			        boot->bpbBackup);
170 			for (i = 11; i < 11 + 90; i++) {
171 				if (block[i] != backup[i])
172 					pfatal("\ti=%d\tprimary 0x%02x\tbackup 0x%02x\n",
173 					       i, block[i], backup[i]);
174 			}
175 		}
176 		/* Check backup bpbFSInfo?					XXX */
177 	}
178 
179 	boot->ClusterOffset = (boot->bpbRootDirEnts * 32 +
180 	    boot->bpbBytesPerSec - 1) / boot->bpbBytesPerSec +
181 	    boot->bpbResSectors + boot->bpbFATs * boot->FATsecs -
182 	    CLUST_FIRST * boot->bpbSecPerClust;
183 
184 	if (boot->bpbBytesPerSec % DOSBOOTBLOCKSIZE_REAL != 0) {
185 		pfatal("Invalid sector size: %u", boot->bpbBytesPerSec);
186 		return FSFATAL;
187 	}
188 	if (boot->bpbSecPerClust == 0) {
189 		pfatal("Invalid cluster size: %u", boot->bpbSecPerClust);
190 		return FSFATAL;
191 	}
192 	if (boot->bpbSectors) {
193 		boot->bpbHugeSectors = 0;
194 		boot->NumSectors = boot->bpbSectors;
195 	} else
196 		boot->NumSectors = boot->bpbHugeSectors;
197 	boot->NumClusters = (boot->NumSectors - boot->ClusterOffset) /
198 	    boot->bpbSecPerClust;
199 
200 	if (boot->flags&FAT32)
201 		boot->ClustMask = CLUST32_MASK;
202 	else if (boot->NumClusters < (CLUST_RSRVD&CLUST12_MASK))
203 		boot->ClustMask = CLUST12_MASK;
204 	else if (boot->NumClusters < (CLUST_RSRVD&CLUST16_MASK))
205 		boot->ClustMask = CLUST16_MASK;
206 	else {
207 		pfatal("Filesystem too big (%u clusters) for non-FAT32 partition",
208 		       boot->NumClusters);
209 		return FSFATAL;
210 	}
211 
212 	switch (boot->ClustMask) {
213 	case CLUST32_MASK:
214 		boot->NumFatEntries = (boot->FATsecs * boot->bpbBytesPerSec) / 4;
215 		break;
216 	case CLUST16_MASK:
217 		boot->NumFatEntries = (boot->FATsecs * boot->bpbBytesPerSec) / 2;
218 		break;
219 	default:
220 		boot->NumFatEntries = (boot->FATsecs * boot->bpbBytesPerSec * 2) / 3;
221 		break;
222 	}
223 
224 	if (boot->NumFatEntries < boot->NumClusters) {
225 		pfatal("FAT size too small, %u entries won't fit into %u sectors\n",
226 		       boot->NumClusters, boot->FATsecs);
227 		return FSFATAL;
228 	}
229 	boot->ClusterSize = boot->bpbBytesPerSec * boot->bpbSecPerClust;
230 
231 	boot->NumFiles = 1;
232 	boot->NumFree = 0;
233 
234 	return ret;
235 }
236 
237 int
238 writefsinfo(int dosfs, struct bootblock *boot)
239 {
240 	u_char fsinfo[2 * DOSBOOTBLOCKSIZE];
241 
242 	if (lseek(dosfs, boot->bpbFSInfo * boot->bpbBytesPerSec, SEEK_SET)
243 	    != boot->bpbFSInfo * boot->bpbBytesPerSec
244 	    || read(dosfs, fsinfo, sizeof fsinfo) != sizeof fsinfo) {
245 		perr("could not read fsinfo block");
246 		return FSFATAL;
247 	}
248 	fsinfo[0x1e8] = (u_char)boot->FSFree;
249 	fsinfo[0x1e9] = (u_char)(boot->FSFree >> 8);
250 	fsinfo[0x1ea] = (u_char)(boot->FSFree >> 16);
251 	fsinfo[0x1eb] = (u_char)(boot->FSFree >> 24);
252 	fsinfo[0x1ec] = (u_char)boot->FSNext;
253 	fsinfo[0x1ed] = (u_char)(boot->FSNext >> 8);
254 	fsinfo[0x1ee] = (u_char)(boot->FSNext >> 16);
255 	fsinfo[0x1ef] = (u_char)(boot->FSNext >> 24);
256 	if (lseek(dosfs, boot->bpbFSInfo * boot->bpbBytesPerSec, SEEK_SET)
257 	    != boot->bpbFSInfo * boot->bpbBytesPerSec
258 	    || write(dosfs, fsinfo, sizeof fsinfo)
259 	    != sizeof fsinfo) {
260 		perr("Unable to write bpbFSInfo");
261 		return FSFATAL;
262 	}
263 	/*
264 	 * Technically, we should return FSBOOTMOD here.
265 	 *
266 	 * However, since Win95 OSR2 (the first M$ OS that has
267 	 * support for FAT32) doesn't maintain the FSINFO block
268 	 * correctly, it has to be fixed pretty often.
269 	 *
270 	 * Therefor, we handle the FSINFO block only informally,
271 	 * fixing it if necessary, but otherwise ignoring the
272 	 * fact that it was incorrect.
273 	 */
274 	return 0;
275 }
276