From patchwork Thu Nov 26 10:52:31 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Fenkart X-Patchwork-Id: 549010 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from theia.denx.de (theia.denx.de [85.214.87.163]) by ozlabs.org (Postfix) with ESMTP id 507361402C4 for ; Thu, 26 Nov 2015 22:02:26 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b=VM9c8SX3; dkim-atps=neutral Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 0B30E4B992; Thu, 26 Nov 2015 12:02:05 +0100 (CET) Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id RFt9zZWgxQC3; Thu, 26 Nov 2015 12:02:04 +0100 (CET) Received: from theia.denx.de (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 47FCA4B96C; Thu, 26 Nov 2015 12:01:47 +0100 (CET) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 224B94B90B for ; Thu, 26 Nov 2015 11:52:51 +0100 (CET) Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 7ZM6lA6i3zgb for ; Thu, 26 Nov 2015 11:52:51 +0100 (CET) X-policyd-weight: NOT_IN_SBL_XBL_SPAMHAUS=-1.5 NOT_IN_SPAMCOP=-1.5 NOT_IN_BL_NJABL=-1.5 (only DNSBL check requested) Received: from mail-wm0-f51.google.com (mail-wm0-f51.google.com [74.125.82.51]) by theia.denx.de (Postfix) with ESMTPS id 1546C4B90E for ; Thu, 26 Nov 2015 11:52:46 +0100 (CET) Received: by wmec201 with SMTP id c201so17215832wme.1 for ; Thu, 26 Nov 2015 02:52:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=bcHm7lYRMfbFv7BEBLnLCUGbRh8ybgCLZ6eA1fYibJ4=; b=VM9c8SX3BlAZ1zpCeKvStyV6BgfmXWXHt7vSI/8uYiSSmeJ0tv8QbwR6K/3HVlaw2s qFrlLa3akkYqxPrb2cch9OnPIDt8lR/BvdXdnuTarC4QINthscC6Ulc0DcdfFVkjdEky wSRBcScVXrJQ7wtNoiPhrRENWs9s5lTMzqfEnQkHMg1OyugkHSALzjaos/+gvcHWSuGT gt01AnMPZWULX/3ZTJDK+WVkdD+xZ0JVVm6DznbccwvvcpOS6vB0a1zKr6lb5jQAyXlm XjhRX53/pjy8rLoIz79jrRqz96OygXZu5BMNoNrsfwCcqCgiRWAGy1v+WRw09zVljA5l 7/Zg== X-Received: by 10.194.188.6 with SMTP id fw6mr54108411wjc.95.1448535166049; Thu, 26 Nov 2015 02:52:46 -0800 (PST) Received: from localhost (ip-89-176-167-254.net.upcbroadband.cz. [89.176.167.254]) by smtp.gmail.com with ESMTPSA id m64sm2117965wmf.14.2015.11.26.02.52.45 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 26 Nov 2015 02:52:45 -0800 (PST) From: Andreas Fenkart X-Google-Original-From: Andreas Fenkart To: u-boot@lists.denx.de Date: Thu, 26 Nov 2015 11:52:31 +0100 Message-Id: <1448535154-6350-5-git-send-email-andreas.fenkart@dev.digitalstrom.org> X-Mailer: git-send-email 2.6.2 In-Reply-To: <1448535154-6350-1-git-send-email-andreas.fenkart@dev.digitalstrom.org> References: <1448535154-6350-1-git-send-email-andreas.fenkart@dev.digitalstrom.org> X-Mailman-Approved-At: Thu, 26 Nov 2015 12:01:36 +0100 Subject: [U-Boot] [PATCH v2 4/7] tools: env: parse aes key / suppress flag into argument struct X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.15 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" disabled original parsing, but not yet removed since the argument indexing needs to be fixed Signed-off-by: Andreas Fenkart --- tools/env/fw_env.c | 64 ++++++++++--------------------------------------- tools/env/fw_env.h | 13 ++++++++++ tools/env/fw_env_main.c | 31 ++++++++++++++++++++---- 3 files changed, 51 insertions(+), 57 deletions(-) diff --git a/tools/env/fw_env.c b/tools/env/fw_env.c index 5b76b74..2cfff02 100644 --- a/tools/env/fw_env.c +++ b/tools/env/fw_env.c @@ -33,8 +33,6 @@ #include "fw_env.h" -#include - #define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d)) #define WHITESPACE(c) ((c == '\t') || (c == ' ')) @@ -104,9 +102,6 @@ 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; @@ -124,7 +119,6 @@ static int parse_config (void); #if defined(CONFIG_FILE) static int get_config (char *); -static char *config_file = CONFIG_FILE; #endif static inline ulong getenvsize (void) { @@ -133,7 +127,7 @@ static inline ulong getenvsize (void) if (HaveRedundEnv) rc -= sizeof (char); - if (aes_flag) + if (common_args.aes_flag) rc &= ~(AES_KEY_LENGTH - 1); return rc; @@ -207,7 +201,7 @@ char *fw_getdefenv(char *name) 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 }; unsigned long ul; @@ -242,32 +236,16 @@ static int parse_aes_key(char *key, uint8_t *bin_key) int fw_printenv (int argc, char *argv[]) { char *env, *nxt; - int i, n_flag; - int rc = 0; + int i, rc = 0; #ifdef CONFIG_FILE 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; argc -= 2; } #endif 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; argc -= 2; } @@ -291,7 +269,6 @@ int fw_printenv (int argc, char *argv[]) } if (strcmp (argv[1], "-n") == 0) { - n_flag = 1; ++argv; --argc; if (argc != 2) { @@ -299,8 +276,6 @@ int fw_printenv (int argc, char *argv[]) "`-n' option requires exactly one argument\n"); return -1; } - } else { - n_flag = 0; } for (i = 1; i < argc; ++i) { /* print single env variables */ @@ -318,7 +293,7 @@ int fw_printenv (int argc, char *argv[]) } val = envmatch (name, env); if (val) { - if (!n_flag) { + if (!printenv_args.name_suppress) { fputs (name, stdout); putc ('=', stdout); } @@ -338,7 +313,7 @@ int fw_printenv (int argc, char *argv[]) int fw_env_close(void) { int ret; - if (aes_flag) { + if (common_args.aes_flag) { ret = env_aes_cbc_crypt(environment.data, 1); if (ret) { fprintf(stderr, @@ -494,7 +469,7 @@ int fw_env_write(char *name, char *value) */ int fw_setenv(int argc, char *argv[]) { - int i, rc; + int i; size_t len; char *name, **valv; char *value = NULL; @@ -502,12 +477,6 @@ int fw_setenv(int argc, char *argv[]) #ifdef CONFIG_FILE 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; argc -= 2; } @@ -519,15 +488,6 @@ int fw_setenv(int argc, char *argv[]) } 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; argc -= 2; } @@ -1025,7 +985,7 @@ static int env_aes_cbc_crypt(char *payload, const int enc) uint32_t aes_blocks; /* 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. */ aes_blocks = DIV_ROUND_UP(len, AES_KEY_LENGTH); @@ -1253,7 +1213,7 @@ int fw_env_open(void) 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); if (ret) return ret; @@ -1310,7 +1270,7 @@ int fw_env_open(void) 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); if (ret) return ret; @@ -1394,9 +1354,9 @@ static int parse_config () #if defined(CONFIG_FILE) /* Fills in DEVNAME(), ENVSIZE(), DEVESIZE(). Or don't. */ - if (get_config (config_file)) { - fprintf (stderr, - "Cannot parse config file '%s': %s\n", config_file, strerror (errno)); + if (get_config(common_args.config_file)) { + fprintf(stderr, "Cannot parse config file '%s': %m\n", + common_args.config_file); return -1; } #else diff --git a/tools/env/fw_env.h b/tools/env/fw_env.h index 1a02c46..696eace 100644 --- a/tools/env/fw_env.h +++ b/tools/env/fw_env.h @@ -5,6 +5,9 @@ * SPDX-License-Identifier: GPL-2.0+ */ +#include +#include + /* Pull in the current config to define the default environment */ #include @@ -54,7 +57,15 @@ "bootm" #endif +struct common_args { + char *config_file; + uint8_t aes_key[AES_KEY_LENGTH]; + int aes_flag; /* Is AES encryption used? */ +}; +extern struct common_args common_args; + struct printenv_args { + int name_suppress; }; extern struct printenv_args printenv_args; @@ -63,6 +74,8 @@ struct 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 char *fw_getenv (char *name); extern int fw_setenv (int argc, char *argv[]); diff --git a/tools/env/fw_env_main.c b/tools/env/fw_env_main.c index 0c9f918..b68f1bf 100644 --- a/tools/env/fw_env_main.c +++ b/tools/env/fw_env_main.c @@ -45,6 +45,7 @@ static struct option long_options[] = { {NULL, 0, NULL, 0} }; +struct common_args common_args; struct printenv_args printenv_args; struct setenv_args setenv_args; @@ -84,17 +85,27 @@ int parse_printenv_args(int argc, char *argv[]) { int c; +#ifdef CONFIG_FILE + common_args.config_file = CONFIG_FILE; +#endif + while ((c = getopt_long (argc, argv, "a:c:ns:h", long_options, NULL)) != EOF) { switch (c) { 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; +#ifdef CONFIG_FILE case 'c': - /* handled later */ + common_args.config_file = optarg; break; +#endif case 'n': - /* handled in fw_printenv */ + printenv_args.name_suppress = 1; break; case 'h': usage(); @@ -113,15 +124,25 @@ int parse_setenv_args(int argc, char *argv[]) { int c; +#ifdef CONFIG_FILE + common_args.config_file = CONFIG_FILE; +#endif + while ((c = getopt_long (argc, argv, "a:c:ns:h", long_options, NULL)) != EOF) { switch (c) { 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; +#ifdef CONFIG_FILE case 'c': - /* handled later */ + common_args.config_file = optarg; break; +#endif case 's': setenv_args.script_file = optarg; break;