blob: 7a17b28f40bc3109a2eb7424f9e45f086127f367 [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'},
Stefano Babicada628b2010-05-24 12:08:16 +020049 {NULL, 0, NULL, 0}
50};
51
Andreas Fenkart24371902016-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 Fenkart9b5a28b2015-12-09 13:13:27 +010060void usage_printenv(void)
Stefano Babicada628b2010-05-24 12:08:16 +020061{
62
Andreas Fenkart9b5a28b2015-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 Heimpold211e17b2015-10-27 22:34:49 +010070#endif
Andreas Fenkart9b5a28b2015-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"
83 "\n"
84 " -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"
92 "\n"
93 "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"
Stefano Babicada628b2010-05-24 12:08:16 +020097 "\n"
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +010098 "Script Syntax:\n"
99 " key [space] value\n"
Vagrant Cascadian34645662016-03-15 12:11:12 -0700100 " lines starting with '#' are treated as comment\n"
Stefano Babicada628b2010-05-24 12:08:16 +0200101 "\n"
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +0100102 " 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 Babicada628b2010-05-24 12:08:16 +0200112}
113
Andreas Fenkart6726fc42015-12-09 13:13:26 +0100114static void parse_common_args(int argc, char *argv[])
wdenk34b613e2002-12-17 01:51:00 +0000115{
Stefano Babicada628b2010-05-24 12:08:16 +0200116 int c;
wdenk34b613e2002-12-17 01:51:00 +0000117
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +0100118#ifdef CONFIG_FILE
Andreas Fenkart24371902016-04-05 23:13:42 +0200119 env_opts.config_file = CONFIG_FILE;
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +0100120#endif
121
Andreas Fenkart6726fc42015-12-09 13:13:26 +0100122 while ((c = getopt_long(argc, argv, ":a:c:h", long_options, NULL)) !=
123 EOF) {
Stefano Babicada628b2010-05-24 12:08:16 +0200124 switch (c) {
Marek Vasut05ed2a52014-03-05 19:59:52 +0100125 case 'a':
Andreas Fenkart24371902016-04-05 23:13:42 +0200126 if (parse_aes_key(optarg, env_opts.aes_key)) {
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +0100127 fprintf(stderr, "AES key parse error\n");
Andreas Fenkart6726fc42015-12-09 13:13:26 +0100128 exit(EXIT_FAILURE);
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +0100129 }
Andreas Fenkart24371902016-04-05 23:13:42 +0200130 env_opts.aes_flag = 1;
Marek Vasut05ed2a52014-03-05 19:59:52 +0100131 break;
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +0100132#ifdef CONFIG_FILE
Michael Heimpold211e17b2015-10-27 22:34:49 +0100133 case 'c':
Andreas Fenkart24371902016-04-05 23:13:42 +0200134 env_opts.config_file = optarg;
Michael Heimpold211e17b2015-10-27 22:34:49 +0100135 break;
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +0100136#endif
Andreas Fenkart6726fc42015-12-09 13:13:26 +0100137 case 'h':
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +0100138 do_printenv ? usage_printenv() : usage_setenv();
Andreas Fenkart6726fc42015-12-09 13:13:26 +0100139 exit(EXIT_SUCCESS);
140 break;
141 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) {
Daniel Hobi6c554262010-09-15 19:46:26 +0200161 case 'n':
Andreas Fenkart24371902016-04-05 23:13:42 +0200162 noheader = 1;
Daniel Hobi6c554262010-09-15 19:46:26 +0200163 break;
Andreas Fenkart6726fc42015-12-09 13:13:26 +0100164 case 'a':
165 case 'c':
Andreas Fenkart33e91772015-12-09 13:13:23 +0100166 case 'h':
Andreas Fenkart6726fc42015-12-09 13:13:26 +0100167 /* ignore common options */
Andreas Fenkart33e91772015-12-09 13:13:23 +0100168 break;
169 default: /* '?' */
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +0100170 usage_printenv();
Andreas Fenkart33e91772015-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 Fenkart6726fc42015-12-09 13:13:26 +0100182 parse_common_args(argc, argv);
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +0100183
Andreas Fenkart6726fc42015-12-09 13:13:26 +0100184 while ((c = getopt_long(argc, argv, "a:c:ns:h", long_options, NULL)) !=
185 EOF) {
Andreas Fenkart33e91772015-12-09 13:13:23 +0100186 switch (c) {
Stefano Babicada628b2010-05-24 12:08:16 +0200187 case 's':
Andreas Fenkart24371902016-04-05 23:13:42 +0200188 script_file = optarg;
Stefano Babicada628b2010-05-24 12:08:16 +0200189 break;
Andreas Fenkart6726fc42015-12-09 13:13:26 +0100190 case 'a':
191 case 'c':
Stefano Babicada628b2010-05-24 12:08:16 +0200192 case 'h':
Andreas Fenkart6726fc42015-12-09 13:13:26 +0100193 /* ignore common options */
Andreas Fenkart33e91772015-12-09 13:13:23 +0100194 break;
Daniel Hobi6a20a322010-09-16 14:36:09 +0200195 default: /* '?' */
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +0100196 usage_setenv();
Andreas Fenkart33e91772015-12-09 13:13:23 +0100197 exit(EXIT_FAILURE);
198 break;
Stefano Babicada628b2010-05-24 12:08:16 +0200199 }
200 }
Andreas Fenkart33e91772015-12-09 13:13:23 +0100201 return 0;
202}
203
204int main(int argc, char *argv[])
205{
Andreas Fenkart33e91772015-12-09 13:13:23 +0100206 const char *lockname = "/var/lock/" CMD_PRINTENV ".lock";
207 int lockfd = -1;
208 int retval = EXIT_SUCCESS;
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +0100209 char *_cmdname;
Andreas Fenkart33e91772015-12-09 13:13:23 +0100210
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +0100211 _cmdname = *argv;
212 if (strrchr(_cmdname, '/') != NULL)
213 _cmdname = strrchr(_cmdname, '/') + 1;
Stefano Babicada628b2010-05-24 12:08:16 +0200214
Andreas Fenkart9b5a28b2015-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 Fenkart33e91772015-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 Fenkart9b5a28b2015-12-09 13:13:27 +0100222 CMD_PRINTENV, CMD_SETENV, _cmdname);
Andreas Fenkart33e91772015-12-09 13:13:23 +0100223 exit(EXIT_FAILURE);
224 }
225
Andreas Fenkart9b5a28b2015-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 Fenkart14908ef2015-12-09 13:13:25 +0100234 /* shift parsed flags, jump to non-option arguments */
235 argc -= optind;
236 argv += optind;
237
Andreas Fenkart33e91772015-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 }
249
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +0100250 if (do_printenv) {
Andreas Fenkart24371902016-04-05 23:13:42 +0200251 if (fw_printenv(argc, argv, noheader, &env_opts) != 0)
Joe Hershberger3f1f1752012-10-03 09:38:49 +0000252 retval = EXIT_FAILURE;
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +0100253 } else {
Andreas Fenkart24371902016-04-05 23:13:42 +0200254 if (!script_file) {
255 if (fw_setenv(argc, argv, &env_opts) != 0)
Joe Hershberger3f1f1752012-10-03 09:38:49 +0000256 retval = EXIT_FAILURE;
Stefano Babicada628b2010-05-24 12:08:16 +0200257 } else {
Andreas Fenkart24371902016-04-05 23:13:42 +0200258 if (fw_parse_script(script_file, &env_opts) != 0)
Joe Hershberger3f1f1752012-10-03 09:38:49 +0000259 retval = EXIT_FAILURE;
Stefano Babicada628b2010-05-24 12:08:16 +0200260 }
wdenk34b613e2002-12-17 01:51:00 +0000261 }
262
Joe Hershberger3f1f1752012-10-03 09:38:49 +0000263 flock(lockfd, LOCK_UN);
264 close(lockfd);
265 return retval;
wdenk34b613e2002-12-17 01:51:00 +0000266}