xref: /openbmc/u-boot/tools/env/fw_env_main.c (revision 470dd6cc)
1 /*
2  * (C) Copyright 2000-2008
3  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4  *
5  * SPDX-License-Identifier:	GPL-2.0+
6  */
7 
8 /*
9  * Command line user interface to firmware (=U-Boot) environment.
10  *
11  * Implements:
12  *	fw_printenv [ -a key ] [[ -n name ] | [ name ... ]]
13  *              - 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.
17  *	fw_setenv [ -a key ] name [ value ... ]
18  *		- 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,
21  *		  separated by single blank characters, and the
22  *		  resulting string is assigned to the environment
23  *		  variable "name"
24  *
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'.
28  */
29 
30 #include <fcntl.h>
31 #include <getopt.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <stdlib.h>
35 #include <sys/file.h>
36 #include <unistd.h>
37 #include <version.h>
38 #include "fw_env_private.h"
39 #include "fw_env.h"
40 
41 #define CMD_PRINTENV	"fw_printenv"
42 #define CMD_SETENV	"fw_setenv"
43 static int do_printenv;
44 
45 static struct option long_options[] = {
46 	{"aes", required_argument, NULL, 'a'},
47 	{"config", required_argument, NULL, 'c'},
48 	{"help", no_argument, NULL, 'h'},
49 	{"script", required_argument, NULL, 's'},
50 	{"noheader", required_argument, NULL, 'n'},
51 	{"lock", required_argument, NULL, 'l'},
52 	{"version", no_argument, NULL, 'v'},
53 	{NULL, 0, NULL, 0}
54 };
55 
56 static struct env_opts env_opts;
57 
58 /* setenv options */
59 static int noheader;
60 
61 /* getenv options */
62 static char *script_file;
63 
64 void usage_printenv(void)
65 {
66 
67 	fprintf(stderr,
68 		"Usage: fw_printenv [OPTIONS]... [VARIABLE]...\n"
69 		"Print variables from U-Boot environment\n"
70 		"\n"
71 		" -h, --help           print this help.\n"
72 		" -v, --version        display version\n"
73 #ifdef CONFIG_ENV_AES
74 		" -a, --aes            aes key to access environment\n"
75 #endif
76 #ifdef CONFIG_FILE
77 		" -c, --config         configuration file, default:" CONFIG_FILE "\n"
78 #endif
79 		" -n, --noheader       do not repeat variable name in output\n"
80 		" -l, --lock           lock node, default:/var/lock\n"
81 		"\n");
82 }
83 
84 void usage_env_set(void)
85 {
86 	fprintf(stderr,
87 		"Usage: fw_setenv [OPTIONS]... [VARIABLE]...\n"
88 		"Modify variables in U-Boot environment\n"
89 		"\n"
90 		" -h, --help           print this help.\n"
91 		" -v, --version        display version\n"
92 #ifdef CONFIG_ENV_AES
93 		" -a, --aes            aes key to access environment\n"
94 #endif
95 #ifdef CONFIG_FILE
96 		" -c, --config         configuration file, default:" CONFIG_FILE "\n"
97 #endif
98 		" -l, --lock           lock node, default:/var/lock\n"
99 		" -s, --script         batch mode to minimize writes\n"
100 		"\n"
101 		"Examples:\n"
102 		"  fw_setenv foo bar   set variable foo equal bar\n"
103 		"  fw_setenv foo       clear variable foo\n"
104 		"  fw_setenv --script file run batch script\n"
105 		"\n"
106 		"Script Syntax:\n"
107 		"  key [space] value\n"
108 		"  lines starting with '#' are treated as comment\n"
109 		"\n"
110 		"  A variable without value will be deleted. Any number of spaces are\n"
111 		"  allowed between key and value. Space inside of the value is treated\n"
112 		"  as part of the value itself.\n"
113 		"\n"
114 		"Script Example:\n"
115 		"  netdev         eth0\n"
116 		"  kernel_addr    400000\n"
117 		"  foo            empty empty empty    empty empty empty\n"
118 		"  bar\n"
119 		"\n");
120 }
121 
122 static void parse_common_args(int argc, char *argv[])
123 {
124 	int c;
125 
126 #ifdef CONFIG_FILE
127 	env_opts.config_file = CONFIG_FILE;
128 #endif
129 
130 	while ((c = getopt_long(argc, argv, ":a:c:l:h:v", long_options, NULL)) !=
131 	       EOF) {
132 		switch (c) {
133 		case 'a':
134 			if (parse_aes_key(optarg, env_opts.aes_key)) {
135 				fprintf(stderr, "AES key parse error\n");
136 				exit(EXIT_FAILURE);
137 			}
138 			env_opts.aes_flag = 1;
139 			break;
140 #ifdef CONFIG_FILE
141 		case 'c':
142 			env_opts.config_file = optarg;
143 			break;
144 #endif
145 		case 'l':
146 			env_opts.lockname = optarg;
147 			break;
148 		case 'h':
149 			do_printenv ? usage_printenv() : usage_env_set();
150 			exit(EXIT_SUCCESS);
151 			break;
152 		case 'v':
153 			fprintf(stderr, "Compiled with " U_BOOT_VERSION "\n");
154 			exit(EXIT_SUCCESS);
155 			break;
156 		default:
157 			/* ignore unknown options */
158 			break;
159 		}
160 	}
161 
162 	/* Reset getopt for the next pass. */
163 	opterr = 1;
164 	optind = 1;
165 }
166 
167 int parse_printenv_args(int argc, char *argv[])
168 {
169 	int c;
170 
171 	parse_common_args(argc, argv);
172 
173 	while ((c = getopt_long(argc, argv, "a:c:ns:l:h:v", long_options, NULL))
174 		!= EOF) {
175 		switch (c) {
176 		case 'n':
177 			noheader = 1;
178 			break;
179 		case 'a':
180 		case 'c':
181 		case 'h':
182 		case 'l':
183 			/* ignore common options */
184 			break;
185 		default: /* '?' */
186 			usage_printenv();
187 			exit(EXIT_FAILURE);
188 			break;
189 		}
190 	}
191 	return 0;
192 }
193 
194 int parse_setenv_args(int argc, char *argv[])
195 {
196 	int c;
197 
198 	parse_common_args(argc, argv);
199 
200 	while ((c = getopt_long(argc, argv, "a:c:ns:l:h:v", long_options, NULL))
201 		!= EOF) {
202 		switch (c) {
203 		case 's':
204 			script_file = optarg;
205 			break;
206 		case 'a':
207 		case 'c':
208 		case 'h':
209 		case 'l':
210 			/* ignore common options */
211 			break;
212 		default: /* '?' */
213 			usage_env_set();
214 			exit(EXIT_FAILURE);
215 			break;
216 		}
217 	}
218 	return 0;
219 }
220 
221 int main(int argc, char *argv[])
222 {
223 	char *lockname = "/var/lock/" CMD_PRINTENV ".lock";
224 	int lockfd = -1;
225 	int retval = EXIT_SUCCESS;
226 	char *_cmdname;
227 
228 	_cmdname = *argv;
229 	if (strrchr(_cmdname, '/') != NULL)
230 		_cmdname = strrchr(_cmdname, '/') + 1;
231 
232 	if (strcmp(_cmdname, CMD_PRINTENV) == 0) {
233 		do_printenv = 1;
234 	} else if (strcmp(_cmdname, CMD_SETENV) == 0) {
235 		do_printenv = 0;
236 	} else {
237 		fprintf(stderr,
238 			"Identity crisis - may be called as `%s' or as `%s' but not as `%s'\n",
239 			CMD_PRINTENV, CMD_SETENV, _cmdname);
240 		exit(EXIT_FAILURE);
241 	}
242 
243 	if (do_printenv) {
244 		if (parse_printenv_args(argc, argv))
245 			exit(EXIT_FAILURE);
246 	} else {
247 		if (parse_setenv_args(argc, argv))
248 			exit(EXIT_FAILURE);
249 	}
250 
251 	/* shift parsed flags, jump to non-option arguments */
252 	argc -= optind;
253 	argv += optind;
254 
255 	if (env_opts.lockname) {
256 		lockname = malloc(sizeof(env_opts.lockname) +
257 				sizeof(CMD_PRINTENV) + 10);
258 		if (!lockname) {
259 			fprintf(stderr, "Unable allocate memory");
260 			exit(EXIT_FAILURE);
261 		}
262 
263 		sprintf(lockname, "%s/%s.lock",
264 			env_opts.lockname, CMD_PRINTENV);
265 	}
266 
267 	lockfd = open(lockname, O_WRONLY | O_CREAT | O_TRUNC, 0666);
268 	if (-1 == lockfd) {
269 		fprintf(stderr, "Error opening lock file %s\n", lockname);
270 		return EXIT_FAILURE;
271 	}
272 
273 	if (-1 == flock(lockfd, LOCK_EX)) {
274 		fprintf(stderr, "Error locking file %s\n", lockname);
275 		close(lockfd);
276 		return EXIT_FAILURE;
277 	}
278 
279 	if (do_printenv) {
280 		if (fw_printenv(argc, argv, noheader, &env_opts) != 0)
281 			retval = EXIT_FAILURE;
282 	} else {
283 		if (!script_file) {
284 			if (fw_env_set(argc, argv, &env_opts) != 0)
285 				retval = EXIT_FAILURE;
286 		} else {
287 			if (fw_parse_script(script_file, &env_opts) != 0)
288 				retval = EXIT_FAILURE;
289 		}
290 	}
291 
292 	if (env_opts.lockname)
293 		free(lockname);
294 
295 	flock(lockfd, LOCK_UN);
296 	close(lockfd);
297 	return retval;
298 }
299