xref: /netbsd-src/sbin/fsck_msdos/boot.c (revision aaf4ece63a859a04e37cf3a7229b5fab0157cc06)
1 /*	$NetBSD: boot.c,v 1.10 2005/01/19 20:00:45 xtraeme Exp $	*/
2 
3 /*
4  * Copyright (C) 1995, 1997 Wolfgang Solfrank
5  * Copyright (c) 1995 Martin Husemann
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by Martin Husemann
18  *	and Wolfgang Solfrank.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 
36 #include <sys/cdefs.h>
37 #ifndef lint
38 __RCSID("$NetBSD: boot.c,v 1.10 2005/01/19 20:00:45 xtraeme 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 #include "fsutil.h"
49 
50 int
51 readboot(int dosfs, struct bootblock *boot)
52 {
53 	u_char block[DOSBOOTBLOCKSIZE];
54 	u_char fsinfo[2 * DOSBOOTBLOCKSIZE];
55 	u_char backup[DOSBOOTBLOCKSIZE];
56 	int ret = FSOK;
57 
58 	if (read(dosfs, block, sizeof block) < sizeof block) {
59 		perror("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", 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",
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 (lseek(dosfs, boot->FSInfo * boot->BytesPerSec, SEEK_SET)
108 		    != boot->FSInfo * boot->BytesPerSec
109 		    || read(dosfs, fsinfo, sizeof fsinfo)
110 		    != sizeof fsinfo) {
111 			perror("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 (lseek(dosfs, boot->FSInfo * boot->BytesPerSec, SEEK_SET)
135 				    != boot->FSInfo * boot->BytesPerSec
136 				    || write(dosfs, fsinfo, sizeof fsinfo)
137 				    != sizeof fsinfo) {
138 					perror("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 (lseek(dosfs, boot->Backup * boot->BytesPerSec, SEEK_SET)
155 		    != boot->Backup * boot->BytesPerSec
156 		    || read(dosfs, backup, sizeof backup) != sizeof  backup) {
157 			perror("could not read backup bootblock");
158 			return FSFATAL;
159 		}
160 		backup[65] = block[65];				/* XXX */
161 		if (memcmp(block + 11, backup + 11, 79)) {
162 			/* Correct?					XXX */
163 			pfatal("backup doesn't compare to primary bootblock");
164 			if (alwaysno)
165 				pfatal("\n");
166 			else
167 				return FSFATAL;
168 		}
169 		/* Check backup FSInfo?					XXX */
170 	}
171 
172 	boot->ClusterOffset = (boot->RootDirEnts * 32 + boot->BytesPerSec - 1)
173 	    / boot->BytesPerSec
174 	    + boot->ResSectors
175 	    + boot->FATs * boot->FATsecs
176 	    - CLUST_FIRST * boot->SecPerClust;
177 
178 	if (boot->BytesPerSec % DOSBOOTBLOCKSIZE != 0) {
179 		pfatal("Invalid sector size: %u", boot->BytesPerSec);
180 		return FSFATAL;
181 	}
182 	if (boot->SecPerClust == 0) {
183 		pfatal("Invalid cluster size: %u", boot->SecPerClust);
184 		return FSFATAL;
185 	}
186 	if (boot->Sectors) {
187 		boot->HugeSectors = 0;
188 		boot->NumSectors = boot->Sectors;
189 	} else
190 		boot->NumSectors = boot->HugeSectors;
191 	boot->NumClusters = (boot->NumSectors - boot->ClusterOffset) / boot->SecPerClust;
192 
193 	if (boot->flags&FAT32)
194 		boot->ClustMask = CLUST32_MASK;
195 	else if (boot->NumClusters < (CLUST_RSRVD&CLUST12_MASK))
196 		boot->ClustMask = CLUST12_MASK;
197 	else if (boot->NumClusters < (CLUST_RSRVD&CLUST16_MASK))
198 		boot->ClustMask = CLUST16_MASK;
199 	else {
200 		pfatal("Filesystem too big (%u clusters) for non-FAT32 partition",
201 		       boot->NumClusters);
202 		return FSFATAL;
203 	}
204 
205 	switch (boot->ClustMask) {
206 	case CLUST32_MASK:
207 		boot->NumFatEntries = (boot->FATsecs * boot->BytesPerSec) / 4;
208 		break;
209 	case CLUST16_MASK:
210 		boot->NumFatEntries = (boot->FATsecs * boot->BytesPerSec) / 2;
211 		break;
212 	default:
213 		boot->NumFatEntries = (boot->FATsecs * boot->BytesPerSec * 2) / 3;
214 		break;
215 	}
216 
217 	if (boot->NumFatEntries < boot->NumClusters) {
218 		pfatal("FAT size too small, %u entries won't fit into %u sectors\n",
219 		       boot->NumClusters, boot->FATsecs);
220 		return FSFATAL;
221 	}
222 	boot->ClusterSize = boot->BytesPerSec * boot->SecPerClust;
223 
224 	boot->NumFiles = 1;
225 	boot->NumFree = 0;
226 
227 	return ret;
228 }
229 
230 int
231 writefsinfo(int dosfs, struct bootblock *boot)
232 {
233 	u_char fsinfo[2 * DOSBOOTBLOCKSIZE];
234 
235 	if (lseek(dosfs, boot->FSInfo * boot->BytesPerSec, SEEK_SET)
236 	    != boot->FSInfo * boot->BytesPerSec
237 	    || read(dosfs, fsinfo, sizeof fsinfo) != sizeof fsinfo) {
238 		perror("could not read fsinfo block");
239 		return FSFATAL;
240 	}
241 	fsinfo[0x1e8] = (u_char)boot->FSFree;
242 	fsinfo[0x1e9] = (u_char)(boot->FSFree >> 8);
243 	fsinfo[0x1ea] = (u_char)(boot->FSFree >> 16);
244 	fsinfo[0x1eb] = (u_char)(boot->FSFree >> 24);
245 	fsinfo[0x1ec] = (u_char)boot->FSNext;
246 	fsinfo[0x1ed] = (u_char)(boot->FSNext >> 8);
247 	fsinfo[0x1ee] = (u_char)(boot->FSNext >> 16);
248 	fsinfo[0x1ef] = (u_char)(boot->FSNext >> 24);
249 	if (lseek(dosfs, boot->FSInfo * boot->BytesPerSec, SEEK_SET)
250 	    != boot->FSInfo * boot->BytesPerSec
251 	    || write(dosfs, fsinfo, sizeof fsinfo)
252 	    != sizeof fsinfo) {
253 		perror("Unable to write FSInfo");
254 		return FSFATAL;
255 	}
256 	/*
257 	 * Technically, we should return FSBOOTMOD here.
258 	 *
259 	 * However, since Win95 OSR2 (the first M$ OS that has
260 	 * support for FAT32) doesn't maintain the FSINFO block
261 	 * correctly, it has to be fixed pretty often.
262 	 *
263 	 * Therefor, we handle the FSINFO block only informally,
264 	 * fixing it if necessary, but otherwise ignoring the
265 	 * fact that it was incorrect.
266 	 */
267 	return 0;
268 }
269