1/*	$NetBSD: boot.c,v 1.14 2008/06/13 20:46:09 martin 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 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28
29#include <sys/cdefs.h>
30#ifndef lint
31__RCSID("$NetBSD: boot.c,v 1.14 2008/06/13 20:46:09 martin Exp $");
32#endif /* not lint */
33
34#include <stdlib.h>
35#include <string.h>
36#include <stdio.h>
37#include <unistd.h>
38
39#include "ext.h"
40#include "fsutil.h"
41
42int
43readboot(int dosfs, struct bootblock *boot)
44{
45	u_char block[DOSBOOTBLOCKSIZE];
46	u_char fsinfo[2 * DOSBOOTBLOCKSIZE];
47	u_char backup[DOSBOOTBLOCKSIZE];
48	int ret = FSOK;
49	int i;
50
51	if ((size_t)read(dosfs, block, sizeof block) != sizeof block) {
52		perr("could not read boot block");
53		return FSFATAL;
54	}
55
56	if (block[510] != 0x55 || block[511] != 0xaa) {
57		pfatal("Invalid signature in boot block: %02x%02x", block[511], block[510]);
58		return FSFATAL;
59	}
60
61	memset(boot, 0, sizeof *boot);
62	boot->ValidFat = -1;
63
64	/* decode bios parameter block */
65	boot->BytesPerSec = block[11] + (block[12] << 8);
66	boot->SecPerClust = block[13];
67	boot->ResSectors = block[14] + (block[15] << 8);
68	boot->FATs = block[16];
69	boot->RootDirEnts = block[17] + (block[18] << 8);
70	boot->Sectors = block[19] + (block[20] << 8);
71	boot->Media = block[21];
72	boot->FATsmall = block[22] + (block[23] << 8);
73	boot->SecPerTrack = block[24] + (block[25] << 8);
74	boot->Heads = block[26] + (block[27] << 8);
75	boot->HiddenSecs = block[28] + (block[29] << 8) + (block[30] << 16) + (block[31] << 24);
76	boot->HugeSectors = block[32] + (block[33] << 8) + (block[34] << 16) + (block[35] << 24);
77
78	boot->FATsecs = boot->FATsmall;
79
80	if (!boot->RootDirEnts)
81		boot->flags |= FAT32;
82	if (boot->flags & FAT32) {
83		boot->FATsecs = block[36] + (block[37] << 8)
84				+ (block[38] << 16) + (block[39] << 24);
85		if (block[40] & 0x80)
86			boot->ValidFat = block[40] & 0x0f;
87
88		/* check version number: */
89		if (block[42] || block[43]) {
90			/* Correct?				XXX */
91			pfatal("Unknown filesystem version: %x.%x",
92			       block[43], block[42]);
93			return FSFATAL;
94		}
95		boot->RootCl = block[44] + (block[45] << 8)
96			       + (block[46] << 16) + (block[47] << 24);
97		boot->FSInfo = block[48] + (block[49] << 8);
98		boot->Backup = block[50] + (block[51] << 8);
99
100		if (lseek(dosfs, boot->FSInfo * boot->BytesPerSec, SEEK_SET)
101		    != boot->FSInfo * boot->BytesPerSec
102		    || read(dosfs, fsinfo, sizeof fsinfo)
103		    != sizeof fsinfo) {
104			perr("could not read fsinfo block");
105			return FSFATAL;
106		}
107		if (memcmp(fsinfo, "RRaA", 4)
108		    || memcmp(fsinfo + 0x1e4, "rrAa", 4)
109		    || fsinfo[0x1fc]
110		    || fsinfo[0x1fd]
111		    || fsinfo[0x1fe] != 0x55
112		    || fsinfo[0x1ff] != 0xaa
113		    || fsinfo[0x3fc]
114		    || fsinfo[0x3fd]
115		    || fsinfo[0x3fe] != 0x55
116		    || fsinfo[0x3ff] != 0xaa) {
117			pwarn("Invalid signature in fsinfo block");
118			if (ask(0, "fix")) {
119				memcpy(fsinfo, "RRaA", 4);
120				memcpy(fsinfo + 0x1e4, "rrAa", 4);
121				fsinfo[0x1fc] = fsinfo[0x1fd] = 0;
122				fsinfo[0x1fe] = 0x55;
123				fsinfo[0x1ff] = 0xaa;
124				fsinfo[0x3fc] = fsinfo[0x3fd] = 0;
125				fsinfo[0x3fe] = 0x55;
126				fsinfo[0x3ff] = 0xaa;
127				if (lseek(dosfs, boot->FSInfo * boot->BytesPerSec, SEEK_SET)
128				    != boot->FSInfo * boot->BytesPerSec
129				    || write(dosfs, fsinfo, sizeof fsinfo)
130				    != sizeof fsinfo) {
131					perr("Unable to write FSInfo");
132					return FSFATAL;
133				}
134				ret = FSBOOTMOD;
135			} else
136				boot->FSInfo = 0;
137		}
138		if (boot->FSInfo) {
139			boot->FSFree = fsinfo[0x1e8] + (fsinfo[0x1e9] << 8)
140				       + (fsinfo[0x1ea] << 16)
141				       + (fsinfo[0x1eb] << 24);
142			boot->FSNext = fsinfo[0x1ec] + (fsinfo[0x1ed] << 8)
143				       + (fsinfo[0x1ee] << 16)
144				       + (fsinfo[0x1ef] << 24);
145		}
146
147		if (lseek(dosfs, boot->Backup * boot->BytesPerSec, SEEK_SET)
148		    != boot->Backup * boot->BytesPerSec
149		    || read(dosfs, backup, sizeof backup) != sizeof  backup) {
150			perr("could not read backup bootblock");
151			return FSFATAL;
152		}
153		backup[65] = block[65];				/* XXX */
154		if (memcmp(block + 11, backup + 11, 79)) {
155			/*
156			 * XXX We require a reference that explains
157			 * that these bytes need to match, or should
158			 * drop the check.  gdt@ has observed
159			 * filesystems that work fine under Windows XP
160			 * and NetBSD that do not match, so the
161			 * requirement is suspect.  For now, just
162			 * print out useful information and continue.
163			 */
164			pfatal("backup (block %d) mismatch with primary bootblock:\n",
165			        boot->Backup);
166			for (i = 11; i < 11 + 90; i++) {
167				if (block[i] != backup[i])
168					pfatal("\ti=%d\tprimary 0x%02x\tbackup 0x%02x\n",
169					       i, block[i], backup[i]);
170			}
171		}
172		/* Check backup FSInfo?					XXX */
173	}
174
175	boot->ClusterOffset = (boot->RootDirEnts * 32 + boot->BytesPerSec - 1)
176	    / boot->BytesPerSec
177	    + boot->ResSectors
178	    + boot->FATs * boot->FATsecs
179	    - CLUST_FIRST * boot->SecPerClust;
180
181	if (boot->BytesPerSec % DOSBOOTBLOCKSIZE != 0) {
182		pfatal("Invalid sector size: %u", boot->BytesPerSec);
183		return FSFATAL;
184	}
185	if (boot->SecPerClust == 0) {
186		pfatal("Invalid cluster size: %u", boot->SecPerClust);
187		return FSFATAL;
188	}
189	if (boot->Sectors) {
190		boot->HugeSectors = 0;
191		boot->NumSectors = boot->Sectors;
192	} else
193		boot->NumSectors = boot->HugeSectors;
194	boot->NumClusters = (boot->NumSectors - boot->ClusterOffset) / boot->SecPerClust;
195
196	if (boot->flags&FAT32)
197		boot->ClustMask = CLUST32_MASK;
198	else if (boot->NumClusters < (CLUST_RSRVD&CLUST12_MASK))
199		boot->ClustMask = CLUST12_MASK;
200	else if (boot->NumClusters < (CLUST_RSRVD&CLUST16_MASK))
201		boot->ClustMask = CLUST16_MASK;
202	else {
203		pfatal("Filesystem too big (%u clusters) for non-FAT32 partition",
204		       boot->NumClusters);
205		return FSFATAL;
206	}
207
208	switch (boot->ClustMask) {
209	case CLUST32_MASK:
210		boot->NumFatEntries = (boot->FATsecs * boot->BytesPerSec) / 4;
211		break;
212	case CLUST16_MASK:
213		boot->NumFatEntries = (boot->FATsecs * boot->BytesPerSec) / 2;
214		break;
215	default:
216		boot->NumFatEntries = (boot->FATsecs * boot->BytesPerSec * 2) / 3;
217		break;
218	}
219
220	if (boot->NumFatEntries < boot->NumClusters) {
221		pfatal("FAT size too small, %u entries won't fit into %u sectors\n",
222		       boot->NumClusters, boot->FATsecs);
223		return FSFATAL;
224	}
225	boot->ClusterSize = boot->BytesPerSec * boot->SecPerClust;
226
227	boot->NumFiles = 1;
228	boot->NumFree = 0;
229
230	return ret;
231}
232
233int
234writefsinfo(int dosfs, struct bootblock *boot)
235{
236	u_char fsinfo[2 * DOSBOOTBLOCKSIZE];
237
238	if (lseek(dosfs, boot->FSInfo * boot->BytesPerSec, SEEK_SET)
239	    != boot->FSInfo * boot->BytesPerSec
240	    || read(dosfs, fsinfo, sizeof fsinfo) != sizeof fsinfo) {
241		perr("could not read fsinfo block");
242		return FSFATAL;
243	}
244	fsinfo[0x1e8] = (u_char)boot->FSFree;
245	fsinfo[0x1e9] = (u_char)(boot->FSFree >> 8);
246	fsinfo[0x1ea] = (u_char)(boot->FSFree >> 16);
247	fsinfo[0x1eb] = (u_char)(boot->FSFree >> 24);
248	fsinfo[0x1ec] = (u_char)boot->FSNext;
249	fsinfo[0x1ed] = (u_char)(boot->FSNext >> 8);
250	fsinfo[0x1ee] = (u_char)(boot->FSNext >> 16);
251	fsinfo[0x1ef] = (u_char)(boot->FSNext >> 24);
252	if (lseek(dosfs, boot->FSInfo * boot->BytesPerSec, SEEK_SET)
253	    != boot->FSInfo * boot->BytesPerSec
254	    || write(dosfs, fsinfo, sizeof fsinfo)
255	    != sizeof fsinfo) {
256		perr("Unable to write FSInfo");
257		return FSFATAL;
258	}
259	/*
260	 * Technically, we should return FSBOOTMOD here.
261	 *
262	 * However, since Win95 OSR2 (the first M$ OS that has
263	 * support for FAT32) doesn't maintain the FSINFO block
264	 * correctly, it has to be fixed pretty often.
265	 *
266	 * Therefor, we handle the FSINFO block only informally,
267	 * fixing it if necessary, but otherwise ignoring the
268	 * fact that it was incorrect.
269	 */
270	return 0;
271}
272