1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2000-2008
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
8 * Command line user interface to firmware (=U-Boot) environment.
11 * fw_printenv [ -a key ] [[ -n name ] | [ name ... ]]
12 * - prints the value of a single environment variable
13 * "name", the ``name=value'' pairs of one or more
14 * environment variables "name", or the whole
15 * environment if no names are specified.
16 * fw_setenv [ -a key ] name [ value ... ]
17 * - If a name without any values is given, the variable
18 * with this name is deleted from the environment;
19 * otherwise, all "value" arguments are concatenated,
20 * separated by single blank characters, and the
21 * resulting string is assigned to the environment
24 * If '-a key' is specified, the env block is encrypted with AES 128 CBC.
25 * The 'key' argument is in the format of 32 hexadecimal numbers (16 bytes
26 * of AES key), eg. '-a aabbccddeeff00112233445566778899'.
37 #include "fw_env_private.h"
40 #define CMD_PRINTENV "fw_printenv"
41 #define CMD_SETENV "fw_setenv"
42 static int do_printenv;
44 static struct option long_options[] = {
45 {"config", required_argument, NULL, 'c'},
46 {"help", no_argument, NULL, 'h'},
47 {"script", required_argument, NULL, 's'},
48 {"noheader", no_argument, NULL, 'n'},
49 {"lock", required_argument, NULL, 'l'},
50 {"version", no_argument, NULL, 'v'},
54 static struct env_opts env_opts;
60 static char *script_file;
62 void usage_printenv(void)
66 "Usage: fw_printenv [OPTIONS]... [VARIABLE]...\n"
67 "Print variables from U-Boot environment\n"
69 " -h, --help print this help.\n"
70 " -v, --version display version\n"
72 " -c, --config configuration file, default:" CONFIG_FILE "\n"
74 " -n, --noheader do not repeat variable name in output\n"
75 " -l, --lock lock node, default:/var/lock\n"
79 void usage_env_set(void)
82 "Usage: fw_setenv [OPTIONS]... [VARIABLE]...\n"
83 "Modify variables in U-Boot environment\n"
85 " -h, --help print this help.\n"
86 " -v, --version display version\n"
88 " -c, --config configuration file, default:" CONFIG_FILE "\n"
90 " -l, --lock lock node, default:/var/lock\n"
91 " -s, --script batch mode to minimize writes\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"
99 " key [space] value\n"
100 " lines starting with '#' are treated as comment\n"
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"
108 " kernel_addr 400000\n"
109 " foo empty empty empty empty empty empty\n"
114 static void parse_common_args(int argc, char *argv[])
119 env_opts.config_file = CONFIG_FILE;
122 while ((c = getopt_long(argc, argv, ":a:c:l:h:v", long_options, NULL)) !=
127 env_opts.config_file = optarg;
131 env_opts.lockname = optarg;
134 do_printenv ? usage_printenv() : usage_env_set();
138 fprintf(stderr, "Compiled with " U_BOOT_VERSION "\n");
142 /* ignore unknown options */
147 /* Reset getopt for the next pass. */
152 int parse_printenv_args(int argc, char *argv[])
156 parse_common_args(argc, argv);
158 while ((c = getopt_long(argc, argv, "a:c:ns:l:h:v", long_options, NULL))
168 /* ignore common options */
179 int parse_setenv_args(int argc, char *argv[])
183 parse_common_args(argc, argv);
185 while ((c = getopt_long(argc, argv, "a:c:ns:l:h:v", long_options, NULL))
189 script_file = optarg;
195 /* ignore common options */
206 int main(int argc, char *argv[])
208 char *lockname = "/var/lock/" CMD_PRINTENV ".lock";
210 int retval = EXIT_SUCCESS;
214 if (strrchr(_cmdname, '/') != NULL)
215 _cmdname = strrchr(_cmdname, '/') + 1;
217 if (strcmp(_cmdname, CMD_PRINTENV) == 0) {
219 } else if (strcmp(_cmdname, CMD_SETENV) == 0) {
223 "Identity crisis - may be called as `%s' or as `%s' but not as `%s'\n",
224 CMD_PRINTENV, CMD_SETENV, _cmdname);
229 if (parse_printenv_args(argc, argv))
232 if (parse_setenv_args(argc, argv))
236 /* shift parsed flags, jump to non-option arguments */
240 if (env_opts.lockname) {
241 lockname = malloc(strlen(env_opts.lockname) +
242 sizeof(CMD_PRINTENV) + 10);
244 fprintf(stderr, "Unable allocate memory");
248 sprintf(lockname, "%s/%s.lock",
249 env_opts.lockname, CMD_PRINTENV);
252 lockfd = open(lockname, O_WRONLY | O_CREAT | O_TRUNC, 0666);
254 fprintf(stderr, "Error opening lock file %s\n", lockname);
258 if (-1 == flock(lockfd, LOCK_EX)) {
259 fprintf(stderr, "Error locking file %s\n", lockname);
265 if (fw_printenv(argc, argv, noheader, &env_opts) != 0)
266 retval = EXIT_FAILURE;
269 if (fw_env_set(argc, argv, &env_opts) != 0)
270 retval = EXIT_FAILURE;
272 if (fw_parse_script(script_file, &env_opts) != 0)
273 retval = EXIT_FAILURE;
277 if (env_opts.lockname)
280 flock(lockfd, LOCK_UN);