xref: /openbmc/u-boot/tools/env/fw_env_main.c (revision d08957e6)
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 struct common_args common_args;
53 struct printenv_args printenv_args;
54 struct setenv_args setenv_args;
55 
56 void usage_printenv(void)
57 {
58 
59 	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"
66 #endif
67 #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 
74 void 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"
93 		"\n"
94 		"Script Syntax:\n"
95 		"  key [space] value\n"
96 		"  lines starting with '#' are treated as comment\n"
97 		"\n"
98 		"  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");
108 }
109 
110 static void parse_common_args(int argc, char *argv[])
111 {
112 	int c;
113 
114 #ifdef CONFIG_FILE
115 	common_args.config_file = CONFIG_FILE;
116 #endif
117 
118 	while ((c = getopt_long(argc, argv, ":a:c:h", long_options, NULL)) !=
119 	       EOF) {
120 		switch (c) {
121 		case 'a':
122 			if (parse_aes_key(optarg, common_args.aes_key)) {
123 				fprintf(stderr, "AES key parse error\n");
124 				exit(EXIT_FAILURE);
125 			}
126 			common_args.aes_flag = 1;
127 			break;
128 #ifdef CONFIG_FILE
129 		case 'c':
130 			common_args.config_file = optarg;
131 			break;
132 #endif
133 		case 'h':
134 			do_printenv ? usage_printenv() : usage_setenv();
135 			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 
148 int 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) {
157 		case 'n':
158 			printenv_args.name_suppress = 1;
159 			break;
160 		case 'a':
161 		case 'c':
162 		case 'h':
163 			/* ignore common options */
164 			break;
165 		default: /* '?' */
166 			usage_printenv();
167 			exit(EXIT_FAILURE);
168 			break;
169 		}
170 	}
171 	return 0;
172 }
173 
174 int parse_setenv_args(int argc, char *argv[])
175 {
176 	int c;
177 
178 	parse_common_args(argc, argv);
179 
180 	while ((c = getopt_long(argc, argv, "a:c:ns:h", long_options, NULL)) !=
181 	       EOF) {
182 		switch (c) {
183 		case 's':
184 			setenv_args.script_file = optarg;
185 			break;
186 		case 'a':
187 		case 'c':
188 		case 'h':
189 			/* ignore common options */
190 			break;
191 		default: /* '?' */
192 			usage_setenv();
193 			exit(EXIT_FAILURE);
194 			break;
195 		}
196 	}
197 	return 0;
198 }
199 
200 int main(int argc, char *argv[])
201 {
202 	const char *lockname = "/var/lock/" CMD_PRINTENV ".lock";
203 	int lockfd = -1;
204 	int retval = EXIT_SUCCESS;
205 	char *_cmdname;
206 
207 	_cmdname = *argv;
208 	if (strrchr(_cmdname, '/') != NULL)
209 		_cmdname = strrchr(_cmdname, '/') + 1;
210 
211 	if (strcmp(_cmdname, CMD_PRINTENV) == 0) {
212 		do_printenv = 1;
213 	} else if (strcmp(_cmdname, CMD_SETENV) == 0) {
214 		do_printenv = 0;
215 	} else {
216 		fprintf(stderr,
217 			"Identity crisis - may be called as `%s' or as `%s' but not as `%s'\n",
218 			CMD_PRINTENV, CMD_SETENV, _cmdname);
219 		exit(EXIT_FAILURE);
220 	}
221 
222 	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 
230 	/* shift parsed flags, jump to non-option arguments */
231 	argc -= optind;
232 	argv += optind;
233 
234 	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 
246 	if (do_printenv) {
247 		if (fw_printenv(argc, argv) != 0)
248 			retval = EXIT_FAILURE;
249 	} else {
250 		if (!setenv_args.script_file) {
251 			if (fw_setenv(argc, argv) != 0)
252 				retval = EXIT_FAILURE;
253 		} else {
254 			if (fw_parse_script(setenv_args.script_file) != 0)
255 				retval = EXIT_FAILURE;
256 		}
257 	}
258 
259 	flock(lockfd, LOCK_UN);
260 	close(lockfd);
261 	return retval;
262 }
263