diff options
author | germeier <germeier> | 2002-09-11 00:18:34 +0000 |
---|---|---|
committer | germeier <germeier> | 2002-09-11 00:18:34 +0000 |
commit | 9219a7fd076dbd9fdb57cb70036803975f867c2b (patch) | |
tree | c81d96132b111af24dd31694bb13e4d9ea229324 /libmpio | |
parent | 196525ab0a4c2350cce745bfb1e6b340ced89d3c (diff) | |
download | mpiosh-9219a7fd076dbd9fdb57cb70036803975f867c2b.tar.gz mpiosh-9219a7fd076dbd9fdb57cb70036803975f867c2b.tar.bz2 mpiosh-9219a7fd076dbd9fdb57cb70036803975f867c2b.zip |
updated writing support to support mpio_fatentry_t
Diffstat (limited to 'libmpio')
-rw-r--r-- | libmpio/defs.h | 3 | ||||
-rw-r--r-- | libmpio/fat.c | 137 | ||||
-rw-r--r-- | libmpio/fat.h | 7 | ||||
-rw-r--r-- | libmpio/io.c | 41 | ||||
-rw-r--r-- | libmpio/mpio.c | 162 |
5 files changed, 230 insertions, 120 deletions
diff --git a/libmpio/defs.h b/libmpio/defs.h index ffd664f..ad4177c 100644 --- a/libmpio/defs.h +++ b/libmpio/defs.h @@ -1,7 +1,7 @@ /* -*- linux-c -*- */ /* - * $Id: defs.h,v 1.2 2002/09/03 21:20:53 germeier Exp $ + * $Id: defs.h,v 1.3 2002/09/11 00:18:34 germeier Exp $ * * Library for USB MPIO-* * @@ -163,6 +163,7 @@ typedef struct { DWORD entry; /* number of FAT entry */ /* internal */ + BYTE i_index; /* file index of file to store */ BYTE i_fat[16]; /* internal FAT entry */ /* external */ diff --git a/libmpio/fat.c b/libmpio/fat.c index 3a5a087..5392bee 100644 --- a/libmpio/fat.c +++ b/libmpio/fat.c @@ -1,6 +1,6 @@ /* * - * $Id: fat.c,v 1.8 2002/09/10 13:41:21 germeier Exp $ + * $Id: fat.c,v 1.9 2002/09/11 00:18:34 germeier Exp $ * * Library for USB MPIO-* * @@ -360,7 +360,12 @@ mpio_fatentry_write(mpio_t *m, mpio_mem_t mem, mpio_fatentry_t *f, WORD value) int e; BYTE backup; - if (mem == MPIO_INTERNAL_MEM) sm = &m->internal; + if (mem == MPIO_INTERNAL_MEM) + { + debug("This should not be used for internal memory!\n"); + exit(-1); + } + if (mem == MPIO_EXTERNAL_MEM) sm = &m->external; if (sm->size == 128) @@ -409,6 +414,39 @@ mpio_fat_internal_find_startsector(mpio_t *m, BYTE start) return found; } +BYTE +mpio_fat_internal_find_fileindex(mpio_t *m) +{ + mpio_fatentry_t *f; + mpio_smartmedia_t *sm = &m->internal; + BYTE index[256]; + BYTE found; /* hmm, ... */ + + memset(index, 1, 256); + + f = mpio_fatentry_new(m, MPIO_INTERNAL_MEM, 0); + while(mpio_fatentry_plus_plus(f)) + { + if (sm->fat[f->entry * 0x10 + 1] != 0xff) + index[sm->fat[f->entry * 0x10 + 1]] = 0; + } + free(f); + + found=6; + while((found<256) && (!index[found])) + found++; + + if (found==256) + { + debug("Oops, did not find a new fileindex!\n" + "This should never happen, aborting now!, Sorry!\n"); + exit(-1); + } + + return found; +} + + int mpio_fat_free_clusters(mpio_t *m, mpio_mem_t mem) { @@ -432,7 +470,7 @@ mpio_fat_free_clusters(mpio_t *m, mpio_mem_t mem) } mpio_fatentry_t * -mpio_fat_find_free(mpio_t *m, mpio_mem_t mem) +mpio_fatentry_find_free(mpio_t *m, mpio_mem_t mem) { mpio_fatentry_t *f; @@ -617,16 +655,18 @@ mpio_fatentry_set_free (mpio_t *m, mpio_mem_t mem, mpio_fatentry_t *f) int e; mpio_smartmedia_t *sm; - if (mem == MPIO_INTERNAL_MEM) { - sm = &m->internal; - e = f->entry * 0x10; - memset((sm->fat+e), 0xff, 0x10); - } + if (mem == MPIO_INTERNAL_MEM) + { + sm = &m->internal; + e = f->entry * 0x10; + memset((sm->fat+e), 0xff, 0x10); + } - if (mem == MPIO_EXTERNAL_MEM) { - sm = &m->internal; - mpio_fatentry_write(m, mem, f, 0); - } + if (mem == MPIO_EXTERNAL_MEM) + { + sm = &m->internal; + mpio_fatentry_write(m, mem, f, 0); + } return 0; } @@ -637,18 +677,69 @@ mpio_fatentry_set_defect(mpio_t *m, mpio_mem_t mem, mpio_fatentry_t *f) int e; mpio_smartmedia_t *sm; - if (mem == MPIO_INTERNAL_MEM) { - sm = &m->internal; - e = f->entry * 0x10; - debug("Sorry, I don't now how to mark an internal block as" - " defective yet.\n"); -/* memset((sm->fat+e), 0xff, 0x10); */ - } + if (mem == MPIO_INTERNAL_MEM) + { + sm = &m->internal; + e = f->entry * 0x10; + debug("Sorry, I don't now how to mark an internal block as" + " defective yet.\n"); + /* memset((sm->fat+e), 0xff, 0x10); */ + } - if (mem == MPIO_EXTERNAL_MEM) { - sm = &m->internal; - mpio_fatentry_write(m, mem, f, 0xfff7); - } + if (mem == MPIO_EXTERNAL_MEM) + { + sm = &m->internal; + mpio_fatentry_write(m, mem, f, 0xfff7); + } + + return 0; +} + +int +mpio_fatentry_set_eof(mpio_t *m, mpio_mem_t mem, mpio_fatentry_t *f) +{ + int e; + mpio_smartmedia_t *sm; + + if (mem == MPIO_INTERNAL_MEM) + { + sm = &m->internal; + e = f->entry * 0x10; + memset((sm->fat+e), 0xff, 0x10); + memset((f->i_fat+0x07), 0xff, 4); + } + + if (mem == MPIO_EXTERNAL_MEM) + { + sm = &m->internal; + mpio_fatentry_write(m, mem, f, 0xffff); + } + + return 0; +} + +int +mpio_fatentry_set_next(mpio_t *m, mpio_mem_t mem, + mpio_fatentry_t *f, mpio_fatentry_t *value) +{ + int e; + mpio_smartmedia_t *sm; + + if (mem == MPIO_INTERNAL_MEM) + { + sm = &m->internal; + e = f->entry * 0x10; + sm->fat[e+0x07]= value->hw_address / 0x1000000; + sm->fat[e+0x08]=(value->hw_address / 0x10000 ) & 0xff; + sm->fat[e+0x09]=(value->hw_address / 0x100 ) & 0xff; + sm->fat[e+0x0a]= value->hw_address & 0xff; + } + + if (mem == MPIO_EXTERNAL_MEM) + { + sm = &m->internal; + mpio_fatentry_write(m, mem, f, value->entry); + } return 0; } diff --git a/libmpio/fat.h b/libmpio/fat.h index b8667f4..da7612b 100644 --- a/libmpio/fat.h +++ b/libmpio/fat.h @@ -1,6 +1,6 @@ /* * - * $Id: fat.h,v 1.4 2002/09/10 12:31:09 germeier Exp $ + * $Id: fat.h,v 1.5 2002/09/11 00:18:34 germeier Exp $ * * Library for USB MPIO-* * @@ -52,8 +52,13 @@ int mpio_fatentry_set_free (mpio_t *, mpio_mem_t, mpio_fatentry_t *); int mpio_fatentry_set_defect(mpio_t *, mpio_mem_t, mpio_fatentry_t *); +int mpio_fatentry_set_eof(mpio_t *, mpio_mem_t, + mpio_fatentry_t *); +int mpio_fatentry_set_next(mpio_t *, mpio_mem_t, + mpio_fatentry_t *, mpio_fatentry_t *); int mpio_fat_internal_find_startsector(mpio_t *, BYTE); +BYTE mpio_fat_internal_find_fileindex(mpio_t *); void mpio_fatentry_hw2entry(mpio_t *, mpio_fatentry_t *); void mpio_fatentry_entry2hw(mpio_t *, mpio_fatentry_t *); diff --git a/libmpio/io.c b/libmpio/io.c index 126e111..4cc134a 100644 --- a/libmpio/io.c +++ b/libmpio/io.c @@ -2,7 +2,7 @@ /* * - * $Id: io.c,v 1.8 2002/09/10 13:41:21 germeier Exp $ + * $Id: io.c,v 1.9 2002/09/11 00:18:34 germeier Exp $ * * Library for USB MPIO-* * @@ -461,17 +461,19 @@ mpio_io_sector_write(mpio_t *m, BYTE mem, DWORD index, BYTE *input) memset(sendbuff + SECTOR_SIZE, 0xff, 0x10); memcpy(sendbuff, input, SECTOR_SIZE); - if (index<0x40) - { - block_address=0; - } else { - ba= (index /0x20) - 2; - debugn(2, "foobar: %4x\n", ba); - block_address= index2blockaddress(ba); - debugn(2, "foobar: %4x\n", block_address); - } - if (mem==MPIO_EXTERNAL_MEM) + if (index<0x40) + { + block_address=0; + } else { + ba= (index / 0x20) - 2; + if (ba > 0x8000) + ba %= 0x8000; + debugn(2, "sector-foo: %4x\n", ba); + block_address= index2blockaddress(ba); + debugn(2, "sector-foo: %4x\n", block_address); + } + { /* generate ECC information for spare area ! */ mpio_ecc_256_gen(sendbuff, @@ -719,6 +721,10 @@ mpio_io_block_write(mpio_t *m, BYTE mem, mpio_fatentry_t *f, BYTE *data) memset(sendbuff + (i * SECTOR_TRANS) + SECTOR_SIZE, 0xff, CMD_SIZE); + if (mem == MPIO_INTERNAL_MEM) + memcpy((sendbuff+SECTOR_SIZE+(i * SECTOR_TRANS)), + f->i_fat, 0x10); + /* fill in block information */ if (mem == MPIO_EXTERNAL_MEM) { @@ -726,10 +732,17 @@ mpio_io_block_write(mpio_t *m, BYTE mem, mpio_fatentry_t *f, BYTE *data) { block_address = 0; } else { - ba = (address / 0x20) - 2; - /* debugn(2, "foobar: %4x\n", ba); */ + debugn(2, "block-foo-1: %06x\n", address); + /* block address is relativ to zone */ + if (address >= 0x8000) + { + ba = (address % 0x8000) / 0x20; + } else { + ba = (address / 0x20) - 2; + } + debugn(2, "block-foo-2: %4x\n", ba); block_address = index2blockaddress(ba); - /* debugn(2, "foobar: %4x\n", block_address); */ + debugn(2, "block-foo-3: %4x\n", block_address); } ba = (block_address / 0x100) & 0xff; diff --git a/libmpio/mpio.c b/libmpio/mpio.c index 67d2ddb..d024918 100644 --- a/libmpio/mpio.c +++ b/libmpio/mpio.c @@ -1,6 +1,6 @@ /* * - * $Id: mpio.c,v 1.8 2002/09/10 13:41:21 germeier Exp $ + * $Id: mpio.c,v 1.9 2002/09/11 00:18:34 germeier Exp $ * * Library for USB MPIO-* * @@ -320,58 +320,69 @@ mpio_file_put(mpio_t *m, mpio_mem_t mem, BYTE *filename, BYTE (*progress_callback)(int, int)) { mpio_smartmedia_t *sm; + mpio_fatentry_t *f, current, start; int data_offset; BYTE block[BLOCK_SIZE]; - DWORD startsector=0, sector, nextsector; - DWORD realsector=0; int fd, toread; struct stat file_stat; - - DWORD filesize, fsize; - DWORD fat_and; - DWORD fat_end; - BYTE abort=0; - - debug("Support for writing files is deactivated!"); - return 0; - if (mem==MPIO_INTERNAL_MEM) { - sm=&m->internal; - data_offset=0x00; - fat_and=0xffffffff; - fat_end=0xffffffff; - debug("writing to internal memory is not yet supported, sorry\n"); - return 0; - } + BYTE *p=NULL; + DWORD filesize, fsize, free; + BYTE abort=0; - if (mem==MPIO_EXTERNAL_MEM) { - sm=&m->external; - data_offset=sm->dir_offset+DIR_NUM-(2*BLOCK_SECTORS); - /* FAT 16 vs. FAT 12 */ - if (sm->size==128) { - fat_and=0xffff; - fat_end=0xfff8; - } else { - fat_and=0xfff; - fat_end=0xff8; - } - } + if (mem==MPIO_INTERNAL_MEM) sm=&m->internal; + if (mem==MPIO_EXTERNAL_MEM) sm=&m->external; if (stat((const char *)filename, &file_stat)!=0) { debug("could not find file: %s\n", filename); return 0; } - fsize=filesize=file_stat.st_size; debugn(2, "filesize: %d\n", fsize); - fd = open(filename, O_RDONLY); - if (fd==-1) { - debug("could not find file: %s\n", filename); + /* check if there is enough space left */ + mpio_memory_free(m, mem, &free); + if (free*1024<fsize) { + debug("not enough space left (only %d KB)\n", free); return 0; } - while ((filesize>0) & (!abort)) { + /* check if filename already exists */ + p = mpio_dentry_find_name(m, mem, filename); + if (!p) + p = mpio_dentry_find_name_8_3(m, mem, filename); + if (p) + { + debug("filename already exists\n"); + return 0; + } + + /* find first free sector */ + f = mpio_fatentry_find_free(m, mem); + if (!f) + { + debug("could not free cluster for file!\n"); + return 0; + } else { + memcpy(&start, f, sizeof(mpio_fatentry_t)); + } + + /* find file-id for internal memory */ + if (mem==MPIO_INTERNAL_MEM) + { + f->i_index=mpio_fat_internal_find_fileindex(m); + debug("fileindex: %02x\n", f->i_index); + } + + /* open file for writing */ + fd = open(filename, O_RDONLY); + if (fd==-1) + { + debug("could not open file: %s\n", filename); + return 0; + } + + while ((filesize>BLOCK_SIZE)) { if (filesize>=BLOCK_SIZE) { toread=BLOCK_SIZE; @@ -384,63 +395,52 @@ mpio_file_put(mpio_t *m, mpio_mem_t mem, BYTE *filename, close(fd); return -1; } - - if (!startsector) { - startsector=mpio_fat_find_free(m, mem); - if (!startsector) { - debug("could not find startsector for file!\n"); - close(fd); - return 0; - } - sector=startsector; - debugn(2, "startsector: %d\n", startsector); - /* evil hacks are us ;-) */ -/* mpio_fatentry_write(m, mem, startsector, fat_and); */ - } else { - nextsector=mpio_fat_find_free(m, mem); - if (!nextsector){ - debug("no more free blocks, short write!\n"); - close(fd); - return (fsize-filesize); - } - debugn(2, " nextsector: %d\n", nextsector); - -/* mpio_fatentry_write(m, mem, sector, nextsector); */ - sector=nextsector; - /* evil hacks are us ;-) */ -/* mpio_fatentry_write(m, mem, sector, fat_and); */ - - } - - if (mem==MPIO_INTERNAL_MEM) { -/* realsector=fat_internal_find_startsector(m, sector); */ -/* realsector=realsector+ /\* chose the right bank *\/ */ -/* (0x01000000*((realsector/(sm->size/16*1000/sm->chips))+1)); */ -/* sector=realsector; */ -/* debugn(2, "startsector (real): %4x\n", sector); */ - } else { -/* realsector=sector_hack(sm->size, sector); */ - } - -/* debug("mager: %04x : %3d\n", realsector*BLOCK_SECTORS, realsector); */ -/* mpio_io_block_delete(m, mem, ((realsector*BLOCK_SECTORS) + data_offset), */ -/* sm->size); */ -/* mpio_io_block_write(m, mem, realsector + (data_offset/BLOCK_SECTORS), */ -/* sm->size, block); */ - filesize -= toread; + + /* get new free block from FAT and write current block out */ + memcpy(¤t, f, sizeof(mpio_fatentry_t)); + if (!(mpio_fatentry_next_free(m, mem, f))) + { + debug ("Found no free cluster during mpio_file_put\n" + "This should never happen\n"); + exit(-1); + } + mpio_fatentry_set_next(m ,mem, ¤t, f); + mpio_io_block_write(m, mem, ¤t, block); if (progress_callback) abort=(*progress_callback)((fsize-filesize), fsize); } + /* handle the last block to write */ + + if (filesize>=BLOCK_SIZE) { + toread=BLOCK_SIZE; + } else { + toread=filesize; + } + + if (read(fd, block, toread)!=toread) { + debug("error reading file data\n"); + close(fd); + return -1; + } + filesize -= toread; + + /* mark end of FAT chain and write last block */ + mpio_fatentry_set_eof(m ,mem, f); + mpio_io_block_write(m, mem, f, block); + close(fd); + if (progress_callback) + abort=(*progress_callback)((fsize-filesize), fsize); + /* FIXEME: add real values here!!! */ mpio_dentry_put(m, mem, filename, strlen(filename), 2002, 8, 13, - 2, 12, fsize, startsector); + 2, 12, fsize, start.entry); /* this writes the FAT *and* the root directory */ mpio_fat_write(m, mem); |