]> git.kernelconcepts.de Git - karo-tx-uboot.git/blobdiff - tools/env/fw_env.c
Fix musl build
[karo-tx-uboot.git] / tools / env / fw_env.c
index d228cc34da8637444ee3264597f33bf7a634dfae..daa02a760e3744b252269113f1784671ed2fb03f 100644 (file)
@@ -8,6 +8,8 @@
  * SPDX-License-Identifier:    GPL-2.0+
  */
 
+#define _GNU_SOURCE
+
 #include <errno.h>
 #include <env_flags.h>
 #include <fcntl.h>
 
 #include "fw_env.h"
 
+#include <aes.h>
+
+#define DIV_ROUND_UP(n, d)     (((n) + (d) - 1) / (d))
+
 #define WHITESPACE(c) ((c == '\t') || (c == ' '))
 
 #define min(x, y) ({                           \
@@ -40,7 +46,7 @@
        _min1 < _min2 ? _min1 : _min2; })
 
 struct envdev_s {
-       char devname[16];               /* Device name */
+       const char *devname;            /* Device name */
        ulong devoff;                   /* Device offset */
        ulong env_size;                 /* environment size */
        ulong erase_size;               /* device erase size */
@@ -98,6 +104,11 @@ static struct environment environment = {
        .flag_scheme = FLAG_NONE,
 };
 
+/* Is AES encryption used? */
+static int aes_flag;
+static uint8_t aes_key[AES_KEY_LENGTH] = { 0 };
+static int env_aes_cbc_crypt(char *data, const int enc);
+
 static int HaveRedundEnv = 0;
 
 static unsigned char active_flag = 1;
@@ -116,10 +127,14 @@ static int get_config (char *);
 #endif
 static inline ulong getenvsize (void)
 {
-       ulong rc = CUR_ENVSIZE - sizeof(long);
+       ulong rc = CUR_ENVSIZE - sizeof(uint32_t);
 
        if (HaveRedundEnv)
                rc -= sizeof (char);
+
+       if (aes_flag)
+               rc &= ~(AES_KEY_LENGTH - 1);
+
        return rc;
 }
 
@@ -191,6 +206,36 @@ char *fw_getdefenv(char *name)
        return NULL;
 }
 
+static int parse_aes_key(char *key)
+{
+       char tmp[5] = { '0', 'x', 0, 0, 0 };
+       unsigned long ul;
+       int i;
+
+       if (strnlen(key, 64) != 32) {
+               fprintf(stderr,
+                       "## Error: '-a' option requires 16-byte AES key\n");
+               return -1;
+       }
+
+       for (i = 0; i < 16; i++) {
+               tmp[2] = key[0];
+               tmp[3] = key[1];
+               errno = 0;
+               ul = strtoul(tmp, NULL, 16);
+               if (errno) {
+                       fprintf(stderr,
+                               "## Error: '-a' option requires valid AES key\n");
+                       return -1;
+               }
+               aes_key[i] = ul & 0xff;
+               key += 2;
+       }
+       aes_flag = 1;
+
+       return 0;
+}
+
 /*
  * Print the current definition of one, or more, or all
  * environment variables
@@ -201,6 +246,19 @@ int fw_printenv (int argc, char *argv[])
        int i, n_flag;
        int rc = 0;
 
+       if (argc >= 2 && strcmp(argv[1], "-a") == 0) {
+               if (argc < 3) {
+                       fprintf(stderr,
+                               "## Error: '-a' option requires AES key\n");
+                       return -1;
+               }
+               rc = parse_aes_key(argv[2]);
+               if (rc)
+                       return rc;
+               argv += 2;
+               argc -= 2;
+       }
+
        if (fw_env_open())
                return -1;
 
@@ -266,6 +324,16 @@ int fw_printenv (int argc, char *argv[])
 
 int fw_env_close(void)
 {
+       int ret;
+       if (aes_flag) {
+               ret = env_aes_cbc_crypt(environment.data, 1);
+               if (ret) {
+                       fprintf(stderr,
+                               "Error: can't encrypt env for flash\n");
+                       return ret;
+               }
+       }
+
        /*
         * Update CRC
         */
@@ -413,7 +481,7 @@ int fw_env_write(char *name, char *value)
  */
 int fw_setenv(int argc, char *argv[])
 {
-       int i;
+       int i, rc;
        size_t len;
        char *name;
        char *value = NULL;
@@ -423,6 +491,24 @@ int fw_setenv(int argc, char *argv[])
                return -1;
        }
 
+       if (strcmp(argv[1], "-a") == 0) {
+               if (argc < 3) {
+                       fprintf(stderr,
+                               "## Error: '-a' option requires AES key\n");
+                       return -1;
+               }
+               rc = parse_aes_key(argv[2]);
+               if (rc)
+                       return rc;
+               argv += 2;
+               argc -= 2;
+       }
+
+       if (argc < 2) {
+               errno = EINVAL;
+               return -1;
+       }
+
        if (fw_env_open()) {
                fprintf(stderr, "Error: environment not initialized\n");
                return -1;
@@ -900,6 +986,28 @@ static int flash_flag_obsolete (int dev, int fd, off_t offset)
        return rc;
 }
 
+/* Encrypt or decrypt the environment before writing or reading it. */
+static int env_aes_cbc_crypt(char *payload, const int enc)
+{
+       uint8_t *data = (uint8_t *)payload;
+       const int len = getenvsize();
+       uint8_t key_exp[AES_EXPAND_KEY_LENGTH];
+       uint32_t aes_blocks;
+
+       /* First we expand the key. */
+       aes_expand_key(aes_key, key_exp);
+
+       /* Calculate the number of AES blocks to encrypt. */
+       aes_blocks = DIV_ROUND_UP(len, AES_KEY_LENGTH);
+
+       if (enc)
+               aes_cbc_encrypt_blocks(key_exp, data, data, aes_blocks);
+       else
+               aes_cbc_decrypt_blocks(key_exp, data, data, aes_blocks);
+
+       return 0;
+}
+
 static int flash_write (int fd_current, int fd_target, int dev_target)
 {
        int rc;
@@ -923,6 +1031,7 @@ static int flash_write (int fd_current, int fd_target, int dev_target)
        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,
                              CUR_ENVSIZE, DEVOFFSET(dev_target),
                              DEVTYPE(dev_target));
@@ -981,8 +1090,10 @@ static int flash_read (int fd)
 
        rc = flash_read_buf(dev_current, fd, environment.image, CUR_ENVSIZE,
                             DEVOFFSET (dev_current), mtdinfo.type);
+       if (rc != CUR_ENVSIZE)
+               return -1;
 
-       return (rc != CUR_ENVSIZE) ? -1 : 0;
+       return 0;
 }
 
 static int flash_io (int mode)
@@ -1075,6 +1186,8 @@ int fw_env_open(void)
        unsigned char flag1;
        void *addr1;
 
+       int ret;
+
        struct env_image_single *single;
        struct env_image_redundant *redundant;
 
@@ -1109,6 +1222,13 @@ int fw_env_open(void)
                return -1;
 
        crc0 = crc32 (0, (uint8_t *) environment.data, ENV_SIZE);
+
+       if (aes_flag) {
+               ret = env_aes_cbc_crypt(environment.data, 0);
+               if (ret)
+                       return ret;
+       }
+
        crc0_ok = (crc0 == *environment.crc);
        if (!HaveRedundEnv) {
                if (!crc0_ok) {
@@ -1159,6 +1279,13 @@ int fw_env_open(void)
                }
 
                crc1 = crc32 (0, (uint8_t *) redundant->data, ENV_SIZE);
+
+               if (aes_flag) {
+                       ret = env_aes_cbc_crypt(redundant->data, 0);
+                       if (ret)
+                               return ret;
+               }
+
                crc1_ok = (crc1 == redundant->crc);
                flag1 = redundant->flags;
 
@@ -1243,7 +1370,7 @@ static int parse_config ()
                return -1;
        }
 #else
-       strcpy (DEVNAME (0), DEVICE1_NAME);
+       DEVNAME (0) = DEVICE1_NAME;
        DEVOFFSET (0) = DEVICE1_OFFSET;
        ENVSIZE (0) = ENV1_SIZE;
        /* Default values are: erase-size=env-size */
@@ -1258,7 +1385,7 @@ static int parse_config ()
 #endif
 
 #ifdef HAVE_REDUND
-       strcpy (DEVNAME (1), DEVICE2_NAME);
+       DEVNAME (1) = DEVICE2_NAME;
        DEVOFFSET (1) = DEVICE2_OFFSET;
        ENVSIZE (1) = ENV2_SIZE;
        /* Default values are: erase-size=env-size */
@@ -1297,6 +1424,7 @@ static int get_config (char *fname)
        int i = 0;
        int rc;
        char dump[128];
+       char *devname;
 
        fp = fopen (fname, "r");
        if (fp == NULL)
@@ -1307,8 +1435,8 @@ static int get_config (char *fname)
                if (dump[0] == '#')
                        continue;
 
-               rc = sscanf (dump, "%s %lx %lx %lx %lx",
-                            DEVNAME (i),
+               rc = sscanf (dump, "%ms %lx %lx %lx %lx",
+                            &devname,
                             &DEVOFFSET (i),
                             &ENVSIZE (i),
                             &DEVESIZE (i),
@@ -1317,6 +1445,8 @@ static int get_config (char *fname)
                if (rc < 3)
                        continue;
 
+               DEVNAME(i) = devname;
+
                if (rc < 4)
                        /* Assume the erase size is the same as the env-size */
                        DEVESIZE(i) = ENVSIZE(i);