tools: env: parse aes key / suppress flag into argument struct

disabled original parsing, but not yet removed since the
argument indexing needs to be fixed

Signed-off-by: Andreas Fenkart <andreas.fenkart@digitalstrom.com>
master
Andreas Fenkart 9 years ago committed by Tom Rini
parent 07ce944021
commit 371ee13760
  1. 64
      tools/env/fw_env.c
  2. 15
      tools/env/fw_env.h
  3. 31
      tools/env/fw_env_main.c

64
tools/env/fw_env.c vendored

@ -34,8 +34,6 @@
#include "fw_env.h" #include "fw_env.h"
#include <aes.h>
#define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d)) #define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
#define WHITESPACE(c) ((c == '\t') || (c == ' ')) #define WHITESPACE(c) ((c == '\t') || (c == ' '))
@ -105,9 +103,6 @@ static struct environment environment = {
.flag_scheme = FLAG_NONE, .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 env_aes_cbc_crypt(char *data, const int enc);
static int HaveRedundEnv = 0; static int HaveRedundEnv = 0;
@ -125,7 +120,6 @@ static int parse_config (void);
#if defined(CONFIG_FILE) #if defined(CONFIG_FILE)
static int get_config (char *); static int get_config (char *);
static char *config_file = CONFIG_FILE;
#endif #endif
static inline ulong getenvsize (void) static inline ulong getenvsize (void)
{ {
@ -134,7 +128,7 @@ static inline ulong getenvsize (void)
if (HaveRedundEnv) if (HaveRedundEnv)
rc -= sizeof (char); rc -= sizeof (char);
if (aes_flag) if (common_args.aes_flag)
rc &= ~(AES_KEY_LENGTH - 1); rc &= ~(AES_KEY_LENGTH - 1);
return rc; return rc;
@ -208,7 +202,7 @@ char *fw_getdefenv(char *name)
return NULL; return NULL;
} }
static int parse_aes_key(char *key, uint8_t *bin_key) int parse_aes_key(char *key, uint8_t *bin_key)
{ {
char tmp[5] = { '0', 'x', 0, 0, 0 }; char tmp[5] = { '0', 'x', 0, 0, 0 };
unsigned long ul; unsigned long ul;
@ -243,32 +237,16 @@ static int parse_aes_key(char *key, uint8_t *bin_key)
int fw_printenv (int argc, char *argv[]) int fw_printenv (int argc, char *argv[])
{ {
char *env, *nxt; char *env, *nxt;
int i, n_flag; int i, rc = 0;
int rc = 0;
#ifdef CONFIG_FILE #ifdef CONFIG_FILE
if (argc >= 2 && strcmp(argv[1], "-c") == 0) { if (argc >= 2 && strcmp(argv[1], "-c") == 0) {
if (argc < 3) {
fprintf(stderr,
"## Error: '-c' option requires the config file to use\n");
return -1;
}
config_file = argv[2];
argv += 2; argv += 2;
argc -= 2; argc -= 2;
} }
#endif #endif
if (argc >= 2 && strcmp(argv[1], "-a") == 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], aes_key);
if (rc)
return rc;
aes_flag = 1;
argv += 2; argv += 2;
argc -= 2; argc -= 2;
} }
@ -292,7 +270,6 @@ int fw_printenv (int argc, char *argv[])
} }
if (strcmp (argv[1], "-n") == 0) { if (strcmp (argv[1], "-n") == 0) {
n_flag = 1;
++argv; ++argv;
--argc; --argc;
if (argc != 2) { if (argc != 2) {
@ -300,8 +277,6 @@ int fw_printenv (int argc, char *argv[])
"`-n' option requires exactly one argument\n"); "`-n' option requires exactly one argument\n");
return -1; return -1;
} }
} else {
n_flag = 0;
} }
for (i = 1; i < argc; ++i) { /* print single env variables */ for (i = 1; i < argc; ++i) { /* print single env variables */
@ -319,7 +294,7 @@ int fw_printenv (int argc, char *argv[])
} }
val = envmatch (name, env); val = envmatch (name, env);
if (val) { if (val) {
if (!n_flag) { if (!printenv_args.name_suppress) {
fputs (name, stdout); fputs (name, stdout);
putc ('=', stdout); putc ('=', stdout);
} }
@ -339,7 +314,7 @@ int fw_printenv (int argc, char *argv[])
int fw_env_close(void) int fw_env_close(void)
{ {
int ret; int ret;
if (aes_flag) { if (common_args.aes_flag) {
ret = env_aes_cbc_crypt(environment.data, 1); ret = env_aes_cbc_crypt(environment.data, 1);
if (ret) { if (ret) {
fprintf(stderr, fprintf(stderr,
@ -495,7 +470,7 @@ int fw_env_write(char *name, char *value)
*/ */
int fw_setenv(int argc, char *argv[]) int fw_setenv(int argc, char *argv[])
{ {
int i, rc; int i;
size_t len; size_t len;
char *name, **valv; char *name, **valv;
char *value = NULL; char *value = NULL;
@ -503,12 +478,6 @@ int fw_setenv(int argc, char *argv[])
#ifdef CONFIG_FILE #ifdef CONFIG_FILE
if (argc >= 2 && strcmp(argv[1], "-c") == 0) { if (argc >= 2 && strcmp(argv[1], "-c") == 0) {
if (argc < 3) {
fprintf(stderr,
"## Error: '-c' option requires the config file to use\n");
return -1;
}
config_file = argv[2];
argv += 2; argv += 2;
argc -= 2; argc -= 2;
} }
@ -520,15 +489,6 @@ int fw_setenv(int argc, char *argv[])
} }
if (strcmp(argv[1], "-a") == 0) { 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], aes_key);
if (rc)
return rc;
aes_flag = 1;
argv += 2; argv += 2;
argc -= 2; argc -= 2;
} }
@ -1026,7 +986,7 @@ static int env_aes_cbc_crypt(char *payload, const int enc)
uint32_t aes_blocks; uint32_t aes_blocks;
/* First we expand the key. */ /* First we expand the key. */
aes_expand_key(aes_key, key_exp); aes_expand_key(common_args.aes_key, key_exp);
/* Calculate the number of AES blocks to encrypt. */ /* Calculate the number of AES blocks to encrypt. */
aes_blocks = DIV_ROUND_UP(len, AES_KEY_LENGTH); aes_blocks = DIV_ROUND_UP(len, AES_KEY_LENGTH);
@ -1254,7 +1214,7 @@ int fw_env_open(void)
crc0 = crc32 (0, (uint8_t *) environment.data, ENV_SIZE); crc0 = crc32 (0, (uint8_t *) environment.data, ENV_SIZE);
if (aes_flag) { if (common_args.aes_flag) {
ret = env_aes_cbc_crypt(environment.data, 0); ret = env_aes_cbc_crypt(environment.data, 0);
if (ret) if (ret)
return ret; return ret;
@ -1311,7 +1271,7 @@ int fw_env_open(void)
crc1 = crc32 (0, (uint8_t *) redundant->data, ENV_SIZE); crc1 = crc32 (0, (uint8_t *) redundant->data, ENV_SIZE);
if (aes_flag) { if (common_args.aes_flag) {
ret = env_aes_cbc_crypt(redundant->data, 0); ret = env_aes_cbc_crypt(redundant->data, 0);
if (ret) if (ret)
return ret; return ret;
@ -1395,9 +1355,9 @@ static int parse_config ()
#if defined(CONFIG_FILE) #if defined(CONFIG_FILE)
/* Fills in DEVNAME(), ENVSIZE(), DEVESIZE(). Or don't. */ /* Fills in DEVNAME(), ENVSIZE(), DEVESIZE(). Or don't. */
if (get_config (config_file)) { if (get_config(common_args.config_file)) {
fprintf (stderr, fprintf(stderr, "Cannot parse config file '%s': %m\n",
"Cannot parse config file '%s': %s\n", config_file, strerror (errno)); common_args.config_file);
return -1; return -1;
} }
#else #else

15
tools/env/fw_env.h vendored

@ -5,6 +5,9 @@
* SPDX-License-Identifier: GPL-2.0+ * SPDX-License-Identifier: GPL-2.0+
*/ */
#include <aes.h>
#include <stdint.h>
/* Pull in the current config to define the default environment */ /* Pull in the current config to define the default environment */
#include <linux/kconfig.h> #include <linux/kconfig.h>
@ -54,7 +57,17 @@
"bootm" "bootm"
#endif #endif
struct common_args {
#ifdef CONFIG_FILE
char *config_file;
#endif
uint8_t aes_key[AES_KEY_LENGTH];
int aes_flag; /* Is AES encryption used? */
};
extern struct common_args common_args;
struct printenv_args { struct printenv_args {
int name_suppress;
}; };
extern struct printenv_args printenv_args; extern struct printenv_args printenv_args;
@ -63,6 +76,8 @@ struct setenv_args {
}; };
extern struct setenv_args setenv_args; extern struct setenv_args setenv_args;
int parse_aes_key(char *key, uint8_t *bin_key);
extern int fw_printenv(int argc, char *argv[]); extern int fw_printenv(int argc, char *argv[]);
extern char *fw_getenv (char *name); extern char *fw_getenv (char *name);
extern int fw_setenv (int argc, char *argv[]); extern int fw_setenv (int argc, char *argv[]);

@ -45,6 +45,7 @@ static struct option long_options[] = {
{NULL, 0, NULL, 0} {NULL, 0, NULL, 0}
}; };
struct common_args common_args;
struct printenv_args printenv_args; struct printenv_args printenv_args;
struct setenv_args setenv_args; struct setenv_args setenv_args;
@ -84,17 +85,27 @@ int parse_printenv_args(int argc, char *argv[])
{ {
int c; int c;
#ifdef CONFIG_FILE
common_args.config_file = CONFIG_FILE;
#endif
while ((c = getopt_long (argc, argv, "a:c:ns:h", while ((c = getopt_long (argc, argv, "a:c:ns:h",
long_options, NULL)) != EOF) { long_options, NULL)) != EOF) {
switch (c) { switch (c) {
case 'a': case 'a':
/* AES key, handled later */ if (parse_aes_key(optarg, common_args.aes_key)) {
fprintf(stderr, "AES key parse error\n");
return EXIT_FAILURE;
}
common_args.aes_flag = 1;
break; break;
#ifdef CONFIG_FILE
case 'c': case 'c':
/* handled later */ common_args.config_file = optarg;
break; break;
#endif
case 'n': case 'n':
/* handled in fw_printenv */ printenv_args.name_suppress = 1;
break; break;
case 'h': case 'h':
usage(); usage();
@ -113,15 +124,25 @@ int parse_setenv_args(int argc, char *argv[])
{ {
int c; int c;
#ifdef CONFIG_FILE
common_args.config_file = CONFIG_FILE;
#endif
while ((c = getopt_long (argc, argv, "a:c:ns:h", while ((c = getopt_long (argc, argv, "a:c:ns:h",
long_options, NULL)) != EOF) { long_options, NULL)) != EOF) {
switch (c) { switch (c) {
case 'a': case 'a':
/* AES key, handled later */ if (parse_aes_key(optarg, common_args.aes_key)) {
fprintf(stderr, "AES key parse error\n");
return EXIT_FAILURE;
}
common_args.aes_flag = 1;
break; break;
#ifdef CONFIG_FILE
case 'c': case 'c':
/* handled later */ common_args.config_file = optarg;
break; break;
#endif
case 's': case 's':
setenv_args.script_file = optarg; setenv_args.script_file = optarg;
break; break;

Loading…
Cancel
Save