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