From 394e96c30d87613fc733167297d1bf594d5e5035 Mon Sep 17 00:00:00 2001 From: germeier Date: Mon, 23 Sep 2002 22:38:03 +0000 Subject: read spare area from external memory too display progress for internal and external memory a few code clean ups and checks renamed debug_memory to dump_memory and dump more memory areas now a lot of little changes and updates --- ChangeLog | 13 ++++++++ libmpio/defs.h | 18 ++++++++++- libmpio/fat.c | 38 +++++++++++++++++------ libmpio/fat.h | 4 +-- libmpio/io.c | 29 ++++++++++-------- libmpio/io.h | 4 +-- libmpio/mpio.c | 90 +++++++++++++++++++++++++++++++++++++++++++++---------- libmpio/mpio.h | 16 +++++----- mpiosh/callback.c | 23 ++++++++++---- mpiosh/callback.h | 6 ++-- mpiosh/mpiosh.c | 8 ++--- 11 files changed, 185 insertions(+), 64 deletions(-) diff --git a/ChangeLog b/ChangeLog index a5117db..f16b419 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,16 @@ +2002-09-24 Markus Germeier + * a lot of little changes and updates + * mpiosh/callback.c (mpiosh_cmd_dump_mem): + renamed function from debug_memory to dump_memory + (mpiosh_callback_init): display progress of internal and + external memory + * libmpio/mpio.c (mpio_check_filename): added function + to check for string zero termination (more to check??) + (mpio_memory_dump): dump all relevant memory areas + (mpio_init): read spare area for external memory + * libmpio/defs.h: added mpio_callback_t, mpio_callback_init_t + and mpio_filename_t + 2002-09-22 Andreas Buesching * configure.in (SPEC_PATH): needed for the spec file to find the diff --git a/libmpio/defs.h b/libmpio/defs.h index 910df64..a6422cd 100644 --- a/libmpio/defs.h +++ b/libmpio/defs.h @@ -1,7 +1,7 @@ /* -*- linux-c -*- */ /* - * $Id: defs.h,v 1.8 2002/09/20 20:49:36 germeier Exp $ + * $Id: defs.h,v 1.9 2002/09/23 22:38:03 germeier Exp $ * * Library for USB MPIO-* * @@ -66,6 +66,14 @@ typedef enum { FTYPE_CONF = 'C', FTYPE_WAV = 'V', FTYPE_ENTRY = 'R' } mpio_file_t; +/* type of callback functions */ +typedef BYTE (*mpio_callback_t)(int, int) ; +typedef BYTE (*mpio_callback_init_t)(mpio_mem_t, int, int) ; + +/* filenames */ +#define MPIO_FILENAME_LEN 129 +typedef BYTE mpio_filename_t[MPIO_FILENAME_LEN]; + #ifndef NULL #define NULL 0 #endif @@ -106,6 +114,8 @@ typedef struct { #define MPIO_ERR_READING_FILE -5 #define MPIO_ERR_PERMISSION_DENIED -6 #define MPIO_ERR_WRITING_FILE -7 +/* internal errors, occur when UI has errors! */ +#define MPIO_ERR_INT_STRING_INVALID -101 /* get formatted information, about the MPIO player */ @@ -166,6 +176,12 @@ typedef struct { int fat_nums; /* # of FATs */ BYTE * fat; /* *real FAT (like in block allocation :-) */ + /* how many physical blocks are available + * for internal memory is this value equal to max_cluster + */ + int max_blocks; + BYTE * spare; + /* seems to be a fixed size according to the Samsung documentation */ BYTE dir[DIR_SIZE]; /* file index */ diff --git a/libmpio/fat.c b/libmpio/fat.c index 6279599..254ca0b 100644 --- a/libmpio/fat.c +++ b/libmpio/fat.c @@ -1,6 +1,6 @@ /* * - * $Id: fat.c,v 1.11 2002/09/18 22:18:29 germeier Exp $ + * $Id: fat.c,v 1.12 2002/09/23 22:38:03 germeier Exp $ * * Library for USB MPIO-* * @@ -259,7 +259,8 @@ mpio_fatentry_plus_plus(mpio_fatentry_t *f) /* read "fat_size" sectors of fat into the provided buffer */ int -mpio_fat_read (mpio_t *m, mpio_mem_t mem, BYTE (*progress_callback)(int, int)) +mpio_fat_read (mpio_t *m, mpio_mem_t mem, + mpio_callback_init_t progress_callback) { mpio_smartmedia_t *sm; BYTE recvbuff[SECTOR_SIZE]; @@ -292,17 +293,24 @@ mpio_fat_read (mpio_t *m, mpio_mem_t mem, BYTE (*progress_callback)(int, int)) int mpio_fatentry_free(mpio_t *m, mpio_mem_t mem, mpio_fatentry_t *f ) { - int e; + int e,i ; mpio_smartmedia_t *sm; if (mem == MPIO_INTERNAL_MEM) { sm = &m->internal; e = f->entry * 0x10; - if((sm->fat[e+0] == 0xff) && - (sm->fat[e+1] == 0xff) && - (sm->fat[e+2] == 0xff)) - return 1; + /* be more strict to avoid writing + * to defective blocks! + */ + i=0; + while (i<0x10) + { + if (sm->fat[e+i] == 0xff) + return 0; + i++; + } + return 1; } if (mem == MPIO_EXTERNAL_MEM) { @@ -323,8 +331,17 @@ mpio_fatentry_read(mpio_t *m, mpio_mem_t mem, mpio_fatentry_t *f ) if (mem == MPIO_INTERNAL_MEM) { sm = &m->internal; - e = f->entry; - e = e * 0x10 + 7; + e = f->entry * 0x10; + /* check if this block became defective */ + if ((sm->fat[e+0x0e] != 'P') || + (sm->fat[e+0x0f] != 'C') || + ((sm->fat[e+0x00] != 0xaa) && + (sm->fat[e+0x00] != 0xee))) + { + debug("defective block encountered, abort reading!\n"); + return 0xaaaaaaaa; + } + e += 7; if((sm->fat[e+0] == 0xff) && (sm->fat[e+1] == 0xff) && (sm->fat[e+2] == 0xff) && @@ -528,6 +545,9 @@ mpio_fatentry_next_entry(mpio_t *m, mpio_mem_t mem, mpio_fatentry_t *f) DWORD endvalue; value = mpio_fatentry_read(m, mem, f); + + if (value == 0xaaaaaaaa) + return -1; if (mem == MPIO_INTERNAL_MEM) { diff --git a/libmpio/fat.h b/libmpio/fat.h index af92d35..84963b5 100644 --- a/libmpio/fat.h +++ b/libmpio/fat.h @@ -1,6 +1,6 @@ /* * - * $Id: fat.h,v 1.6 2002/09/18 22:18:29 germeier Exp $ + * $Id: fat.h,v 1.7 2002/09/23 22:38:03 germeier Exp $ * * Library for USB MPIO-* * @@ -30,7 +30,7 @@ int mpio_bootblocks_read(mpio_t *, mpio_mem_t); -int mpio_fat_read(mpio_t *, mpio_mem_t, BYTE (*)(int, int)); +int mpio_fat_read(mpio_t *, mpio_mem_t, mpio_callback_init_t); int mpio_fat_write(mpio_t *, mpio_mem_t); int mpio_fat_clear(mpio_t *, mpio_mem_t); int mpio_fat_free_clusters(mpio_t *, mpio_mem_t); diff --git a/libmpio/io.c b/libmpio/io.c index ad5df53..7a5b58e 100644 --- a/libmpio/io.c +++ b/libmpio/io.c @@ -2,7 +2,7 @@ /* * - * $Id: io.c,v 1.15 2002/09/20 20:49:36 germeier Exp $ + * $Id: io.c,v 1.16 2002/09/23 22:38:03 germeier Exp $ * * Library for USB MPIO-* * @@ -663,27 +663,30 @@ mpio_io_block_read(mpio_t *m, BYTE mem, mpio_fatentry_t *f, BYTE *output) */ int -mpio_io_spare_read(mpio_t *m, BYTE area, DWORD index, BYTE size, +mpio_io_spare_read(mpio_t *m, BYTE mem, DWORD index, BYTE size, BYTE wsize, BYTE *output, int toread, - BYTE (*progress_callback)(int, int)) + mpio_callback_init_t progress_callback) { + mpio_smartmedia_t *sm; int i; int nwrite, nread; int chip = 0; int chips = 0; BYTE cmdpacket[CMD_SIZE]; - if (area != MPIO_INTERNAL_MEM) { - debug("Something fishy happened, aborting!n"); - exit(1); - } + if (mem == MPIO_INTERNAL_MEM) sm = &m->internal; + if (mem == MPIO_EXTERNAL_MEM) sm = &m->external; - chips = m->internal.chips; + chips = sm->chips; for (chip = 1; chip <= chips; chip++) - { - mpio_io_set_cmdpacket(m, GET_SPARE_AREA, chip, index, size, - wsize, cmdpacket); + { + if (mem == MPIO_INTERNAL_MEM) + mpio_io_set_cmdpacket(m, GET_SPARE_AREA, chip, index, size, + wsize, cmdpacket); + if (mem == MPIO_EXTERNAL_MEM) + mpio_io_set_cmdpacket(m, GET_SPARE_AREA, mem, index, size, + wsize, cmdpacket); debugn(5, "\n>>> MPIO\n"); hexdump(cmdpacket, sizeof(cmdpacket)); @@ -704,7 +707,7 @@ mpio_io_spare_read(mpio_t *m, BYTE area, DWORD index, BYTE size, CMD_SIZE); if ((progress_callback) && (i % 256)) - (*progress_callback)((i*CMD_SIZE), toread); + (*progress_callback)(mem, (i*CMD_SIZE), toread); if(nread != CMD_SIZE) { @@ -718,7 +721,7 @@ mpio_io_spare_read(mpio_t *m, BYTE area, DWORD index, BYTE size, } } if (progress_callback) - (*progress_callback)(toread, toread); + (*progress_callback)(mem, toread, toread); return 0; } diff --git a/libmpio/io.h b/libmpio/io.h index 9eb6217..cd8b70c 100644 --- a/libmpio/io.h +++ b/libmpio/io.h @@ -2,7 +2,7 @@ /* * - * $Id: io.h,v 1.7 2002/09/18 22:18:29 germeier Exp $ + * $Id: io.h,v 1.8 2002/09/23 22:38:03 germeier Exp $ * * Library for USB MPIO-* * @@ -54,6 +54,6 @@ int mpio_io_block_delete(mpio_t *, BYTE, mpio_fatentry_t *); /* */ int mpio_io_spare_read (mpio_t *, BYTE, DWORD, BYTE, BYTE, BYTE *, int, - BYTE (*)(int, int)); + mpio_callback_init_t); #endif /* _MPIO_IO_H_ */ diff --git a/libmpio/mpio.c b/libmpio/mpio.c index 1bd9f41..97c5884 100644 --- a/libmpio/mpio.c +++ b/libmpio/mpio.c @@ -1,6 +1,6 @@ /* * - * $Id: mpio.c,v 1.25 2002/09/21 22:17:15 germeier Exp $ + * $Id: mpio.c,v 1.26 2002/09/23 22:38:03 germeier Exp $ * * Library for USB MPIO-* * @@ -41,7 +41,10 @@ #include "smartmedia.h" #include "fat.h" -#define DSTRING 100 +void mpio_init_internal(mpio_t *); +void mpio_init_external(mpio_t *); +int mpio_check_filename(mpio_filename_t); + static BYTE *mpio_model_name[] = { "MPIO-DME", @@ -67,7 +70,9 @@ static mpio_error_t mpio_errors[] = { { MPIO_ERR_PERMISSION_DENIED, "There are not enough rights to access the file/directory." }, { MPIO_ERR_WRITING_FILE, - "There are no permisson to write to the selected file." } + "There are no permisson to write to the selected file." }, + { MPIO_ERR_INT_STRING_INVALID, + "Internal Error: Supported is invalid!" } }; static const int mpio_error_num = sizeof mpio_errors / sizeof(mpio_error_t); @@ -76,8 +81,26 @@ static int _mpio_errno = 0; #define MPIO_ERR_RETURN(err) { _mpio_errno = err; return -1 ; } -void mpio_init_internal(mpio_t *); -void mpio_init_external(mpio_t *); +#define MPIO_CHECK_FILENAME(filename) \ + if (!mpio_check_filename(filename)) { \ + MPIO_ERR_RETURN(MPIO_ERR_INT_STRING_INVALID); \ + } + +int +mpio_check_filename(mpio_filename_t filename) +{ + BYTE *p=filename; + + while (p < (filename+MPIO_FILENAME_LEN)) + { + if (*p) + return 1; + p++; + } + + return 0; +} + void mpio_init_internal(mpio_t *m) @@ -120,6 +143,7 @@ mpio_init_internal(mpio_t *m) /* read FAT information from spare area */ sm->max_cluster = sm->size/16*1024; /* 1 cluster == 16 KB */ + sm->max_blocks = sm->max_cluster; debugn(2,"max_cluster: %d\n", sm->max_cluster); /* 16 bytes per cluster */ sm->fat_size = sm->max_cluster*16/SECTOR_SIZE; @@ -173,13 +197,18 @@ mpio_init_external(mpio_t *m) sm->fat = malloc(SECTOR_SIZE*sm->fat_size); mpio_fat_read(m, MPIO_EXTERNAL_MEM, NULL); mpio_rootdir_read(m, MPIO_EXTERNAL_MEM); + + /* for reading the spare area later! */ + sm->max_blocks = sm->size/16*1024; /* 1 cluster == 16 KB */ + sm->spare = malloc(sm->max_blocks * 0x10); } } mpio_t * -mpio_init(BYTE (*progress_callback)(int, int)) +mpio_init(mpio_callback_init_t progress_callback) { mpio_t *new_mpio; + mpio_smartmedia_t *sm; new_mpio = malloc(sizeof(mpio_t)); if (!new_mpio) { @@ -241,6 +270,12 @@ mpio_init(BYTE (*progress_callback)(int, int)) /* read FAT/spare area */ if (new_mpio->internal.id) mpio_fat_read(new_mpio, MPIO_INTERNAL_MEM, progress_callback); + + /* read the spare area (for block mapping) */ + sm = &new_mpio->external; + mpio_io_spare_read(new_mpio, MPIO_EXTERNAL_MEM, 0, + sm->size, 0, sm->spare, + (sm->max_blocks * 0x10), progress_callback); return new_mpio; } @@ -330,8 +365,8 @@ mpio_get_info(mpio_t *m, mpio_info_t *info) } int -mpio_file_get(mpio_t *m, mpio_mem_t mem, BYTE *filename, - BYTE (*progress_callback)(int, int)) +mpio_file_get(mpio_t *m, mpio_mem_t mem, mpio_filename_t filename, + mpio_callback_t progress_callback) { mpio_smartmedia_t *sm; BYTE block[BLOCK_SIZE]; @@ -342,7 +377,10 @@ mpio_file_get(mpio_t *m, mpio_mem_t mem, BYTE *filename, long mtime; DWORD filesize, fsize; BYTE abort = 0; - + int merror; + + MPIO_CHECK_FILENAME(filename); + /* please fix me sometime */ /* the system entries are kind of special ! */ if (strncmp("sysdum", filename, 6) == 0) @@ -390,8 +428,11 @@ mpio_file_get(mpio_t *m, mpio_mem_t mem, BYTE *filename, if (abort) debug("aborting operation"); - } while ((mpio_fatentry_next_entry(m, mem, f) && (filesize>0)) && - (!abort)); + } while ((((merror=(mpio_fatentry_next_entry(m, mem, f)))>0) && + (filesize>0)) && (!abort)); + + if (merror<0) + debug("defective block encountered!\n"); close (fd); free (f); @@ -412,8 +453,8 @@ mpio_file_get(mpio_t *m, mpio_mem_t mem, BYTE *filename, } int -mpio_file_put(mpio_t *m, mpio_mem_t mem, BYTE *filename, - BYTE (*progress_callback)(int, int)) +mpio_file_put(mpio_t *m, mpio_mem_t mem, mpio_filename_t filename, + mpio_callback_t progress_callback) { mpio_smartmedia_t *sm; mpio_fatentry_t *f, current, firstblock, backup; @@ -425,6 +466,8 @@ mpio_file_put(mpio_t *m, mpio_mem_t mem, BYTE *filename, BYTE *p=NULL; DWORD filesize, fsize, free, blocks; BYTE abort=0; + + MPIO_CHECK_FILENAME(filename); if (mem==MPIO_INTERNAL_MEM) sm=&m->internal; if (mem==MPIO_EXTERNAL_MEM) sm=&m->external; @@ -665,8 +708,8 @@ mpio_memory_format(mpio_t *m, mpio_mem_t mem, } int -mpio_file_del(mpio_t *m, mpio_mem_t mem, BYTE *filename, - BYTE (*progress_callback)(int, int)) +mpio_file_del(mpio_t *m, mpio_mem_t mem, mpio_filename_t filename, + mpio_callback_t progress_callback) { BYTE *p; mpio_smartmedia_t *sm; @@ -674,6 +717,8 @@ mpio_file_del(mpio_t *m, mpio_mem_t mem, BYTE *filename, DWORD filesize, fsize; BYTE abort=0; + MPIO_CHECK_FILENAME(filename); + /* please fix me sometime */ /* the system entry are kind of special ! */ if (strncmp("sysdum", filename, 6)==0) @@ -752,10 +797,23 @@ mpio_sync(mpio_t *m, mpio_mem_t mem) } int -mpio_memory_debug(mpio_t *m, mpio_mem_t mem) +mpio_memory_dump(mpio_t *m, mpio_mem_t mem) { BYTE block[BLOCK_SIZE]; int i; + + if (mem == MPIO_INTERNAL_MEM) + { + hexdump(m->internal.fat, m->internal.max_blocks*0x10); + hexdump(m->internal.dir, DIR_SIZE); + } + + if (mem == MPIO_EXTERNAL_MEM) + { + hexdump(m->external.spare, m->external.max_blocks*0x10); + hexdump(m->external.fat, m->external.fat_size*SECTOR_SIZE); + hexdump(m->external.dir, DIR_SIZE); + } for (i = 0 ; i<=0x100 ; i++) mpio_io_sector_read(m, mem, i, block); diff --git a/libmpio/mpio.h b/libmpio/mpio.h index b3d781d..10272c9 100644 --- a/libmpio/mpio.h +++ b/libmpio/mpio.h @@ -1,7 +1,7 @@ -/* -*- linux-c -*- */ +#/* -*- linux-c -*- */ /* - * $Id: mpio.h,v 1.7 2002/09/19 22:23:01 crunchy Exp $ + * $Id: mpio.h,v 1.8 2002/09/23 22:38:03 germeier Exp $ * * Library for USB MPIO-* * @@ -45,7 +45,7 @@ *init and shutdown */ -mpio_t *mpio_init(BYTE (*)(int, int)); +mpio_t *mpio_init(mpio_callback_init_t); void mpio_close(mpio_t *); /* @@ -72,16 +72,16 @@ int mpio_dentry_get(mpio_t *, mpio_mem_t, BYTE *, BYTE *, int,WORD *, BYTE *, BYTE *, BYTE *, BYTE *, DWORD *); /* context, memory bank, filename, callback */ -int mpio_file_get(mpio_t *, mpio_mem_t, BYTE *, BYTE (*)(int, int)); +int mpio_file_get(mpio_t *, mpio_mem_t, mpio_filename_t, mpio_callback_t); /* context, memory bank, filename, callback */ -int mpio_file_put(mpio_t *, mpio_mem_t, BYTE *, BYTE (*)(int, int)); +int mpio_file_put(mpio_t *, mpio_mem_t, mpio_filename_t, mpio_callback_t); /* context, memory bank, filename, callback */ -int mpio_file_del(mpio_t *, mpio_mem_t, BYTE *, BYTE (*)(int, int)); +int mpio_file_del(mpio_t *, mpio_mem_t, mpio_filename_t, mpio_callback_t); /* context, memory bank, callback */ -int mpio_memory_format(mpio_t *, mpio_mem_t, BYTE (*)(int, int)); +int mpio_memory_format(mpio_t *, mpio_mem_t, mpio_callback_t); /* mpio_sync has to be called after every set of mpio_file_{del,put} * operations to write the current state of FAT and root directory. @@ -92,7 +92,7 @@ int mpio_memory_format(mpio_t *, mpio_mem_t, BYTE (*)(int, int)); int mpio_sync(mpio_t *, mpio_mem_t); /* context, memory bank */ -int mpio_memory_debug(mpio_t *, mpio_mem_t); +int mpio_memory_dump(mpio_t *, mpio_mem_t); /* * error handling diff --git a/mpiosh/callback.c b/mpiosh/callback.c index 6f9463c..8fd1e5f 100644 --- a/mpiosh/callback.c +++ b/mpiosh/callback.c @@ -2,7 +2,7 @@ * * Author: Andreas Büsching * - * $Id: callback.c,v 1.17 2002/09/21 22:17:15 germeier Exp $ + * $Id: callback.c,v 1.18 2002/09/23 22:38:03 germeier Exp $ * * Copyright (C) 2001 Andreas Büsching * @@ -31,7 +31,6 @@ #include #include "mpiosh.h" - #include "libmpio/debug.h" /* commands */ @@ -230,9 +229,21 @@ mpiosh_cmd_quit(char *args[]) } BYTE -mpiosh_callback_init(int read, int total) +mpiosh_callback_init(mpio_mem_t mem, int read, int total) { - printf("\rinitialized %.2f %%", ((double) read / total) * 100.0 ); + switch(mem) + { + case MPIO_INTERNAL_MEM: + printf("\rinternal memory: " ); + break; + case MPIO_EXTERNAL_MEM: + printf("\rexternal memory: " ); + break; + default: + printf("\runknown memory: " ); + } + + printf("initialized %.2f %% ", ((double) read / total) * 100.0 ); fflush(stdout); return mpiosh_cancel; // continue @@ -597,14 +608,14 @@ mpiosh_cmd_switch(char *args[]) } void -mpiosh_cmd_debug_mem(char *args[]) +mpiosh_cmd_dump_mem(char *args[]) { MPIOSH_CHECK_CONNECTION_CLOSED; UNUSED(args); - mpio_memory_debug(mpiosh.dev, mpiosh.card); + mpio_memory_dump(mpiosh.dev, mpiosh.card); } diff --git a/mpiosh/callback.h b/mpiosh/callback.h index 08a756c..05019bf 100644 --- a/mpiosh/callback.h +++ b/mpiosh/callback.h @@ -2,7 +2,7 @@ * * Author: Andreas Büsching * - * $Id: callback.h,v 1.4 2002/09/18 22:18:29 germeier Exp $ + * $Id: callback.h,v 1.5 2002/09/23 22:38:03 germeier Exp $ * * Copyright (C) 2001 Andreas Büsching * @@ -46,7 +46,7 @@ void mpiosh_cmd_dump(char *args[]); void mpiosh_cmd_free(char *args[]); void mpiosh_cmd_format(char *args[]); void mpiosh_cmd_switch(char *args[]); -void mpiosh_cmd_debug_mem(char *args[]); +void mpiosh_cmd_dump_mem(char *args[]); /* local command callbacks */ void mpiosh_cmd_ldir(char *args[]); @@ -55,7 +55,7 @@ void mpiosh_cmd_lcd(char *args[]); void mpiosh_cmd_lmkdir(char *args[]); /* progress callbacks */ -BYTE mpiosh_callback_init(int read, int total); +BYTE mpiosh_callback_init(mpio_mem_t, int read, int total); BYTE mpiosh_callback_get(int read, int total); BYTE mpiosh_callback_put(int read, int total); BYTE mpiosh_callback_del(int read, int total); diff --git a/mpiosh/mpiosh.c b/mpiosh/mpiosh.c index f40fd9d..508bf3d 100644 --- a/mpiosh/mpiosh.c +++ b/mpiosh/mpiosh.c @@ -2,7 +2,7 @@ /* * - * $Id: mpiosh.c,v 1.17 2002/09/21 22:17:15 germeier Exp $ + * $Id: mpiosh.c,v 1.18 2002/09/23 22:38:03 germeier Exp $ * * Author: Andreas Büsching * @@ -125,9 +125,9 @@ mpiosh_cmd_t commands[] = { { "lmkdir", NULL, "create a local directory", mpiosh_cmd_lmkdir }, - { "debug_memory", NULL, - "dump the first 0x100 of the selected memory card", - mpiosh_cmd_debug_mem }, + { "dump_memory", NULL, + "dump FAT, directory, spare area and the first 0x100 of the selected memory card", + mpiosh_cmd_dump_mem }, { NULL, NULL, NULL, NULL } }; -- cgit v1.2.3