blob: 4bd4216625aab30435c27c48bea85778c119639f [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 Fenkartaa01f8d2015-12-09 13:13:24 +010052struct common_args common_args;
Andreas Fenkart33e91772015-12-09 13:13:23 +010053struct printenv_args printenv_args;
54struct setenv_args setenv_args;
55
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +010056void usage_printenv(void)
Stefano Babicada628b2010-05-24 12:08:16 +020057{
58
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +010059 fprintf(stderr,
60 "Usage: fw_printenv [OPTIONS]... [VARIABLE]...\n"
61 "Print variables from U-Boot environment\n"
62 "\n"
63 " -h, --help print this help.\n"
64#ifdef CONFIG_ENV_AES
65 " -a, --aes aes key to access environment\n"
Michael Heimpold211e17b2015-10-27 22:34:49 +010066#endif
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +010067#ifdef CONFIG_FILE
68 " -c, --config configuration file, default:" CONFIG_FILE "\n"
69#endif
70 " -n, --noheader do not repeat variable name in output\n"
71 "\n");
72}
73
74void usage_setenv(void)
75{
76 fprintf(stderr,
77 "Usage: fw_setenv [OPTIONS]... [VARIABLE]...\n"
78 "Modify variables in U-Boot environment\n"
79 "\n"
80 " -h, --help print this help.\n"
81#ifdef CONFIG_ENV_AES
82 " -a, --aes aes key to access environment\n"
83#endif
84#ifdef CONFIG_FILE
85 " -c, --config configuration file, default:" CONFIG_FILE "\n"
86#endif
87 " -s, --script batch mode to minimize writes\n"
88 "\n"
89 "Examples:\n"
90 " fw_setenv foo bar set variable foo equal bar\n"
91 " fw_setenv foo clear variable foo\n"
92 " fw_setenv --script file run batch script\n"
Stefano Babicada628b2010-05-24 12:08:16 +020093 "\n"
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +010094 "Script Syntax:\n"
95 " key [space] value\n"
96 " lines starting with '#' are treated as commment\n"
Stefano Babicada628b2010-05-24 12:08:16 +020097 "\n"
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +010098 " A variable without value will be deleted. Any number of spaces are\n"
99 " allowed between key and value. Space inside of the value is treated\n"
100 " as part of the value itself.\n"
101 "\n"
102 "Script Example:\n"
103 " netdev eth0\n"
104 " kernel_addr 400000\n"
105 " foo empty empty empty empty empty empty\n"
106 " bar\n"
107 "\n");
Stefano Babicada628b2010-05-24 12:08:16 +0200108}
109
Andreas Fenkart6726fc42015-12-09 13:13:26 +0100110static void parse_common_args(int argc, char *argv[])
wdenk34b613e2002-12-17 01:51:00 +0000111{
Stefano Babicada628b2010-05-24 12:08:16 +0200112 int c;
wdenk34b613e2002-12-17 01:51:00 +0000113
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +0100114#ifdef CONFIG_FILE
115 common_args.config_file = CONFIG_FILE;
116#endif
117
Andreas Fenkart6726fc42015-12-09 13:13:26 +0100118 while ((c = getopt_long(argc, argv, ":a:c:h", long_options, NULL)) !=
119 EOF) {
Stefano Babicada628b2010-05-24 12:08:16 +0200120 switch (c) {
Marek Vasut05ed2a52014-03-05 19:59:52 +0100121 case 'a':
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +0100122 if (parse_aes_key(optarg, common_args.aes_key)) {
123 fprintf(stderr, "AES key parse error\n");
Andreas Fenkart6726fc42015-12-09 13:13:26 +0100124 exit(EXIT_FAILURE);
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +0100125 }
126 common_args.aes_flag = 1;
Marek Vasut05ed2a52014-03-05 19:59:52 +0100127 break;
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +0100128#ifdef CONFIG_FILE
Michael Heimpold211e17b2015-10-27 22:34:49 +0100129 case 'c':
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +0100130 common_args.config_file = optarg;
Michael Heimpold211e17b2015-10-27 22:34:49 +0100131 break;
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +0100132#endif
Andreas Fenkart6726fc42015-12-09 13:13:26 +0100133 case 'h':
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +0100134 do_printenv ? usage_printenv() : usage_setenv();
Andreas Fenkart6726fc42015-12-09 13:13:26 +0100135 exit(EXIT_SUCCESS);
136 break;
137 default:
138 /* ignore unknown options */
139 break;
140 }
141 }
142
143 /* Reset getopt for the next pass. */
144 opterr = 1;
145 optind = 1;
146}
147
148int parse_printenv_args(int argc, char *argv[])
149{
150 int c;
151
152 parse_common_args(argc, argv);
153
154 while ((c = getopt_long(argc, argv, "a:c:ns:h", long_options, NULL)) !=
155 EOF) {
156 switch (c) {
Daniel Hobi6c554262010-09-15 19:46:26 +0200157 case 'n':
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +0100158 printenv_args.name_suppress = 1;
Daniel Hobi6c554262010-09-15 19:46:26 +0200159 break;
Andreas Fenkart6726fc42015-12-09 13:13:26 +0100160 case 'a':
161 case 'c':
Andreas Fenkart33e91772015-12-09 13:13:23 +0100162 case 'h':
Andreas Fenkart6726fc42015-12-09 13:13:26 +0100163 /* ignore common options */
Andreas Fenkart33e91772015-12-09 13:13:23 +0100164 break;
165 default: /* '?' */
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +0100166 usage_printenv();
Andreas Fenkart33e91772015-12-09 13:13:23 +0100167 exit(EXIT_FAILURE);
168 break;
169 }
170 }
171 return 0;
172}
173
174int parse_setenv_args(int argc, char *argv[])
175{
176 int c;
177
Andreas Fenkart6726fc42015-12-09 13:13:26 +0100178 parse_common_args(argc, argv);
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +0100179
Andreas Fenkart6726fc42015-12-09 13:13:26 +0100180 while ((c = getopt_long(argc, argv, "a:c:ns:h", long_options, NULL)) !=
181 EOF) {
Andreas Fenkart33e91772015-12-09 13:13:23 +0100182 switch (c) {
Stefano Babicada628b2010-05-24 12:08:16 +0200183 case 's':
Andreas Fenkart33e91772015-12-09 13:13:23 +0100184 setenv_args.script_file = optarg;
Stefano Babicada628b2010-05-24 12:08:16 +0200185 break;
Andreas Fenkart6726fc42015-12-09 13:13:26 +0100186 case 'a':
187 case 'c':
Stefano Babicada628b2010-05-24 12:08:16 +0200188 case 'h':
Andreas Fenkart6726fc42015-12-09 13:13:26 +0100189 /* ignore common options */
Andreas Fenkart33e91772015-12-09 13:13:23 +0100190 break;
Daniel Hobi6a20a322010-09-16 14:36:09 +0200191 default: /* '?' */
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +0100192 usage_setenv();
Andreas Fenkart33e91772015-12-09 13:13:23 +0100193 exit(EXIT_FAILURE);
194 break;
Stefano Babicada628b2010-05-24 12:08:16 +0200195 }
196 }
Andreas Fenkart33e91772015-12-09 13:13:23 +0100197 return 0;
198}
199
200int main(int argc, char *argv[])
201{
Andreas Fenkart33e91772015-12-09 13:13:23 +0100202 const char *lockname = "/var/lock/" CMD_PRINTENV ".lock";
203 int lockfd = -1;
204 int retval = EXIT_SUCCESS;
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +0100205 char *_cmdname;
Andreas Fenkart33e91772015-12-09 13:13:23 +0100206
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +0100207 _cmdname = *argv;
208 if (strrchr(_cmdname, '/') != NULL)
209 _cmdname = strrchr(_cmdname, '/') + 1;
Stefano Babicada628b2010-05-24 12:08:16 +0200210
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +0100211 if (strcmp(_cmdname, CMD_PRINTENV) == 0) {
212 do_printenv = 1;
213 } else if (strcmp(_cmdname, CMD_SETENV) == 0) {
214 do_printenv = 0;
Andreas Fenkart33e91772015-12-09 13:13:23 +0100215 } else {
216 fprintf(stderr,
217 "Identity crisis - may be called as `%s' or as `%s' but not as `%s'\n",
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +0100218 CMD_PRINTENV, CMD_SETENV, _cmdname);
Andreas Fenkart33e91772015-12-09 13:13:23 +0100219 exit(EXIT_FAILURE);
220 }
221
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +0100222 if (do_printenv) {
223 if (parse_printenv_args(argc, argv))
224 exit(EXIT_FAILURE);
225 } else {
226 if (parse_setenv_args(argc, argv))
227 exit(EXIT_FAILURE);
228 }
229
Andreas Fenkart14908ef2015-12-09 13:13:25 +0100230 /* shift parsed flags, jump to non-option arguments */
231 argc -= optind;
232 argv += optind;
233
Andreas Fenkart33e91772015-12-09 13:13:23 +0100234 lockfd = open(lockname, O_WRONLY | O_CREAT | O_TRUNC, 0666);
235 if (-1 == lockfd) {
236 fprintf(stderr, "Error opening lock file %s\n", lockname);
237 return EXIT_FAILURE;
238 }
239
240 if (-1 == flock(lockfd, LOCK_EX)) {
241 fprintf(stderr, "Error locking file %s\n", lockname);
242 close(lockfd);
243 return EXIT_FAILURE;
244 }
245
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +0100246 if (do_printenv) {
Joe Hershberger3f1f1752012-10-03 09:38:49 +0000247 if (fw_printenv(argc, argv) != 0)
248 retval = EXIT_FAILURE;
Andreas Fenkart9b5a28b2015-12-09 13:13:27 +0100249 } else {
Andreas Fenkart33e91772015-12-09 13:13:23 +0100250 if (!setenv_args.script_file) {
Stefano Babicada628b2010-05-24 12:08:16 +0200251 if (fw_setenv(argc, argv) != 0)
Joe Hershberger3f1f1752012-10-03 09:38:49 +0000252 retval = EXIT_FAILURE;
Stefano Babicada628b2010-05-24 12:08:16 +0200253 } else {
Andreas Fenkart33e91772015-12-09 13:13:23 +0100254 if (fw_parse_script(setenv_args.script_file) != 0)
Joe Hershberger3f1f1752012-10-03 09:38:49 +0000255 retval = EXIT_FAILURE;
Stefano Babicada628b2010-05-24 12:08:16 +0200256 }
wdenk34b613e2002-12-17 01:51:00 +0000257 }
258
Joe Hershberger3f1f1752012-10-03 09:38:49 +0000259 flock(lockfd, LOCK_UN);
260 close(lockfd);
261 return retval;
wdenk34b613e2002-12-17 01:51:00 +0000262}