tizen 2.3 release
[framework/system/deviced.git] / src / fsck-msdos / 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  * 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 <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(1, "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         printf("Attempting to allocate %u KB for FAT\n",
167                 (boot->FATsecs * boot->BytesPerSec) / 1024);
168
169         *buffer = malloc(boot->FATsecs * boot->BytesPerSec);
170         if (*buffer == NULL) {
171                 perror("No space for FAT");
172                 return 0;
173         }
174
175         off = boot->ResSectors + no * boot->FATsecs;
176         off *= boot->BytesPerSec;
177
178         if (lseek(fs, off, SEEK_SET) != off) {
179                 perror("Unable to read FAT");
180                 goto err;
181         }
182
183         if (read(fs, *buffer, boot->FATsecs * boot->BytesPerSec)
184             != boot->FATsecs * boot->BytesPerSec) {
185                 perror("Unable to read FAT");
186                 goto err;
187         }
188
189         return 1;
190
191     err:
192         free(*buffer);
193         return 0;
194 }
195
196 /*
197  * Read a FAT and decode it into internal format
198  */
199 int
200 readfat(int fs, struct bootblock *boot, int no, struct fatEntry **fp)
201 {
202         struct fatEntry *fat;
203         u_char *buffer, *p;
204         cl_t cl;
205         int ret = FSOK;
206
207         boot->NumFree = boot->NumBad = 0;
208
209         if (!_readfat(fs, boot, no, &buffer))
210                 return FSFATAL;
211
212         fat = calloc(boot->NumClusters, sizeof(struct fatEntry));
213         if (fat == NULL) {
214                 perror("No space for FAT");
215                 free(buffer);
216                 return FSFATAL;
217         }
218
219         if (buffer[0] != boot->Media
220             || buffer[1] != 0xff || buffer[2] != 0xff
221             || (boot->ClustMask == CLUST16_MASK && buffer[3] != 0xff)
222             || (boot->ClustMask == CLUST32_MASK
223                 && ((buffer[3]&0x0f) != 0x0f
224                     || buffer[4] != 0xff || buffer[5] != 0xff
225                     || buffer[6] != 0xff || (buffer[7]&0x0f) != 0x0f))) {
226
227                 /* Windows 95 OSR2 (and possibly any later) changes
228                  * the FAT signature to 0xXXffff7f for FAT16 and to
229                  * 0xXXffff0fffffff07 for FAT32 upon boot, to know that the
230                  * file system is dirty if it doesn't reboot cleanly.
231                  * Check this special condition before errorring out.
232                  */
233                 if (buffer[0] == boot->Media && buffer[1] == 0xff
234                     && buffer[2] == 0xff
235                     && ((boot->ClustMask == CLUST16_MASK && buffer[3] == 0x7f)
236                         || (boot->ClustMask == CLUST32_MASK
237                             && buffer[3] == 0x0f && buffer[4] == 0xff
238                             && buffer[5] == 0xff && buffer[6] == 0xff
239                             && buffer[7] == 0x07)))
240                         ret |= FSDIRTY;
241                 else {
242                         /* just some odd byte sequence in FAT */
243
244                         switch (boot->ClustMask) {
245                         case CLUST32_MASK:
246                                 pwarn("%s (%02x%02x%02x%02x%02x%02x%02x%02x)\n",
247                                       "FAT starts with odd byte sequence",
248                                       buffer[0], buffer[1], buffer[2], buffer[3],
249                                       buffer[4], buffer[5], buffer[6], buffer[7]);
250                                 break;
251                         case CLUST16_MASK:
252                                 pwarn("%s (%02x%02x%02x%02x)\n",
253                                     "FAT starts with odd byte sequence",
254                                     buffer[0], buffer[1], buffer[2], buffer[3]);
255                                 break;
256                         default:
257                                 pwarn("%s (%02x%02x%02x)\n",
258                                     "FAT starts with odd byte sequence",
259                                     buffer[0], buffer[1], buffer[2]);
260                                 break;
261                         }
262
263
264                         if (ask(1, "Correct"))
265                                 ret |= FSFIXFAT;
266                 }
267         }
268         switch (boot->ClustMask) {
269         case CLUST32_MASK:
270                 p = buffer + 8;
271                 break;
272         case CLUST16_MASK:
273                 p = buffer + 4;
274                 break;
275         default:
276                 p = buffer + 3;
277                 break;
278         }
279         for (cl = CLUST_FIRST; cl < boot->NumClusters;) {
280                 switch (boot->ClustMask) {
281                 case CLUST32_MASK:
282                         fat[cl].next = p[0] + (p[1] << 8)
283                                        + (p[2] << 16) + (p[3] << 24);
284                         fat[cl].next &= boot->ClustMask;
285                         ret |= checkclnum(boot, no, cl, &fat[cl].next);
286                         cl++;
287                         p += 4;
288                         break;
289                 case CLUST16_MASK:
290                         fat[cl].next = p[0] + (p[1] << 8);
291                         ret |= checkclnum(boot, no, cl, &fat[cl].next);
292                         cl++;
293                         p += 2;
294                         break;
295                 default:
296                         fat[cl].next = (p[0] + (p[1] << 8)) & 0x0fff;
297                         ret |= checkclnum(boot, no, cl, &fat[cl].next);
298                         cl++;
299                         if (cl >= boot->NumClusters)
300                                 break;
301                         fat[cl].next = ((p[1] >> 4) + (p[2] << 4)) & 0x0fff;
302                         ret |= checkclnum(boot, no, cl, &fat[cl].next);
303                         cl++;
304                         p += 3;
305                         break;
306                 }
307         }
308
309         free(buffer);
310         *fp = fat;
311         return ret;
312 }
313
314 /*
315  * Get type of reserved cluster
316  */
317 char *
318 rsrvdcltype(cl_t cl)
319 {
320         if (cl == CLUST_FREE)
321                 return "free";
322         if (cl < CLUST_BAD)
323                 return "reserved";
324         if (cl > CLUST_BAD)
325                 return "as EOF";
326         return "bad";
327 }
328
329 static int
330 clustdiffer(cl_t cl, cl_t *cp1, cl_t *cp2, int fatnum)
331 {
332         if (*cp1 == CLUST_FREE || *cp1 >= CLUST_RSRVD) {
333                 if (*cp2 == CLUST_FREE || *cp2 >= CLUST_RSRVD) {
334                         if ((*cp1 != CLUST_FREE && *cp1 < CLUST_BAD
335                              && *cp2 != CLUST_FREE && *cp2 < CLUST_BAD)
336                             || (*cp1 > CLUST_BAD && *cp2 > CLUST_BAD)) {
337                                 pwarn("Cluster %u is marked %s with different indicators\n",
338                                       cl, rsrvdcltype(*cp1));
339                                 if (ask(1, "Fix")) {
340                                         *cp2 = *cp1;
341                                         return FSFATMOD;
342                                 }
343                                 return FSFATAL;
344                         }
345                         pwarn("Cluster %u is marked %s in FAT 0, %s in FAT %d\n",
346                               cl, rsrvdcltype(*cp1), rsrvdcltype(*cp2), fatnum);
347                         if (ask(1, "Use FAT 0's entry")) {
348                                 *cp2 = *cp1;
349                                 return FSFATMOD;
350                         }
351                         if (ask(1, "Use FAT %d's entry", fatnum)) {
352                                 *cp1 = *cp2;
353                                 return FSFATMOD;
354                         }
355                         return FSFATAL;
356                 }
357                 pwarn("Cluster %u is marked %s in FAT 0, but continues with cluster %u in FAT %d\n",
358                       cl, rsrvdcltype(*cp1), *cp2, fatnum);
359                 if (ask(1, "Use continuation from FAT %d", fatnum)) {
360                         *cp1 = *cp2;
361                         return FSFATMOD;
362                 }
363                 if (ask(1, "Use mark from FAT 0")) {
364                         *cp2 = *cp1;
365                         return FSFATMOD;
366                 }
367                 return FSFATAL;
368         }
369         if (*cp2 == CLUST_FREE || *cp2 >= CLUST_RSRVD) {
370                 pwarn("Cluster %u continues with cluster %u in FAT 0, but is marked %s in FAT %d\n",
371                       cl, *cp1, rsrvdcltype(*cp2), fatnum);
372                 if (ask(1, "Use continuation from FAT 0")) {
373                         *cp2 = *cp1;
374                         return FSFATMOD;
375                 }
376                 if (ask(1, "Use mark from FAT %d", fatnum)) {
377                         *cp1 = *cp2;
378                         return FSFATMOD;
379                 }
380                 return FSERROR;
381         }
382         pwarn("Cluster %u continues with cluster %u in FAT 0, but with cluster %u in FAT %d\n",
383               cl, *cp1, *cp2, fatnum);
384         if (ask(1, "Use continuation from FAT 0")) {
385                 *cp2 = *cp1;
386                 return FSFATMOD;
387         }
388         if (ask(1, "Use continuation from FAT %d", fatnum)) {
389                 *cp1 = *cp2;
390                 return FSFATMOD;
391         }
392         return FSERROR;
393 }
394
395 /*
396  * Compare two FAT copies in memory. Resolve any conflicts and merge them
397  * into the first one.
398  */
399 int
400 comparefat(struct bootblock *boot, struct fatEntry *first,
401     struct fatEntry *second, int fatnum)
402 {
403         cl_t cl;
404         int ret = FSOK;
405
406         for (cl = CLUST_FIRST; cl < boot->NumClusters; cl++)
407                 if (first[cl].next != second[cl].next)
408                         ret |= clustdiffer(cl, &first[cl].next, &second[cl].next, fatnum);
409         return ret;
410 }
411
412 void
413 clearchain(struct bootblock *boot, struct fatEntry *fat, cl_t head)
414 {
415         cl_t p, q;
416
417         for (p = head; p >= CLUST_FIRST && p < boot->NumClusters; p = q) {
418                 if (fat[p].head != head)
419                         break;
420                 q = fat[p].next;
421                 fat[p].next = fat[p].head = CLUST_FREE;
422                 fat[p].length = 0;
423         }
424 }
425
426 int
427 tryclear(struct bootblock *boot, struct fatEntry *fat, cl_t head, cl_t *trunc)
428 {
429         if (ask(1, "Clear chain starting at %u", head)) {
430                 clearchain(boot, fat, head);
431                 return FSFATMOD;
432         } else if (ask(1, "Truncate")) {
433                 *trunc = CLUST_EOF;
434                 return FSFATMOD;
435         } else
436                 return FSERROR;
437 }
438
439 /*
440  * Check a complete FAT in-memory for crosslinks
441  */
442 int
443 checkfat(struct bootblock *boot, struct fatEntry *fat)
444 {
445         cl_t head, p, h, n, wdk;
446         u_int len;
447         int ret = 0;
448         int conf;
449
450         /*
451          * pass 1: figure out the cluster chains.
452          */
453         for (head = CLUST_FIRST; head < boot->NumClusters; head++) {
454                 /* find next untravelled chain */
455                 if (fat[head].head != 0         /* cluster already belongs to some chain */
456                     || fat[head].next == CLUST_FREE
457                     || fat[head].next == CLUST_BAD)
458                         continue;               /* skip it. */
459
460                 /* follow the chain and mark all clusters on the way */
461                 for (len = 0, p = head;
462                          p >= CLUST_FIRST && p < boot->NumClusters;
463                          p = fat[p].next) {
464                                 /* we have to check the len, to avoid infinite loop */
465                                 if (len > boot->NumClusters) {
466                                         printf("detect cluster chain loop: head %u for p %u\n", head, p);
467                                         break;
468                         }
469
470                         fat[p].head = head;
471                         len++;
472                 }
473
474                 /* the head record gets the length */
475                 fat[head].length = fat[head].next == CLUST_FREE ? 0 : len;
476         }
477
478         /*
479          * pass 2: check for crosslinked chains (we couldn't do this in pass 1 because
480          * we didn't know the real start of the chain then - would have treated partial
481          * chains as interlinked with their main chain)
482          */
483         for (head = CLUST_FIRST; head < boot->NumClusters; head++) {
484                 /* find next untravelled chain */
485                 if (fat[head].head != head)
486                         continue;
487
488                 /* follow the chain to its end (hopefully) */
489                 /* also possible infinite loop, that's why I insert wdk counter */
490                 for (p = head,wdk=boot->NumClusters;
491                      (n = fat[p].next) >= CLUST_FIRST && n < boot->NumClusters && wdk;
492                                  p = n,wdk--) {
493                         if (fat[n].head != head)
494                                 break;
495                 }
496
497                 if (n >= CLUST_EOFS)
498                         continue;
499
500                 if (n == CLUST_FREE || n >= CLUST_RSRVD) {
501                         pwarn("Cluster chain starting at %u ends with cluster marked %s\n",
502                               head, rsrvdcltype(n));
503                         ret |= tryclear(boot, fat, head, &fat[p].next);
504                         continue;
505                 }
506                 if (n < CLUST_FIRST || n >= boot->NumClusters) {
507                         pwarn("Cluster chain starting at %u ends with cluster out of range (%u)\n",
508                               head, n);
509                         ret |= tryclear(boot, fat, head, &fat[p].next);
510                         continue;
511                 }
512                 pwarn("Cluster chains starting at %u and %u are linked at cluster %u\n",
513                       head, fat[n].head, n);
514                 conf = tryclear(boot, fat, head, &fat[p].next);
515                 if (ask(1, "Clear chain starting at %u", h = fat[n].head)) {
516                         if (conf == FSERROR) {
517                                 /*
518                                  * Transfer the common chain to the one not cleared above.
519                                  */
520                                 for (p = n;
521                                      p >= CLUST_FIRST && p < boot->NumClusters;
522                                      p = fat[p].next) {
523                                         if (h != fat[p].head) {
524                                                 /*
525                                                  * Have to reexamine this chain.
526                                                  */
527                                                 head--;
528                                                 break;
529                                         }
530                                         fat[p].head = head;
531                                 }
532                         }
533                         clearchain(boot, fat, h);
534                         conf |= FSFATMOD;
535                 }
536                 ret |= conf;
537         }
538
539         return ret;
540 }
541
542 /*
543  * Write out FATs encoding them from the internal format
544  */
545 int
546 writefat(int fs, struct bootblock *boot, struct fatEntry *fat, int correct_fat)
547 {
548         u_char *buffer, *p;
549         cl_t cl;
550         int i;
551         u_int32_t fatsz;
552         off_t off;
553         int ret = FSOK;
554
555         buffer = malloc(fatsz = boot->FATsecs * boot->BytesPerSec);
556         if (buffer == NULL) {
557                 perror("No space for FAT");
558                 return FSFATAL;
559         }
560         memset(buffer, 0, fatsz);
561         boot->NumFree = 0;
562         p = buffer;
563         if (correct_fat) {
564                 *p++ = (u_char)boot->Media;
565                 *p++ = 0xff;
566                 *p++ = 0xff;
567                 switch (boot->ClustMask) {
568                 case CLUST16_MASK:
569                         *p++ = 0xff;
570                         break;
571                 case CLUST32_MASK:
572                         *p++ = 0x0f;
573                         *p++ = 0xff;
574                         *p++ = 0xff;
575                         *p++ = 0xff;
576                         *p++ = 0x0f;
577                         break;
578                 }
579         } else {
580                 /* use same FAT signature as the old FAT has */
581                 int count;
582                 u_char *old_fat;
583
584                 switch (boot->ClustMask) {
585                 case CLUST32_MASK:
586                         count = 8;
587                         break;
588                 case CLUST16_MASK:
589                         count = 4;
590                         break;
591                 default:
592                         count = 3;
593                         break;
594                 }
595
596                 if (!_readfat(fs, boot, boot->ValidFat >= 0 ? boot->ValidFat :0,
597                                          &old_fat)) {
598                         free(buffer);
599                         return FSFATAL;
600                 }
601
602                 memcpy(p, old_fat, count);
603                 free(old_fat);
604                 p += count;
605         }
606
607         for (cl = CLUST_FIRST; cl < boot->NumClusters; cl++) {
608                 switch (boot->ClustMask) {
609                 case CLUST32_MASK:
610                         if (fat[cl].next == CLUST_FREE)
611                                 boot->NumFree++;
612                         *p++ = (u_char)fat[cl].next;
613                         *p++ = (u_char)(fat[cl].next >> 8);
614                         *p++ = (u_char)(fat[cl].next >> 16);
615                         *p &= 0xf0;
616                         *p++ |= (fat[cl].next >> 24)&0x0f;
617                         break;
618                 case CLUST16_MASK:
619                         if (fat[cl].next == CLUST_FREE)
620                                 boot->NumFree++;
621                         *p++ = (u_char)fat[cl].next;
622                         *p++ = (u_char)(fat[cl].next >> 8);
623                         break;
624                 default:
625                         if (fat[cl].next == CLUST_FREE)
626                                 boot->NumFree++;
627                         if (cl + 1 < boot->NumClusters
628                             && fat[cl + 1].next == CLUST_FREE)
629                                 boot->NumFree++;
630                         *p++ = (u_char)fat[cl].next;
631                         *p++ = (u_char)((fat[cl].next >> 8) & 0xf)
632                                |(u_char)(fat[cl+1].next << 4);
633                         *p++ = (u_char)(fat[++cl].next >> 4);
634                         break;
635                 }
636         }
637         for (i = 0; i < boot->FATs; i++) {
638                 off = boot->ResSectors + i * boot->FATsecs;
639                 off *= boot->BytesPerSec;
640                 if (lseek(fs, off, SEEK_SET) != off
641                     || write(fs, buffer, fatsz) != fatsz) {
642                         perror("Unable to write FAT");
643                         ret = FSFATAL; /* Return immediately?           XXX */
644                 }
645         }
646         free(buffer);
647         return ret;
648 }
649
650 /*
651  * Check a complete in-memory FAT for lost cluster chains
652  */
653 int
654 checklost(int dosfs, struct bootblock *boot, struct fatEntry *fat)
655 {
656         cl_t head;
657         int mod = FSOK;
658         int ret;
659
660         for (head = CLUST_FIRST; head < boot->NumClusters; head++) {
661                 /* find next untravelled chain */
662                 if (fat[head].head != head
663                     || fat[head].next == CLUST_FREE
664                     || (fat[head].next >= CLUST_RSRVD
665                         && fat[head].next < CLUST_EOFS)
666                     || (fat[head].flags & FAT_USED))
667                         continue;
668
669                 pwarn("Lost cluster chain at cluster %u\n%d Cluster(s) lost\n",
670                       head, fat[head].length);
671                 mod |= ret = reconnect(dosfs, boot, fat, head);
672                 if (mod & FSFATAL) {
673                         /* If the reconnect failed, then just clear the chain */
674                         pwarn("Error reconnecting chain - clearing\n");
675                         mod &= ~FSFATAL;
676                         clearchain(boot, fat, head);
677                         mod |= FSFATMOD;
678                         continue;
679                 }
680                 if (ret == FSERROR && ask(1, "Clear")) {
681                         clearchain(boot, fat, head);
682                         mod |= FSFATMOD;
683                 }
684         }
685         finishlf();
686
687         if (boot->FSInfo) {
688                 ret = 0;
689                 if (boot->FSFree != boot->NumFree) {
690                         pwarn("Free space in FSInfo block (%d) not correct (%d)\n",
691                               boot->FSFree, boot->NumFree);
692                         if (ask(1, "Fix")) {
693                                 boot->FSFree = boot->NumFree;
694                                 ret = 1;
695                         }
696                 }
697
698                 if (boot->NumFree) {
699                         if ((boot->FSNext >= boot->NumClusters) || (fat[boot->FSNext].next != CLUST_FREE)) {
700                                 pwarn("Next free cluster in FSInfo block (%u) not free\n",
701                                       boot->FSNext);
702                                 if (ask(1, "Fix"))
703                                         for (head = CLUST_FIRST; head < boot->NumClusters; head++)
704                                                 if (fat[head].next == CLUST_FREE) {
705                                                         boot->FSNext = head;
706                                                         ret = 1;
707                                                         break;
708                                                 }
709                         }
710         }
711
712                 if (boot->FSNext > boot->NumClusters  ) {
713                         pwarn("FSNext block (%d) not correct NumClusters (%d)\n",
714                                         boot->FSNext, boot->NumClusters);
715                         boot->FSNext=CLUST_FIRST; // boot->FSNext can have -1 value.
716             }
717
718                 if (boot->NumFree && fat[boot->FSNext].next != CLUST_FREE) {
719                         pwarn("Next free cluster in FSInfo block (%u) not free\n",
720                                         boot->FSNext);
721                         if (ask(1, "Fix"))
722                                 for (head = CLUST_FIRST; head < boot->NumClusters; head++)
723                                         if (fat[head].next == CLUST_FREE) {
724                                                 boot->FSNext = head;
725                                                 ret = 1;
726                                                 break;
727                                         }
728             }
729
730                 if (ret)
731                         mod |= writefsinfo(dosfs, boot);
732         }
733
734         return mod;
735 }