diff options
Diffstat (limited to 'tools/env/fw_env.c')
-rw-r--r-- | tools/env/fw_env.c | 104 |
1 files changed, 46 insertions, 58 deletions
diff --git a/tools/env/fw_env.c b/tools/env/fw_env.c index 1a2c22756e..ab8c15d30e 100644 --- a/tools/env/fw_env.c +++ b/tools/env/fw_env.c @@ -26,6 +26,7 @@ #include <errno.h> #include <fcntl.h> +#include <linux/stringify.h> #include <stdio.h> #include <stdlib.h> #include <stddef.h> @@ -45,8 +46,6 @@ #include "fw_env.h" -#include <config.h> - #define WHITESPACE(c) ((c == '\t') || (c == ' ')) #define min(x, y) ({ \ @@ -81,7 +80,7 @@ static int dev_current; #define ENVSECTORS(i) envdevices[(i)].env_sectors #define DEVTYPE(i) envdevices[(i)].mtd_type -#define CONFIG_ENV_SIZE ENVSIZE(dev_current) +#define CUR_ENVSIZE ENVSIZE(dev_current) #define ENV_SIZE getenvsize() @@ -121,9 +120,6 @@ static unsigned char active_flag = 1; static unsigned char obsolete_flag = 0; -#define XMK_STR(x) #x -#define MK_STR(x) XMK_STR(x) - static char default_environment[] = { #if defined(CONFIG_BOOTARGS) "bootargs=" CONFIG_BOOTARGS "\0" @@ -138,40 +134,40 @@ static char default_environment[] = { "nfsboot=" CONFIG_NFSBOOTCOMMAND "\0" #endif #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0) - "bootdelay=" MK_STR (CONFIG_BOOTDELAY) "\0" + "bootdelay=" __stringify(CONFIG_BOOTDELAY) "\0" #endif #if defined(CONFIG_BAUDRATE) && (CONFIG_BAUDRATE >= 0) - "baudrate=" MK_STR (CONFIG_BAUDRATE) "\0" + "baudrate=" __stringify(CONFIG_BAUDRATE) "\0" #endif #ifdef CONFIG_LOADS_ECHO - "loads_echo=" MK_STR (CONFIG_LOADS_ECHO) "\0" + "loads_echo=" __stringify(CONFIG_LOADS_ECHO) "\0" #endif #ifdef CONFIG_ETHADDR - "ethaddr=" MK_STR (CONFIG_ETHADDR) "\0" + "ethaddr=" __stringify(CONFIG_ETHADDR) "\0" #endif #ifdef CONFIG_ETH1ADDR - "eth1addr=" MK_STR (CONFIG_ETH1ADDR) "\0" + "eth1addr=" __stringify(CONFIG_ETH1ADDR) "\0" #endif #ifdef CONFIG_ETH2ADDR - "eth2addr=" MK_STR (CONFIG_ETH2ADDR) "\0" + "eth2addr=" __stringify(CONFIG_ETH2ADDR) "\0" #endif #ifdef CONFIG_ETH3ADDR - "eth3addr=" MK_STR (CONFIG_ETH3ADDR) "\0" + "eth3addr=" __stringify(CONFIG_ETH3ADDR) "\0" #endif #ifdef CONFIG_ETH4ADDR - "eth4addr=" MK_STR (CONFIG_ETH4ADDR) "\0" + "eth4addr=" __stringify(CONFIG_ETH4ADDR) "\0" #endif #ifdef CONFIG_ETH5ADDR - "eth5addr=" MK_STR (CONFIG_ETH5ADDR) "\0" + "eth5addr=" __stringify(CONFIG_ETH5ADDR) "\0" #endif #ifdef CONFIG_ETHPRIME "ethprime=" CONFIG_ETHPRIME "\0" #endif #ifdef CONFIG_IPADDR - "ipaddr=" MK_STR (CONFIG_IPADDR) "\0" + "ipaddr=" __stringify(CONFIG_IPADDR) "\0" #endif #ifdef CONFIG_SERVERIP - "serverip=" MK_STR (CONFIG_SERVERIP) "\0" + "serverip=" __stringify(CONFIG_SERVERIP) "\0" #endif #ifdef CONFIG_SYS_AUTOLOAD "autoload=" CONFIG_SYS_AUTOLOAD "\0" @@ -180,19 +176,19 @@ static char default_environment[] = { "rootpath=" CONFIG_ROOTPATH "\0" #endif #ifdef CONFIG_GATEWAYIP - "gatewayip=" MK_STR (CONFIG_GATEWAYIP) "\0" + "gatewayip=" __stringify(CONFIG_GATEWAYIP) "\0" #endif #ifdef CONFIG_NETMASK - "netmask=" MK_STR (CONFIG_NETMASK) "\0" + "netmask=" __stringify(CONFIG_NETMASK) "\0" #endif #ifdef CONFIG_HOSTNAME - "hostname=" MK_STR (CONFIG_HOSTNAME) "\0" + "hostname=" __stringify(CONFIG_HOSTNAME) "\0" #endif #ifdef CONFIG_BOOTFILE "bootfile=" CONFIG_BOOTFILE "\0" #endif #ifdef CONFIG_LOADADDR - "loadaddr=" MK_STR (CONFIG_LOADADDR) "\0" + "loadaddr=" __stringify(CONFIG_LOADADDR) "\0" #endif #ifdef CONFIG_PREBOOT "preboot=" CONFIG_PREBOOT "\0" @@ -201,7 +197,7 @@ static char default_environment[] = { "clocks_in_mhz=" "1" "\0" #endif #if defined(CONFIG_PCI_BOOTDELAY) && (CONFIG_PCI_BOOTDELAY > 0) - "pcidelay=" MK_STR (CONFIG_PCI_BOOTDELAY) "\0" + "pcidelay=" __stringify(CONFIG_PCI_BOOTDELAY) "\0" #endif #ifdef CONFIG_ENV_VARS_UBOOT_CONFIG "arch=" CONFIG_SYS_ARCH "\0" @@ -229,7 +225,7 @@ static int get_config (char *); #endif static inline ulong getenvsize (void) { - ulong rc = CONFIG_ENV_SIZE - sizeof (long); + ulong rc = CUR_ENVSIZE - sizeof(long); if (HaveRedundEnv) rc -= sizeof (char); @@ -260,9 +256,6 @@ char *fw_getenv (char *name) { char *env, *nxt; - if (fw_env_open()) - return NULL; - for (env = environment.data; *env; env = nxt + 1) { char *val; @@ -411,7 +404,7 @@ int fw_env_write(char *name, char *value) (strcmp(name, "serial#") == 0) || ((strcmp(name, "ethaddr") == 0) #if defined(CONFIG_OVERWRITE_ETHADDR_ONCE) && defined(CONFIG_ETHADDR) - && (strcmp(oldval, MK_STR(CONFIG_ETHADDR)) != 0) + && (strcmp(oldval, __stringify(CONFIG_ETHADDR)) != 0) #endif /* CONFIG_OVERWRITE_ETHADDR_ONCE && CONFIG_ETHADDR */ ) ) { fprintf (stderr, "Can't overwrite \"%s\"\n", name); @@ -445,7 +438,7 @@ int fw_env_write(char *name, char *value) ++env; /* * Overflow when: - * "name" + "=" + "val" +"\0\0" > CONFIG_ENV_SIZE - (env-environment) + * "name" + "=" + "val" +"\0\0" > CUR_ENVSIZE - (env-environment) */ len = strlen (name) + 2; /* add '=' for first arg, ' ' for all others */ @@ -483,7 +476,6 @@ int fw_setenv(int argc, char *argv[]) int i, len; char *name; char *value = NULL; - char *tmpval = NULL; if (argc < 2) { errno = EINVAL; @@ -497,34 +489,28 @@ int fw_setenv(int argc, char *argv[]) name = argv[1]; - len = strlen(name) + 2; - for (i = 2; i < argc; ++i) - len += strlen(argv[i]) + 1; - - /* Allocate enough place to the data string */ + len = 0; for (i = 2; i < argc; ++i) { char *val = argv[i]; + size_t val_len = strlen(val); + + value = realloc(value, len + val_len + 1); if (!value) { - value = (char *)malloc(len - strlen(name)); - if (!value) { - fprintf(stderr, + fprintf(stderr, "Cannot malloc %zu bytes: %s\n", - len - strlen(name), strerror(errno)); - return -1; - } - memset(value, 0, len - strlen(name)); - tmpval = value; + len, strerror(errno)); + return -1; } - if (i != 2) - *tmpval++ = ' '; - while (*val != '\0') - *tmpval++ = *val++; + + memcpy(value + len, val, val_len); + len += val_len; + value[len++] = ' '; } + value[len - 1] = '\0'; fw_env_write(name, value); - if (value) - free(value); + free(value); return fw_env_close(); } @@ -960,8 +946,8 @@ static int flash_write (int fd_current, int fd_target, int dev_target) printf ("Writing new environment at 0x%lx on %s\n", DEVOFFSET (dev_target), DEVNAME (dev_target)); #endif - rc = flash_write_buf (dev_target, fd_target, environment.image, - CONFIG_ENV_SIZE, DEVOFFSET (dev_target), + rc = flash_write_buf(dev_target, fd_target, environment.image, + CUR_ENVSIZE, DEVOFFSET(dev_target), DEVTYPE(dev_target)); if (rc < 0) return rc; @@ -1000,10 +986,10 @@ static int flash_read (int fd) DEVTYPE(dev_current) = mtdinfo.type; - rc = flash_read_buf (dev_current, fd, environment.image, CONFIG_ENV_SIZE, + rc = flash_read_buf(dev_current, fd, environment.image, CUR_ENVSIZE, DEVOFFSET (dev_current), mtdinfo.type); - return (rc != CONFIG_ENV_SIZE) ? -1 : 0; + return (rc != CUR_ENVSIZE) ? -1 : 0; } static int flash_io (int mode) @@ -1072,6 +1058,8 @@ exit: static char *envmatch (char * s1, char * s2) { + if (s1 == NULL || s2 == NULL) + return NULL; while (*s1 == *s2++) if (*s1++ == '=') @@ -1100,11 +1088,11 @@ int fw_env_open(void) if (parse_config ()) /* should fill envdevices */ return -1; - addr0 = calloc (1, CONFIG_ENV_SIZE); + addr0 = calloc(1, CUR_ENVSIZE); if (addr0 == NULL) { - fprintf (stderr, + fprintf(stderr, "Not enough memory for environment (%ld bytes)\n", - CONFIG_ENV_SIZE); + CUR_ENVSIZE); return -1; } @@ -1139,11 +1127,11 @@ int fw_env_open(void) flag0 = *environment.flags; dev_current = 1; - addr1 = calloc (1, CONFIG_ENV_SIZE); + addr1 = calloc(1, CUR_ENVSIZE); if (addr1 == NULL) { - fprintf (stderr, + fprintf(stderr, "Not enough memory for environment (%ld bytes)\n", - CONFIG_ENV_SIZE); + CUR_ENVSIZE); return -1; } redundant = addr1; |