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'},
51 fprintf(stderr, "fw_printenv/fw_setenv, "
52 "a command line interface to U-Boot environment\n\n"
54 "usage:\tfw_printenv [-a key] [-n] [variable name]\n"
55 "\tfw_setenv [-a key] [variable name] [variable value]\n"
57 "usage:\tfw_printenv [-c /my/fw_env.config] [-a key] [-n] [variable name]\n"
58 "\tfw_setenv [-c /my/fw_env.config] [-a key] [variable name] [variable value]\n"
60 "\tfw_setenv -s [ file ]\n"
61 "\tfw_setenv -s - < [ file ]\n\n"
62 "The file passed as argument contains only pairs "
65 "# Any line starting with # is treated as comment\n"
68 "\t kernel_addr 400000\n"
70 "\t var2 The quick brown fox jumps over the "
73 "A variable without value will be dropped. It is possible\n"
74 "to put any number of spaces between the fields, but any\n"
75 "space inside the value is treated as part of the value "
80 int main(int argc, char *argv[])
83 char *cmdname = *argv;
84 char *script_file = NULL;
86 const char *lockname = "/var/lock/" CMD_PRINTENV ".lock";
88 int retval = EXIT_SUCCESS;
90 lockfd = open(lockname, O_WRONLY | O_CREAT | O_TRUNC, 0666);
92 fprintf(stderr, "Error opening lock file %s\n", lockname);
96 if (-1 == flock(lockfd, LOCK_EX)) {
97 fprintf(stderr, "Error locking file %s\n", lockname);
102 if ((p = strrchr (cmdname, '/')) != NULL) {
106 while ((c = getopt_long (argc, argv, "a:c:ns:h",
107 long_options, NULL)) != EOF) {
110 /* AES key, handled later */
116 /* handled in fw_printenv */
119 script_file = optarg;
125 fprintf(stderr, "Try `%s --help' for more information."
127 retval = EXIT_FAILURE;
132 if (strcmp(cmdname, CMD_PRINTENV) == 0) {
133 if (fw_printenv(argc, argv) != 0)
134 retval = EXIT_FAILURE;
135 } else if (strcmp(cmdname, CMD_SETENV) == 0) {
137 if (fw_setenv(argc, argv) != 0)
138 retval = EXIT_FAILURE;
140 if (fw_parse_script(script_file) != 0)
141 retval = EXIT_FAILURE;
145 "Identity crisis - may be called as `" CMD_PRINTENV
146 "' or as `" CMD_SETENV "' but not as `%s'\n",
148 retval = EXIT_FAILURE;
152 flock(lockfd, LOCK_UN);