wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1 | /* |
Grant Erickson | 73a4e5e | 2008-05-06 20:16:15 -0700 | [diff] [blame] | 2 | * (C) Copyright 2000-2008 |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
Wolfgang Denk | bd8ec7e | 2013-10-07 13:07:26 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | /* |
wdenk | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 9 | * Command line user interface to firmware (=U-Boot) environment. |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 10 | * |
| 11 | * Implements: |
Marek Vasut | 05ed2a5 | 2014-03-05 19:59:52 +0100 | [diff] [blame] | 12 | * fw_printenv [ -a key ] [[ -n name ] | [ name ... ]] |
Grant Erickson | 73a4e5e | 2008-05-06 20:16:15 -0700 | [diff] [blame] | 13 | * - prints the value of a single environment variable |
| 14 | * "name", the ``name=value'' pairs of one or more |
| 15 | * environment variables "name", or the whole |
| 16 | * environment if no names are specified. |
Marek Vasut | 05ed2a5 | 2014-03-05 19:59:52 +0100 | [diff] [blame] | 17 | * fw_setenv [ -a key ] name [ value ... ] |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 18 | * - If a name without any values is given, the variable |
| 19 | * with this name is deleted from the environment; |
| 20 | * otherwise, all "value" arguments are concatenated, |
Grant Erickson | 73a4e5e | 2008-05-06 20:16:15 -0700 | [diff] [blame] | 21 | * separated by single blank characters, and the |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 22 | * resulting string is assigned to the environment |
| 23 | * variable "name" |
Marek Vasut | 05ed2a5 | 2014-03-05 19:59:52 +0100 | [diff] [blame] | 24 | * |
| 25 | * If '-a key' is specified, the env block is encrypted with AES 128 CBC. |
| 26 | * The 'key' argument is in the format of 32 hexadecimal numbers (16 bytes |
| 27 | * of AES key), eg. '-a aabbccddeeff00112233445566778899'. |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 28 | */ |
| 29 | |
Joe Hershberger | 3f1f175 | 2012-10-03 09:38:49 +0000 | [diff] [blame] | 30 | #include <fcntl.h> |
| 31 | #include <getopt.h> |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 32 | #include <stdio.h> |
| 33 | #include <string.h> |
| 34 | #include <stdlib.h> |
Joe Hershberger | 3f1f175 | 2012-10-03 09:38:49 +0000 | [diff] [blame] | 35 | #include <sys/file.h> |
| 36 | #include <unistd.h> |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 37 | #include "fw_env.h" |
| 38 | |
Andreas Fenkart | 9b5a28b | 2015-12-09 13:13:27 +0100 | [diff] [blame] | 39 | #define CMD_PRINTENV "fw_printenv" |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 40 | #define CMD_SETENV "fw_setenv" |
Andreas Fenkart | 9b5a28b | 2015-12-09 13:13:27 +0100 | [diff] [blame] | 41 | static int do_printenv; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 42 | |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 43 | static struct option long_options[] = { |
Andreas Fenkart | 9b5a28b | 2015-12-09 13:13:27 +0100 | [diff] [blame] | 44 | {"aes", required_argument, NULL, 'a'}, |
| 45 | {"config", required_argument, NULL, 'c'}, |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 46 | {"help", no_argument, NULL, 'h'}, |
Andreas Fenkart | 9b5a28b | 2015-12-09 13:13:27 +0100 | [diff] [blame] | 47 | {"script", required_argument, NULL, 's'}, |
| 48 | {"noheader", required_argument, NULL, 'n'}, |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 49 | {NULL, 0, NULL, 0} |
| 50 | }; |
| 51 | |
Andreas Fenkart | 2437190 | 2016-04-05 23:13:42 +0200 | [diff] [blame] | 52 | static struct env_opts env_opts; |
| 53 | |
| 54 | /* setenv options */ |
| 55 | static int noheader; |
| 56 | |
| 57 | /* getenv options */ |
| 58 | static char *script_file; |
| 59 | |
Andreas Fenkart | 9b5a28b | 2015-12-09 13:13:27 +0100 | [diff] [blame] | 60 | void usage_printenv(void) |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 61 | { |
| 62 | |
Andreas Fenkart | 9b5a28b | 2015-12-09 13:13:27 +0100 | [diff] [blame] | 63 | fprintf(stderr, |
| 64 | "Usage: fw_printenv [OPTIONS]... [VARIABLE]...\n" |
| 65 | "Print variables from U-Boot environment\n" |
| 66 | "\n" |
| 67 | " -h, --help print this help.\n" |
| 68 | #ifdef CONFIG_ENV_AES |
| 69 | " -a, --aes aes key to access environment\n" |
Michael Heimpold | 211e17b | 2015-10-27 22:34:49 +0100 | [diff] [blame] | 70 | #endif |
Andreas Fenkart | 9b5a28b | 2015-12-09 13:13:27 +0100 | [diff] [blame] | 71 | #ifdef CONFIG_FILE |
| 72 | " -c, --config configuration file, default:" CONFIG_FILE "\n" |
| 73 | #endif |
| 74 | " -n, --noheader do not repeat variable name in output\n" |
| 75 | "\n"); |
| 76 | } |
| 77 | |
| 78 | void usage_setenv(void) |
| 79 | { |
| 80 | fprintf(stderr, |
| 81 | "Usage: fw_setenv [OPTIONS]... [VARIABLE]...\n" |
| 82 | "Modify variables in U-Boot environment\n" |
| 83 | "\n" |
| 84 | " -h, --help print this help.\n" |
| 85 | #ifdef CONFIG_ENV_AES |
| 86 | " -a, --aes aes key to access environment\n" |
| 87 | #endif |
| 88 | #ifdef CONFIG_FILE |
| 89 | " -c, --config configuration file, default:" CONFIG_FILE "\n" |
| 90 | #endif |
| 91 | " -s, --script batch mode to minimize writes\n" |
| 92 | "\n" |
| 93 | "Examples:\n" |
| 94 | " fw_setenv foo bar set variable foo equal bar\n" |
| 95 | " fw_setenv foo clear variable foo\n" |
| 96 | " fw_setenv --script file run batch script\n" |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 97 | "\n" |
Andreas Fenkart | 9b5a28b | 2015-12-09 13:13:27 +0100 | [diff] [blame] | 98 | "Script Syntax:\n" |
| 99 | " key [space] value\n" |
Vagrant Cascadian | 3464566 | 2016-03-15 12:11:12 -0700 | [diff] [blame] | 100 | " lines starting with '#' are treated as comment\n" |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 101 | "\n" |
Andreas Fenkart | 9b5a28b | 2015-12-09 13:13:27 +0100 | [diff] [blame] | 102 | " A variable without value will be deleted. Any number of spaces are\n" |
| 103 | " allowed between key and value. Space inside of the value is treated\n" |
| 104 | " as part of the value itself.\n" |
| 105 | "\n" |
| 106 | "Script Example:\n" |
| 107 | " netdev eth0\n" |
| 108 | " kernel_addr 400000\n" |
| 109 | " foo empty empty empty empty empty empty\n" |
| 110 | " bar\n" |
| 111 | "\n"); |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 112 | } |
| 113 | |
Andreas Fenkart | 6726fc4 | 2015-12-09 13:13:26 +0100 | [diff] [blame] | 114 | static void parse_common_args(int argc, char *argv[]) |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 115 | { |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 116 | int c; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 117 | |
Andreas Fenkart | aa01f8d | 2015-12-09 13:13:24 +0100 | [diff] [blame] | 118 | #ifdef CONFIG_FILE |
Andreas Fenkart | 2437190 | 2016-04-05 23:13:42 +0200 | [diff] [blame] | 119 | env_opts.config_file = CONFIG_FILE; |
Andreas Fenkart | aa01f8d | 2015-12-09 13:13:24 +0100 | [diff] [blame] | 120 | #endif |
| 121 | |
Andreas Fenkart | 6726fc4 | 2015-12-09 13:13:26 +0100 | [diff] [blame] | 122 | while ((c = getopt_long(argc, argv, ":a:c:h", long_options, NULL)) != |
| 123 | EOF) { |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 124 | switch (c) { |
Marek Vasut | 05ed2a5 | 2014-03-05 19:59:52 +0100 | [diff] [blame] | 125 | case 'a': |
Andreas Fenkart | 2437190 | 2016-04-05 23:13:42 +0200 | [diff] [blame] | 126 | if (parse_aes_key(optarg, env_opts.aes_key)) { |
Andreas Fenkart | aa01f8d | 2015-12-09 13:13:24 +0100 | [diff] [blame] | 127 | fprintf(stderr, "AES key parse error\n"); |
Andreas Fenkart | 6726fc4 | 2015-12-09 13:13:26 +0100 | [diff] [blame] | 128 | exit(EXIT_FAILURE); |
Andreas Fenkart | aa01f8d | 2015-12-09 13:13:24 +0100 | [diff] [blame] | 129 | } |
Andreas Fenkart | 2437190 | 2016-04-05 23:13:42 +0200 | [diff] [blame] | 130 | env_opts.aes_flag = 1; |
Marek Vasut | 05ed2a5 | 2014-03-05 19:59:52 +0100 | [diff] [blame] | 131 | break; |
Andreas Fenkart | aa01f8d | 2015-12-09 13:13:24 +0100 | [diff] [blame] | 132 | #ifdef CONFIG_FILE |
Michael Heimpold | 211e17b | 2015-10-27 22:34:49 +0100 | [diff] [blame] | 133 | case 'c': |
Andreas Fenkart | 2437190 | 2016-04-05 23:13:42 +0200 | [diff] [blame] | 134 | env_opts.config_file = optarg; |
Michael Heimpold | 211e17b | 2015-10-27 22:34:49 +0100 | [diff] [blame] | 135 | break; |
Andreas Fenkart | aa01f8d | 2015-12-09 13:13:24 +0100 | [diff] [blame] | 136 | #endif |
Andreas Fenkart | 6726fc4 | 2015-12-09 13:13:26 +0100 | [diff] [blame] | 137 | case 'h': |
Andreas Fenkart | 9b5a28b | 2015-12-09 13:13:27 +0100 | [diff] [blame] | 138 | do_printenv ? usage_printenv() : usage_setenv(); |
Andreas Fenkart | 6726fc4 | 2015-12-09 13:13:26 +0100 | [diff] [blame] | 139 | exit(EXIT_SUCCESS); |
| 140 | break; |
| 141 | default: |
| 142 | /* ignore unknown options */ |
| 143 | break; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | /* Reset getopt for the next pass. */ |
| 148 | opterr = 1; |
| 149 | optind = 1; |
| 150 | } |
| 151 | |
| 152 | int parse_printenv_args(int argc, char *argv[]) |
| 153 | { |
| 154 | int c; |
| 155 | |
| 156 | parse_common_args(argc, argv); |
| 157 | |
| 158 | while ((c = getopt_long(argc, argv, "a:c:ns:h", long_options, NULL)) != |
| 159 | EOF) { |
| 160 | switch (c) { |
Daniel Hobi | 6c55426 | 2010-09-15 19:46:26 +0200 | [diff] [blame] | 161 | case 'n': |
Andreas Fenkart | 2437190 | 2016-04-05 23:13:42 +0200 | [diff] [blame] | 162 | noheader = 1; |
Daniel Hobi | 6c55426 | 2010-09-15 19:46:26 +0200 | [diff] [blame] | 163 | break; |
Andreas Fenkart | 6726fc4 | 2015-12-09 13:13:26 +0100 | [diff] [blame] | 164 | case 'a': |
| 165 | case 'c': |
Andreas Fenkart | 33e9177 | 2015-12-09 13:13:23 +0100 | [diff] [blame] | 166 | case 'h': |
Andreas Fenkart | 6726fc4 | 2015-12-09 13:13:26 +0100 | [diff] [blame] | 167 | /* ignore common options */ |
Andreas Fenkart | 33e9177 | 2015-12-09 13:13:23 +0100 | [diff] [blame] | 168 | break; |
| 169 | default: /* '?' */ |
Andreas Fenkart | 9b5a28b | 2015-12-09 13:13:27 +0100 | [diff] [blame] | 170 | usage_printenv(); |
Andreas Fenkart | 33e9177 | 2015-12-09 13:13:23 +0100 | [diff] [blame] | 171 | exit(EXIT_FAILURE); |
| 172 | break; |
| 173 | } |
| 174 | } |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | int parse_setenv_args(int argc, char *argv[]) |
| 179 | { |
| 180 | int c; |
| 181 | |
Andreas Fenkart | 6726fc4 | 2015-12-09 13:13:26 +0100 | [diff] [blame] | 182 | parse_common_args(argc, argv); |
Andreas Fenkart | aa01f8d | 2015-12-09 13:13:24 +0100 | [diff] [blame] | 183 | |
Andreas Fenkart | 6726fc4 | 2015-12-09 13:13:26 +0100 | [diff] [blame] | 184 | while ((c = getopt_long(argc, argv, "a:c:ns:h", long_options, NULL)) != |
| 185 | EOF) { |
Andreas Fenkart | 33e9177 | 2015-12-09 13:13:23 +0100 | [diff] [blame] | 186 | switch (c) { |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 187 | case 's': |
Andreas Fenkart | 2437190 | 2016-04-05 23:13:42 +0200 | [diff] [blame] | 188 | script_file = optarg; |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 189 | break; |
Andreas Fenkart | 6726fc4 | 2015-12-09 13:13:26 +0100 | [diff] [blame] | 190 | case 'a': |
| 191 | case 'c': |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 192 | case 'h': |
Andreas Fenkart | 6726fc4 | 2015-12-09 13:13:26 +0100 | [diff] [blame] | 193 | /* ignore common options */ |
Andreas Fenkart | 33e9177 | 2015-12-09 13:13:23 +0100 | [diff] [blame] | 194 | break; |
Daniel Hobi | 6a20a32 | 2010-09-16 14:36:09 +0200 | [diff] [blame] | 195 | default: /* '?' */ |
Andreas Fenkart | 9b5a28b | 2015-12-09 13:13:27 +0100 | [diff] [blame] | 196 | usage_setenv(); |
Andreas Fenkart | 33e9177 | 2015-12-09 13:13:23 +0100 | [diff] [blame] | 197 | exit(EXIT_FAILURE); |
| 198 | break; |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 199 | } |
| 200 | } |
Andreas Fenkart | 33e9177 | 2015-12-09 13:13:23 +0100 | [diff] [blame] | 201 | return 0; |
| 202 | } |
| 203 | |
| 204 | int main(int argc, char *argv[]) |
| 205 | { |
Andreas Fenkart | 33e9177 | 2015-12-09 13:13:23 +0100 | [diff] [blame] | 206 | const char *lockname = "/var/lock/" CMD_PRINTENV ".lock"; |
| 207 | int lockfd = -1; |
| 208 | int retval = EXIT_SUCCESS; |
Andreas Fenkart | 9b5a28b | 2015-12-09 13:13:27 +0100 | [diff] [blame] | 209 | char *_cmdname; |
Andreas Fenkart | 33e9177 | 2015-12-09 13:13:23 +0100 | [diff] [blame] | 210 | |
Andreas Fenkart | 9b5a28b | 2015-12-09 13:13:27 +0100 | [diff] [blame] | 211 | _cmdname = *argv; |
| 212 | if (strrchr(_cmdname, '/') != NULL) |
| 213 | _cmdname = strrchr(_cmdname, '/') + 1; |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 214 | |
Andreas Fenkart | 9b5a28b | 2015-12-09 13:13:27 +0100 | [diff] [blame] | 215 | if (strcmp(_cmdname, CMD_PRINTENV) == 0) { |
| 216 | do_printenv = 1; |
| 217 | } else if (strcmp(_cmdname, CMD_SETENV) == 0) { |
| 218 | do_printenv = 0; |
Andreas Fenkart | 33e9177 | 2015-12-09 13:13:23 +0100 | [diff] [blame] | 219 | } else { |
| 220 | fprintf(stderr, |
| 221 | "Identity crisis - may be called as `%s' or as `%s' but not as `%s'\n", |
Andreas Fenkart | 9b5a28b | 2015-12-09 13:13:27 +0100 | [diff] [blame] | 222 | CMD_PRINTENV, CMD_SETENV, _cmdname); |
Andreas Fenkart | 33e9177 | 2015-12-09 13:13:23 +0100 | [diff] [blame] | 223 | exit(EXIT_FAILURE); |
| 224 | } |
| 225 | |
Andreas Fenkart | 9b5a28b | 2015-12-09 13:13:27 +0100 | [diff] [blame] | 226 | if (do_printenv) { |
| 227 | if (parse_printenv_args(argc, argv)) |
| 228 | exit(EXIT_FAILURE); |
| 229 | } else { |
| 230 | if (parse_setenv_args(argc, argv)) |
| 231 | exit(EXIT_FAILURE); |
| 232 | } |
| 233 | |
Andreas Fenkart | 14908ef | 2015-12-09 13:13:25 +0100 | [diff] [blame] | 234 | /* shift parsed flags, jump to non-option arguments */ |
| 235 | argc -= optind; |
| 236 | argv += optind; |
| 237 | |
Andreas Fenkart | 33e9177 | 2015-12-09 13:13:23 +0100 | [diff] [blame] | 238 | lockfd = open(lockname, O_WRONLY | O_CREAT | O_TRUNC, 0666); |
| 239 | if (-1 == lockfd) { |
| 240 | fprintf(stderr, "Error opening lock file %s\n", lockname); |
| 241 | return EXIT_FAILURE; |
| 242 | } |
| 243 | |
| 244 | if (-1 == flock(lockfd, LOCK_EX)) { |
| 245 | fprintf(stderr, "Error locking file %s\n", lockname); |
| 246 | close(lockfd); |
| 247 | return EXIT_FAILURE; |
| 248 | } |
| 249 | |
Andreas Fenkart | 9b5a28b | 2015-12-09 13:13:27 +0100 | [diff] [blame] | 250 | if (do_printenv) { |
Andreas Fenkart | 2437190 | 2016-04-05 23:13:42 +0200 | [diff] [blame] | 251 | if (fw_printenv(argc, argv, noheader, &env_opts) != 0) |
Joe Hershberger | 3f1f175 | 2012-10-03 09:38:49 +0000 | [diff] [blame] | 252 | retval = EXIT_FAILURE; |
Andreas Fenkart | 9b5a28b | 2015-12-09 13:13:27 +0100 | [diff] [blame] | 253 | } else { |
Andreas Fenkart | 2437190 | 2016-04-05 23:13:42 +0200 | [diff] [blame] | 254 | if (!script_file) { |
| 255 | if (fw_setenv(argc, argv, &env_opts) != 0) |
Joe Hershberger | 3f1f175 | 2012-10-03 09:38:49 +0000 | [diff] [blame] | 256 | retval = EXIT_FAILURE; |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 257 | } else { |
Andreas Fenkart | 2437190 | 2016-04-05 23:13:42 +0200 | [diff] [blame] | 258 | if (fw_parse_script(script_file, &env_opts) != 0) |
Joe Hershberger | 3f1f175 | 2012-10-03 09:38:49 +0000 | [diff] [blame] | 259 | retval = EXIT_FAILURE; |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 260 | } |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Joe Hershberger | 3f1f175 | 2012-10-03 09:38:49 +0000 | [diff] [blame] | 263 | flock(lockfd, LOCK_UN); |
| 264 | close(lockfd); |
| 265 | return retval; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 266 | } |