Fat32 write updates: Nixed some bugs. Basic file creation now works. Todo: Long filenames and extensive test cases.

git-svn-id: svn://svn.rockbox.org/rockbox/trunk@2742 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
Björn Stenberg 2002-10-22 15:06:08 +00:00
parent a901c3a433
commit 46ddacf533
5 changed files with 121 additions and 106 deletions

View file

@ -178,7 +178,7 @@ int close(int fd)
} }
/* tie up all loose ends */ /* tie up all loose ends */
fat_closewrite(&(openfiles[fd].fatfile), openfiles[fd].size); fat_closewrite(&(openfiles[fd].fatfile), openfiles[fd].fileoffset);
} }
openfiles[fd].busy = false; openfiles[fd].busy = false;
return rc; return rc;
@ -239,8 +239,6 @@ static int readwrite(int fd, void* buf, int count, bool write)
nread = headbytes; nread = headbytes;
count -= headbytes; count -= headbytes;
LDEBUGF("readwrite incache: offs=%d\n",openfiles[fd].cacheoffset);
} }
/* read whole sectors right into the supplied buffer */ /* read whole sectors right into the supplied buffer */
@ -316,6 +314,13 @@ int lseek(int fd, int offset, int whence)
return -1; return -1;
} }
if ( openfiles[fd].write ) {
DEBUGF("lseek() is not supported when writing\n");
errno = EROFS;
return -2;
}
switch ( whence ) { switch ( whence ) {
case SEEK_SET: case SEEK_SET:
pos = offset; pos = offset;

View file

@ -241,8 +241,7 @@ int fat_mount(int startsector)
err = ata_read_sectors(startsector,1,buf); err = ata_read_sectors(startsector,1,buf);
if(err) if(err)
{ {
DEBUGF( "fat_mount() - Couldn't read BPB (error code %d)\n", DEBUGF( "fat_mount() - Couldn't read BPB (error code %d)\n", err);
err);
return -1; return -1;
} }
@ -325,7 +324,7 @@ int fat_mount(int startsector)
if (err) if (err)
{ {
DEBUGF( "fat_mount() - Couldn't read FSInfo (error code %d)\n", err); DEBUGF( "fat_mount() - Couldn't read FSInfo (error code %d)\n", err);
return -1; return -4;
} }
fat_bpb.fsinfo.freecount = BYTES2INT32(buf, FSINFO_FREECOUNT); fat_bpb.fsinfo.freecount = BYTES2INT32(buf, FSINFO_FREECOUNT);
fat_bpb.fsinfo.nextfree = BYTES2INT32(buf, FSINFO_NEXTFREE); fat_bpb.fsinfo.nextfree = BYTES2INT32(buf, FSINFO_NEXTFREE);
@ -430,8 +429,6 @@ static int find_free_cluster(int startcluster)
int offset = startcluster % CLUSTERS_PER_FAT_SECTOR; int offset = startcluster % CLUSTERS_PER_FAT_SECTOR;
int i; int i;
LDEBUGF("find_free_cluster(%x)\n",startcluster);
for (i = sector; i<fat_bpb.fatsize; i++) { for (i = sector; i<fat_bpb.fatsize; i++) {
int j; int j;
unsigned int* fat = cache_fat_sector(i); unsigned int* fat = cache_fat_sector(i);
@ -440,15 +437,16 @@ static int find_free_cluster(int startcluster)
for (j = offset; j < CLUSTERS_PER_FAT_SECTOR; j++) for (j = offset; j < CLUSTERS_PER_FAT_SECTOR; j++)
if (!(SWAB32(fat[j]) & 0x0fffffff)) { if (!(SWAB32(fat[j]) & 0x0fffffff)) {
int c = i * CLUSTERS_PER_FAT_SECTOR + j; int c = i * CLUSTERS_PER_FAT_SECTOR + j;
LDEBUGF("Found free cluster %x\n",c); LDEBUGF("find_free_cluster(%x) == %x\n",startcluster,c);
fat_bpb.fsinfo.nextfree = c; fat_bpb.fsinfo.nextfree = c;
return c; return c;
} }
} }
LDEBUGF("find_free_cluster(%x) == 0\n",startcluster);
return 0; /* 0 is an illegal cluster number */ return 0; /* 0 is an illegal cluster number */
} }
static int update_fat_entry(int entry, unsigned int val) static int update_fat_entry(unsigned int entry, unsigned int val)
{ {
int sector = entry / CLUSTERS_PER_FAT_SECTOR; int sector = entry / CLUSTERS_PER_FAT_SECTOR;
int offset = entry % CLUSTERS_PER_FAT_SECTOR; int offset = entry % CLUSTERS_PER_FAT_SECTOR;
@ -456,6 +454,12 @@ static int update_fat_entry(int entry, unsigned int val)
LDEBUGF("update_fat_entry(%x,%x)\n",entry,val); LDEBUGF("update_fat_entry(%x,%x)\n",entry,val);
if (entry==val)
panicf("Creating FAT loop: %x,%x\n",entry,val);
if ( entry < 2 )
panicf("Updating reserved FAT entry %d.\n",entry);
sec = cache_fat_sector(sector); sec = cache_fat_sector(sector);
if (!sec) if (!sec)
{ {
@ -465,14 +469,14 @@ static int update_fat_entry(int entry, unsigned int val)
fat_cache[sector & FAT_CACHE_MASK].dirty = true; fat_cache[sector & FAT_CACHE_MASK].dirty = true;
if ( val ) { if ( val ) {
if (!(sec[offset] & 0x0fffffff)) if (!(SWAB32(sec[offset]) & 0x0fffffff))
fat_bpb.fsinfo.freecount--; fat_bpb.fsinfo.freecount--;
} }
else else
fat_bpb.fsinfo.freecount++; fat_bpb.fsinfo.freecount++;
/* don't change top 4 bits */ /* don't change top 4 bits */
sec[offset] &= SWAB32(0xf000000); sec[offset] &= SWAB32(0xf0000000);
sec[offset] |= SWAB32(val & 0x0fffffff); sec[offset] |= SWAB32(val & 0x0fffffff);
return 0; return 0;
@ -491,7 +495,7 @@ static int read_fat_entry(int entry)
return -1; return -1;
} }
return SWAB32(sec[offset]); return SWAB32(sec[offset]) & 0x0fffffff;
} }
static int get_next_cluster(unsigned int cluster) static int get_next_cluster(unsigned int cluster)
@ -499,6 +503,7 @@ static int get_next_cluster(unsigned int cluster)
int next_cluster; int next_cluster;
next_cluster = read_fat_entry(cluster); next_cluster = read_fat_entry(cluster);
LDEBUGF("get_next_cluster(%x) == %x\n",cluster,next_cluster);
/* is this last cluster in chain? */ /* is this last cluster in chain? */
if ( next_cluster >= FAT_EOF_MARK ) if ( next_cluster >= FAT_EOF_MARK )
@ -608,34 +613,37 @@ static int add_dir_entry(struct fat_dir* dir,
int i; int i;
int err; int err;
int sec; int sec;
int sec_cnt; int sec_cnt = 0;
bool need_to_update_last_empty_marker = false; bool need_to_update_last_empty_marker = false;
bool done = false; bool done = false;
unsigned char firstbyte; unsigned char firstbyte;
int currdir = dir->startcluster; int currdir = dir->startcluster;
bool is_rootdir = (currdir == 0); bool is_rootdir = (currdir == 0);
LDEBUGF( "add_dir_entry()\n"); LDEBUGF( "add_dir_entry(%x,%s,%x)\n",
dir->startcluster, de->name, file->firstcluster);
if (is_rootdir) if (is_rootdir)
sec = fat_bpb.rootdirsector; sec = fat_bpb.rootdirsector;
else else
sec = first_sector_of_cluster(currdir); sec = first_sector_of_cluster(currdir);
sec_cnt = 0;
while(!done) while(!done)
{ {
if (sec_cnt >= fat_bpb.bpb_secperclus) if (sec_cnt >= fat_bpb.bpb_secperclus)
{ {
int oldcluster = currdir; int oldcluster;
/* We have reached the end of this cluster */ if (!currdir)
LDEBUGF("Moving to the next cluster..."); currdir = sec2cluster(fat_bpb.rootdirsector);
currdir = get_next_cluster(currdir); oldcluster = currdir;
LDEBUGF("new cluster is %d\n", currdir);
/* We have reached the end of this cluster */
LDEBUGF("Moving to the next cluster...\n");
currdir = get_next_cluster(currdir);
if (!currdir) if (!currdir)
{ {
/* end of dir, add new cluster */
LDEBUGF("Adding cluster to dir\n");
currdir = find_free_cluster(fat_bpb.fsinfo.nextfree); currdir = find_free_cluster(fat_bpb.fsinfo.nextfree);
if (!currdir) { if (!currdir) {
currdir = find_free_cluster(0); currdir = find_free_cluster(0);
@ -646,9 +654,11 @@ static int add_dir_entry(struct fat_dir* dir,
} }
update_fat_entry(oldcluster, currdir); update_fat_entry(oldcluster, currdir);
} }
LDEBUGF("new cluster is %x\n", currdir);
sec = cluster2sec(currdir);
} }
LDEBUGF("Reading sector %d...\n", sec); LDEBUGF("Reading sector %x...\n", sec);
/* Read the next sector in the current dir */ /* Read the next sector in the current dir */
err = ata_read_sectors(sec + fat_bpb.startsector,1,buf); err = ata_read_sectors(sec + fat_bpb.startsector,1,buf);
if (err) if (err)
@ -673,7 +683,7 @@ static int add_dir_entry(struct fat_dir* dir,
firstbyte = buf[i]; firstbyte = buf[i];
if (firstbyte == 0xe5 || firstbyte == 0) if (firstbyte == 0xe5 || firstbyte == 0)
{ {
LDEBUGF("Found free slot at entry %d in sector %x\n", LDEBUGF("Found free entry %d in sector %x\n",
i/DIR_ENTRY_SIZE, sec); i/DIR_ENTRY_SIZE, sec);
eptr = &buf[i]; eptr = &buf[i];
memset(eptr, 0, DIR_ENTRY_SIZE); memset(eptr, 0, DIR_ENTRY_SIZE);
@ -681,22 +691,6 @@ static int add_dir_entry(struct fat_dir* dir,
eptr[FATDIR_ATTR] = de->attr; eptr[FATDIR_ATTR] = de->attr;
eptr[FATDIR_NTRES] = 0; 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;
/* remember where the dir entry is located */ /* remember where the dir entry is located */
file->dirsector = sec; file->dirsector = sec;
file->direntry = i/DIR_ENTRY_SIZE; file->direntry = i/DIR_ENTRY_SIZE;
@ -854,14 +848,20 @@ static void update_dir_entry( struct fat_file* file, int size )
return; return;
} }
clusptr = (short*)(entry + FATDIR_FSTCLUSHI); if ( size == -1 ) {
*clusptr = SWAB16(file->firstcluster >> 16); /* mark entry deleted */
entry[0] = 0xe5;
}
else {
clusptr = (short*)(entry + FATDIR_FSTCLUSHI);
*clusptr = SWAB16(file->firstcluster >> 16);
clusptr = (short*)(entry + FATDIR_FSTCLUSLO); clusptr = (short*)(entry + FATDIR_FSTCLUSLO);
*clusptr = SWAB16(file->firstcluster & 0xffff); *clusptr = SWAB16(file->firstcluster & 0xffff);
sizeptr = (int*)(entry + FATDIR_FILESIZE); sizeptr = (int*)(entry + FATDIR_FILESIZE);
*sizeptr = SWAB32(size); *sizeptr = SWAB32(size);
}
err = ata_write_sectors(sector, 1, buf); err = ata_write_sectors(sector, 1, buf);
if (err) if (err)
@ -902,8 +902,8 @@ int fat_open(unsigned int startcluster,
struct fat_dir* dir) struct fat_dir* dir)
{ {
file->firstcluster = startcluster; file->firstcluster = startcluster;
file->nextcluster = startcluster; file->lastcluster = startcluster;
file->nextsector = cluster2sec(startcluster); file->lastsector = cluster2sec(startcluster);
file->sectornum = 0; file->sectornum = 0;
/* remember where the file's dir entry is located */ /* remember where the file's dir entry is located */
@ -935,8 +935,8 @@ int fat_create_file(char* name,
err = add_dir_entry(dir, &de, file); err = add_dir_entry(dir, &de, file);
if (!err) { if (!err) {
file->firstcluster = 0; file->firstcluster = 0;
file->nextcluster = 0; file->lastcluster = 0;
file->nextsector = 0; file->lastsector = 0;
file->sectornum = 0; file->sectornum = 0;
} }
@ -945,21 +945,38 @@ int fat_create_file(char* name,
int fat_closewrite(struct fat_file *file, int size) int fat_closewrite(struct fat_file *file, int size)
{ {
int endcluster = file->nextcluster; int next, last = file->lastcluster;
int next, last = endcluster; int endcluster = last;
/* free unused clusters, if any */ LDEBUGF("fat_closewrite()\n");
for ( next = get_next_cluster(last); next; last = next ) {
LDEBUGF("Clearing cluster %x\n",last); last = get_next_cluster(last);
while ( last && last != FAT_EOF_MARK ) {
next = get_next_cluster(last);
update_fat_entry(last,0); update_fat_entry(last,0);
last = next;
} }
/* mark last used cluster as last in chain */
update_fat_entry(endcluster, FAT_EOF_MARK); update_fat_entry(endcluster, FAT_EOF_MARK);
update_dir_entry(file, size);
flush_fat(); flush_fat();
update_dir_entry(file, size); return 0;
}
int fat_remove(struct fat_file* file)
{
int next, last = file->firstcluster;
LDEBUGF("fat_remove(%x)\n",last);
while ( last != FAT_EOF_MARK ) {
LDEBUGF("Freeing cluster %x\n",last);
next = get_next_cluster(last);
update_fat_entry(last,0);
last = next;
}
update_dir_entry(file, -1);
return 0; return 0;
} }
@ -967,15 +984,15 @@ int fat_closewrite(struct fat_file *file, int size)
int fat_readwrite( struct fat_file *file, int sectorcount, int fat_readwrite( struct fat_file *file, int sectorcount,
void* buf, bool write ) void* buf, bool write )
{ {
int cluster = file->nextcluster; int cluster = file->lastcluster;
int sector = file->nextsector; int sector = file->lastsector;
int numsec = file->sectornum; int numsec = file->sectornum;
int first, last; int first=0, last=0;
int err, i; int err, i;
LDEBUGF( "fat_readwrite(file:%x,count:%d,buf:%x,%s)\n", LDEBUGF( "fat_readwrite(file:%x,count:%d,buf:%x,%s)\n",
cluster,sectorcount,buf,write?"write":"read"); cluster,sectorcount,buf,write?"write":"read");
LDEBUGF( "fat_readwrite: c=%x s=%x n=%d\n", cluster,sector,numsec);
if ( sector == -1 ) if ( sector == -1 )
return 0; return 0;
@ -995,12 +1012,8 @@ int fat_readwrite( struct fat_file *file, int sectorcount,
return -1; return -1;
} }
first = sector;
last = sector;
/* find sequential sectors and read/write them all at once */ /* find sequential sectors and read/write them all at once */
for (i=0; i<sectorcount && sector>=0; i++ ) { for (i=0; i<sectorcount && sector>=0; i++ ) {
numsec++;
if ( numsec >= fat_bpb.bpb_secperclus ) { if ( numsec >= fat_bpb.bpb_secperclus ) {
int oldcluster = cluster; int oldcluster = cluster;
cluster = get_next_cluster(cluster); cluster = get_next_cluster(cluster);
@ -1008,9 +1021,9 @@ int fat_readwrite( struct fat_file *file, int sectorcount,
if ( write ) { if ( write ) {
/* writing past end-of-file, /* writing past end-of-file,
find a new free cluster to use. */ find a new free cluster to use. */
cluster = find_free_cluster(cluster); cluster = find_free_cluster(oldcluster+1);
if (!cluster) { if (!cluster) {
/* no cluster found after last, /* no free cluster found after last,
search from beginning */ search from beginning */
cluster = find_free_cluster(0); cluster = find_free_cluster(0);
if (!cluster) { if (!cluster) {
@ -1030,6 +1043,7 @@ int fat_readwrite( struct fat_file *file, int sectorcount,
if (cluster) { if (cluster) {
sector = cluster2sec(cluster); sector = cluster2sec(cluster);
LDEBUGF("cluster2sec(%x) == %x\n",cluster,sector);
if (sector<0) if (sector<0)
return -1; return -1;
numsec=0; numsec=0;
@ -1037,6 +1051,12 @@ int fat_readwrite( struct fat_file *file, int sectorcount,
} }
else else
sector++; sector++;
numsec++;
if (!first)
first = sector;
if (!last)
last = sector;
if ( (sector != last+1) || /* not sequential any more? */ if ( (sector != last+1) || /* not sequential any more? */
(i == sectorcount-1) || /* last sector requested? */ (i == sectorcount-1) || /* last sector requested? */
@ -1056,8 +1076,9 @@ int fat_readwrite( struct fat_file *file, int sectorcount,
} }
last = sector; last = sector;
} }
file->nextcluster = cluster;
file->nextsector = sector; file->lastcluster = cluster;
file->lastsector = sector;
file->sectornum = numsec; file->sectornum = numsec;
return sectorcount; return sectorcount;
@ -1099,8 +1120,8 @@ int fat_seek(struct fat_file *file, int seeksector )
} }
} }
file->nextcluster = cluster; file->lastcluster = cluster;
file->nextsector = sector; file->lastsector = sector;
file->sectornum = numsec; file->sectornum = numsec;
return 0; return 0;
} }

View file

@ -58,8 +58,8 @@ struct fat_dir
struct fat_file struct fat_file
{ {
int firstcluster; /* first cluster in file */ int firstcluster; /* first cluster in file */
int nextcluster; /* cluster of last access */ int lastcluster; /* cluster of last access */
int nextsector; /* sector of last access */ int lastsector; /* sector of last access */
int sectornum; /* sector number in this cluster */ int sectornum; /* sector number in this cluster */
int dirsector; /* sector where the dir entry is located */ int dirsector; /* sector where the dir entry is located */
int direntry; /* dir entry index in sector */ int direntry; /* dir entry index in sector */
@ -79,6 +79,7 @@ extern int fat_readwrite(struct fat_file *ent, int sectorcount,
void* buf, bool write ); void* buf, bool write );
extern int fat_closewrite(struct fat_file *ent, int size); extern int fat_closewrite(struct fat_file *ent, int size);
extern int fat_seek(struct fat_file *ent, int sector ); extern int fat_seek(struct fat_file *ent, int sector );
extern int fat_remove(struct fat_file *ent);
extern int fat_opendir(struct fat_dir *ent, unsigned int currdir); extern int fat_opendir(struct fat_dir *ent, unsigned int currdir);
extern int fat_getnext(struct fat_dir *ent, struct fat_direntry *entry); extern int fat_getnext(struct fat_dir *ent, struct fat_direntry *entry);

View file

@ -9,7 +9,7 @@ static FILE* file;
int ata_read_sectors(unsigned long start, unsigned char count, void* buf) int ata_read_sectors(unsigned long start, unsigned char count, void* buf)
{ {
DEBUGF("Reading block 0x%lx\n",start); DEBUGF("[Reading block 0x%lx]\n",start);
if(fseek(file,start*BLOCK_SIZE,SEEK_SET)) { if(fseek(file,start*BLOCK_SIZE,SEEK_SET)) {
perror("fseek"); perror("fseek");
return -1; return -1;
@ -17,14 +17,14 @@ int ata_read_sectors(unsigned long start, unsigned char count, void* buf)
if(!fread(buf,BLOCK_SIZE,count,file)) { if(!fread(buf,BLOCK_SIZE,count,file)) {
printf("Failed reading %d blocks starting at block 0x%lx\n",count,start); printf("Failed reading %d blocks starting at block 0x%lx\n",count,start);
perror("fread"); perror("fread");
return -1; return -2;
} }
return 0; return 0;
} }
int ata_write_sectors(unsigned long start, unsigned char count, void* buf) int ata_write_sectors(unsigned long start, unsigned char count, void* buf)
{ {
DEBUGF("Writing block 0x%lx\n",start); DEBUGF("[Writing block 0x%lx]\n",start);
if (start == 0) { if (start == 0) {
DEBUGF("Holy crap! You're writing on sector 0!\n"); DEBUGF("Holy crap! You're writing on sector 0!\n");
@ -37,7 +37,7 @@ int ata_write_sectors(unsigned long start, unsigned char count, void* buf)
} }
if(!fwrite(buf,BLOCK_SIZE,count,file)) { if(!fwrite(buf,BLOCK_SIZE,count,file)) {
perror("fwrite"); perror("fwrite");
return -1; return -2;
} }
return 0; return 0;
} }

View file

@ -19,6 +19,7 @@ void panicf( char *fmt, ...)
{ {
va_list ap; va_list ap;
va_start( ap, fmt ); va_start( ap, fmt );
printf("***PANIC*** ");
vprintf( fmt, ap ); vprintf( fmt, ap );
va_end( ap ); va_end( ap );
exit(0); exit(0);
@ -82,17 +83,20 @@ void dbg_dir(char* currdir)
} }
} }
void dbg_mkfile(char* name) void dbg_mkfile(char* name, int num)
{ {
char* text = "Detta är en dummy-text\n"; char text[800];
int i; int i;
int fd = open(name,O_WRONLY); int fd = open(name,O_WRONLY);
if (fd<0) { if (fd<0) {
DEBUGF("Failed creating file\n"); DEBUGF("Failed creating file\n");
return; return;
} }
for (i=0;i<200;i++) for (i=0; i<sizeof(text)/4; i++ )
if (write(fd, text, strlen(text)) < 0) sprintf(text+i*4,"%03x,",i);
for (i=0;i<num;i++)
if (write(fd, text, sizeof(text)) < 0)
DEBUGF("Failed writing data\n"); DEBUGF("Failed writing data\n");
close(fd); close(fd);
@ -137,27 +141,7 @@ void dbg_tail(char* name)
return; return;
DEBUGF("Got file descriptor %d\n",fd); DEBUGF("Got file descriptor %d\n",fd);
rc = lseek(fd,512,SEEK_SET); rc = lseek(fd,-512,SEEK_END);
if ( rc >= 0 ) {
rc = read(fd, buf, SECTOR_SIZE);
if( rc > 0 )
{
buf[rc]=0;
printf("%d: %s\n", strlen(buf), buf);
}
else if ( rc == 0 ) {
DEBUGF("EOF\n");
}
else
{
DEBUGF("Failed reading file: %d\n",rc);
}
}
else {
perror("lseek");
}
rc = lseek(fd,-100,SEEK_CUR);
if ( rc >= 0 ) { if ( rc >= 0 ) {
rc = read(fd, buf, SECTOR_SIZE); rc = read(fd, buf, SECTOR_SIZE);
if( rc > 0 ) if( rc > 0 )
@ -284,6 +268,8 @@ void dbg_console(void)
} }
} }
extern void ata_exit(void);
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
int rc,i; int rc,i;
@ -325,12 +311,14 @@ int main(int argc, char *argv[])
//dbg_console(); //dbg_console();
//dbg_dir("/"); //dbg_dir("/");
#if 1 #if 1
dbg_head("/bepa.txt"); dbg_tail("/depa.txt");
#else #else
dbg_mkfile("/bepa.txt"); dbg_mkfile("/depa.txt", 10);
#endif #endif
dbg_dir("/"); dbg_dir("/");
ata_exit();
return 0; return 0;
} }