2019-06-04 03:08:00 +03:00
|
|
|
/* $NetBSD: fat.c,v 1.30 2019/06/04 00:08:00 christos Exp $ */
|
1996-05-14 21:39:21 +04:00
|
|
|
|
|
|
|
/*
|
1997-10-17 15:19:23 +04:00
|
|
|
* Copyright (C) 1995, 1996, 1997 Wolfgang Solfrank
|
1996-05-14 21:39:21 +04:00
|
|
|
* Copyright (c) 1995 Martin Husemann
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
1997-09-14 18:40:10 +04:00
|
|
|
#include <sys/cdefs.h>
|
1996-05-14 21:39:21 +04:00
|
|
|
#ifndef lint
|
2019-06-04 03:08:00 +03:00
|
|
|
__RCSID("$NetBSD: fat.c,v 1.30 2019/06/04 00:08:00 christos Exp $");
|
1996-05-14 21:39:21 +04:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "ext.h"
|
1996-09-28 03:22:50 +04:00
|
|
|
#include "fsutil.h"
|
1996-09-23 20:27:56 +04:00
|
|
|
|
2009-04-11 10:59:18 +04:00
|
|
|
static int checkclnum(struct bootblock *, u_int, cl_t, cl_t *);
|
|
|
|
static int clustdiffer(cl_t, cl_t *, cl_t *, u_int);
|
2005-01-19 23:00:45 +03:00
|
|
|
static int tryclear(struct bootblock *, struct fatEntry *, cl_t, cl_t *);
|
2009-04-11 10:59:18 +04:00
|
|
|
static int _readfat(int, struct bootblock *, u_int, u_char **);
|
1996-05-14 21:39:21 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Check a cluster number for valid value
|
|
|
|
*/
|
|
|
|
static int
|
2009-04-11 10:59:18 +04:00
|
|
|
checkclnum(struct bootblock *boot, u_int fat, cl_t cl, cl_t *next)
|
1996-05-14 21:39:21 +04:00
|
|
|
{
|
1997-10-17 15:19:23 +04:00
|
|
|
if (*next >= (CLUST_RSRVD&boot->ClustMask))
|
|
|
|
*next |= ~boot->ClustMask;
|
1996-05-14 21:39:21 +04:00
|
|
|
if (*next == CLUST_FREE) {
|
|
|
|
boot->NumFree++;
|
|
|
|
return FSOK;
|
|
|
|
}
|
1997-01-03 17:32:47 +03:00
|
|
|
if (*next == CLUST_BAD) {
|
|
|
|
boot->NumBad++;
|
|
|
|
return FSOK;
|
|
|
|
}
|
1996-05-14 21:39:21 +04:00
|
|
|
if (*next < CLUST_FIRST
|
|
|
|
|| (*next >= boot->NumClusters && *next < CLUST_EOFS)) {
|
2009-04-11 10:59:18 +04:00
|
|
|
pwarn("Cluster %u in FAT %u continues with %s cluster number %u\n",
|
1996-05-14 21:39:21 +04:00
|
|
|
cl, fat,
|
|
|
|
*next < CLUST_RSRVD ? "out of range" : "reserved",
|
1997-10-17 15:19:23 +04:00
|
|
|
*next&boot->ClustMask);
|
1996-05-14 21:39:21 +04:00
|
|
|
if (ask(0, "Truncate")) {
|
|
|
|
*next = CLUST_EOF;
|
|
|
|
return FSFATMOD;
|
|
|
|
}
|
|
|
|
return FSERROR;
|
|
|
|
}
|
|
|
|
return FSOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2000-04-26 03:02:51 +04:00
|
|
|
* Read a FAT from disk. Returns 1 if successful, 0 otherwise.
|
1996-05-14 21:39:21 +04:00
|
|
|
*/
|
2000-04-26 20:45:02 +04:00
|
|
|
static int
|
2009-04-11 10:59:18 +04:00
|
|
|
_readfat(int fs, struct bootblock *boot, u_int no, u_char **buffer)
|
1996-05-14 21:39:21 +04:00
|
|
|
{
|
|
|
|
off_t off;
|
2006-06-05 20:51:18 +04:00
|
|
|
size_t len;
|
1996-05-14 21:39:21 +04:00
|
|
|
|
2006-06-05 20:51:18 +04:00
|
|
|
*buffer = malloc(len = boot->FATsecs * boot->BytesPerSec);
|
2000-04-26 03:02:51 +04:00
|
|
|
if (*buffer == NULL) {
|
2006-06-05 20:51:18 +04:00
|
|
|
perr("No space for FAT sectors (%zu)", len);
|
2000-04-26 03:02:51 +04:00
|
|
|
return 0;
|
1996-05-14 21:39:21 +04:00
|
|
|
}
|
1997-10-17 15:19:23 +04:00
|
|
|
|
1996-05-14 21:39:21 +04:00
|
|
|
off = boot->ResSectors + no * boot->FATsecs;
|
|
|
|
off *= boot->BytesPerSec;
|
|
|
|
|
|
|
|
if (lseek(fs, off, SEEK_SET) != off) {
|
2006-06-05 20:51:18 +04:00
|
|
|
perr("Unable to read FAT");
|
2000-04-26 03:02:51 +04:00
|
|
|
goto err;
|
1996-05-14 21:39:21 +04:00
|
|
|
}
|
1997-10-17 15:19:23 +04:00
|
|
|
|
2009-04-11 10:59:18 +04:00
|
|
|
if ((size_t)read(fs, *buffer, boot->FATsecs * boot->BytesPerSec)
|
1996-05-14 21:39:21 +04:00
|
|
|
!= boot->FATsecs * boot->BytesPerSec) {
|
2006-06-05 20:51:18 +04:00
|
|
|
perr("Unable to read FAT");
|
2000-04-26 03:02:51 +04:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
err:
|
|
|
|
free(*buffer);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read a FAT and decode it into internal format
|
|
|
|
*/
|
|
|
|
int
|
2009-04-11 10:59:18 +04:00
|
|
|
readfat(int fs, struct bootblock *boot, u_int no, struct fatEntry **fp)
|
2000-04-26 03:02:51 +04:00
|
|
|
{
|
|
|
|
struct fatEntry *fat;
|
|
|
|
u_char *buffer, *p;
|
|
|
|
cl_t cl;
|
|
|
|
int ret = FSOK;
|
2006-06-05 20:51:18 +04:00
|
|
|
size_t len;
|
2000-04-26 03:02:51 +04:00
|
|
|
|
|
|
|
boot->NumFree = boot->NumBad = 0;
|
|
|
|
|
|
|
|
if (!_readfat(fs, boot, no, &buffer))
|
|
|
|
return FSFATAL;
|
2008-07-24 18:23:16 +04:00
|
|
|
|
2006-06-05 20:51:18 +04:00
|
|
|
fat = malloc(len = boot->NumClusters * sizeof(struct fatEntry));
|
2000-04-26 03:02:51 +04:00
|
|
|
if (fat == NULL) {
|
2006-06-05 20:51:18 +04:00
|
|
|
perr("No space for FAT clusters (%zu)", len);
|
1996-05-14 21:39:21 +04:00
|
|
|
free(buffer);
|
|
|
|
return FSFATAL;
|
|
|
|
}
|
2006-06-05 20:51:18 +04:00
|
|
|
(void)memset(fat, 0, len);
|
1996-05-14 21:39:21 +04:00
|
|
|
|
1997-09-08 18:05:30 +04:00
|
|
|
if (buffer[0] != boot->Media
|
|
|
|
|| buffer[1] != 0xff || buffer[2] != 0xff
|
1997-10-17 15:19:23 +04:00
|
|
|
|| (boot->ClustMask == CLUST16_MASK && buffer[3] != 0xff)
|
|
|
|
|| (boot->ClustMask == CLUST32_MASK
|
|
|
|
&& ((buffer[3]&0x0f) != 0x0f
|
|
|
|
|| buffer[4] != 0xff || buffer[5] != 0xff
|
|
|
|
|| buffer[6] != 0xff || (buffer[7]&0x0f) != 0x0f))) {
|
2000-04-26 03:02:51 +04:00
|
|
|
|
|
|
|
/* Windows 95 OSR2 (and possibly any later) changes
|
|
|
|
* the FAT signature to 0xXXffff7f for FAT16 and to
|
|
|
|
* 0xXXffff0fffffff07 for FAT32 upon boot, to know that the
|
|
|
|
* filesystem is dirty if it doesn't reboot cleanly.
|
|
|
|
* Check this special condition before errorring out.
|
|
|
|
*/
|
|
|
|
if (buffer[0] == boot->Media && buffer[1] == 0xff
|
|
|
|
&& buffer[2] == 0xff
|
|
|
|
&& ((boot->ClustMask == CLUST16_MASK && buffer[3] == 0x7f)
|
|
|
|
|| (boot->ClustMask == CLUST32_MASK
|
|
|
|
&& buffer[3] == 0x0f && buffer[4] == 0xff
|
|
|
|
&& buffer[5] == 0xff && buffer[6] == 0xff
|
|
|
|
&& buffer[7] == 0x07)))
|
|
|
|
ret |= FSDIRTY;
|
|
|
|
else {
|
|
|
|
/* just some odd byte sequence in FAT */
|
2008-07-24 18:23:16 +04:00
|
|
|
|
2000-04-26 03:02:51 +04:00
|
|
|
switch (boot->ClustMask) {
|
|
|
|
case CLUST32_MASK:
|
2000-10-11 00:24:49 +04:00
|
|
|
pwarn("%s (%02x%02x%02x%02x%02x%02x%02x%02x)\n",
|
|
|
|
"FAT starts with odd byte sequence",
|
|
|
|
buffer[0], buffer[1], buffer[2], buffer[3],
|
|
|
|
buffer[4], buffer[5], buffer[6], buffer[7]);
|
2000-04-26 03:02:51 +04:00
|
|
|
break;
|
|
|
|
case CLUST16_MASK:
|
2001-01-05 04:41:34 +03:00
|
|
|
pwarn("%s (%02x%02x%02x%02x)\n",
|
|
|
|
"FAT starts with odd byte sequence",
|
|
|
|
buffer[0], buffer[1], buffer[2], buffer[3]);
|
2000-04-26 03:02:51 +04:00
|
|
|
break;
|
|
|
|
default:
|
2001-01-05 04:41:34 +03:00
|
|
|
pwarn("%s (%02x%02x%02x)\n",
|
|
|
|
"FAT starts with odd byte sequence",
|
|
|
|
buffer[0], buffer[1], buffer[2]);
|
2000-04-26 03:02:51 +04:00
|
|
|
break;
|
|
|
|
}
|
1997-10-17 15:19:23 +04:00
|
|
|
|
2008-07-24 18:23:16 +04:00
|
|
|
|
2000-04-26 03:02:51 +04:00
|
|
|
if (ask(1, "Correct"))
|
|
|
|
ret |= FSFIXFAT;
|
1996-05-14 21:39:21 +04:00
|
|
|
}
|
1997-10-17 15:19:23 +04:00
|
|
|
}
|
|
|
|
switch (boot->ClustMask) {
|
|
|
|
case CLUST32_MASK:
|
|
|
|
p = buffer + 8;
|
|
|
|
break;
|
|
|
|
case CLUST16_MASK:
|
|
|
|
p = buffer + 4;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
p = buffer + 3;
|
|
|
|
break;
|
1996-05-14 21:39:21 +04:00
|
|
|
}
|
|
|
|
for (cl = CLUST_FIRST; cl < boot->NumClusters;) {
|
1997-10-17 15:19:23 +04:00
|
|
|
switch (boot->ClustMask) {
|
|
|
|
case CLUST32_MASK:
|
|
|
|
fat[cl].next = p[0] + (p[1] << 8)
|
|
|
|
+ (p[2] << 16) + (p[3] << 24);
|
|
|
|
fat[cl].next &= boot->ClustMask;
|
|
|
|
ret |= checkclnum(boot, no, cl, &fat[cl].next);
|
|
|
|
cl++;
|
|
|
|
p += 4;
|
|
|
|
break;
|
|
|
|
case CLUST16_MASK:
|
1996-05-14 21:39:21 +04:00
|
|
|
fat[cl].next = p[0] + (p[1] << 8);
|
|
|
|
ret |= checkclnum(boot, no, cl, &fat[cl].next);
|
|
|
|
cl++;
|
|
|
|
p += 2;
|
1997-10-17 15:19:23 +04:00
|
|
|
break;
|
|
|
|
default:
|
1996-05-14 21:39:21 +04:00
|
|
|
fat[cl].next = (p[0] + (p[1] << 8)) & 0x0fff;
|
|
|
|
ret |= checkclnum(boot, no, cl, &fat[cl].next);
|
|
|
|
cl++;
|
|
|
|
if (cl >= boot->NumClusters)
|
|
|
|
break;
|
|
|
|
fat[cl].next = ((p[1] >> 4) + (p[2] << 4)) & 0x0fff;
|
|
|
|
ret |= checkclnum(boot, no, cl, &fat[cl].next);
|
|
|
|
cl++;
|
|
|
|
p += 3;
|
1997-10-17 15:19:23 +04:00
|
|
|
break;
|
1996-05-14 21:39:21 +04:00
|
|
|
}
|
|
|
|
}
|
1997-10-17 15:19:23 +04:00
|
|
|
|
1996-05-14 21:39:21 +04:00
|
|
|
free(buffer);
|
2006-03-20 04:42:47 +03:00
|
|
|
if (ret & FSFATAL) {
|
|
|
|
free(fat);
|
|
|
|
*fp = NULL;
|
|
|
|
} else
|
|
|
|
*fp = fat;
|
1996-05-14 21:39:21 +04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get type of reserved cluster
|
|
|
|
*/
|
2001-04-06 20:52:41 +04:00
|
|
|
const char *
|
2005-01-19 23:00:45 +03:00
|
|
|
rsrvdcltype(cl_t cl)
|
1996-05-14 21:39:21 +04:00
|
|
|
{
|
1998-01-22 21:48:43 +03:00
|
|
|
if (cl == CLUST_FREE)
|
|
|
|
return "free";
|
1996-05-14 21:39:21 +04:00
|
|
|
if (cl < CLUST_BAD)
|
|
|
|
return "reserved";
|
|
|
|
if (cl > CLUST_BAD)
|
|
|
|
return "as EOF";
|
|
|
|
return "bad";
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2009-04-11 10:59:18 +04:00
|
|
|
clustdiffer(cl_t cl, cl_t *cp1, cl_t *cp2, u_int fatnum)
|
1996-05-14 21:39:21 +04:00
|
|
|
{
|
1998-01-22 21:48:43 +03:00
|
|
|
if (*cp1 == CLUST_FREE || *cp1 >= CLUST_RSRVD) {
|
|
|
|
if (*cp2 == CLUST_FREE || *cp2 >= CLUST_RSRVD) {
|
|
|
|
if ((*cp1 != CLUST_FREE && *cp1 < CLUST_BAD
|
|
|
|
&& *cp2 != CLUST_FREE && *cp2 < CLUST_BAD)
|
1996-05-14 21:39:21 +04:00
|
|
|
|| (*cp1 > CLUST_BAD && *cp2 > CLUST_BAD)) {
|
1997-10-17 15:19:23 +04:00
|
|
|
pwarn("Cluster %u is marked %s with different indicators, ",
|
1996-05-14 21:39:21 +04:00
|
|
|
cl, rsrvdcltype(*cp1));
|
|
|
|
if (ask(1, "fix")) {
|
|
|
|
*cp2 = *cp1;
|
|
|
|
return FSFATMOD;
|
|
|
|
}
|
|
|
|
return FSFATAL;
|
|
|
|
}
|
2009-04-11 10:59:18 +04:00
|
|
|
pwarn("Cluster %u is marked %s in FAT 0, %s in FAT %u\n",
|
1996-05-14 21:39:21 +04:00
|
|
|
cl, rsrvdcltype(*cp1), rsrvdcltype(*cp2), fatnum);
|
1997-10-17 15:19:23 +04:00
|
|
|
if (ask(0, "use FAT 0's entry")) {
|
1996-05-14 21:39:21 +04:00
|
|
|
*cp2 = *cp1;
|
|
|
|
return FSFATMOD;
|
|
|
|
}
|
2009-04-11 10:59:18 +04:00
|
|
|
if (ask(0, "use FAT %u's entry", fatnum)) {
|
1996-05-14 21:39:21 +04:00
|
|
|
*cp1 = *cp2;
|
|
|
|
return FSFATMOD;
|
|
|
|
}
|
|
|
|
return FSFATAL;
|
|
|
|
}
|
2009-04-11 10:59:18 +04:00
|
|
|
pwarn("Cluster %u is marked %s in FAT 0, but continues with cluster %u in FAT %u\n",
|
1996-05-14 21:39:21 +04:00
|
|
|
cl, rsrvdcltype(*cp1), *cp2, fatnum);
|
2009-04-11 10:59:18 +04:00
|
|
|
if (ask(0, "Use continuation from FAT %u", fatnum)) {
|
1996-05-14 21:39:21 +04:00
|
|
|
*cp1 = *cp2;
|
|
|
|
return FSFATMOD;
|
|
|
|
}
|
1997-10-17 15:19:23 +04:00
|
|
|
if (ask(0, "Use mark from FAT 0")) {
|
1996-05-14 21:39:21 +04:00
|
|
|
*cp2 = *cp1;
|
|
|
|
return FSFATMOD;
|
|
|
|
}
|
|
|
|
return FSFATAL;
|
|
|
|
}
|
1998-01-22 21:48:43 +03:00
|
|
|
if (*cp2 == CLUST_FREE || *cp2 >= CLUST_RSRVD) {
|
2009-04-11 10:59:18 +04:00
|
|
|
pwarn("Cluster %u continues with cluster %u in FAT 0, but is marked %s in FAT %u\n",
|
1996-05-14 21:39:21 +04:00
|
|
|
cl, *cp1, rsrvdcltype(*cp2), fatnum);
|
1997-10-17 15:19:23 +04:00
|
|
|
if (ask(0, "Use continuation from FAT 0")) {
|
1996-05-14 21:39:21 +04:00
|
|
|
*cp2 = *cp1;
|
|
|
|
return FSFATMOD;
|
|
|
|
}
|
2009-04-11 10:59:18 +04:00
|
|
|
if (ask(0, "Use mark from FAT %u", fatnum)) {
|
1996-05-14 21:39:21 +04:00
|
|
|
*cp1 = *cp2;
|
|
|
|
return FSFATMOD;
|
|
|
|
}
|
|
|
|
return FSERROR;
|
|
|
|
}
|
2009-04-11 10:59:18 +04:00
|
|
|
pwarn("Cluster %u continues with cluster %u in FAT 0, but with cluster %u in FAT %u\n",
|
1996-05-14 21:39:21 +04:00
|
|
|
cl, *cp1, *cp2, fatnum);
|
1997-10-17 15:19:23 +04:00
|
|
|
if (ask(0, "Use continuation from FAT 0")) {
|
1996-05-14 21:39:21 +04:00
|
|
|
*cp2 = *cp1;
|
|
|
|
return FSFATMOD;
|
|
|
|
}
|
2009-04-11 10:59:18 +04:00
|
|
|
if (ask(0, "Use continuation from FAT %u", fatnum)) {
|
1996-05-14 21:39:21 +04:00
|
|
|
*cp1 = *cp2;
|
|
|
|
return FSFATMOD;
|
|
|
|
}
|
|
|
|
return FSERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Compare two FAT copies in memory. Resolve any conflicts and merge them
|
|
|
|
* into the first one.
|
|
|
|
*/
|
|
|
|
int
|
2008-07-24 18:23:16 +04:00
|
|
|
comparefat(struct bootblock *boot, struct fatEntry *first,
|
2009-04-11 10:59:18 +04:00
|
|
|
struct fatEntry *second, u_int fatnum)
|
1996-05-14 21:39:21 +04:00
|
|
|
{
|
|
|
|
cl_t cl;
|
|
|
|
int ret = FSOK;
|
|
|
|
|
|
|
|
for (cl = CLUST_FIRST; cl < boot->NumClusters; cl++)
|
|
|
|
if (first[cl].next != second[cl].next)
|
|
|
|
ret |= clustdiffer(cl, &first[cl].next, &second[cl].next, fatnum);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2005-01-19 23:00:45 +03:00
|
|
|
clearchain(struct bootblock *boot, struct fatEntry *fat, cl_t head)
|
1996-05-14 21:39:21 +04:00
|
|
|
{
|
|
|
|
cl_t p, q;
|
|
|
|
|
|
|
|
for (p = head; p >= CLUST_FIRST && p < boot->NumClusters; p = q) {
|
|
|
|
if (fat[p].head != head)
|
|
|
|
break;
|
|
|
|
q = fat[p].next;
|
|
|
|
fat[p].next = fat[p].head = CLUST_FREE;
|
|
|
|
fat[p].length = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-01-22 21:48:43 +03:00
|
|
|
int
|
2005-01-19 23:00:45 +03:00
|
|
|
tryclear(struct bootblock *boot, struct fatEntry *fat, cl_t head, cl_t *truncp)
|
1998-01-22 21:48:43 +03:00
|
|
|
{
|
|
|
|
if (ask(0, "Clear chain starting at %u", head)) {
|
|
|
|
clearchain(boot, fat, head);
|
|
|
|
return FSFATMOD;
|
|
|
|
} else if (ask(0, "Truncate")) {
|
2014-07-07 21:45:42 +04:00
|
|
|
uint32_t len;
|
|
|
|
cl_t p;
|
|
|
|
|
|
|
|
for (p = head, len = 0;
|
|
|
|
p >= CLUST_FIRST && p < boot->NumClusters;
|
|
|
|
p = fat[p].next, len++)
|
|
|
|
continue;
|
2003-02-28 08:50:33 +03:00
|
|
|
*truncp = CLUST_EOF;
|
2014-07-07 21:45:42 +04:00
|
|
|
fat[head].length = len;
|
1998-01-22 21:48:43 +03:00
|
|
|
return FSFATMOD;
|
|
|
|
} else
|
|
|
|
return FSERROR;
|
|
|
|
}
|
|
|
|
|
1996-05-14 21:39:21 +04:00
|
|
|
/*
|
|
|
|
* Check a complete FAT in-memory for crosslinks
|
|
|
|
*/
|
|
|
|
int
|
2005-01-19 23:00:45 +03:00
|
|
|
checkfat(struct bootblock *boot, struct fatEntry *fat)
|
1996-05-14 21:39:21 +04:00
|
|
|
{
|
1998-01-22 21:48:43 +03:00
|
|
|
cl_t head, p, h, n;
|
1996-05-14 21:39:21 +04:00
|
|
|
u_int len;
|
|
|
|
int ret = 0;
|
|
|
|
int conf;
|
1997-10-17 15:19:23 +04:00
|
|
|
|
1996-05-14 21:39:21 +04:00
|
|
|
/*
|
|
|
|
* pass 1: figure out the cluster chains.
|
|
|
|
*/
|
|
|
|
for (head = CLUST_FIRST; head < boot->NumClusters; head++) {
|
1997-10-17 15:19:23 +04:00
|
|
|
/* find next untravelled chain */
|
1997-09-08 18:05:30 +04:00
|
|
|
if (fat[head].head != 0 /* cluster already belongs to some chain */
|
1997-01-03 17:32:47 +03:00
|
|
|
|| fat[head].next == CLUST_FREE
|
|
|
|
|| fat[head].next == CLUST_BAD)
|
1996-05-14 21:39:21 +04:00
|
|
|
continue; /* skip it. */
|
|
|
|
|
|
|
|
/* follow the chain and mark all clusters on the way */
|
|
|
|
for (len = 0, p = head;
|
2014-07-11 18:59:53 +04:00
|
|
|
p >= CLUST_FIRST && p < boot->NumClusters &&
|
|
|
|
fat[p].head != head;
|
1996-05-14 21:39:21 +04:00
|
|
|
p = fat[p].next) {
|
|
|
|
fat[p].head = head;
|
|
|
|
len++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* the head record gets the length */
|
1997-10-17 15:19:23 +04:00
|
|
|
fat[head].length = fat[head].next == CLUST_FREE ? 0 : len;
|
1996-05-14 21:39:21 +04:00
|
|
|
}
|
1997-10-17 15:19:23 +04:00
|
|
|
|
1996-05-14 21:39:21 +04:00
|
|
|
/*
|
|
|
|
* pass 2: check for crosslinked chains (we couldn't do this in pass 1 because
|
|
|
|
* we didn't know the real start of the chain then - would have treated partial
|
|
|
|
* chains as interlinked with their main chain)
|
|
|
|
*/
|
|
|
|
for (head = CLUST_FIRST; head < boot->NumClusters; head++) {
|
1997-10-17 15:19:23 +04:00
|
|
|
/* find next untravelled chain */
|
1996-05-14 21:39:21 +04:00
|
|
|
if (fat[head].head != head)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* follow the chain to its end (hopefully) */
|
2014-07-07 21:55:53 +04:00
|
|
|
for (len = fat[head].length, p = head;
|
1998-01-22 21:48:43 +03:00
|
|
|
(n = fat[p].next) >= CLUST_FIRST && n < boot->NumClusters;
|
|
|
|
p = n)
|
2014-07-07 21:55:53 +04:00
|
|
|
if (fat[n].head != head || len-- < 2)
|
1996-05-14 21:39:21 +04:00
|
|
|
break;
|
1998-01-22 21:48:43 +03:00
|
|
|
if (n >= CLUST_EOFS)
|
1996-05-14 21:39:21 +04:00
|
|
|
continue;
|
1997-10-17 15:19:23 +04:00
|
|
|
|
1998-01-22 21:48:43 +03:00
|
|
|
if (n == CLUST_FREE || n >= CLUST_RSRVD) {
|
1997-10-17 15:19:23 +04:00
|
|
|
pwarn("Cluster chain starting at %u ends with cluster marked %s\n",
|
1998-01-22 21:48:43 +03:00
|
|
|
head, rsrvdcltype(n));
|
2014-07-07 21:55:53 +04:00
|
|
|
clear:
|
1998-01-22 21:48:43 +03:00
|
|
|
ret |= tryclear(boot, fat, head, &fat[p].next);
|
1996-05-14 21:39:21 +04:00
|
|
|
continue;
|
|
|
|
}
|
1998-01-22 21:48:43 +03:00
|
|
|
if (n < CLUST_FIRST || n >= boot->NumClusters) {
|
1997-10-17 15:19:23 +04:00
|
|
|
pwarn("Cluster chain starting at %u ends with cluster out of range (%u)\n",
|
2014-07-07 21:55:53 +04:00
|
|
|
head, n);
|
|
|
|
goto clear;
|
|
|
|
}
|
|
|
|
if (head == fat[n].head) {
|
|
|
|
pwarn("Cluster chain starting at %u loops at cluster %u\n",
|
|
|
|
|
|
|
|
head, p);
|
|
|
|
goto clear;
|
1996-05-14 21:39:21 +04:00
|
|
|
}
|
1997-10-17 15:19:23 +04:00
|
|
|
pwarn("Cluster chains starting at %u and %u are linked at cluster %u\n",
|
1998-01-22 21:48:43 +03:00
|
|
|
head, fat[n].head, n);
|
|
|
|
conf = tryclear(boot, fat, head, &fat[p].next);
|
|
|
|
if (ask(0, "Clear chain starting at %u", h = fat[n].head)) {
|
1996-05-14 21:39:21 +04:00
|
|
|
if (conf == FSERROR) {
|
|
|
|
/*
|
|
|
|
* Transfer the common chain to the one not cleared above.
|
|
|
|
*/
|
1998-01-22 21:48:43 +03:00
|
|
|
for (p = n;
|
|
|
|
p >= CLUST_FIRST && p < boot->NumClusters;
|
1996-05-14 21:39:21 +04:00
|
|
|
p = fat[p].next) {
|
|
|
|
if (h != fat[p].head) {
|
|
|
|
/*
|
|
|
|
* Have to reexamine this chain.
|
|
|
|
*/
|
|
|
|
head--;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
fat[p].head = head;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
clearchain(boot, fat, h);
|
|
|
|
conf |= FSFATMOD;
|
|
|
|
}
|
|
|
|
ret |= conf;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write out FATs encoding them from the internal format
|
|
|
|
*/
|
|
|
|
int
|
2005-01-19 23:00:45 +03:00
|
|
|
writefat(int fs, struct bootblock *boot, struct fatEntry *fat, int correct_fat)
|
1996-05-14 21:39:21 +04:00
|
|
|
{
|
|
|
|
u_char *buffer, *p;
|
|
|
|
cl_t cl;
|
2009-04-11 10:59:18 +04:00
|
|
|
u_int i;
|
2006-06-06 20:19:11 +04:00
|
|
|
size_t fatsz;
|
1996-05-14 21:39:21 +04:00
|
|
|
off_t off;
|
|
|
|
int ret = FSOK;
|
1997-10-17 15:19:23 +04:00
|
|
|
|
1996-05-14 21:39:21 +04:00
|
|
|
buffer = malloc(fatsz = boot->FATsecs * boot->BytesPerSec);
|
|
|
|
if (buffer == NULL) {
|
2006-06-05 20:51:18 +04:00
|
|
|
perr("No space for FAT sectors (%zu)", fatsz);
|
1996-05-14 21:39:21 +04:00
|
|
|
return FSFATAL;
|
|
|
|
}
|
|
|
|
memset(buffer, 0, fatsz);
|
|
|
|
boot->NumFree = 0;
|
1997-09-08 18:05:30 +04:00
|
|
|
p = buffer;
|
2000-04-26 03:02:51 +04:00
|
|
|
if (correct_fat) {
|
|
|
|
*p++ = (u_char)boot->Media;
|
1997-10-17 15:19:23 +04:00
|
|
|
*p++ = 0xff;
|
|
|
|
*p++ = 0xff;
|
2000-04-26 03:02:51 +04:00
|
|
|
switch (boot->ClustMask) {
|
|
|
|
case CLUST16_MASK:
|
|
|
|
*p++ = 0xff;
|
|
|
|
break;
|
|
|
|
case CLUST32_MASK:
|
|
|
|
*p++ = 0x0f;
|
|
|
|
*p++ = 0xff;
|
|
|
|
*p++ = 0xff;
|
|
|
|
*p++ = 0xff;
|
|
|
|
*p++ = 0x0f;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* use same FAT signature as the old FAT has */
|
|
|
|
int count;
|
|
|
|
u_char *old_fat;
|
|
|
|
|
|
|
|
switch (boot->ClustMask) {
|
|
|
|
case CLUST32_MASK:
|
|
|
|
count = 8;
|
|
|
|
break;
|
|
|
|
case CLUST16_MASK:
|
|
|
|
count = 4;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
count = 3;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_readfat(fs, boot, boot->ValidFat >= 0 ? boot->ValidFat :0,
|
|
|
|
&old_fat)) {
|
|
|
|
free(buffer);
|
|
|
|
return FSFATAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(p, old_fat, count);
|
|
|
|
free(old_fat);
|
|
|
|
p += count;
|
1997-10-17 15:19:23 +04:00
|
|
|
}
|
2008-07-24 18:23:16 +04:00
|
|
|
|
1997-09-08 18:05:30 +04:00
|
|
|
for (cl = CLUST_FIRST; cl < boot->NumClusters; cl++) {
|
1997-10-17 15:19:23 +04:00
|
|
|
switch (boot->ClustMask) {
|
|
|
|
case CLUST32_MASK:
|
1996-05-14 21:39:21 +04:00
|
|
|
if (fat[cl].next == CLUST_FREE)
|
|
|
|
boot->NumFree++;
|
1997-10-17 15:19:23 +04:00
|
|
|
*p++ = (u_char)fat[cl].next;
|
|
|
|
*p++ = (u_char)(fat[cl].next >> 8);
|
|
|
|
*p++ = (u_char)(fat[cl].next >> 16);
|
|
|
|
*p &= 0xf0;
|
|
|
|
*p++ |= (fat[cl].next >> 24)&0x0f;
|
|
|
|
break;
|
|
|
|
case CLUST16_MASK:
|
|
|
|
if (fat[cl].next == CLUST_FREE)
|
|
|
|
boot->NumFree++;
|
|
|
|
*p++ = (u_char)fat[cl].next;
|
|
|
|
*p++ = (u_char)(fat[cl].next >> 8);
|
|
|
|
break;
|
|
|
|
default:
|
1996-05-14 21:39:21 +04:00
|
|
|
if (fat[cl].next == CLUST_FREE)
|
|
|
|
boot->NumFree++;
|
1997-10-17 15:19:23 +04:00
|
|
|
*p++ = (u_char)fat[cl].next;
|
2014-07-11 01:00:52 +04:00
|
|
|
*p = (u_char)((fat[cl].next >> 8) & 0xf);
|
2014-07-07 22:46:45 +04:00
|
|
|
cl++;
|
|
|
|
if (cl >= boot->NumClusters)
|
|
|
|
break;
|
|
|
|
if (fat[cl].next == CLUST_FREE)
|
|
|
|
boot->NumFree++;
|
2019-06-04 03:08:00 +03:00
|
|
|
*p++ |= (u_char)(fat[cl].next << 4);
|
|
|
|
*p++ = (u_char)(fat[cl].next >> 4);
|
1997-10-17 15:19:23 +04:00
|
|
|
break;
|
1996-05-14 21:39:21 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for (i = 0; i < boot->FATs; i++) {
|
|
|
|
off = boot->ResSectors + i * boot->FATsecs;
|
|
|
|
off *= boot->BytesPerSec;
|
|
|
|
if (lseek(fs, off, SEEK_SET) != off
|
2009-04-11 10:59:18 +04:00
|
|
|
|| (size_t)write(fs, buffer, fatsz) != fatsz) {
|
2006-06-05 20:51:18 +04:00
|
|
|
perr("Unable to write FAT");
|
1996-05-14 21:39:21 +04:00
|
|
|
ret = FSFATAL; /* Return immediately? XXX */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(buffer);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check a complete in-memory FAT for lost cluster chains
|
|
|
|
*/
|
|
|
|
int
|
2005-01-19 23:00:45 +03:00
|
|
|
checklost(int dosfs, struct bootblock *boot, struct fatEntry *fat)
|
1996-05-14 21:39:21 +04:00
|
|
|
{
|
|
|
|
cl_t head;
|
|
|
|
int mod = FSOK;
|
1997-10-17 15:19:23 +04:00
|
|
|
int ret;
|
2008-07-24 18:23:16 +04:00
|
|
|
|
1996-05-14 21:39:21 +04:00
|
|
|
for (head = CLUST_FIRST; head < boot->NumClusters; head++) {
|
1997-10-17 15:19:23 +04:00
|
|
|
/* find next untravelled chain */
|
1996-05-14 21:39:21 +04:00
|
|
|
if (fat[head].head != head
|
|
|
|
|| fat[head].next == CLUST_FREE
|
|
|
|
|| (fat[head].next >= CLUST_RSRVD
|
1996-05-28 23:51:11 +04:00
|
|
|
&& fat[head].next < CLUST_EOFS)
|
|
|
|
|| (fat[head].flags & FAT_USED))
|
1996-05-14 21:39:21 +04:00
|
|
|
continue;
|
|
|
|
|
1997-10-17 15:19:23 +04:00
|
|
|
pwarn("Lost cluster chain at cluster %u\n%d Cluster(s) lost\n",
|
1996-05-28 23:51:11 +04:00
|
|
|
head, fat[head].length);
|
1997-10-17 15:19:23 +04:00
|
|
|
mod |= ret = reconnect(dosfs, boot, fat, head);
|
1996-05-28 23:51:11 +04:00
|
|
|
if (mod & FSFATAL)
|
|
|
|
break;
|
1997-10-17 15:19:23 +04:00
|
|
|
if (ret == FSERROR && ask(0, "Clear")) {
|
|
|
|
clearchain(boot, fat, head);
|
|
|
|
mod |= FSFATMOD;
|
|
|
|
}
|
1996-05-14 21:39:21 +04:00
|
|
|
}
|
|
|
|
finishlf();
|
1997-10-17 15:19:23 +04:00
|
|
|
|
|
|
|
if (boot->FSInfo) {
|
|
|
|
ret = 0;
|
2012-08-07 23:30:41 +04:00
|
|
|
if (boot->FSFree != 0xffffffffU &&
|
|
|
|
boot->FSFree != boot->NumFree) {
|
|
|
|
pwarn("Free space in FSInfo block (%u) not correct (%u)\n",
|
1997-10-17 15:19:23 +04:00
|
|
|
boot->FSFree, boot->NumFree);
|
|
|
|
if (ask(1, "fix")) {
|
|
|
|
boot->FSFree = boot->NumFree;
|
|
|
|
ret = 1;
|
|
|
|
}
|
|
|
|
}
|
2013-01-17 20:45:48 +04:00
|
|
|
if (boot->FSNext != 0xffffffffU &&
|
|
|
|
(boot->FSNext >= boot->NumClusters ||
|
|
|
|
(boot->NumFree && fat[boot->FSNext].next != CLUST_FREE))) {
|
2008-07-24 18:23:16 +04:00
|
|
|
pwarn("Next free cluster in FSInfo block (%u) %s\n",
|
|
|
|
boot->FSNext,
|
|
|
|
(boot->FSNext >= boot->NumClusters) ? "invalid" : "not free");
|
1997-10-17 15:19:23 +04:00
|
|
|
if (ask(1, "fix"))
|
|
|
|
for (head = CLUST_FIRST; head < boot->NumClusters; head++)
|
|
|
|
if (fat[head].next == CLUST_FREE) {
|
|
|
|
boot->FSNext = head;
|
|
|
|
ret = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ret)
|
|
|
|
mod |= writefsinfo(dosfs, boot);
|
|
|
|
}
|
|
|
|
|
1996-05-14 21:39:21 +04:00
|
|
|
return mod;
|
|
|
|
}
|