2002-03-28 15:09:10 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2002 by Linus Nielsen Feltzing
|
|
|
|
*
|
|
|
|
* All files in this archive are subject to the GNU General Public License.
|
|
|
|
* See the file COPYING in the source tree root for full license agreement.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <ctype.h>
|
2002-06-19 14:34:54 +00:00
|
|
|
#ifdef DISK_WRITE
|
2002-03-28 15:09:10 +00:00
|
|
|
#include <time.h>
|
|
|
|
#include <sys/timeb.h>
|
2002-06-19 14:34:54 +00:00
|
|
|
#endif
|
2002-05-29 11:56:42 +00:00
|
|
|
#include <stdbool.h>
|
2002-03-28 15:09:10 +00:00
|
|
|
#include "fat.h"
|
2002-04-22 12:33:04 +00:00
|
|
|
#include "ata.h"
|
2002-05-02 14:05:51 +00:00
|
|
|
#include "debug.h"
|
2002-05-09 22:57:54 +00:00
|
|
|
#include "system.h"
|
2002-03-28 15:09:10 +00:00
|
|
|
|
2002-04-26 16:44:58 +00:00
|
|
|
#define BYTES2INT16(array,pos) \
|
|
|
|
(array[pos] | (array[pos+1] << 8 ))
|
|
|
|
#define BYTES2INT32(array,pos) \
|
|
|
|
(array[pos] | (array[pos+1] << 8 ) | \
|
|
|
|
(array[pos+2] << 16 ) | (array[pos+3] << 24 ))
|
|
|
|
|
2002-03-28 15:09:10 +00:00
|
|
|
#define NUM_ROOT_DIR_ENTRIES 512
|
|
|
|
#define NUM_FATS 2
|
|
|
|
#define NUM_RESERVED_SECTORS 1
|
|
|
|
#define NUM_BLOCKS 10000
|
|
|
|
|
2002-04-27 01:25:22 +00:00
|
|
|
#define FATTYPE_FAT12 0
|
|
|
|
#define FATTYPE_FAT16 1
|
|
|
|
#define FATTYPE_FAT32 2
|
|
|
|
|
|
|
|
/* BPB offsets; generic */
|
|
|
|
#define BS_JMPBOOT 0
|
|
|
|
#define BS_OEMNAME 3
|
|
|
|
#define BPB_BYTSPERSEC 11
|
|
|
|
#define BPB_SECPERCLUS 13
|
|
|
|
#define BPB_RSVDSECCNT 14
|
|
|
|
#define BPB_NUMFATS 16
|
|
|
|
#define BPB_ROOTENTCNT 17
|
|
|
|
#define BPB_TOTSEC16 19
|
|
|
|
#define BPB_MEDIA 21
|
|
|
|
#define BPB_FATSZ16 22
|
|
|
|
#define BPB_SECPERTRK 24
|
|
|
|
#define BPB_NUMHEADS 26
|
|
|
|
#define BPB_HIDDSEC 28
|
|
|
|
#define BPB_TOTSEC32 32
|
|
|
|
|
|
|
|
/* fat12/16 */
|
|
|
|
#define BS_DRVNUM 36
|
|
|
|
#define BS_RESERVED1 37
|
|
|
|
#define BS_BOOTSIG 38
|
|
|
|
#define BS_VOLID 39
|
|
|
|
#define BS_VOLLAB 43
|
|
|
|
#define BS_FILSYSTYPE 54
|
|
|
|
|
|
|
|
/* fat32 */
|
|
|
|
#define BPB_FATSZ32 36
|
|
|
|
#define BPB_EXTFLAGS 40
|
|
|
|
#define BPB_FSVER 42
|
|
|
|
#define BPB_ROOTCLUS 44
|
|
|
|
#define BPB_FSINFO 48
|
|
|
|
#define BPB_BKBOOTSEC 50
|
|
|
|
#define BS_32_DRVNUM 64
|
|
|
|
#define BS_32_BOOTSIG 66
|
|
|
|
#define BS_32_VOLID 67
|
|
|
|
#define BS_32_VOLLAB 71
|
|
|
|
#define BS_32_FILSYSTYPE 82
|
|
|
|
|
|
|
|
#define BPB_LAST_WORD 510
|
|
|
|
|
|
|
|
|
|
|
|
/* attributes */
|
|
|
|
#define FAT_ATTR_LONG_NAME (FAT_ATTR_READ_ONLY | FAT_ATTR_HIDDEN | \
|
|
|
|
FAT_ATTR_SYSTEM | FAT_ATTR_VOLUME_ID)
|
|
|
|
#define FAT_ATTR_LONG_NAME_MASK (FAT_ATTR_READ_ONLY | FAT_ATTR_HIDDEN | \
|
|
|
|
FAT_ATTR_SYSTEM | FAT_ATTR_VOLUME_ID | \
|
|
|
|
FAT_ATTR_DIRECTORY | FAT_ATTR_ARCHIVE )
|
|
|
|
|
|
|
|
#define FATDIR_NAME 0
|
|
|
|
#define FATDIR_ATTR 11
|
|
|
|
#define FATDIR_NTRES 12
|
|
|
|
#define FATDIR_CRTTIMETENTH 13
|
|
|
|
#define FATDIR_CRTTIME 14
|
|
|
|
#define FATDIR_CRTDATE 16
|
|
|
|
#define FATDIR_LSTACCDATE 18
|
|
|
|
#define FATDIR_FSTCLUSHI 20
|
|
|
|
#define FATDIR_WRTTIME 22
|
|
|
|
#define FATDIR_WRTDATE 24
|
|
|
|
#define FATDIR_FSTCLUSLO 26
|
|
|
|
#define FATDIR_FILESIZE 28
|
|
|
|
|
|
|
|
struct fsinfo {
|
|
|
|
int freecount; /* last known free cluster count */
|
|
|
|
int nextfree; /* first cluster to start looking for free clusters,
|
|
|
|
or 0xffffffff for no hint */
|
2002-03-28 15:09:10 +00:00
|
|
|
};
|
2002-04-27 01:25:22 +00:00
|
|
|
/* fsinfo offsets */
|
|
|
|
#define FSINFO_FREECOUNT 488
|
|
|
|
#define FSINFO_NEXTFREE 492
|
|
|
|
|
2002-05-30 19:47:56 +00:00
|
|
|
struct bpb
|
|
|
|
{
|
|
|
|
char bs_oemname[9]; /* OEM string, ending with \0 */
|
|
|
|
int bpb_bytspersec; /* Bytes per sectory, typically 512 */
|
|
|
|
int bpb_secperclus; /* Sectors per cluster */
|
|
|
|
int bpb_rsvdseccnt; /* Number of reserved sectors */
|
|
|
|
int bpb_numfats; /* Number of FAT structures, typically 2 */
|
|
|
|
int bpb_rootentcnt; /* Number of dir entries in the root */
|
|
|
|
int bpb_totsec16; /* Number of sectors on the volume (old 16-bit) */
|
|
|
|
int bpb_media; /* Media type (typically 0xf0 or 0xf8) */
|
|
|
|
int bpb_fatsz16; /* Number of used sectors per FAT structure */
|
|
|
|
int bpb_secpertrk; /* Number of sectors per track */
|
|
|
|
int bpb_numheads; /* Number of heads */
|
|
|
|
int bpb_hiddsec; /* Hidden sectors before the volume */
|
|
|
|
unsigned int bpb_totsec32; /* Number of sectors on the volume
|
|
|
|
(new 32-bit) */
|
|
|
|
int last_word; /* 0xAA55 */
|
|
|
|
|
|
|
|
/**** FAT12/16 specific *****/
|
|
|
|
int bs_drvnum; /* Drive number */
|
|
|
|
int bs_bootsig; /* Is 0x29 if the following 3 fields are valid */
|
|
|
|
unsigned int bs_volid; /* Volume ID */
|
|
|
|
char bs_vollab[12]; /* Volume label, 11 chars plus \0 */
|
|
|
|
char bs_filsystype[9]; /* File system type, 8 chars plus \0 */
|
|
|
|
|
|
|
|
/**** FAT32 specific *****/
|
|
|
|
int bpb_fatsz32;
|
|
|
|
int bpb_extflags;
|
|
|
|
int bpb_fsver;
|
|
|
|
int bpb_rootclus;
|
|
|
|
int bpb_fsinfo;
|
|
|
|
int bpb_bkbootsec;
|
|
|
|
|
|
|
|
/* variables for internal use */
|
|
|
|
int fatsize;
|
|
|
|
int totalsectors;
|
|
|
|
int rootdirsector;
|
|
|
|
int firstdatasector;
|
|
|
|
int startsector;
|
|
|
|
};
|
|
|
|
|
2002-05-30 19:49:15 +00:00
|
|
|
static struct bpb fat_bpb;
|
2002-05-30 19:47:56 +00:00
|
|
|
|
2002-05-14 12:28:48 +00:00
|
|
|
static int first_sector_of_cluster(int cluster);
|
2002-05-03 15:35:51 +00:00
|
|
|
static int bpb_is_sane(void);
|
|
|
|
static void *cache_fat_sector(int secnum);
|
2002-04-27 19:37:41 +00:00
|
|
|
#ifdef DISK_WRITE
|
2002-04-27 01:25:22 +00:00
|
|
|
static unsigned int getcurrdostime(unsigned short *dosdate,
|
|
|
|
unsigned short *dostime,
|
|
|
|
unsigned char *dostenth);
|
|
|
|
static int create_dos_name(unsigned char *name, unsigned char *newname);
|
2002-04-27 19:37:41 +00:00
|
|
|
#endif
|
2002-04-27 01:25:22 +00:00
|
|
|
|
2002-05-09 21:27:49 +00:00
|
|
|
#define FAT_CACHE_SIZE 0x20
|
|
|
|
#define FAT_CACHE_MASK (FAT_CACHE_SIZE-1)
|
|
|
|
|
|
|
|
struct fat_cache_entry
|
|
|
|
{
|
|
|
|
int secnum;
|
2002-05-29 11:56:42 +00:00
|
|
|
bool inuse;
|
|
|
|
bool dirty;
|
2002-05-09 21:27:49 +00:00
|
|
|
};
|
|
|
|
|
2002-05-29 11:56:42 +00:00
|
|
|
static char fat_cache_sectors[FAT_CACHE_SIZE][SECTOR_SIZE];
|
|
|
|
static struct fat_cache_entry fat_cache[FAT_CACHE_SIZE];
|
2002-03-28 15:09:10 +00:00
|
|
|
|
2002-04-27 22:32:37 +00:00
|
|
|
/* sectors cache for longname use */
|
|
|
|
static unsigned char lastsector[SECTOR_SIZE];
|
|
|
|
static unsigned char lastsector2[SECTOR_SIZE];
|
|
|
|
|
2002-05-14 12:28:48 +00:00
|
|
|
static int sec2cluster(int sec)
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
2002-05-03 15:35:51 +00:00
|
|
|
if ( sec < fat_bpb.firstdatasector )
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
2002-05-02 14:05:51 +00:00
|
|
|
DEBUGF( "sec2cluster() - Bad sector number (%d)\n", sec);
|
2002-03-28 15:09:10 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2002-05-03 15:35:51 +00:00
|
|
|
return ((sec - fat_bpb.firstdatasector) / fat_bpb.bpb_secperclus) + 2;
|
2002-03-28 15:09:10 +00:00
|
|
|
}
|
|
|
|
|
2002-05-14 12:28:48 +00:00
|
|
|
static int cluster2sec(int cluster)
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
2002-05-03 15:35:51 +00:00
|
|
|
int max_cluster = fat_bpb.totalsectors -
|
|
|
|
fat_bpb.firstdatasector / fat_bpb.bpb_secperclus + 1;
|
2002-03-28 15:09:10 +00:00
|
|
|
|
|
|
|
if(cluster > max_cluster)
|
|
|
|
{
|
2002-05-02 14:05:51 +00:00
|
|
|
DEBUGF( "cluster2sec() - Bad cluster number (%d)\n",
|
2002-03-28 15:09:10 +00:00
|
|
|
cluster);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2002-05-03 15:35:51 +00:00
|
|
|
return first_sector_of_cluster(cluster);
|
2002-03-28 15:09:10 +00:00
|
|
|
}
|
|
|
|
|
2002-05-14 12:28:48 +00:00
|
|
|
static int first_sector_of_cluster(int cluster)
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
2002-05-03 15:35:51 +00:00
|
|
|
return (cluster - 2) * fat_bpb.bpb_secperclus + fat_bpb.firstdatasector;
|
2002-03-28 15:09:10 +00:00
|
|
|
}
|
|
|
|
|
2002-05-03 15:35:51 +00:00
|
|
|
int fat_mount(int startsector)
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
2002-04-27 19:37:41 +00:00
|
|
|
unsigned char buf[SECTOR_SIZE];
|
2002-03-28 15:09:10 +00:00
|
|
|
int err;
|
|
|
|
int datasec;
|
|
|
|
int countofclusters;
|
2002-05-09 21:27:49 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for(i = 0;i < FAT_CACHE_SIZE;i++)
|
|
|
|
{
|
|
|
|
fat_cache[i].secnum = 8; /* We use a "safe" sector just in case */
|
2002-05-29 11:56:42 +00:00
|
|
|
fat_cache[i].inuse = false;
|
|
|
|
fat_cache[i].dirty = false;
|
2002-05-09 21:27:49 +00:00
|
|
|
}
|
2002-03-28 15:09:10 +00:00
|
|
|
|
|
|
|
/* Read the sector */
|
2002-05-03 11:59:53 +00:00
|
|
|
err = ata_read_sectors(startsector,1,buf);
|
2002-04-22 12:33:04 +00:00
|
|
|
if(err)
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
2002-05-08 08:26:38 +00:00
|
|
|
DEBUGF( "fat_mount() - Couldn't read BPB (error code %d)\n",
|
2002-03-28 15:09:10 +00:00
|
|
|
err);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2002-05-03 15:35:51 +00:00
|
|
|
memset(&fat_bpb, 0, sizeof(struct bpb));
|
|
|
|
fat_bpb.startsector = startsector;
|
2002-03-28 15:09:10 +00:00
|
|
|
|
2002-05-03 15:35:51 +00:00
|
|
|
strncpy(fat_bpb.bs_oemname, &buf[BS_OEMNAME], 8);
|
|
|
|
fat_bpb.bs_oemname[8] = 0;
|
|
|
|
|
|
|
|
fat_bpb.bpb_bytspersec = BYTES2INT16(buf,BPB_BYTSPERSEC);
|
|
|
|
fat_bpb.bpb_secperclus = buf[BPB_SECPERCLUS];
|
|
|
|
fat_bpb.bpb_rsvdseccnt = BYTES2INT16(buf,BPB_RSVDSECCNT);
|
|
|
|
fat_bpb.bpb_numfats = buf[BPB_NUMFATS];
|
|
|
|
fat_bpb.bpb_rootentcnt = BYTES2INT16(buf,BPB_ROOTENTCNT);
|
|
|
|
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_secpertrk = BYTES2INT16(buf,BPB_SECPERTRK);
|
|
|
|
fat_bpb.bpb_numheads = BYTES2INT16(buf,BPB_NUMHEADS);
|
|
|
|
fat_bpb.bpb_hiddsec = BYTES2INT32(buf,BPB_HIDDSEC);
|
|
|
|
fat_bpb.bpb_totsec32 = BYTES2INT32(buf,BPB_TOTSEC32);
|
|
|
|
fat_bpb.last_word = BYTES2INT16(buf,BPB_LAST_WORD);
|
2002-04-26 16:44:58 +00:00
|
|
|
|
2002-04-27 01:25:22 +00:00
|
|
|
/* calculate a few commonly used values */
|
2002-05-03 15:35:51 +00:00
|
|
|
if (fat_bpb.bpb_fatsz16 != 0)
|
|
|
|
fat_bpb.fatsize = fat_bpb.bpb_fatsz16;
|
2002-04-27 01:25:22 +00:00
|
|
|
else
|
2002-05-03 15:35:51 +00:00
|
|
|
fat_bpb.fatsize = fat_bpb.bpb_fatsz32;
|
2002-03-28 15:09:10 +00:00
|
|
|
|
2002-05-03 15:35:51 +00:00
|
|
|
if (fat_bpb.bpb_totsec16 != 0)
|
|
|
|
fat_bpb.totalsectors = fat_bpb.bpb_totsec16;
|
2002-03-28 15:09:10 +00:00
|
|
|
else
|
2002-05-03 15:35:51 +00:00
|
|
|
fat_bpb.totalsectors = fat_bpb.bpb_totsec32;
|
|
|
|
fat_bpb.firstdatasector = fat_bpb.bpb_rsvdseccnt +
|
|
|
|
fat_bpb.bpb_numfats * fat_bpb.fatsize;
|
2002-03-28 15:09:10 +00:00
|
|
|
|
2002-04-27 01:25:22 +00:00
|
|
|
/* Determine FAT type */
|
2002-05-03 15:35:51 +00:00
|
|
|
datasec = fat_bpb.totalsectors - fat_bpb.firstdatasector;
|
|
|
|
countofclusters = datasec / fat_bpb.bpb_secperclus;
|
2002-03-28 15:09:10 +00:00
|
|
|
|
2002-04-27 01:25:22 +00:00
|
|
|
#ifdef TEST_FAT
|
2002-04-26 16:44:58 +00:00
|
|
|
/*
|
2002-04-27 01:25:22 +00:00
|
|
|
we are sometimes testing with "illegally small" fat32 images,
|
|
|
|
so we don't use the proper fat32 test case for test code
|
2002-04-26 16:44:58 +00:00
|
|
|
*/
|
2002-05-03 15:35:51 +00:00
|
|
|
if ( fat_bpb.bpb_fatsz16 )
|
2002-04-27 01:25:22 +00:00
|
|
|
#else
|
|
|
|
if ( countofclusters < 65525 )
|
|
|
|
#endif
|
|
|
|
{
|
2002-05-02 14:05:51 +00:00
|
|
|
DEBUGF("This is not FAT32. Go away!\n");
|
2002-04-26 16:44:58 +00:00
|
|
|
return -1;
|
2002-03-28 15:09:10 +00:00
|
|
|
}
|
|
|
|
|
2002-05-03 15:35:51 +00:00
|
|
|
fat_bpb.bpb_extflags = BYTES2INT16(buf,BPB_EXTFLAGS);
|
|
|
|
fat_bpb.bpb_fsver = BYTES2INT16(buf,BPB_FSVER);
|
|
|
|
fat_bpb.bpb_rootclus = BYTES2INT32(buf,BPB_ROOTCLUS);
|
|
|
|
fat_bpb.bpb_fsinfo = BYTES2INT16(buf,BPB_FSINFO);
|
|
|
|
fat_bpb.bpb_bkbootsec = BYTES2INT16(buf,BPB_BKBOOTSEC);
|
|
|
|
fat_bpb.bs_drvnum = buf[BS_32_DRVNUM];
|
|
|
|
fat_bpb.bs_bootsig = buf[BS_32_BOOTSIG];
|
2002-04-27 01:25:22 +00:00
|
|
|
|
2002-05-03 15:35:51 +00:00
|
|
|
if(fat_bpb.bs_bootsig == 0x29)
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
2002-05-03 15:35:51 +00:00
|
|
|
fat_bpb.bs_volid = BYTES2INT32(buf,BS_32_VOLID);
|
|
|
|
strncpy(fat_bpb.bs_vollab, &buf[BS_32_VOLLAB], 11);
|
|
|
|
strncpy(fat_bpb.bs_filsystype, &buf[BS_32_FILSYSTYPE], 8);
|
2002-04-27 01:25:22 +00:00
|
|
|
}
|
|
|
|
|
2002-05-03 15:35:51 +00:00
|
|
|
if (bpb_is_sane() < 0)
|
2002-04-27 01:25:22 +00:00
|
|
|
{
|
2002-05-02 14:05:51 +00:00
|
|
|
DEBUGF( "fat_mount() - BPB is not sane\n");
|
2002-03-28 15:09:10 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2002-05-03 15:35:51 +00:00
|
|
|
fat_bpb.rootdirsector = cluster2sec(fat_bpb.bpb_rootclus);
|
2002-04-27 01:25:22 +00:00
|
|
|
|
2002-03-28 15:09:10 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-05-03 15:35:51 +00:00
|
|
|
static int bpb_is_sane(void)
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
2002-05-03 15:35:51 +00:00
|
|
|
if(fat_bpb.bpb_bytspersec != 512)
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
2002-05-08 08:26:38 +00:00
|
|
|
DEBUGF( "bpb_is_sane() - Error: sector size is not 512 (%d)\n",
|
2002-05-03 15:35:51 +00:00
|
|
|
fat_bpb.bpb_bytspersec);
|
2002-04-27 19:37:41 +00:00
|
|
|
return -1;
|
2002-03-28 15:09:10 +00:00
|
|
|
}
|
2002-05-03 15:35:51 +00:00
|
|
|
if(fat_bpb.bpb_secperclus * fat_bpb.bpb_bytspersec > 32768)
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
2002-05-02 14:05:51 +00:00
|
|
|
DEBUGF( "bpb_is_sane() - Warning: cluster size is larger than 32K "
|
2002-05-08 08:26:38 +00:00
|
|
|
"(%d * %d = %d)\n",
|
2002-05-03 15:35:51 +00:00
|
|
|
fat_bpb.bpb_bytspersec, fat_bpb.bpb_secperclus,
|
|
|
|
fat_bpb.bpb_bytspersec * fat_bpb.bpb_secperclus);
|
2002-03-28 15:09:10 +00:00
|
|
|
}
|
2002-05-03 15:35:51 +00:00
|
|
|
if(fat_bpb.bpb_rsvdseccnt != 1)
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
2002-05-08 08:26:38 +00:00
|
|
|
DEBUGF( "bpb_is_sane() - Warning: Reserved sectors is not 1 (%d)\n",
|
2002-05-03 15:35:51 +00:00
|
|
|
fat_bpb.bpb_rsvdseccnt);
|
2002-03-28 15:09:10 +00:00
|
|
|
}
|
2002-05-03 15:35:51 +00:00
|
|
|
if(fat_bpb.bpb_numfats != 2)
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
2002-05-08 08:26:38 +00:00
|
|
|
DEBUGF( "bpb_is_sane() - Warning: NumFATS is not 2 (%d)\n",
|
2002-05-03 15:35:51 +00:00
|
|
|
fat_bpb.bpb_numfats);
|
2002-03-28 15:09:10 +00:00
|
|
|
}
|
2002-05-03 15:35:51 +00:00
|
|
|
if(fat_bpb.bpb_rootentcnt != 512)
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
2002-05-08 08:26:38 +00:00
|
|
|
DEBUGF( "bpb_is_sane() - Warning: RootEntCnt is not 512 (%d)\n",
|
2002-05-03 15:35:51 +00:00
|
|
|
fat_bpb.bpb_rootentcnt);
|
2002-03-28 15:09:10 +00:00
|
|
|
}
|
2002-05-03 15:35:51 +00:00
|
|
|
if(fat_bpb.bpb_media != 0xf0 && fat_bpb.bpb_media < 0xf8)
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
2002-05-02 14:05:51 +00:00
|
|
|
DEBUGF( "bpb_is_sane() - Warning: Non-standard "
|
2002-03-28 15:09:10 +00:00
|
|
|
"media type (0x%02x)\n",
|
2002-05-03 15:35:51 +00:00
|
|
|
fat_bpb.bpb_media);
|
2002-03-28 15:09:10 +00:00
|
|
|
}
|
2002-05-03 15:35:51 +00:00
|
|
|
if(fat_bpb.last_word != 0xaa55)
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
2002-05-02 14:05:51 +00:00
|
|
|
DEBUGF( "bpb_is_sane() - Error: Last word is not "
|
2002-05-03 15:35:51 +00:00
|
|
|
"0xaa55 (0x%04x)\n", fat_bpb.last_word);
|
2002-03-28 15:09:10 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-05-03 15:35:51 +00:00
|
|
|
static void *cache_fat_sector(int secnum)
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
2002-05-09 21:27:49 +00:00
|
|
|
int cache_index = secnum & FAT_CACHE_MASK;
|
|
|
|
|
|
|
|
/* Delete the cache entry if it isn't the sector we want */
|
2002-05-29 11:56:42 +00:00
|
|
|
if(fat_cache[cache_index].inuse &&
|
|
|
|
fat_cache[cache_index].secnum != secnum)
|
2002-05-09 21:27:49 +00:00
|
|
|
{
|
|
|
|
#ifdef WRITE
|
|
|
|
/* Write back if it is dirty */
|
|
|
|
if(fat_cache[cache_index].dirty)
|
|
|
|
{
|
2002-05-09 22:57:54 +00:00
|
|
|
if(ata_write_sectors(secnum + fat_bpb.startsector, 1, sec))
|
2002-05-09 21:27:49 +00:00
|
|
|
{
|
|
|
|
panic("cache_fat_sector() - Could"
|
|
|
|
" not write sector %d\n",
|
|
|
|
secnum);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fat_cache[cache_index].secnum = 8; /* Normally an unused sector */
|
2002-05-29 11:56:42 +00:00
|
|
|
fat_cache[cache_index].dirty = false;
|
|
|
|
#endif
|
|
|
|
fat_cache[cache_index].inuse = false;
|
2002-05-09 21:27:49 +00:00
|
|
|
}
|
|
|
|
|
2002-03-28 15:09:10 +00:00
|
|
|
/* Load the sector if it is not cached */
|
2002-05-29 11:56:42 +00:00
|
|
|
if(!fat_cache[cache_index].inuse)
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
2002-05-29 11:56:42 +00:00
|
|
|
if(ata_read_sectors(secnum + fat_bpb.startsector,1,
|
|
|
|
fat_cache_sectors[cache_index]))
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
2002-05-29 11:56:42 +00:00
|
|
|
DEBUGF( "cache_fat_sector() - Could not read sector %d\n", secnum);
|
2002-03-28 15:09:10 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2002-05-29 11:56:42 +00:00
|
|
|
fat_cache[cache_index].inuse = true;
|
2002-05-09 21:27:49 +00:00
|
|
|
fat_cache[cache_index].secnum = secnum;
|
2002-03-28 15:09:10 +00:00
|
|
|
}
|
2002-05-29 11:56:42 +00:00
|
|
|
return fat_cache_sectors[cache_index];
|
2002-03-28 15:09:10 +00:00
|
|
|
}
|
|
|
|
|
2002-04-27 19:37:41 +00:00
|
|
|
#ifdef DISK_WRITE
|
2002-05-03 15:35:51 +00:00
|
|
|
static int update_entry(int entry, unsigned int val)
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
2002-04-27 01:25:22 +00:00
|
|
|
unsigned long *sec;
|
2002-03-28 15:09:10 +00:00
|
|
|
int fatoffset;
|
|
|
|
int thisfatsecnum;
|
|
|
|
int thisfatentoffset;
|
|
|
|
|
2002-04-26 16:44:58 +00:00
|
|
|
fatoffset = entry * 4;
|
2002-05-03 15:35:51 +00:00
|
|
|
thisfatsecnum = fatoffset / fat_bpb.bpb_bytspersec + fat_bpb.bpb_rsvdseccnt;
|
|
|
|
thisfatentoffset = fatoffset % fat_bpb.bpb_bytspersec;
|
2002-03-28 15:09:10 +00:00
|
|
|
|
|
|
|
/* Load the sector if it is not cached */
|
2002-05-03 15:35:51 +00:00
|
|
|
sec = cache_fat_sector(thisfatsecnum);
|
2002-03-28 15:09:10 +00:00
|
|
|
if(!sec)
|
|
|
|
{
|
2002-05-02 14:05:51 +00:00
|
|
|
DEBUGF( "update_entry() - Could not cache sector %d\n",
|
2002-03-28 15:09:10 +00:00
|
|
|
thisfatsecnum);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2002-05-09 21:27:49 +00:00
|
|
|
/* We can safely assume that the correct sector is in the cache,
|
|
|
|
so we mark it dirty without checking the sector number */
|
|
|
|
fat_cache[thisfatsecnum & FAT_CACHE_MASK].dirty = 1;
|
2002-04-27 01:25:22 +00:00
|
|
|
|
|
|
|
/* don't change top 4 bits */
|
|
|
|
sec[thisfatentoffset/sizeof(int)] &= 0xf000000;
|
|
|
|
sec[thisfatentoffset/sizeof(int)] |= val & 0x0fffffff;
|
2002-04-26 16:44:58 +00:00
|
|
|
|
2002-03-28 15:09:10 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2002-04-27 19:37:41 +00:00
|
|
|
#endif
|
2002-03-28 15:09:10 +00:00
|
|
|
|
2002-05-03 15:35:51 +00:00
|
|
|
static int read_entry(int entry)
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
2002-04-27 01:25:22 +00:00
|
|
|
unsigned long *sec;
|
2002-03-28 15:09:10 +00:00
|
|
|
int fatoffset;
|
|
|
|
int thisfatsecnum;
|
|
|
|
int thisfatentoffset;
|
2002-04-22 12:33:04 +00:00
|
|
|
int val = -1;
|
2002-03-28 15:09:10 +00:00
|
|
|
|
2002-04-26 16:44:58 +00:00
|
|
|
fatoffset = entry * 4;
|
2002-05-03 15:35:51 +00:00
|
|
|
thisfatsecnum = fatoffset / fat_bpb.bpb_bytspersec + fat_bpb.bpb_rsvdseccnt;
|
|
|
|
thisfatentoffset = fatoffset % fat_bpb.bpb_bytspersec;
|
2002-03-28 15:09:10 +00:00
|
|
|
|
|
|
|
/* Load the sector if it is not cached */
|
2002-05-03 15:35:51 +00:00
|
|
|
sec = cache_fat_sector(thisfatsecnum);
|
2002-03-28 15:09:10 +00:00
|
|
|
if(!sec)
|
|
|
|
{
|
2002-05-09 22:57:54 +00:00
|
|
|
DEBUGF( "read_entry() - Could not cache sector %d\n",
|
2002-03-28 15:09:10 +00:00
|
|
|
thisfatsecnum);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2002-04-27 01:25:22 +00:00
|
|
|
val = sec[thisfatentoffset/sizeof(int)];
|
2002-05-09 22:57:54 +00:00
|
|
|
|
|
|
|
val = SWAB32(val);
|
|
|
|
|
2002-03-28 15:09:10 +00:00
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2002-05-03 15:35:51 +00:00
|
|
|
static int get_next_cluster(unsigned int cluster)
|
2002-04-27 01:25:22 +00:00
|
|
|
{
|
2002-05-09 22:57:54 +00:00
|
|
|
int next_cluster;
|
2002-04-27 01:25:22 +00:00
|
|
|
|
2002-05-09 22:57:54 +00:00
|
|
|
next_cluster = read_entry(cluster);
|
|
|
|
|
2002-04-27 01:25:22 +00:00
|
|
|
/* is this last cluster in chain? */
|
|
|
|
if ( next_cluster >= 0x0ffffff8 )
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return next_cluster;
|
|
|
|
}
|
|
|
|
|
2002-04-27 19:37:41 +00:00
|
|
|
#ifdef DISK_WRITE
|
2002-04-27 01:25:22 +00:00
|
|
|
static int flush_fat(struct bpb *bpb)
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int err;
|
|
|
|
unsigned char *sec;
|
2002-05-09 21:27:49 +00:00
|
|
|
int secnum;
|
2002-03-28 15:09:10 +00:00
|
|
|
int fatsz;
|
|
|
|
unsigned short d, t;
|
|
|
|
char m;
|
|
|
|
|
2002-05-03 15:35:51 +00:00
|
|
|
fatsz = fat_bpb.fatsize;
|
2002-03-28 15:09:10 +00:00
|
|
|
|
2002-05-09 21:27:49 +00:00
|
|
|
for(i = 0;i < FAT_CACHE_SIZE;i++)
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
2002-05-09 21:27:49 +00:00
|
|
|
if(fat_cache[i].ptr && fat_cache[i].dirty)
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
2002-05-09 21:27:49 +00:00
|
|
|
secnum = fat_cache[i].secnum + fat_bpb.bpb_rsvdseccnt +
|
|
|
|
fat_bpb.startsector;
|
|
|
|
DEBUGF("Flushing FAT sector %d\n", secnum);
|
|
|
|
sec = fat_cache[i].ptr;
|
|
|
|
|
|
|
|
/* Write to the first FAT */
|
|
|
|
err = ata_write_sectors(secnum, 1, sec);
|
2002-04-22 12:33:04 +00:00
|
|
|
if(err)
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
2002-05-02 14:05:51 +00:00
|
|
|
DEBUGF( "flush_fat() - Couldn't write"
|
2002-05-09 21:27:49 +00:00
|
|
|
" sector (%d)\n", secnum);
|
2002-03-28 15:09:10 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2002-05-09 21:27:49 +00:00
|
|
|
|
|
|
|
/* Write to the second FAT */
|
|
|
|
err = ata_write_sectors(secnum + fatsz, 1, sec);
|
2002-04-22 12:33:04 +00:00
|
|
|
if(err)
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
2002-05-02 14:05:51 +00:00
|
|
|
DEBUGF( "flush_fat() - Couldn't write"
|
2002-05-09 21:27:49 +00:00
|
|
|
" sector (%d)\n", secnum + fatsz);
|
2002-03-28 15:09:10 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2002-05-09 21:27:49 +00:00
|
|
|
fat_cache[i].dirty = 0;
|
2002-03-28 15:09:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-04-27 01:25:22 +00:00
|
|
|
getcurrdostime(&d, &t, &m);
|
2002-03-28 15:09:10 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-05-03 15:35:51 +00:00
|
|
|
static unsigned int getcurrdostime(unsigned short *dosdate,
|
|
|
|
unsigned short *dostime,
|
|
|
|
unsigned char *dostenth)
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
|
|
|
struct timeb tb;
|
|
|
|
struct tm *tm;
|
|
|
|
|
|
|
|
ftime(&tb);
|
|
|
|
tm = localtime(&tb.time);
|
|
|
|
|
|
|
|
*dosdate = ((tm->tm_year - 80) << 9) |
|
|
|
|
((tm->tm_mon + 1) << 5) |
|
|
|
|
(tm->tm_mday);
|
|
|
|
|
|
|
|
*dostime = (tm->tm_hour << 11) |
|
|
|
|
(tm->tm_min << 5) |
|
|
|
|
(tm->tm_sec >> 1);
|
|
|
|
|
|
|
|
*dostenth = (tm->tm_sec & 1) * 100 + tb.millitm / 10;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-05-03 15:35:51 +00:00
|
|
|
static int add_dir_entry(unsigned int currdir, struct fat_direntry *de)
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
2002-04-27 19:37:41 +00:00
|
|
|
unsigned char buf[SECTOR_SIZE];
|
2002-03-28 15:09:10 +00:00
|
|
|
unsigned char *eptr;
|
|
|
|
int i;
|
|
|
|
int err;
|
|
|
|
unsigned int sec;
|
|
|
|
unsigned int sec_cnt;
|
|
|
|
int need_to_update_last_empty_marker = 0;
|
|
|
|
int is_rootdir = (currdir == 0);
|
|
|
|
int done = 0;
|
|
|
|
unsigned char firstbyte;
|
|
|
|
|
|
|
|
if(is_rootdir)
|
|
|
|
{
|
2002-05-03 15:35:51 +00:00
|
|
|
sec = fat_bpb.rootdirsector;
|
2002-03-28 15:09:10 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2002-05-03 15:35:51 +00:00
|
|
|
sec = first_sector_of_cluster(currdir);
|
2002-03-28 15:09:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sec_cnt = 0;
|
|
|
|
|
|
|
|
while(!done)
|
|
|
|
{
|
|
|
|
/* The root dir has a fixed size */
|
|
|
|
if(is_rootdir)
|
|
|
|
{
|
2002-05-03 15:35:51 +00:00
|
|
|
if(sec_cnt >= fat_bpb.bpb_rootentcnt * 32 / fat_bpb.bpb_bytspersec)
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
|
|
|
/* We have reached the last sector of the root dir */
|
|
|
|
if(need_to_update_last_empty_marker)
|
|
|
|
{
|
|
|
|
/* Since the directory is full, there is no room for
|
|
|
|
a marker, so we just exit */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2002-05-02 14:05:51 +00:00
|
|
|
DEBUGF( "add_dir_entry() -"
|
2002-03-28 15:09:10 +00:00
|
|
|
" Root dir is full\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2002-05-03 15:35:51 +00:00
|
|
|
if(sec_cnt >= fat_bpb.bpb_secperclus)
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
|
|
|
/* We have reached the end of this cluster */
|
2002-05-02 14:05:51 +00:00
|
|
|
DEBUGF("Moving to the next cluster...");
|
2002-05-03 15:35:51 +00:00
|
|
|
currdir = get_next_cluster(currdir);
|
2002-05-02 14:05:51 +00:00
|
|
|
DEBUGF("new cluster is %d\n", currdir);
|
2002-03-28 15:09:10 +00:00
|
|
|
|
|
|
|
if(!currdir)
|
|
|
|
{
|
|
|
|
/* This was the last in the chain,
|
|
|
|
we have to allocate a new cluster */
|
|
|
|
/* TODO */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-05-02 14:05:51 +00:00
|
|
|
DEBUGF("Reading sector %d...\n", sec);
|
2002-03-28 15:09:10 +00:00
|
|
|
/* Read the next sector in the current dir */
|
2002-05-03 15:35:51 +00:00
|
|
|
err = ata_read_sectors(sec + fat_bpb.startsector,1,buf);
|
2002-04-22 12:33:04 +00:00
|
|
|
if(err)
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
2002-05-02 14:05:51 +00:00
|
|
|
DEBUGF( "add_dir_entry() - Couldn't read dir sector"
|
2002-05-08 08:26:38 +00:00
|
|
|
" (error code %d)\n", err);
|
2002-03-28 15:09:10 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(need_to_update_last_empty_marker)
|
|
|
|
{
|
|
|
|
/* All we need to do is to set the first entry to 0 */
|
2002-05-02 14:05:51 +00:00
|
|
|
DEBUGF("Clearing the first entry in sector %d\n", sec);
|
2002-03-28 15:09:10 +00:00
|
|
|
buf[0] = 0;
|
|
|
|
done = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Look for a free slot */
|
2002-04-27 19:37:41 +00:00
|
|
|
for(i = 0;i < SECTOR_SIZE;i+=32)
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
|
|
|
firstbyte = buf[i];
|
|
|
|
if(firstbyte == 0xe5 || firstbyte == 0)
|
|
|
|
{
|
2002-05-02 14:05:51 +00:00
|
|
|
DEBUGF("Found free slot at entry %d in sector %d\n",
|
2002-03-28 15:09:10 +00:00
|
|
|
i/32, sec);
|
|
|
|
eptr = &buf[i];
|
|
|
|
memset(eptr, 0, 32);
|
|
|
|
strncpy(&eptr[FATDIR_NAME], de->name, 11);
|
|
|
|
eptr[FATDIR_ATTR] = de->attr;
|
|
|
|
eptr[FATDIR_NTRES] = 0;
|
|
|
|
|
|
|
|
eptr[FATDIR_CRTTIMETENTH] = de->crttimetenth;
|
|
|
|
eptr[FATDIR_CRTDATE] = de->crtdate & 0xff;
|
|
|
|
eptr[FATDIR_CRTDATE+1] = de->crtdate >> 8;
|
|
|
|
eptr[FATDIR_CRTTIME] = de->crttime & 0xff;
|
|
|
|
eptr[FATDIR_CRTTIME+1] = de->crttime >> 8;
|
|
|
|
|
|
|
|
eptr[FATDIR_WRTDATE] = de->wrtdate & 0xff;
|
|
|
|
eptr[FATDIR_WRTDATE+1] = de->wrtdate >> 8;
|
|
|
|
eptr[FATDIR_WRTTIME] = de->wrttime & 0xff;
|
|
|
|
eptr[FATDIR_WRTTIME+1] = de->wrttime >> 8;
|
|
|
|
|
|
|
|
eptr[FATDIR_FILESIZE] = de->filesize & 0xff;
|
|
|
|
eptr[FATDIR_FILESIZE+1] = (de->filesize >> 8) & 0xff;
|
|
|
|
eptr[FATDIR_FILESIZE+2] = (de->filesize >> 16) & 0xff;
|
|
|
|
eptr[FATDIR_FILESIZE+3] = (de->filesize >> 24) & 0xff;
|
|
|
|
|
|
|
|
/* Advance the last_empty_entry marker */
|
|
|
|
if(firstbyte == 0)
|
|
|
|
{
|
|
|
|
i += 32;
|
2002-04-27 19:37:41 +00:00
|
|
|
if(i < SECTOR_SIZE)
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
|
|
|
buf[i] = 0;
|
|
|
|
/* We are done */
|
|
|
|
done = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* We must fill in the first entry
|
|
|
|
in the next sector */
|
|
|
|
need_to_update_last_empty_marker = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-05-03 15:35:51 +00:00
|
|
|
err = ata_write_sectors(sec + fat_bpb.startsector,1,buf);
|
2002-04-22 12:33:04 +00:00
|
|
|
if(err)
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
2002-05-02 14:05:51 +00:00
|
|
|
DEBUGF( "add_dir_entry() - "
|
2002-03-28 15:09:10 +00:00
|
|
|
" Couldn't write dir"
|
2002-05-08 08:26:38 +00:00
|
|
|
" sector (error code %d)\n", err);
|
2002-03-28 15:09:10 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sec++;
|
|
|
|
sec_cnt++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-04-27 01:25:22 +00:00
|
|
|
unsigned char char2dos(unsigned char c)
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
|
|
|
switch(c)
|
|
|
|
{
|
|
|
|
case 0xe5: /* Special kanji character */
|
|
|
|
c = 0x05;
|
|
|
|
break;
|
|
|
|
case 0x22:
|
|
|
|
case 0x2a:
|
|
|
|
case 0x2b:
|
|
|
|
case 0x2c:
|
|
|
|
case 0x2e:
|
|
|
|
case 0x3a:
|
|
|
|
case 0x3b:
|
|
|
|
case 0x3c:
|
|
|
|
case 0x3d:
|
|
|
|
case 0x3e:
|
|
|
|
case 0x3f:
|
|
|
|
case 0x5b:
|
|
|
|
case 0x5c:
|
|
|
|
case 0x5d:
|
|
|
|
case 0x7c:
|
|
|
|
/* Illegal name */
|
|
|
|
c = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
if(c < 0x20)
|
|
|
|
{
|
|
|
|
/* Illegal name */
|
|
|
|
c = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2002-04-27 01:25:22 +00:00
|
|
|
static int create_dos_name(unsigned char *name, unsigned char *newname)
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
|
|
|
unsigned char n[12];
|
|
|
|
unsigned char c;
|
|
|
|
int i;
|
|
|
|
char *ext;
|
|
|
|
|
|
|
|
if(strlen(name) > 12)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
strcpy(n, name);
|
|
|
|
|
|
|
|
ext = strchr(n, '.');
|
|
|
|
if(ext)
|
|
|
|
{
|
|
|
|
*ext++ = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The file name is either empty, or there was only an extension.
|
|
|
|
In either case it is illegal. */
|
|
|
|
if(n[0] == 0)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Name part */
|
|
|
|
for(i = 0;n[i] && (i < 8);i++)
|
|
|
|
{
|
2002-04-27 01:25:22 +00:00
|
|
|
c = char2dos(n[i]);
|
2002-03-28 15:09:10 +00:00
|
|
|
if(c)
|
|
|
|
{
|
|
|
|
newname[i] = toupper(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while(i < 8)
|
|
|
|
{
|
|
|
|
newname[i++] = ' ';
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Extension part */
|
|
|
|
for(i = 0;ext && ext[i] && (i < 3);i++)
|
|
|
|
{
|
2002-04-27 01:25:22 +00:00
|
|
|
c = char2dos(ext[i]);
|
2002-03-28 15:09:10 +00:00
|
|
|
if(c)
|
|
|
|
{
|
|
|
|
newname[8+i] = toupper(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while(i < 3)
|
|
|
|
{
|
|
|
|
newname[8+i++] = ' ';
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-05-03 15:35:51 +00:00
|
|
|
int fat_create_dir(unsigned int currdir, char *name)
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
|
|
|
struct fat_direntry de;
|
|
|
|
int err;
|
|
|
|
|
2002-05-02 14:05:51 +00:00
|
|
|
DEBUGF("fat_create_file()\n");
|
2002-03-28 15:09:10 +00:00
|
|
|
memset(&de, 0, sizeof(struct fat_direntry));
|
2002-04-27 01:25:22 +00:00
|
|
|
if(create_dos_name(name, de.name) < 0)
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
2002-05-02 14:05:51 +00:00
|
|
|
DEBUGF( "fat_create_file() - Illegal file name (%s)\n", name);
|
2002-03-28 15:09:10 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2002-04-27 01:25:22 +00:00
|
|
|
getcurrdostime(&de.crtdate, &de.crttime, &de.crttimetenth);
|
2002-03-28 15:09:10 +00:00
|
|
|
de.wrtdate = de.crtdate;
|
|
|
|
de.wrttime = de.crttime;
|
|
|
|
de.filesize = 0;
|
|
|
|
de.attr = FAT_ATTR_DIRECTORY;
|
|
|
|
|
2002-05-03 15:35:51 +00:00
|
|
|
err = add_dir_entry(currdir, &de);
|
2002-03-28 15:09:10 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-05-03 15:35:51 +00:00
|
|
|
int fat_create_file(unsigned int currdir, char *name)
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
|
|
|
struct fat_direntry de;
|
|
|
|
int err;
|
|
|
|
|
2002-05-02 14:05:51 +00:00
|
|
|
DEBUGF("fat_create_file()\n");
|
2002-03-28 15:09:10 +00:00
|
|
|
memset(&de, 0, sizeof(struct fat_direntry));
|
2002-04-27 01:25:22 +00:00
|
|
|
if(create_dos_name(name, de.name) < 0)
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
2002-05-02 14:05:51 +00:00
|
|
|
DEBUGF( "fat_create_file() - Illegal file name (%s)\n", name);
|
2002-03-28 15:09:10 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2002-04-27 01:25:22 +00:00
|
|
|
getcurrdostime(&de.crtdate, &de.crttime, &de.crttimetenth);
|
2002-03-28 15:09:10 +00:00
|
|
|
de.wrtdate = de.crtdate;
|
|
|
|
de.wrttime = de.crttime;
|
|
|
|
de.filesize = 0;
|
|
|
|
|
2002-05-03 15:35:51 +00:00
|
|
|
err = add_dir_entry(currdir, &de);
|
2002-03-28 15:09:10 +00:00
|
|
|
return err;
|
|
|
|
}
|
2002-04-27 19:37:41 +00:00
|
|
|
#endif
|
2002-03-28 15:09:10 +00:00
|
|
|
|
2002-04-27 19:37:41 +00:00
|
|
|
static int parse_direntry(struct fat_direntry *de, unsigned char *buf)
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
2002-05-07 18:56:32 +00:00
|
|
|
int i=0,j=0;
|
2002-04-26 16:44:58 +00:00
|
|
|
memset(de, 0, sizeof(struct fat_direntry));
|
2002-03-28 15:09:10 +00:00
|
|
|
de->attr = buf[FATDIR_ATTR];
|
|
|
|
de->crttimetenth = buf[FATDIR_CRTTIMETENTH];
|
2002-04-26 16:44:58 +00:00
|
|
|
de->crtdate = BYTES2INT16(buf,FATDIR_CRTDATE);
|
|
|
|
de->crttime = BYTES2INT16(buf,FATDIR_CRTTIME);
|
|
|
|
de->wrtdate = BYTES2INT16(buf,FATDIR_WRTDATE);
|
|
|
|
de->wrttime = BYTES2INT16(buf,FATDIR_WRTTIME);
|
|
|
|
de->filesize = BYTES2INT32(buf,FATDIR_FILESIZE);
|
2002-04-27 19:37:41 +00:00
|
|
|
de->firstcluster = BYTES2INT16(buf,FATDIR_FSTCLUSLO) |
|
|
|
|
(BYTES2INT16(buf,FATDIR_FSTCLUSHI) << 16);
|
2002-04-26 16:44:58 +00:00
|
|
|
|
2002-05-07 18:56:32 +00:00
|
|
|
/* fix the name */
|
|
|
|
for (i=0; (i<8) && (buf[FATDIR_NAME+i] != ' '); i++)
|
|
|
|
de->name[j++] = buf[FATDIR_NAME+i];
|
|
|
|
if ( buf[FATDIR_NAME+8] != ' ' ) {
|
|
|
|
de->name[j++] = '.';
|
|
|
|
for (i=8; (i<11) && (buf[FATDIR_NAME+i] != ' '); i++)
|
|
|
|
de->name[j++] = buf[FATDIR_NAME+i];
|
|
|
|
}
|
2002-04-26 16:44:58 +00:00
|
|
|
return 1;
|
2002-03-28 15:09:10 +00:00
|
|
|
}
|
|
|
|
|
2002-05-03 15:35:51 +00:00
|
|
|
int fat_open( unsigned int startcluster, struct fat_file *file)
|
2002-04-27 19:37:41 +00:00
|
|
|
{
|
2002-05-03 15:35:51 +00:00
|
|
|
file->firstcluster = startcluster;
|
|
|
|
file->nextcluster = startcluster;
|
|
|
|
file->nextsector = cluster2sec(startcluster);
|
|
|
|
file->sectornum = 0;
|
2002-04-27 19:37:41 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-05-03 15:35:51 +00:00
|
|
|
int fat_read( struct fat_file *file, int sectorcount, void* buf )
|
2002-04-27 19:37:41 +00:00
|
|
|
{
|
2002-05-03 15:35:51 +00:00
|
|
|
int cluster = file->nextcluster;
|
|
|
|
int sector = file->nextsector;
|
|
|
|
int numsec = file->sectornum;
|
2002-04-27 19:37:41 +00:00
|
|
|
int err, i;
|
|
|
|
|
2002-05-08 12:02:42 +00:00
|
|
|
if ( sector == -1 )
|
|
|
|
return 0;
|
|
|
|
|
2002-04-27 19:37:41 +00:00
|
|
|
for ( i=0; i<sectorcount; i++ ) {
|
2002-05-03 15:35:51 +00:00
|
|
|
err = ata_read_sectors(sector + fat_bpb.startsector, 1,
|
2002-05-03 11:59:53 +00:00
|
|
|
(char*)buf+(i*SECTOR_SIZE));
|
2002-04-27 19:37:41 +00:00
|
|
|
if(err) {
|
2002-05-02 14:05:51 +00:00
|
|
|
DEBUGF( "fat_read() - Couldn't read sector %d"
|
2002-05-08 08:26:38 +00:00
|
|
|
" (error code %d)\n", sector,err);
|
2002-04-27 19:37:41 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
numsec++;
|
2002-05-03 15:35:51 +00:00
|
|
|
if ( numsec >= fat_bpb.bpb_secperclus ) {
|
|
|
|
cluster = get_next_cluster(cluster);
|
2002-05-08 12:02:42 +00:00
|
|
|
if (!cluster) {
|
|
|
|
/* end of file */
|
|
|
|
sector = -1;
|
|
|
|
break;
|
|
|
|
}
|
2002-04-27 19:37:41 +00:00
|
|
|
|
2002-05-03 15:35:51 +00:00
|
|
|
sector = cluster2sec(cluster);
|
2002-04-27 19:37:41 +00:00
|
|
|
if (sector<0)
|
|
|
|
return -1;
|
|
|
|
numsec=0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
sector++;
|
|
|
|
}
|
2002-05-03 15:35:51 +00:00
|
|
|
file->nextcluster = cluster;
|
|
|
|
file->nextsector = sector;
|
|
|
|
file->sectornum = numsec;
|
2002-04-27 19:37:41 +00:00
|
|
|
|
|
|
|
return sectorcount;
|
|
|
|
}
|
|
|
|
|
2002-05-03 15:35:51 +00:00
|
|
|
int fat_seek(struct fat_file *file, int seeksector )
|
2002-04-27 20:03:05 +00:00
|
|
|
{
|
2002-05-03 15:35:51 +00:00
|
|
|
int cluster = file->firstcluster;
|
2002-05-27 14:04:18 +00:00
|
|
|
int sector = cluster2sec(cluster);
|
2002-04-27 20:03:05 +00:00
|
|
|
int numsec = 0;
|
|
|
|
int i;
|
|
|
|
|
2002-05-08 15:16:02 +00:00
|
|
|
if ( seeksector ) {
|
|
|
|
for (i=0; i<seeksector; i++) {
|
|
|
|
numsec++;
|
|
|
|
if ( numsec >= fat_bpb.bpb_secperclus ) {
|
|
|
|
cluster = get_next_cluster(cluster);
|
|
|
|
if (!cluster)
|
|
|
|
/* end of file */
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
sector = cluster2sec(cluster);
|
|
|
|
if (sector<0)
|
|
|
|
return -2;
|
|
|
|
numsec=0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
sector++;
|
2002-04-27 20:03:05 +00:00
|
|
|
}
|
|
|
|
}
|
2002-05-08 15:16:02 +00:00
|
|
|
|
2002-05-03 15:35:51 +00:00
|
|
|
file->nextcluster = cluster;
|
|
|
|
file->nextsector = sector;
|
|
|
|
file->sectornum = numsec;
|
2002-04-27 20:03:05 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2002-04-27 19:37:41 +00:00
|
|
|
|
2002-05-03 15:35:51 +00:00
|
|
|
int fat_opendir(struct fat_dir *dir, unsigned int currdir)
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
|
|
|
int is_rootdir = (currdir == 0);
|
|
|
|
unsigned int sec;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if(is_rootdir)
|
|
|
|
{
|
2002-05-03 15:35:51 +00:00
|
|
|
sec = fat_bpb.rootdirsector;
|
2002-03-28 15:09:10 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2002-05-03 15:35:51 +00:00
|
|
|
sec = first_sector_of_cluster(currdir);
|
2002-03-28 15:09:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Read the first sector in the current dir */
|
2002-05-03 15:35:51 +00:00
|
|
|
err = ata_read_sectors(sec + fat_bpb.startsector,1,dir->cached_buf);
|
2002-04-22 12:33:04 +00:00
|
|
|
if(err)
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
2002-05-08 08:26:38 +00:00
|
|
|
DEBUGF( "fat_opendir() - Couldn't read dir sector"
|
|
|
|
" (error code %d)\n", err);
|
2002-03-28 15:09:10 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2002-05-03 15:35:51 +00:00
|
|
|
dir->entry = 0;
|
|
|
|
dir->cached_sec = sec;
|
|
|
|
dir->num_sec = 0;
|
2002-04-27 01:25:22 +00:00
|
|
|
|
2002-03-28 15:09:10 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-05-03 15:35:51 +00:00
|
|
|
int fat_getnext(struct fat_dir *dir, struct fat_direntry *entry)
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
|
|
|
int done = 0;
|
|
|
|
int i;
|
|
|
|
int err;
|
|
|
|
unsigned char firstbyte;
|
2002-04-27 22:32:37 +00:00
|
|
|
int longarray[20];
|
|
|
|
int longs=0;
|
|
|
|
int sectoridx=0;
|
2002-03-28 15:09:10 +00:00
|
|
|
|
|
|
|
while(!done)
|
|
|
|
{
|
2002-05-03 15:35:51 +00:00
|
|
|
for(i = dir->entry;i < SECTOR_SIZE/32;i++)
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
2002-05-03 15:35:51 +00:00
|
|
|
firstbyte = dir->cached_buf[i*32];
|
2002-04-27 22:32:37 +00:00
|
|
|
|
2002-03-28 15:09:10 +00:00
|
|
|
if(firstbyte == 0xe5)
|
2002-04-27 22:32:37 +00:00
|
|
|
/* free entry */
|
2002-03-28 15:09:10 +00:00
|
|
|
continue;
|
|
|
|
|
2002-05-03 15:35:51 +00:00
|
|
|
if(firstbyte == 0) {
|
2002-04-27 22:32:37 +00:00
|
|
|
/* last entry */
|
2002-05-03 15:35:51 +00:00
|
|
|
entry->name[0] = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
2002-03-28 15:09:10 +00:00
|
|
|
|
2002-04-27 22:32:37 +00:00
|
|
|
/* longname entry? */
|
2002-05-03 15:35:51 +00:00
|
|
|
if ( ( dir->cached_buf[i*32 + FATDIR_ATTR] &
|
2002-04-27 22:32:37 +00:00
|
|
|
FAT_ATTR_LONG_NAME_MASK ) == FAT_ATTR_LONG_NAME ) {
|
|
|
|
longarray[longs++] = i*32 + sectoridx;
|
|
|
|
}
|
|
|
|
else {
|
2002-05-03 15:35:51 +00:00
|
|
|
if ( parse_direntry(entry, &dir->cached_buf[i*32]) ) {
|
2002-04-27 22:32:37 +00:00
|
|
|
|
2002-06-19 17:31:35 +00:00
|
|
|
/* don't return volume id entry */
|
|
|
|
if ( entry->attr == FAT_ATTR_VOLUME_ID )
|
|
|
|
continue;
|
|
|
|
|
2002-04-27 22:32:37 +00:00
|
|
|
/* replace shortname with longname? */
|
|
|
|
if ( longs ) {
|
|
|
|
int j,k,l=0;
|
|
|
|
|
|
|
|
/* iterate backwards through the dir entries */
|
|
|
|
for (j=longs-1; j>=0; j--) {
|
2002-05-03 15:35:51 +00:00
|
|
|
unsigned char* ptr = dir->cached_buf;
|
2002-04-27 22:32:37 +00:00
|
|
|
int index = longarray[j];
|
|
|
|
|
|
|
|
/* current or cached sector? */
|
|
|
|
if ( sectoridx >= SECTOR_SIZE ) {
|
|
|
|
if ( sectoridx >= SECTOR_SIZE*2 ) {
|
2002-04-27 22:45:23 +00:00
|
|
|
if ( ( index >= SECTOR_SIZE ) &&
|
|
|
|
( index < SECTOR_SIZE*2 ))
|
|
|
|
ptr = lastsector;
|
2002-04-27 22:32:37 +00:00
|
|
|
else
|
|
|
|
ptr = lastsector2;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if ( index < SECTOR_SIZE )
|
|
|
|
ptr = lastsector;
|
|
|
|
}
|
|
|
|
|
|
|
|
index &= SECTOR_SIZE-1;
|
|
|
|
}
|
|
|
|
|
2002-04-29 08:03:59 +00:00
|
|
|
/* piece together the name subcomponents.
|
|
|
|
names are stored in unicode, but we
|
|
|
|
only grab the low byte (iso8859-1).
|
|
|
|
*/
|
2002-04-27 22:32:37 +00:00
|
|
|
for (k=0; k<5; k++)
|
|
|
|
entry->name[l++] = ptr[index + k*2 + 1];
|
|
|
|
for (k=0; k<6; k++)
|
|
|
|
entry->name[l++] = ptr[index + k*2 + 14];
|
|
|
|
for (k=0; k<2; k++)
|
|
|
|
entry->name[l++] = ptr[index + k*2 + 28];
|
|
|
|
}
|
|
|
|
entry->name[l]=0;
|
|
|
|
}
|
|
|
|
done = 1;
|
|
|
|
break;
|
|
|
|
}
|
2002-04-26 16:44:58 +00:00
|
|
|
}
|
2002-03-28 15:09:10 +00:00
|
|
|
}
|
|
|
|
|
2002-04-27 22:32:37 +00:00
|
|
|
/* save this sector, for longname use */
|
|
|
|
if ( sectoridx )
|
2002-05-03 15:35:51 +00:00
|
|
|
memcpy( lastsector2, dir->cached_buf, SECTOR_SIZE );
|
2002-04-27 22:32:37 +00:00
|
|
|
else
|
2002-05-03 15:35:51 +00:00
|
|
|
memcpy( lastsector, dir->cached_buf, SECTOR_SIZE );
|
2002-04-27 22:32:37 +00:00
|
|
|
sectoridx += SECTOR_SIZE;
|
|
|
|
|
2002-03-28 15:09:10 +00:00
|
|
|
/* Next sector? */
|
2002-04-27 19:37:41 +00:00
|
|
|
if(i < SECTOR_SIZE/32)
|
2002-04-27 01:25:22 +00:00
|
|
|
{
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
else
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
2002-05-03 15:35:51 +00:00
|
|
|
dir->num_sec++;
|
2002-04-27 01:25:22 +00:00
|
|
|
|
2002-03-28 15:09:10 +00:00
|
|
|
/* Do we need to advance one cluster? */
|
2002-05-03 15:35:51 +00:00
|
|
|
if(dir->num_sec < fat_bpb.bpb_secperclus)
|
2002-04-27 01:25:22 +00:00
|
|
|
{
|
2002-05-03 15:35:51 +00:00
|
|
|
dir->cached_sec++;
|
2002-04-27 01:25:22 +00:00
|
|
|
}
|
|
|
|
else
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
2002-05-03 15:35:51 +00:00
|
|
|
int cluster = sec2cluster(dir->cached_sec);
|
2002-04-26 16:44:58 +00:00
|
|
|
if ( cluster < 0 ) {
|
2002-05-02 14:05:51 +00:00
|
|
|
DEBUGF("sec2cluster failed\n");
|
2002-04-26 16:44:58 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2002-05-03 15:35:51 +00:00
|
|
|
dir->num_sec = 0;
|
|
|
|
cluster = get_next_cluster( cluster );
|
2002-04-27 01:25:22 +00:00
|
|
|
if(!cluster)
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
2002-05-02 14:05:51 +00:00
|
|
|
DEBUGF("End of cluster chain.\n");
|
2002-04-27 01:25:22 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2002-05-03 15:35:51 +00:00
|
|
|
dir->cached_sec = cluster2sec(cluster);
|
|
|
|
if ( dir->cached_sec < 0 )
|
2002-04-27 01:25:22 +00:00
|
|
|
{
|
2002-05-02 14:05:51 +00:00
|
|
|
DEBUGF("Invalid cluster: %d\n",cluster);
|
2002-03-28 15:09:10 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2002-04-27 01:25:22 +00:00
|
|
|
}
|
|
|
|
|
2002-03-28 15:09:10 +00:00
|
|
|
/* Read the next sector */
|
2002-05-03 15:35:51 +00:00
|
|
|
err = ata_read_sectors(dir->cached_sec + fat_bpb.startsector, 1,
|
|
|
|
dir->cached_buf);
|
2002-04-22 12:33:04 +00:00
|
|
|
if(err)
|
2002-03-28 15:09:10 +00:00
|
|
|
{
|
2002-05-02 14:05:51 +00:00
|
|
|
DEBUGF( "fat_getnext() - Couldn't read dir sector"
|
2002-05-08 08:26:38 +00:00
|
|
|
" (error code %d)\n", err);
|
2002-03-28 15:09:10 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
}
|
2002-05-03 15:35:51 +00:00
|
|
|
dir->entry = i;
|
2002-03-28 15:09:10 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2002-06-19 17:31:35 +00:00
|
|
|
|
|
|
|
/* -----------------------------------------------------------------
|
|
|
|
* local variables:
|
|
|
|
* eval: (load-file "../rockbox-mode.el")
|
|
|
|
* end:
|
|
|
|
*/
|