nand{dump, test, write}: clean up --help handling
PATCH v2 1/4 nand{dump, test, write}: clean up --help handling
We should send the output to stdout when the user passes -h/--help and then exit(0), but otherwise the output should go to stderr and then exit(1).
---
v2
- no changes from v1
nanddump.c | 24 ++++++++++++------------ nandtest.c | 16 ++++++++++------ nandwrite.c | 26 +++++++++++++------------- 3 files changed, 35 insertions(+), 31 deletions(-)
nanddump.c +12 -12
@@ -33,13 +33,13 @@#include "common.h" #include <libmtd.h> -static void display_help (void ) +static void display_help (int status) { -printf ( +fprintf (status ==EXIT_SUCCESS ? stdout : stderr,"Usage: %s [OPTIONS] MTD-device\n" "Dumps the contents of a nand mtd partition.\n" "\n" -" --help Display this help and exit\n" +"-h --help Display this help and exit\n" " --version Output version information and exit\n" " --bb=METHOD Choose bad block handling method (see below).\n" "-a --forcebinary Force printing of binary data to tty\n"
@@ -58,7 +58,7 @@ static void display_help(void)" dumpbad: dump flash data, including any bad blocks\n" " skipbad: dump good data, completely skipping any bad blocks (default)\n" , PROGRAM_NAME); -exit (EXIT_SUCCESS ); +exit (status); }static void display_version (void )
@@ -101,12 +101,12 @@ static void process_options(int argc, char * const argv[])for (;;) {int option_index =0 ; -static const char *short_options ="s:f:l:opqnca" ; +static const char short_options[] ="hs:f:l:opqnca" ;static const struct option long_options[] = { - {"help" , no_argument,0 ,0 }, {"version" , no_argument,0 ,0 }, {"bb" , required_argument,0 ,0 }, {"omitoob" , no_argument,0 ,0 }, + {"help" , no_argument,0 ,'h' }, {"forcebinary" , no_argument,0 ,'a' }, {"canonicalprint" , no_argument,0 ,'c' }, {"file" , required_argument,0 ,'f' },
@@ -129,12 +129,9 @@ static void process_options(int argc, char * const argv[])case 0 :switch (option_index) {case 0 : -display_help (); -break ; -case 1 :display_version ();break ; -case 2 : +case 1 :/* Handle --bb=METHOD */ if (!strcmp (optarg,"padbad" )) bb_method = padbad;
@@ -145,7 +142,7 @@ static void process_options(int argc, char * const argv[])else error++;break ; -case 3 :/* --omitoob */ +case 2 :/* --omitoob */ if (oob_default) { oob_default = false; omitoob = true;
@@ -186,6 +183,9 @@ static void process_options(int argc, char * const argv[])case 'n' : noecc = true;break ; +case 'h' : +display_help (EXIT_SUCCESS ); +break ;case '?' : error++;break ;
@@ -213,7 +213,7 @@ static void process_options(int argc, char * const argv[]) }if ((argc - optind) !=1 || error) -display_help (); +display_help (EXIT_FAILURE ); mtddev = argv[optind]; }
nandtest.c +10 -6
@@ -16,9 +16,10 @@#include <asm/types.h> #include "mtd/mtd-user.h" -void usage (void ) +void usage (int status) { -fprintf (stderr,"usage: %s [OPTIONS] <device>\n\n" +fprintf (status ? stderr : stdout, +"usage: %s [OPTIONS] <device>\n\n" " -h, --help Display this help output\n" " -m, --markbad Mark blocks bad if they appear so\n" " -s, --seed Supply random seed\n"
@@ -27,7 +28,7 @@ void usage(void)" -l, --length Length of flash to test\n" " -k, --keep Restore existing contents after test\n" , PROGRAM_NAME); -exit (1 ); +exit (status); }struct mtd_info_user meminfo;
@@ -142,7 +143,7 @@ int main(int argc, char **argv) seed =time (NULL );for (;;) { -static const char *short_options="hkl:mo:p:s:" ; +static const char short_options[] ="hkl:mo:p:s:" ;static const struct option long_options[] = { {"help" , no_argument,0 ,'h' }, {"markbad" , no_argument,0 ,'m' },
@@ -160,8 +161,11 @@ int main(int argc, char **argv)switch (c) {case 'h' : +usage (0 ); +break ; +case '?' : -usage (); +usage (1 );break ;case 'm' :
@@ -191,7 +195,7 @@ int main(int argc, char **argv) } }if (argc - optind !=1 ) -usage (); +usage (1 ); fd =open (argv[optind],O_RDWR );if (fd <0 ) {
nandwrite.c +13 -13
@@ -42,9 +42,9 @@#include "common.h" #include <libmtd.h> -static void display_help (void ) +static void display_help (int status) { -printf ( +fprintf (status ==EXIT_SUCCESS ? stdout : stderr,"Usage: nandwrite [OPTION] MTD_DEVICE [INPUTFILE|-]\n" "Writes to the specified MTD device.\n" "\n"
@@ -58,10 +58,10 @@ static void display_help(void)" -p, --pad Pad to page size\n" " -b, --blockalign=1|2|4 Set multiple of eraseblocks to align to\n" " -q, --quiet Don't display progress messages\n" -" --help Display this help and exit\n" +" -h, --help Display this help and exit\n" " --version Output version information and exit\n" ); -exit (EXIT_SUCCESS ); +exit (status); }static void display_version (void )
@@ -99,10 +99,10 @@ static void process_options(int argc, char * const argv[])for (;;) {int option_index =0 ; -static const char *short_options ="b:mnNoOpqs:a" ; +static const char short_options[] ="hb:mnNoOpqs:a" ;static const struct option long_options[] = { - {"help" , no_argument,0 ,0 }, {"version" , no_argument,0 ,0 }, + {"help" , no_argument,0 ,'h' }, {"blockalign" , required_argument,0 ,'b' }, {"markbad" , no_argument,0 ,'m' }, {"noecc" , no_argument,0 ,'n' },
@@ -124,12 +124,9 @@ static void process_options(int argc, char * const argv[])switch (c) {case 0 :switch (option_index) { -case 0 : -display_help (); -break ; -case 1 : -display_version (); -break ; +case 0 :/* --version */ +display_version (); +break ; }break ;case 'q' :
@@ -163,6 +160,9 @@ static void process_options(int argc, char * const argv[])case 'a' : autoplace = true;break ; +case 'h' : +display_help (EXIT_SUCCESS ); +break ;case '?' : error++;break ;
@@ -192,7 +192,7 @@ static void process_options(int argc, char * const argv[])*/ if (argc <1 || argc >2 || error) -display_help (); +display_help (EXIT_FAILURE ); mtd_device = argv[0 ];
Signature · 1 line
1.8.2.1
PATCH v2 2/4 nandwrite: clean up length types
We use 'int' in many places to represent offsets/sizes. That obviously does not play well with larger NAND devices on 32bit systems. Instead, use the right type as needed:
- long long to represent the length of the image
- use fstat() rather than lseek();lseek(); to get the length of the image
- use size_t/ssize_t when working with read()
- tweak the printf formats as needed
---
v2
- no changes from v1
nandwrite.c | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-)
nandwrite.c +21 -16
@@ -218,10 +218,10 @@ static void erase_buffer(void *buffer, size_t size)*/ int main (int argc,char *const argv[]) { -int cnt =0 ;int fd =-1 ;int ifd =-1 ; -int imglen =0 , pagelen; +int pagelen; +long long imglen =0 ;bool baderaseblock = false;long long blockstart =-1 ;struct mtd_dev_info mtd;
@@ -313,8 +313,12 @@ int main(int argc, char * const argv[])if (ifd ==STDIN_FILENO ) { imglen = pagelen; }else { - imglen =lseek (ifd,0 ,SEEK_END ); -lseek (ifd,0 ,SEEK_SET ); +struct stat st; +if (fstat (ifd, &st)) { +sys_errmsg ("unable to stat input image" ); + goto closeall; + } + imglen = st.st_size; }/* Check, if file is page-aligned */
@@ -326,7 +330,7 @@ int main(int argc, char * const argv[])/* Check, if length fits into device */ if ((imglen / pagelen) * mtd.min_io_size > mtd.size - mtdoffset) { -fprintf (stderr,"Image %d bytes, NAND page %d bytes, OOB area %d" +fprintf (stderr,"Image %lld bytes, NAND page %d bytes, OOB area %d" " bytes, device size %lld bytes\n" , imglen, pagelen, mtd.oob_size, mtd.size);sys_errmsg ("Input file does not fit into device" );
@@ -412,9 +416,10 @@ int main(int argc, char * const argv[])/* Read more data from the input if there isn't enough in the buffer */ if (writebuf + mtd.min_io_size > filebuf + filebuf_len) { -int readlen = mtd.min_io_size; -int alreadyread = (filebuf + filebuf_len) - writebuf; -int tinycnt = alreadyread; +size_t readlen = mtd.min_io_size; +size_t alreadyread = (filebuf + filebuf_len) - writebuf; +size_t tinycnt = alreadyread; +ssize_t cnt =0 ;while (tinycnt < readlen) { cnt =read (ifd, writebuf + tinycnt, readlen - tinycnt);
@@ -444,7 +449,7 @@ int main(int argc, char * const argv[])if (tinycnt < readlen) {if (!pad) {fprintf (stderr,"Unexpected EOF. Expecting at least " -"%d more bytes. Use the padding option.\n" , +"%zu more bytes. Use the padding option.\n" , readlen - tinycnt); goto closeall; }
@@ -465,9 +470,10 @@ int main(int argc, char * const argv[])/* Read more data for the OOB from the input if there isn't enough in the buffer */ if (oobbuf + mtd.oob_size > filebuf + filebuf_len) { -int readlen = mtd.oob_size; -int alreadyread = (filebuf + filebuf_len) - oobbuf; -int tinycnt = alreadyread; +size_t readlen = mtd.oob_size; +size_t alreadyread = (filebuf + filebuf_len) - oobbuf; +size_t tinycnt = alreadyread; +ssize_t cnt;while (tinycnt < readlen) { cnt =read (ifd, oobbuf + tinycnt, readlen - tinycnt);
@@ -482,7 +488,7 @@ int main(int argc, char * const argv[])if (tinycnt < readlen) {fprintf (stderr,"Unexpected EOF. Expecting at least " -"%d more bytes for OOB\n" , readlen - tinycnt); +"%zu more bytes for OOB\n" , readlen - tinycnt); goto closeall; }
@@ -505,7 +511,7 @@ int main(int argc, char * const argv[]) writeoob ? mtd.oob_size :0 , write_mode);if (ret) { -int i; +long long i;if (errno !=EIO ) {sys_errmsg ("%s: MTD write failure" , mtd_device); goto closeall;
@@ -520,9 +526,8 @@ int main(int argc, char * const argv[])if (mtd_erase (mtd_desc, &mtd, fd, i / mtd.eb_size)) {int errno_tmp = errno;sys_errmsg ("%s: MTD Erase failure" , mtd_device); -if (errno_tmp !=EIO ) { +if (errno_tmp !=EIO ) goto closeall; - } } }
Signature · 1 line
1.8.2.1
PATCH v2 3/4 nandwrite: add --input-{skip,size} options
In reply to Mike FrysingerIf you have a file image and want to copy sub-portions out and into NAND, there's no easy way to do that. You can use dd to extract it to a temp file, or pipe it to nandwrite 1 page at a time. Both suck.
Add two new flags to explicitly set the size and offset of the input file. Seeking stdin isn't currently supported as I'm not sure it's necessary. It wouldn't be hard to add though...
---
v2
- removed inputskip from imglen when getting size from stat()
nandwrite.c | 54 ++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 34 insertions(+), 20 deletions(-)
nandwrite.c +34 -20
@@ -52,11 +52,13 @@ static void display_help(int status)" -m, --markbad Mark blocks bad if write fails\n" " -n, --noecc Write without ecc\n" " -N, --noskipbad Write without bad block skipping\n" -" -o, --oob Image contains oob data\n" -" -O, --onlyoob Image contains oob data and only write the oob part\n" -" -s addr, --start=addr Set start address (default is 0)\n" -" -p, --pad Pad to page size\n" +" -o, --oob Input contains oob data\n" +" -O, --onlyoob Input contains oob data and only write the oob part\n" +" -s addr, --start=addr Set output start address (default is 0)\n" +" -p, --pad Pad writes to page size\n" " -b, --blockalign=1|2|4 Set multiple of eraseblocks to align to\n" +" --input-skip=length Skip |length| bytes of the input file\n" +" --input-size=length Only read |length| bytes of the input file\n" " -q, --quiet Don't display progress messages\n" " -h, --help Display this help and exit\n" " --version Output version information and exit\n"
@@ -83,6 +85,8 @@ static void display_version(void)static const char *standard_input ="-" ;static const char *mtd_device, *img;static long long mtdoffset =0 ; +static long long inputskip =0 ; +static long long inputsize =0 ;static bool quiet = false;static bool writeoob = false;static bool onlyoob = false;
@@ -101,7 +105,10 @@ static void process_options(int argc, char * const argv[])int option_index =0 ;static const char short_options[] ="hb:mnNoOpqs:a" ;static const struct option long_options[] = { +/* Order of these args with val==0 matters; see option_index. */ {"version" , no_argument,0 ,0 }, + {"input-skip" , required_argument,0 ,0 }, + {"input-size" , required_argument,0 ,0 }, {"help" , no_argument,0 ,'h' }, {"blockalign" , required_argument,0 ,'b' }, {"markbad" , no_argument,0 ,'m' },
@@ -127,6 +134,12 @@ static void process_options(int argc, char * const argv[])case 0 :/* --version */ display_version ();break ; +case 1 :/* --input-skip */ + inputskip =simple_strtoll (optarg, &error); +break ; +case 2 :/* --input-size */ + inputsize =simple_strtoll (optarg, &error); +break ; }break ;case 'q' :
@@ -299,26 +312,27 @@ int main(int argc, char * const argv[]) pagelen = mtd.min_io_size + ((writeoob) ? mtd.oob_size :0 ); -/* -* For the standard input case, the input size is merely an -* invariant placeholder and is set to the write page -* size. Otherwise, just use the input file size. -* -* TODO: Add support for the -l,--length=length option (see -* previous discussion by Tommi Airikka <[email protected]> at -* <http://lists.infradead.org/pipermail/linux-mtd/2008-September/ -* 022913.html> -*/ -if (ifd ==STDIN_FILENO ) { - imglen = pagelen; + imglen = inputsize ? : pagelen; +if (inputskip) { +errmsg ("seeking stdin not supported" ); + goto closeall; + } }else { -struct stat st; -if (fstat (ifd, &st)) { -sys_errmsg ("unable to stat input image" ); +if (!inputsize) { +struct stat st; +if (fstat (ifd, &st)) { +sys_errmsg ("unable to stat input image" ); + goto closeall; + } + imglen = st.st_size - inputskip; + }else + imglen = inputsize; + +if (inputskip &&lseek (ifd, inputskip,SEEK_CUR ) ==-1 ) { +sys_errmsg ("lseek input by %lld failed" , inputskip); goto closeall; } - imglen = st.st_size; }/* Check, if file is page-aligned */
Signature · 1 line
1.8.2.1
PATCH v2 4/4 ubiupdatevol: add a --skip option
In reply to Mike FrysingerThis already has a --size option for controlling how many bytes to read from the input. Add a --skip option to control the offset into the input too. This way people don't have to do dd | ubiupdatevol.
While we're here, I've fixed the types used with args.size and the read loop so that they can hold the right sizes (like setting a 32bit+ size).
---
v2
- there was no v1!
ubi-utils/ubiupdatevol.c | 45 +++++++++++++++++++++++++++++++++------------ 1 file changed, 33 insertions(+), 12 deletions(-)
ubi-utils/ubiupdatevol.c +33 -12
@@ -45,7 +45,8 @@ struct args {const char *img;/* For deprecated -d and -B options handling */ char dev_name[256 ]; -int size; +long long size; +long long skip;int use_stdin; };
@@ -56,7 +57,8 @@ static const char doc[] = PROGRAM_NAME " version " VERSIONstatic const char optionsstr[] ="-t, --truncate truncate volume (wipe it out)\n" -"-s, --size=<bytes> bytes in input, if not reading from file\n" +"-s, --size=<bytes> bytes to read from input\n" +" --skip=<bytes> leading bytes to skip from input\n" "-h, --help print help message\n" "-V, --version print program version" ;
@@ -67,6 +69,8 @@ static const char usage[] ="Example 2: " PROGRAM_NAME " /dev/ubi0_1 -t - wipe out UBI volume /dev/ubi0_1" ;static const struct option long_options[] = { +/* Order matters for opts w/val=0; see option_index below. */ + { .name ="skip" , .has_arg =1 , .flag =NULL , .val =0 }, { .name ="truncate" , .has_arg =0 , .flag =NULL , .val ='t' }, { .name ="help" , .has_arg =0 , .flag =NULL , .val ='h' }, { .name ="version" , .has_arg =0 , .flag =NULL , .val ='V' },
@@ -77,19 +81,29 @@ static const struct option long_options[] = {static int parse_opt (int argc,char *const argv[]) {while (1 ) { -int key, error =0 ; +int option_index, key, error =0 ; - key =getopt_long (argc, argv,"ts:h?V" , long_options,NULL ); + key =getopt_long (argc, argv,"ts:h?V" , long_options, &option_index);if (key ==-1 )break ;switch (key) { +case 0 : +switch (option_index) { +case 0 :/* --skip */ + args.skip =simple_strtoull (optarg, &error); +if (error || args.skip <0 ) +return errmsg ("bad skip: " "\"%s\"" , optarg); +break ; + } +break ; +case 't' : args.truncate =1 ;break ;case 's' : - args.size =simple_strtoul (optarg, &error); + args.size =simple_strtoull (optarg, &error);if (error || args.size <0 )return errmsg ("bad size: " "\"%s\"" , optarg);break ;
@@ -198,7 +212,7 @@ static int update_volume(libubi_t libubi, struct ubi_vol_info *vol_info) goto out_free; } - bytes = st.st_size; + bytes = st.st_size - args.skip; }else bytes = args.size;
@@ -214,14 +228,23 @@ static int update_volume(libubi_t libubi, struct ubi_vol_info *vol_info) goto out_free; } -if (args.use_stdin) +if (args.use_stdin) { ifd =STDIN_FILENO ; -else { +if (args.skip) { +errmsg ("seeking stdin not supported" ); + goto out_close1; + } + }else { ifd =open (args.img,O_RDONLY );if (ifd ==-1 ) {sys_errmsg ("cannot open \"%s\"" , args.img); goto out_close1; } + +if (args.skip &&lseek (ifd, args.skip,SEEK_CUR ) ==-1 ) { +sys_errmsg ("lseek input by %lld failed" , args.skip); + goto out_close; + } } err =ubi_update_start (libubi, fd, bytes);
@@ -231,10 +254,8 @@ static int update_volume(libubi_t libubi, struct ubi_vol_info *vol_info) }while (bytes) { -int ret, to_copy = vol_info->leb_size; - -if (to_copy > bytes) - to_copy = bytes; +ssize_t ret; +int to_copy =min (vol_info->leb_size, bytes); ret =read (ifd, buf, to_copy);if (ret <=0 ) {
Signature · 1 line
1.8.2.1
Quoted 7 lines · Mike Frysinger
We should send the output to stdout when the user passes -h/--help and then exit(0), but otherwise the output should go to stderr and then exit(1).
Signed-off-by Mike Frysinger <[email protected]>---
v2
Ok, I've committed a delta between v2 and v1 as well. And then also pushed the "[PATCH mtd-utils 4/4 v2] ubiupdatevol: add a --skip option" patch. So everything should be there.
Signature · 2 lines
Best Regards, Artem Bityutskiy