Home | History | Annotate | Line # | Download | only in fsck_msdos
fat.c revision 1.8
      1  1.8        ws /*	$NetBSD: fat.c,v 1.8 1997/10/17 11:19:53 ws 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.8        ws __RCSID("$NetBSD: fat.c,v 1.8 1997/10/17 11:19:53 ws 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.1        ws 
     53  1.1        ws /*
     54  1.1        ws  * Check a cluster number for valid value
     55  1.1        ws  */
     56  1.1        ws static int
     57  1.1        ws checkclnum(boot, fat, cl, next)
     58  1.1        ws 	struct bootblock *boot;
     59  1.1        ws 	int fat;
     60  1.1        ws 	cl_t cl;
     61  1.1        ws 	cl_t *next;
     62  1.1        ws {
     63  1.8        ws 	if (*next >= (CLUST_RSRVD&boot->ClustMask))
     64  1.8        ws 		*next |= ~boot->ClustMask;
     65  1.1        ws 	if (*next == CLUST_FREE) {
     66  1.1        ws 		boot->NumFree++;
     67  1.1        ws 		return FSOK;
     68  1.1        ws 	}
     69  1.5        ws 	if (*next == CLUST_BAD) {
     70  1.5        ws 		boot->NumBad++;
     71  1.5        ws 		return FSOK;
     72  1.5        ws 	}
     73  1.1        ws 	if (*next < CLUST_FIRST
     74  1.1        ws 	    || (*next >= boot->NumClusters && *next < CLUST_EOFS)) {
     75  1.8        ws 		pwarn("Cluster %u in FAT %d continues with %s cluster number %u\n",
     76  1.1        ws 		      cl, fat,
     77  1.1        ws 		      *next < CLUST_RSRVD ? "out of range" : "reserved",
     78  1.8        ws 		      *next&boot->ClustMask);
     79  1.1        ws 		if (ask(0, "Truncate")) {
     80  1.1        ws 			*next = CLUST_EOF;
     81  1.1        ws 			return FSFATMOD;
     82  1.1        ws 		}
     83  1.1        ws 		return FSERROR;
     84  1.1        ws 	}
     85  1.1        ws 	return FSOK;
     86  1.1        ws }
     87  1.1        ws 
     88  1.1        ws /*
     89  1.1        ws  * Read a FAT and decode it into internal format
     90  1.1        ws  */
     91  1.1        ws int
     92  1.1        ws readfat(fs, boot, no, fp)
     93  1.1        ws 	int fs;
     94  1.1        ws 	struct bootblock *boot;
     95  1.1        ws 	int no;
     96  1.1        ws 	struct fatEntry **fp;
     97  1.1        ws {
     98  1.1        ws 	struct fatEntry *fat;
     99  1.1        ws 	u_char *buffer, *p;
    100  1.1        ws 	cl_t cl;
    101  1.1        ws 	off_t off;
    102  1.1        ws 	int ret = FSOK;
    103  1.1        ws 
    104  1.5        ws 	boot->NumFree = boot->NumBad = 0;
    105  1.1        ws 	fat = malloc(sizeof(struct fatEntry) * boot->NumClusters);
    106  1.1        ws 	buffer = malloc(boot->FATsecs * boot->BytesPerSec);
    107  1.1        ws 	if (fat == NULL || buffer == NULL) {
    108  1.1        ws 		perror("No space for FAT");
    109  1.1        ws 		if (fat)
    110  1.1        ws 			free(fat);
    111  1.1        ws 		return FSFATAL;
    112  1.1        ws 	}
    113  1.8        ws 
    114  1.1        ws 	memset(fat, 0, sizeof(struct fatEntry) * boot->NumClusters);
    115  1.1        ws 
    116  1.1        ws 	off = boot->ResSectors + no * boot->FATsecs;
    117  1.1        ws 	off *= boot->BytesPerSec;
    118  1.1        ws 
    119  1.1        ws 	if (lseek(fs, off, SEEK_SET) != off) {
    120  1.1        ws 		perror("Unable to read FAT");
    121  1.1        ws 		free(buffer);
    122  1.1        ws 		free(fat);
    123  1.1        ws 		return FSFATAL;
    124  1.1        ws 	}
    125  1.8        ws 
    126  1.8        ws 	if (read(fs, buffer, boot->FATsecs * boot->BytesPerSec)
    127  1.1        ws 	    != boot->FATsecs * boot->BytesPerSec) {
    128  1.8        ws 		perror("Unable to read FAT");
    129  1.1        ws 		free(buffer);
    130  1.1        ws 		free(fat);
    131  1.1        ws 		return FSFATAL;
    132  1.1        ws 	}
    133  1.1        ws 
    134  1.6        ws 	if (buffer[0] != boot->Media
    135  1.6        ws 	    || buffer[1] != 0xff || buffer[2] != 0xff
    136  1.8        ws 	    || (boot->ClustMask == CLUST16_MASK && buffer[3] != 0xff)
    137  1.8        ws 	    || (boot->ClustMask == CLUST32_MASK
    138  1.8        ws 		&& ((buffer[3]&0x0f) != 0x0f
    139  1.8        ws 		    || buffer[4] != 0xff || buffer[5] != 0xff
    140  1.8        ws 		    || buffer[6] != 0xff || (buffer[7]&0x0f) != 0x0f))) {
    141  1.8        ws 		char *msg;
    142  1.8        ws 
    143  1.8        ws 		switch (boot->ClustMask) {
    144  1.8        ws 		case CLUST32_MASK:
    145  1.8        ws 			msg = "FAT starts with odd byte sequence (%02x%02x%02x%02x%02x%02x%02x%02x)\n";
    146  1.8        ws 			break;
    147  1.8        ws 		case CLUST16_MASK:
    148  1.8        ws 			msg = "FAT starts with odd byte sequence (%02x%02x%02x%02x)\n";
    149  1.8        ws 			break;
    150  1.8        ws 		default:
    151  1.8        ws 			msg = "FAT starts with odd byte sequence (%02x%02x%02x)\n";
    152  1.8        ws 			break;
    153  1.8        ws 		}
    154  1.8        ws 		pwarn(msg,
    155  1.8        ws 		      buffer[0], buffer[1], buffer[2], buffer[3],
    156  1.8        ws 		      buffer[4], buffer[5], buffer[6], buffer[7]);
    157  1.8        ws 		if (ask(1, "Correct"))
    158  1.1        ws 			ret |= FSFATMOD;
    159  1.1        ws 	}
    160  1.8        ws 	switch (boot->ClustMask) {
    161  1.8        ws 	case CLUST32_MASK:
    162  1.8        ws 		p = buffer + 8;
    163  1.8        ws 		break;
    164  1.8        ws 	case CLUST16_MASK:
    165  1.8        ws 		p = buffer + 4;
    166  1.8        ws 		break;
    167  1.8        ws 	default:
    168  1.8        ws 		p = buffer + 3;
    169  1.8        ws 		break;
    170  1.8        ws 	}
    171  1.1        ws 	for (cl = CLUST_FIRST; cl < boot->NumClusters;) {
    172  1.8        ws 		switch (boot->ClustMask) {
    173  1.8        ws 		case CLUST32_MASK:
    174  1.8        ws 			fat[cl].next = p[0] + (p[1] << 8)
    175  1.8        ws 				       + (p[2] << 16) + (p[3] << 24);
    176  1.8        ws 			fat[cl].next &= boot->ClustMask;
    177  1.8        ws 			ret |= checkclnum(boot, no, cl, &fat[cl].next);
    178  1.8        ws 			cl++;
    179  1.8        ws 			p += 4;
    180  1.8        ws 			break;
    181  1.8        ws 		case CLUST16_MASK:
    182  1.1        ws 			fat[cl].next = p[0] + (p[1] << 8);
    183  1.1        ws 			ret |= checkclnum(boot, no, cl, &fat[cl].next);
    184  1.1        ws 			cl++;
    185  1.1        ws 			p += 2;
    186  1.8        ws 			break;
    187  1.8        ws 		default:
    188  1.1        ws 			fat[cl].next = (p[0] + (p[1] << 8)) & 0x0fff;
    189  1.1        ws 			ret |= checkclnum(boot, no, cl, &fat[cl].next);
    190  1.1        ws 			cl++;
    191  1.1        ws 			if (cl >= boot->NumClusters)
    192  1.1        ws 				break;
    193  1.1        ws 			fat[cl].next = ((p[1] >> 4) + (p[2] << 4)) & 0x0fff;
    194  1.1        ws 			ret |= checkclnum(boot, no, cl, &fat[cl].next);
    195  1.1        ws 			cl++;
    196  1.1        ws 			p += 3;
    197  1.8        ws 			break;
    198  1.1        ws 		}
    199  1.1        ws 	}
    200  1.8        ws 
    201  1.1        ws 	free(buffer);
    202  1.1        ws 	*fp = fat;
    203  1.1        ws 	return ret;
    204  1.1        ws }
    205  1.1        ws 
    206  1.1        ws /*
    207  1.1        ws  * Get type of reserved cluster
    208  1.1        ws  */
    209  1.1        ws char *
    210  1.1        ws rsrvdcltype(cl)
    211  1.1        ws 	cl_t cl;
    212  1.1        ws {
    213  1.1        ws 	if (cl < CLUST_BAD)
    214  1.1        ws 		return "reserved";
    215  1.1        ws 	if (cl > CLUST_BAD)
    216  1.1        ws 		return "as EOF";
    217  1.1        ws 	return "bad";
    218  1.1        ws }
    219  1.1        ws 
    220  1.1        ws static int
    221  1.1        ws clustdiffer(cl, cp1, cp2, fatnum)
    222  1.1        ws 	cl_t cl;
    223  1.1        ws 	cl_t *cp1;
    224  1.1        ws 	cl_t *cp2;
    225  1.1        ws 	int fatnum;
    226  1.1        ws {
    227  1.1        ws 	if (*cp1 >= CLUST_RSRVD) {
    228  1.1        ws 		if (*cp2 >= CLUST_RSRVD) {
    229  1.1        ws 			if ((*cp1 < CLUST_BAD && *cp2 < CLUST_BAD)
    230  1.1        ws 			    || (*cp1 > CLUST_BAD && *cp2 > CLUST_BAD)) {
    231  1.8        ws 				pwarn("Cluster %u is marked %s with different indicators, ",
    232  1.1        ws 				      cl, rsrvdcltype(*cp1));
    233  1.1        ws 				if (ask(1, "fix")) {
    234  1.1        ws 					*cp2 = *cp1;
    235  1.1        ws 					return FSFATMOD;
    236  1.1        ws 				}
    237  1.1        ws 				return FSFATAL;
    238  1.1        ws 			}
    239  1.8        ws 			pwarn("Cluster %u is marked %s in FAT 0, %s in FAT %d\n",
    240  1.1        ws 			      cl, rsrvdcltype(*cp1), rsrvdcltype(*cp2), fatnum);
    241  1.8        ws 			if (ask(0, "use FAT 0's entry")) {
    242  1.1        ws 				*cp2 = *cp1;
    243  1.1        ws 				return FSFATMOD;
    244  1.1        ws 			}
    245  1.8        ws 			if (ask(0, "use FAT %d's entry", fatnum)) {
    246  1.1        ws 				*cp1 = *cp2;
    247  1.1        ws 				return FSFATMOD;
    248  1.1        ws 			}
    249  1.1        ws 			return FSFATAL;
    250  1.1        ws 		}
    251  1.8        ws 		pwarn("Cluster %u is marked %s in FAT 0, but continues with cluster %u in FAT %d\n",
    252  1.1        ws 		      cl, rsrvdcltype(*cp1), *cp2, fatnum);
    253  1.1        ws 		if (ask(0, "Use continuation from FAT %d", fatnum)) {
    254  1.1        ws 			*cp1 = *cp2;
    255  1.1        ws 			return FSFATMOD;
    256  1.1        ws 		}
    257  1.8        ws 		if (ask(0, "Use mark from FAT 0")) {
    258  1.1        ws 			*cp2 = *cp1;
    259  1.1        ws 			return FSFATMOD;
    260  1.1        ws 		}
    261  1.1        ws 		return FSFATAL;
    262  1.1        ws 	}
    263  1.1        ws 	if (*cp2 >= CLUST_RSRVD) {
    264  1.8        ws 		pwarn("Cluster %u continues with cluster %u in FAT 0, but is marked %s in FAT %d\n",
    265  1.1        ws 		      cl, *cp1, rsrvdcltype(*cp2), fatnum);
    266  1.8        ws 		if (ask(0, "Use continuation from FAT 0")) {
    267  1.1        ws 			*cp2 = *cp1;
    268  1.1        ws 			return FSFATMOD;
    269  1.1        ws 		}
    270  1.1        ws 		if (ask(0, "Use mark from FAT %d", fatnum)) {
    271  1.1        ws 			*cp1 = *cp2;
    272  1.1        ws 			return FSFATMOD;
    273  1.1        ws 		}
    274  1.1        ws 		return FSERROR;
    275  1.1        ws 	}
    276  1.8        ws 	pwarn("Cluster %u continues with cluster %u in FAT 0, but with cluster %u in FAT %d\n",
    277  1.1        ws 	      cl, *cp1, *cp2, fatnum);
    278  1.8        ws 	if (ask(0, "Use continuation from FAT 0")) {
    279  1.1        ws 		*cp2 = *cp1;
    280  1.1        ws 		return FSFATMOD;
    281  1.1        ws 	}
    282  1.1        ws 	if (ask(0, "Use continuation from FAT %d", fatnum)) {
    283  1.1        ws 		*cp1 = *cp2;
    284  1.1        ws 		return FSFATMOD;
    285  1.1        ws 	}
    286  1.1        ws 	return FSERROR;
    287  1.1        ws }
    288  1.1        ws 
    289  1.1        ws /*
    290  1.1        ws  * Compare two FAT copies in memory. Resolve any conflicts and merge them
    291  1.1        ws  * into the first one.
    292  1.1        ws  */
    293  1.1        ws int
    294  1.1        ws comparefat(boot, first, second, fatnum)
    295  1.1        ws 	struct bootblock *boot;
    296  1.1        ws 	struct fatEntry *first;
    297  1.1        ws 	struct fatEntry *second;
    298  1.1        ws 	int fatnum;
    299  1.1        ws {
    300  1.1        ws 	cl_t cl;
    301  1.1        ws 	int ret = FSOK;
    302  1.1        ws 
    303  1.1        ws 	for (cl = CLUST_FIRST; cl < boot->NumClusters; cl++)
    304  1.1        ws 		if (first[cl].next != second[cl].next)
    305  1.1        ws 			ret |= clustdiffer(cl, &first[cl].next, &second[cl].next, fatnum);
    306  1.1        ws 	return ret;
    307  1.1        ws }
    308  1.1        ws 
    309  1.1        ws void
    310  1.1        ws clearchain(boot, fat, head)
    311  1.1        ws 	struct bootblock *boot;
    312  1.1        ws 	struct fatEntry *fat;
    313  1.1        ws 	cl_t head;
    314  1.1        ws {
    315  1.1        ws 	cl_t p, q;
    316  1.1        ws 
    317  1.1        ws 	for (p = head; p >= CLUST_FIRST && p < boot->NumClusters; p = q) {
    318  1.1        ws 		if (fat[p].head != head)
    319  1.1        ws 			break;
    320  1.1        ws 		q = fat[p].next;
    321  1.1        ws 		fat[p].next = fat[p].head = CLUST_FREE;
    322  1.1        ws 		fat[p].length = 0;
    323  1.1        ws 	}
    324  1.1        ws }
    325  1.1        ws 
    326  1.1        ws /*
    327  1.1        ws  * Check a complete FAT in-memory for crosslinks
    328  1.1        ws  */
    329  1.1        ws int
    330  1.1        ws checkfat(boot, fat)
    331  1.1        ws 	struct bootblock *boot;
    332  1.1        ws 	struct fatEntry *fat;
    333  1.1        ws {
    334  1.1        ws 	cl_t head, p, h;
    335  1.1        ws 	u_int len;
    336  1.1        ws 	int ret = 0;
    337  1.1        ws 	int conf;
    338  1.8        ws 
    339  1.1        ws 	/*
    340  1.1        ws 	 * pass 1: figure out the cluster chains.
    341  1.1        ws 	 */
    342  1.1        ws 	for (head = CLUST_FIRST; head < boot->NumClusters; head++) {
    343  1.8        ws 		/* find next untravelled chain */
    344  1.6        ws 		if (fat[head].head != 0		/* cluster already belongs to some chain */
    345  1.5        ws 		    || fat[head].next == CLUST_FREE
    346  1.5        ws 		    || fat[head].next == CLUST_BAD)
    347  1.1        ws 			continue;		/* skip it. */
    348  1.1        ws 
    349  1.1        ws 		/* follow the chain and mark all clusters on the way */
    350  1.1        ws 		for (len = 0, p = head;
    351  1.1        ws 		     p >= CLUST_FIRST && p < boot->NumClusters;
    352  1.1        ws 		     p = fat[p].next) {
    353  1.1        ws 			fat[p].head = head;
    354  1.1        ws 			len++;
    355  1.1        ws 		}
    356  1.1        ws 
    357  1.1        ws 		/* the head record gets the length */
    358  1.8        ws 		fat[head].length = fat[head].next == CLUST_FREE ? 0 : len;
    359  1.1        ws 	}
    360  1.8        ws 
    361  1.1        ws 	/*
    362  1.1        ws 	 * pass 2: check for crosslinked chains (we couldn't do this in pass 1 because
    363  1.1        ws 	 * we didn't know the real start of the chain then - would have treated partial
    364  1.1        ws 	 * chains as interlinked with their main chain)
    365  1.1        ws 	 */
    366  1.1        ws 	for (head = CLUST_FIRST; head < boot->NumClusters; head++) {
    367  1.8        ws 		/* find next untravelled chain */
    368  1.1        ws 		if (fat[head].head != head)
    369  1.1        ws 			continue;
    370  1.1        ws 
    371  1.1        ws 		/* follow the chain to its end (hopefully) */
    372  1.1        ws 		for (p = head;
    373  1.1        ws 		     fat[p].next >= CLUST_FIRST && fat[p].next < boot->NumClusters;
    374  1.1        ws 		     p = fat[p].next)
    375  1.1        ws 			if (fat[fat[p].next].head != head)
    376  1.1        ws 				break;
    377  1.1        ws 		if (fat[p].next >= CLUST_EOFS)
    378  1.1        ws 			continue;
    379  1.8        ws 
    380  1.1        ws 		if (fat[p].next == 0) {
    381  1.8        ws 			pwarn("Cluster chain starting at %u ends with free cluster\n", head);
    382  1.8        ws 			if (ask(0, "Clear chain starting at %u", head)) {
    383  1.1        ws 				clearchain(boot, fat, head);
    384  1.1        ws 				ret |= FSFATMOD;
    385  1.1        ws 			} else
    386  1.1        ws 				ret |= FSERROR;
    387  1.1        ws 			continue;
    388  1.1        ws 		}
    389  1.1        ws 		if (fat[p].next >= CLUST_RSRVD) {
    390  1.8        ws 			pwarn("Cluster chain starting at %u ends with cluster marked %s\n",
    391  1.1        ws 			      head, rsrvdcltype(fat[p].next));
    392  1.8        ws 			if (ask(0, "Clear chain starting at %u", head)) {
    393  1.1        ws 				clearchain(boot, fat, head);
    394  1.1        ws 				ret |= FSFATMOD;
    395  1.1        ws 			} else
    396  1.1        ws 				ret |= FSERROR;
    397  1.1        ws 			continue;
    398  1.1        ws 		}
    399  1.1        ws 		if (fat[p].next < CLUST_FIRST || fat[p].next >= boot->NumClusters) {
    400  1.8        ws 			pwarn("Cluster chain starting at %u ends with cluster out of range (%u)\n",
    401  1.1        ws 			      head, fat[p].next);
    402  1.8        ws 			if (ask(0, "Clear chain starting at %u", head)) {
    403  1.1        ws 				clearchain(boot, fat, head);
    404  1.1        ws 				ret |= FSFATMOD;
    405  1.1        ws 			} else
    406  1.1        ws 				ret |= FSERROR;
    407  1.1        ws 		}
    408  1.8        ws 		pwarn("Cluster chains starting at %u and %u are linked at cluster %u\n",
    409  1.1        ws 		      head, fat[p].head, p);
    410  1.1        ws 		conf = FSERROR;
    411  1.8        ws 		if (ask(0, "Clear chain starting at %u", head)) {
    412  1.1        ws 			clearchain(boot, fat, head);
    413  1.1        ws 			conf = FSFATMOD;
    414  1.1        ws 		}
    415  1.8        ws 		if (ask(0, "Clear chain starting at %u", h = fat[p].head)) {
    416  1.1        ws 			if (conf == FSERROR) {
    417  1.1        ws 				/*
    418  1.1        ws 				 * Transfer the common chain to the one not cleared above.
    419  1.1        ws 				 */
    420  1.1        ws 				for (; p >= CLUST_FIRST && p < boot->NumClusters;
    421  1.1        ws 				     p = fat[p].next) {
    422  1.1        ws 					if (h != fat[p].head) {
    423  1.1        ws 						/*
    424  1.1        ws 						 * Have to reexamine this chain.
    425  1.1        ws 						 */
    426  1.1        ws 						head--;
    427  1.1        ws 						break;
    428  1.1        ws 					}
    429  1.1        ws 					fat[p].head = head;
    430  1.1        ws 				}
    431  1.1        ws 			}
    432  1.1        ws 			clearchain(boot, fat, h);
    433  1.1        ws 			conf |= FSFATMOD;
    434  1.1        ws 		}
    435  1.1        ws 		ret |= conf;
    436  1.1        ws 	}
    437  1.1        ws 
    438  1.1        ws 	return ret;
    439  1.1        ws }
    440  1.1        ws 
    441  1.1        ws /*
    442  1.1        ws  * Write out FATs encoding them from the internal format
    443  1.1        ws  */
    444  1.1        ws int
    445  1.1        ws writefat(fs, boot, fat)
    446  1.1        ws 	int fs;
    447  1.1        ws 	struct bootblock *boot;
    448  1.1        ws 	struct fatEntry *fat;
    449  1.1        ws {
    450  1.1        ws 	u_char *buffer, *p;
    451  1.1        ws 	cl_t cl;
    452  1.1        ws 	int i;
    453  1.1        ws 	u_int32_t fatsz;
    454  1.1        ws 	off_t off;
    455  1.1        ws 	int ret = FSOK;
    456  1.8        ws 
    457  1.1        ws 	buffer = malloc(fatsz = boot->FATsecs * boot->BytesPerSec);
    458  1.1        ws 	if (buffer == NULL) {
    459  1.1        ws 		perror("No space for FAT");
    460  1.1        ws 		return FSFATAL;
    461  1.1        ws 	}
    462  1.1        ws 	memset(buffer, 0, fatsz);
    463  1.1        ws 	boot->NumFree = 0;
    464  1.6        ws 	p = buffer;
    465  1.8        ws 	*p++ = (u_char)boot->Media;
    466  1.8        ws 	*p++ = 0xff;
    467  1.8        ws 	*p++ = 0xff;
    468  1.8        ws 	switch (boot->ClustMask) {
    469  1.8        ws 	case CLUST16_MASK:
    470  1.8        ws 		*p++ = 0xff;
    471  1.8        ws 		break;
    472  1.8        ws 	case CLUST32_MASK:
    473  1.8        ws 		*p++ = 0x0f;
    474  1.8        ws 		*p++ = 0xff;
    475  1.8        ws 		*p++ = 0xff;
    476  1.8        ws 		*p++ = 0xff;
    477  1.8        ws 		*p++ = 0x0f;
    478  1.8        ws 		break;
    479  1.8        ws 	}
    480  1.6        ws 	for (cl = CLUST_FIRST; cl < boot->NumClusters; cl++) {
    481  1.8        ws 		switch (boot->ClustMask) {
    482  1.8        ws 		case CLUST32_MASK:
    483  1.8        ws 			if (fat[cl].next == CLUST_FREE)
    484  1.8        ws 				boot->NumFree++;
    485  1.8        ws 			*p++ = (u_char)fat[cl].next;
    486  1.8        ws 			*p++ = (u_char)(fat[cl].next >> 8);
    487  1.8        ws 			*p++ = (u_char)(fat[cl].next >> 16);
    488  1.8        ws 			*p &= 0xf0;
    489  1.8        ws 			*p++ |= (fat[cl].next >> 24)&0x0f;
    490  1.8        ws 			break;
    491  1.8        ws 		case CLUST16_MASK:
    492  1.1        ws 			if (fat[cl].next == CLUST_FREE)
    493  1.1        ws 				boot->NumFree++;
    494  1.8        ws 			*p++ = (u_char)fat[cl].next;
    495  1.8        ws 			*p++ = (u_char)(fat[cl].next >> 8);
    496  1.8        ws 			break;
    497  1.8        ws 		default:
    498  1.1        ws 			if (fat[cl].next == CLUST_FREE)
    499  1.1        ws 				boot->NumFree++;
    500  1.1        ws 			if (cl + 1 < boot->NumClusters
    501  1.1        ws 			    && fat[cl + 1].next == CLUST_FREE)
    502  1.1        ws 				boot->NumFree++;
    503  1.8        ws 			*p++ = (u_char)fat[cl].next;
    504  1.8        ws 			*p++ = (u_char)((fat[cl].next >> 8) & 0xf)
    505  1.8        ws 			       |(u_char)(fat[cl+1].next << 4);
    506  1.8        ws 			*p++ = (u_char)(fat[++cl].next >> 4);
    507  1.8        ws 			break;
    508  1.1        ws 		}
    509  1.1        ws 	}
    510  1.1        ws 	for (i = 0; i < boot->FATs; i++) {
    511  1.1        ws 		off = boot->ResSectors + i * boot->FATsecs;
    512  1.1        ws 		off *= boot->BytesPerSec;
    513  1.1        ws 		if (lseek(fs, off, SEEK_SET) != off
    514  1.1        ws 		    || write(fs, buffer, fatsz) != fatsz) {
    515  1.1        ws 			perror("Unable to write FAT");
    516  1.1        ws 			ret = FSFATAL; /* Return immediately?		XXX */
    517  1.1        ws 		}
    518  1.1        ws 	}
    519  1.1        ws 	free(buffer);
    520  1.1        ws 	return ret;
    521  1.1        ws }
    522  1.1        ws 
    523  1.1        ws /*
    524  1.1        ws  * Check a complete in-memory FAT for lost cluster chains
    525  1.1        ws  */
    526  1.1        ws int
    527  1.2        ws checklost(dosfs, boot, fat)
    528  1.1        ws 	int dosfs;
    529  1.1        ws 	struct bootblock *boot;
    530  1.1        ws 	struct fatEntry *fat;
    531  1.1        ws {
    532  1.1        ws 	cl_t head;
    533  1.1        ws 	int mod = FSOK;
    534  1.8        ws 	int ret;
    535  1.1        ws 
    536  1.1        ws 	for (head = CLUST_FIRST; head < boot->NumClusters; head++) {
    537  1.8        ws 		/* find next untravelled chain */
    538  1.1        ws 		if (fat[head].head != head
    539  1.1        ws 		    || fat[head].next == CLUST_FREE
    540  1.1        ws 		    || (fat[head].next >= CLUST_RSRVD
    541  1.2        ws 			&& fat[head].next < CLUST_EOFS)
    542  1.2        ws 		    || (fat[head].flags & FAT_USED))
    543  1.1        ws 			continue;
    544  1.1        ws 
    545  1.8        ws 		pwarn("Lost cluster chain at cluster %u\n%d Cluster(s) lost\n",
    546  1.2        ws 		      head, fat[head].length);
    547  1.8        ws 		mod |= ret = reconnect(dosfs, boot, fat, head);
    548  1.2        ws 		if (mod & FSFATAL)
    549  1.2        ws 			break;
    550  1.8        ws 		if (ret == FSERROR && ask(0, "Clear")) {
    551  1.8        ws 			clearchain(boot, fat, head);
    552  1.8        ws 			mod |= FSFATMOD;
    553  1.8        ws 		}
    554  1.1        ws 	}
    555  1.1        ws 	finishlf();
    556  1.8        ws 
    557  1.8        ws 	if (boot->FSInfo) {
    558  1.8        ws 		ret = 0;
    559  1.8        ws 		if (boot->FSFree != boot->NumFree) {
    560  1.8        ws 			pwarn("Free space in FSInfo block (%d) not correct (%d)\n",
    561  1.8        ws 			      boot->FSFree, boot->NumFree);
    562  1.8        ws 			if (ask(1, "fix")) {
    563  1.8        ws 				boot->FSFree = boot->NumFree;
    564  1.8        ws 				ret = 1;
    565  1.8        ws 			}
    566  1.8        ws 		}
    567  1.8        ws 		if (boot->NumFree && fat[boot->FSNext].next != CLUST_FREE) {
    568  1.8        ws 			pwarn("Next free cluster in FSInfo block (%u) not free\n",
    569  1.8        ws 			      boot->FSNext);
    570  1.8        ws 			if (ask(1, "fix"))
    571  1.8        ws 				for (head = CLUST_FIRST; head < boot->NumClusters; head++)
    572  1.8        ws 					if (fat[head].next == CLUST_FREE) {
    573  1.8        ws 						boot->FSNext = head;
    574  1.8        ws 						ret = 1;
    575  1.8        ws 						break;
    576  1.8        ws 					}
    577  1.8        ws 		}
    578  1.8        ws 		if (ret)
    579  1.8        ws 			mod |= writefsinfo(dosfs, boot);
    580  1.8        ws 	}
    581  1.8        ws 
    582  1.1        ws 	return mod;
    583  1.1        ws }
    584