]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sbin/fsck_msdosfs/fat.c
MFC r203871:
[FreeBSD/stable/8.git] / sbin / fsck_msdosfs / fat.c
1 /*
2  * Copyright (C) 1995, 1996, 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  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26
27 #include <sys/cdefs.h>
28 #ifndef lint
29 __RCSID("$NetBSD: fat.c,v 1.12 2000/10/10 20:24:52 is Exp $");
30 static const char rcsid[] =
31   "$FreeBSD$";
32 #endif /* not lint */
33
34 #include <stdlib.h>
35 #include <string.h>
36 #include <ctype.h>
37 #include <stdio.h>
38 #include <unistd.h>
39
40 #include "ext.h"
41 #include "fsutil.h"
42
43 static int checkclnum(struct bootblock *, int, cl_t, cl_t *);
44 static int clustdiffer(cl_t, cl_t *, cl_t *, int);
45 static int tryclear(struct bootblock *, struct fatEntry *, cl_t, cl_t *);
46 static int _readfat(int, struct bootblock *, int, u_char **);
47
48 /*-
49  * The first 2 FAT entries contain pseudo-cluster numbers with the following
50  * layout:
51  *
52  * 31...... ........ ........ .......0
53  * rrrr1111 11111111 11111111 mmmmmmmm         FAT32 entry 0
54  * rrrrsh11 11111111 11111111 11111xxx         FAT32 entry 1
55  * 
56  *                   11111111 mmmmmmmm         FAT16 entry 0
57  *                   sh111111 11111xxx         FAT16 entry 1
58  * 
59  * r = reserved
60  * m = BPB media ID byte
61  * s = clean flag (1 = dismounted; 0 = still mounted)
62  * h = hard error flag (1 = ok; 0 = I/O error)
63  * x = any value ok
64  */
65
66 int
67 checkdirty(int fs, struct bootblock *boot)
68 {
69         off_t off;
70         u_char *buffer;
71         int ret = 0;
72
73         if (boot->ClustMask != CLUST16_MASK && boot->ClustMask != CLUST32_MASK)
74                 return 0;
75
76         off = boot->ResSectors;
77         off *= boot->BytesPerSec;
78
79         buffer = malloc(boot->BytesPerSec);
80         if (buffer == NULL) {
81                 perror("No space for FAT");
82                 return 1;
83         }
84
85         if (lseek(fs, off, SEEK_SET) != off) {
86                 perror("Unable to read FAT");
87                 goto err;
88         }
89
90         if (read(fs, buffer, boot->BytesPerSec) != boot->BytesPerSec) {
91                 perror("Unable to read FAT");
92                 goto err;
93         }
94
95         /*
96          * If we don't understand the FAT, then the file system must be
97          * assumed to be unclean.
98          */
99         if (buffer[0] != boot->Media || buffer[1] != 0xff)
100                 goto err;
101         if (boot->ClustMask == CLUST16_MASK) {
102                 if ((buffer[2] & 0xf8) != 0xf8 || (buffer[3] & 0x3f) != 0x3f)
103                         goto err;
104         } else {
105                 if (buffer[2] != 0xff || (buffer[3] & 0x0f) != 0x0f
106                     || (buffer[4] & 0xf8) != 0xf8 || buffer[5] != 0xff
107                     || buffer[6] != 0xff || (buffer[7] & 0x03) != 0x03)
108                         goto err;
109         }
110
111         /*
112          * Now check the actual clean flag (and the no-error flag).
113          */
114         if (boot->ClustMask == CLUST16_MASK) {
115                 if ((buffer[3] & 0xc0) == 0xc0)
116                         ret = 1;
117         } else {
118                 if ((buffer[7] & 0x0c) == 0x0c)
119                         ret = 1;
120         }
121
122 err:
123         free(buffer);
124         return ret;
125 }
126
127 /*
128  * Check a cluster number for valid value
129  */
130 static int
131 checkclnum(struct bootblock *boot, int fat, cl_t cl, cl_t *next)
132 {
133         if (*next >= (CLUST_RSRVD&boot->ClustMask))
134                 *next |= ~boot->ClustMask;
135         if (*next == CLUST_FREE) {
136                 boot->NumFree++;
137                 return FSOK;
138         }
139         if (*next == CLUST_BAD) {
140                 boot->NumBad++;
141                 return FSOK;
142         }
143         if (*next < CLUST_FIRST
144             || (*next >= boot->NumClusters && *next < CLUST_EOFS)) {
145                 pwarn("Cluster %u in FAT %d continues with %s cluster number %u\n",
146                       cl, fat,
147                       *next < CLUST_RSRVD ? "out of range" : "reserved",
148                       *next&boot->ClustMask);
149                 if (ask(0, "Truncate")) {
150                         *next = CLUST_EOF;
151                         return FSFATMOD;
152                 }
153                 return FSERROR;
154         }
155         return FSOK;
156 }
157
158 /*
159  * Read a FAT from disk. Returns 1 if successful, 0 otherwise.
160  */
161 static int
162 _readfat(int fs, struct bootblock *boot, int no, u_char **buffer)
163 {
164         off_t off;
165
166         *buffer = malloc(boot->FATsecs * boot->BytesPerSec);
167         if (*buffer == NULL) {
168                 perror("No space for FAT");
169                 return 0;
170         }
171
172         off = boot->ResSectors + no * boot->FATsecs;
173         off *= boot->BytesPerSec;
174
175         if (lseek(fs, off, SEEK_SET) != off) {
176                 perror("Unable to read FAT");
177                 goto err;
178         }
179
180         if (read(fs, *buffer, boot->FATsecs * boot->BytesPerSec)
181             != boot->FATsecs * boot->BytesPerSec) {
182                 perror("Unable to read FAT");
183                 goto err;
184         }
185
186         return 1;
187
188     err:
189         free(*buffer);
190         return 0;
191 }
192
193 /*
194  * Read a FAT and decode it into internal format
195  */
196 int
197 readfat(int fs, struct bootblock *boot, int no, struct fatEntry **fp)
198 {
199         struct fatEntry *fat;
200         u_char *buffer, *p;
201         cl_t cl;
202         int ret = FSOK;
203
204         boot->NumFree = boot->NumBad = 0;
205
206         if (!_readfat(fs, boot, no, &buffer))
207                 return FSFATAL;
208                 
209         fat = calloc(boot->NumClusters, sizeof(struct fatEntry));
210         if (fat == NULL) {
211                 perror("No space for FAT");
212                 free(buffer);
213                 return FSFATAL;
214         }
215
216         if (buffer[0] != boot->Media
217             || buffer[1] != 0xff || buffer[2] != 0xff
218             || (boot->ClustMask == CLUST16_MASK && buffer[3] != 0xff)
219             || (boot->ClustMask == CLUST32_MASK
220                 && ((buffer[3]&0x0f) != 0x0f
221                     || buffer[4] != 0xff || buffer[5] != 0xff
222                     || buffer[6] != 0xff || (buffer[7]&0x0f) != 0x0f))) {
223
224                 /* Windows 95 OSR2 (and possibly any later) changes
225                  * the FAT signature to 0xXXffff7f for FAT16 and to
226                  * 0xXXffff0fffffff07 for FAT32 upon boot, to know that the
227                  * file system is dirty if it doesn't reboot cleanly.
228                  * Check this special condition before errorring out.
229                  */
230                 if (buffer[0] == boot->Media && buffer[1] == 0xff
231                     && buffer[2] == 0xff
232                     && ((boot->ClustMask == CLUST16_MASK && buffer[3] == 0x7f)
233                         || (boot->ClustMask == CLUST32_MASK
234                             && buffer[3] == 0x0f && buffer[4] == 0xff
235                             && buffer[5] == 0xff && buffer[6] == 0xff
236                             && buffer[7] == 0x07)))
237                         ret |= FSDIRTY;
238                 else {
239                         /* just some odd byte sequence in FAT */
240                                 
241                         switch (boot->ClustMask) {
242                         case CLUST32_MASK:
243                                 pwarn("%s (%02x%02x%02x%02x%02x%02x%02x%02x)\n",
244                                       "FAT starts with odd byte sequence",
245                                       buffer[0], buffer[1], buffer[2], buffer[3],
246                                       buffer[4], buffer[5], buffer[6], buffer[7]);
247                                 break;
248                         case CLUST16_MASK:
249                                 pwarn("%s (%02x%02x%02x%02x)\n",
250                                     "FAT starts with odd byte sequence",
251                                     buffer[0], buffer[1], buffer[2], buffer[3]);
252                                 break;
253                         default:
254                                 pwarn("%s (%02x%02x%02x)\n",
255                                     "FAT starts with odd byte sequence",
256                                     buffer[0], buffer[1], buffer[2]);
257                                 break;
258                         }
259
260         
261                         if (ask(1, "Correct"))
262                                 ret |= FSFIXFAT;
263                 }
264         }
265         switch (boot->ClustMask) {
266         case CLUST32_MASK:
267                 p = buffer + 8;
268                 break;
269         case CLUST16_MASK:
270                 p = buffer + 4;
271                 break;
272         default:
273                 p = buffer + 3;
274                 break;
275         }
276         for (cl = CLUST_FIRST; cl < boot->NumClusters;) {
277                 switch (boot->ClustMask) {
278                 case CLUST32_MASK:
279                         fat[cl].next = p[0] + (p[1] << 8)
280                                        + (p[2] << 16) + (p[3] << 24);
281                         fat[cl].next &= boot->ClustMask;
282                         ret |= checkclnum(boot, no, cl, &fat[cl].next);
283                         cl++;
284                         p += 4;
285                         break;
286                 case CLUST16_MASK:
287                         fat[cl].next = p[0] + (p[1] << 8);
288                         ret |= checkclnum(boot, no, cl, &fat[cl].next);
289                         cl++;
290                         p += 2;
291                         break;
292                 default:
293                         fat[cl].next = (p[0] + (p[1] << 8)) & 0x0fff;
294                         ret |= checkclnum(boot, no, cl, &fat[cl].next);
295                         cl++;
296                         if (cl >= boot->NumClusters)
297                                 break;
298                         fat[cl].next = ((p[1] >> 4) + (p[2] << 4)) & 0x0fff;
299                         ret |= checkclnum(boot, no, cl, &fat[cl].next);
300                         cl++;
301                         p += 3;
302                         break;
303                 }
304         }
305
306         free(buffer);
307         *fp = fat;
308         return ret;
309 }
310
311 /*
312  * Get type of reserved cluster
313  */
314 char *
315 rsrvdcltype(cl_t cl)
316 {
317         if (cl == CLUST_FREE)
318                 return "free";
319         if (cl < CLUST_BAD)
320                 return "reserved";
321         if (cl > CLUST_BAD)
322                 return "as EOF";
323         return "bad";
324 }
325
326 static int
327 clustdiffer(cl_t cl, cl_t *cp1, cl_t *cp2, int fatnum)
328 {
329         if (*cp1 == CLUST_FREE || *cp1 >= CLUST_RSRVD) {
330                 if (*cp2 == CLUST_FREE || *cp2 >= CLUST_RSRVD) {
331                         if ((*cp1 != CLUST_FREE && *cp1 < CLUST_BAD
332                              && *cp2 != CLUST_FREE && *cp2 < CLUST_BAD)
333                             || (*cp1 > CLUST_BAD && *cp2 > CLUST_BAD)) {
334                                 pwarn("Cluster %u is marked %s with different indicators\n",
335                                       cl, rsrvdcltype(*cp1));
336                                 if (ask(1, "Fix")) {
337                                         *cp2 = *cp1;
338                                         return FSFATMOD;
339                                 }
340                                 return FSFATAL;
341                         }
342                         pwarn("Cluster %u is marked %s in FAT 0, %s in FAT %d\n",
343                               cl, rsrvdcltype(*cp1), rsrvdcltype(*cp2), fatnum);
344                         if (ask(0, "Use FAT 0's entry")) {
345                                 *cp2 = *cp1;
346                                 return FSFATMOD;
347                         }
348                         if (ask(0, "Use FAT %d's entry", fatnum)) {
349                                 *cp1 = *cp2;
350                                 return FSFATMOD;
351                         }
352                         return FSFATAL;
353                 }
354                 pwarn("Cluster %u is marked %s in FAT 0, but continues with cluster %u in FAT %d\n",
355                       cl, rsrvdcltype(*cp1), *cp2, fatnum);
356                 if (ask(0, "Use continuation from FAT %d", fatnum)) {
357                         *cp1 = *cp2;
358                         return FSFATMOD;
359                 }
360                 if (ask(0, "Use mark from FAT 0")) {
361                         *cp2 = *cp1;
362                         return FSFATMOD;
363                 }
364                 return FSFATAL;
365         }
366         if (*cp2 == CLUST_FREE || *cp2 >= CLUST_RSRVD) {
367                 pwarn("Cluster %u continues with cluster %u in FAT 0, but is marked %s in FAT %d\n",
368                       cl, *cp1, rsrvdcltype(*cp2), fatnum);
369                 if (ask(0, "Use continuation from FAT 0")) {
370                         *cp2 = *cp1;
371                         return FSFATMOD;
372                 }
373                 if (ask(0, "Use mark from FAT %d", fatnum)) {
374                         *cp1 = *cp2;
375                         return FSFATMOD;
376                 }
377                 return FSERROR;
378         }
379         pwarn("Cluster %u continues with cluster %u in FAT 0, but with cluster %u in FAT %d\n",
380               cl, *cp1, *cp2, fatnum);
381         if (ask(0, "Use continuation from FAT 0")) {
382                 *cp2 = *cp1;
383                 return FSFATMOD;
384         }
385         if (ask(0, "Use continuation from FAT %d", fatnum)) {
386                 *cp1 = *cp2;
387                 return FSFATMOD;
388         }
389         return FSERROR;
390 }
391
392 /*
393  * Compare two FAT copies in memory. Resolve any conflicts and merge them
394  * into the first one.
395  */
396 int
397 comparefat(struct bootblock *boot, struct fatEntry *first, 
398     struct fatEntry *second, int fatnum)
399 {
400         cl_t cl;
401         int ret = FSOK;
402
403         for (cl = CLUST_FIRST; cl < boot->NumClusters; cl++)
404                 if (first[cl].next != second[cl].next)
405                         ret |= clustdiffer(cl, &first[cl].next, &second[cl].next, fatnum);
406         return ret;
407 }
408
409 void
410 clearchain(struct bootblock *boot, struct fatEntry *fat, cl_t head)
411 {
412         cl_t p, q;
413
414         for (p = head; p >= CLUST_FIRST && p < boot->NumClusters; p = q) {
415                 if (fat[p].head != head)
416                         break;
417                 q = fat[p].next;
418                 fat[p].next = fat[p].head = CLUST_FREE;
419                 fat[p].length = 0;
420         }
421 }
422
423 int
424 tryclear(struct bootblock *boot, struct fatEntry *fat, cl_t head, cl_t *trunc)
425 {
426         if (ask(0, "Clear chain starting at %u", head)) {
427                 clearchain(boot, fat, head);
428                 return FSFATMOD;
429         } else if (ask(0, "Truncate")) {
430                 *trunc = CLUST_EOF;
431                 return FSFATMOD;
432         } else
433                 return FSERROR;
434 }
435
436 /*
437  * Check a complete FAT in-memory for crosslinks
438  */
439 int
440 checkfat(struct bootblock *boot, struct fatEntry *fat)
441 {
442         cl_t head, p, h, n;
443         u_int len;
444         int ret = 0;
445         int conf;
446
447         /*
448          * pass 1: figure out the cluster chains.
449          */
450         for (head = CLUST_FIRST; head < boot->NumClusters; head++) {
451                 /* find next untravelled chain */
452                 if (fat[head].head != 0         /* cluster already belongs to some chain */
453                     || fat[head].next == CLUST_FREE
454                     || fat[head].next == CLUST_BAD)
455                         continue;               /* skip it. */
456
457                 /* follow the chain and mark all clusters on the way */
458                 for (len = 0, p = head;
459                      p >= CLUST_FIRST && p < boot->NumClusters;
460                      p = fat[p].next) {
461                         fat[p].head = head;
462                         len++;
463                 }
464
465                 /* the head record gets the length */
466                 fat[head].length = fat[head].next == CLUST_FREE ? 0 : len;
467         }
468
469         /*
470          * pass 2: check for crosslinked chains (we couldn't do this in pass 1 because
471          * we didn't know the real start of the chain then - would have treated partial
472          * chains as interlinked with their main chain)
473          */
474         for (head = CLUST_FIRST; head < boot->NumClusters; head++) {
475                 /* find next untravelled chain */
476                 if (fat[head].head != head)
477                         continue;
478
479                 /* follow the chain to its end (hopefully) */
480                 for (p = head;
481                      (n = fat[p].next) >= CLUST_FIRST && n < boot->NumClusters;
482                      p = n)
483                         if (fat[n].head != head)
484                                 break;
485                 if (n >= CLUST_EOFS)
486                         continue;
487
488                 if (n == CLUST_FREE || n >= CLUST_RSRVD) {
489                         pwarn("Cluster chain starting at %u ends with cluster marked %s\n",
490                               head, rsrvdcltype(n));
491                         ret |= tryclear(boot, fat, head, &fat[p].next);
492                         continue;
493                 }
494                 if (n < CLUST_FIRST || n >= boot->NumClusters) {
495                         pwarn("Cluster chain starting at %u ends with cluster out of range (%u)\n",
496                               head, n);
497                         ret |= tryclear(boot, fat, head, &fat[p].next);
498                         continue;
499                 }
500                 pwarn("Cluster chains starting at %u and %u are linked at cluster %u\n",
501                       head, fat[n].head, n);
502                 conf = tryclear(boot, fat, head, &fat[p].next);
503                 if (ask(0, "Clear chain starting at %u", h = fat[n].head)) {
504                         if (conf == FSERROR) {
505                                 /*
506                                  * Transfer the common chain to the one not cleared above.
507                                  */
508                                 for (p = n;
509                                      p >= CLUST_FIRST && p < boot->NumClusters;
510                                      p = fat[p].next) {
511                                         if (h != fat[p].head) {
512                                                 /*
513                                                  * Have to reexamine this chain.
514                                                  */
515                                                 head--;
516                                                 break;
517                                         }
518                                         fat[p].head = head;
519                                 }
520                         }
521                         clearchain(boot, fat, h);
522                         conf |= FSFATMOD;
523                 }
524                 ret |= conf;
525         }
526
527         return ret;
528 }
529
530 /*
531  * Write out FATs encoding them from the internal format
532  */
533 int
534 writefat(int fs, struct bootblock *boot, struct fatEntry *fat, int correct_fat)
535 {
536         u_char *buffer, *p;
537         cl_t cl;
538         int i;
539         u_int32_t fatsz;
540         off_t off;
541         int ret = FSOK;
542
543         buffer = malloc(fatsz = boot->FATsecs * boot->BytesPerSec);
544         if (buffer == NULL) {
545                 perror("No space for FAT");
546                 return FSFATAL;
547         }
548         memset(buffer, 0, fatsz);
549         boot->NumFree = 0;
550         p = buffer;
551         if (correct_fat) {
552                 *p++ = (u_char)boot->Media;
553                 *p++ = 0xff;
554                 *p++ = 0xff;
555                 switch (boot->ClustMask) {
556                 case CLUST16_MASK:
557                         *p++ = 0xff;
558                         break;
559                 case CLUST32_MASK:
560                         *p++ = 0x0f;
561                         *p++ = 0xff;
562                         *p++ = 0xff;
563                         *p++ = 0xff;
564                         *p++ = 0x0f;
565                         break;
566                 }
567         } else {
568                 /* use same FAT signature as the old FAT has */
569                 int count;
570                 u_char *old_fat;
571
572                 switch (boot->ClustMask) {
573                 case CLUST32_MASK:
574                         count = 8;
575                         break;
576                 case CLUST16_MASK:
577                         count = 4;
578                         break;
579                 default:
580                         count = 3;
581                         break;
582                 }
583
584                 if (!_readfat(fs, boot, boot->ValidFat >= 0 ? boot->ValidFat :0,
585                                          &old_fat)) {
586                         free(buffer);
587                         return FSFATAL;
588                 }
589
590                 memcpy(p, old_fat, count);
591                 free(old_fat);
592                 p += count;
593         }
594                         
595         for (cl = CLUST_FIRST; cl < boot->NumClusters; cl++) {
596                 switch (boot->ClustMask) {
597                 case CLUST32_MASK:
598                         if (fat[cl].next == CLUST_FREE)
599                                 boot->NumFree++;
600                         *p++ = (u_char)fat[cl].next;
601                         *p++ = (u_char)(fat[cl].next >> 8);
602                         *p++ = (u_char)(fat[cl].next >> 16);
603                         *p &= 0xf0;
604                         *p++ |= (fat[cl].next >> 24)&0x0f;
605                         break;
606                 case CLUST16_MASK:
607                         if (fat[cl].next == CLUST_FREE)
608                                 boot->NumFree++;
609                         *p++ = (u_char)fat[cl].next;
610                         *p++ = (u_char)(fat[cl].next >> 8);
611                         break;
612                 default:
613                         if (fat[cl].next == CLUST_FREE)
614                                 boot->NumFree++;
615                         if (cl + 1 < boot->NumClusters
616                             && fat[cl + 1].next == CLUST_FREE)
617                                 boot->NumFree++;
618                         *p++ = (u_char)fat[cl].next;
619                         *p++ = (u_char)((fat[cl].next >> 8) & 0xf)
620                                |(u_char)(fat[cl+1].next << 4);
621                         *p++ = (u_char)(fat[++cl].next >> 4);
622                         break;
623                 }
624         }
625         for (i = 0; i < boot->FATs; i++) {
626                 off = boot->ResSectors + i * boot->FATsecs;
627                 off *= boot->BytesPerSec;
628                 if (lseek(fs, off, SEEK_SET) != off
629                     || write(fs, buffer, fatsz) != fatsz) {
630                         perror("Unable to write FAT");
631                         ret = FSFATAL; /* Return immediately?           XXX */
632                 }
633         }
634         free(buffer);
635         return ret;
636 }
637
638 /*
639  * Check a complete in-memory FAT for lost cluster chains
640  */
641 int
642 checklost(int dosfs, struct bootblock *boot, struct fatEntry *fat)
643 {
644         cl_t head;
645         int mod = FSOK;
646         int ret;
647         
648         for (head = CLUST_FIRST; head < boot->NumClusters; head++) {
649                 /* find next untravelled chain */
650                 if (fat[head].head != head
651                     || fat[head].next == CLUST_FREE
652                     || (fat[head].next >= CLUST_RSRVD
653                         && fat[head].next < CLUST_EOFS)
654                     || (fat[head].flags & FAT_USED))
655                         continue;
656
657                 pwarn("Lost cluster chain at cluster %u\n%d Cluster(s) lost\n",
658                       head, fat[head].length);
659                 mod |= ret = reconnect(dosfs, boot, fat, head);
660                 if (mod & FSFATAL)
661                         break;
662                 if (ret == FSERROR && ask(0, "Clear")) {
663                         clearchain(boot, fat, head);
664                         mod |= FSFATMOD;
665                 }
666         }
667         finishlf();
668
669         if (boot->FSInfo) {
670                 ret = 0;
671                 if (boot->FSFree != boot->NumFree) {
672                         pwarn("Free space in FSInfo block (%d) not correct (%d)\n",
673                               boot->FSFree, boot->NumFree);
674                         if (ask(1, "Fix")) {
675                                 boot->FSFree = boot->NumFree;
676                                 ret = 1;
677                         }
678                 }
679                 if (boot->NumFree && fat[boot->FSNext].next != CLUST_FREE) {
680                         pwarn("Next free cluster in FSInfo block (%u) not free\n",
681                               boot->FSNext);
682                         if (ask(1, "Fix"))
683                                 for (head = CLUST_FIRST; head < boot->NumClusters; head++)
684                                         if (fat[head].next == CLUST_FREE) {
685                                                 boot->FSNext = head;
686                                                 ret = 1;
687                                                 break;
688                                         }
689                 }
690                 if (ret)
691                         mod |= writefsinfo(dosfs, boot);
692         }
693
694         return mod;
695 }