blob: 6fdf41c8760422b27fd381b4ba56473f5a6fce4e [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>
Stefan Agner84d46e72017-10-26 14:57:53 +020037#include <version.h>
Stefano Babic9d80b492017-04-05 18:08:01 +020038#include "fw_env_private.h"
wdenk6aff3112002-12-17 01:51:00 +000039#include "fw_env.h"
40
Andreas Fenkartb92ae3a2015-12-09 13:13:27 +010041#define CMD_PRINTENV "fw_printenv"
wdenk6aff3112002-12-17 01:51:00 +000042#define CMD_SETENV "fw_setenv"
Andreas Fenkartb92ae3a2015-12-09 13:13:27 +010043static int do_printenv;
wdenk6aff3112002-12-17 01:51:00 +000044
Stefano Babicbd7b26f2010-05-24 12:08:16 +020045static struct option long_options[] = {
Andreas Fenkartb92ae3a2015-12-09 13:13:27 +010046 {"config", required_argument, NULL, 'c'},
Stefano Babicbd7b26f2010-05-24 12:08:16 +020047 {"help", no_argument, NULL, 'h'},
Andreas Fenkartb92ae3a2015-12-09 13:13:27 +010048 {"script", required_argument, NULL, 's'},
49 {"noheader", required_argument, NULL, 'n'},
B, Ravid40dbfb2016-09-26 18:24:08 +053050 {"lock", required_argument, NULL, 'l'},
Stefan Agner84d46e72017-10-26 14:57:53 +020051 {"version", no_argument, NULL, 'v'},
Stefano Babicbd7b26f2010-05-24 12:08:16 +020052 {NULL, 0, NULL, 0}
53};
54
Andreas Fenkart81974f42016-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 Fenkartb92ae3a2015-12-09 13:13:27 +010063void usage_printenv(void)
Stefano Babicbd7b26f2010-05-24 12:08:16 +020064{
65
Andreas Fenkartb92ae3a2015-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 Agner84d46e72017-10-26 14:57:53 +020071 " -v, --version display version\n"
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
Simon Glass382bee52017-08-03 12:22:09 -060080void usage_env_set(void)
Andreas Fenkartb92ae3a2015-12-09 13:13:27 +010081{
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"
Stefan Agner84d46e72017-10-26 14:57:53 +020087 " -v, --version display version\n"
Andreas Fenkartb92ae3a2015-12-09 13:13:27 +010088#ifdef CONFIG_FILE
89 " -c, --config configuration file, default:" CONFIG_FILE "\n"
90#endif
B, Ravid40dbfb2016-09-26 18:24:08 +053091 " -l, --lock lock node, default:/var/lock\n"
Andreas Fenkartb92ae3a2015-12-09 13:13:27 +010092 " -s, --script batch mode to minimize writes\n"
Stefano Babicbd7b26f2010-05-24 12:08:16 +020093 "\n"
Andreas Fenkartb92ae3a2015-12-09 13:13:27 +010094 "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"
98 "\n"
99 "Script Syntax:\n"
100 " key [space] value\n"
Vagrant Cascadian0e280652016-03-15 12:11:12 -0700101 " lines starting with '#' are treated as comment\n"
Andreas Fenkartb92ae3a2015-12-09 13:13:27 +0100102 "\n"
103 " 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 Babicbd7b26f2010-05-24 12:08:16 +0200113}
114
Andreas Fenkartaf93e3d2015-12-09 13:13:26 +0100115static void parse_common_args(int argc, char *argv[])
wdenk6aff3112002-12-17 01:51:00 +0000116{
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200117 int c;
wdenk6aff3112002-12-17 01:51:00 +0000118
Andreas Fenkart371ee132015-12-09 13:13:24 +0100119#ifdef CONFIG_FILE
Andreas Fenkart81974f42016-04-05 23:13:42 +0200120 env_opts.config_file = CONFIG_FILE;
Andreas Fenkart371ee132015-12-09 13:13:24 +0100121#endif
122
Stefan Agner84d46e72017-10-26 14:57:53 +0200123 while ((c = getopt_long(argc, argv, ":a:c:l:h:v", long_options, NULL)) !=
Andreas Fenkartaf93e3d2015-12-09 13:13:26 +0100124 EOF) {
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200125 switch (c) {
Andreas Fenkart371ee132015-12-09 13:13:24 +0100126#ifdef CONFIG_FILE
Michael Heimpold9884f442015-10-27 22:34:49 +0100127 case 'c':
Andreas Fenkart81974f42016-04-05 23:13:42 +0200128 env_opts.config_file = optarg;
Michael Heimpold9884f442015-10-27 22:34:49 +0100129 break;
Andreas Fenkart371ee132015-12-09 13:13:24 +0100130#endif
B, Ravid40dbfb2016-09-26 18:24:08 +0530131 case 'l':
132 env_opts.lockname = optarg;
133 break;
Andreas Fenkart07ce9442015-12-09 13:13:23 +0100134 case 'h':
Simon Glass382bee52017-08-03 12:22:09 -0600135 do_printenv ? usage_printenv() : usage_env_set();
Andreas Fenkart07ce9442015-12-09 13:13:23 +0100136 exit(EXIT_SUCCESS);
137 break;
Stefan Agner84d46e72017-10-26 14:57:53 +0200138 case 'v':
139 fprintf(stderr, "Compiled with " U_BOOT_VERSION "\n");
140 exit(EXIT_SUCCESS);
141 break;
Andreas Fenkartaf93e3d2015-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 Agner84d46e72017-10-26 14:57:53 +0200159 while ((c = getopt_long(argc, argv, "a:c:ns:l:h:v", long_options, NULL))
B, Ravid40dbfb2016-09-26 18:24:08 +0530160 != EOF) {
Andreas Fenkartaf93e3d2015-12-09 13:13:26 +0100161 switch (c) {
162 case 'n':
Andreas Fenkart81974f42016-04-05 23:13:42 +0200163 noheader = 1;
Andreas Fenkartaf93e3d2015-12-09 13:13:26 +0100164 break;
165 case 'a':
166 case 'c':
167 case 'h':
B, Ravid40dbfb2016-09-26 18:24:08 +0530168 case 'l':
Andreas Fenkartaf93e3d2015-12-09 13:13:26 +0100169 /* ignore common options */
170 break;
Andreas Fenkart07ce9442015-12-09 13:13:23 +0100171 default: /* '?' */
Andreas Fenkartb92ae3a2015-12-09 13:13:27 +0100172 usage_printenv();
Andreas Fenkart07ce9442015-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 Fenkartaf93e3d2015-12-09 13:13:26 +0100184 parse_common_args(argc, argv);
Andreas Fenkart371ee132015-12-09 13:13:24 +0100185
Stefan Agner84d46e72017-10-26 14:57:53 +0200186 while ((c = getopt_long(argc, argv, "a:c:ns:l:h:v", long_options, NULL))
B, Ravid40dbfb2016-09-26 18:24:08 +0530187 != EOF) {
Andreas Fenkart07ce9442015-12-09 13:13:23 +0100188 switch (c) {
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200189 case 's':
Andreas Fenkart81974f42016-04-05 23:13:42 +0200190 script_file = optarg;
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200191 break;
Andreas Fenkartaf93e3d2015-12-09 13:13:26 +0100192 case 'a':
193 case 'c':
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200194 case 'h':
B, Ravid40dbfb2016-09-26 18:24:08 +0530195 case 'l':
Andreas Fenkartaf93e3d2015-12-09 13:13:26 +0100196 /* ignore common options */
Andreas Fenkart07ce9442015-12-09 13:13:23 +0100197 break;
Daniel Hobi29ccd7c2010-09-16 14:36:09 +0200198 default: /* '?' */
Simon Glass382bee52017-08-03 12:22:09 -0600199 usage_env_set();
Andreas Fenkart07ce9442015-12-09 13:13:23 +0100200 exit(EXIT_FAILURE);
201 break;
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200202 }
203 }
Andreas Fenkart07ce9442015-12-09 13:13:23 +0100204 return 0;
205}
206
207int main(int argc, char *argv[])
208{
B, Ravid40dbfb2016-09-26 18:24:08 +0530209 char *lockname = "/var/lock/" CMD_PRINTENV ".lock";
Andreas Fenkart07ce9442015-12-09 13:13:23 +0100210 int lockfd = -1;
211 int retval = EXIT_SUCCESS;
Andreas Fenkartb92ae3a2015-12-09 13:13:27 +0100212 char *_cmdname;
Andreas Fenkart07ce9442015-12-09 13:13:23 +0100213
Andreas Fenkartb92ae3a2015-12-09 13:13:27 +0100214 _cmdname = *argv;
215 if (strrchr(_cmdname, '/') != NULL)
216 _cmdname = strrchr(_cmdname, '/') + 1;
Andreas Fenkart07ce9442015-12-09 13:13:23 +0100217
Andreas Fenkartb92ae3a2015-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 Fenkart07ce9442015-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 Fenkartb92ae3a2015-12-09 13:13:27 +0100225 CMD_PRINTENV, CMD_SETENV, _cmdname);
Andreas Fenkart07ce9442015-12-09 13:13:23 +0100226 exit(EXIT_FAILURE);
227 }
228
Andreas Fenkartb92ae3a2015-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 Fenkart1ce68692015-12-09 13:13:25 +0100237 /* shift parsed flags, jump to non-option arguments */
238 argc -= optind;
239 argv += optind;
240
B, Ravid40dbfb2016-09-26 18:24:08 +0530241 if (env_opts.lockname) {
242 lockname = malloc(sizeof(env_opts.lockname) +
243 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 Fenkart07ce9442015-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 }
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200264
Andreas Fenkartb92ae3a2015-12-09 13:13:27 +0100265 if (do_printenv) {
Andreas Fenkart81974f42016-04-05 23:13:42 +0200266 if (fw_printenv(argc, argv, noheader, &env_opts) != 0)
Joe Hershbergere4a223f2012-10-03 09:38:49 +0000267 retval = EXIT_FAILURE;
Andreas Fenkartb92ae3a2015-12-09 13:13:27 +0100268 } else {
Andreas Fenkart81974f42016-04-05 23:13:42 +0200269 if (!script_file) {
Simon Glass382bee52017-08-03 12:22:09 -0600270 if (fw_env_set(argc, argv, &env_opts) != 0)
Joe Hershbergere4a223f2012-10-03 09:38:49 +0000271 retval = EXIT_FAILURE;
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200272 } else {
Andreas Fenkart81974f42016-04-05 23:13:42 +0200273 if (fw_parse_script(script_file, &env_opts) != 0)
Joe Hershbergere4a223f2012-10-03 09:38:49 +0000274 retval = EXIT_FAILURE;
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200275 }
wdenk6aff3112002-12-17 01:51:00 +0000276 }
277
B, Ravid40dbfb2016-09-26 18:24:08 +0530278 if (env_opts.lockname)
279 free(lockname);
280
Joe Hershbergere4a223f2012-10-03 09:38:49 +0000281 flock(lockfd, LOCK_UN);
282 close(lockfd);
283 return retval;
wdenk6aff3112002-12-17 01:51:00 +0000284}