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"
42 static struct option long_options[] = {
43 {"script", required_argument, NULL, 's'},
44 {"help", no_argument, NULL, 'h'},
48 struct common_args common_args;
49 struct printenv_args printenv_args;
50 struct setenv_args setenv_args;
55 fprintf(stderr, "fw_printenv/fw_setenv, "
56 "a command line interface to U-Boot environment\n\n"
58 "usage:\tfw_printenv [-a key] [-n] [variable name]\n"
59 "\tfw_setenv [-a key] [variable name] [variable value]\n"
61 "usage:\tfw_printenv [-c /my/fw_env.config] [-a key] [-n] [variable name]\n"
62 "\tfw_setenv [-c /my/fw_env.config] [-a key] [variable name] [variable value]\n"
64 "\tfw_setenv -s [ file ]\n"
65 "\tfw_setenv -s - < [ file ]\n\n"
66 "The file passed as argument contains only pairs "
69 "# Any line starting with # is treated as comment\n"
72 "\t kernel_addr 400000\n"
74 "\t var2 The quick brown fox jumps over the "
77 "A variable without value will be dropped. It is possible\n"
78 "to put any number of spaces between the fields, but any\n"
79 "space inside the value is treated as part of the value "
84 static void parse_common_args(int argc, char *argv[])
89 common_args.config_file = CONFIG_FILE;
92 while ((c = getopt_long(argc, argv, ":a:c:h", long_options, NULL)) !=
96 if (parse_aes_key(optarg, common_args.aes_key)) {
97 fprintf(stderr, "AES key parse error\n");
100 common_args.aes_flag = 1;
104 common_args.config_file = optarg;
112 /* ignore unknown options */
117 /* Reset getopt for the next pass. */
122 int parse_printenv_args(int argc, char *argv[])
126 parse_common_args(argc, argv);
128 while ((c = getopt_long(argc, argv, "a:c:ns:h", long_options, NULL)) !=
132 printenv_args.name_suppress = 1;
137 /* ignore common options */
148 int parse_setenv_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 setenv_args.script_file = optarg;
163 /* ignore common options */
174 int main(int argc, char *argv[])
176 char *cmdname = *argv;
177 const char *lockname = "/var/lock/" CMD_PRINTENV ".lock";
179 int retval = EXIT_SUCCESS;
181 if (strrchr(cmdname, '/') != NULL)
182 cmdname = strrchr(cmdname, '/') + 1;
184 if (strcmp(cmdname, CMD_PRINTENV) == 0) {
185 if (parse_printenv_args(argc, argv))
187 } else if (strcmp(cmdname, CMD_SETENV) == 0) {
188 if (parse_setenv_args(argc, argv))
192 "Identity crisis - may be called as `%s' or as `%s' but not as `%s'\n",
193 CMD_PRINTENV, CMD_SETENV, cmdname);
197 /* shift parsed flags, jump to non-option arguments */
201 lockfd = open(lockname, O_WRONLY | O_CREAT | O_TRUNC, 0666);
203 fprintf(stderr, "Error opening lock file %s\n", lockname);
207 if (-1 == flock(lockfd, LOCK_EX)) {
208 fprintf(stderr, "Error locking file %s\n", lockname);
213 if (strcmp(cmdname, CMD_PRINTENV) == 0) {
214 if (fw_printenv(argc, argv) != 0)
215 retval = EXIT_FAILURE;
216 } else if (strcmp(cmdname, CMD_SETENV) == 0) {
217 if (!setenv_args.script_file) {
218 if (fw_setenv(argc, argv) != 0)
219 retval = EXIT_FAILURE;
221 if (fw_parse_script(setenv_args.script_file) != 0)
222 retval = EXIT_FAILURE;
226 flock(lockfd, LOCK_UN);