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