Commit 371ee13760b9e70d1a931afc643de3f4057b4a28

Authored by Andreas Fenkart
Committed by Tom Rini
1 parent 07ce944021

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>

Showing 3 changed files with 53 additions and 57 deletions Side-by-side Diff

... ... @@ -34,8 +34,6 @@
34 34  
35 35 #include "fw_env.h"
36 36  
37   -#include <aes.h>
38   -
39 37 #define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
40 38  
41 39 #define WHITESPACE(c) ((c == '\t') || (c == ' '))
... ... @@ -105,9 +103,6 @@
105 103 .flag_scheme = FLAG_NONE,
106 104 };
107 105  
108   -/* Is AES encryption used? */
109   -static int aes_flag;
110   -static uint8_t aes_key[AES_KEY_LENGTH] = { 0 };
111 106 static int env_aes_cbc_crypt(char *data, const int enc);
112 107  
113 108 static int HaveRedundEnv = 0;
... ... @@ -125,7 +120,6 @@
125 120  
126 121 #if defined(CONFIG_FILE)
127 122 static int get_config (char *);
128   -static char *config_file = CONFIG_FILE;
129 123 #endif
130 124 static inline ulong getenvsize (void)
131 125 {
... ... @@ -134,7 +128,7 @@
134 128 if (HaveRedundEnv)
135 129 rc -= sizeof (char);
136 130  
137   - if (aes_flag)
  131 + if (common_args.aes_flag)
138 132 rc &= ~(AES_KEY_LENGTH - 1);
139 133  
140 134 return rc;
... ... @@ -208,7 +202,7 @@
208 202 return NULL;
209 203 }
210 204  
211   -static int parse_aes_key(char *key, uint8_t *bin_key)
  205 +int parse_aes_key(char *key, uint8_t *bin_key)
212 206 {
213 207 char tmp[5] = { '0', 'x', 0, 0, 0 };
214 208 unsigned long ul;
215 209  
216 210  
... ... @@ -243,32 +237,16 @@
243 237 int fw_printenv (int argc, char *argv[])
244 238 {
245 239 char *env, *nxt;
246   - int i, n_flag;
247   - int rc = 0;
  240 + int i, rc = 0;
248 241  
249 242 #ifdef CONFIG_FILE
250 243 if (argc >= 2 && strcmp(argv[1], "-c") == 0) {
251   - if (argc < 3) {
252   - fprintf(stderr,
253   - "## Error: '-c' option requires the config file to use\n");
254   - return -1;
255   - }
256   - config_file = argv[2];
257 244 argv += 2;
258 245 argc -= 2;
259 246 }
260 247 #endif
261 248  
262 249 if (argc >= 2 && strcmp(argv[1], "-a") == 0) {
263   - if (argc < 3) {
264   - fprintf(stderr,
265   - "## Error: '-a' option requires AES key\n");
266   - return -1;
267   - }
268   - rc = parse_aes_key(argv[2], aes_key);
269   - if (rc)
270   - return rc;
271   - aes_flag = 1;
272 250 argv += 2;
273 251 argc -= 2;
274 252 }
... ... @@ -292,7 +270,6 @@
292 270 }
293 271  
294 272 if (strcmp (argv[1], "-n") == 0) {
295   - n_flag = 1;
296 273 ++argv;
297 274 --argc;
298 275 if (argc != 2) {
... ... @@ -300,8 +277,6 @@
300 277 "`-n' option requires exactly one argument\n");
301 278 return -1;
302 279 }
303   - } else {
304   - n_flag = 0;
305 280 }
306 281  
307 282 for (i = 1; i < argc; ++i) { /* print single env variables */
... ... @@ -319,7 +294,7 @@
319 294 }
320 295 val = envmatch (name, env);
321 296 if (val) {
322   - if (!n_flag) {
  297 + if (!printenv_args.name_suppress) {
323 298 fputs (name, stdout);
324 299 putc ('=', stdout);
325 300 }
... ... @@ -339,7 +314,7 @@
339 314 int fw_env_close(void)
340 315 {
341 316 int ret;
342   - if (aes_flag) {
  317 + if (common_args.aes_flag) {
343 318 ret = env_aes_cbc_crypt(environment.data, 1);
344 319 if (ret) {
345 320 fprintf(stderr,
... ... @@ -495,7 +470,7 @@
495 470 */
496 471 int fw_setenv(int argc, char *argv[])
497 472 {
498   - int i, rc;
  473 + int i;
499 474 size_t len;
500 475 char *name, **valv;
501 476 char *value = NULL;
... ... @@ -503,12 +478,6 @@
503 478  
504 479 #ifdef CONFIG_FILE
505 480 if (argc >= 2 && strcmp(argv[1], "-c") == 0) {
506   - if (argc < 3) {
507   - fprintf(stderr,
508   - "## Error: '-c' option requires the config file to use\n");
509   - return -1;
510   - }
511   - config_file = argv[2];
512 481 argv += 2;
513 482 argc -= 2;
514 483 }
... ... @@ -520,15 +489,6 @@
520 489 }
521 490  
522 491 if (strcmp(argv[1], "-a") == 0) {
523   - if (argc < 3) {
524   - fprintf(stderr,
525   - "## Error: '-a' option requires AES key\n");
526   - return -1;
527   - }
528   - rc = parse_aes_key(argv[2], aes_key);
529   - if (rc)
530   - return rc;
531   - aes_flag = 1;
532 492 argv += 2;
533 493 argc -= 2;
534 494 }
... ... @@ -1026,7 +986,7 @@
1026 986 uint32_t aes_blocks;
1027 987  
1028 988 /* First we expand the key. */
1029   - aes_expand_key(aes_key, key_exp);
  989 + aes_expand_key(common_args.aes_key, key_exp);
1030 990  
1031 991 /* Calculate the number of AES blocks to encrypt. */
1032 992 aes_blocks = DIV_ROUND_UP(len, AES_KEY_LENGTH);
... ... @@ -1254,7 +1214,7 @@
1254 1214  
1255 1215 crc0 = crc32 (0, (uint8_t *) environment.data, ENV_SIZE);
1256 1216  
1257   - if (aes_flag) {
  1217 + if (common_args.aes_flag) {
1258 1218 ret = env_aes_cbc_crypt(environment.data, 0);
1259 1219 if (ret)
1260 1220 return ret;
... ... @@ -1311,7 +1271,7 @@
1311 1271  
1312 1272 crc1 = crc32 (0, (uint8_t *) redundant->data, ENV_SIZE);
1313 1273  
1314   - if (aes_flag) {
  1274 + if (common_args.aes_flag) {
1315 1275 ret = env_aes_cbc_crypt(redundant->data, 0);
1316 1276 if (ret)
1317 1277 return ret;
... ... @@ -1395,9 +1355,9 @@
1395 1355  
1396 1356 #if defined(CONFIG_FILE)
1397 1357 /* Fills in DEVNAME(), ENVSIZE(), DEVESIZE(). Or don't. */
1398   - if (get_config (config_file)) {
1399   - fprintf (stderr,
1400   - "Cannot parse config file '%s': %s\n", config_file, strerror (errno));
  1358 + if (get_config(common_args.config_file)) {
  1359 + fprintf(stderr, "Cannot parse config file '%s': %m\n",
  1360 + common_args.config_file);
1401 1361 return -1;
1402 1362 }
1403 1363 #else
... ... @@ -5,6 +5,9 @@
5 5 * SPDX-License-Identifier: GPL-2.0+
6 6 */
7 7  
  8 +#include <aes.h>
  9 +#include <stdint.h>
  10 +
8 11 /* Pull in the current config to define the default environment */
9 12 #include <linux/kconfig.h>
10 13  
11 14  
... ... @@ -54,7 +57,17 @@
54 57 "bootm"
55 58 #endif
56 59  
  60 +struct common_args {
  61 +#ifdef CONFIG_FILE
  62 + char *config_file;
  63 +#endif
  64 + uint8_t aes_key[AES_KEY_LENGTH];
  65 + int aes_flag; /* Is AES encryption used? */
  66 +};
  67 +extern struct common_args common_args;
  68 +
57 69 struct printenv_args {
  70 + int name_suppress;
58 71 };
59 72 extern struct printenv_args printenv_args;
60 73  
... ... @@ -62,6 +75,8 @@
62 75 char *script_file;
63 76 };
64 77 extern struct setenv_args setenv_args;
  78 +
  79 +int parse_aes_key(char *key, uint8_t *bin_key);
65 80  
66 81 extern int fw_printenv(int argc, char *argv[]);
67 82 extern char *fw_getenv (char *name);
tools/env/fw_env_main.c
... ... @@ -45,6 +45,7 @@
45 45 {NULL, 0, NULL, 0}
46 46 };
47 47  
  48 +struct common_args common_args;
48 49 struct printenv_args printenv_args;
49 50 struct setenv_args setenv_args;
50 51  
51 52  
52 53  
53 54  
54 55  
55 56  
... ... @@ -84,17 +85,27 @@
84 85 {
85 86 int c;
86 87  
  88 +#ifdef CONFIG_FILE
  89 + common_args.config_file = CONFIG_FILE;
  90 +#endif
  91 +
87 92 while ((c = getopt_long (argc, argv, "a:c:ns:h",
88 93 long_options, NULL)) != EOF) {
89 94 switch (c) {
90 95 case 'a':
91   - /* AES key, handled later */
  96 + if (parse_aes_key(optarg, common_args.aes_key)) {
  97 + fprintf(stderr, "AES key parse error\n");
  98 + return EXIT_FAILURE;
  99 + }
  100 + common_args.aes_flag = 1;
92 101 break;
  102 +#ifdef CONFIG_FILE
93 103 case 'c':
94   - /* handled later */
  104 + common_args.config_file = optarg;
95 105 break;
  106 +#endif
96 107 case 'n':
97   - /* handled in fw_printenv */
  108 + printenv_args.name_suppress = 1;
98 109 break;
99 110 case 'h':
100 111 usage();
101 112  
102 113  
103 114  
104 115  
... ... @@ -113,15 +124,25 @@
113 124 {
114 125 int c;
115 126  
  127 +#ifdef CONFIG_FILE
  128 + common_args.config_file = CONFIG_FILE;
  129 +#endif
  130 +
116 131 while ((c = getopt_long (argc, argv, "a:c:ns:h",
117 132 long_options, NULL)) != EOF) {
118 133 switch (c) {
119 134 case 'a':
120   - /* AES key, handled later */
  135 + if (parse_aes_key(optarg, common_args.aes_key)) {
  136 + fprintf(stderr, "AES key parse error\n");
  137 + return EXIT_FAILURE;
  138 + }
  139 + common_args.aes_flag = 1;
121 140 break;
  141 +#ifdef CONFIG_FILE
122 142 case 'c':
123   - /* handled later */
  143 + common_args.config_file = optarg;
124 144 break;
  145 +#endif
125 146 case 's':
126 147 setenv_args.script_file = optarg;
127 148 break;