xref: /openbsd-src/sbin/fsck_msdos/boot.c (revision 8500990981f885cbe5e6a4958549cacc238b5ae6)
1 /*	$OpenBSD: boot.c,v 1.9 2003/07/29 18:38:35 deraadt 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.9 2003/07/29 18:38:35 deraadt 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(dosfs, boot)
51 	int dosfs;
52 	struct bootblock *boot;
53 {
54 	u_char block[DOSBOOTBLOCKSIZE];
55 	u_char fsinfo[2 * DOSBOOTBLOCKSIZE];
56 	u_char backup[DOSBOOTBLOCKSIZE];
57 	int ret = FSOK;
58 
59 	if (read(dosfs, block, sizeof block) < sizeof block) {
60 		perror("could not read boot block");
61 		return (FSFATAL);
62 	}
63 
64 	if (block[510] != 0x55 || block[511] != 0xaa) {
65 		pfatal("Invalid signature in boot block: %02x%02x\n", block[511], block[510]);
66 		return FSFATAL;
67 	}
68 
69 	memset(boot, 0, sizeof *boot);
70 	boot->ValidFat = -1;
71 
72 	/* decode bios parameter block */
73 	boot->BytesPerSec = block[11] + (block[12] << 8);
74 	boot->SecPerClust = block[13];
75 	boot->ResSectors = block[14] + (block[15] << 8);
76 	boot->FATs = block[16];
77 	boot->RootDirEnts = block[17] + (block[18] << 8);
78 	boot->Sectors = block[19] + (block[20] << 8);
79 	boot->Media = block[21];
80 	boot->FATsmall = block[22] + (block[23] << 8);
81 	boot->SecPerTrack = block[24] + (block[25] << 8);
82 	boot->Heads = block[26] + (block[27] << 8);
83 	boot->HiddenSecs = block[28] + (block[29] << 8) + (block[30] << 16) + (block[31] << 24);
84 	boot->HugeSectors = block[32] + (block[33] << 8) + (block[34] << 16) + (block[35] << 24);
85 
86 	boot->FATsecs = boot->FATsmall;
87 
88 	if (!boot->RootDirEnts)
89 		boot->flags |= FAT32;
90 	if (boot->flags & FAT32) {
91 		boot->FATsecs = block[36] + (block[37] << 8)
92 				+ (block[38] << 16) + (block[39] << 24);
93 		if (block[40] & 0x80)
94 			boot->ValidFat = block[40] & 0x0f;
95 
96 		/* check version number: */
97 		if (block[42] || block[43]) {
98 			/* Correct?				XXX */
99 			pfatal("Unknown filesystem version: %x.%x\n",
100 			       block[43], block[42]);
101 			return FSFATAL;
102 		}
103 		boot->RootCl = block[44] + (block[45] << 8)
104 			       + (block[46] << 16) + (block[47] << 24);
105 		boot->FSInfo = block[48] + (block[49] << 8);
106 		boot->Backup = block[50] + (block[51] << 8);
107 
108 		if (lseek(dosfs, boot->FSInfo * boot->BytesPerSec, SEEK_SET)
109 		    != boot->FSInfo * boot->BytesPerSec
110 		    || read(dosfs, fsinfo, sizeof fsinfo)
111 		    != sizeof fsinfo) {
112 			perror("could not read fsinfo block");
113 			return FSFATAL;
114 		}
115 		if (memcmp(fsinfo, "RRaA", 4)
116 		    || memcmp(fsinfo + 0x1e4, "rrAa", 4)
117 		    || fsinfo[0x1fc]
118 		    || fsinfo[0x1fd]
119 		    || fsinfo[0x1fe] != 0x55
120 		    || fsinfo[0x1ff] != 0xaa
121 		    || fsinfo[0x3fc]
122 		    || fsinfo[0x3fd]
123 		    || fsinfo[0x3fe] != 0x55
124 		    || fsinfo[0x3ff] != 0xaa) {
125 			pwarn("Invalid signature in fsinfo block");
126 			if (ask(0, "fix")) {
127 				memcpy(fsinfo, "RRaA", 4);
128 				memcpy(fsinfo + 0x1e4, "rrAa", 4);
129 				fsinfo[0x1fc] = fsinfo[0x1fd] = 0;
130 				fsinfo[0x1fe] = 0x55;
131 				fsinfo[0x1ff] = 0xaa;
132 				fsinfo[0x3fc] = fsinfo[0x3fd] = 0;
133 				fsinfo[0x3fe] = 0x55;
134 				fsinfo[0x3ff] = 0xaa;
135 				if (lseek(dosfs, boot->FSInfo * boot->BytesPerSec, SEEK_SET)
136 				    != boot->FSInfo * boot->BytesPerSec
137 				    || write(dosfs, fsinfo, sizeof fsinfo)
138 				    != sizeof fsinfo) {
139 					perror("Unable to write FSInfo");
140 					return FSFATAL;
141 				}
142 				ret = FSBOOTMOD;
143 			} else
144 				boot->FSInfo = 0;
145 		}
146 		if (boot->FSInfo) {
147 			boot->FSFree = fsinfo[0x1e8] + (fsinfo[0x1e9] << 8)
148 				       + (fsinfo[0x1ea] << 16)
149 				       + (fsinfo[0x1eb] << 24);
150 			boot->FSNext = fsinfo[0x1ec] + (fsinfo[0x1ed] << 8)
151 				       + (fsinfo[0x1ee] << 16)
152 				       + (fsinfo[0x1ef] << 24);
153 		}
154 
155 		if (lseek(dosfs, boot->Backup * boot->BytesPerSec, SEEK_SET)
156 		    != boot->Backup * boot->BytesPerSec
157 		    || read(dosfs, backup, sizeof backup) != sizeof  backup) {
158 			perror("could not read backup bootblock");
159 			return FSFATAL;
160 		}
161 		if (memcmp(block, backup, DOSBOOTBLOCKSIZE)) {
162 			/* Correct?					XXX */
163 			pfatal("backup doesn't compare to primary bootblock\n");
164 			return FSFATAL;
165 		}
166 		/* Check backup FSInfo?					XXX */
167 	}
168 
169 	if (boot->BytesPerSec == 0 || boot->BytesPerSec % DOSBOOTBLOCKSIZE
170 	    != 0) {
171 		pfatal("Invalid sector size: %u\n", boot->BytesPerSec);
172 		return (FSFATAL);
173 	}
174 	if (boot->SecPerClust == 0) {
175 		pfatal("Invalid cluster size: %u\n", boot->SecPerClust);
176 		return (FSFATAL);
177 	}
178 
179 	boot->ClusterOffset = (boot->RootDirEnts * 32 + boot->BytesPerSec - 1)
180 	    / boot->BytesPerSec
181 	    + boot->ResSectors
182 	    + boot->FATs * boot->FATsecs
183 	    - CLUST_FIRST * boot->SecPerClust;
184 
185 	if (boot->Sectors) {
186 		boot->HugeSectors = 0;
187 		boot->NumSectors = boot->Sectors;
188 	} else
189 		boot->NumSectors = boot->HugeSectors;
190 	boot->NumClusters = (boot->NumSectors - boot->ClusterOffset) / boot->SecPerClust;
191 
192 	if (boot->flags&FAT32)
193 		boot->ClustMask = CLUST32_MASK;
194 	else if (boot->NumClusters < (CLUST_RSRVD&CLUST12_MASK))
195 		boot->ClustMask = CLUST12_MASK;
196 	else if (boot->NumClusters < (CLUST_RSRVD&CLUST16_MASK))
197 		boot->ClustMask = CLUST16_MASK;
198 	else {
199 		pfatal("Filesystem too big (%u clusters) for non-FAT32 partition\n",
200 		       boot->NumClusters);
201 		return FSFATAL;
202 	}
203 
204 	switch (boot->ClustMask) {
205 	case CLUST32_MASK:
206 		boot->NumFatEntries = (boot->FATsecs * boot->BytesPerSec) / 4;
207 		break;
208 	case CLUST16_MASK:
209 		boot->NumFatEntries = (boot->FATsecs * boot->BytesPerSec) / 2;
210 		break;
211 	default:
212 		boot->NumFatEntries = (boot->FATsecs * boot->BytesPerSec * 2) / 3;
213 		break;
214 	}
215 
216 	if (boot->NumFatEntries < boot->NumClusters) {
217 		pfatal("FAT size too small, %u entries won't fit into %u sectors\n",
218 		       boot->NumClusters, boot->FATsecs);
219 		return (FSFATAL);
220 	}
221 	boot->ClusterSize = boot->BytesPerSec * boot->SecPerClust;
222 
223 	boot->NumFiles = 1;
224 	boot->NumFree = 0;
225 
226 	return ret;
227 }
228 
229 int
230 writefsinfo(dosfs, boot)
231 	int dosfs;
232 	struct bootblock *boot;
233 {
234 	u_char fsinfo[2 * DOSBOOTBLOCKSIZE];
235 
236 	if (lseek(dosfs, boot->FSInfo * boot->BytesPerSec, SEEK_SET)
237 	    != boot->FSInfo * boot->BytesPerSec
238 	    || read(dosfs, fsinfo, sizeof fsinfo) != sizeof fsinfo) {
239 		perror("could not read fsinfo block");
240 		return FSFATAL;
241 	}
242 	fsinfo[0x1e8] = (u_char)boot->FSFree;
243 	fsinfo[0x1e9] = (u_char)(boot->FSFree >> 8);
244 	fsinfo[0x1ea] = (u_char)(boot->FSFree >> 16);
245 	fsinfo[0x1eb] = (u_char)(boot->FSFree >> 24);
246 	fsinfo[0x1ec] = (u_char)boot->FSNext;
247 	fsinfo[0x1ed] = (u_char)(boot->FSNext >> 8);
248 	fsinfo[0x1ee] = (u_char)(boot->FSNext >> 16);
249 	fsinfo[0x1ef] = (u_char)(boot->FSNext >> 24);
250 	if (lseek(dosfs, boot->FSInfo * boot->BytesPerSec, SEEK_SET)
251 	    != boot->FSInfo * boot->BytesPerSec
252 	    || write(dosfs, fsinfo, sizeof fsinfo)
253 	    != sizeof fsinfo) {
254 		perror("Unable to write FSInfo");
255 		return FSFATAL;
256 	}
257 	/*
258 	 * Technically, we should return FSBOOTMOD here.
259 	 *
260 	 * However, since Win95 OSR2 (the first M$ OS that has
261 	 * support for FAT32) doesn't maintain the FSINFO block
262 	 * correctly, it has to be fixed pretty often.
263 	 *
264 	 * Therefor, we handle the FSINFO block only informally,
265 	 * fixing it if necessary, but otherwise ignoring the
266 	 * fact that it was incorrect.
267 	 */
268 	return 0;
269 }
270