2 * Copyright (C) 1995, 1997 Wolfgang Solfrank
3 * Copyright (c) 1995 Martin Husemann
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
34 #include <sys/cdefs.h>
36 /* __RCSID("$NetBSD: boot.c,v 1.9 2003/07/24 19:25:46 ws Exp $");*/
37 static const char rcsid
[] =
38 "$FreeBSD: src/sbin/fsck_msdosfs/boot.c,v 1.4.28.1 2009/04/15 03:14:26 kensmith Exp $";
53 struct bootblock
*boot
;
55 u_char block
[DOSBOOTBLOCKSIZE
];
56 u_char fsinfo
[2 * DOSBOOTBLOCKSIZE
];
57 u_char backup
[DOSBOOTBLOCKSIZE
];
60 if (read(dosfs
, block
, sizeof block
) < sizeof block
) {
61 perror("could not read boot block");
65 if (block
[510] != 0x55 || block
[511] != 0xaa) {
66 pfatal("Invalid signature in boot block: %02x%02x", block
[511], block
[510]);
70 memset(boot
, 0, sizeof *boot
);
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);
87 boot
->FATsecs
= boot
->FATsmall
;
89 if (!boot
->RootDirEnts
)
91 if (boot
->flags
& FAT32
) {
92 boot
->FATsecs
= block
[36] + (block
[37] << 8)
93 + (block
[38] << 16) + (block
[39] << 24);
95 boot
->ValidFat
= block
[40] & 0x0f;
97 /* check version number: */
98 if (block
[42] || block
[43]) {
100 pfatal("Unknown file system version: %x.%x",
101 block
[43], block
[42]);
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);
109 if (lseek(dosfs
, boot
->FSInfo
* boot
->BytesPerSec
, SEEK_SET
)
110 != boot
->FSInfo
* boot
->BytesPerSec
111 || read(dosfs
, fsinfo
, sizeof fsinfo
)
113 perror("could not read fsinfo block");
116 if (memcmp(fsinfo
, "RRaA", 4)
117 || memcmp(fsinfo
+ 0x1e4, "rrAa", 4)
120 || fsinfo
[0x1fe] != 0x55
121 || fsinfo
[0x1ff] != 0xaa
124 || fsinfo
[0x3fe] != 0x55
125 || fsinfo
[0x3ff] != 0xaa) {
126 pwarn("Invalid signature in fsinfo block\n");
128 memcpy(fsinfo
, "RRaA", 4);
129 memcpy(fsinfo
+ 0x1e4, "rrAa", 4);
130 fsinfo
[0x1fc] = fsinfo
[0x1fd] = 0;
131 fsinfo
[0x1fe] = 0x55;
132 fsinfo
[0x1ff] = 0xaa;
133 fsinfo
[0x3fc] = fsinfo
[0x3fd] = 0;
134 fsinfo
[0x3fe] = 0x55;
135 fsinfo
[0x3ff] = 0xaa;
136 if (lseek(dosfs
, boot
->FSInfo
* boot
->BytesPerSec
, SEEK_SET
)
137 != boot
->FSInfo
* boot
->BytesPerSec
138 || write(dosfs
, fsinfo
, sizeof fsinfo
)
140 perror("Unable to write FSInfo");
148 boot
->FSFree
= fsinfo
[0x1e8] + (fsinfo
[0x1e9] << 8)
149 + (fsinfo
[0x1ea] << 16)
150 + (fsinfo
[0x1eb] << 24);
151 boot
->FSNext
= fsinfo
[0x1ec] + (fsinfo
[0x1ed] << 8)
152 + (fsinfo
[0x1ee] << 16)
153 + (fsinfo
[0x1ef] << 24);
156 if (lseek(dosfs
, boot
->Backup
* boot
->BytesPerSec
, SEEK_SET
)
157 != boot
->Backup
* boot
->BytesPerSec
158 || read(dosfs
, backup
, sizeof backup
) != sizeof backup
) {
159 perror("could not read backup bootblock");
162 backup
[65] = block
[65]; /* XXX */
163 if (memcmp(block
+ 11, backup
+ 11, 79)) {
168 * For now, lets not bail out if they don't match
169 * It seems a lot of sdcards are formatted with
170 * the backup either empty or containing garbage.
173 pwarn("Primary/Backup bootblock miscompare\n");
177 for (i
= 0; i
< 79; i
++) {
179 snprintf(tmp2
, sizeof(tmp2
), "%.2x ", block
[11 + i
]);
186 for (i
= 0; i
< 79; i
++) {
188 snprintf(tmp2
, sizeof(tmp2
), "%.2x ", backup
[11 + i
]);
193 /* Check backup FSInfo? XXX */
196 boot
->ClusterOffset
= (boot
->RootDirEnts
* 32 + boot
->BytesPerSec
- 1)
199 + boot
->FATs
* boot
->FATsecs
200 - CLUST_FIRST
* boot
->SecPerClust
;
202 if (boot
->BytesPerSec
% DOSBOOTBLOCKSIZE
!= 0) {
203 pfatal("Invalid sector size: %u", boot
->BytesPerSec
);
206 if (boot
->SecPerClust
== 0) {
207 pfatal("Invalid cluster size: %u", boot
->SecPerClust
);
211 boot
->HugeSectors
= 0;
212 boot
->NumSectors
= boot
->Sectors
;
214 boot
->NumSectors
= boot
->HugeSectors
;
215 boot
->NumClusters
= (boot
->NumSectors
- boot
->ClusterOffset
) / boot
->SecPerClust
;
217 if (boot
->flags
&FAT32
)
218 boot
->ClustMask
= CLUST32_MASK
;
219 else if (boot
->NumClusters
< (CLUST_RSRVD
&CLUST12_MASK
))
220 boot
->ClustMask
= CLUST12_MASK
;
221 else if (boot
->NumClusters
< (CLUST_RSRVD
&CLUST16_MASK
))
222 boot
->ClustMask
= CLUST16_MASK
;
224 pfatal("Filesystem too big (%u clusters) for non-FAT32 partition",
229 switch (boot
->ClustMask
) {
231 boot
->NumFatEntries
= (boot
->FATsecs
* boot
->BytesPerSec
) / 4;
234 boot
->NumFatEntries
= (boot
->FATsecs
* boot
->BytesPerSec
) / 2;
237 boot
->NumFatEntries
= (boot
->FATsecs
* boot
->BytesPerSec
* 2) / 3;
241 if (boot
->NumFatEntries
< boot
->NumClusters
) {
242 pfatal("FAT size too small, %u entries won't fit into %u sectors\n",
243 boot
->NumClusters
, boot
->FATsecs
);
246 boot
->ClusterSize
= boot
->BytesPerSec
* boot
->SecPerClust
;
255 writefsinfo(dosfs
, boot
)
257 struct bootblock
*boot
;
259 u_char fsinfo
[2 * DOSBOOTBLOCKSIZE
];
261 if (lseek(dosfs
, boot
->FSInfo
* boot
->BytesPerSec
, SEEK_SET
)
262 != boot
->FSInfo
* boot
->BytesPerSec
263 || read(dosfs
, fsinfo
, sizeof fsinfo
) != sizeof fsinfo
) {
264 perror("could not read fsinfo block");
267 fsinfo
[0x1e8] = (u_char
)boot
->FSFree
;
268 fsinfo
[0x1e9] = (u_char
)(boot
->FSFree
>> 8);
269 fsinfo
[0x1ea] = (u_char
)(boot
->FSFree
>> 16);
270 fsinfo
[0x1eb] = (u_char
)(boot
->FSFree
>> 24);
271 fsinfo
[0x1ec] = (u_char
)boot
->FSNext
;
272 fsinfo
[0x1ed] = (u_char
)(boot
->FSNext
>> 8);
273 fsinfo
[0x1ee] = (u_char
)(boot
->FSNext
>> 16);
274 fsinfo
[0x1ef] = (u_char
)(boot
->FSNext
>> 24);
275 if (lseek(dosfs
, boot
->FSInfo
* boot
->BytesPerSec
, SEEK_SET
)
276 != boot
->FSInfo
* boot
->BytesPerSec
277 || write(dosfs
, fsinfo
, sizeof fsinfo
)
279 perror("Unable to write FSInfo");
283 * Technically, we should return FSBOOTMOD here.
285 * However, since Win95 OSR2 (the first M$ OS that has
286 * support for FAT32) doesn't maintain the FSINFO block
287 * correctly, it has to be fixed pretty often.
289 * Therefor, we handle the FSINFO block only informally,
290 * fixing it if necessary, but otherwise ignoring the
291 * fact that it was incorrect.