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