tools/env: no global variable sharing between application and library

Signed-off-by: Andreas Fenkart <andreas.fenkart@digitalstrom.com>
This commit is contained in:
Andreas Fenkart 2016-04-05 23:13:42 +02:00 committed by Tom Rini
parent 367d789d87
commit 81974f4479
3 changed files with 48 additions and 55 deletions

50
tools/env/fw_env.c vendored
View File

@ -35,10 +35,6 @@
#include "fw_env.h" #include "fw_env.h"
struct common_args common_args;
struct printenv_args printenv_args;
struct setenv_args setenv_args;
#define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d)) #define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
#define min(x, y) ({ \ #define min(x, y) ({ \
@ -120,7 +116,7 @@ static unsigned char obsolete_flag = 0;
static int flash_io (int mode); static int flash_io (int mode);
static char *envmatch (char * s1, char * s2); static char *envmatch (char * s1, char * s2);
static int parse_config (void); static int parse_config(struct env_opts *opts);
#if defined(CONFIG_FILE) #if defined(CONFIG_FILE)
static int get_config (char *); static int get_config (char *);
@ -228,12 +224,12 @@ int parse_aes_key(char *key, uint8_t *bin_key)
* Print the current definition of one, or more, or all * Print the current definition of one, or more, or all
* environment variables * environment variables
*/ */
int fw_printenv(int argc, char *argv[], int value_only) int fw_printenv(int argc, char *argv[], int value_only, struct env_opts *opts)
{ {
char *env, *nxt; char *env, *nxt;
int i, rc = 0; int i, rc = 0;
if (fw_env_open()) if (fw_env_open(opts))
return -1; return -1;
if (argc == 0) { /* Print all env variables */ if (argc == 0) { /* Print all env variables */
@ -289,12 +285,13 @@ int fw_printenv(int argc, char *argv[], int value_only)
return rc; return rc;
} }
int fw_env_close(void) int fw_env_close(struct env_opts *opts)
{ {
int ret; int ret;
if (common_args.aes_flag) {
if (opts->aes_flag) {
ret = env_aes_cbc_crypt(environment.data, 1, ret = env_aes_cbc_crypt(environment.data, 1,
common_args.aes_key); opts->aes_key);
if (ret) { if (ret) {
fprintf(stderr, fprintf(stderr,
"Error: can't encrypt env for flash\n"); "Error: can't encrypt env for flash\n");
@ -447,7 +444,7 @@ int fw_env_write(char *name, char *value)
* modified or deleted * modified or deleted
* *
*/ */
int fw_setenv(int argc, char *argv[]) int fw_setenv(int argc, char *argv[], struct env_opts *opts)
{ {
int i; int i;
size_t len; size_t len;
@ -461,7 +458,7 @@ int fw_setenv(int argc, char *argv[])
return -1; return -1;
} }
if (fw_env_open()) { if (fw_env_open(opts)) {
fprintf(stderr, "Error: environment not initialized\n"); fprintf(stderr, "Error: environment not initialized\n");
return -1; return -1;
} }
@ -497,7 +494,7 @@ int fw_setenv(int argc, char *argv[])
free(value); free(value);
return fw_env_close(); return fw_env_close(opts);
} }
/* /*
@ -517,7 +514,7 @@ int fw_setenv(int argc, char *argv[])
* 0 - OK * 0 - OK
* -1 - Error * -1 - Error
*/ */
int fw_parse_script(char *fname) int fw_parse_script(char *fname, struct env_opts *opts)
{ {
FILE *fp; FILE *fp;
char dump[1024]; /* Maximum line length in the file */ char dump[1024]; /* Maximum line length in the file */
@ -527,7 +524,7 @@ int fw_parse_script(char *fname)
int len; int len;
int ret = 0; int ret = 0;
if (fw_env_open()) { if (fw_env_open(opts)) {
fprintf(stderr, "Error: environment not initialized\n"); fprintf(stderr, "Error: environment not initialized\n");
return -1; return -1;
} }
@ -615,10 +612,9 @@ int fw_parse_script(char *fname)
if (strcmp(fname, "-") != 0) if (strcmp(fname, "-") != 0)
fclose(fp); fclose(fp);
ret |= fw_env_close(); ret |= fw_env_close(opts);
return ret; return ret;
} }
/* /*
@ -1128,7 +1124,7 @@ static char *envmatch (char * s1, char * s2)
/* /*
* Prevent confusion if running from erased flash memory * Prevent confusion if running from erased flash memory
*/ */
int fw_env_open(void) int fw_env_open(struct env_opts *opts)
{ {
int crc0, crc0_ok; int crc0, crc0_ok;
unsigned char flag0; unsigned char flag0;
@ -1143,7 +1139,7 @@ int fw_env_open(void)
struct env_image_single *single; struct env_image_single *single;
struct env_image_redundant *redundant; struct env_image_redundant *redundant;
if (parse_config ()) /* should fill envdevices */ if (parse_config(opts)) /* should fill envdevices */
return -1; return -1;
addr0 = calloc(1, CUR_ENVSIZE); addr0 = calloc(1, CUR_ENVSIZE);
@ -1175,9 +1171,9 @@ int fw_env_open(void)
crc0 = crc32 (0, (uint8_t *) environment.data, ENV_SIZE); crc0 = crc32 (0, (uint8_t *) environment.data, ENV_SIZE);
if (common_args.aes_flag) { if (opts->aes_flag) {
ret = env_aes_cbc_crypt(environment.data, 0, ret = env_aes_cbc_crypt(environment.data, 0,
common_args.aes_key); opts->aes_key);
if (ret) if (ret)
return ret; return ret;
} }
@ -1233,9 +1229,9 @@ int fw_env_open(void)
crc1 = crc32 (0, (uint8_t *) redundant->data, ENV_SIZE); crc1 = crc32 (0, (uint8_t *) redundant->data, ENV_SIZE);
if (common_args.aes_flag) { if (opts->aes_flag) {
ret = env_aes_cbc_crypt(redundant->data, 0, ret = env_aes_cbc_crypt(redundant->data, 0,
common_args.aes_key); opts->aes_key);
if (ret) if (ret)
return ret; return ret;
} }
@ -1312,7 +1308,7 @@ int fw_env_open(void)
} }
static int parse_config () static int parse_config(struct env_opts *opts)
{ {
struct stat st; struct stat st;
@ -1321,9 +1317,9 @@ static int parse_config ()
common_args.config_file = CONFIG_FILE; common_args.config_file = CONFIG_FILE;
/* Fills in DEVNAME(), ENVSIZE(), DEVESIZE(). Or don't. */ /* Fills in DEVNAME(), ENVSIZE(), DEVESIZE(). Or don't. */
if (get_config(common_args.config_file)) { if (get_config(opts->config_file)) {
fprintf(stderr, "Cannot parse config file '%s': %m\n", fprintf(stderr, "Cannot parse config file '%s': %m\n",
common_args.config_file); opts->config_file);
return -1; return -1;
} }
#else #else
@ -1383,7 +1379,7 @@ static int parse_config ()
if (HaveRedundEnv) if (HaveRedundEnv)
usable_envsize -= sizeof(char); usable_envsize -= sizeof(char);
if (common_args.aes_flag) if (opts->aes_flag)
usable_envsize &= ~(AES_KEY_LENGTH - 1); usable_envsize &= ~(AES_KEY_LENGTH - 1);
return 0; return 0;

25
tools/env/fw_env.h vendored
View File

@ -57,33 +57,22 @@
"bootm" "bootm"
#endif #endif
struct common_args { struct env_opts {
#ifdef CONFIG_FILE #ifdef CONFIG_FILE
char *config_file; char *config_file;
#endif #endif
uint8_t aes_key[AES_KEY_LENGTH];
int aes_flag; /* Is AES encryption used? */ int aes_flag; /* Is AES encryption used? */
uint8_t aes_key[AES_KEY_LENGTH];
}; };
extern struct common_args common_args;
struct printenv_args {
int value_only;
};
extern struct printenv_args printenv_args;
struct setenv_args {
char *script_file;
};
extern struct setenv_args setenv_args;
int parse_aes_key(char *key, uint8_t *bin_key); int parse_aes_key(char *key, uint8_t *bin_key);
int fw_printenv(int argc, char *argv[], int value_only); int fw_printenv(int argc, char *argv[], int value_only, struct env_opts *opts);
char *fw_getenv(char *name); char *fw_getenv(char *name);
int fw_setenv(int argc, char *argv[]); int fw_setenv(int argc, char *argv[], struct env_opts *opts);
int fw_parse_script(char *fname); int fw_parse_script(char *fname, struct env_opts *opts);
int fw_env_open(void); int fw_env_open(struct env_opts *opts);
int fw_env_write(char *name, char *value); int fw_env_write(char *name, char *value);
int fw_env_close(void); int fw_env_close(struct env_opts *opts);
unsigned long crc32(unsigned long, const unsigned char *, unsigned); unsigned long crc32(unsigned long, const unsigned char *, unsigned);

View File

@ -49,6 +49,14 @@ static struct option long_options[] = {
{NULL, 0, NULL, 0} {NULL, 0, NULL, 0}
}; };
static struct env_opts env_opts;
/* setenv options */
static int noheader;
/* getenv options */
static char *script_file;
void usage_printenv(void) void usage_printenv(void)
{ {
@ -108,22 +116,22 @@ static void parse_common_args(int argc, char *argv[])
int c; int c;
#ifdef CONFIG_FILE #ifdef CONFIG_FILE
common_args.config_file = CONFIG_FILE; env_opts.config_file = CONFIG_FILE;
#endif #endif
while ((c = getopt_long(argc, argv, ":a:c:h", long_options, NULL)) != while ((c = getopt_long(argc, argv, ":a:c:h", long_options, NULL)) !=
EOF) { EOF) {
switch (c) { switch (c) {
case 'a': case 'a':
if (parse_aes_key(optarg, common_args.aes_key)) { if (parse_aes_key(optarg, env_opts.aes_key)) {
fprintf(stderr, "AES key parse error\n"); fprintf(stderr, "AES key parse error\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
common_args.aes_flag = 1; env_opts.aes_flag = 1;
break; break;
#ifdef CONFIG_FILE #ifdef CONFIG_FILE
case 'c': case 'c':
common_args.config_file = optarg; env_opts.config_file = optarg;
break; break;
#endif #endif
case 'h': case 'h':
@ -151,7 +159,7 @@ int parse_printenv_args(int argc, char *argv[])
EOF) { EOF) {
switch (c) { switch (c) {
case 'n': case 'n':
printenv_args.value_only = 1; noheader = 1;
break; break;
case 'a': case 'a':
case 'c': case 'c':
@ -177,7 +185,7 @@ int parse_setenv_args(int argc, char *argv[])
EOF) { EOF) {
switch (c) { switch (c) {
case 's': case 's':
setenv_args.script_file = optarg; script_file = optarg;
break; break;
case 'a': case 'a':
case 'c': case 'c':
@ -240,14 +248,14 @@ int main(int argc, char *argv[])
} }
if (do_printenv) { if (do_printenv) {
if (fw_printenv(argc, argv, printenv_args.value_only)) if (fw_printenv(argc, argv, noheader, &env_opts) != 0)
retval = EXIT_FAILURE; retval = EXIT_FAILURE;
} else { } else {
if (!setenv_args.script_file) { if (!script_file) {
if (fw_setenv(argc, argv) != 0) if (fw_setenv(argc, argv, &env_opts) != 0)
retval = EXIT_FAILURE; retval = EXIT_FAILURE;
} else { } else {
if (fw_parse_script(setenv_args.script_file) != 0) if (fw_parse_script(script_file, &env_opts) != 0)
retval = EXIT_FAILURE; retval = EXIT_FAILURE;
} }
} }