blob: 443de36e437e063bfdd55624d0472c0aaa61639f [file] [log] [blame]
wdenk6aff3112002-12-17 01:51:00 +00001/*
Grant Ericksonbc117562008-05-06 20:16:15 -07002 * (C) Copyright 2000-2008
wdenk6aff3112002-12-17 01:51:00 +00003 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
Wolfgang Denk3765b3e2013-10-07 13:07:26 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenk6aff3112002-12-17 01:51:00 +00006 */
7
8/*
wdenk3bac3512003-03-12 10:41:04 +00009 * Command line user interface to firmware (=U-Boot) environment.
wdenk6aff3112002-12-17 01:51:00 +000010 *
11 * Implements:
Marek Vasuta8a752c2014-03-05 19:59:52 +010012 * fw_printenv [ -a key ] [[ -n name ] | [ name ... ]]
Grant Ericksonbc117562008-05-06 20:16:15 -070013 * - 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.
Marek Vasuta8a752c2014-03-05 19:59:52 +010017 * fw_setenv [ -a key ] name [ value ... ]
wdenk6aff3112002-12-17 01:51:00 +000018 * - 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,
Grant Ericksonbc117562008-05-06 20:16:15 -070021 * separated by single blank characters, and the
wdenk6aff3112002-12-17 01:51:00 +000022 * resulting string is assigned to the environment
23 * variable "name"
Marek Vasuta8a752c2014-03-05 19:59:52 +010024 *
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'.
wdenk6aff3112002-12-17 01:51:00 +000028 */
29
Joe Hershbergere4a223f2012-10-03 09:38:49 +000030#include <fcntl.h>
31#include <getopt.h>
wdenk6aff3112002-12-17 01:51:00 +000032#include <stdio.h>
33#include <string.h>
34#include <stdlib.h>
Joe Hershbergere4a223f2012-10-03 09:38:49 +000035#include <sys/file.h>
36#include <unistd.h>
wdenk6aff3112002-12-17 01:51:00 +000037#include "fw_env.h"
38
Andreas Fenkartb92ae3a2015-12-09 13:13:27 +010039#define CMD_PRINTENV "fw_printenv"
wdenk6aff3112002-12-17 01:51:00 +000040#define CMD_SETENV "fw_setenv"
Andreas Fenkartb92ae3a2015-12-09 13:13:27 +010041static int do_printenv;
wdenk6aff3112002-12-17 01:51:00 +000042
Stefano Babicbd7b26f2010-05-24 12:08:16 +020043static struct option long_options[] = {
Andreas Fenkartb92ae3a2015-12-09 13:13:27 +010044 {"aes", required_argument, NULL, 'a'},
45 {"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'},
48 {"noheader", required_argument, NULL, 'n'},
B, Ravid40dbfb2016-09-26 18:24:08 +053049 {"lock", required_argument, NULL, 'l'},
Stefano Babicbd7b26f2010-05-24 12:08:16 +020050 {NULL, 0, NULL, 0}
51};
52
Andreas Fenkart81974f42016-04-05 23:13:42 +020053static struct env_opts env_opts;
54
55/* setenv options */
56static int noheader;
57
58/* getenv options */
59static char *script_file;
60
Andreas Fenkartb92ae3a2015-12-09 13:13:27 +010061void usage_printenv(void)
Stefano Babicbd7b26f2010-05-24 12:08:16 +020062{
63
Andreas Fenkartb92ae3a2015-12-09 13:13:27 +010064 fprintf(stderr,
65 "Usage: fw_printenv [OPTIONS]... [VARIABLE]...\n"
66 "Print variables from U-Boot environment\n"
67 "\n"
68 " -h, --help print this help.\n"
69#ifdef CONFIG_ENV_AES
70 " -a, --aes aes key to access environment\n"
Michael Heimpold9884f442015-10-27 22:34:49 +010071#endif
Andreas Fenkartb92ae3a2015-12-09 13:13:27 +010072#ifdef CONFIG_FILE
73 " -c, --config configuration file, default:" CONFIG_FILE "\n"
74#endif
75 " -n, --noheader do not repeat variable name in output\n"
B, Ravid40dbfb2016-09-26 18:24:08 +053076 " -l, --lock lock node, default:/var/lock\n"
Andreas Fenkartb92ae3a2015-12-09 13:13:27 +010077 "\n");
78}
79
80void usage_setenv(void)
81{
82 fprintf(stderr,
83 "Usage: fw_setenv [OPTIONS]... [VARIABLE]...\n"
84 "Modify variables in U-Boot environment\n"
Stefano Babicbd7b26f2010-05-24 12:08:16 +020085 "\n"
Andreas Fenkartb92ae3a2015-12-09 13:13:27 +010086 " -h, --help print this help.\n"
87#ifdef CONFIG_ENV_AES
88 " -a, --aes aes key to access environment\n"
89#endif
90#ifdef CONFIG_FILE
91 " -c, --config configuration file, default:" CONFIG_FILE "\n"
92#endif
B, Ravid40dbfb2016-09-26 18:24:08 +053093 " -l, --lock lock node, default:/var/lock\n"
Andreas Fenkartb92ae3a2015-12-09 13:13:27 +010094 " -s, --script batch mode to minimize writes\n"
Stefano Babicbd7b26f2010-05-24 12:08:16 +020095 "\n"
Andreas Fenkartb92ae3a2015-12-09 13:13:27 +010096 "Examples:\n"
97 " fw_setenv foo bar set variable foo equal bar\n"
98 " fw_setenv foo clear variable foo\n"
99 " fw_setenv --script file run batch script\n"
100 "\n"
101 "Script Syntax:\n"
102 " key [space] value\n"
Vagrant Cascadian0e280652016-03-15 12:11:12 -0700103 " lines starting with '#' are treated as comment\n"
Andreas Fenkartb92ae3a2015-12-09 13:13:27 +0100104 "\n"
105 " A variable without value will be deleted. Any number of spaces are\n"
106 " allowed between key and value. Space inside of the value is treated\n"
107 " as part of the value itself.\n"
108 "\n"
109 "Script Example:\n"
110 " netdev eth0\n"
111 " kernel_addr 400000\n"
112 " foo empty empty empty empty empty empty\n"
113 " bar\n"
114 "\n");
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200115}
116
Andreas Fenkartaf93e3d2015-12-09 13:13:26 +0100117static void parse_common_args(int argc, char *argv[])
wdenk6aff3112002-12-17 01:51:00 +0000118{
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200119 int c;
wdenk6aff3112002-12-17 01:51:00 +0000120
Andreas Fenkart371ee132015-12-09 13:13:24 +0100121#ifdef CONFIG_FILE
Andreas Fenkart81974f42016-04-05 23:13:42 +0200122 env_opts.config_file = CONFIG_FILE;
Andreas Fenkart371ee132015-12-09 13:13:24 +0100123#endif
124
B, Ravid40dbfb2016-09-26 18:24:08 +0530125 while ((c = getopt_long(argc, argv, ":a:c:l:h", long_options, NULL)) !=
Andreas Fenkartaf93e3d2015-12-09 13:13:26 +0100126 EOF) {
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200127 switch (c) {
Marek Vasuta8a752c2014-03-05 19:59:52 +0100128 case 'a':
Andreas Fenkart81974f42016-04-05 23:13:42 +0200129 if (parse_aes_key(optarg, env_opts.aes_key)) {
Andreas Fenkart371ee132015-12-09 13:13:24 +0100130 fprintf(stderr, "AES key parse error\n");
Andreas Fenkartaf93e3d2015-12-09 13:13:26 +0100131 exit(EXIT_FAILURE);
Andreas Fenkart371ee132015-12-09 13:13:24 +0100132 }
Andreas Fenkart81974f42016-04-05 23:13:42 +0200133 env_opts.aes_flag = 1;
Marek Vasuta8a752c2014-03-05 19:59:52 +0100134 break;
Andreas Fenkart371ee132015-12-09 13:13:24 +0100135#ifdef CONFIG_FILE
Michael Heimpold9884f442015-10-27 22:34:49 +0100136 case 'c':
Andreas Fenkart81974f42016-04-05 23:13:42 +0200137 env_opts.config_file = optarg;
Michael Heimpold9884f442015-10-27 22:34:49 +0100138 break;
Andreas Fenkart371ee132015-12-09 13:13:24 +0100139#endif
B, Ravid40dbfb2016-09-26 18:24:08 +0530140 case 'l':
141 env_opts.lockname = optarg;
142 break;
Andreas Fenkart07ce9442015-12-09 13:13:23 +0100143 case 'h':
Andreas Fenkartb92ae3a2015-12-09 13:13:27 +0100144 do_printenv ? usage_printenv() : usage_setenv();
Andreas Fenkart07ce9442015-12-09 13:13:23 +0100145 exit(EXIT_SUCCESS);
146 break;
Andreas Fenkartaf93e3d2015-12-09 13:13:26 +0100147 default:
148 /* ignore unknown options */
149 break;
150 }
151 }
152
153 /* Reset getopt for the next pass. */
154 opterr = 1;
155 optind = 1;
156}
157
158int parse_printenv_args(int argc, char *argv[])
159{
160 int c;
161
162 parse_common_args(argc, argv);
163
B, Ravid40dbfb2016-09-26 18:24:08 +0530164 while ((c = getopt_long(argc, argv, "a:c:ns:l:h", long_options, NULL))
165 != EOF) {
Andreas Fenkartaf93e3d2015-12-09 13:13:26 +0100166 switch (c) {
167 case 'n':
Andreas Fenkart81974f42016-04-05 23:13:42 +0200168 noheader = 1;
Andreas Fenkartaf93e3d2015-12-09 13:13:26 +0100169 break;
170 case 'a':
171 case 'c':
172 case 'h':
B, Ravid40dbfb2016-09-26 18:24:08 +0530173 case 'l':
Andreas Fenkartaf93e3d2015-12-09 13:13:26 +0100174 /* ignore common options */
175 break;
Andreas Fenkart07ce9442015-12-09 13:13:23 +0100176 default: /* '?' */
Andreas Fenkartb92ae3a2015-12-09 13:13:27 +0100177 usage_printenv();
Andreas Fenkart07ce9442015-12-09 13:13:23 +0100178 exit(EXIT_FAILURE);
179 break;
180 }
181 }
182 return 0;
183}
184
185int parse_setenv_args(int argc, char *argv[])
186{
187 int c;
188
Andreas Fenkartaf93e3d2015-12-09 13:13:26 +0100189 parse_common_args(argc, argv);
Andreas Fenkart371ee132015-12-09 13:13:24 +0100190
B, Ravid40dbfb2016-09-26 18:24:08 +0530191 while ((c = getopt_long(argc, argv, "a:c:ns:l:h", long_options, NULL))
192 != EOF) {
Andreas Fenkart07ce9442015-12-09 13:13:23 +0100193 switch (c) {
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200194 case 's':
Andreas Fenkart81974f42016-04-05 23:13:42 +0200195 script_file = optarg;
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200196 break;
Andreas Fenkartaf93e3d2015-12-09 13:13:26 +0100197 case 'a':
198 case 'c':
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200199 case 'h':
B, Ravid40dbfb2016-09-26 18:24:08 +0530200 case 'l':
Andreas Fenkartaf93e3d2015-12-09 13:13:26 +0100201 /* ignore common options */
Andreas Fenkart07ce9442015-12-09 13:13:23 +0100202 break;
Daniel Hobi29ccd7c2010-09-16 14:36:09 +0200203 default: /* '?' */
Andreas Fenkartb92ae3a2015-12-09 13:13:27 +0100204 usage_setenv();
Andreas Fenkart07ce9442015-12-09 13:13:23 +0100205 exit(EXIT_FAILURE);
206 break;
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200207 }
208 }
Andreas Fenkart07ce9442015-12-09 13:13:23 +0100209 return 0;
210}
211
212int main(int argc, char *argv[])
213{
B, Ravid40dbfb2016-09-26 18:24:08 +0530214 char *lockname = "/var/lock/" CMD_PRINTENV ".lock";
Andreas Fenkart07ce9442015-12-09 13:13:23 +0100215 int lockfd = -1;
216 int retval = EXIT_SUCCESS;
Andreas Fenkartb92ae3a2015-12-09 13:13:27 +0100217 char *_cmdname;
Andreas Fenkart07ce9442015-12-09 13:13:23 +0100218
Andreas Fenkartb92ae3a2015-12-09 13:13:27 +0100219 _cmdname = *argv;
220 if (strrchr(_cmdname, '/') != NULL)
221 _cmdname = strrchr(_cmdname, '/') + 1;
Andreas Fenkart07ce9442015-12-09 13:13:23 +0100222
Andreas Fenkartb92ae3a2015-12-09 13:13:27 +0100223 if (strcmp(_cmdname, CMD_PRINTENV) == 0) {
224 do_printenv = 1;
225 } else if (strcmp(_cmdname, CMD_SETENV) == 0) {
226 do_printenv = 0;
Andreas Fenkart07ce9442015-12-09 13:13:23 +0100227 } else {
228 fprintf(stderr,
229 "Identity crisis - may be called as `%s' or as `%s' but not as `%s'\n",
Andreas Fenkartb92ae3a2015-12-09 13:13:27 +0100230 CMD_PRINTENV, CMD_SETENV, _cmdname);
Andreas Fenkart07ce9442015-12-09 13:13:23 +0100231 exit(EXIT_FAILURE);
232 }
233
Andreas Fenkartb92ae3a2015-12-09 13:13:27 +0100234 if (do_printenv) {
235 if (parse_printenv_args(argc, argv))
236 exit(EXIT_FAILURE);
237 } else {
238 if (parse_setenv_args(argc, argv))
239 exit(EXIT_FAILURE);
240 }
241
Andreas Fenkart1ce68692015-12-09 13:13:25 +0100242 /* shift parsed flags, jump to non-option arguments */
243 argc -= optind;
244 argv += optind;
245
B, Ravid40dbfb2016-09-26 18:24:08 +0530246 if (env_opts.lockname) {
247 lockname = malloc(sizeof(env_opts.lockname) +
248 sizeof(CMD_PRINTENV) + 10);
249 if (!lockname) {
250 fprintf(stderr, "Unable allocate memory");
251 exit(EXIT_FAILURE);
252 }
253
254 sprintf(lockname, "%s/%s.lock",
255 env_opts.lockname, CMD_PRINTENV);
256 }
257
Andreas Fenkart07ce9442015-12-09 13:13:23 +0100258 lockfd = open(lockname, O_WRONLY | O_CREAT | O_TRUNC, 0666);
259 if (-1 == lockfd) {
260 fprintf(stderr, "Error opening lock file %s\n", lockname);
261 return EXIT_FAILURE;
262 }
263
264 if (-1 == flock(lockfd, LOCK_EX)) {
265 fprintf(stderr, "Error locking file %s\n", lockname);
266 close(lockfd);
267 return EXIT_FAILURE;
268 }
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200269
Andreas Fenkartb92ae3a2015-12-09 13:13:27 +0100270 if (do_printenv) {
Andreas Fenkart81974f42016-04-05 23:13:42 +0200271 if (fw_printenv(argc, argv, noheader, &env_opts) != 0)
Joe Hershbergere4a223f2012-10-03 09:38:49 +0000272 retval = EXIT_FAILURE;
Andreas Fenkartb92ae3a2015-12-09 13:13:27 +0100273 } else {
Andreas Fenkart81974f42016-04-05 23:13:42 +0200274 if (!script_file) {
275 if (fw_setenv(argc, argv, &env_opts) != 0)
Joe Hershbergere4a223f2012-10-03 09:38:49 +0000276 retval = EXIT_FAILURE;
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200277 } else {
Andreas Fenkart81974f42016-04-05 23:13:42 +0200278 if (fw_parse_script(script_file, &env_opts) != 0)
Joe Hershbergere4a223f2012-10-03 09:38:49 +0000279 retval = EXIT_FAILURE;
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200280 }
wdenk6aff3112002-12-17 01:51:00 +0000281 }
282
B, Ravid40dbfb2016-09-26 18:24:08 +0530283 if (env_opts.lockname)
284 free(lockname);
285
Joe Hershbergere4a223f2012-10-03 09:38:49 +0000286 flock(lockfd, LOCK_UN);
287 close(lockfd);
288 return retval;
wdenk6aff3112002-12-17 01:51:00 +0000289}