xref: /openbsd/sbin/fsck_msdos/boot.c (revision 404b540a)
1 /*	$OpenBSD: boot.c,v 1.13 2008/02/07 01:34:12 ian Exp $	*/
2 /*	$NetBSD: boot.c,v 1.5 1997/10/17 11:19:23 ws Exp $	*/
3 
4 /*
5  * Copyright (C) 1995, 1997 Wolfgang Solfrank
6  * Copyright (c) 1995 Martin Husemann
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by Martin Husemann
19  *	and Wolfgang Solfrank.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
25  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 
37 #ifndef lint
38 static char rcsid[] = "$OpenBSD: boot.c,v 1.13 2008/02/07 01:34:12 ian Exp $";
39 #endif /* not lint */
40 
41 #include <stdlib.h>
42 #include <string.h>
43 #include <ctype.h>
44 #include <stdio.h>
45 #include <unistd.h>
46 
47 #include "ext.h"
48 
49 int
50 readboot(int dosfs, struct bootblock *boot)
51 {
52 	u_char block[DOSBOOTBLOCKSIZE];
53 	u_char fsinfo[2 * DOSBOOTBLOCKSIZE];
54 	u_char backup[DOSBOOTBLOCKSIZE];
55 	int ret = FSOK;
56 	int n;
57 
58 	if ((n = read(dosfs, block, sizeof block)) == -1 || n != sizeof block) {
59 		xperror("could not read boot block");
60 		return (FSFATAL);
61 	}
62 
63 	if (block[510] != 0x55 || block[511] != 0xaa) {
64 		pfatal("Invalid signature in boot block: %02x%02x\n", block[511], block[510]);
65 		return FSFATAL;
66 	}
67 
68 	memset(boot, 0, sizeof *boot);
69 	boot->ValidFat = -1;
70 
71 	/* decode bios parameter block */
72 	boot->BytesPerSec = block[11] + (block[12] << 8);
73 	boot->SecPerClust = block[13];
74 	boot->ResSectors = block[14] + (block[15] << 8);
75 	boot->FATs = block[16];
76 	boot->RootDirEnts = block[17] + (block[18] << 8);
77 	boot->Sectors = block[19] + (block[20] << 8);
78 	boot->Media = block[21];
79 	boot->FATsmall = block[22] + (block[23] << 8);
80 	boot->SecPerTrack = block[24] + (block[25] << 8);
81 	boot->Heads = block[26] + (block[27] << 8);
82 	boot->HiddenSecs = block[28] + (block[29] << 8) + (block[30] << 16) + (block[31] << 24);
83 	boot->HugeSectors = block[32] + (block[33] << 8) + (block[34] << 16) + (block[35] << 24);
84 
85 	boot->FATsecs = boot->FATsmall;
86 
87 	if (!boot->RootDirEnts)
88 		boot->flags |= FAT32;
89 	if (boot->flags & FAT32) {
90 		boot->FATsecs = block[36] + (block[37] << 8)
91 				+ (block[38] << 16) + (block[39] << 24);
92 		if (block[40] & 0x80)
93 			boot->ValidFat = block[40] & 0x0f;
94 
95 		/* check version number: */
96 		if (block[42] || block[43]) {
97 			/* Correct?				XXX */
98 			pfatal("Unknown filesystem version: %x.%x\n",
99 			       block[43], block[42]);
100 			return FSFATAL;
101 		}
102 		boot->RootCl = block[44] + (block[45] << 8)
103 			       + (block[46] << 16) + (block[47] << 24);
104 		boot->FSInfo = block[48] + (block[49] << 8);
105 		boot->Backup = block[50] + (block[51] << 8);
106 
107 		if ((n = lseek(dosfs, boot->FSInfo * boot->BytesPerSec, SEEK_SET)) == -1
108 		    || n != boot->FSInfo * boot->BytesPerSec
109 		    || (n = read(dosfs, fsinfo, sizeof fsinfo)) == -1
110 		    || n != sizeof fsinfo) {
111 			xperror("could not read fsinfo block");
112 			return FSFATAL;
113 		}
114 		if (memcmp(fsinfo, "RRaA", 4)
115 		    || memcmp(fsinfo + 0x1e4, "rrAa", 4)
116 		    || fsinfo[0x1fc]
117 		    || fsinfo[0x1fd]
118 		    || fsinfo[0x1fe] != 0x55
119 		    || fsinfo[0x1ff] != 0xaa
120 		    || fsinfo[0x3fc]
121 		    || fsinfo[0x3fd]
122 		    || fsinfo[0x3fe] != 0x55
123 		    || fsinfo[0x3ff] != 0xaa) {
124 			pwarn("Invalid signature in fsinfo block");
125 			if (ask(0, "fix")) {
126 				memcpy(fsinfo, "RRaA", 4);
127 				memcpy(fsinfo + 0x1e4, "rrAa", 4);
128 				fsinfo[0x1fc] = fsinfo[0x1fd] = 0;
129 				fsinfo[0x1fe] = 0x55;
130 				fsinfo[0x1ff] = 0xaa;
131 				fsinfo[0x3fc] = fsinfo[0x3fd] = 0;
132 				fsinfo[0x3fe] = 0x55;
133 				fsinfo[0x3ff] = 0xaa;
134 				if ((n = lseek(dosfs, boot->FSInfo * boot->BytesPerSec, SEEK_SET)) == -1
135 				    || n != boot->FSInfo * boot->BytesPerSec
136 				    || (n = write(dosfs, fsinfo, sizeof fsinfo)) == -1
137 				    || n != sizeof fsinfo) {
138 					xperror("Unable to write FSInfo");
139 					return FSFATAL;
140 				}
141 				ret = FSBOOTMOD;
142 			} else
143 				boot->FSInfo = 0;
144 		}
145 		if (boot->FSInfo) {
146 			boot->FSFree = fsinfo[0x1e8] + (fsinfo[0x1e9] << 8)
147 				       + (fsinfo[0x1ea] << 16)
148 				       + (fsinfo[0x1eb] << 24);
149 			boot->FSNext = fsinfo[0x1ec] + (fsinfo[0x1ed] << 8)
150 				       + (fsinfo[0x1ee] << 16)
151 				       + (fsinfo[0x1ef] << 24);
152 		}
153 
154 		if ((n = lseek(dosfs, boot->Backup * boot->BytesPerSec, SEEK_SET)) == -1
155 		    || n != boot->Backup * boot->BytesPerSec
156 		    || (n = read(dosfs, backup, sizeof backup)) == -1
157 		    || n != sizeof backup) {
158 			xperror("could not read backup bootblock");
159 			return FSFATAL;
160 		}
161 
162 		/*
163 		 * Check that the backup boot block matches the primary one.
164 		 * We don't check every byte, since some vendor utilities
165 		 * seem to overwrite the boot code when they feel like it,
166 		 * without changing the backup block.  Specifically, we check
167 		 * the two-byte signature at the end, the BIOS parameter
168 		 * block (which starts after the 3-byte JMP and the 8-byte
169 		 * OEM name/version) and the filesystem information that
170 		 * follows the BPB (bsPBP[53] and bsExt[26] for FAT32, so we
171 		 * check 79 bytes).
172 		 */
173 		if (backup[510] != 0x55 || backup[511] != 0xaa) {
174 			pfatal("Invalid signature in backup boot block: %02x%02x\n", backup[511], backup[510]);
175 			return FSFATAL;
176 		}
177 		if (memcmp(block + 11, backup + 11, 79)) {
178 			pfatal("backup doesn't compare to primary bootblock\n");
179 			return FSFATAL;
180 		}
181 		/* Check backup FSInfo?					XXX */
182 	}
183 
184 	if (boot->BytesPerSec == 0 || boot->BytesPerSec % DOSBOOTBLOCKSIZE
185 	    != 0) {
186 		pfatal("Invalid sector size: %u\n", boot->BytesPerSec);
187 		return (FSFATAL);
188 	}
189 	if (boot->SecPerClust == 0) {
190 		pfatal("Invalid cluster size: %u\n", boot->SecPerClust);
191 		return (FSFATAL);
192 	}
193 
194 	boot->ClusterOffset = (boot->RootDirEnts * 32 + boot->BytesPerSec - 1)
195 	    / boot->BytesPerSec
196 	    + boot->ResSectors
197 	    + boot->FATs * boot->FATsecs
198 	    - CLUST_FIRST * boot->SecPerClust;
199 
200 	if (boot->Sectors) {
201 		boot->HugeSectors = 0;
202 		boot->NumSectors = boot->Sectors;
203 	} else
204 		boot->NumSectors = boot->HugeSectors;
205 	boot->NumClusters = (boot->NumSectors - boot->ClusterOffset) / boot->SecPerClust;
206 
207 	if (boot->flags&FAT32)
208 		boot->ClustMask = CLUST32_MASK;
209 	else if (boot->NumClusters < (CLUST_RSRVD&CLUST12_MASK))
210 		boot->ClustMask = CLUST12_MASK;
211 	else if (boot->NumClusters < (CLUST_RSRVD&CLUST16_MASK))
212 		boot->ClustMask = CLUST16_MASK;
213 	else {
214 		pfatal("Filesystem too big (%u clusters) for non-FAT32 partition\n",
215 		       boot->NumClusters);
216 		return FSFATAL;
217 	}
218 
219 	switch (boot->ClustMask) {
220 	case CLUST32_MASK:
221 		boot->NumFatEntries = (boot->FATsecs * boot->BytesPerSec) / 4;
222 		break;
223 	case CLUST16_MASK:
224 		boot->NumFatEntries = (boot->FATsecs * boot->BytesPerSec) / 2;
225 		break;
226 	default:
227 		boot->NumFatEntries = (boot->FATsecs * boot->BytesPerSec * 2) / 3;
228 		break;
229 	}
230 
231 	if (boot->NumFatEntries < boot->NumClusters) {
232 		pfatal("FAT size too small, %u entries won't fit into %u sectors\n",
233 		       boot->NumClusters, boot->FATsecs);
234 		return (FSFATAL);
235 	}
236 	boot->ClusterSize = boot->BytesPerSec * boot->SecPerClust;
237 
238 	boot->NumFiles = 1;
239 	boot->NumFree = 0;
240 
241 	return ret;
242 }
243 
244 int
245 writefsinfo(int dosfs, struct bootblock *boot)
246 {
247 	u_char fsinfo[2 * DOSBOOTBLOCKSIZE];
248 	int n;
249 
250 	if ((n = lseek(dosfs, boot->FSInfo * boot->BytesPerSec, SEEK_SET)) == -1
251 	    || n != boot->FSInfo * boot->BytesPerSec
252 	    || (n = read(dosfs, fsinfo, sizeof fsinfo)) == -1
253 	    || n != sizeof fsinfo) {
254 		xperror("could not read fsinfo block");
255 		return FSFATAL;
256 	}
257 	fsinfo[0x1e8] = (u_char)boot->FSFree;
258 	fsinfo[0x1e9] = (u_char)(boot->FSFree >> 8);
259 	fsinfo[0x1ea] = (u_char)(boot->FSFree >> 16);
260 	fsinfo[0x1eb] = (u_char)(boot->FSFree >> 24);
261 	fsinfo[0x1ec] = (u_char)boot->FSNext;
262 	fsinfo[0x1ed] = (u_char)(boot->FSNext >> 8);
263 	fsinfo[0x1ee] = (u_char)(boot->FSNext >> 16);
264 	fsinfo[0x1ef] = (u_char)(boot->FSNext >> 24);
265 	if ((n = lseek(dosfs, boot->FSInfo * boot->BytesPerSec, SEEK_SET)) == -1
266 	    || n != boot->FSInfo * boot->BytesPerSec
267 	    || (n = write(dosfs, fsinfo, sizeof fsinfo)) == -1
268 	    || n != sizeof fsinfo) {
269 		xperror("Unable to write FSInfo");
270 		return FSFATAL;
271 	}
272 	/*
273 	 * Technically, we should return FSBOOTMOD here.
274 	 *
275 	 * However, since Win95 OSR2 (the first M$ OS that has
276 	 * support for FAT32) doesn't maintain the FSINFO block
277 	 * correctly, it has to be fixed pretty often.
278 	 *
279 	 * Therefor, we handle the FSINFO block only informally,
280 	 * fixing it if necessary, but otherwise ignoring the
281 	 * fact that it was incorrect.
282 	 */
283 	return 0;
284 }
285