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 [[ -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 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
35 #define CMD_PRINTENV "fw_printenv"
36 #define CMD_SETENV "fw_setenv"
38 static struct option long_options[] = {
39 {"script", required_argument, NULL, 's'},
40 {"help", no_argument, NULL, 'h'},
47 fprintf(stderr, "fw_printenv/fw_setenv, "
48 "a command line interface to U-Boot environment\n\n"
49 "usage:\tfw_printenv [-n] [variable name]\n"
50 "\tfw_setenv [variable name] [variable value]\n"
51 "\tfw_setenv -s [ file ]\n"
52 "\tfw_setenv -s - < [ file ]\n\n"
53 "The file passed as argument contains only pairs "
56 "# Any line starting with # is treated as comment\n"
59 "\t kernel_addr 400000\n"
61 "\t var2 The quick brown fox jumps over the "
64 "A variable without value will be dropped. It is possible\n"
65 "to put any number of spaces between the fields, but any\n"
66 "space inside the value is treated as part of the value "
71 int main(int argc, char *argv[])
74 char *cmdname = *argv;
75 char *script_file = NULL;
77 const char *lockname = "/var/lock/" CMD_PRINTENV ".lock";
79 int retval = EXIT_SUCCESS;
81 lockfd = open(lockname, O_WRONLY | O_CREAT | O_TRUNC, 0666);
83 fprintf(stderr, "Error opening lock file %s\n", lockname);
87 if (-1 == flock(lockfd, LOCK_EX)) {
88 fprintf(stderr, "Error locking file %s\n", lockname);
93 if ((p = strrchr (cmdname, '/')) != NULL) {
97 while ((c = getopt_long (argc, argv, "ns:h",
98 long_options, NULL)) != EOF) {
101 /* handled in fw_printenv */
104 script_file = optarg;
110 fprintf(stderr, "Try `%s --help' for more information."
112 retval = EXIT_FAILURE;
117 if (strcmp(cmdname, CMD_PRINTENV) == 0) {
118 if (fw_printenv(argc, argv) != 0)
119 retval = EXIT_FAILURE;
120 } else if (strcmp(cmdname, CMD_SETENV) == 0) {
122 if (fw_setenv(argc, argv) != 0)
123 retval = EXIT_FAILURE;
125 if (fw_parse_script(script_file) != 0)
126 retval = EXIT_FAILURE;
130 "Identity crisis - may be called as `" CMD_PRINTENV
131 "' or as `" CMD_SETENV "' but not as `%s'\n",
133 retval = EXIT_FAILURE;
137 flock(lockfd, LOCK_UN);