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
33
34#include <sys/cdefs.h>
35#ifndef lint
36__RCSID("$NetBSD: boot.c,v 1.9 2003/07/24 19:25:46 ws Exp $");
37static const char rcsid[] =
38  "$FreeBSD: src/sbin/fsck_msdosfs/boot.c,v 1.4.28.1 2009/04/15 03:14:26 kensmith 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
50int
51readboot(dosfs, boot)
52	int dosfs;
53	struct bootblock *boot;
54{
55	u_char block[DOSBOOTBLOCKSIZE];
56	u_char fsinfo[2 * DOSBOOTBLOCKSIZE];
57	u_char backup[DOSBOOTBLOCKSIZE];
58	int ret = FSOK;
59
60	if (read(dosfs, block, sizeof block) < sizeof block) {
61		perror("could not read boot block");
62                exit(2);
63	}
64
65	if (block[510] != 0x55 || block[511] != 0xaa) {
66		pfatal("Invalid signature in boot block: %02x%02x", block[511], block[510]);
67                exit(2);
68	}
69
70	memset(boot, 0, sizeof *boot);
71	boot->ValidFat = -1;
72
73	/* decode bios parameter block */
74	boot->BytesPerSec = block[11] + (block[12] << 8);
75	boot->SecPerClust = block[13];
76	boot->ResSectors = block[14] + (block[15] << 8);
77	boot->FATs = block[16];
78	boot->RootDirEnts = block[17] + (block[18] << 8);
79	boot->Sectors = block[19] + (block[20] << 8);
80	boot->Media = block[21];
81	boot->FATsmall = block[22] + (block[23] << 8);
82	boot->SecPerTrack = block[24] + (block[25] << 8);
83	boot->Heads = block[26] + (block[27] << 8);
84	boot->HiddenSecs = block[28] + (block[29] << 8) + (block[30] << 16) + (block[31] << 24);
85	boot->HugeSectors = block[32] + (block[33] << 8) + (block[34] << 16) + (block[35] << 24);
86
87	boot->FATsecs = boot->FATsmall;
88
89	if (!boot->RootDirEnts)
90		boot->flags |= FAT32;
91	if (boot->flags & FAT32) {
92		boot->FATsecs = block[36] + (block[37] << 8)
93				+ (block[38] << 16) + (block[39] << 24);
94		if (block[40] & 0x80)
95			boot->ValidFat = block[40] & 0x0f;
96
97		/* check version number: */
98		if (block[42] || block[43]) {
99			/* Correct?				XXX */
100			pfatal("Unknown file system version: %x.%x",
101			       block[43], block[42]);
102                        exit(2);
103		}
104		boot->RootCl = block[44] + (block[45] << 8)
105			       + (block[46] << 16) + (block[47] << 24);
106		boot->FSInfo = block[48] + (block[49] << 8);
107		boot->Backup = block[50] + (block[51] << 8);
108
109		/* If the OEM Name field is EXFAT, it's not FAT32, so bail */
110		if (!memcmp(&block[3], "EXFAT   ", 8)) {
111			pfatal("exFAT filesystem is not supported.");
112			return FSFATAL;
113		}
114
115		/* check basic parameters */
116		if ((boot->FSInfo == 0) ||
117		    (boot->BytesPerSec % DOSBOOTBLOCKSIZE != 0) ||
118		    (boot->BytesPerSec / DOSBOOTBLOCKSIZE == 0) ||
119		    (boot->SecPerClust == 0)) {
120			/*
121			 * Either the BIOS Parameter Block has been corrupted,
122                         * or this is not a FAT32 filesystem, most likely an
123                         * exFAT filesystem.
124			 */
125			pfatal("Invalid FAT32 Extended BIOS Parameter Block");
126			return FSFATAL;
127		}
128		if (lseek(dosfs, boot->FSInfo * boot->BytesPerSec, SEEK_SET)
129		    != boot->FSInfo * boot->BytesPerSec
130		    || read(dosfs, fsinfo, sizeof fsinfo)
131		    != sizeof fsinfo) {
132			perror("could not read fsinfo block");
133			return FSFATAL;
134		}
135		if (memcmp(fsinfo, "RRaA", 4)
136		    || memcmp(fsinfo + 0x1e4, "rrAa", 4)
137		    || fsinfo[0x1fc]
138		    || fsinfo[0x1fd]
139		    || fsinfo[0x1fe] != 0x55
140		    || fsinfo[0x1ff] != 0xaa
141		    || fsinfo[0x3fc]
142		    || fsinfo[0x3fd]
143		    || fsinfo[0x3fe] != 0x55
144		    || fsinfo[0x3ff] != 0xaa) {
145			pwarn("Invalid signature in fsinfo block\n");
146			if (ask(1, "fix")) {
147				memcpy(fsinfo, "RRaA", 4);
148				memcpy(fsinfo + 0x1e4, "rrAa", 4);
149				fsinfo[0x1fc] = fsinfo[0x1fd] = 0;
150				fsinfo[0x1fe] = 0x55;
151				fsinfo[0x1ff] = 0xaa;
152				fsinfo[0x3fc] = fsinfo[0x3fd] = 0;
153				fsinfo[0x3fe] = 0x55;
154				fsinfo[0x3ff] = 0xaa;
155				if (lseek(dosfs, boot->FSInfo * boot->BytesPerSec, SEEK_SET)
156				    != boot->FSInfo * boot->BytesPerSec
157				    || write(dosfs, fsinfo, sizeof fsinfo)
158				    != sizeof fsinfo) {
159					perror("Unable to write FSInfo");
160					return FSFATAL;
161				}
162				ret = FSBOOTMOD;
163			} else
164				boot->FSInfo = 0;
165		}
166		if (boot->FSInfo) {
167			boot->FSFree = fsinfo[0x1e8] + (fsinfo[0x1e9] << 8)
168				       + (fsinfo[0x1ea] << 16)
169				       + (fsinfo[0x1eb] << 24);
170			boot->FSNext = fsinfo[0x1ec] + (fsinfo[0x1ed] << 8)
171				       + (fsinfo[0x1ee] << 16)
172				       + (fsinfo[0x1ef] << 24);
173		}
174
175		if (lseek(dosfs, boot->Backup * boot->BytesPerSec, SEEK_SET)
176		    != boot->Backup * boot->BytesPerSec
177		    || read(dosfs, backup, sizeof backup) != sizeof  backup) {
178			perror("could not read backup bootblock");
179			return FSFATAL;
180		}
181		backup[65] = block[65];				/* XXX */
182		if (memcmp(block + 11, backup + 11, 79)) {
183                        char tmp[255];
184                        int i;
185
186			/*
187			 * For now, lets not bail out if they don't match
188			 * It seems a lot of sdcards are formatted with
189			 * the backup either empty or containing garbage.
190			 */
191
192			pwarn("Primary/Backup bootblock miscompare\n");
193
194                        strcpy(tmp, "");
195                        pwarn("Primary:\n");
196			for (i = 0; i < 79; i++) {
197				char tmp2[16];
198                                snprintf(tmp2, sizeof(tmp2), "%.2x ", block[11 + i]);
199				strcat(tmp, tmp2);
200                        }
201                        pwarn("%s\n", tmp);
202
203			strcpy(tmp, "");
204                        pwarn("Backup:\n");
205			for (i = 0; i < 79; i++) {
206				char tmp2[16];
207                                snprintf(tmp2, sizeof(tmp2), "%.2x ", backup[11 + i]);
208				strcat(tmp, tmp2);
209                        }
210                        pwarn("%s\n", tmp);
211		}
212		/* Check backup FSInfo?					XXX */
213	}
214
215	if (boot->BytesPerSec % DOSBOOTBLOCKSIZE != 0) {
216		pfatal("Invalid sector size: %u", boot->BytesPerSec);
217		return FSFATAL;
218	}
219	if (boot->SecPerClust == 0) {
220		pfatal("Invalid cluster size: %u", boot->SecPerClust);
221		return FSFATAL;
222	}
223	if (boot->BytesPerSec == 0) {
224		pfatal("Invalid sector size: %u", boot->BytesPerSec);
225		return FSFATAL;
226	}
227	if (boot->FATs == 0) {
228		pfatal("Invalid number of FATs: %u", boot->FATs);
229		return FSFATAL;
230	}
231	if (boot->Sectors) {
232		boot->HugeSectors = 0;
233		boot->NumSectors = boot->Sectors;
234	} else
235		boot->NumSectors = boot->HugeSectors;
236
237	boot->ClusterOffset = (boot->RootDirEnts * 32 + boot->BytesPerSec - 1)
238	    / boot->BytesPerSec
239	    + boot->ResSectors
240	    + boot->FATs * boot->FATsecs
241	    - CLUST_FIRST * boot->SecPerClust;
242
243	boot->NumClusters = (boot->NumSectors - boot->ClusterOffset) / boot->SecPerClust;
244
245	if (boot->flags&FAT32)
246		boot->ClustMask = CLUST32_MASK;
247	else if (boot->NumClusters < (CLUST_RSRVD&CLUST12_MASK))
248		boot->ClustMask = CLUST12_MASK;
249	else if (boot->NumClusters < (CLUST_RSRVD&CLUST16_MASK))
250		boot->ClustMask = CLUST16_MASK;
251	else {
252		pfatal("Filesystem too big (%u clusters) for non-FAT32 partition",
253		       boot->NumClusters);
254		return FSFATAL;
255	}
256
257	switch (boot->ClustMask) {
258	case CLUST32_MASK:
259		boot->NumFatEntries = (boot->FATsecs * boot->BytesPerSec) / 4;
260		break;
261	case CLUST16_MASK:
262		boot->NumFatEntries = (boot->FATsecs * boot->BytesPerSec) / 2;
263		break;
264	default:
265		boot->NumFatEntries = (boot->FATsecs * boot->BytesPerSec * 2) / 3;
266		break;
267	}
268
269	if (boot->NumFatEntries < boot->NumClusters) {
270		pfatal("FAT size too small, %u entries won't fit into %u sectors\n",
271		       boot->NumClusters, boot->FATsecs);
272		return FSFATAL;
273	}
274	boot->ClusterSize = boot->BytesPerSec * boot->SecPerClust;
275
276	boot->NumFiles = 1;
277	boot->NumFree = 0;
278
279	return ret;
280}
281
282int
283writefsinfo(dosfs, boot)
284	int dosfs;
285	struct bootblock *boot;
286{
287	u_char fsinfo[2 * DOSBOOTBLOCKSIZE];
288
289	if (lseek(dosfs, boot->FSInfo * boot->BytesPerSec, SEEK_SET)
290	    != boot->FSInfo * boot->BytesPerSec
291	    || read(dosfs, fsinfo, sizeof fsinfo) != sizeof fsinfo) {
292		perror("could not read fsinfo block");
293		return FSFATAL;
294	}
295	fsinfo[0x1e8] = (u_char)boot->FSFree;
296	fsinfo[0x1e9] = (u_char)(boot->FSFree >> 8);
297	fsinfo[0x1ea] = (u_char)(boot->FSFree >> 16);
298	fsinfo[0x1eb] = (u_char)(boot->FSFree >> 24);
299	fsinfo[0x1ec] = (u_char)boot->FSNext;
300	fsinfo[0x1ed] = (u_char)(boot->FSNext >> 8);
301	fsinfo[0x1ee] = (u_char)(boot->FSNext >> 16);
302	fsinfo[0x1ef] = (u_char)(boot->FSNext >> 24);
303	if (lseek(dosfs, boot->FSInfo * boot->BytesPerSec, SEEK_SET)
304	    != boot->FSInfo * boot->BytesPerSec
305	    || write(dosfs, fsinfo, sizeof fsinfo)
306	    != sizeof fsinfo) {
307		perror("Unable to write FSInfo");
308		return FSFATAL;
309	}
310	/*
311	 * Technically, we should return FSBOOTMOD here.
312	 *
313	 * However, since Win95 OSR2 (the first M$ OS that has
314	 * support for FAT32) doesn't maintain the FSINFO block
315	 * correctly, it has to be fixed pretty often.
316	 *
317	 * Therefor, we handle the FSINFO block only informally,
318	 * fixing it if necessary, but otherwise ignoring the
319	 * fact that it was incorrect.
320	 */
321	return 0;
322}
323