blob: fb4afa5ee91c427a4604f4d602a299c11febbdae [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>
Stefan Agnere5308f82017-10-26 14:57:53 +020037#include <version.h>
Stefano Babic831c4d52017-04-05 18:08:01 +020038#include "fw_env_private.h"
wdenk34b613e2002-12-17 01:51:00 +000039#include "fw_env.h"
40
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +010041#define CMD_PRINTENV "fw_printenv"
wdenk34b613e2002-12-17 01:51:00 +000042#define CMD_SETENV "fw_setenv"
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +010043static int do_printenv;
wdenk34b613e2002-12-17 01:51:00 +000044
Stefano Babicada628b2010-05-24 12:08:16 +020045static struct option long_options[] = {
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +010046 {"config", required_argument, NULL, 'c'},
Stefano Babicada628b2010-05-24 12:08:16 +020047 {"help", no_argument, NULL, 'h'},
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +010048 {"script", required_argument, NULL, 's'},
Alex Kiernan688cee92018-02-11 17:16:46 +000049 {"noheader", no_argument, NULL, 'n'},
B, Raviccb293f2016-09-26 18:24:08 +053050 {"lock", required_argument, NULL, 'l'},
Stefan Agnere5308f82017-10-26 14:57:53 +020051 {"version", no_argument, NULL, 'v'},
Stefano Babicada628b2010-05-24 12:08:16 +020052 {NULL, 0, NULL, 0}
53};
54
Andreas Fenkart24371902016-04-05 23:13:42 +020055static struct env_opts env_opts;
56
57/* setenv options */
58static int noheader;
59
60/* getenv options */
61static char *script_file;
62
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +010063void usage_printenv(void)
Stefano Babicada628b2010-05-24 12:08:16 +020064{
65
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +010066 fprintf(stderr,
67 "Usage: fw_printenv [OPTIONS]... [VARIABLE]...\n"
68 "Print variables from U-Boot environment\n"
69 "\n"
70 " -h, --help print this help.\n"
Stefan Agnere5308f82017-10-26 14:57:53 +020071 " -v, --version display version\n"
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
Simon Glass6a38e412017-08-03 12:22:09 -060080void usage_env_set(void)
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +010081{
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"
Stefan Agnere5308f82017-10-26 14:57:53 +020087 " -v, --version display version\n"
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +010088#ifdef CONFIG_FILE
89 " -c, --config configuration file, default:" CONFIG_FILE "\n"
90#endif
B, Raviccb293f2016-09-26 18:24:08 +053091 " -l, --lock lock node, default:/var/lock\n"
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +010092 " -s, --script batch mode to minimize writes\n"
93 "\n"
94 "Examples:\n"
95 " fw_setenv foo bar set variable foo equal bar\n"
96 " fw_setenv foo clear variable foo\n"
97 " fw_setenv --script file run batch script\n"
Stefano Babicada628b2010-05-24 12:08:16 +020098 "\n"
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +010099 "Script Syntax:\n"
100 " key [space] value\n"
Vagrant Cascadian34645662016-03-15 12:11:12 -0700101 " lines starting with '#' are treated as comment\n"
Stefano Babicada628b2010-05-24 12:08:16 +0200102 "\n"
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +0100103 " A variable without value will be deleted. Any number of spaces are\n"
104 " allowed between key and value. Space inside of the value is treated\n"
105 " as part of the value itself.\n"
106 "\n"
107 "Script Example:\n"
108 " netdev eth0\n"
109 " kernel_addr 400000\n"
110 " foo empty empty empty empty empty empty\n"
111 " bar\n"
112 "\n");
Stefano Babicada628b2010-05-24 12:08:16 +0200113}
114
Andreas Fenkart6726fc42015-12-09 13:13:26 +0100115static void parse_common_args(int argc, char *argv[])
wdenk34b613e2002-12-17 01:51:00 +0000116{
Stefano Babicada628b2010-05-24 12:08:16 +0200117 int c;
wdenk34b613e2002-12-17 01:51:00 +0000118
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +0100119#ifdef CONFIG_FILE
Andreas Fenkart24371902016-04-05 23:13:42 +0200120 env_opts.config_file = CONFIG_FILE;
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +0100121#endif
122
Stefan Agnere5308f82017-10-26 14:57:53 +0200123 while ((c = getopt_long(argc, argv, ":a:c:l:h:v", long_options, NULL)) !=
Andreas Fenkart6726fc42015-12-09 13:13:26 +0100124 EOF) {
Stefano Babicada628b2010-05-24 12:08:16 +0200125 switch (c) {
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +0100126#ifdef CONFIG_FILE
Michael Heimpold211e17b2015-10-27 22:34:49 +0100127 case 'c':
Andreas Fenkart24371902016-04-05 23:13:42 +0200128 env_opts.config_file = optarg;
Michael Heimpold211e17b2015-10-27 22:34:49 +0100129 break;
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +0100130#endif
B, Raviccb293f2016-09-26 18:24:08 +0530131 case 'l':
132 env_opts.lockname = optarg;
133 break;
Andreas Fenkart6726fc42015-12-09 13:13:26 +0100134 case 'h':
Simon Glass6a38e412017-08-03 12:22:09 -0600135 do_printenv ? usage_printenv() : usage_env_set();
Andreas Fenkart6726fc42015-12-09 13:13:26 +0100136 exit(EXIT_SUCCESS);
137 break;
Stefan Agnere5308f82017-10-26 14:57:53 +0200138 case 'v':
139 fprintf(stderr, "Compiled with " U_BOOT_VERSION "\n");
140 exit(EXIT_SUCCESS);
141 break;
Andreas Fenkart6726fc42015-12-09 13:13:26 +0100142 default:
143 /* ignore unknown options */
144 break;
145 }
146 }
147
148 /* Reset getopt for the next pass. */
149 opterr = 1;
150 optind = 1;
151}
152
153int parse_printenv_args(int argc, char *argv[])
154{
155 int c;
156
157 parse_common_args(argc, argv);
158
Stefan Agnere5308f82017-10-26 14:57:53 +0200159 while ((c = getopt_long(argc, argv, "a:c:ns:l:h:v", long_options, NULL))
B, Raviccb293f2016-09-26 18:24:08 +0530160 != EOF) {
Andreas Fenkart6726fc42015-12-09 13:13:26 +0100161 switch (c) {
Daniel Hobi6c554262010-09-15 19:46:26 +0200162 case 'n':
Andreas Fenkart24371902016-04-05 23:13:42 +0200163 noheader = 1;
Daniel Hobi6c554262010-09-15 19:46:26 +0200164 break;
Andreas Fenkart6726fc42015-12-09 13:13:26 +0100165 case 'a':
166 case 'c':
Andreas Fenkart33e91772015-12-09 13:13:23 +0100167 case 'h':
B, Raviccb293f2016-09-26 18:24:08 +0530168 case 'l':
Andreas Fenkart6726fc42015-12-09 13:13:26 +0100169 /* ignore common options */
Andreas Fenkart33e91772015-12-09 13:13:23 +0100170 break;
171 default: /* '?' */
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +0100172 usage_printenv();
Andreas Fenkart33e91772015-12-09 13:13:23 +0100173 exit(EXIT_FAILURE);
174 break;
175 }
176 }
177 return 0;
178}
179
180int parse_setenv_args(int argc, char *argv[])
181{
182 int c;
183
Andreas Fenkart6726fc42015-12-09 13:13:26 +0100184 parse_common_args(argc, argv);
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +0100185
Stefan Agnere5308f82017-10-26 14:57:53 +0200186 while ((c = getopt_long(argc, argv, "a:c:ns:l:h:v", long_options, NULL))
B, Raviccb293f2016-09-26 18:24:08 +0530187 != EOF) {
Andreas Fenkart33e91772015-12-09 13:13:23 +0100188 switch (c) {
Stefano Babicada628b2010-05-24 12:08:16 +0200189 case 's':
Andreas Fenkart24371902016-04-05 23:13:42 +0200190 script_file = optarg;
Stefano Babicada628b2010-05-24 12:08:16 +0200191 break;
Andreas Fenkart6726fc42015-12-09 13:13:26 +0100192 case 'a':
193 case 'c':
Stefano Babicada628b2010-05-24 12:08:16 +0200194 case 'h':
B, Raviccb293f2016-09-26 18:24:08 +0530195 case 'l':
Andreas Fenkart6726fc42015-12-09 13:13:26 +0100196 /* ignore common options */
Andreas Fenkart33e91772015-12-09 13:13:23 +0100197 break;
Daniel Hobi6a20a322010-09-16 14:36:09 +0200198 default: /* '?' */
Simon Glass6a38e412017-08-03 12:22:09 -0600199 usage_env_set();
Andreas Fenkart33e91772015-12-09 13:13:23 +0100200 exit(EXIT_FAILURE);
201 break;
Stefano Babicada628b2010-05-24 12:08:16 +0200202 }
203 }
Andreas Fenkart33e91772015-12-09 13:13:23 +0100204 return 0;
205}
206
207int main(int argc, char *argv[])
208{
B, Raviccb293f2016-09-26 18:24:08 +0530209 char *lockname = "/var/lock/" CMD_PRINTENV ".lock";
Andreas Fenkart33e91772015-12-09 13:13:23 +0100210 int lockfd = -1;
211 int retval = EXIT_SUCCESS;
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +0100212 char *_cmdname;
Andreas Fenkart33e91772015-12-09 13:13:23 +0100213
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +0100214 _cmdname = *argv;
215 if (strrchr(_cmdname, '/') != NULL)
216 _cmdname = strrchr(_cmdname, '/') + 1;
Stefano Babicada628b2010-05-24 12:08:16 +0200217
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +0100218 if (strcmp(_cmdname, CMD_PRINTENV) == 0) {
219 do_printenv = 1;
220 } else if (strcmp(_cmdname, CMD_SETENV) == 0) {
221 do_printenv = 0;
Andreas Fenkart33e91772015-12-09 13:13:23 +0100222 } else {
223 fprintf(stderr,
224 "Identity crisis - may be called as `%s' or as `%s' but not as `%s'\n",
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +0100225 CMD_PRINTENV, CMD_SETENV, _cmdname);
Andreas Fenkart33e91772015-12-09 13:13:23 +0100226 exit(EXIT_FAILURE);
227 }
228
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +0100229 if (do_printenv) {
230 if (parse_printenv_args(argc, argv))
231 exit(EXIT_FAILURE);
232 } else {
233 if (parse_setenv_args(argc, argv))
234 exit(EXIT_FAILURE);
235 }
236
Andreas Fenkart14908ef2015-12-09 13:13:25 +0100237 /* shift parsed flags, jump to non-option arguments */
238 argc -= optind;
239 argv += optind;
240
B, Raviccb293f2016-09-26 18:24:08 +0530241 if (env_opts.lockname) {
Kristian Amliec0855f22018-04-04 10:09:57 +0200242 lockname = malloc(strlen(env_opts.lockname) +
B, Raviccb293f2016-09-26 18:24:08 +0530243 sizeof(CMD_PRINTENV) + 10);
244 if (!lockname) {
245 fprintf(stderr, "Unable allocate memory");
246 exit(EXIT_FAILURE);
247 }
248
249 sprintf(lockname, "%s/%s.lock",
250 env_opts.lockname, CMD_PRINTENV);
251 }
252
Andreas Fenkart33e91772015-12-09 13:13:23 +0100253 lockfd = open(lockname, O_WRONLY | O_CREAT | O_TRUNC, 0666);
254 if (-1 == lockfd) {
255 fprintf(stderr, "Error opening lock file %s\n", lockname);
256 return EXIT_FAILURE;
257 }
258
259 if (-1 == flock(lockfd, LOCK_EX)) {
260 fprintf(stderr, "Error locking file %s\n", lockname);
261 close(lockfd);
262 return EXIT_FAILURE;
263 }
264
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +0100265 if (do_printenv) {
Andreas Fenkart24371902016-04-05 23:13:42 +0200266 if (fw_printenv(argc, argv, noheader, &env_opts) != 0)
Joe Hershberger3f1f1752012-10-03 09:38:49 +0000267 retval = EXIT_FAILURE;
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +0100268 } else {
Andreas Fenkart24371902016-04-05 23:13:42 +0200269 if (!script_file) {
Simon Glass6a38e412017-08-03 12:22:09 -0600270 if (fw_env_set(argc, argv, &env_opts) != 0)
Joe Hershberger3f1f1752012-10-03 09:38:49 +0000271 retval = EXIT_FAILURE;
Stefano Babicada628b2010-05-24 12:08:16 +0200272 } else {
Andreas Fenkart24371902016-04-05 23:13:42 +0200273 if (fw_parse_script(script_file, &env_opts) != 0)
Joe Hershberger3f1f1752012-10-03 09:38:49 +0000274 retval = EXIT_FAILURE;
Stefano Babicada628b2010-05-24 12:08:16 +0200275 }
wdenk34b613e2002-12-17 01:51:00 +0000276 }
277
B, Raviccb293f2016-09-26 18:24:08 +0530278 if (env_opts.lockname)
279 free(lockname);
280
Joe Hershberger3f1f1752012-10-03 09:38:49 +0000281 flock(lockfd, LOCK_UN);
282 close(lockfd);
283 return retval;
wdenk34b613e2002-12-17 01:51:00 +0000284}