blob: 7a17b28f40bc3109a2eb7424f9e45f086127f367 [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'},
Stefano Babicbd7b26f2010-05-24 12:08:16 +020049 {NULL, 0, NULL, 0}
50};
51
Andreas Fenkart81974f42016-04-05 23:13:42 +020052static struct env_opts env_opts;
53
54/* setenv options */
55static int noheader;
56
57/* getenv options */
58static char *script_file;
59
Andreas Fenkartb92ae3a2015-12-09 13:13:27 +010060void usage_printenv(void)
Stefano Babicbd7b26f2010-05-24 12:08:16 +020061{
62
Andreas Fenkartb92ae3a2015-12-09 13:13:27 +010063 fprintf(stderr,
64 "Usage: fw_printenv [OPTIONS]... [VARIABLE]...\n"
65 "Print variables from U-Boot environment\n"
66 "\n"
67 " -h, --help print this help.\n"
68#ifdef CONFIG_ENV_AES
69 " -a, --aes aes key to access environment\n"
Michael Heimpold9884f442015-10-27 22:34:49 +010070#endif
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"
75 "\n");
76}
77
78void usage_setenv(void)
79{
80 fprintf(stderr,
81 "Usage: fw_setenv [OPTIONS]... [VARIABLE]...\n"
82 "Modify variables in U-Boot environment\n"
Stefano Babicbd7b26f2010-05-24 12:08:16 +020083 "\n"
Andreas Fenkartb92ae3a2015-12-09 13:13:27 +010084 " -h, --help print this help.\n"
85#ifdef CONFIG_ENV_AES
86 " -a, --aes aes key to access environment\n"
87#endif
88#ifdef CONFIG_FILE
89 " -c, --config configuration file, default:" CONFIG_FILE "\n"
90#endif
91 " -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
Andreas Fenkartaf93e3d2015-12-09 13:13:26 +0100122 while ((c = getopt_long(argc, argv, ":a:c:h", long_options, NULL)) !=
123 EOF) {
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200124 switch (c) {
Marek Vasuta8a752c2014-03-05 19:59:52 +0100125 case 'a':
Andreas Fenkart81974f42016-04-05 23:13:42 +0200126 if (parse_aes_key(optarg, env_opts.aes_key)) {
Andreas Fenkart371ee132015-12-09 13:13:24 +0100127 fprintf(stderr, "AES key parse error\n");
Andreas Fenkartaf93e3d2015-12-09 13:13:26 +0100128 exit(EXIT_FAILURE);
Andreas Fenkart371ee132015-12-09 13:13:24 +0100129 }
Andreas Fenkart81974f42016-04-05 23:13:42 +0200130 env_opts.aes_flag = 1;
Marek Vasuta8a752c2014-03-05 19:59:52 +0100131 break;
Andreas Fenkart371ee132015-12-09 13:13:24 +0100132#ifdef CONFIG_FILE
Michael Heimpold9884f442015-10-27 22:34:49 +0100133 case 'c':
Andreas Fenkart81974f42016-04-05 23:13:42 +0200134 env_opts.config_file = optarg;
Michael Heimpold9884f442015-10-27 22:34:49 +0100135 break;
Andreas Fenkart371ee132015-12-09 13:13:24 +0100136#endif
Andreas Fenkart07ce9442015-12-09 13:13:23 +0100137 case 'h':
Andreas Fenkartb92ae3a2015-12-09 13:13:27 +0100138 do_printenv ? usage_printenv() : usage_setenv();
Andreas Fenkart07ce9442015-12-09 13:13:23 +0100139 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
158 while ((c = getopt_long(argc, argv, "a:c:ns:h", long_options, NULL)) !=
159 EOF) {
160 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':
167 /* ignore common options */
168 break;
Andreas Fenkart07ce9442015-12-09 13:13:23 +0100169 default: /* '?' */
Andreas Fenkartb92ae3a2015-12-09 13:13:27 +0100170 usage_printenv();
Andreas Fenkart07ce9442015-12-09 13:13:23 +0100171 exit(EXIT_FAILURE);
172 break;
173 }
174 }
175 return 0;
176}
177
178int parse_setenv_args(int argc, char *argv[])
179{
180 int c;
181
Andreas Fenkartaf93e3d2015-12-09 13:13:26 +0100182 parse_common_args(argc, argv);
Andreas Fenkart371ee132015-12-09 13:13:24 +0100183
Andreas Fenkartaf93e3d2015-12-09 13:13:26 +0100184 while ((c = getopt_long(argc, argv, "a:c:ns:h", long_options, NULL)) !=
185 EOF) {
Andreas Fenkart07ce9442015-12-09 13:13:23 +0100186 switch (c) {
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200187 case 's':
Andreas Fenkart81974f42016-04-05 23:13:42 +0200188 script_file = optarg;
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200189 break;
Andreas Fenkartaf93e3d2015-12-09 13:13:26 +0100190 case 'a':
191 case 'c':
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200192 case 'h':
Andreas Fenkartaf93e3d2015-12-09 13:13:26 +0100193 /* ignore common options */
Andreas Fenkart07ce9442015-12-09 13:13:23 +0100194 break;
Daniel Hobi29ccd7c2010-09-16 14:36:09 +0200195 default: /* '?' */
Andreas Fenkartb92ae3a2015-12-09 13:13:27 +0100196 usage_setenv();
Andreas Fenkart07ce9442015-12-09 13:13:23 +0100197 exit(EXIT_FAILURE);
198 break;
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200199 }
200 }
Andreas Fenkart07ce9442015-12-09 13:13:23 +0100201 return 0;
202}
203
204int main(int argc, char *argv[])
205{
Andreas Fenkart07ce9442015-12-09 13:13:23 +0100206 const char *lockname = "/var/lock/" CMD_PRINTENV ".lock";
207 int lockfd = -1;
208 int retval = EXIT_SUCCESS;
Andreas Fenkartb92ae3a2015-12-09 13:13:27 +0100209 char *_cmdname;
Andreas Fenkart07ce9442015-12-09 13:13:23 +0100210
Andreas Fenkartb92ae3a2015-12-09 13:13:27 +0100211 _cmdname = *argv;
212 if (strrchr(_cmdname, '/') != NULL)
213 _cmdname = strrchr(_cmdname, '/') + 1;
Andreas Fenkart07ce9442015-12-09 13:13:23 +0100214
Andreas Fenkartb92ae3a2015-12-09 13:13:27 +0100215 if (strcmp(_cmdname, CMD_PRINTENV) == 0) {
216 do_printenv = 1;
217 } else if (strcmp(_cmdname, CMD_SETENV) == 0) {
218 do_printenv = 0;
Andreas Fenkart07ce9442015-12-09 13:13:23 +0100219 } else {
220 fprintf(stderr,
221 "Identity crisis - may be called as `%s' or as `%s' but not as `%s'\n",
Andreas Fenkartb92ae3a2015-12-09 13:13:27 +0100222 CMD_PRINTENV, CMD_SETENV, _cmdname);
Andreas Fenkart07ce9442015-12-09 13:13:23 +0100223 exit(EXIT_FAILURE);
224 }
225
Andreas Fenkartb92ae3a2015-12-09 13:13:27 +0100226 if (do_printenv) {
227 if (parse_printenv_args(argc, argv))
228 exit(EXIT_FAILURE);
229 } else {
230 if (parse_setenv_args(argc, argv))
231 exit(EXIT_FAILURE);
232 }
233
Andreas Fenkart1ce68692015-12-09 13:13:25 +0100234 /* shift parsed flags, jump to non-option arguments */
235 argc -= optind;
236 argv += optind;
237
Andreas Fenkart07ce9442015-12-09 13:13:23 +0100238 lockfd = open(lockname, O_WRONLY | O_CREAT | O_TRUNC, 0666);
239 if (-1 == lockfd) {
240 fprintf(stderr, "Error opening lock file %s\n", lockname);
241 return EXIT_FAILURE;
242 }
243
244 if (-1 == flock(lockfd, LOCK_EX)) {
245 fprintf(stderr, "Error locking file %s\n", lockname);
246 close(lockfd);
247 return EXIT_FAILURE;
248 }
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200249
Andreas Fenkartb92ae3a2015-12-09 13:13:27 +0100250 if (do_printenv) {
Andreas Fenkart81974f42016-04-05 23:13:42 +0200251 if (fw_printenv(argc, argv, noheader, &env_opts) != 0)
Joe Hershbergere4a223f2012-10-03 09:38:49 +0000252 retval = EXIT_FAILURE;
Andreas Fenkartb92ae3a2015-12-09 13:13:27 +0100253 } else {
Andreas Fenkart81974f42016-04-05 23:13:42 +0200254 if (!script_file) {
255 if (fw_setenv(argc, argv, &env_opts) != 0)
Joe Hershbergere4a223f2012-10-03 09:38:49 +0000256 retval = EXIT_FAILURE;
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200257 } else {
Andreas Fenkart81974f42016-04-05 23:13:42 +0200258 if (fw_parse_script(script_file, &env_opts) != 0)
Joe Hershbergere4a223f2012-10-03 09:38:49 +0000259 retval = EXIT_FAILURE;
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200260 }
wdenk6aff3112002-12-17 01:51:00 +0000261 }
262
Joe Hershbergere4a223f2012-10-03 09:38:49 +0000263 flock(lockfd, LOCK_UN);
264 close(lockfd);
265 return retval;
wdenk6aff3112002-12-17 01:51:00 +0000266}