blob: 26ba6624cd19cb2be078e683122c95506ca11d26 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk34b613e2002-12-17 01:51:00 +00002/*
Grant Erickson73a4e5e2008-05-06 20:16:15 -07003 * (C) Copyright 2000-2008
wdenk34b613e2002-12-17 01:51:00 +00004 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
wdenk34b613e2002-12-17 01:51:00 +00005 */
6
7/*
wdenkef5fe752003-03-12 10:41:04 +00008 * Command line user interface to firmware (=U-Boot) environment.
wdenk34b613e2002-12-17 01:51:00 +00009 *
10 * Implements:
Marek Vasut05ed2a52014-03-05 19:59:52 +010011 * fw_printenv [ -a key ] [[ -n name ] | [ name ... ]]
Grant Erickson73a4e5e2008-05-06 20:16:15 -070012 * - prints the value of a single environment variable
13 * "name", the ``name=value'' pairs of one or more
14 * environment variables "name", or the whole
15 * environment if no names are specified.
Marek Vasut05ed2a52014-03-05 19:59:52 +010016 * fw_setenv [ -a key ] name [ value ... ]
wdenk34b613e2002-12-17 01:51:00 +000017 * - If a name without any values is given, the variable
18 * with this name is deleted from the environment;
19 * otherwise, all "value" arguments are concatenated,
Grant Erickson73a4e5e2008-05-06 20:16:15 -070020 * separated by single blank characters, and the
wdenk34b613e2002-12-17 01:51:00 +000021 * resulting string is assigned to the environment
22 * variable "name"
Marek Vasut05ed2a52014-03-05 19:59:52 +010023 *
24 * If '-a key' is specified, the env block is encrypted with AES 128 CBC.
25 * The 'key' argument is in the format of 32 hexadecimal numbers (16 bytes
26 * of AES key), eg. '-a aabbccddeeff00112233445566778899'.
wdenk34b613e2002-12-17 01:51:00 +000027 */
28
Joe Hershberger3f1f1752012-10-03 09:38:49 +000029#include <fcntl.h>
30#include <getopt.h>
wdenk34b613e2002-12-17 01:51:00 +000031#include <stdio.h>
32#include <string.h>
33#include <stdlib.h>
Joe Hershberger3f1f1752012-10-03 09:38:49 +000034#include <sys/file.h>
35#include <unistd.h>
Stefan Agnere5308f82017-10-26 14:57:53 +020036#include <version.h>
Stefano Babic831c4d52017-04-05 18:08:01 +020037#include "fw_env_private.h"
wdenk34b613e2002-12-17 01:51:00 +000038#include "fw_env.h"
39
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +010040#define CMD_PRINTENV "fw_printenv"
wdenk34b613e2002-12-17 01:51:00 +000041#define CMD_SETENV "fw_setenv"
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +010042static int do_printenv;
wdenk34b613e2002-12-17 01:51:00 +000043
Stefano Babicada628b2010-05-24 12:08:16 +020044static struct option long_options[] = {
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +010045 {"config", required_argument, NULL, 'c'},
Stefano Babicada628b2010-05-24 12:08:16 +020046 {"help", no_argument, NULL, 'h'},
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +010047 {"script", required_argument, NULL, 's'},
Alex Kiernan688cee92018-02-11 17:16:46 +000048 {"noheader", no_argument, NULL, 'n'},
B, Raviccb293f2016-09-26 18:24:08 +053049 {"lock", required_argument, NULL, 'l'},
Stefan Agnere5308f82017-10-26 14:57:53 +020050 {"version", no_argument, NULL, 'v'},
Stefano Babicada628b2010-05-24 12:08:16 +020051 {NULL, 0, NULL, 0}
52};
53
Andreas Fenkart24371902016-04-05 23:13:42 +020054static struct env_opts env_opts;
55
56/* setenv options */
57static int noheader;
58
59/* getenv options */
60static char *script_file;
61
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +010062void usage_printenv(void)
Stefano Babicada628b2010-05-24 12:08:16 +020063{
64
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +010065 fprintf(stderr,
66 "Usage: fw_printenv [OPTIONS]... [VARIABLE]...\n"
67 "Print variables from U-Boot environment\n"
68 "\n"
69 " -h, --help print this help.\n"
Stefan Agnere5308f82017-10-26 14:57:53 +020070 " -v, --version display version\n"
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +010071#ifdef CONFIG_FILE
72 " -c, --config configuration file, default:" CONFIG_FILE "\n"
73#endif
74 " -n, --noheader do not repeat variable name in output\n"
B, Raviccb293f2016-09-26 18:24:08 +053075 " -l, --lock lock node, default:/var/lock\n"
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +010076 "\n");
77}
78
Simon Glass6a38e412017-08-03 12:22:09 -060079void usage_env_set(void)
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +010080{
81 fprintf(stderr,
82 "Usage: fw_setenv [OPTIONS]... [VARIABLE]...\n"
83 "Modify variables in U-Boot environment\n"
84 "\n"
85 " -h, --help print this help.\n"
Stefan Agnere5308f82017-10-26 14:57:53 +020086 " -v, --version display version\n"
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +010087#ifdef CONFIG_FILE
88 " -c, --config configuration file, default:" CONFIG_FILE "\n"
89#endif
B, Raviccb293f2016-09-26 18:24:08 +053090 " -l, --lock lock node, default:/var/lock\n"
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +010091 " -s, --script batch mode to minimize writes\n"
92 "\n"
93 "Examples:\n"
94 " fw_setenv foo bar set variable foo equal bar\n"
95 " fw_setenv foo clear variable foo\n"
96 " fw_setenv --script file run batch script\n"
Stefano Babicada628b2010-05-24 12:08:16 +020097 "\n"
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +010098 "Script Syntax:\n"
99 " key [space] value\n"
Vagrant Cascadian34645662016-03-15 12:11:12 -0700100 " lines starting with '#' are treated as comment\n"
Stefano Babicada628b2010-05-24 12:08:16 +0200101 "\n"
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +0100102 " A variable without value will be deleted. Any number of spaces are\n"
103 " allowed between key and value. Space inside of the value is treated\n"
104 " as part of the value itself.\n"
105 "\n"
106 "Script Example:\n"
107 " netdev eth0\n"
108 " kernel_addr 400000\n"
109 " foo empty empty empty empty empty empty\n"
110 " bar\n"
111 "\n");
Stefano Babicada628b2010-05-24 12:08:16 +0200112}
113
Andreas Fenkart6726fc42015-12-09 13:13:26 +0100114static void parse_common_args(int argc, char *argv[])
wdenk34b613e2002-12-17 01:51:00 +0000115{
Stefano Babicada628b2010-05-24 12:08:16 +0200116 int c;
wdenk34b613e2002-12-17 01:51:00 +0000117
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +0100118#ifdef CONFIG_FILE
Andreas Fenkart24371902016-04-05 23:13:42 +0200119 env_opts.config_file = CONFIG_FILE;
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +0100120#endif
121
Stefan Agnere5308f82017-10-26 14:57:53 +0200122 while ((c = getopt_long(argc, argv, ":a:c:l:h:v", long_options, NULL)) !=
Andreas Fenkart6726fc42015-12-09 13:13:26 +0100123 EOF) {
Stefano Babicada628b2010-05-24 12:08:16 +0200124 switch (c) {
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +0100125#ifdef CONFIG_FILE
Michael Heimpold211e17b2015-10-27 22:34:49 +0100126 case 'c':
Andreas Fenkart24371902016-04-05 23:13:42 +0200127 env_opts.config_file = optarg;
Michael Heimpold211e17b2015-10-27 22:34:49 +0100128 break;
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +0100129#endif
B, Raviccb293f2016-09-26 18:24:08 +0530130 case 'l':
131 env_opts.lockname = optarg;
132 break;
Andreas Fenkart6726fc42015-12-09 13:13:26 +0100133 case 'h':
Simon Glass6a38e412017-08-03 12:22:09 -0600134 do_printenv ? usage_printenv() : usage_env_set();
Andreas Fenkart6726fc42015-12-09 13:13:26 +0100135 exit(EXIT_SUCCESS);
136 break;
Stefan Agnere5308f82017-10-26 14:57:53 +0200137 case 'v':
138 fprintf(stderr, "Compiled with " U_BOOT_VERSION "\n");
139 exit(EXIT_SUCCESS);
140 break;
Andreas Fenkart6726fc42015-12-09 13:13:26 +0100141 default:
142 /* ignore unknown options */
143 break;
144 }
145 }
146
147 /* Reset getopt for the next pass. */
148 opterr = 1;
149 optind = 1;
150}
151
152int parse_printenv_args(int argc, char *argv[])
153{
154 int c;
155
156 parse_common_args(argc, argv);
157
Stefan Agnere5308f82017-10-26 14:57:53 +0200158 while ((c = getopt_long(argc, argv, "a:c:ns:l:h:v", long_options, NULL))
B, Raviccb293f2016-09-26 18:24:08 +0530159 != EOF) {
Andreas Fenkart6726fc42015-12-09 13:13:26 +0100160 switch (c) {
Daniel Hobi6c554262010-09-15 19:46:26 +0200161 case 'n':
Andreas Fenkart24371902016-04-05 23:13:42 +0200162 noheader = 1;
Daniel Hobi6c554262010-09-15 19:46:26 +0200163 break;
Andreas Fenkart6726fc42015-12-09 13:13:26 +0100164 case 'a':
165 case 'c':
Andreas Fenkart33e91772015-12-09 13:13:23 +0100166 case 'h':
B, Raviccb293f2016-09-26 18:24:08 +0530167 case 'l':
Andreas Fenkart6726fc42015-12-09 13:13:26 +0100168 /* ignore common options */
Andreas Fenkart33e91772015-12-09 13:13:23 +0100169 break;
170 default: /* '?' */
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +0100171 usage_printenv();
Andreas Fenkart33e91772015-12-09 13:13:23 +0100172 exit(EXIT_FAILURE);
173 break;
174 }
175 }
176 return 0;
177}
178
179int parse_setenv_args(int argc, char *argv[])
180{
181 int c;
182
Andreas Fenkart6726fc42015-12-09 13:13:26 +0100183 parse_common_args(argc, argv);
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +0100184
Stefan Agnere5308f82017-10-26 14:57:53 +0200185 while ((c = getopt_long(argc, argv, "a:c:ns:l:h:v", long_options, NULL))
B, Raviccb293f2016-09-26 18:24:08 +0530186 != EOF) {
Andreas Fenkart33e91772015-12-09 13:13:23 +0100187 switch (c) {
Stefano Babicada628b2010-05-24 12:08:16 +0200188 case 's':
Andreas Fenkart24371902016-04-05 23:13:42 +0200189 script_file = optarg;
Stefano Babicada628b2010-05-24 12:08:16 +0200190 break;
Andreas Fenkart6726fc42015-12-09 13:13:26 +0100191 case 'a':
192 case 'c':
Stefano Babicada628b2010-05-24 12:08:16 +0200193 case 'h':
B, Raviccb293f2016-09-26 18:24:08 +0530194 case 'l':
Andreas Fenkart6726fc42015-12-09 13:13:26 +0100195 /* ignore common options */
Andreas Fenkart33e91772015-12-09 13:13:23 +0100196 break;
Daniel Hobi6a20a322010-09-16 14:36:09 +0200197 default: /* '?' */
Simon Glass6a38e412017-08-03 12:22:09 -0600198 usage_env_set();
Andreas Fenkart33e91772015-12-09 13:13:23 +0100199 exit(EXIT_FAILURE);
200 break;
Stefano Babicada628b2010-05-24 12:08:16 +0200201 }
202 }
Andreas Fenkart33e91772015-12-09 13:13:23 +0100203 return 0;
204}
205
206int main(int argc, char *argv[])
207{
B, Raviccb293f2016-09-26 18:24:08 +0530208 char *lockname = "/var/lock/" CMD_PRINTENV ".lock";
Andreas Fenkart33e91772015-12-09 13:13:23 +0100209 int lockfd = -1;
210 int retval = EXIT_SUCCESS;
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +0100211 char *_cmdname;
Andreas Fenkart33e91772015-12-09 13:13:23 +0100212
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +0100213 _cmdname = *argv;
214 if (strrchr(_cmdname, '/') != NULL)
215 _cmdname = strrchr(_cmdname, '/') + 1;
Stefano Babicada628b2010-05-24 12:08:16 +0200216
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +0100217 if (strcmp(_cmdname, CMD_PRINTENV) == 0) {
218 do_printenv = 1;
219 } else if (strcmp(_cmdname, CMD_SETENV) == 0) {
220 do_printenv = 0;
Andreas Fenkart33e91772015-12-09 13:13:23 +0100221 } else {
222 fprintf(stderr,
223 "Identity crisis - may be called as `%s' or as `%s' but not as `%s'\n",
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +0100224 CMD_PRINTENV, CMD_SETENV, _cmdname);
Andreas Fenkart33e91772015-12-09 13:13:23 +0100225 exit(EXIT_FAILURE);
226 }
227
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +0100228 if (do_printenv) {
229 if (parse_printenv_args(argc, argv))
230 exit(EXIT_FAILURE);
231 } else {
232 if (parse_setenv_args(argc, argv))
233 exit(EXIT_FAILURE);
234 }
235
Andreas Fenkart14908ef2015-12-09 13:13:25 +0100236 /* shift parsed flags, jump to non-option arguments */
237 argc -= optind;
238 argv += optind;
239
B, Raviccb293f2016-09-26 18:24:08 +0530240 if (env_opts.lockname) {
Kristian Amliec0855f22018-04-04 10:09:57 +0200241 lockname = malloc(strlen(env_opts.lockname) +
B, Raviccb293f2016-09-26 18:24:08 +0530242 sizeof(CMD_PRINTENV) + 10);
243 if (!lockname) {
244 fprintf(stderr, "Unable allocate memory");
245 exit(EXIT_FAILURE);
246 }
247
248 sprintf(lockname, "%s/%s.lock",
249 env_opts.lockname, CMD_PRINTENV);
250 }
251
Andreas Fenkart33e91772015-12-09 13:13:23 +0100252 lockfd = open(lockname, O_WRONLY | O_CREAT | O_TRUNC, 0666);
253 if (-1 == lockfd) {
254 fprintf(stderr, "Error opening lock file %s\n", lockname);
255 return EXIT_FAILURE;
256 }
257
258 if (-1 == flock(lockfd, LOCK_EX)) {
259 fprintf(stderr, "Error locking file %s\n", lockname);
260 close(lockfd);
261 return EXIT_FAILURE;
262 }
263
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +0100264 if (do_printenv) {
Andreas Fenkart24371902016-04-05 23:13:42 +0200265 if (fw_printenv(argc, argv, noheader, &env_opts) != 0)
Joe Hershberger3f1f1752012-10-03 09:38:49 +0000266 retval = EXIT_FAILURE;
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +0100267 } else {
Andreas Fenkart24371902016-04-05 23:13:42 +0200268 if (!script_file) {
Simon Glass6a38e412017-08-03 12:22:09 -0600269 if (fw_env_set(argc, argv, &env_opts) != 0)
Joe Hershberger3f1f1752012-10-03 09:38:49 +0000270 retval = EXIT_FAILURE;
Stefano Babicada628b2010-05-24 12:08:16 +0200271 } else {
Andreas Fenkart24371902016-04-05 23:13:42 +0200272 if (fw_parse_script(script_file, &env_opts) != 0)
Joe Hershberger3f1f1752012-10-03 09:38:49 +0000273 retval = EXIT_FAILURE;
Stefano Babicada628b2010-05-24 12:08:16 +0200274 }
wdenk34b613e2002-12-17 01:51:00 +0000275 }
276
B, Raviccb293f2016-09-26 18:24:08 +0530277 if (env_opts.lockname)
278 free(lockname);
279
Joe Hershberger3f1f1752012-10-03 09:38:49 +0000280 flock(lockfd, LOCK_UN);
281 close(lockfd);
282 return retval;
wdenk34b613e2002-12-17 01:51:00 +0000283}