]>
git.ipfire.org Git - people/ms/u-boot.git/blob - tools/env/fw_env_main.c
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'.
38 #include "fw_env_private.h"
41 #define CMD_PRINTENV "fw_printenv"
42 #define CMD_SETENV "fw_setenv"
43 static int do_printenv
;
45 static struct option long_options
[] = {
46 {"aes", required_argument
, NULL
, 'a'},
47 {"config", required_argument
, NULL
, 'c'},
48 {"help", no_argument
, NULL
, 'h'},
49 {"script", required_argument
, NULL
, 's'},
50 {"noheader", required_argument
, NULL
, 'n'},
51 {"lock", required_argument
, NULL
, 'l'},
52 {"version", no_argument
, NULL
, 'v'},
56 static struct env_opts env_opts
;
62 static char *script_file
;
64 void usage_printenv(void)
68 "Usage: fw_printenv [OPTIONS]... [VARIABLE]...\n"
69 "Print variables from U-Boot environment\n"
71 " -h, --help print this help.\n"
72 " -v, --version display version\n"
74 " -a, --aes aes key to access environment\n"
77 " -c, --config configuration file, default:" CONFIG_FILE
"\n"
79 " -n, --noheader do not repeat variable name in output\n"
80 " -l, --lock lock node, default:/var/lock\n"
84 void usage_env_set(void)
87 "Usage: fw_setenv [OPTIONS]... [VARIABLE]...\n"
88 "Modify variables in U-Boot environment\n"
90 " -h, --help print this help.\n"
91 " -v, --version display version\n"
93 " -a, --aes aes key to access environment\n"
96 " -c, --config configuration file, default:" CONFIG_FILE
"\n"
98 " -l, --lock lock node, default:/var/lock\n"
99 " -s, --script batch mode to minimize writes\n"
102 " fw_setenv foo bar set variable foo equal bar\n"
103 " fw_setenv foo clear variable foo\n"
104 " fw_setenv --script file run batch script\n"
107 " key [space] value\n"
108 " lines starting with '#' are treated as comment\n"
110 " A variable without value will be deleted. Any number of spaces are\n"
111 " allowed between key and value. Space inside of the value is treated\n"
112 " as part of the value itself.\n"
116 " kernel_addr 400000\n"
117 " foo empty empty empty empty empty empty\n"
122 static void parse_common_args(int argc
, char *argv
[])
127 env_opts
.config_file
= CONFIG_FILE
;
130 while ((c
= getopt_long(argc
, argv
, ":a:c:l:h:v", long_options
, NULL
)) !=
134 if (parse_aes_key(optarg
, env_opts
.aes_key
)) {
135 fprintf(stderr
, "AES key parse error\n");
138 env_opts
.aes_flag
= 1;
142 env_opts
.config_file
= optarg
;
146 env_opts
.lockname
= optarg
;
149 do_printenv
? usage_printenv() : usage_env_set();
153 fprintf(stderr
, "Compiled with " U_BOOT_VERSION
"\n");
157 /* ignore unknown options */
162 /* Reset getopt for the next pass. */
167 int parse_printenv_args(int argc
, char *argv
[])
171 parse_common_args(argc
, argv
);
173 while ((c
= getopt_long(argc
, argv
, "a:c:ns:l:h:v", long_options
, NULL
))
183 /* ignore common options */
194 int parse_setenv_args(int argc
, char *argv
[])
198 parse_common_args(argc
, argv
);
200 while ((c
= getopt_long(argc
, argv
, "a:c:ns:l:h:v", long_options
, NULL
))
204 script_file
= optarg
;
210 /* ignore common options */
221 int main(int argc
, char *argv
[])
223 char *lockname
= "/var/lock/" CMD_PRINTENV
".lock";
225 int retval
= EXIT_SUCCESS
;
229 if (strrchr(_cmdname
, '/') != NULL
)
230 _cmdname
= strrchr(_cmdname
, '/') + 1;
232 if (strcmp(_cmdname
, CMD_PRINTENV
) == 0) {
234 } else if (strcmp(_cmdname
, CMD_SETENV
) == 0) {
238 "Identity crisis - may be called as `%s' or as `%s' but not as `%s'\n",
239 CMD_PRINTENV
, CMD_SETENV
, _cmdname
);
244 if (parse_printenv_args(argc
, argv
))
247 if (parse_setenv_args(argc
, argv
))
251 /* shift parsed flags, jump to non-option arguments */
255 if (env_opts
.lockname
) {
256 lockname
= malloc(sizeof(env_opts
.lockname
) +
257 sizeof(CMD_PRINTENV
) + 10);
259 fprintf(stderr
, "Unable allocate memory");
263 sprintf(lockname
, "%s/%s.lock",
264 env_opts
.lockname
, CMD_PRINTENV
);
267 lockfd
= open(lockname
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0666);
269 fprintf(stderr
, "Error opening lock file %s\n", lockname
);
273 if (-1 == flock(lockfd
, LOCK_EX
)) {
274 fprintf(stderr
, "Error locking file %s\n", lockname
);
280 if (fw_printenv(argc
, argv
, noheader
, &env_opts
) != 0)
281 retval
= EXIT_FAILURE
;
284 if (fw_env_set(argc
, argv
, &env_opts
) != 0)
285 retval
= EXIT_FAILURE
;
287 if (fw_parse_script(script_file
, &env_opts
) != 0)
288 retval
= EXIT_FAILURE
;
292 if (env_opts
.lockname
)
295 flock(lockfd
, LOCK_UN
);