blob: 443de36e437e063bfdd55624d0472c0aaa61639f [file] [log] [blame]
wdenk34b613e2002-12-17 01:51:00 +00001/*
Grant Erickson73a4e5e2008-05-06 20:16:15 -07002 * (C) Copyright 2000-2008
wdenk34b613e2002-12-17 01:51:00 +00003 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
Wolfgang Denkbd8ec7e2013-10-07 13:07:26 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenk34b613e2002-12-17 01:51:00 +00006 */
7
8/*
wdenkef5fe752003-03-12 10:41:04 +00009 * Command line user interface to firmware (=U-Boot) environment.
wdenk34b613e2002-12-17 01:51:00 +000010 *
11 * Implements:
Marek Vasut05ed2a52014-03-05 19:59:52 +010012 * fw_printenv [ -a key ] [[ -n name ] | [ name ... ]]
Grant Erickson73a4e5e2008-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 Vasut05ed2a52014-03-05 19:59:52 +010017 * fw_setenv [ -a key ] name [ value ... ]
wdenk34b613e2002-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 Erickson73a4e5e2008-05-06 20:16:15 -070021 * separated by single blank characters, and the
wdenk34b613e2002-12-17 01:51:00 +000022 * resulting string is assigned to the environment
23 * variable "name"
Marek Vasut05ed2a52014-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'.
wdenk34b613e2002-12-17 01:51:00 +000028 */
29
Joe Hershberger3f1f1752012-10-03 09:38:49 +000030#include <fcntl.h>
31#include <getopt.h>
wdenk34b613e2002-12-17 01:51:00 +000032#include <stdio.h>
33#include <string.h>
34#include <stdlib.h>
Joe Hershberger3f1f1752012-10-03 09:38:49 +000035#include <sys/file.h>
36#include <unistd.h>
wdenk34b613e2002-12-17 01:51:00 +000037#include "fw_env.h"
38
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +010039#define CMD_PRINTENV "fw_printenv"
wdenk34b613e2002-12-17 01:51:00 +000040#define CMD_SETENV "fw_setenv"
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +010041static int do_printenv;
wdenk34b613e2002-12-17 01:51:00 +000042
Stefano Babicada628b2010-05-24 12:08:16 +020043static struct option long_options[] = {
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +010044 {"aes", required_argument, NULL, 'a'},
45 {"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'},
48 {"noheader", required_argument, NULL, 'n'},
B, Raviccb293f2016-09-26 18:24:08 +053049 {"lock", required_argument, NULL, 'l'},
Stefano Babicada628b2010-05-24 12:08:16 +020050 {NULL, 0, NULL, 0}
51};
52
Andreas Fenkart24371902016-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 Fenkart9b5a28b2015-12-09 13:13:27 +010061void usage_printenv(void)
Stefano Babicada628b2010-05-24 12:08:16 +020062{
63
Andreas Fenkart9b5a28b2015-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 Heimpold211e17b2015-10-27 22:34:49 +010071#endif
Andreas Fenkart9b5a28b2015-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, Raviccb293f2016-09-26 18:24:08 +053076 " -l, --lock lock node, default:/var/lock\n"
Andreas Fenkart9b5a28b2015-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"
85 "\n"
86 " -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, Raviccb293f2016-09-26 18:24:08 +053093 " -l, --lock lock node, default:/var/lock\n"
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +010094 " -s, --script batch mode to minimize writes\n"
95 "\n"
96 "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"
Stefano Babicada628b2010-05-24 12:08:16 +0200100 "\n"
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +0100101 "Script Syntax:\n"
102 " key [space] value\n"
Vagrant Cascadian34645662016-03-15 12:11:12 -0700103 " lines starting with '#' are treated as comment\n"
Stefano Babicada628b2010-05-24 12:08:16 +0200104 "\n"
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +0100105 " 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 Babicada628b2010-05-24 12:08:16 +0200115}
116
Andreas Fenkart6726fc42015-12-09 13:13:26 +0100117static void parse_common_args(int argc, char *argv[])
wdenk34b613e2002-12-17 01:51:00 +0000118{
Stefano Babicada628b2010-05-24 12:08:16 +0200119 int c;
wdenk34b613e2002-12-17 01:51:00 +0000120
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +0100121#ifdef CONFIG_FILE
Andreas Fenkart24371902016-04-05 23:13:42 +0200122 env_opts.config_file = CONFIG_FILE;
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +0100123#endif
124
B, Raviccb293f2016-09-26 18:24:08 +0530125 while ((c = getopt_long(argc, argv, ":a:c:l:h", long_options, NULL)) !=
Andreas Fenkart6726fc42015-12-09 13:13:26 +0100126 EOF) {
Stefano Babicada628b2010-05-24 12:08:16 +0200127 switch (c) {
Marek Vasut05ed2a52014-03-05 19:59:52 +0100128 case 'a':
Andreas Fenkart24371902016-04-05 23:13:42 +0200129 if (parse_aes_key(optarg, env_opts.aes_key)) {
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +0100130 fprintf(stderr, "AES key parse error\n");
Andreas Fenkart6726fc42015-12-09 13:13:26 +0100131 exit(EXIT_FAILURE);
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +0100132 }
Andreas Fenkart24371902016-04-05 23:13:42 +0200133 env_opts.aes_flag = 1;
Marek Vasut05ed2a52014-03-05 19:59:52 +0100134 break;
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +0100135#ifdef CONFIG_FILE
Michael Heimpold211e17b2015-10-27 22:34:49 +0100136 case 'c':
Andreas Fenkart24371902016-04-05 23:13:42 +0200137 env_opts.config_file = optarg;
Michael Heimpold211e17b2015-10-27 22:34:49 +0100138 break;
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +0100139#endif
B, Raviccb293f2016-09-26 18:24:08 +0530140 case 'l':
141 env_opts.lockname = optarg;
142 break;
Andreas Fenkart6726fc42015-12-09 13:13:26 +0100143 case 'h':
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +0100144 do_printenv ? usage_printenv() : usage_setenv();
Andreas Fenkart6726fc42015-12-09 13:13:26 +0100145 exit(EXIT_SUCCESS);
146 break;
147 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, Raviccb293f2016-09-26 18:24:08 +0530164 while ((c = getopt_long(argc, argv, "a:c:ns:l:h", long_options, NULL))
165 != EOF) {
Andreas Fenkart6726fc42015-12-09 13:13:26 +0100166 switch (c) {
Daniel Hobi6c554262010-09-15 19:46:26 +0200167 case 'n':
Andreas Fenkart24371902016-04-05 23:13:42 +0200168 noheader = 1;
Daniel Hobi6c554262010-09-15 19:46:26 +0200169 break;
Andreas Fenkart6726fc42015-12-09 13:13:26 +0100170 case 'a':
171 case 'c':
Andreas Fenkart33e91772015-12-09 13:13:23 +0100172 case 'h':
B, Raviccb293f2016-09-26 18:24:08 +0530173 case 'l':
Andreas Fenkart6726fc42015-12-09 13:13:26 +0100174 /* ignore common options */
Andreas Fenkart33e91772015-12-09 13:13:23 +0100175 break;
176 default: /* '?' */
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +0100177 usage_printenv();
Andreas Fenkart33e91772015-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 Fenkart6726fc42015-12-09 13:13:26 +0100189 parse_common_args(argc, argv);
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +0100190
B, Raviccb293f2016-09-26 18:24:08 +0530191 while ((c = getopt_long(argc, argv, "a:c:ns:l:h", long_options, NULL))
192 != EOF) {
Andreas Fenkart33e91772015-12-09 13:13:23 +0100193 switch (c) {
Stefano Babicada628b2010-05-24 12:08:16 +0200194 case 's':
Andreas Fenkart24371902016-04-05 23:13:42 +0200195 script_file = optarg;
Stefano Babicada628b2010-05-24 12:08:16 +0200196 break;
Andreas Fenkart6726fc42015-12-09 13:13:26 +0100197 case 'a':
198 case 'c':
Stefano Babicada628b2010-05-24 12:08:16 +0200199 case 'h':
B, Raviccb293f2016-09-26 18:24:08 +0530200 case 'l':
Andreas Fenkart6726fc42015-12-09 13:13:26 +0100201 /* ignore common options */
Andreas Fenkart33e91772015-12-09 13:13:23 +0100202 break;
Daniel Hobi6a20a322010-09-16 14:36:09 +0200203 default: /* '?' */
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +0100204 usage_setenv();
Andreas Fenkart33e91772015-12-09 13:13:23 +0100205 exit(EXIT_FAILURE);
206 break;
Stefano Babicada628b2010-05-24 12:08:16 +0200207 }
208 }
Andreas Fenkart33e91772015-12-09 13:13:23 +0100209 return 0;
210}
211
212int main(int argc, char *argv[])
213{
B, Raviccb293f2016-09-26 18:24:08 +0530214 char *lockname = "/var/lock/" CMD_PRINTENV ".lock";
Andreas Fenkart33e91772015-12-09 13:13:23 +0100215 int lockfd = -1;
216 int retval = EXIT_SUCCESS;
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +0100217 char *_cmdname;
Andreas Fenkart33e91772015-12-09 13:13:23 +0100218
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +0100219 _cmdname = *argv;
220 if (strrchr(_cmdname, '/') != NULL)
221 _cmdname = strrchr(_cmdname, '/') + 1;
Stefano Babicada628b2010-05-24 12:08:16 +0200222
Andreas Fenkart9b5a28b2015-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 Fenkart33e91772015-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 Fenkart9b5a28b2015-12-09 13:13:27 +0100230 CMD_PRINTENV, CMD_SETENV, _cmdname);
Andreas Fenkart33e91772015-12-09 13:13:23 +0100231 exit(EXIT_FAILURE);
232 }
233
Andreas Fenkart9b5a28b2015-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 Fenkart14908ef2015-12-09 13:13:25 +0100242 /* shift parsed flags, jump to non-option arguments */
243 argc -= optind;
244 argv += optind;
245
B, Raviccb293f2016-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 Fenkart33e91772015-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 }
269
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +0100270 if (do_printenv) {
Andreas Fenkart24371902016-04-05 23:13:42 +0200271 if (fw_printenv(argc, argv, noheader, &env_opts) != 0)
Joe Hershberger3f1f1752012-10-03 09:38:49 +0000272 retval = EXIT_FAILURE;
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +0100273 } else {
Andreas Fenkart24371902016-04-05 23:13:42 +0200274 if (!script_file) {
275 if (fw_setenv(argc, argv, &env_opts) != 0)
Joe Hershberger3f1f1752012-10-03 09:38:49 +0000276 retval = EXIT_FAILURE;
Stefano Babicada628b2010-05-24 12:08:16 +0200277 } else {
Andreas Fenkart24371902016-04-05 23:13:42 +0200278 if (fw_parse_script(script_file, &env_opts) != 0)
Joe Hershberger3f1f1752012-10-03 09:38:49 +0000279 retval = EXIT_FAILURE;
Stefano Babicada628b2010-05-24 12:08:16 +0200280 }
wdenk34b613e2002-12-17 01:51:00 +0000281 }
282
B, Raviccb293f2016-09-26 18:24:08 +0530283 if (env_opts.lockname)
284 free(lockname);
285
Joe Hershberger3f1f1752012-10-03 09:38:49 +0000286 flock(lockfd, LOCK_UN);
287 close(lockfd);
288 return retval;
wdenk34b613e2002-12-17 01:51:00 +0000289}