xref: /openbsd/sbin/fsck_msdos/boot.c (revision d485f761)
1 /*	$OpenBSD: boot.c,v 1.6 2001/07/03 13:03:44 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.6 2001/07/03 13:03:44 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(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 	boot->ClusterOffset = (boot->RootDirEnts * 32 + boot->BytesPerSec - 1)
170 	    / boot->BytesPerSec
171 	    + boot->ResSectors
172 	    + boot->FATs * boot->FATsecs
173 	    - CLUST_FIRST * boot->SecPerClust;
174 
175 	if (boot->BytesPerSec % DOSBOOTBLOCKSIZE != 0) {
176 		pfatal("Invalid sector size: %u\n", boot->BytesPerSec);
177 		return (FSFATAL);
178 	}
179 	if (boot->SecPerClust == 0) {
180 		pfatal("Invalid cluster size: %u\n", boot->SecPerClust);
181 		return (FSFATAL);
182 	}
183 	if (boot->Sectors) {
184 		boot->HugeSectors = 0;
185 		boot->NumSectors = boot->Sectors;
186 	} else
187 		boot->NumSectors = boot->HugeSectors;
188 	boot->NumClusters = (boot->NumSectors - boot->ClusterOffset) / boot->SecPerClust;
189 
190 	if (boot->flags&FAT32)
191 		boot->ClustMask = CLUST32_MASK;
192 	else if (boot->NumClusters < (CLUST_RSRVD&CLUST12_MASK))
193 		boot->ClustMask = CLUST12_MASK;
194 	else if (boot->NumClusters < (CLUST_RSRVD&CLUST16_MASK))
195 		boot->ClustMask = CLUST16_MASK;
196 	else {
197 		pfatal("Filesystem too big (%u clusters) for non-FAT32 partition\n",
198 		       boot->NumClusters);
199 		return FSFATAL;
200 	}
201 
202 	switch (boot->ClustMask) {
203 	case CLUST32_MASK:
204 		boot->NumFatEntries = (boot->FATsecs * boot->BytesPerSec) / 4;
205 		break;
206 	case CLUST16_MASK:
207 		boot->NumFatEntries = (boot->FATsecs * boot->BytesPerSec) / 2;
208 		break;
209 	default:
210 		boot->NumFatEntries = (boot->FATsecs * boot->BytesPerSec * 2) / 3;
211 		break;
212 	}
213 
214 	if (boot->NumFatEntries < boot->NumClusters) {
215 		pfatal("FAT size too small, %u entries won't fit into %u sectors\n",
216 		       boot->NumClusters, boot->FATsecs);
217 		return (FSFATAL);
218 	}
219 	boot->ClusterSize = boot->BytesPerSec * boot->SecPerClust;
220 
221 	boot->NumFiles = 1;
222 	boot->NumFree = 0;
223 
224 	return ret;
225 }
226 
227 int
228 writefsinfo(dosfs, boot)
229 	int dosfs;
230 	struct bootblock *boot;
231 {
232 	u_char fsinfo[2 * DOSBOOTBLOCKSIZE];
233 
234 	if (lseek(dosfs, boot->FSInfo * boot->BytesPerSec, SEEK_SET)
235 	    != boot->FSInfo * boot->BytesPerSec
236 	    || read(dosfs, fsinfo, sizeof fsinfo) != sizeof fsinfo) {
237 		perror("could not read fsinfo block");
238 		return FSFATAL;
239 	}
240 	fsinfo[0x1e8] = (u_char)boot->FSFree;
241 	fsinfo[0x1e9] = (u_char)(boot->FSFree >> 8);
242 	fsinfo[0x1ea] = (u_char)(boot->FSFree >> 16);
243 	fsinfo[0x1eb] = (u_char)(boot->FSFree >> 24);
244 	fsinfo[0x1ec] = (u_char)boot->FSNext;
245 	fsinfo[0x1ed] = (u_char)(boot->FSNext >> 8);
246 	fsinfo[0x1ee] = (u_char)(boot->FSNext >> 16);
247 	fsinfo[0x1ef] = (u_char)(boot->FSNext >> 24);
248 	if (lseek(dosfs, boot->FSInfo * boot->BytesPerSec, SEEK_SET)
249 	    != boot->FSInfo * boot->BytesPerSec
250 	    || write(dosfs, fsinfo, sizeof fsinfo)
251 	    != sizeof fsinfo) {
252 		perror("Unable to write FSInfo");
253 		return FSFATAL;
254 	}
255 	/*
256 	 * Technically, we should return FSBOOTMOD here.
257 	 *
258 	 * However, since Win95 OSR2 (the first M$ OS that has
259 	 * support for FAT32) doesn't maintain the FSINFO block
260 	 * correctly, it has to be fixed pretty often.
261 	 *
262 	 * Therefor, we handle the FSINFO block only informally,
263 	 * fixing it if neccessary, but otherwise ignoring the
264 	 * fact that it was incorrect.
265 	 */
266 	return 0;
267 }
268