1
0
Fork 0
forked from len0rd/rockbox

Added fat_open() and fat_read().

Renamed BLOCK_SIZE to SECTOR_SIZE.
Added #ifdef DISK_WRITE to all write code.


git-svn-id: svn://svn.rockbox.org/rockbox/trunk@269 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
Björn Stenberg 2002-04-27 19:37:41 +00:00
parent 31500d1824
commit e8bcc01edf
2 changed files with 89 additions and 15 deletions

View file

@ -115,13 +115,13 @@ struct fsinfo {
static int first_sector_of_cluster(struct bpb *bpb, unsigned int cluster); static int first_sector_of_cluster(struct bpb *bpb, unsigned int cluster);
static int get_bpb(struct bpb *bpb); static int get_bpb(struct bpb *bpb);
static int bpb_is_sane(struct bpb *bpb); static int bpb_is_sane(struct bpb *bpb);
static int flush_fat(struct bpb *bpb);
static void *cache_fat_sector(struct bpb *bpb, int secnum); static void *cache_fat_sector(struct bpb *bpb, int secnum);
static int update_entry(struct bpb *bpb, int entry, unsigned int val); #ifdef DISK_WRITE
static unsigned int getcurrdostime(unsigned short *dosdate, static unsigned int getcurrdostime(unsigned short *dosdate,
unsigned short *dostime, unsigned short *dostime,
unsigned char *dostenth); unsigned char *dostenth);
static int create_dos_name(unsigned char *name, unsigned char *newname); static int create_dos_name(unsigned char *name, unsigned char *newname);
#endif
static unsigned char *fat_cache[256]; static unsigned char *fat_cache[256];
static int fat_cache_dirty[256]; static int fat_cache_dirty[256];
@ -189,7 +189,7 @@ static int first_sector_of_cluster(struct bpb *bpb, unsigned int cluster)
static int get_bpb(struct bpb *bpb) static int get_bpb(struct bpb *bpb)
{ {
unsigned char buf[BLOCK_SIZE]; unsigned char buf[SECTOR_SIZE];
int err; int err;
int datasec; int datasec;
int countofclusters; int countofclusters;
@ -284,8 +284,9 @@ static int bpb_is_sane(struct bpb *bpb)
{ {
if(bpb->bpb_bytspersec != 512) if(bpb->bpb_bytspersec != 512)
{ {
DEBUG1( "bpb_is_sane() - Warning: sector size is not 512 (%i)\n", DEBUG1( "bpb_is_sane() - Error: sector size is not 512 (%i)\n",
bpb->bpb_bytspersec); bpb->bpb_bytspersec);
return -1;
} }
if(bpb->bpb_secperclus * bpb->bpb_bytspersec > 32768) if(bpb->bpb_secperclus * bpb->bpb_bytspersec > 32768)
{ {
@ -365,6 +366,7 @@ static void *cache_fat_sector(struct bpb *bpb, int secnum)
return sec; return sec;
} }
#ifdef DISK_WRITE
static int update_entry(struct bpb *bpb, int entry, unsigned int val) static int update_entry(struct bpb *bpb, int entry, unsigned int val)
{ {
unsigned long *sec; unsigned long *sec;
@ -393,6 +395,7 @@ static int update_entry(struct bpb *bpb, int entry, unsigned int val)
return 0; return 0;
} }
#endif
static int read_entry(struct bpb *bpb, int entry) static int read_entry(struct bpb *bpb, int entry)
{ {
@ -430,6 +433,7 @@ static int get_next_cluster(struct bpb *bpb, unsigned int cluster)
return next_cluster; return next_cluster;
} }
#ifdef DISK_WRITE
static int flush_fat(struct bpb *bpb) static int flush_fat(struct bpb *bpb)
{ {
int i; int i;
@ -495,7 +499,7 @@ static int add_dir_entry(struct bpb *bpb,
unsigned int currdir, unsigned int currdir,
struct fat_direntry *de) struct fat_direntry *de)
{ {
unsigned char buf[BLOCK_SIZE]; unsigned char buf[SECTOR_SIZE];
unsigned char *eptr; unsigned char *eptr;
int i; int i;
int err; int err;
@ -577,7 +581,7 @@ static int add_dir_entry(struct bpb *bpb,
else else
{ {
/* Look for a free slot */ /* Look for a free slot */
for(i = 0;i < BLOCK_SIZE;i+=32) for(i = 0;i < SECTOR_SIZE;i+=32)
{ {
firstbyte = buf[i]; firstbyte = buf[i];
if(firstbyte == 0xe5 || firstbyte == 0) if(firstbyte == 0xe5 || firstbyte == 0)
@ -610,7 +614,7 @@ static int add_dir_entry(struct bpb *bpb,
if(firstbyte == 0) if(firstbyte == 0)
{ {
i += 32; i += 32;
if(i < BLOCK_SIZE) if(i < SECTOR_SIZE)
{ {
buf[i] = 0; buf[i] = 0;
/* We are done */ /* We are done */
@ -780,8 +784,9 @@ int fat_create_file(struct bpb *bpb, unsigned int currdir, char *name)
err = add_dir_entry(bpb, currdir, &de); err = add_dir_entry(bpb, currdir, &de);
return err; return err;
} }
#endif
static int parse_direntry(struct fat_direntry *de, char *buf) static int parse_direntry(struct fat_direntry *de, unsigned char *buf)
{ {
/* is this a long filename entry? */ /* is this a long filename entry? */
if ( ( buf[FATDIR_ATTR] & FAT_ATTR_LONG_NAME_MASK ) == if ( ( buf[FATDIR_ATTR] & FAT_ATTR_LONG_NAME_MASK ) ==
@ -798,11 +803,64 @@ static int parse_direntry(struct fat_direntry *de, char *buf)
de->wrtdate = BYTES2INT16(buf,FATDIR_WRTDATE); de->wrtdate = BYTES2INT16(buf,FATDIR_WRTDATE);
de->wrttime = BYTES2INT16(buf,FATDIR_WRTTIME); de->wrttime = BYTES2INT16(buf,FATDIR_WRTTIME);
de->filesize = BYTES2INT32(buf,FATDIR_FILESIZE); de->filesize = BYTES2INT32(buf,FATDIR_FILESIZE);
de->firstcluster = BYTES2INT16(buf,FATDIR_FSTCLUSLO) |
(BYTES2INT16(buf,FATDIR_FSTCLUSHI) << 16);
strncpy(de->name, &buf[FATDIR_NAME], 11); strncpy(de->name, &buf[FATDIR_NAME], 11);
return 1; return 1;
} }
int fat_open(struct bpb *bpb,
unsigned int startcluster,
struct fat_fileent *ent)
{
ent->firstcluster = startcluster;
ent->nextcluster = startcluster;
ent->nextsector = cluster2sec(bpb,startcluster);
ent->sectornum = 0;
return 0;
}
int fat_read(struct bpb *bpb,
struct fat_fileent *ent,
int sectorcount,
void* buf )
{
int cluster = ent->nextcluster;
int sector = ent->nextsector;
int numsec = ent->sectornum;
int err, i;
for ( i=0; i<sectorcount; i++ ) {
err = ata_read_sectors(sector,1,(char*)buf+(i*SECTOR_SIZE));
if(err) {
DEBUG2( "fat_read() - Couldn't read sector %d"
" (error code %i)\n", sector,err);
return -1;
}
numsec++;
if ( numsec >= bpb->bpb_secperclus ) {
cluster = get_next_cluster(bpb,cluster);
if (!cluster)
break; /* end of file */
sector = cluster2sec(bpb,cluster);
if (sector<0)
return -1;
numsec=0;
}
else
sector++;
}
ent->nextcluster = cluster;
ent->nextsector = sector;
ent->sectornum = numsec;
return sectorcount;
}
int fat_opendir(struct bpb *bpb, int fat_opendir(struct bpb *bpb,
struct fat_dirent *ent, struct fat_dirent *ent,
unsigned int currdir) unsigned int currdir)
@ -848,7 +906,7 @@ int fat_getnext(struct bpb *bpb,
while(!done) while(!done)
{ {
/* Look for a free slot */ /* Look for a free slot */
for(i = ent->entry;i < BLOCK_SIZE/32;i++) for(i = ent->entry;i < SECTOR_SIZE/32;i++)
{ {
firstbyte = ent->cached_buf[i*32]; firstbyte = ent->cached_buf[i*32];
if(firstbyte == 0xe5) if(firstbyte == 0xe5)
@ -865,7 +923,7 @@ int fat_getnext(struct bpb *bpb,
} }
/* Next sector? */ /* Next sector? */
if(i < BLOCK_SIZE/32) if(i < SECTOR_SIZE/32)
{ {
i++; i++;
} }

View file

@ -20,7 +20,7 @@
#ifndef FAT_H #ifndef FAT_H
#define FAT_H #define FAT_H
#define BLOCK_SIZE 512 #define SECTOR_SIZE 512
struct bpb struct bpb
{ {
@ -71,12 +71,12 @@ struct fat_direntry
unsigned short crttime; /* Creation time */ unsigned short crttime; /* Creation time */
unsigned short crtdate; /* Creation date */ unsigned short crtdate; /* Creation date */
unsigned short lstaccdate; /* Last access date */ unsigned short lstaccdate; /* Last access date */
unsigned short fstclushi; /* High word of first cluster
(0 for FAT12/16) */
unsigned short wrttime; /* Last write time */ unsigned short wrttime; /* Last write time */
unsigned short wrtdate; /* Last write date */ unsigned short wrtdate; /* Last write date */
unsigned short fstcluslo; /* Low word of first cluster */
unsigned int filesize; /* File size in bytes */ unsigned int filesize; /* File size in bytes */
unsigned short fstclusterlo;
unsigned short fstclusterhi;
int firstcluster; /* fstclusterhi<<16 + fstcluslo */
}; };
#define FAT_ATTR_READ_ONLY 0x01 #define FAT_ATTR_READ_ONLY 0x01
@ -91,7 +91,15 @@ struct fat_dirent
int entry; int entry;
unsigned int cached_sec; unsigned int cached_sec;
unsigned int num_sec; unsigned int num_sec;
char cached_buf[BLOCK_SIZE]; char cached_buf[SECTOR_SIZE];
};
struct fat_fileent
{
int firstcluster; /* first cluster in file */
int nextcluster; /* cluster of last access */
int nextsector; /* sector of last access */
int sectornum; /* sector number in this cluster */
}; };
extern int fat_create_file(struct bpb *bpb, extern int fat_create_file(struct bpb *bpb,
@ -100,6 +108,14 @@ extern int fat_create_file(struct bpb *bpb,
extern int fat_create_dir(struct bpb *bpb, extern int fat_create_dir(struct bpb *bpb,
unsigned int currdir, unsigned int currdir,
char *name); char *name);
extern int fat_open(struct bpb *bpb,
unsigned int cluster,
struct fat_fileent *ent);
extern int fat_read(struct bpb *bpb,
struct fat_fileent *ent,
int sectorcount,
void* buf );
extern int fat_opendir(struct bpb *bpb, extern int fat_opendir(struct bpb *bpb,
struct fat_dirent *ent, struct fat_dirent *ent,
unsigned int currdir); unsigned int currdir);