xref: /openbmc/u-boot/tools/env/fw_env_main.c (revision e04f9d0c)
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 "fw_env.h"
38 
39 #define CMD_PRINTENV	"fw_printenv"
40 #define CMD_SETENV	"fw_setenv"
41 static int do_printenv;
42 
43 static struct option long_options[] = {
44 	{"aes", required_argument, NULL, 'a'},
45 	{"config", required_argument, NULL, 'c'},
46 	{"help", no_argument, NULL, 'h'},
47 	{"script", required_argument, NULL, 's'},
48 	{"noheader", required_argument, NULL, 'n'},
49 	{NULL, 0, NULL, 0}
50 };
51 
52 static struct env_opts env_opts;
53 
54 /* setenv options */
55 static int noheader;
56 
57 /* getenv options */
58 static char *script_file;
59 
60 void usage_printenv(void)
61 {
62 
63 	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"
70 #endif
71 #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 
78 void 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"
97 		"\n"
98 		"Script Syntax:\n"
99 		"  key [space] value\n"
100 		"  lines starting with '#' are treated as comment\n"
101 		"\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");
112 }
113 
114 static void parse_common_args(int argc, char *argv[])
115 {
116 	int c;
117 
118 #ifdef CONFIG_FILE
119 	env_opts.config_file = CONFIG_FILE;
120 #endif
121 
122 	while ((c = getopt_long(argc, argv, ":a:c:h", long_options, NULL)) !=
123 	       EOF) {
124 		switch (c) {
125 		case 'a':
126 			if (parse_aes_key(optarg, env_opts.aes_key)) {
127 				fprintf(stderr, "AES key parse error\n");
128 				exit(EXIT_FAILURE);
129 			}
130 			env_opts.aes_flag = 1;
131 			break;
132 #ifdef CONFIG_FILE
133 		case 'c':
134 			env_opts.config_file = optarg;
135 			break;
136 #endif
137 		case 'h':
138 			do_printenv ? usage_printenv() : usage_setenv();
139 			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 
152 int 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':
162 			noheader = 1;
163 			break;
164 		case 'a':
165 		case 'c':
166 		case 'h':
167 			/* ignore common options */
168 			break;
169 		default: /* '?' */
170 			usage_printenv();
171 			exit(EXIT_FAILURE);
172 			break;
173 		}
174 	}
175 	return 0;
176 }
177 
178 int parse_setenv_args(int argc, char *argv[])
179 {
180 	int c;
181 
182 	parse_common_args(argc, argv);
183 
184 	while ((c = getopt_long(argc, argv, "a:c:ns:h", long_options, NULL)) !=
185 	       EOF) {
186 		switch (c) {
187 		case 's':
188 			script_file = optarg;
189 			break;
190 		case 'a':
191 		case 'c':
192 		case 'h':
193 			/* ignore common options */
194 			break;
195 		default: /* '?' */
196 			usage_setenv();
197 			exit(EXIT_FAILURE);
198 			break;
199 		}
200 	}
201 	return 0;
202 }
203 
204 int main(int argc, char *argv[])
205 {
206 	const char *lockname = "/var/lock/" CMD_PRINTENV ".lock";
207 	int lockfd = -1;
208 	int retval = EXIT_SUCCESS;
209 	char *_cmdname;
210 
211 	_cmdname = *argv;
212 	if (strrchr(_cmdname, '/') != NULL)
213 		_cmdname = strrchr(_cmdname, '/') + 1;
214 
215 	if (strcmp(_cmdname, CMD_PRINTENV) == 0) {
216 		do_printenv = 1;
217 	} else if (strcmp(_cmdname, CMD_SETENV) == 0) {
218 		do_printenv = 0;
219 	} else {
220 		fprintf(stderr,
221 			"Identity crisis - may be called as `%s' or as `%s' but not as `%s'\n",
222 			CMD_PRINTENV, CMD_SETENV, _cmdname);
223 		exit(EXIT_FAILURE);
224 	}
225 
226 	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 
234 	/* shift parsed flags, jump to non-option arguments */
235 	argc -= optind;
236 	argv += optind;
237 
238 	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 
250 	if (do_printenv) {
251 		if (fw_printenv(argc, argv, noheader, &env_opts) != 0)
252 			retval = EXIT_FAILURE;
253 	} else {
254 		if (!script_file) {
255 			if (fw_setenv(argc, argv, &env_opts) != 0)
256 				retval = EXIT_FAILURE;
257 		} else {
258 			if (fw_parse_script(script_file, &env_opts) != 0)
259 				retval = EXIT_FAILURE;
260 		}
261 	}
262 
263 	flock(lockfd, LOCK_UN);
264 	close(lockfd);
265 	return retval;
266 }
267