2000-05-06 21:34:20 +02:00
|
|
|
/****************************************************************/
|
|
|
|
/* */
|
|
|
|
/* fattab.c */
|
|
|
|
/* */
|
|
|
|
/* FAT File System Table Functions */
|
|
|
|
/* */
|
|
|
|
/* Copyright (c) 1995 */
|
|
|
|
/* Pasquale J. Villani */
|
|
|
|
/* All Rights Reserved */
|
|
|
|
/* */
|
|
|
|
/* This file is part of DOS-C. */
|
|
|
|
/* */
|
|
|
|
/* DOS-C is free software; you can redistribute it and/or */
|
|
|
|
/* modify it under the terms of the GNU General Public License */
|
|
|
|
/* as published by the Free Software Foundation; either version */
|
|
|
|
/* 2, or (at your option) any later version. */
|
|
|
|
/* */
|
|
|
|
/* DOS-C is distributed in the hope that it will be useful, but */
|
|
|
|
/* WITHOUT ANY WARRANTY; without even the implied warranty of */
|
|
|
|
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See */
|
|
|
|
/* the GNU General Public License for more details. */
|
|
|
|
/* */
|
|
|
|
/* You should have received a copy of the GNU General Public */
|
|
|
|
/* License along with DOS-C; see the file COPYING. If not, */
|
|
|
|
/* write to the Free Software Foundation, 675 Mass Ave, */
|
|
|
|
/* Cambridge, MA 02139, USA. */
|
|
|
|
/****************************************************************/
|
|
|
|
|
|
|
|
#include "portab.h"
|
|
|
|
#include "globals.h"
|
|
|
|
|
|
|
|
#ifdef VERSION_STRINGS
|
2001-11-18 15:01:12 +01:00
|
|
|
static BYTE *RcsId =
|
|
|
|
"$Id$";
|
2000-05-06 21:34:20 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/************************************************************************/
|
|
|
|
/* */
|
|
|
|
/* cluster/sector routines */
|
|
|
|
/* */
|
|
|
|
/************************************************************************/
|
|
|
|
|
2001-09-23 22:39:44 +02:00
|
|
|
#ifndef ISFAT32
|
2001-11-18 15:01:12 +01:00
|
|
|
int ISFAT32(struct dpb FAR * dpbp)
|
2001-09-23 22:39:44 +02:00
|
|
|
{
|
2001-11-18 15:01:12 +01:00
|
|
|
return _ISFAT32(dpbp);
|
2001-09-23 22:39:44 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2002-08-03 04:25:17 +02:00
|
|
|
struct buffer FAR *getFATblock(ULONG clussec, struct dpb FAR * dpbp)
|
2001-06-03 16:16:18 +02:00
|
|
|
{
|
|
|
|
struct buffer FAR *bp;
|
2001-09-23 22:39:44 +02:00
|
|
|
|
2001-06-03 16:16:18 +02:00
|
|
|
if (ISFAT12(dpbp))
|
2001-11-18 15:01:12 +01:00
|
|
|
{
|
2002-08-03 04:25:17 +02:00
|
|
|
clussec = (((unsigned)clussec << 1) + (unsigned)clussec) >> 1;
|
2001-11-18 15:01:12 +01:00
|
|
|
}
|
2001-09-23 22:39:44 +02:00
|
|
|
#ifdef WITHFAT32
|
|
|
|
else if (ISFAT32(dpbp))
|
2001-11-18 15:01:12 +01:00
|
|
|
{
|
2002-08-03 04:25:17 +02:00
|
|
|
clussec = clussec * SIZEOF_CLST32;
|
2001-11-18 15:01:12 +01:00
|
|
|
}
|
2001-09-23 22:39:44 +02:00
|
|
|
#endif
|
|
|
|
else
|
2001-11-18 15:01:12 +01:00
|
|
|
{
|
2002-08-03 04:25:17 +02:00
|
|
|
clussec = clussec * SIZEOF_CLST16;
|
2001-11-18 15:01:12 +01:00
|
|
|
}
|
2002-08-03 04:25:17 +02:00
|
|
|
clussec = clussec / dpbp->dpb_secsize + dpbp->dpb_fatstrt;
|
2001-09-23 22:39:44 +02:00
|
|
|
#ifdef WITHFAT32
|
2001-11-18 15:01:12 +01:00
|
|
|
if (ISFAT32(dpbp) && (dpbp->dpb_xflags & FAT_NO_MIRRORING))
|
|
|
|
{
|
2001-09-23 22:39:44 +02:00
|
|
|
/* we must modify the active fat,
|
|
|
|
it's number is in the 0-3 bits of dpb_xflags */
|
2002-08-03 04:25:17 +02:00
|
|
|
clussec += (dpbp->dpb_xflags & 0xf) * dpbp->dpb_xfatsize;
|
2001-09-23 22:39:44 +02:00
|
|
|
}
|
|
|
|
#endif
|
2001-11-18 15:01:12 +01:00
|
|
|
|
2002-08-03 04:25:17 +02:00
|
|
|
bp = getblock(clussec, dpbp->dpb_unit);
|
2001-11-18 15:01:12 +01:00
|
|
|
|
2001-06-03 16:16:18 +02:00
|
|
|
if (bp)
|
2001-11-04 20:47:39 +01:00
|
|
|
{
|
|
|
|
bp->b_flag &= ~(BFR_DATA | BFR_DIR);
|
|
|
|
bp->b_flag |= BFR_FAT | BFR_VALID;
|
2002-08-03 04:25:17 +02:00
|
|
|
bp->b_dpbp = dpbp;
|
2002-12-09 01:17:15 +01:00
|
|
|
bp->b_copies = dpbp->dpb_fats;
|
|
|
|
bp->b_offset = dpbp->dpb_fatsize;
|
|
|
|
#ifdef WITHFAT32
|
|
|
|
if (ISFAT32(dpbp))
|
|
|
|
{
|
|
|
|
if (dpbp->dpb_xflags & FAT_NO_MIRRORING)
|
|
|
|
bp->b_copies = 1;
|
|
|
|
}
|
|
|
|
#endif
|
2001-11-04 20:47:39 +01:00
|
|
|
}
|
2001-11-18 15:01:12 +01:00
|
|
|
return bp;
|
2001-06-03 16:16:18 +02:00
|
|
|
}
|
2001-09-23 22:39:44 +02:00
|
|
|
|
|
|
|
#ifdef WITHFAT32
|
2001-11-18 15:01:12 +01:00
|
|
|
void read_fsinfo(struct dpb FAR * dpbp)
|
2001-09-23 22:39:44 +02:00
|
|
|
{
|
|
|
|
struct buffer FAR *bp;
|
|
|
|
struct fsinfo FAR *fip;
|
|
|
|
|
|
|
|
bp = getblock(dpbp->dpb_xfsinfosec, dpbp->dpb_unit);
|
|
|
|
bp->b_flag &= ~(BFR_DATA | BFR_DIR | BFR_FAT | BFR_DIRTY);
|
|
|
|
bp->b_flag |= BFR_VALID;
|
|
|
|
|
2001-11-18 15:01:12 +01:00
|
|
|
fip = (struct fsinfo FAR *)&bp->b_buffer[0x1e4];
|
2001-11-04 20:47:39 +01:00
|
|
|
dpbp->dpb_xnfreeclst = fip->fi_nfreeclst;
|
|
|
|
dpbp->dpb_xcluster = fip->fi_cluster;
|
2001-09-23 22:39:44 +02:00
|
|
|
}
|
|
|
|
|
2001-11-18 15:01:12 +01:00
|
|
|
void write_fsinfo(struct dpb FAR * dpbp)
|
2001-09-23 22:39:44 +02:00
|
|
|
{
|
|
|
|
struct buffer FAR *bp;
|
|
|
|
struct fsinfo FAR *fip;
|
|
|
|
|
|
|
|
bp = getblock(dpbp->dpb_xfsinfosec, dpbp->dpb_unit);
|
|
|
|
bp->b_flag &= ~(BFR_DATA | BFR_DIR | BFR_FAT);
|
|
|
|
bp->b_flag |= BFR_VALID | BFR_DIRTY;
|
|
|
|
|
2001-11-18 15:01:12 +01:00
|
|
|
fip = (struct fsinfo FAR *)&bp->b_buffer[0x1e4];
|
2001-11-04 20:47:39 +01:00
|
|
|
fip->fi_nfreeclst = dpbp->dpb_xnfreeclst;
|
|
|
|
fip->fi_cluster = dpbp->dpb_xcluster;
|
2001-09-23 22:39:44 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2000-05-06 21:34:20 +02:00
|
|
|
/* */
|
|
|
|
/* The FAT file system is difficult to trace through FAT table. */
|
|
|
|
/* There are two kinds of FAT's, 12 bit and 16 bit. The 16 bit */
|
|
|
|
/* FAT is the easiest, since it is noting more than a series of */
|
|
|
|
/* UWORD's. The 12 bit FAT is difficult, because it packs 3 FAT */
|
|
|
|
/* entries into two BYTE's. The are packed as follows: */
|
|
|
|
/* */
|
|
|
|
/* 0x0003 0x0004 0x0005 0x0006 0x0007 0x0008 0x0009 ... */
|
|
|
|
/* */
|
|
|
|
/* are packed as */
|
|
|
|
/* */
|
|
|
|
/* 0x03 0x40 0x00 0x05 0x60 0x00 0x07 0x80 0x00 0x09 ... */
|
|
|
|
/* */
|
|
|
|
/* 12 bytes are compressed to 9 bytes */
|
|
|
|
/* */
|
|
|
|
|
2002-05-09 00:49:35 +02:00
|
|
|
unsigned link_fat(struct dpb FAR * dpbp, CLUSTER Cluster1,
|
2001-11-18 15:01:12 +01:00
|
|
|
REG CLUSTER Cluster2)
|
2000-05-06 21:34:20 +02:00
|
|
|
{
|
2002-08-03 04:25:17 +02:00
|
|
|
struct buffer FAR *bp;
|
|
|
|
|
|
|
|
/* Get the block that this cluster is in */
|
|
|
|
bp = getFATblock(Cluster1, dpbp);
|
|
|
|
|
|
|
|
if (bp == NULL)
|
|
|
|
return DE_BLKINVLD;
|
2001-11-18 15:01:12 +01:00
|
|
|
|
2000-05-06 21:34:20 +02:00
|
|
|
if (ISFAT12(dpbp))
|
2002-08-03 04:25:17 +02:00
|
|
|
{
|
|
|
|
unsigned idx;
|
|
|
|
REG UBYTE FAR *fbp0, FAR * fbp1;
|
|
|
|
struct buffer FAR * bp1;
|
|
|
|
|
|
|
|
/* form an index so that we can read the block as a */
|
|
|
|
/* byte array */
|
|
|
|
idx = (unsigned) (((Cluster1 << 1) + Cluster1) >> 1) % dpbp->dpb_secsize;
|
|
|
|
|
|
|
|
/* Test to see if the cluster straddles the block. If */
|
|
|
|
/* it does, get the next block and use both to form the */
|
|
|
|
/* the FAT word. Otherwise, just point to the next */
|
|
|
|
/* block. */
|
|
|
|
fbp0 = &bp->b_buffer[idx];
|
|
|
|
fbp1 = fbp0 + 1;
|
|
|
|
|
|
|
|
if (idx >= (unsigned)dpbp->dpb_secsize - 1)
|
|
|
|
{
|
|
|
|
bp1 = getFATblock(Cluster1 + 1, dpbp);
|
|
|
|
if (bp1 == 0)
|
|
|
|
return DE_BLKINVLD;
|
|
|
|
|
|
|
|
bp1->b_flag |= BFR_DIRTY | BFR_VALID;
|
|
|
|
|
|
|
|
fbp1 = &bp1->b_buffer[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now pack the value in */
|
|
|
|
if (Cluster1 & 0x01)
|
|
|
|
{
|
|
|
|
*fbp0 = (*fbp0 & 0x0f) | ((Cluster2 & 0x0f) << 4);
|
|
|
|
*fbp1 = (Cluster2 >> 4) & 0xff;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*fbp0 = Cluster2 & 0xff;
|
|
|
|
*fbp1 = (*fbp1 & 0xf0) | ((Cluster2 >> 8) & 0x0f);
|
|
|
|
}
|
|
|
|
}
|
2003-02-20 21:51:42 +01:00
|
|
|
else if (ISFAT16(dpbp))
|
2002-08-03 04:25:17 +02:00
|
|
|
{
|
|
|
|
/* form an index so that we can read the block as a */
|
|
|
|
/* byte array */
|
|
|
|
/* Finally, put the word into the buffer and mark the */
|
|
|
|
/* buffer as dirty. */
|
|
|
|
fputword(
|
|
|
|
&bp->b_buffer[((unsigned)Cluster1 * SIZEOF_CLST16) % dpbp->dpb_secsize],
|
|
|
|
(UWORD)Cluster2);
|
|
|
|
}
|
2001-09-23 22:39:44 +02:00
|
|
|
#ifdef WITHFAT32
|
|
|
|
else if (ISFAT32(dpbp))
|
2002-08-03 04:25:17 +02:00
|
|
|
{
|
|
|
|
/* form an index so that we can read the block as a */
|
|
|
|
/* byte array */
|
|
|
|
/* Finally, put the word into the buffer and mark the */
|
|
|
|
/* buffer as dirty. */
|
|
|
|
fputlong(
|
|
|
|
&bp->b_buffer[(UWORD) ((Cluster1 * SIZEOF_CLST32) % dpbp->dpb_secsize)],
|
|
|
|
Cluster2);
|
|
|
|
}
|
2001-09-23 22:39:44 +02:00
|
|
|
#endif
|
2000-05-06 21:34:20 +02:00
|
|
|
else
|
|
|
|
return DE_BLKINVLD;
|
2001-07-10 00:19:33 +02:00
|
|
|
|
2001-09-23 22:39:44 +02:00
|
|
|
/* update the free space count */
|
2002-08-03 04:25:17 +02:00
|
|
|
bp->b_flag |= BFR_DIRTY | BFR_VALID;
|
|
|
|
if (Cluster2 == FREE)
|
2001-11-04 20:47:39 +01:00
|
|
|
{
|
|
|
|
#ifdef WITHFAT32
|
|
|
|
if (ISFAT32(dpbp) && dpbp->dpb_xnfreeclst != XUNKNCLSTFREE)
|
|
|
|
{
|
|
|
|
/* update the free space count for returned */
|
2002-08-03 04:25:17 +02:00
|
|
|
/* cluster */
|
2001-11-04 20:47:39 +01:00
|
|
|
++dpbp->dpb_xnfreeclst;
|
|
|
|
write_fsinfo(dpbp);
|
2001-11-18 15:01:12 +01:00
|
|
|
}
|
|
|
|
else
|
2001-11-04 20:47:39 +01:00
|
|
|
#endif
|
2001-07-10 00:19:33 +02:00
|
|
|
if (dpbp->dpb_nfreeclst != UNKNCLSTFREE)
|
2001-11-04 20:47:39 +01:00
|
|
|
++dpbp->dpb_nfreeclst;
|
|
|
|
}
|
|
|
|
|
2001-11-18 15:01:12 +01:00
|
|
|
/*if (Cluster2 == FREE)
|
|
|
|
{ */
|
|
|
|
/* update the free space count for returned */
|
|
|
|
/* cluster */
|
|
|
|
/* ++dpbp->dpb_nfreeclst;
|
|
|
|
} */
|
|
|
|
|
|
|
|
/* update the free space count for removed */
|
|
|
|
/* cluster */
|
|
|
|
/* BUG: was counted twice for 2nd,.. cluster. moved to find_fat_free() */
|
|
|
|
/* BO: don't completely understand this yet - leave here for now as
|
|
|
|
a comment */
|
|
|
|
/* else
|
|
|
|
{
|
|
|
|
--dpbp->dpb_nfreeclst;
|
|
|
|
} */
|
2000-05-06 21:34:20 +02:00
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Given the disk parameters, and a cluster number, this function
|
|
|
|
looks at the FAT, and returns the next cluster in the clain. */
|
2001-11-18 15:01:12 +01:00
|
|
|
CLUSTER next_cluster(struct dpb FAR * dpbp, CLUSTER ClusterNum)
|
2001-09-23 22:39:44 +02:00
|
|
|
{
|
|
|
|
struct buffer FAR *bp;
|
2001-11-14 00:36:45 +01:00
|
|
|
#ifdef DEBUG
|
2001-11-18 15:01:12 +01:00
|
|
|
if (ClusterNum == LONG_LAST_CLUSTER)
|
|
|
|
printf("fatal error: trying to do next_cluster(dpbp, EOC)!\n");
|
|
|
|
if (ClusterNum == 0)
|
|
|
|
printf("fatal error: trying to do next_cluster(dpbp, 0)!\n");
|
2001-11-14 00:36:45 +01:00
|
|
|
#endif
|
2001-09-23 22:39:44 +02:00
|
|
|
|
|
|
|
/* Get the block that this cluster is in */
|
|
|
|
bp = getFATblock(ClusterNum, dpbp);
|
|
|
|
|
|
|
|
if (bp == NULL)
|
2002-08-03 18:54:09 +02:00
|
|
|
return 1; /* the only error code possible here */
|
2001-09-23 22:39:44 +02:00
|
|
|
|
2001-11-04 20:47:39 +01:00
|
|
|
if (ISFAT12(dpbp))
|
|
|
|
{
|
|
|
|
union {
|
2001-11-18 15:01:12 +01:00
|
|
|
UBYTE bytes[2];
|
2002-05-09 00:49:35 +02:00
|
|
|
unsigned word;
|
2001-11-04 20:47:39 +01:00
|
|
|
} clusterbuff;
|
2001-11-18 15:01:12 +01:00
|
|
|
|
2002-05-09 00:49:35 +02:00
|
|
|
unsigned idx;
|
2001-06-03 16:16:18 +02:00
|
|
|
|
2001-11-04 20:47:39 +01:00
|
|
|
/* form an index so that we can read the block as a */
|
|
|
|
/* byte array */
|
2002-05-09 00:49:35 +02:00
|
|
|
idx = ((((unsigned)ClusterNum << 1) + (unsigned)ClusterNum) >> 1) %
|
|
|
|
dpbp->dpb_secsize;
|
2001-04-22 00:32:53 +02:00
|
|
|
|
2001-11-04 20:47:39 +01:00
|
|
|
clusterbuff.bytes[0] = bp->b_buffer[idx];
|
2001-04-22 00:32:53 +02:00
|
|
|
|
2001-11-18 15:01:12 +01:00
|
|
|
clusterbuff.bytes[1] = bp->b_buffer[idx + 1]; /* next byte, will be overwritten,
|
|
|
|
if not valid */
|
2000-05-06 21:34:20 +02:00
|
|
|
|
2001-11-04 20:47:39 +01:00
|
|
|
/* Test to see if the cluster straddles the block. If it */
|
|
|
|
/* does, get the next block and use both to form the */
|
|
|
|
/* the FAT word. Otherwise, just point to the next */
|
|
|
|
/* block. */
|
2002-08-03 04:25:17 +02:00
|
|
|
if (idx >= (unsigned)dpbp->dpb_secsize - 1)
|
2001-11-04 20:47:39 +01:00
|
|
|
{
|
2001-11-18 15:01:12 +01:00
|
|
|
bp = getFATblock(ClusterNum + 1, dpbp);
|
2000-05-06 21:34:20 +02:00
|
|
|
|
2001-11-04 20:47:39 +01:00
|
|
|
if (bp == 0)
|
|
|
|
return LONG_BAD;
|
2001-09-23 22:39:44 +02:00
|
|
|
|
2001-11-04 20:47:39 +01:00
|
|
|
clusterbuff.bytes[1] = bp->b_buffer[0];
|
|
|
|
}
|
2000-05-06 21:34:20 +02:00
|
|
|
|
2001-11-04 20:47:39 +01:00
|
|
|
/* Now to unpack the contents of the FAT entry. Odd and */
|
|
|
|
/* even bytes are packed differently. */
|
2001-11-18 15:01:12 +01:00
|
|
|
|
|
|
|
#ifndef I86 /* the latter assumes byte ordering */
|
2001-11-04 20:47:39 +01:00
|
|
|
if (ClusterNum & 0x01)
|
2001-11-18 15:01:12 +01:00
|
|
|
idx =
|
2002-08-03 04:25:17 +02:00
|
|
|
((clusterbuff.bytes[0] & 0xf0) >> 4) | (clusterbuff.bytes[1] << 4);
|
2001-11-04 20:47:39 +01:00
|
|
|
else
|
2002-08-03 04:25:17 +02:00
|
|
|
idx = clusterbuff.bytes[0] | ((clusterbuff.bytes[1] & 0x0f) << 8);
|
2001-11-04 20:47:39 +01:00
|
|
|
#else
|
2001-11-18 15:01:12 +01:00
|
|
|
|
2001-11-04 20:47:39 +01:00
|
|
|
if (ClusterNum & 0x01)
|
|
|
|
idx = (unsigned short)clusterbuff.word >> 4;
|
|
|
|
else
|
2001-11-18 15:01:12 +01:00
|
|
|
idx = clusterbuff.word & 0x0fff;
|
|
|
|
#endif
|
2001-06-03 16:16:18 +02:00
|
|
|
|
2001-11-04 20:47:39 +01:00
|
|
|
if (idx >= MASK12)
|
|
|
|
return LONG_LAST_CLUSTER;
|
|
|
|
if (idx == BAD12)
|
2000-05-06 21:34:20 +02:00
|
|
|
return LONG_BAD;
|
2001-11-04 20:47:39 +01:00
|
|
|
return idx;
|
2000-05-06 21:34:20 +02:00
|
|
|
}
|
2003-02-20 21:51:42 +01:00
|
|
|
else if (ISFAT16(dpbp))
|
2001-11-18 15:01:12 +01:00
|
|
|
{
|
2001-11-04 20:47:39 +01:00
|
|
|
UWORD res;
|
2000-05-06 21:34:20 +02:00
|
|
|
|
2001-11-04 20:47:39 +01:00
|
|
|
/* form an index so that we can read the block as a */
|
|
|
|
/* byte array */
|
2002-05-09 00:49:35 +02:00
|
|
|
/* and get the cluster number */
|
2001-11-18 15:01:12 +01:00
|
|
|
|
2002-05-09 00:49:35 +02:00
|
|
|
res = fgetword(&bp->b_buffer[((unsigned)ClusterNum * SIZEOF_CLST16) %
|
|
|
|
dpbp->dpb_secsize]);
|
2001-07-10 00:19:33 +02:00
|
|
|
|
2001-11-18 15:01:12 +01:00
|
|
|
if (res >= MASK16)
|
|
|
|
return LONG_LAST_CLUSTER;
|
|
|
|
if (res == BAD16)
|
|
|
|
return LONG_BAD;
|
2001-07-10 00:19:33 +02:00
|
|
|
|
2001-11-04 20:47:39 +01:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
#ifdef WITHFAT32
|
|
|
|
else if (ISFAT32(dpbp))
|
2001-07-10 00:19:33 +02:00
|
|
|
{
|
2001-11-04 20:47:39 +01:00
|
|
|
UDWORD res;
|
2001-07-10 00:19:33 +02:00
|
|
|
|
2002-05-09 00:49:35 +02:00
|
|
|
res = fgetlong(&bp->b_buffer[((unsigned)ClusterNum * SIZEOF_CLST32) %
|
|
|
|
dpbp->dpb_secsize]);
|
2001-11-18 15:01:12 +01:00
|
|
|
if (res > LONG_BAD)
|
|
|
|
return LONG_LAST_CLUSTER;
|
2001-07-10 00:19:33 +02:00
|
|
|
|
2001-11-18 15:01:12 +01:00
|
|
|
return res;
|
2001-07-10 00:19:33 +02:00
|
|
|
}
|
|
|
|
#endif
|
2001-11-04 20:47:39 +01:00
|
|
|
return LONG_LAST_CLUSTER;
|
|
|
|
}
|
|
|
|
|