[U-Boot-Users] [PATCH] fw_printenv: Add -v and -q options.
Joakim Tjernlund
Joakim.Tjernlund at transmode.se
Fri Apr 18 15:57:01 CEST 2008
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.
Signed-off-by: Joakim Tjernlund <Joakim.Tjernlund at transmode.se>
---
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(-)
diff --git a/tools/env/fw_env.c b/tools/env/fw_env.c
index e083a5b..07872b4 100644
--- a/tools/env/fw_env.c
+++ b/tools/env/fw_env.c
@@ -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;
diff --git a/tools/env/fw_env.h b/tools/env/fw_env.h
index 58607de..bde6e75 100644
--- a/tools/env/fw_env.h
+++ b/tools/env/fw_env.h
@@ -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);
diff --git a/tools/env/fw_env_main.c b/tools/env/fw_env_main.c
index 696e30e..91776ea 100644
--- a/tools/env/fw_env_main.c
+++ b/tools/env/fw_env_main.c
@@ -51,28 +51,51 @@ main(int argc, char *argv[])
{
char *p;
char *cmdname = *argv;
+ int help;
if ((p = strrchr (cmdname, '/')) != NULL) {
cmdname = p + 1;
}
+ help = check_option(&argc, argv, "--help");
+ help |= check_option(&argc, argv, "-help");
+ help |= check_option(&argc, argv, "-?");
+ check_quiet(&argc, argv); /* checks for -q */
+ check_verbose(&argc, argv); /* checks for -v */
if (strcmp(cmdname, CMD_PRINTENV) == 0) {
-
- fw_printenv (argc, argv);
-
+ if (help) {
+ printf("Usage: "CMD_PRINTENV" "
+ "-q -v -n [name...]\n\n"
+ "Print firmware environment variables\n\n"
+ "Options:\n"
+ "-q - quiet, do not print error messages\n"
+ "-v - verbose\n"
+ "-n - print only value\n");
return (EXIT_SUCCESS);
+ }
+
+ fw_printenv (argc, argv);
+ return (EXIT_SUCCESS);
} else if (strcmp(cmdname, CMD_SETENV) == 0) {
+ if (help) {
+ printf("Usage: "CMD_SETENV" "
+ "-q -v name [value]\n\n"
+ "Set/Unset firmware environment variable\n\n"
+ "Options:\n"
+ "-q - quiet, do not print error messages\n"
+ "-v - verbose\n");
+ return (EXIT_SUCCESS);
+ }
- if (fw_setenv (argc, argv) != 0)
- return (EXIT_FAILURE);
+ if (fw_setenv (argc, argv) != 0)
+ return (EXIT_FAILURE);
- return (EXIT_SUCCESS);
+ return (EXIT_SUCCESS);
}
- fprintf (stderr,
- "Identity crisis - may be called as `" CMD_PRINTENV
- "' or as `" CMD_SETENV "' but not as `%s'\n",
- cmdname);
+ fw_print("Identity crisis - may be called as `" CMD_PRINTENV
+ "' or as `" CMD_SETENV "' but not as `%s'\n",
+ cmdname);
return (EXIT_FAILURE);
}
--
1.5.4.5
More information about the U-Boot
mailing list