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 | aa01f8d | 2015-12-09 13:13:24 +0100 | [diff] [blame] | 52 | struct common_args common_args; |
Andreas Fenkart | 33e9177 | 2015-12-09 13:13:23 +0100 | [diff] [blame] | 53 | struct printenv_args printenv_args; |
| 54 | struct setenv_args setenv_args; |
| 55 | |
Andreas Fenkart | 9b5a28b | 2015-12-09 13:13:27 +0100 | [diff] [blame^] | 56 | void usage_printenv(void) |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 57 | { |
| 58 | |
Andreas Fenkart | 9b5a28b | 2015-12-09 13:13:27 +0100 | [diff] [blame^] | 59 | fprintf(stderr, |
| 60 | "Usage: fw_printenv [OPTIONS]... [VARIABLE]...\n" |
| 61 | "Print variables from U-Boot environment\n" |
| 62 | "\n" |
| 63 | " -h, --help print this help.\n" |
| 64 | #ifdef CONFIG_ENV_AES |
| 65 | " -a, --aes aes key to access environment\n" |
Michael Heimpold | 211e17b | 2015-10-27 22:34:49 +0100 | [diff] [blame] | 66 | #endif |
Andreas Fenkart | 9b5a28b | 2015-12-09 13:13:27 +0100 | [diff] [blame^] | 67 | #ifdef CONFIG_FILE |
| 68 | " -c, --config configuration file, default:" CONFIG_FILE "\n" |
| 69 | #endif |
| 70 | " -n, --noheader do not repeat variable name in output\n" |
| 71 | "\n"); |
| 72 | } |
| 73 | |
| 74 | void usage_setenv(void) |
| 75 | { |
| 76 | fprintf(stderr, |
| 77 | "Usage: fw_setenv [OPTIONS]... [VARIABLE]...\n" |
| 78 | "Modify variables in U-Boot environment\n" |
| 79 | "\n" |
| 80 | " -h, --help print this help.\n" |
| 81 | #ifdef CONFIG_ENV_AES |
| 82 | " -a, --aes aes key to access environment\n" |
| 83 | #endif |
| 84 | #ifdef CONFIG_FILE |
| 85 | " -c, --config configuration file, default:" CONFIG_FILE "\n" |
| 86 | #endif |
| 87 | " -s, --script batch mode to minimize writes\n" |
| 88 | "\n" |
| 89 | "Examples:\n" |
| 90 | " fw_setenv foo bar set variable foo equal bar\n" |
| 91 | " fw_setenv foo clear variable foo\n" |
| 92 | " fw_setenv --script file run batch script\n" |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 93 | "\n" |
Andreas Fenkart | 9b5a28b | 2015-12-09 13:13:27 +0100 | [diff] [blame^] | 94 | "Script Syntax:\n" |
| 95 | " key [space] value\n" |
| 96 | " lines starting with '#' are treated as commment\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 | " A variable without value will be deleted. Any number of spaces are\n" |
| 99 | " allowed between key and value. Space inside of the value is treated\n" |
| 100 | " as part of the value itself.\n" |
| 101 | "\n" |
| 102 | "Script Example:\n" |
| 103 | " netdev eth0\n" |
| 104 | " kernel_addr 400000\n" |
| 105 | " foo empty empty empty empty empty empty\n" |
| 106 | " bar\n" |
| 107 | "\n"); |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 108 | } |
| 109 | |
Andreas Fenkart | 6726fc4 | 2015-12-09 13:13:26 +0100 | [diff] [blame] | 110 | static void parse_common_args(int argc, char *argv[]) |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 111 | { |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 112 | int c; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 113 | |
Andreas Fenkart | aa01f8d | 2015-12-09 13:13:24 +0100 | [diff] [blame] | 114 | #ifdef CONFIG_FILE |
| 115 | common_args.config_file = CONFIG_FILE; |
| 116 | #endif |
| 117 | |
Andreas Fenkart | 6726fc4 | 2015-12-09 13:13:26 +0100 | [diff] [blame] | 118 | while ((c = getopt_long(argc, argv, ":a:c:h", long_options, NULL)) != |
| 119 | EOF) { |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 120 | switch (c) { |
Marek Vasut | 05ed2a5 | 2014-03-05 19:59:52 +0100 | [diff] [blame] | 121 | case 'a': |
Andreas Fenkart | aa01f8d | 2015-12-09 13:13:24 +0100 | [diff] [blame] | 122 | if (parse_aes_key(optarg, common_args.aes_key)) { |
| 123 | fprintf(stderr, "AES key parse error\n"); |
Andreas Fenkart | 6726fc4 | 2015-12-09 13:13:26 +0100 | [diff] [blame] | 124 | exit(EXIT_FAILURE); |
Andreas Fenkart | aa01f8d | 2015-12-09 13:13:24 +0100 | [diff] [blame] | 125 | } |
| 126 | common_args.aes_flag = 1; |
Marek Vasut | 05ed2a5 | 2014-03-05 19:59:52 +0100 | [diff] [blame] | 127 | break; |
Andreas Fenkart | aa01f8d | 2015-12-09 13:13:24 +0100 | [diff] [blame] | 128 | #ifdef CONFIG_FILE |
Michael Heimpold | 211e17b | 2015-10-27 22:34:49 +0100 | [diff] [blame] | 129 | case 'c': |
Andreas Fenkart | aa01f8d | 2015-12-09 13:13:24 +0100 | [diff] [blame] | 130 | common_args.config_file = optarg; |
Michael Heimpold | 211e17b | 2015-10-27 22:34:49 +0100 | [diff] [blame] | 131 | break; |
Andreas Fenkart | aa01f8d | 2015-12-09 13:13:24 +0100 | [diff] [blame] | 132 | #endif |
Andreas Fenkart | 6726fc4 | 2015-12-09 13:13:26 +0100 | [diff] [blame] | 133 | case 'h': |
Andreas Fenkart | 9b5a28b | 2015-12-09 13:13:27 +0100 | [diff] [blame^] | 134 | do_printenv ? usage_printenv() : usage_setenv(); |
Andreas Fenkart | 6726fc4 | 2015-12-09 13:13:26 +0100 | [diff] [blame] | 135 | exit(EXIT_SUCCESS); |
| 136 | break; |
| 137 | default: |
| 138 | /* ignore unknown options */ |
| 139 | break; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | /* Reset getopt for the next pass. */ |
| 144 | opterr = 1; |
| 145 | optind = 1; |
| 146 | } |
| 147 | |
| 148 | int parse_printenv_args(int argc, char *argv[]) |
| 149 | { |
| 150 | int c; |
| 151 | |
| 152 | parse_common_args(argc, argv); |
| 153 | |
| 154 | while ((c = getopt_long(argc, argv, "a:c:ns:h", long_options, NULL)) != |
| 155 | EOF) { |
| 156 | switch (c) { |
Daniel Hobi | 6c55426 | 2010-09-15 19:46:26 +0200 | [diff] [blame] | 157 | case 'n': |
Andreas Fenkart | aa01f8d | 2015-12-09 13:13:24 +0100 | [diff] [blame] | 158 | printenv_args.name_suppress = 1; |
Daniel Hobi | 6c55426 | 2010-09-15 19:46:26 +0200 | [diff] [blame] | 159 | break; |
Andreas Fenkart | 6726fc4 | 2015-12-09 13:13:26 +0100 | [diff] [blame] | 160 | case 'a': |
| 161 | case 'c': |
Andreas Fenkart | 33e9177 | 2015-12-09 13:13:23 +0100 | [diff] [blame] | 162 | case 'h': |
Andreas Fenkart | 6726fc4 | 2015-12-09 13:13:26 +0100 | [diff] [blame] | 163 | /* ignore common options */ |
Andreas Fenkart | 33e9177 | 2015-12-09 13:13:23 +0100 | [diff] [blame] | 164 | break; |
| 165 | default: /* '?' */ |
Andreas Fenkart | 9b5a28b | 2015-12-09 13:13:27 +0100 | [diff] [blame^] | 166 | usage_printenv(); |
Andreas Fenkart | 33e9177 | 2015-12-09 13:13:23 +0100 | [diff] [blame] | 167 | exit(EXIT_FAILURE); |
| 168 | break; |
| 169 | } |
| 170 | } |
| 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | int parse_setenv_args(int argc, char *argv[]) |
| 175 | { |
| 176 | int c; |
| 177 | |
Andreas Fenkart | 6726fc4 | 2015-12-09 13:13:26 +0100 | [diff] [blame] | 178 | parse_common_args(argc, argv); |
Andreas Fenkart | aa01f8d | 2015-12-09 13:13:24 +0100 | [diff] [blame] | 179 | |
Andreas Fenkart | 6726fc4 | 2015-12-09 13:13:26 +0100 | [diff] [blame] | 180 | while ((c = getopt_long(argc, argv, "a:c:ns:h", long_options, NULL)) != |
| 181 | EOF) { |
Andreas Fenkart | 33e9177 | 2015-12-09 13:13:23 +0100 | [diff] [blame] | 182 | switch (c) { |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 183 | case 's': |
Andreas Fenkart | 33e9177 | 2015-12-09 13:13:23 +0100 | [diff] [blame] | 184 | setenv_args.script_file = optarg; |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 185 | break; |
Andreas Fenkart | 6726fc4 | 2015-12-09 13:13:26 +0100 | [diff] [blame] | 186 | case 'a': |
| 187 | case 'c': |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 188 | case 'h': |
Andreas Fenkart | 6726fc4 | 2015-12-09 13:13:26 +0100 | [diff] [blame] | 189 | /* ignore common options */ |
Andreas Fenkart | 33e9177 | 2015-12-09 13:13:23 +0100 | [diff] [blame] | 190 | break; |
Daniel Hobi | 6a20a32 | 2010-09-16 14:36:09 +0200 | [diff] [blame] | 191 | default: /* '?' */ |
Andreas Fenkart | 9b5a28b | 2015-12-09 13:13:27 +0100 | [diff] [blame^] | 192 | usage_setenv(); |
Andreas Fenkart | 33e9177 | 2015-12-09 13:13:23 +0100 | [diff] [blame] | 193 | exit(EXIT_FAILURE); |
| 194 | break; |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 195 | } |
| 196 | } |
Andreas Fenkart | 33e9177 | 2015-12-09 13:13:23 +0100 | [diff] [blame] | 197 | return 0; |
| 198 | } |
| 199 | |
| 200 | int main(int argc, char *argv[]) |
| 201 | { |
Andreas Fenkart | 33e9177 | 2015-12-09 13:13:23 +0100 | [diff] [blame] | 202 | const char *lockname = "/var/lock/" CMD_PRINTENV ".lock"; |
| 203 | int lockfd = -1; |
| 204 | int retval = EXIT_SUCCESS; |
Andreas Fenkart | 9b5a28b | 2015-12-09 13:13:27 +0100 | [diff] [blame^] | 205 | char *_cmdname; |
Andreas Fenkart | 33e9177 | 2015-12-09 13:13:23 +0100 | [diff] [blame] | 206 | |
Andreas Fenkart | 9b5a28b | 2015-12-09 13:13:27 +0100 | [diff] [blame^] | 207 | _cmdname = *argv; |
| 208 | if (strrchr(_cmdname, '/') != NULL) |
| 209 | _cmdname = strrchr(_cmdname, '/') + 1; |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 210 | |
Andreas Fenkart | 9b5a28b | 2015-12-09 13:13:27 +0100 | [diff] [blame^] | 211 | if (strcmp(_cmdname, CMD_PRINTENV) == 0) { |
| 212 | do_printenv = 1; |
| 213 | } else if (strcmp(_cmdname, CMD_SETENV) == 0) { |
| 214 | do_printenv = 0; |
Andreas Fenkart | 33e9177 | 2015-12-09 13:13:23 +0100 | [diff] [blame] | 215 | } else { |
| 216 | fprintf(stderr, |
| 217 | "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^] | 218 | CMD_PRINTENV, CMD_SETENV, _cmdname); |
Andreas Fenkart | 33e9177 | 2015-12-09 13:13:23 +0100 | [diff] [blame] | 219 | exit(EXIT_FAILURE); |
| 220 | } |
| 221 | |
Andreas Fenkart | 9b5a28b | 2015-12-09 13:13:27 +0100 | [diff] [blame^] | 222 | if (do_printenv) { |
| 223 | if (parse_printenv_args(argc, argv)) |
| 224 | exit(EXIT_FAILURE); |
| 225 | } else { |
| 226 | if (parse_setenv_args(argc, argv)) |
| 227 | exit(EXIT_FAILURE); |
| 228 | } |
| 229 | |
Andreas Fenkart | 14908ef | 2015-12-09 13:13:25 +0100 | [diff] [blame] | 230 | /* shift parsed flags, jump to non-option arguments */ |
| 231 | argc -= optind; |
| 232 | argv += optind; |
| 233 | |
Andreas Fenkart | 33e9177 | 2015-12-09 13:13:23 +0100 | [diff] [blame] | 234 | lockfd = open(lockname, O_WRONLY | O_CREAT | O_TRUNC, 0666); |
| 235 | if (-1 == lockfd) { |
| 236 | fprintf(stderr, "Error opening lock file %s\n", lockname); |
| 237 | return EXIT_FAILURE; |
| 238 | } |
| 239 | |
| 240 | if (-1 == flock(lockfd, LOCK_EX)) { |
| 241 | fprintf(stderr, "Error locking file %s\n", lockname); |
| 242 | close(lockfd); |
| 243 | return EXIT_FAILURE; |
| 244 | } |
| 245 | |
Andreas Fenkart | 9b5a28b | 2015-12-09 13:13:27 +0100 | [diff] [blame^] | 246 | if (do_printenv) { |
Joe Hershberger | 3f1f175 | 2012-10-03 09:38:49 +0000 | [diff] [blame] | 247 | if (fw_printenv(argc, argv) != 0) |
| 248 | retval = EXIT_FAILURE; |
Andreas Fenkart | 9b5a28b | 2015-12-09 13:13:27 +0100 | [diff] [blame^] | 249 | } else { |
Andreas Fenkart | 33e9177 | 2015-12-09 13:13:23 +0100 | [diff] [blame] | 250 | if (!setenv_args.script_file) { |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 251 | if (fw_setenv(argc, argv) != 0) |
Joe Hershberger | 3f1f175 | 2012-10-03 09:38:49 +0000 | [diff] [blame] | 252 | retval = EXIT_FAILURE; |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 253 | } else { |
Andreas Fenkart | 33e9177 | 2015-12-09 13:13:23 +0100 | [diff] [blame] | 254 | if (fw_parse_script(setenv_args.script_file) != 0) |
Joe Hershberger | 3f1f175 | 2012-10-03 09:38:49 +0000 | [diff] [blame] | 255 | retval = EXIT_FAILURE; |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 256 | } |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Joe Hershberger | 3f1f175 | 2012-10-03 09:38:49 +0000 | [diff] [blame] | 259 | flock(lockfd, LOCK_UN); |
| 260 | close(lockfd); |
| 261 | return retval; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 262 | } |