2 * Copyright (C) 1995, 1996, 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: fat.c,v 1.12 2000/10/10 20:24:52 is Exp $");*/
37 static const char rcsid
[] =
38 "$FreeBSD: src/sbin/fsck_msdosfs/fat.c,v 1.9 2008/01/31 13:22:13 yar Exp $";
50 static int checkclnum(struct bootblock
*, int, cl_t
, cl_t
*);
51 static int clustdiffer(cl_t
, cl_t
*, cl_t
*, int);
52 static int tryclear(struct bootblock
*, struct fatEntry
*, cl_t
, cl_t
*);
53 static int _readfat(int, struct bootblock
*, int, u_char
**);
56 * The first 2 FAT entries contain pseudo-cluster numbers with the following
59 * 31...... ........ ........ .......0
60 * rrrr1111 11111111 11111111 mmmmmmmm FAT32 entry 0
61 * rrrrsh11 11111111 11111111 11111xxx FAT32 entry 1
63 * 11111111 mmmmmmmm FAT16 entry 0
64 * sh111111 11111xxx FAT16 entry 1
67 * m = BPB media ID byte
68 * s = clean flag (1 = dismounted; 0 = still mounted)
69 * h = hard error flag (1 = ok; 0 = I/O error)
74 checkdirty(int fs
, struct bootblock
*boot
)
80 if (boot
->ClustMask
!= CLUST16_MASK
&& boot
->ClustMask
!= CLUST32_MASK
)
83 off
= boot
->ResSectors
;
84 off
*= boot
->BytesPerSec
;
86 buffer
= malloc(boot
->BytesPerSec
);
88 perror("No space for FAT");
92 if (lseek(fs
, off
, SEEK_SET
) != off
) {
93 perror("Unable to read FAT");
97 if (read(fs
, buffer
, boot
->BytesPerSec
) != boot
->BytesPerSec
) {
98 perror("Unable to read FAT");
103 * If we don't understand the FAT, then the file system must be
104 * assumed to be unclean.
106 if (buffer
[0] != boot
->Media
|| buffer
[1] != 0xff)
108 if (boot
->ClustMask
== CLUST16_MASK
) {
109 if ((buffer
[2] & 0xf8) != 0xf8 || (buffer
[3] & 0x3f) != 0x3f)
112 if (buffer
[2] != 0xff || (buffer
[3] & 0x0f) != 0x0f
113 || (buffer
[4] & 0xf8) != 0xf8 || buffer
[5] != 0xff
114 || buffer
[6] != 0xff || (buffer
[7] & 0x03) != 0x03)
119 * Now check the actual clean flag (and the no-error flag).
121 if (boot
->ClustMask
== CLUST16_MASK
) {
122 if ((buffer
[3] & 0xc0) == 0xc0)
125 if ((buffer
[7] & 0x0c) == 0x0c)
135 * Check a cluster number for valid value
138 checkclnum(struct bootblock
*boot
, int fat
, cl_t cl
, cl_t
*next
)
140 if (*next
>= (CLUST_RSRVD
&boot
->ClustMask
))
141 *next
|= ~boot
->ClustMask
;
142 if (*next
== CLUST_FREE
) {
146 if (*next
== CLUST_BAD
) {
150 if (*next
< CLUST_FIRST
151 || (*next
>= boot
->NumClusters
&& *next
< CLUST_EOFS
)) {
152 pwarn("Cluster %u in FAT %d continues with %s cluster number %u\n",
154 *next
< CLUST_RSRVD
? "out of range" : "reserved",
155 *next
&boot
->ClustMask
);
156 if (ask(1, "Truncate")) {
166 * Read a FAT from disk. Returns 1 if successful, 0 otherwise.
169 _readfat(int fs
, struct bootblock
*boot
, int no
, u_char
**buffer
)
173 printf("Attempting to allocate %u KB for FAT\n",
174 (boot
->FATsecs
* boot
->BytesPerSec
) / 1024);
176 *buffer
= malloc(boot
->FATsecs
* boot
->BytesPerSec
);
177 if (*buffer
== NULL
) {
178 perror("No space for FAT");
182 off
= boot
->ResSectors
+ no
* boot
->FATsecs
;
183 off
*= boot
->BytesPerSec
;
185 if (lseek(fs
, off
, SEEK_SET
) != off
) {
186 perror("Unable to read FAT");
190 if (read(fs
, *buffer
, boot
->FATsecs
* boot
->BytesPerSec
)
191 != boot
->FATsecs
* boot
->BytesPerSec
) {
192 perror("Unable to read FAT");
204 * Read a FAT and decode it into internal format
207 readfat(int fs
, struct bootblock
*boot
, int no
, struct fatEntry
**fp
)
209 struct fatEntry
*fat
;
214 boot
->NumFree
= boot
->NumBad
= 0;
216 if (!_readfat(fs
, boot
, no
, &buffer
))
219 fat
= calloc(boot
->NumClusters
, sizeof(struct fatEntry
));
221 perror("No space for FAT");
226 if (buffer
[0] != boot
->Media
227 || buffer
[1] != 0xff || buffer
[2] != 0xff
228 || (boot
->ClustMask
== CLUST16_MASK
&& buffer
[3] != 0xff)
229 || (boot
->ClustMask
== CLUST32_MASK
230 && ((buffer
[3]&0x0f) != 0x0f
231 || buffer
[4] != 0xff || buffer
[5] != 0xff
232 || buffer
[6] != 0xff || (buffer
[7]&0x0f) != 0x0f))) {
234 /* Windows 95 OSR2 (and possibly any later) changes
235 * the FAT signature to 0xXXffff7f for FAT16 and to
236 * 0xXXffff0fffffff07 for FAT32 upon boot, to know that the
237 * file system is dirty if it doesn't reboot cleanly.
238 * Check this special condition before errorring out.
240 if (buffer
[0] == boot
->Media
&& buffer
[1] == 0xff
242 && ((boot
->ClustMask
== CLUST16_MASK
&& buffer
[3] == 0x7f)
243 || (boot
->ClustMask
== CLUST32_MASK
244 && buffer
[3] == 0x0f && buffer
[4] == 0xff
245 && buffer
[5] == 0xff && buffer
[6] == 0xff
246 && buffer
[7] == 0x07)))
249 /* just some odd byte sequence in FAT */
251 switch (boot
->ClustMask
) {
253 pwarn("%s (%02x%02x%02x%02x%02x%02x%02x%02x)\n",
254 "FAT starts with odd byte sequence",
255 buffer
[0], buffer
[1], buffer
[2], buffer
[3],
256 buffer
[4], buffer
[5], buffer
[6], buffer
[7]);
259 pwarn("%s (%02x%02x%02x%02x)\n",
260 "FAT starts with odd byte sequence",
261 buffer
[0], buffer
[1], buffer
[2], buffer
[3]);
264 pwarn("%s (%02x%02x%02x)\n",
265 "FAT starts with odd byte sequence",
266 buffer
[0], buffer
[1], buffer
[2]);
271 if (ask(1, "Correct"))
275 switch (boot
->ClustMask
) {
286 for (cl
= CLUST_FIRST
; cl
< boot
->NumClusters
;) {
287 switch (boot
->ClustMask
) {
289 fat
[cl
].next
= p
[0] + (p
[1] << 8)
290 + (p
[2] << 16) + (p
[3] << 24);
291 fat
[cl
].next
&= boot
->ClustMask
;
292 ret
|= checkclnum(boot
, no
, cl
, &fat
[cl
].next
);
297 fat
[cl
].next
= p
[0] + (p
[1] << 8);
298 ret
|= checkclnum(boot
, no
, cl
, &fat
[cl
].next
);
303 fat
[cl
].next
= (p
[0] + (p
[1] << 8)) & 0x0fff;
304 ret
|= checkclnum(boot
, no
, cl
, &fat
[cl
].next
);
306 if (cl
>= boot
->NumClusters
)
308 fat
[cl
].next
= ((p
[1] >> 4) + (p
[2] << 4)) & 0x0fff;
309 ret
|= checkclnum(boot
, no
, cl
, &fat
[cl
].next
);
322 * Get type of reserved cluster
327 if (cl
== CLUST_FREE
)
337 clustdiffer(cl_t cl
, cl_t
*cp1
, cl_t
*cp2
, int fatnum
)
339 if (*cp1
== CLUST_FREE
|| *cp1
>= CLUST_RSRVD
) {
340 if (*cp2
== CLUST_FREE
|| *cp2
>= CLUST_RSRVD
) {
341 if ((*cp1
!= CLUST_FREE
&& *cp1
< CLUST_BAD
342 && *cp2
!= CLUST_FREE
&& *cp2
< CLUST_BAD
)
343 || (*cp1
> CLUST_BAD
&& *cp2
> CLUST_BAD
)) {
344 pwarn("Cluster %u is marked %s with different indicators\n",
345 cl
, rsrvdcltype(*cp1
));
352 pwarn("Cluster %u is marked %s in FAT 0, %s in FAT %d\n",
353 cl
, rsrvdcltype(*cp1
), rsrvdcltype(*cp2
), fatnum
);
354 if (ask(1, "Use FAT 0's entry")) {
358 if (ask(1, "Use FAT %d's entry", fatnum
)) {
364 pwarn("Cluster %u is marked %s in FAT 0, but continues with cluster %u in FAT %d\n",
365 cl
, rsrvdcltype(*cp1
), *cp2
, fatnum
);
366 if (ask(1, "Use continuation from FAT %d", fatnum
)) {
370 if (ask(1, "Use mark from FAT 0")) {
376 if (*cp2
== CLUST_FREE
|| *cp2
>= CLUST_RSRVD
) {
377 pwarn("Cluster %u continues with cluster %u in FAT 0, but is marked %s in FAT %d\n",
378 cl
, *cp1
, rsrvdcltype(*cp2
), fatnum
);
379 if (ask(1, "Use continuation from FAT 0")) {
383 if (ask(1, "Use mark from FAT %d", fatnum
)) {
389 pwarn("Cluster %u continues with cluster %u in FAT 0, but with cluster %u in FAT %d\n",
390 cl
, *cp1
, *cp2
, fatnum
);
391 if (ask(1, "Use continuation from FAT 0")) {
395 if (ask(1, "Use continuation from FAT %d", fatnum
)) {
403 * Compare two FAT copies in memory. Resolve any conflicts and merge them
404 * into the first one.
407 comparefat(struct bootblock
*boot
, struct fatEntry
*first
,
408 struct fatEntry
*second
, int fatnum
)
413 for (cl
= CLUST_FIRST
; cl
< boot
->NumClusters
; cl
++)
414 if (first
[cl
].next
!= second
[cl
].next
)
415 ret
|= clustdiffer(cl
, &first
[cl
].next
, &second
[cl
].next
, fatnum
);
420 clearchain(struct bootblock
*boot
, struct fatEntry
*fat
, cl_t head
)
424 for (p
= head
; p
>= CLUST_FIRST
&& p
< boot
->NumClusters
; p
= q
) {
425 if (fat
[p
].head
!= head
)
428 fat
[p
].next
= fat
[p
].head
= CLUST_FREE
;
434 tryclear(struct bootblock
*boot
, struct fatEntry
*fat
, cl_t head
, cl_t
*trunc
)
436 if (ask(1, "Clear chain starting at %u", head
)) {
437 clearchain(boot
, fat
, head
);
439 } else if (ask(1, "Truncate")) {
447 * Check a complete FAT in-memory for crosslinks
450 checkfat(struct bootblock
*boot
, struct fatEntry
*fat
)
452 cl_t head
, p
, h
, n
, wdk
;
458 * pass 1: figure out the cluster chains.
460 for (head
= CLUST_FIRST
; head
< boot
->NumClusters
; head
++) {
461 /* find next untravelled chain */
462 if (fat
[head
].head
!= 0 /* cluster already belongs to some chain */
463 || fat
[head
].next
== CLUST_FREE
464 || fat
[head
].next
== CLUST_BAD
)
465 continue; /* skip it. */
467 /* follow the chain and mark all clusters on the way */
468 for (len
= 0, p
= head
;
469 p
>= CLUST_FIRST
&& p
< boot
->NumClusters
;
471 /* we have to check the len, to avoid infinite loop */
472 if (len
> boot
->NumClusters
) {
473 printf("detect cluster chain loop: head %u for p %u\n", head
, p
);
481 /* the head record gets the length */
482 fat
[head
].length
= fat
[head
].next
== CLUST_FREE
? 0 : len
;
486 * pass 2: check for crosslinked chains (we couldn't do this in pass 1 because
487 * we didn't know the real start of the chain then - would have treated partial
488 * chains as interlinked with their main chain)
490 for (head
= CLUST_FIRST
; head
< boot
->NumClusters
; head
++) {
491 /* find next untravelled chain */
492 if (fat
[head
].head
!= head
)
495 /* follow the chain to its end (hopefully) */
496 /* also possible infinite loop, that's why I insert wdk counter */
497 for (p
= head
,wdk
=boot
->NumClusters
;
498 (n
= fat
[p
].next
) >= CLUST_FIRST
&& n
< boot
->NumClusters
&& wdk
;
500 if (fat
[n
].head
!= head
)
507 if (n
== CLUST_FREE
|| n
>= CLUST_RSRVD
) {
508 pwarn("Cluster chain starting at %u ends with cluster marked %s\n",
509 head
, rsrvdcltype(n
));
510 ret
|= tryclear(boot
, fat
, head
, &fat
[p
].next
);
513 if (n
< CLUST_FIRST
|| n
>= boot
->NumClusters
) {
514 pwarn("Cluster chain starting at %u ends with cluster out of range (%u)\n",
516 ret
|= tryclear(boot
, fat
, head
, &fat
[p
].next
);
519 pwarn("Cluster chains starting at %u and %u are linked at cluster %u\n",
520 head
, fat
[n
].head
, n
);
521 conf
= tryclear(boot
, fat
, head
, &fat
[p
].next
);
522 if (ask(1, "Clear chain starting at %u", h
= fat
[n
].head
)) {
523 if (conf
== FSERROR
) {
525 * Transfer the common chain to the one not cleared above.
528 p
>= CLUST_FIRST
&& p
< boot
->NumClusters
;
530 if (h
!= fat
[p
].head
) {
532 * Have to reexamine this chain.
540 clearchain(boot
, fat
, h
);
550 * Write out FATs encoding them from the internal format
553 writefat(int fs
, struct bootblock
*boot
, struct fatEntry
*fat
, int correct_fat
)
562 buffer
= malloc(fatsz
= boot
->FATsecs
* boot
->BytesPerSec
);
563 if (buffer
== NULL
) {
564 perror("No space for FAT");
567 memset(buffer
, 0, fatsz
);
571 *p
++ = (u_char
)boot
->Media
;
574 switch (boot
->ClustMask
) {
587 /* use same FAT signature as the old FAT has */
591 switch (boot
->ClustMask
) {
603 if (!_readfat(fs
, boot
, boot
->ValidFat
>= 0 ? boot
->ValidFat
:0,
609 memcpy(p
, old_fat
, count
);
614 for (cl
= CLUST_FIRST
; cl
< boot
->NumClusters
; cl
++) {
615 switch (boot
->ClustMask
) {
617 if (fat
[cl
].next
== CLUST_FREE
)
619 *p
++ = (u_char
)fat
[cl
].next
;
620 *p
++ = (u_char
)(fat
[cl
].next
>> 8);
621 *p
++ = (u_char
)(fat
[cl
].next
>> 16);
623 *p
++ |= (fat
[cl
].next
>> 24)&0x0f;
626 if (fat
[cl
].next
== CLUST_FREE
)
628 *p
++ = (u_char
)fat
[cl
].next
;
629 *p
++ = (u_char
)(fat
[cl
].next
>> 8);
632 if (fat
[cl
].next
== CLUST_FREE
)
634 if (cl
+ 1 < boot
->NumClusters
635 && fat
[cl
+ 1].next
== CLUST_FREE
)
637 *p
++ = (u_char
)fat
[cl
].next
;
638 *p
++ = (u_char
)((fat
[cl
].next
>> 8) & 0xf)
639 |(u_char
)(fat
[cl
+1].next
<< 4);
640 *p
++ = (u_char
)(fat
[++cl
].next
>> 4);
644 for (i
= 0; i
< boot
->FATs
; i
++) {
645 off
= boot
->ResSectors
+ i
* boot
->FATsecs
;
646 off
*= boot
->BytesPerSec
;
647 if (lseek(fs
, off
, SEEK_SET
) != off
648 || write(fs
, buffer
, fatsz
) != fatsz
) {
649 perror("Unable to write FAT");
650 ret
= FSFATAL
; /* Return immediately? XXX */
658 * Check a complete in-memory FAT for lost cluster chains
661 checklost(int dosfs
, struct bootblock
*boot
, struct fatEntry
*fat
)
667 for (head
= CLUST_FIRST
; head
< boot
->NumClusters
; head
++) {
668 /* find next untravelled chain */
669 if (fat
[head
].head
!= head
670 || fat
[head
].next
== CLUST_FREE
671 || (fat
[head
].next
>= CLUST_RSRVD
672 && fat
[head
].next
< CLUST_EOFS
)
673 || (fat
[head
].flags
& FAT_USED
))
676 pwarn("Lost cluster chain at cluster %u\n%d Cluster(s) lost\n",
677 head
, fat
[head
].length
);
678 mod
|= ret
= reconnect(dosfs
, boot
, fat
, head
);
680 /* If the reconnect failed, then just clear the chain */
681 pwarn("Error reconnecting chain - clearing\n");
683 clearchain(boot
, fat
, head
);
687 if (ret
== FSERROR
&& ask(1, "Clear")) {
688 clearchain(boot
, fat
, head
);
696 if (boot
->FSFree
!= boot
->NumFree
) {
697 pwarn("Free space in FSInfo block (%d) not correct (%d)\n",
698 boot
->FSFree
, boot
->NumFree
);
700 boot
->FSFree
= boot
->NumFree
;
705 if (boot
->FSNext
> boot
->NumClusters
) {
706 pwarn("FSNext block (%d) not correct NumClusters (%d)\n",
707 boot
->FSNext
, boot
->NumClusters
);
708 boot
->FSNext
=CLUST_FIRST
; // boot->FSNext can have -1 value.
711 if (boot
->NumFree
&& fat
[boot
->FSNext
].next
!= CLUST_FREE
) {
712 pwarn("Next free cluster in FSInfo block (%u) not free\n",
715 for (head
= CLUST_FIRST
; head
< boot
->NumClusters
; head
++)
716 if (fat
[head
].next
== CLUST_FREE
) {
723 mod
|= writefsinfo(dosfs
, boot
);