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 void usage_printenv(void)
56 "Usage: fw_printenv [OPTIONS]... [VARIABLE]...\n"
57 "Print variables from U-Boot environment\n"
59 " -h, --help print this help.\n"
61 " -a, --aes aes key to access environment\n"
64 " -c, --config configuration file, default:" CONFIG_FILE "\n"
66 " -n, --noheader do not repeat variable name in output\n"
70 void usage_setenv(void)
73 "Usage: fw_setenv [OPTIONS]... [VARIABLE]...\n"
74 "Modify variables in U-Boot environment\n"
76 " -h, --help print this help.\n"
78 " -a, --aes aes key to access environment\n"
81 " -c, --config configuration file, default:" CONFIG_FILE "\n"
83 " -s, --script batch mode to minimize writes\n"
86 " fw_setenv foo bar set variable foo equal bar\n"
87 " fw_setenv foo clear variable foo\n"
88 " fw_setenv --script file run batch script\n"
91 " key [space] value\n"
92 " lines starting with '#' are treated as comment\n"
94 " A variable without value will be deleted. Any number of spaces are\n"
95 " allowed between key and value. Space inside of the value is treated\n"
96 " as part of the value itself.\n"
100 " kernel_addr 400000\n"
101 " foo empty empty empty empty empty empty\n"
106 static void parse_common_args(int argc, char *argv[])
111 common_args.config_file = CONFIG_FILE;
114 while ((c = getopt_long(argc, argv, ":a:c:h", long_options, NULL)) !=
118 if (parse_aes_key(optarg, common_args.aes_key)) {
119 fprintf(stderr, "AES key parse error\n");
122 common_args.aes_flag = 1;
126 common_args.config_file = optarg;
130 do_printenv ? usage_printenv() : usage_setenv();
134 /* ignore unknown options */
139 /* Reset getopt for the next pass. */
144 int parse_printenv_args(int argc, char *argv[])
148 parse_common_args(argc, argv);
150 while ((c = getopt_long(argc, argv, "a:c:ns:h", long_options, NULL)) !=
154 printenv_args.value_only = 1;
159 /* ignore common options */
170 int parse_setenv_args(int argc, char *argv[])
174 parse_common_args(argc, argv);
176 while ((c = getopt_long(argc, argv, "a:c:ns:h", long_options, NULL)) !=
180 setenv_args.script_file = optarg;
185 /* ignore common options */
196 int main(int argc, char *argv[])
198 const char *lockname = "/var/lock/" CMD_PRINTENV ".lock";
200 int retval = EXIT_SUCCESS;
204 if (strrchr(_cmdname, '/') != NULL)
205 _cmdname = strrchr(_cmdname, '/') + 1;
207 if (strcmp(_cmdname, CMD_PRINTENV) == 0) {
209 } else if (strcmp(_cmdname, CMD_SETENV) == 0) {
213 "Identity crisis - may be called as `%s' or as `%s' but not as `%s'\n",
214 CMD_PRINTENV, CMD_SETENV, _cmdname);
219 if (parse_printenv_args(argc, argv))
222 if (parse_setenv_args(argc, argv))
226 /* shift parsed flags, jump to non-option arguments */
230 lockfd = open(lockname, O_WRONLY | O_CREAT | O_TRUNC, 0666);
232 fprintf(stderr, "Error opening lock file %s\n", lockname);
236 if (-1 == flock(lockfd, LOCK_EX)) {
237 fprintf(stderr, "Error locking file %s\n", lockname);
243 if (fw_printenv(argc, argv, printenv_args.value_only))
244 retval = EXIT_FAILURE;
246 if (!setenv_args.script_file) {
247 if (fw_setenv(argc, argv) != 0)
248 retval = EXIT_FAILURE;
250 if (fw_parse_script(setenv_args.script_file) != 0)
251 retval = EXIT_FAILURE;
255 flock(lockfd, LOCK_UN);