]> git.kernelconcepts.de Git - karo-tx-uboot.git/blobdiff - tools/env/fw_env.c
Revert "fw_env: fix building w/out a config.h"
[karo-tx-uboot.git] / tools / env / fw_env.c
index ab8c15d30eef712af4679d9aeeb5641a09afd097..37b60b80a7624566d9ecefdd04c0658134bd654d 100644 (file)
@@ -25,6 +25,7 @@
  */
 
 #include <errno.h>
+#include <env_flags.h>
 #include <fcntl.h>
 #include <linux/stringify.h>
 #include <stdio.h>
@@ -119,102 +120,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);
@@ -274,6 +181,32 @@ char *fw_getenv (char *name)
        return NULL;
 }
 
+/*
+ * Search the default environment for a variable.
+ * Return the value, if found, or NULL, if not found.
+ */
+char *fw_getdefenv(char *name)
+{
+       char *env, *nxt;
+
+       for (env = default_environment; *env; env = nxt + 1) {
+               char *val;
+
+               for (nxt = env; *nxt; ++nxt) {
+                       if (nxt >= &default_environment[ENV_SIZE]) {
+                               fprintf(stderr, "## Error: "
+                                       "default environment not terminated\n");
+                               return NULL;
+                       }
+               }
+               val = envmatch(name, env);
+               if (!val)
+                       continue;
+               return val;
+       }
+       return NULL;
+}
+
 /*
  * Print the current definition of one, or more, or all
  * environment variables
@@ -375,6 +308,7 @@ int fw_env_write(char *name, char *value)
        int len;
        char *env, *nxt;
        char *oldval = NULL;
+       int deleting, creating, overwriting;
 
        /*
         * search if variable with this name already exists
@@ -392,27 +326,49 @@ int fw_env_write(char *name, char *value)
                        break;
        }
 
-       /*
-        * Delete any existing definition
-        */
-       if (oldval) {
-#ifndef CONFIG_ENV_OVERWRITE
-               /*
-                * Ethernet Address and serial# can be set only once
-                */
-               if (
-                   (strcmp(name, "serial#") == 0) ||
-                   ((strcmp(name, "ethaddr") == 0)
-#if defined(CONFIG_OVERWRITE_ETHADDR_ONCE) && defined(CONFIG_ETHADDR)
-                   && (strcmp(oldval, __stringify(CONFIG_ETHADDR)) != 0)
-#endif /* CONFIG_OVERWRITE_ETHADDR_ONCE && CONFIG_ETHADDR */
-                  ) ) {
-                       fprintf (stderr, "Can't overwrite \"%s\"\n", name);
+       deleting = (oldval && !(value && strlen(value)));
+       creating = (!oldval && (value && strlen(value)));
+       overwriting = (oldval && (value && strlen(value)));
+
+       /* check for permission */
+       if (deleting) {
+               if (env_flags_validate_varaccess(name,
+                   ENV_FLAGS_VARACCESS_PREVENT_DELETE)) {
+                       printf("Can't delete \"%s\"\n", name);
                        errno = EROFS;
                        return -1;
                }
-#endif /* CONFIG_ENV_OVERWRITE */
+       } else if (overwriting) {
+               if (env_flags_validate_varaccess(name,
+                   ENV_FLAGS_VARACCESS_PREVENT_OVERWR)) {
+                       printf("Can't overwrite \"%s\"\n", name);
+                       errno = EROFS;
+                       return -1;
+               } else if (env_flags_validate_varaccess(name,
+                   ENV_FLAGS_VARACCESS_PREVENT_NONDEF_OVERWR)) {
+                       const char *defval = fw_getdefenv(name);
+
+                       if (defval == NULL)
+                               defval = "";
+                       if (strcmp(oldval, defval)
+                           != 0) {
+                               printf("Can't overwrite \"%s\"\n", name);
+                               errno = EROFS;
+                               return -1;
+                       }
+               }
+       } else if (creating) {
+               if (env_flags_validate_varaccess(name,
+                   ENV_FLAGS_VARACCESS_PREVENT_CREATE)) {
+                       printf("Can't create \"%s\"\n", name);
+                       errno = EROFS;
+                       return -1;
+               }
+       } else
+               /* Nothing to do */
+               return 0;
 
+       if (deleting || overwriting) {
                if (*++nxt == '\0') {
                        *env = '\0';
                } else {
@@ -473,7 +429,8 @@ int fw_env_write(char *name, char *value)
  */
 int fw_setenv(int argc, char *argv[])
 {
-       int i, len;
+       int i;
+       size_t len;
        char *name;
        char *value = NULL;
 
@@ -489,11 +446,16 @@ int fw_setenv(int argc, char *argv[])
 
        name = argv[1];
 
+       if (env_flags_validate_env_set_params(argc, argv) < 0)
+               return 1;
+
        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) {
                        fprintf(stderr,
@@ -504,9 +466,8 @@ int fw_setenv(int argc, char *argv[])
 
                memcpy(value + len, val, val_len);
                len += val_len;
-               value[len++] = ' ';
+               value[len++] = '\0';
        }
-       value[len - 1] = '\0';
 
        fw_env_write(name, value);
 
@@ -609,6 +570,11 @@ int fw_parse_script(char *fname)
                        name, val ? val : " removed");
 #endif
 
+               if (env_flags_validate_type(name, val) < 0) {
+                       ret = -1;
+                       break;
+               }
+
                /*
                 * If there is an error setting a variable,
                 * try to save the environment and returns an error
@@ -738,8 +704,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);
@@ -818,6 +784,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 {
@@ -876,7 +854,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",
@@ -943,7 +922,7 @@ 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,
@@ -957,7 +936,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);
@@ -1224,6 +1204,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;
 }