fw_printenv: Add -v and -q options.
PATCH 1/1 fw_printenv: Add -v and -q options.
Add -v for verbose output, "Unlocking flash...", "Done" etc. Add -q for quiet operation, do not print error and verbose messages.
Add a --help(-help, -?) option too.
The -q option is intended for scripting.
---
I hope this patch can be merge now as fw_printenv/fw_setenv is only a demo applikation.
tools/env/fw_env.c | 189 +++++++++++++++++++++++++++-------------------- tools/env/fw_env.h | 16 +++- tools/env/fw_env_main.c | 43 ++++++++--- 3 files changed, 153 insertions(+), 95 deletions(-)
tools/env/fw_env.c +109 -80
@@ -26,6 +26,7 @@#include <stdio.h> #include <stdlib.h> #include <stddef.h> +#include <stdarg.h> #include <string.h> #include <sys/types.h> #include <sys/ioctl.h>
@@ -53,6 +54,8 @@ typedef struct envdev_s {static envdev_t envdevices[2 ];static int curdev; +static int fw_quiet =0 ; +static int fw_verbose =0 ;#define DEVNAME (i) envdevices[(i)].devname#define DEVOFFSET (i) envdevices[(i)].devoff
@@ -164,6 +167,43 @@ static char *envmatch (char * s1, char * s2);static int env_init (void );static int parse_config (void ); +int check_option (int *argc,char *argv[],const char *option) +{ +int i,j; + +for (i =1 ; i < *argc; i++) +if (strcmp (argv[i], option) ==0 ) { +for (j=i; j < *argc; j++) + argv[j] = argv[j+1 ];/* remove option */ + *argc -=1 ; +return 1 ; + } +return 0 ; +} + +void check_quiet (int *argc,char *argv[]) +{ +if (check_option (argc, argv,"-q" )) + fw_quiet =1 ; +} + +void check_verbose (int *argc,char *argv[]) +{ +if (check_option (argc, argv,"-v" )) + fw_verbose =1 ; +} + +void fw_print (const char *fmt, ...) +{ +va_list ap; + +if (fw_quiet) +return ; +va_start (ap, fmt); +vfprintf (stderr, fmt, ap); +va_end (ap); +} +#if defined(CONFIG_FILE )static int get_config (char *);#endif
@@ -192,8 +232,8 @@ char *fw_getenv (char *name)for (nxt = env; *nxt; ++nxt) {if (nxt >= &environment.data[ENV_SIZE ]) { -fprintf (stderr,"## Error: " -"environment not terminated\n" ); +fw_print ("## Error: " +"environment not terminated\n" );return (NULL ); } }
@@ -221,8 +261,8 @@ void fw_printenv (int argc, char *argv[])for (env = environment.data; *env; env = nxt +1 ) {for (nxt = env; *nxt; ++nxt) {if (nxt >= &environment.data[ENV_SIZE ]) { -fprintf (stderr,"## Error: " -"environment not terminated\n" ); +fw_print ("## Error: " +"environment not terminated\n" );return ; } }
@@ -232,13 +272,11 @@ void fw_printenv (int argc, char *argv[])return ; } -if (strcmp (argv[1 ],"-n" ) ==0 ) { +if (check_option (&argc, argv,"-n" )) { n_flag =1 ; - ++argv; - --argc;if (argc !=2 ) { -fprintf (stderr,"## Error: " -"`-n' option requires exactly one argument\n" ); +fw_print ("## Error: " +"`-n' option requires exactly one argument\n" );return ; } }else {
@@ -253,8 +291,8 @@ void fw_printenv (int argc, char *argv[])for (nxt = env; *nxt; ++nxt) {if (nxt >= &environment.data[ENV_SIZE ]) { -fprintf (stderr,"## Error: " -"environment not terminated\n" ); +fw_print ("## Error: " +"environment not terminated\n" );return ; } }
@@ -269,7 +307,7 @@ void fw_printenv (int argc, char *argv[]) } }if (!val) -fprintf (stderr,"## Error: \"%s\" not defined\n" , name); +fw_print ("## Error: \"%s\" not defined\n" , name); } }
@@ -303,8 +341,8 @@ int fw_setenv (int argc, char *argv[])for (nxt = env = environment.data; *env; env = nxt +1 ) {for (nxt = env; *nxt; ++nxt) {if (nxt >= &environment.data[ENV_SIZE ]) { -fprintf (stderr,"## Error: " -"environment not terminated\n" ); +fw_print ("## Error: " +"environment not terminated\n" );return (EINVAL ); } }
@@ -321,7 +359,7 @@ int fw_setenv (int argc, char *argv[])*/ if ((strcmp (name,"ethaddr" ) ==0 ) || (strcmp (name,"serial#" ) ==0 )) { -fprintf (stderr,"Can't overwrite \"%s\"\n" , name); +fw_print ("Can't overwrite \"%s\"\n" , name);return (EROFS ); }
@@ -358,7 +396,7 @@ int fw_setenv (int argc, char *argv[]) len +=strlen (argv[i]) +1 ; }if (len > (&environment.data[ENV_SIZE ] - env)) { -fprintf (stderr, +fw_print ("Error: environment overflow, \"%s\" deleted\n" , name);return (-1 );
@@ -382,7 +420,7 @@ int fw_setenv (int argc, char *argv[])/* write environment back to flash */ if (flash_io (O_RDWR )) { -fprintf (stderr,"Error: can't write fw_env to flash\n" ); +fw_print ("Error: can't write fw_env to flash\n" );return (-1 ); }
@@ -396,7 +434,7 @@ static int flash_io (int mode)char *data =NULL ;if ((fd =open (DEVNAME (curdev), mode)) <0 ) { -fprintf (stderr, +fw_print ("Can't open %s: %s\n" ,DEVNAME (curdev),strerror (errno));return (-1 );
@@ -412,7 +450,7 @@ static int flash_io (int mode)/* switch to next partition for writing */ otherdev = !curdev;if ((fdr =open (DEVNAME (otherdev), mode)) <0 ) { -fprintf (stderr, +fw_print ("Can't open %s: %s\n" ,DEVNAME (otherdev),strerror (errno));
@@ -422,7 +460,8 @@ static int flash_io (int mode) otherdev = curdev; fdr = fd; } -printf ("Unlocking flash...\n" ); +if (fw_verbose) +fw_print ("Unlocking flash...\n" ); erase.length =DEVESIZE (otherdev); erase.start =DEVOFFSET (otherdev);ioctl (fdr,MEMUNLOCK , &erase);
@@ -434,11 +473,12 @@ static int flash_io (int mode) environment.flags = active_flag; } -printf ("Done\n" ); +if (fw_verbose) +fw_print ("Done\n" ); resid =DEVESIZE (otherdev) -CFG_ENV_SIZE ;if (resid) {if ((data =malloc (resid)) ==NULL ) { -fprintf (stderr, +fw_print ("Cannot malloc %d bytes: %s\n" , resid,strerror (errno));
@@ -446,13 +486,13 @@ static int flash_io (int mode) }if (lseek (fdr,DEVOFFSET (otherdev) +CFG_ENV_SIZE ,SEEK_SET ) ==-1 ) { -fprintf (stderr,"seek error on %s: %s\n" , +fw_print ("seek error on %s: %s\n" ,DEVNAME (otherdev),strerror (errno));return (-1 ); }if ((rc =read (fdr, data, resid)) != resid) { -fprintf (stderr, +fw_print ("read error on %s: %s\n" ,DEVNAME (otherdev),strerror (errno));
@@ -460,43 +500,42 @@ static int flash_io (int mode) } } -printf ("Erasing old environment...\n" ); +if (fw_verbose) +fw_print ("Erasing old environment...\n" ); erase.length =DEVESIZE (otherdev); erase.start =DEVOFFSET (otherdev);if (ioctl (fdr,MEMERASE , &erase) !=0 ) { -fprintf (stderr,"MTD erase error on %s: %s\n" , +fw_print ("MTD erase error on %s: %s\n" ,DEVNAME (otherdev),strerror (errno));return (-1 ); } -printf ("Done\n" ); +if (fw_verbose) +fw_print ("Done\n" ); -printf ("Writing environment to %s...\n" ,DEVNAME (otherdev)); +if (fw_verbose) +fw_print ("Writing environment to %s...\n" ,DEVNAME (otherdev));if (lseek (fdr,DEVOFFSET (otherdev),SEEK_SET ) ==-1 ) { -fprintf (stderr, -"seek error on %s: %s\n" , -DEVNAME (otherdev),strerror (errno)); +fw_print ("seek error on %s: %s\n" , +DEVNAME (otherdev),strerror (errno));return (-1 ); }if (write (fdr, &environment, len) != len) { -fprintf (stderr, -"CRC write error on %s: %s\n" , -DEVNAME (otherdev),strerror (errno)); +fw_print ("CRC write error on %s: %s\n" , +DEVNAME (otherdev),strerror (errno));return (-1 ); }if (write (fdr, environment.data,ENV_SIZE ) !=ENV_SIZE ) { -fprintf (stderr, -"Write error on %s: %s\n" , -DEVNAME (otherdev),strerror (errno)); +fw_print ("Write error on %s: %s\n" , +DEVNAME (otherdev),strerror (errno));return (-1 ); }if (resid) {if (write (fdr, data, resid) != resid) { -fprintf (stderr, -"write error on %s: %s\n" , -DEVNAME (curdev),strerror (errno)); +fw_print ("write error on %s: %s\n" , +DEVNAME (curdev),strerror (errno));return (-1 ); }free (data);
@@ -505,20 +544,21 @@ static int flash_io (int mode)/* change flag on current active env partition */ if (lseek (fd,DEVOFFSET (curdev) +sizeof (ulong),SEEK_SET ) ==-1 ) { -fprintf (stderr,"seek error on %s: %s\n" , -DEVNAME (curdev),strerror (errno)); +fw_print ("seek error on %s: %s\n" , +DEVNAME (curdev),strerror (errno));return (-1 ); }if (write (fd, &obsolete_flag,sizeof (obsolete_flag)) !=sizeof (obsolete_flag)) { -fprintf (stderr, -"Write error on %s: %s\n" , -DEVNAME (curdev),strerror (errno)); +fw_print ("Write error on %s: %s\n" , +DEVNAME (curdev),strerror (errno));return (-1 ); } } -printf ("Done\n" ); -printf ("Locking ...\n" ); +if (fw_verbose) +fw_print ("Done\n" ); +if (fw_verbose) +fw_print ("Locking ...\n" ); erase.length =DEVESIZE (otherdev); erase.start =DEVOFFSET (otherdev);ioctl (fdr,MEMLOCK , &erase);
@@ -527,40 +567,36 @@ static int flash_io (int mode) erase.start =DEVOFFSET (curdev);ioctl (fd,MEMLOCK , &erase);if (close (fdr)) { -fprintf (stderr, -"I/O error on %s: %s\n" , +fw_print ("I/O error on %s: %s\n" ,DEVNAME (otherdev),strerror (errno));return (-1 ); } } -printf ("Done\n" ); +if (fw_verbose) +fw_print ("Done\n" ); }else {if (lseek (fd,DEVOFFSET (curdev),SEEK_SET ) ==-1 ) { -fprintf (stderr, -"seek error on %s: %s\n" , -DEVNAME (curdev),strerror (errno)); +fw_print ("seek error on %s: %s\n" , +DEVNAME (curdev),strerror (errno));return (-1 ); }if (read (fd, &environment, len) != len) { -fprintf (stderr, -"CRC read error on %s: %s\n" , -DEVNAME (curdev),strerror (errno)); +fw_print ("CRC read error on %s: %s\n" , +DEVNAME (curdev),strerror (errno));return (-1 ); }if ((rc =read (fd, environment.data,ENV_SIZE )) !=ENV_SIZE ) { -fprintf (stderr, -"Read error on %s: %s\n" , -DEVNAME (curdev),strerror (errno)); +fw_print ("Read error on %s: %s\n" , +DEVNAME (curdev),strerror (errno));return (-1 ); } }if (close (fd)) { -fprintf (stderr, -"I/O error on %s: %s\n" , -DEVNAME (curdev),strerror (errno)); +fw_print ("I/O error on %s: %s\n" , +DEVNAME (curdev),strerror (errno));return (-1 ); }
@@ -600,9 +636,8 @@ static int env_init (void)return 1 ;if ((addr1 =calloc (1 ,ENV_SIZE )) ==NULL ) { -fprintf (stderr, -"Not enough memory for environment (%ld bytes)\n" , -ENV_SIZE ); +fw_print ("Not enough memory for environment (%ld bytes)\n" , +ENV_SIZE );return (errno); }
@@ -617,8 +652,7 @@ static int env_init (void) == environment.crc);if (!HaveRedundEnv) {if (!crc1_ok) { -fprintf (stderr, -"Warning: Bad CRC, using default environment\n" ); +fw_print ("Warning: Bad CRC, using default environment\n" );memcpy (environment.data, default_environment,sizeof default_environment); } }else {
@@ -626,9 +660,8 @@ static int env_init (void) curdev =1 ;if ((addr2 =calloc (1 ,ENV_SIZE )) ==NULL ) { -fprintf (stderr, -"Not enough memory for environment (%ld bytes)\n" , -ENV_SIZE ); +fw_print ("Not enough memory for environment (%ld bytes)\n" , +ENV_SIZE );return (errno); } environment.data = addr2;
@@ -654,8 +687,7 @@ static int env_init (void) curdev =1 ;free (addr1); }else if (!crc1_ok && !crc2_ok) { -fprintf (stderr, -"Warning: Bad CRC, using default environment\n" ); +fw_print ("Warning: Bad CRC, using default environment\n" );memcpy (environment.data, default_environment,sizeof default_environment); curdev =0 ;free (addr1);
@@ -702,8 +734,7 @@ static int parse_config ()#if defined(CONFIG_FILE )/* Fills in DEVNAME(), ENVSIZE(), DEVESIZE(). Or don't. */ if (get_config (CONFIG_FILE )) { -fprintf (stderr, -"Cannot parse config file: %s\n" ,strerror (errno)); +fw_print ("Cannot parse config file: %s\n" ,strerror (errno));return 1 ; }#else
@@ -720,16 +751,14 @@ static int parse_config ()#endif #endif if (stat (DEVNAME (0 ), &st)) { -fprintf (stderr, -"Cannot access MTD device %s: %s\n" , -DEVNAME (0 ),strerror (errno)); +fw_print ("Cannot access MTD device %s: %s\n" , +DEVNAME (0 ),strerror (errno));return 1 ; }if (HaveRedundEnv &&stat (DEVNAME (1 ), &st)) { -fprintf (stderr, -"Cannot access MTD device %s: %s\n" , -DEVNAME (1 ),strerror (errno)); +fw_print ("Cannot access MTD device %s: %s\n" , +DEVNAME (1 ),strerror (errno));return 1 ; }return 0 ;
tools/env/fw_env.h +11 -5
@@ -27,17 +27,18 @@* See included "fw_env.config" sample file (TRAB board) * for notes on configuration. */ -#define CONFIG_FILE "/etc/fw_env.config" + +/* #define CONFIG_FILE "/etc/fw_env.config" */ #define HAVE_REDUND /* For systems with 2 env sectors */ #define DEVICE1_NAME "/dev/mtd1"#define DEVICE2_NAME "/dev/mtd2"#define DEVICE1_OFFSET 0x0000 -#define ENV1_SIZE 0x4000 -#define DEVICE1_ESIZE 0x4000 +#define ENV1_SIZE 0x2000 +#define DEVICE1_ESIZE (32*1024)/* 32 KB */ #define DEVICE2_OFFSET 0x0000 -#define ENV2_SIZE 0x4000 -#define DEVICE2_ESIZE 0x4000 +#define ENV2_SIZE 0x2000 +#define DEVICE2_ESIZE (32*1024)/* 32 KB */ #define CONFIG_BAUDRATE 115200#define CONFIG_BOOTDELAY 5/* autoboot after 5 seconds */
@@ -51,4 +52,9 @@ extern void fw_printenv(int argc, char *argv[]);extern char *fw_getenv (char *name);extern int fw_setenv (int argc,char *argv[]); +extern int check_option (int *argc,char *argv[],const char *option); +extern void check_quiet (int *argc,char *argv[]); +extern void check_verbose (int *argc,char *argv[]); +extern void fw_print (const char *fmt, ...); +extern unsigned long crc32 (unsigned long ,const unsigned char *,unsigned );
tools/env/fw_env_main.c +33 -10
61 lines not on this page · raw message
Signature · 1 line
1.5.4.5
PATCH 1/1 fw_printenv: Add -v and -q options.
Quoted 3 lines · In message <[email protected]>…
Add -v for verbose output, "Unlocking flash...", "Done" etc. Add -q for quiet operation, do not print error and verbose messages.
This seems kind of redundandant to me.
And not printing error messages seems an error to me.
Quoted 3 lines
Add a --help(-help, -?) option too.
The -q option is intended for scripting.
Feel free to add "2>/dev/null" to your scripts when you think this is really what you want to have.
Quoted 15 lines
tools/env/fw_env.c +15
-0+int check_option (int *argc,char *argv[],const char *option) +{ +int i,j; + +for (i =1 ; i < *argc; i++) +if (strcmp (argv[i], option) ==0 ) { +for (j=i; j < *argc; j++) + argv[j] = argv[j+1 ];/* remove option */ + *argc -=1 ; +return 1 ; + } +return 0 ; +} + +void check_quiet (int *argc,char *argv[])
tools/env/fw_env.c static char *envmatch (char * s1, char * s2); · line 194
Quoted 10 lines
tools/env/fw_env.c +10
-0+{ +if (check_option (argc, argv,"-q" )) + fw_quiet =1 ; +} + +void check_verbose (int *argc,char *argv[]) +{ +if (check_option (argc, argv,"-v" )) + fw_verbose =1 ; +}
I've seen many diffeent versions of argument checking code. This is not a nice one. See for example "tools/mkimage.c" for the "classic" approach.
Best regards,
Wolfgang Denk
Signature · 5 lines
DENX Software Engineering GmbH, MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
Perl already has an IDE. It's called Unix.
-- Tom Christiansen in 375bd509 at cs.colorado.eduPATCH 1/1 fw_printenv: Add -v and -q options.
Quoted 8 lines · Wolfgang Denk
Quoted 3 lines · In message <[email protected]>…
Add -v for verbose output, "Unlocking flash...", "Done" etc. Add -q for quiet operation, do not print error and verbose messages.
This seems kind of redundandant to me.
And not printing error messages seems an error to me.
Perhaps a little overkill. How about removing the -v printouts "Unlocking flash...", "Done" etc. and removing the
"## Error: \"%s\" not defined\n", name);
message?
The verbose messages are a bit annoying when using it interactively and the error message when it can't find a variable too. Note that this particular error message is only
printed when using the -n option.
Quoted 7 lines
Quoted 4 lines
Add a --help(-help, -?) option too.
The -q option is intended for scripting.
Feel free to add "2>/dev/null" to your scripts when you think this is really what you want to have.
Yes, can do that too, just figured I could add a -q while I was at it.
Quoted 22 lines
Quoted 22 lines
tools/env/fw_env.c +21
-0+int check_option (int *argc,char *argv[],const char *option) +{ +int i,j; + +for (i =1 ; i < *argc; i++) +if (strcmp (argv[i], option) ==0 ) { +for (j=i; j < *argc; j++) + argv[j] = argv[j+1 ];/* remove option */ + *argc -=1 ; +return 1 ; + } +return 0 ; +} + +void check_quiet (int *argc,char *argv[]) +{ +if (check_option (argc, argv,"-q" )) + fw_quiet =1 ; +} + +void check_verbose (int *argc,char *argv[])
Quoted 8 lines
tools/env/fw_env.c static char *envmatch (char * s1, char * s2); · line 194
Quoted 4 lines
tools/env/fw_env.c +4
-0+{ +if (check_option (argc, argv,"-v" )) + fw_verbose =1 ; +}I've seen many diffeent versions of argument checking code. This is not a nice one. See for example "
tools/mkimage.c" for the "classic" approach.
:), I figured it was creative so I didn't have to touch so much of the existing code.