summaryrefslogtreecommitdiffstats
path: root/firmware/drivers/fat.c
diff options
context:
space:
mode:
Diffstat (limited to 'firmware/drivers/fat.c')
-rw-r--r--firmware/drivers/fat.c159
1 files changed, 59 insertions, 100 deletions
diff --git a/firmware/drivers/fat.c b/firmware/drivers/fat.c
index 004e2a78ca..f9d116eba8 100644
--- a/firmware/drivers/fat.c
+++ b/firmware/drivers/fat.c
@@ -109,9 +109,9 @@
#define FATLONG_TYPE 12
#define FATLONG_CHKSUM 13
-#define CLUSTERS_PER_FAT_SECTOR ((unsigned int)(fat_bpb->bpb_bytspersec / 4))
-#define CLUSTERS_PER_FAT16_SECTOR ((unsigned int)(fat_bpb->bpb_bytspersec / 2))
-#define DIR_ENTRIES_PER_SECTOR ((unsigned int)(fat_bpb->bpb_bytspersec / DIR_ENTRY_SIZE))
+#define CLUSTERS_PER_FAT_SECTOR (SECTOR_SIZE / 4)
+#define CLUSTERS_PER_FAT16_SECTOR (SECTOR_SIZE / 2)
+#define DIR_ENTRIES_PER_SECTOR (SECTOR_SIZE / DIR_ENTRY_SIZE)
#define DIR_ENTRY_SIZE 32
#define NAME_BYTES_PER_ENTRY 13
#define FAT_BAD_MARK 0x0ffffff7
@@ -128,6 +128,9 @@ struct fsinfo {
#define FSINFO_FREECOUNT 488
#define FSINFO_NEXTFREE 492
+/* Note: This struct doesn't hold the raw values after mounting if
+ * bpb_bytspersec isn't 512. All sector counts are normalized to 512 byte
+ * physical sectors. */
struct bpb
{
int bpb_bytspersec; /* Bytes per sector, typically 512 */
@@ -165,8 +168,6 @@ struct bpb
int drive; /* on which physical device is this located */
bool mounted; /* flag if this volume is mounted */
#endif
-
- int secmult; /* bpb_bytspersec / PHYSICAL_SECTOR_SIZE */
};
static struct bpb fat_bpbs[NUM_VOLUMES]; /* mounted partition info */
@@ -193,7 +194,7 @@ struct fat_cache_entry
#endif
};
-static char fat_cache_sectors[FAT_CACHE_SIZE][MAX_SECTOR_SIZE];
+static char fat_cache_sectors[FAT_CACHE_SIZE][SECTOR_SIZE];
static struct fat_cache_entry fat_cache[FAT_CACHE_SIZE];
static struct mutex cache_mutex;
@@ -235,11 +236,9 @@ void fat_size(IF_MV2(int volume,) unsigned long* size, unsigned long* free)
#endif
struct bpb* fat_bpb = &fat_bpbs[volume];
if (size)
- *size = (fat_bpb->dataclusters * fat_bpb->bpb_secperclus
- * fat_bpb->secmult) / 2;
+ *size = fat_bpb->dataclusters * fat_bpb->bpb_secperclus / 2;
if (free)
- *free = (fat_bpb->fsinfo.freecount * fat_bpb->bpb_secperclus
- * fat_bpb->secmult) / 2;
+ *free = fat_bpb->fsinfo.freecount * fat_bpb->bpb_secperclus / 2;
}
void fat_init(void)
@@ -273,8 +272,9 @@ int fat_mount(IF_MV2(int volume,) IF_MV2(int drive,) long startsector)
const int volume = 0;
#endif
struct bpb* fat_bpb = &fat_bpbs[volume];
- unsigned char buf[MAX_SECTOR_SIZE];
+ unsigned char buf[SECTOR_SIZE];
int rc;
+ int secmult;
long datasec;
#ifdef HAVE_FAT16SUPPORT
int rootdirsectors;
@@ -295,19 +295,20 @@ int fat_mount(IF_MV2(int volume,) IF_MV2(int drive,) long startsector)
#endif
fat_bpb->bpb_bytspersec = BYTES2INT16(buf,BPB_BYTSPERSEC);
- fat_bpb->bpb_secperclus = buf[BPB_SECPERCLUS];
- fat_bpb->bpb_rsvdseccnt = BYTES2INT16(buf,BPB_RSVDSECCNT);
+ secmult = fat_bpb->bpb_bytspersec / SECTOR_SIZE;
+ /* Sanity check is performed later */
+
+ fat_bpb->bpb_secperclus = secmult * buf[BPB_SECPERCLUS];
+ fat_bpb->bpb_rsvdseccnt = secmult * BYTES2INT16(buf,BPB_RSVDSECCNT);
fat_bpb->bpb_numfats = buf[BPB_NUMFATS];
- fat_bpb->bpb_totsec16 = BYTES2INT16(buf,BPB_TOTSEC16);
fat_bpb->bpb_media = buf[BPB_MEDIA];
- fat_bpb->bpb_fatsz16 = BYTES2INT16(buf,BPB_FATSZ16);
- fat_bpb->bpb_fatsz32 = BYTES2INT32(buf,BPB_FATSZ32);
- fat_bpb->bpb_totsec32 = BYTES2INT32(buf,BPB_TOTSEC32);
+ fat_bpb->bpb_fatsz16 = secmult * BYTES2INT16(buf,BPB_FATSZ16);
+ fat_bpb->bpb_fatsz32 = secmult * BYTES2INT32(buf,BPB_FATSZ32);
+ fat_bpb->bpb_totsec16 = secmult * BYTES2INT16(buf,BPB_TOTSEC16);
+ fat_bpb->bpb_totsec32 = secmult * BYTES2INT32(buf,BPB_TOTSEC32);
fat_bpb->last_word = BYTES2INT16(buf,BPB_LAST_WORD);
/* calculate a few commonly used values */
- fat_bpb->secmult = fat_bpb->bpb_bytspersec / PHYSICAL_SECTOR_SIZE;
-
if (fat_bpb->bpb_fatsz16 != 0)
fat_bpb->fatsize = fat_bpb->bpb_fatsz16;
else
@@ -320,8 +321,8 @@ int fat_mount(IF_MV2(int volume,) IF_MV2(int drive,) long startsector)
#ifdef HAVE_FAT16SUPPORT
fat_bpb->bpb_rootentcnt = BYTES2INT16(buf,BPB_ROOTENTCNT);
- rootdirsectors = ((fat_bpb->bpb_rootentcnt * 32)
- + (fat_bpb->bpb_bytspersec - 1)) / fat_bpb->bpb_bytspersec;
+ rootdirsectors = secmult * ((fat_bpb->bpb_rootentcnt * DIR_ENTRY_SIZE
+ + fat_bpb->bpb_bytspersec - 1) / fat_bpb->bpb_bytspersec);
#endif /* #ifdef HAVE_FAT16SUPPORT */
fat_bpb->firstdatasector = fat_bpb->bpb_rsvdseccnt
@@ -375,7 +376,7 @@ int fat_mount(IF_MV2(int volume,) IF_MV2(int drive,) long startsector)
#endif /* #ifdef HAVE_FAT16SUPPORT */
{ /* FAT32 specific part of BPB */
fat_bpb->bpb_rootclus = BYTES2INT32(buf,BPB_ROOTCLUS);
- fat_bpb->bpb_fsinfo = BYTES2INT16(buf,BPB_FSINFO);
+ fat_bpb->bpb_fsinfo = secmult * BYTES2INT16(buf,BPB_FSINFO);
fat_bpb->rootdirsector = cluster2sec(IF_MV2(fat_bpb,) fat_bpb->bpb_rootclus);
}
@@ -397,7 +398,7 @@ int fat_mount(IF_MV2(int volume,) IF_MV2(int drive,) long startsector)
{
/* Read the fsinfo sector */
rc = ata_read_sectors(IF_MV2(drive,)
- startsector + (fat_bpb->bpb_fsinfo * fat_bpb->secmult), 1, buf);
+ startsector + fat_bpb->bpb_fsinfo, 1, buf);
if (rc < 0)
{
DEBUGF( "fat_mount() - Couldn't read FSInfo (error code %d)\n", rc);
@@ -512,15 +513,12 @@ static int bpb_is_sane(IF_MV_NONVOID(struct bpb* fat_bpb))
#ifndef HAVE_MULTIVOLUME
struct bpb* fat_bpb = &fat_bpbs[0];
#endif
- if(fat_bpb->bpb_bytspersec > MAX_SECTOR_SIZE
- || fat_bpb->bpb_bytspersec < PHYSICAL_SECTOR_SIZE
- || fat_bpb->bpb_bytspersec % PHYSICAL_SECTOR_SIZE)
+ if(fat_bpb->bpb_bytspersec % SECTOR_SIZE)
{
DEBUGF( "bpb_is_sane() - Error: sector size is not sane (%d)\n",
fat_bpb->bpb_bytspersec);
return -1;
}
-
if((long)fat_bpb->bpb_secperclus * (long)fat_bpb->bpb_bytspersec > 128L*1024L)
{
DEBUGF( "bpb_is_sane() - Error: cluster size is larger than 128K "
@@ -564,20 +562,17 @@ static void flush_fat_sector(struct fat_cache_entry *fce,
{
int rc;
long secnum;
- int secmult;
/* With multivolume, use only the FAT info from the cached sector! */
#ifdef HAVE_MULTIVOLUME
- secmult = fce->fat_vol->secmult;
- secnum = (fce->secnum * secmult) + fce->fat_vol->startsector;
+ secnum = fce->secnum + fce->fat_vol->startsector;
#else
- secmult = fat_bpbs[0].secmult;
- secnum = (fce->secnum * secmult) + fat_bpbs[0].startsector;
+ secnum = fce->secnum + fat_bpbs[0].startsector;
#endif
/* Write to the first FAT */
rc = ata_write_sectors(IF_MV2(fce->fat_vol->drive,)
- secnum, secmult,
+ secnum, 1,
sectorbuf);
if(rc < 0)
{
@@ -593,12 +588,12 @@ static void flush_fat_sector(struct fat_cache_entry *fce,
{
/* Write to the second FAT */
#ifdef HAVE_MULTIVOLUME
- secnum += fce->fat_vol->fatsize * secmult;
+ secnum += fce->fat_vol->fatsize;
#else
- secnum += fat_bpbs[0].fatsize * secmult;
+ secnum += fat_bpbs[0].fatsize;
#endif
rc = ata_write_sectors(IF_MV2(fce->fat_vol->drive,)
- secnum, secmult, sectorbuf);
+ secnum, 1, sectorbuf);
if(rc < 0)
{
panicf("flush_fat_sector() - Could not write sector %ld"
@@ -644,8 +639,8 @@ static void *cache_fat_sector(IF_MV2(struct bpb* fat_bpb,)
if(!fce->inuse)
{
rc = ata_read_sectors(IF_MV2(fat_bpb->drive,)
- (secnum * fat_bpb->secmult) + fat_bpb->startsector,
- fat_bpb->secmult, sectorbuf);
+ secnum + fat_bpb->startsector,1,
+ sectorbuf);
if(rc < 0)
{
DEBUGF( "cache_fat_sector() - Could not read sector %ld"
@@ -821,10 +816,10 @@ static int update_fat_entry(IF_MV2(struct bpb* fat_bpb,) unsigned long entry, un
static long read_fat_entry(IF_MV2(struct bpb* fat_bpb,) unsigned long entry)
{
+#ifdef HAVE_FAT16SUPPORT
#ifndef HAVE_MULTIVOLUME
struct bpb* fat_bpb = &fat_bpbs[0];
#endif
-#ifdef HAVE_FAT16SUPPORT
if (fat_bpb->is_fat16)
{
int sector = entry / CLUSTERS_PER_FAT16_SECTOR;
@@ -888,7 +883,7 @@ static int update_fsinfo(IF_MV_NONVOID(struct bpb* fat_bpb))
#ifndef HAVE_MULTIVOLUME
struct bpb* fat_bpb = &fat_bpbs[0];
#endif
- unsigned char fsinfo[MAX_SECTOR_SIZE];
+ unsigned char fsinfo[SECTOR_SIZE];
unsigned long* intptr;
int rc;
@@ -899,8 +894,7 @@ static int update_fsinfo(IF_MV_NONVOID(struct bpb* fat_bpb))
/* update fsinfo */
rc = ata_read_sectors(IF_MV2(fat_bpb->drive,)
- fat_bpb->startsector +
- (fat_bpb->bpb_fsinfo * fat_bpb->secmult), 1, fsinfo);
+ fat_bpb->startsector + fat_bpb->bpb_fsinfo, 1,fsinfo);
if (rc < 0)
{
DEBUGF( "flush_fat() - Couldn't read FSInfo (error code %d)\n", rc);
@@ -913,8 +907,7 @@ static int update_fsinfo(IF_MV_NONVOID(struct bpb* fat_bpb))
*intptr = htole32(fat_bpb->fsinfo.nextfree);
rc = ata_write_sectors(IF_MV2(fat_bpb->drive,)
- fat_bpb->startsector +
- (fat_bpb->bpb_fsinfo * fat_bpb->secmult), 1, fsinfo);
+ fat_bpb->startsector + fat_bpb->bpb_fsinfo,1,fsinfo);
if (rc < 0)
{
DEBUGF( "flush_fat() - Couldn't write FSInfo (error code %d)\n", rc);
@@ -1050,12 +1043,7 @@ static int write_long_name(struct fat_file* file,
const unsigned char* shortname,
bool is_directory)
{
-#ifdef HAVE_MULTIVOLUME
- struct bpb *fat_bpb = &fat_bpbs[file->volume];
-#else
- struct bpb *fat_bpb = &fat_bpbs[0];
-#endif
- unsigned char buf[MAX_SECTOR_SIZE];
+ unsigned char buf[SECTOR_SIZE];
unsigned char* entry;
unsigned int idx = firstentry % DIR_ENTRIES_PER_SECTOR;
unsigned int sector = firstentry / DIR_ENTRIES_PER_SECTOR;
@@ -1213,7 +1201,7 @@ static int add_dir_entry(struct fat_dir* dir,
#else
struct bpb* fat_bpb = &fat_bpbs[0];
#endif
- unsigned char buf[MAX_SECTOR_SIZE];
+ unsigned char buf[SECTOR_SIZE];
unsigned char shortname[12];
int rc;
unsigned int sector;
@@ -1448,12 +1436,7 @@ static void randomize_dos_name(unsigned char *name)
static int update_short_entry( struct fat_file* file, long size, int attr )
{
-#ifdef HAVE_MULTIVOLUME
- struct bpb *fat_bpb = &fat_bpbs[file->volume];
-#else
- struct bpb *fat_bpb = &fat_bpbs[0];
-#endif
- unsigned char buf[MAX_SECTOR_SIZE];
+ unsigned char buf[SECTOR_SIZE];
int sector = file->direntry / DIR_ENTRIES_PER_SECTOR;
unsigned char* entry =
buf + DIR_ENTRY_SIZE * (file->direntry % DIR_ENTRIES_PER_SECTOR);
@@ -1618,7 +1601,7 @@ int fat_create_dir(const char* name,
#else
struct bpb* fat_bpb = &fat_bpbs[0];
#endif
- unsigned char buf[MAX_SECTOR_SIZE];
+ unsigned char buf[SECTOR_SIZE];
int i;
long sector;
int rc;
@@ -1740,10 +1723,10 @@ int fat_closewrite(struct fat_file *file, long size, int attr)
LDEBUGF("cluster %ld: %lx\n", count, next);
count++;
}
- len = count * fat_bpb->bpb_secperclus * fat_bpb->bpb_bytspersec;
+ len = count * fat_bpb->bpb_secperclus * SECTOR_SIZE;
LDEBUGF("File is %ld clusters (chainlen=%ld, size=%ld)\n",
count, len, size );
- if ( len > size + fat_bpb->bpb_secperclus * fat_bpb->bpb_bytspersec)
+ if ( len > size + fat_bpb->bpb_secperclus * SECTOR_SIZE)
panicf("Cluster chain is too long\n");
if ( len < size )
panicf("Cluster chain is too short\n");
@@ -1755,12 +1738,7 @@ int fat_closewrite(struct fat_file *file, long size, int attr)
static int free_direntries(struct fat_file* file)
{
-#ifdef HAVE_MULTIVOLUME
- struct bpb *fat_bpb = &fat_bpbs[file->volume];
-#else
- struct bpb *fat_bpb = &fat_bpbs[0];
-#endif
- unsigned char buf[MAX_SECTOR_SIZE];
+ unsigned char buf[SECTOR_SIZE];
struct fat_file dir;
int numentries = file->direntries;
unsigned int entry = file->direntry - numentries + 1;
@@ -1962,9 +1940,6 @@ static int transfer(IF_MV2(struct bpb* fat_bpb,)
struct bpb* fat_bpb = &fat_bpbs[0];
#endif
int rc;
-
- start *= fat_bpb->secmult;
- count *= fat_bpb->secmult;
LDEBUGF("transfer(s=%lx, c=%lx, %s)\n",
start+ fat_bpb->startsector, count, write?"write":"read");
@@ -1977,9 +1952,9 @@ static int transfer(IF_MV2(struct bpb* fat_bpb,)
#endif
firstallowed = fat_bpb->firstdatasector;
- if (start < (firstallowed * fat_bpb->secmult))
+ if (start < firstallowed)
panicf("Write %ld before data\n", firstallowed - start);
- if (start + count > (fat_bpb->totalsectors * fat_bpb->secmult))
+ if (start + count > fat_bpb->totalsectors)
panicf("Write %ld after data\n",
start + count - fat_bpb->totalsectors);
rc = ata_write_sectors(IF_MV2(fat_bpb->drive,)
@@ -2073,14 +2048,13 @@ long fat_readwrite( struct fat_file *file, long sectorcount,
first = sector;
if ( ((sector != first) && (sector != last+1)) || /* not sequential */
- (last-first+1 == (256 / fat_bpb->secmult)) ) {
- /* max 256 sectors per ata request */
+ (last-first+1 == 256) ) { /* max 256 sectors per ata request */
long count = last - first + 1;
rc = transfer(IF_MV2(fat_bpb,) first, count, buf, write );
if (rc < 0)
return rc * 10 - 1;
- buf = (char *)buf + count * fat_bpb->bpb_bytspersec;
+ buf = (char *)buf + count * SECTOR_SIZE;
first = sector;
}
@@ -2110,16 +2084,6 @@ long fat_readwrite( struct fat_file *file, long sectorcount,
return i;
}
-int fat_get_secsize(const struct fat_file *file)
-{
-#ifdef HAVE_MULTIVOLUME
- return fat_bpbs[file->volume].bpb_bytspersec;
-#else
- (void)file;
- return fat_bpbs[0].bpb_bytspersec;
-#endif
-}
-
int fat_seek(struct fat_file *file, unsigned long seeksector )
{
#ifdef HAVE_MULTIVOLUME
@@ -2239,11 +2203,6 @@ static int fat_copy_long_name_segment(unsigned char *utf16src,
int fat_getnext(struct fat_dir *dir, struct fat_direntry *entry)
{
-#ifdef HAVE_MULTIVOLUME
- struct bpb* fat_bpb = &fat_bpbs[dir->file.volume];
-#else
- struct bpb* fat_bpb = &fat_bpbs[0];
-#endif
bool done = false;
int i;
int rc;
@@ -2277,7 +2236,7 @@ int fat_getnext(struct fat_dir *dir, struct fat_direntry *entry)
}
for (i = dir->entry % DIR_ENTRIES_PER_SECTOR;
- i < (int)DIR_ENTRIES_PER_SECTOR; i++)
+ i < DIR_ENTRIES_PER_SECTOR; i++)
{
unsigned int entrypos = i * DIR_ENTRY_SIZE;
@@ -2331,20 +2290,20 @@ int fat_getnext(struct fat_dir *dir, struct fat_direntry *entry)
unsigned char* ptr = cached_buf;
int index = longarray[j];
/* current or cached sector? */
- if ( sectoridx >= fat_bpb->bpb_bytspersec ) {
- if ( sectoridx >= fat_bpb->bpb_bytspersec*2 ) {
- if ( ( index >= fat_bpb->bpb_bytspersec ) &&
- ( index < fat_bpb->bpb_bytspersec*2 ))
+ if ( sectoridx >= SECTOR_SIZE ) {
+ if ( sectoridx >= SECTOR_SIZE*2 ) {
+ if ( ( index >= SECTOR_SIZE ) &&
+ ( index < SECTOR_SIZE*2 ))
ptr = dir->sectorcache[1];
else
ptr = dir->sectorcache[2];
}
else {
- if ( index < fat_bpb->bpb_bytspersec )
+ if ( index < SECTOR_SIZE )
ptr = dir->sectorcache[1];
}
- index &= fat_bpb->bpb_bytspersec-1;
+ index &= SECTOR_SIZE-1;
}
/* Try to append each segment of the long name. Check if we'd
@@ -2404,10 +2363,10 @@ int fat_getnext(struct fat_dir *dir, struct fat_direntry *entry)
/* save this sector, for longname use */
if ( sectoridx )
- memcpy( dir->sectorcache[2], dir->sectorcache[0], fat_bpb->bpb_bytspersec );
+ memcpy( dir->sectorcache[2], dir->sectorcache[0], SECTOR_SIZE );
else
- memcpy( dir->sectorcache[1], dir->sectorcache[0], fat_bpb->bpb_bytspersec );
- sectoridx += fat_bpb->bpb_bytspersec;
+ memcpy( dir->sectorcache[1], dir->sectorcache[0], SECTOR_SIZE );
+ sectoridx += SECTOR_SIZE;
}
return 0;
@@ -2419,7 +2378,7 @@ unsigned int fat_get_cluster_size(IF_MV_NONVOID(int volume))
const int volume = 0;
#endif
struct bpb* fat_bpb = &fat_bpbs[volume];
- return fat_bpb->bpb_secperclus * fat_bpb->bpb_bytspersec;
+ return fat_bpb->bpb_secperclus * SECTOR_SIZE;
}
#ifdef HAVE_MULTIVOLUME