]> git.kernelconcepts.de Git - karo-tx-uboot.git/blobdiff - tools/env/fw_env.c
Merge branch 'next'
[karo-tx-uboot.git] / tools / env / fw_env.c
index 9f8dfdccbf41a844d4aba98b309f49c0ee380cdb..9b023e807bc446bebf10caf2864d92dbed8469b3 100644 (file)
@@ -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()
 
@@ -120,102 +119,8 @@ static unsigned char active_flag = 1;
 /* obsolete_flag must be 0 to efficiently set it on NOR flash without erasing */
 static unsigned char obsolete_flag = 0;
 
-
-static char default_environment[] = {
-#if defined(CONFIG_BOOTARGS)
-       "bootargs=" CONFIG_BOOTARGS "\0"
-#endif
-#if defined(CONFIG_BOOTCOMMAND)
-       "bootcmd=" CONFIG_BOOTCOMMAND "\0"
-#endif
-#if defined(CONFIG_RAMBOOTCOMMAND)
-       "ramboot=" CONFIG_RAMBOOTCOMMAND "\0"
-#endif
-#if defined(CONFIG_NFSBOOTCOMMAND)
-       "nfsboot=" CONFIG_NFSBOOTCOMMAND "\0"
-#endif
-#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
-       "bootdelay=" __stringify(CONFIG_BOOTDELAY) "\0"
-#endif
-#if defined(CONFIG_BAUDRATE) && (CONFIG_BAUDRATE >= 0)
-       "baudrate=" __stringify(CONFIG_BAUDRATE) "\0"
-#endif
-#ifdef CONFIG_LOADS_ECHO
-       "loads_echo=" __stringify(CONFIG_LOADS_ECHO) "\0"
-#endif
-#ifdef CONFIG_ETHADDR
-       "ethaddr=" __stringify(CONFIG_ETHADDR) "\0"
-#endif
-#ifdef CONFIG_ETH1ADDR
-       "eth1addr=" __stringify(CONFIG_ETH1ADDR) "\0"
-#endif
-#ifdef CONFIG_ETH2ADDR
-       "eth2addr=" __stringify(CONFIG_ETH2ADDR) "\0"
-#endif
-#ifdef CONFIG_ETH3ADDR
-       "eth3addr=" __stringify(CONFIG_ETH3ADDR) "\0"
-#endif
-#ifdef CONFIG_ETH4ADDR
-       "eth4addr=" __stringify(CONFIG_ETH4ADDR) "\0"
-#endif
-#ifdef CONFIG_ETH5ADDR
-       "eth5addr=" __stringify(CONFIG_ETH5ADDR) "\0"
-#endif
-#ifdef CONFIG_ETHPRIME
-       "ethprime=" CONFIG_ETHPRIME "\0"
-#endif
-#ifdef CONFIG_IPADDR
-       "ipaddr=" __stringify(CONFIG_IPADDR) "\0"
-#endif
-#ifdef CONFIG_SERVERIP
-       "serverip=" __stringify(CONFIG_SERVERIP) "\0"
-#endif
-#ifdef CONFIG_SYS_AUTOLOAD
-       "autoload=" CONFIG_SYS_AUTOLOAD "\0"
-#endif
-#ifdef CONFIG_ROOTPATH
-       "rootpath=" CONFIG_ROOTPATH "\0"
-#endif
-#ifdef CONFIG_GATEWAYIP
-       "gatewayip=" __stringify(CONFIG_GATEWAYIP) "\0"
-#endif
-#ifdef CONFIG_NETMASK
-       "netmask=" __stringify(CONFIG_NETMASK) "\0"
-#endif
-#ifdef CONFIG_HOSTNAME
-       "hostname=" __stringify(CONFIG_HOSTNAME) "\0"
-#endif
-#ifdef CONFIG_BOOTFILE
-       "bootfile=" CONFIG_BOOTFILE "\0"
-#endif
-#ifdef CONFIG_LOADADDR
-       "loadaddr=" __stringify(CONFIG_LOADADDR) "\0"
-#endif
-#ifdef CONFIG_PREBOOT
-       "preboot=" CONFIG_PREBOOT "\0"
-#endif
-#ifdef CONFIG_CLOCKS_IN_MHZ
-       "clocks_in_mhz=" "1" "\0"
-#endif
-#if defined(CONFIG_PCI_BOOTDELAY) && (CONFIG_PCI_BOOTDELAY > 0)
-       "pcidelay=" __stringify(CONFIG_PCI_BOOTDELAY) "\0"
-#endif
-#ifdef CONFIG_ENV_VARS_UBOOT_CONFIG
-       "arch=" CONFIG_SYS_ARCH "\0"
-       "cpu=" CONFIG_SYS_CPU "\0"
-       "board=" CONFIG_SYS_BOARD "\0"
-#ifdef CONFIG_SYS_VENDOR
-       "vendor=" CONFIG_SYS_VENDOR "\0"
-#endif
-#ifdef CONFIG_SYS_SOC
-       "soc=" CONFIG_SYS_SOC "\0"
-#endif
-#endif
-#ifdef  CONFIG_EXTRA_ENV_SETTINGS
-       CONFIG_EXTRA_ENV_SETTINGS
-#endif
-       "\0"            /* Termimate struct environment data with 2 NULs */
-};
+#define DEFAULT_ENV_INSTANCE_STATIC
+#include <env_default.h>
 
 static int flash_io (int mode);
 static char *envmatch (char * s1, char * s2);
@@ -226,7 +131,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);
@@ -257,9 +162,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;
 
@@ -442,7 +344,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 */
@@ -480,7 +382,6 @@ int fw_setenv(int argc, char *argv[])
        int i, len;
        char *name;
        char *value = NULL;
-       char *tmpval = NULL;
 
        if (argc < 2) {
                errno = EINVAL;
@@ -494,34 +395,29 @@ 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);
+
+               if (value)
+                       value[len - 1] = ' ';
+               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++] = '\0';
        }
 
        fw_env_write(name, value);
 
-       if (value)
-               free(value);
+       free(value);
 
        return fw_env_close();
 }
@@ -749,8 +645,8 @@ static int flash_read_buf (int dev, int fd, void *buf, size_t count,
                        return -1;
                }
 #ifdef DEBUG
-               fprintf (stderr, "Read 0x%x bytes at 0x%llx\n",
-                        rc, blockstart + block_seek);
+               fprintf(stderr, "Read 0x%x bytes at 0x%llx on %s\n",
+                        rc, blockstart + block_seek, DEVNAME(dev));
 #endif
                processed += readlen;
                readlen = min (blocklen, count - processed);
@@ -829,6 +725,18 @@ static int flash_write_buf (int dev, int fd, void *buf, size_t count,
                if (write_total != rc)
                        return -1;
 
+#ifdef DEBUG
+               fprintf(stderr, "Preserving data ");
+               if (block_seek != 0)
+                       fprintf(stderr, "0x%x - 0x%lx", 0, block_seek - 1);
+               if (block_seek + count != write_total) {
+                       if (block_seek != 0)
+                               fprintf(stderr, " and ");
+                       fprintf(stderr, "0x%lx - 0x%x",
+                               block_seek + count, write_total - 1);
+               }
+               fprintf(stderr, "\n");
+#endif
                /* Overwrite the old environment */
                memcpy (data + block_seek, buf, count);
        } else {
@@ -887,7 +795,8 @@ static int flash_write_buf (int dev, int fd, void *buf, size_t count,
                }
 
 #ifdef DEBUG
-               printf ("Write 0x%x bytes at 0x%llx\n", erasesize, blockstart);
+               fprintf(stderr, "Write 0x%x bytes at 0x%llx\n", erasesize,
+                       blockstart);
 #endif
                if (write (fd, data + processed, erasesize) != erasesize) {
                        fprintf (stderr, "Write error on %s: %s\n",
@@ -954,11 +863,11 @@ static int flash_write (int fd_current, int fd_target, int dev_target)
        }
 
 #ifdef DEBUG
-       printf ("Writing new environment at 0x%lx on %s\n",
+       fprintf(stderr, "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;
@@ -968,7 +877,8 @@ static int flash_write (int fd_current, int fd_target, int dev_target)
                off_t offset = DEVOFFSET (dev_current) +
                        offsetof (struct env_image_redundant, flags);
 #ifdef DEBUG
-               printf ("Setting obsolete flag in environment at 0x%lx on %s\n",
+               fprintf(stderr,
+                       "Setting obsolete flag in environment at 0x%lx on %s\n",
                        DEVOFFSET (dev_current), DEVNAME (dev_current));
 #endif
                flash_flag_obsolete (dev_current, fd_current, offset);
@@ -997,10 +907,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)
@@ -1069,6 +979,8 @@ exit:
 
 static char *envmatch (char * s1, char * s2)
 {
+       if (s1 == NULL || s2 == NULL)
+               return NULL;
 
        while (*s1 == *s2++)
                if (*s1++ == '=')
@@ -1097,11 +1009,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;
        }
 
@@ -1136,11 +1048,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;
@@ -1233,6 +1145,9 @@ int fw_env_open(void)
                        /* Other pointers are already set */
                        free (addr1);
                }
+#ifdef DEBUG
+               fprintf(stderr, "Selected env in %s\n", DEVNAME(dev_current));
+#endif
        }
        return 0;
 }