blob: 26ba6624cd19cb2be078e683122c95506ca11d26 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk6aff3112002-12-17 01:51:00 +00002/*
Grant Ericksonbc117562008-05-06 20:16:15 -07003 * (C) Copyright 2000-2008
wdenk6aff3112002-12-17 01:51:00 +00004 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
wdenk6aff3112002-12-17 01:51:00 +00005 */
6
7/*
wdenk3bac3512003-03-12 10:41:04 +00008 * Command line user interface to firmware (=U-Boot) environment.
wdenk6aff3112002-12-17 01:51:00 +00009 *
10 * Implements:
Marek Vasuta8a752c2014-03-05 19:59:52 +010011 * fw_printenv [ -a key ] [[ -n name ] | [ name ... ]]
Grant Ericksonbc117562008-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 Vasuta8a752c2014-03-05 19:59:52 +010016 * fw_setenv [ -a key ] name [ value ... ]
wdenk6aff3112002-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 Ericksonbc117562008-05-06 20:16:15 -070020 * separated by single blank characters, and the
wdenk6aff3112002-12-17 01:51:00 +000021 * resulting string is assigned to the environment
22 * variable "name"
Marek Vasuta8a752c2014-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'.
wdenk6aff3112002-12-17 01:51:00 +000027 */
28
Joe Hershbergere4a223f2012-10-03 09:38:49 +000029#include <fcntl.h>
30#include <getopt.h>
wdenk6aff3112002-12-17 01:51:00 +000031#include <stdio.h>
32#include <string.h>
33#include <stdlib.h>
Joe Hershbergere4a223f2012-10-03 09:38:49 +000034#include <sys/file.h>
35#include <unistd.h>
Stefan Agner84d46e72017-10-26 14:57:53 +020036#include <version.h>
Stefano Babic9d80b492017-04-05 18:08:01 +020037#include "fw_env_private.h"
wdenk6aff3112002-12-17 01:51:00 +000038#include "fw_env.h"
39
Andreas Fenkartb92ae3a2015-12-09 13:13:27 +010040#define CMD_PRINTENV "fw_printenv"
wdenk6aff3112002-12-17 01:51:00 +000041#define CMD_SETENV "fw_setenv"
Andreas Fenkartb92ae3a2015-12-09 13:13:27 +010042static int do_printenv;
wdenk6aff3112002-12-17 01:51:00 +000043
Stefano Babicbd7b26f2010-05-24 12:08:16 +020044static struct option long_options[] = {
Andreas Fenkartb92ae3a2015-12-09 13:13:27 +010045 {"config", required_argument, NULL, 'c'},
Stefano Babicbd7b26f2010-05-24 12:08:16 +020046 {"help", no_argument, NULL, 'h'},
Andreas Fenkartb92ae3a2015-12-09 13:13:27 +010047 {"script", required_argument, NULL, 's'},
Alex Kiernand877a6c2018-02-11 17:16:46 +000048 {"noheader", no_argument, NULL, 'n'},
B, Ravid40dbfb2016-09-26 18:24:08 +053049 {"lock", required_argument, NULL, 'l'},
Stefan Agner84d46e72017-10-26 14:57:53 +020050 {"version", no_argument, NULL, 'v'},
Stefano Babicbd7b26f2010-05-24 12:08:16 +020051 {NULL, 0, NULL, 0}
52};
53
Andreas Fenkart81974f42016-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 Fenkartb92ae3a2015-12-09 13:13:27 +010062void usage_printenv(void)
Stefano Babicbd7b26f2010-05-24 12:08:16 +020063{
64
Andreas Fenkartb92ae3a2015-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 Agner84d46e72017-10-26 14:57:53 +020070 " -v, --version display version\n"
Andreas Fenkartb92ae3a2015-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, Ravid40dbfb2016-09-26 18:24:08 +053075 " -l, --lock lock node, default:/var/lock\n"
Andreas Fenkartb92ae3a2015-12-09 13:13:27 +010076 "\n");
77}
78
Simon Glass382bee52017-08-03 12:22:09 -060079void usage_env_set(void)
Andreas Fenkartb92ae3a2015-12-09 13:13:27 +010080{
81 fprintf(stderr,
82 "Usage: fw_setenv [OPTIONS]... [VARIABLE]...\n"
83 "Modify variables in U-Boot environment\n"
Stefano Babicbd7b26f2010-05-24 12:08:16 +020084 "\n"
Andreas Fenkartb92ae3a2015-12-09 13:13:27 +010085 " -h, --help print this help.\n"
Stefan Agner84d46e72017-10-26 14:57:53 +020086 " -v, --version display version\n"
Andreas Fenkartb92ae3a2015-12-09 13:13:27 +010087#ifdef CONFIG_FILE
88 " -c, --config configuration file, default:" CONFIG_FILE "\n"
89#endif
B, Ravid40dbfb2016-09-26 18:24:08 +053090 " -l, --lock lock node, default:/var/lock\n"
Andreas Fenkartb92ae3a2015-12-09 13:13:27 +010091 " -s, --script batch mode to minimize writes\n"
Stefano Babicbd7b26f2010-05-24 12:08:16 +020092 "\n"
Andreas Fenkartb92ae3a2015-12-09 13:13:27 +010093 "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"
97 "\n"
98 "Script Syntax:\n"
99 " key [space] value\n"
Vagrant Cascadian0e280652016-03-15 12:11:12 -0700100 " lines starting with '#' are treated as comment\n"
Andreas Fenkartb92ae3a2015-12-09 13:13:27 +0100101 "\n"
102 " 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 Babicbd7b26f2010-05-24 12:08:16 +0200112}
113
Andreas Fenkartaf93e3d2015-12-09 13:13:26 +0100114static void parse_common_args(int argc, char *argv[])
wdenk6aff3112002-12-17 01:51:00 +0000115{
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200116 int c;
wdenk6aff3112002-12-17 01:51:00 +0000117
Andreas Fenkart371ee132015-12-09 13:13:24 +0100118#ifdef CONFIG_FILE
Andreas Fenkart81974f42016-04-05 23:13:42 +0200119 env_opts.config_file = CONFIG_FILE;
Andreas Fenkart371ee132015-12-09 13:13:24 +0100120#endif
121
Stefan Agner84d46e72017-10-26 14:57:53 +0200122 while ((c = getopt_long(argc, argv, ":a:c:l:h:v", long_options, NULL)) !=
Andreas Fenkartaf93e3d2015-12-09 13:13:26 +0100123 EOF) {
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200124 switch (c) {
Andreas Fenkart371ee132015-12-09 13:13:24 +0100125#ifdef CONFIG_FILE
Michael Heimpold9884f442015-10-27 22:34:49 +0100126 case 'c':
Andreas Fenkart81974f42016-04-05 23:13:42 +0200127 env_opts.config_file = optarg;
Michael Heimpold9884f442015-10-27 22:34:49 +0100128 break;
Andreas Fenkart371ee132015-12-09 13:13:24 +0100129#endif
B, Ravid40dbfb2016-09-26 18:24:08 +0530130 case 'l':
131 env_opts.lockname = optarg;
132 break;
Andreas Fenkart07ce9442015-12-09 13:13:23 +0100133 case 'h':
Simon Glass382bee52017-08-03 12:22:09 -0600134 do_printenv ? usage_printenv() : usage_env_set();
Andreas Fenkart07ce9442015-12-09 13:13:23 +0100135 exit(EXIT_SUCCESS);
136 break;
Stefan Agner84d46e72017-10-26 14:57:53 +0200137 case 'v':
138 fprintf(stderr, "Compiled with " U_BOOT_VERSION "\n");
139 exit(EXIT_SUCCESS);
140 break;
Andreas Fenkartaf93e3d2015-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 Agner84d46e72017-10-26 14:57:53 +0200158 while ((c = getopt_long(argc, argv, "a:c:ns:l:h:v", long_options, NULL))
B, Ravid40dbfb2016-09-26 18:24:08 +0530159 != EOF) {
Andreas Fenkartaf93e3d2015-12-09 13:13:26 +0100160 switch (c) {
161 case 'n':
Andreas Fenkart81974f42016-04-05 23:13:42 +0200162 noheader = 1;
Andreas Fenkartaf93e3d2015-12-09 13:13:26 +0100163 break;
164 case 'a':
165 case 'c':
166 case 'h':
B, Ravid40dbfb2016-09-26 18:24:08 +0530167 case 'l':
Andreas Fenkartaf93e3d2015-12-09 13:13:26 +0100168 /* ignore common options */
169 break;
Andreas Fenkart07ce9442015-12-09 13:13:23 +0100170 default: /* '?' */
Andreas Fenkartb92ae3a2015-12-09 13:13:27 +0100171 usage_printenv();
Andreas Fenkart07ce9442015-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 Fenkartaf93e3d2015-12-09 13:13:26 +0100183 parse_common_args(argc, argv);
Andreas Fenkart371ee132015-12-09 13:13:24 +0100184
Stefan Agner84d46e72017-10-26 14:57:53 +0200185 while ((c = getopt_long(argc, argv, "a:c:ns:l:h:v", long_options, NULL))
B, Ravid40dbfb2016-09-26 18:24:08 +0530186 != EOF) {
Andreas Fenkart07ce9442015-12-09 13:13:23 +0100187 switch (c) {
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200188 case 's':
Andreas Fenkart81974f42016-04-05 23:13:42 +0200189 script_file = optarg;
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200190 break;
Andreas Fenkartaf93e3d2015-12-09 13:13:26 +0100191 case 'a':
192 case 'c':
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200193 case 'h':
B, Ravid40dbfb2016-09-26 18:24:08 +0530194 case 'l':
Andreas Fenkartaf93e3d2015-12-09 13:13:26 +0100195 /* ignore common options */
Andreas Fenkart07ce9442015-12-09 13:13:23 +0100196 break;
Daniel Hobi29ccd7c2010-09-16 14:36:09 +0200197 default: /* '?' */
Simon Glass382bee52017-08-03 12:22:09 -0600198 usage_env_set();
Andreas Fenkart07ce9442015-12-09 13:13:23 +0100199 exit(EXIT_FAILURE);
200 break;
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200201 }
202 }
Andreas Fenkart07ce9442015-12-09 13:13:23 +0100203 return 0;
204}
205
206int main(int argc, char *argv[])
207{
B, Ravid40dbfb2016-09-26 18:24:08 +0530208 char *lockname = "/var/lock/" CMD_PRINTENV ".lock";
Andreas Fenkart07ce9442015-12-09 13:13:23 +0100209 int lockfd = -1;
210 int retval = EXIT_SUCCESS;
Andreas Fenkartb92ae3a2015-12-09 13:13:27 +0100211 char *_cmdname;
Andreas Fenkart07ce9442015-12-09 13:13:23 +0100212
Andreas Fenkartb92ae3a2015-12-09 13:13:27 +0100213 _cmdname = *argv;
214 if (strrchr(_cmdname, '/') != NULL)
215 _cmdname = strrchr(_cmdname, '/') + 1;
Andreas Fenkart07ce9442015-12-09 13:13:23 +0100216
Andreas Fenkartb92ae3a2015-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 Fenkart07ce9442015-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 Fenkartb92ae3a2015-12-09 13:13:27 +0100224 CMD_PRINTENV, CMD_SETENV, _cmdname);
Andreas Fenkart07ce9442015-12-09 13:13:23 +0100225 exit(EXIT_FAILURE);
226 }
227
Andreas Fenkartb92ae3a2015-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 Fenkart1ce68692015-12-09 13:13:25 +0100236 /* shift parsed flags, jump to non-option arguments */
237 argc -= optind;
238 argv += optind;
239
B, Ravid40dbfb2016-09-26 18:24:08 +0530240 if (env_opts.lockname) {
Kristian Amlie8a0b8272018-04-04 10:09:57 +0200241 lockname = malloc(strlen(env_opts.lockname) +
B, Ravid40dbfb2016-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 Fenkart07ce9442015-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 }
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200263
Andreas Fenkartb92ae3a2015-12-09 13:13:27 +0100264 if (do_printenv) {
Andreas Fenkart81974f42016-04-05 23:13:42 +0200265 if (fw_printenv(argc, argv, noheader, &env_opts) != 0)
Joe Hershbergere4a223f2012-10-03 09:38:49 +0000266 retval = EXIT_FAILURE;
Andreas Fenkartb92ae3a2015-12-09 13:13:27 +0100267 } else {
Andreas Fenkart81974f42016-04-05 23:13:42 +0200268 if (!script_file) {
Simon Glass382bee52017-08-03 12:22:09 -0600269 if (fw_env_set(argc, argv, &env_opts) != 0)
Joe Hershbergere4a223f2012-10-03 09:38:49 +0000270 retval = EXIT_FAILURE;
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200271 } else {
Andreas Fenkart81974f42016-04-05 23:13:42 +0200272 if (fw_parse_script(script_file, &env_opts) != 0)
Joe Hershbergere4a223f2012-10-03 09:38:49 +0000273 retval = EXIT_FAILURE;
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200274 }
wdenk6aff3112002-12-17 01:51:00 +0000275 }
276
B, Ravid40dbfb2016-09-26 18:24:08 +0530277 if (env_opts.lockname)
278 free(lockname);
279
Joe Hershbergere4a223f2012-10-03 09:38:49 +0000280 flock(lockfd, LOCK_UN);
281 close(lockfd);
282 return retval;
wdenk6aff3112002-12-17 01:51:00 +0000283}