2 * (C) Copyright 2000-2008
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * See file CREDITS for list of people who contributed to this
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
25 * Command line user interface to firmware (=U-Boot) environment.
28 * fw_printenv [[ -n name ] | [ name ... ]]
29 * - prints the value of a single environment variable
30 * "name", the ``name=value'' pairs of one or more
31 * environment variables "name", or the whole
32 * environment if no names are specified.
33 * fw_setenv name [ value ... ]
34 * - If a name without any values is given, the variable
35 * with this name is deleted from the environment;
36 * otherwise, all "value" arguments are concatenated,
37 * separated by single blank characters, and the
38 * resulting string is assigned to the environment
51 #define CMD_PRINTENV "fw_printenv"
52 #define CMD_SETENV "fw_setenv"
54 static struct option long_options[] = {
55 {"script", required_argument, NULL, 's'},
56 {"help", no_argument, NULL, 'h'},
63 fprintf(stderr, "fw_printenv/fw_setenv, "
64 "a command line interface to U-Boot environment\n\n"
65 "usage:\tfw_printenv [-n] [variable name]\n"
66 "\tfw_setenv [variable name] [variable value]\n"
67 "\tfw_setenv -s [ file ]\n"
68 "\tfw_setenv -s - < [ file ]\n\n"
69 "The file passed as argument contains only pairs "
72 "# Any line starting with # is treated as comment\n"
75 "\t kernel_addr 400000\n"
77 "\t var2 The quick brown fox jumps over the "
80 "A variable without value will be dropped. It is possible\n"
81 "to put any number of spaces between the fields, but any\n"
82 "space inside the value is treated as part of the value "
87 int main(int argc, char *argv[])
90 char *cmdname = *argv;
91 char *script_file = NULL;
93 const char *lockname = "/var/lock/" CMD_PRINTENV ".lock";
95 int retval = EXIT_SUCCESS;
97 lockfd = open(lockname, O_WRONLY | O_CREAT | O_TRUNC);
99 fprintf(stderr, "Error opening lock file %s\n", lockname);
103 if (-1 == flock(lockfd, LOCK_EX)) {
104 fprintf(stderr, "Error locking file %s\n", lockname);
109 if ((p = strrchr (cmdname, '/')) != NULL) {
113 while ((c = getopt_long (argc, argv, "ns:h",
114 long_options, NULL)) != EOF) {
117 /* handled in fw_printenv */
120 script_file = optarg;
126 fprintf(stderr, "Try `%s --help' for more information."
128 retval = EXIT_FAILURE;
133 if (strcmp(cmdname, CMD_PRINTENV) == 0) {
134 if (fw_printenv(argc, argv) != 0)
135 retval = EXIT_FAILURE;
136 } else if (strcmp(cmdname, CMD_SETENV) == 0) {
138 if (fw_setenv(argc, argv) != 0)
139 retval = EXIT_FAILURE;
141 if (fw_parse_script(script_file) != 0)
142 retval = EXIT_FAILURE;
146 "Identity crisis - may be called as `" CMD_PRINTENV
147 "' or as `" CMD_SETENV "' but not as `%s'\n",
149 retval = EXIT_FAILURE;
153 flock(lockfd, LOCK_UN);