2 * (C) Copyright 2000-2008
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * SPDX-License-Identifier: GPL-2.0+
9 * Command line user interface to firmware (=U-Boot) environment.
12 * fw_printenv [ -a key ] [[ -n name ] | [ name ... ]]
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.
17 * fw_setenv [ -a key ] name [ value ... ]
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,
21 * separated by single blank characters, and the
22 * resulting string is assigned to the environment
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'.
39 #define CMD_PRINTENV "fw_printenv"
40 #define CMD_SETENV "fw_setenv"
41 static int do_printenv;
43 static struct option long_options[] = {
44 {"aes", required_argument, NULL, 'a'},
45 {"config", required_argument, NULL, 'c'},
46 {"help", no_argument, NULL, 'h'},
47 {"script", required_argument, NULL, 's'},
48 {"noheader", required_argument, NULL, 'n'},
52 struct common_args common_args;
53 struct printenv_args printenv_args;
54 struct setenv_args setenv_args;
56 void usage_printenv(void)
60 "Usage: fw_printenv [OPTIONS]... [VARIABLE]...\n"
61 "Print variables from U-Boot environment\n"
63 " -h, --help print this help.\n"
65 " -a, --aes aes key to access environment\n"
68 " -c, --config configuration file, default:" CONFIG_FILE "\n"
70 " -n, --noheader do not repeat variable name in output\n"
74 void usage_setenv(void)
77 "Usage: fw_setenv [OPTIONS]... [VARIABLE]...\n"
78 "Modify variables in U-Boot environment\n"
80 " -h, --help print this help.\n"
82 " -a, --aes aes key to access environment\n"
85 " -c, --config configuration file, default:" CONFIG_FILE "\n"
87 " -s, --script batch mode to minimize writes\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"
95 " key [space] value\n"
96 " lines starting with '#' are treated as commment\n"
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"
104 " kernel_addr 400000\n"
105 " foo empty empty empty empty empty empty\n"
110 static void parse_common_args(int argc, char *argv[])
115 common_args.config_file = CONFIG_FILE;
118 while ((c = getopt_long(argc, argv, ":a:c:h", long_options, NULL)) !=
122 if (parse_aes_key(optarg, common_args.aes_key)) {
123 fprintf(stderr, "AES key parse error\n");
126 common_args.aes_flag = 1;
130 common_args.config_file = optarg;
134 do_printenv ? usage_printenv() : usage_setenv();
138 /* ignore unknown options */
143 /* Reset getopt for the next pass. */
148 int parse_printenv_args(int argc, char *argv[])
152 parse_common_args(argc, argv);
154 while ((c = getopt_long(argc, argv, "a:c:ns:h", long_options, NULL)) !=
158 printenv_args.name_suppress = 1;
163 /* ignore common options */
174 int parse_setenv_args(int argc, char *argv[])
178 parse_common_args(argc, argv);
180 while ((c = getopt_long(argc, argv, "a:c:ns:h", long_options, NULL)) !=
184 setenv_args.script_file = optarg;
189 /* ignore common options */
200 int main(int argc, char *argv[])
202 const char *lockname = "/var/lock/" CMD_PRINTENV ".lock";
204 int retval = EXIT_SUCCESS;
208 if (strrchr(_cmdname, '/') != NULL)
209 _cmdname = strrchr(_cmdname, '/') + 1;
211 if (strcmp(_cmdname, CMD_PRINTENV) == 0) {
213 } else if (strcmp(_cmdname, CMD_SETENV) == 0) {
217 "Identity crisis - may be called as `%s' or as `%s' but not as `%s'\n",
218 CMD_PRINTENV, CMD_SETENV, _cmdname);
223 if (parse_printenv_args(argc, argv))
226 if (parse_setenv_args(argc, argv))
230 /* shift parsed flags, jump to non-option arguments */
234 lockfd = open(lockname, O_WRONLY | O_CREAT | O_TRUNC, 0666);
236 fprintf(stderr, "Error opening lock file %s\n", lockname);
240 if (-1 == flock(lockfd, LOCK_EX)) {
241 fprintf(stderr, "Error locking file %s\n", lockname);
247 if (fw_printenv(argc, argv) != 0)
248 retval = EXIT_FAILURE;
250 if (!setenv_args.script_file) {
251 if (fw_setenv(argc, argv) != 0)
252 retval = EXIT_FAILURE;
254 if (fw_parse_script(setenv_args.script_file) != 0)
255 retval = EXIT_FAILURE;
259 flock(lockfd, LOCK_UN);