xref: /openbmc/u-boot/tools/env/fw_env_main.c (revision ee7bb5be)
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 void usage_printenv(void)
53 {
54 
55 	fprintf(stderr,
56 		"Usage: fw_printenv [OPTIONS]... [VARIABLE]...\n"
57 		"Print variables from U-Boot environment\n"
58 		"\n"
59 		" -h, --help           print this help.\n"
60 #ifdef CONFIG_ENV_AES
61 		" -a, --aes            aes key to access environment\n"
62 #endif
63 #ifdef CONFIG_FILE
64 		" -c, --config         configuration file, default:" CONFIG_FILE "\n"
65 #endif
66 		" -n, --noheader       do not repeat variable name in output\n"
67 		"\n");
68 }
69 
70 void usage_setenv(void)
71 {
72 	fprintf(stderr,
73 		"Usage: fw_setenv [OPTIONS]... [VARIABLE]...\n"
74 		"Modify variables in U-Boot environment\n"
75 		"\n"
76 		" -h, --help           print this help.\n"
77 #ifdef CONFIG_ENV_AES
78 		" -a, --aes            aes key to access environment\n"
79 #endif
80 #ifdef CONFIG_FILE
81 		" -c, --config         configuration file, default:" CONFIG_FILE "\n"
82 #endif
83 		" -s, --script         batch mode to minimize writes\n"
84 		"\n"
85 		"Examples:\n"
86 		"  fw_setenv foo bar   set variable foo equal bar\n"
87 		"  fw_setenv foo       clear variable foo\n"
88 		"  fw_setenv --script file run batch script\n"
89 		"\n"
90 		"Script Syntax:\n"
91 		"  key [space] value\n"
92 		"  lines starting with '#' are treated as comment\n"
93 		"\n"
94 		"  A variable without value will be deleted. Any number of spaces are\n"
95 		"  allowed between key and value. Space inside of the value is treated\n"
96 		"  as part of the value itself.\n"
97 		"\n"
98 		"Script Example:\n"
99 		"  netdev         eth0\n"
100 		"  kernel_addr    400000\n"
101 		"  foo            empty empty empty    empty empty empty\n"
102 		"  bar\n"
103 		"\n");
104 }
105 
106 static void parse_common_args(int argc, char *argv[])
107 {
108 	int c;
109 
110 #ifdef CONFIG_FILE
111 	common_args.config_file = CONFIG_FILE;
112 #endif
113 
114 	while ((c = getopt_long(argc, argv, ":a:c:h", long_options, NULL)) !=
115 	       EOF) {
116 		switch (c) {
117 		case 'a':
118 			if (parse_aes_key(optarg, common_args.aes_key)) {
119 				fprintf(stderr, "AES key parse error\n");
120 				exit(EXIT_FAILURE);
121 			}
122 			common_args.aes_flag = 1;
123 			break;
124 #ifdef CONFIG_FILE
125 		case 'c':
126 			common_args.config_file = optarg;
127 			break;
128 #endif
129 		case 'h':
130 			do_printenv ? usage_printenv() : usage_setenv();
131 			exit(EXIT_SUCCESS);
132 			break;
133 		default:
134 			/* ignore unknown options */
135 			break;
136 		}
137 	}
138 
139 	/* Reset getopt for the next pass. */
140 	opterr = 1;
141 	optind = 1;
142 }
143 
144 int parse_printenv_args(int argc, char *argv[])
145 {
146 	int c;
147 
148 	parse_common_args(argc, argv);
149 
150 	while ((c = getopt_long(argc, argv, "a:c:ns:h", long_options, NULL)) !=
151 	       EOF) {
152 		switch (c) {
153 		case 'n':
154 			printenv_args.name_suppress = 1;
155 			break;
156 		case 'a':
157 		case 'c':
158 		case 'h':
159 			/* ignore common options */
160 			break;
161 		default: /* '?' */
162 			usage_printenv();
163 			exit(EXIT_FAILURE);
164 			break;
165 		}
166 	}
167 	return 0;
168 }
169 
170 int parse_setenv_args(int argc, char *argv[])
171 {
172 	int c;
173 
174 	parse_common_args(argc, argv);
175 
176 	while ((c = getopt_long(argc, argv, "a:c:ns:h", long_options, NULL)) !=
177 	       EOF) {
178 		switch (c) {
179 		case 's':
180 			setenv_args.script_file = optarg;
181 			break;
182 		case 'a':
183 		case 'c':
184 		case 'h':
185 			/* ignore common options */
186 			break;
187 		default: /* '?' */
188 			usage_setenv();
189 			exit(EXIT_FAILURE);
190 			break;
191 		}
192 	}
193 	return 0;
194 }
195 
196 int main(int argc, char *argv[])
197 {
198 	const char *lockname = "/var/lock/" CMD_PRINTENV ".lock";
199 	int lockfd = -1;
200 	int retval = EXIT_SUCCESS;
201 	char *_cmdname;
202 
203 	_cmdname = *argv;
204 	if (strrchr(_cmdname, '/') != NULL)
205 		_cmdname = strrchr(_cmdname, '/') + 1;
206 
207 	if (strcmp(_cmdname, CMD_PRINTENV) == 0) {
208 		do_printenv = 1;
209 	} else if (strcmp(_cmdname, CMD_SETENV) == 0) {
210 		do_printenv = 0;
211 	} else {
212 		fprintf(stderr,
213 			"Identity crisis - may be called as `%s' or as `%s' but not as `%s'\n",
214 			CMD_PRINTENV, CMD_SETENV, _cmdname);
215 		exit(EXIT_FAILURE);
216 	}
217 
218 	if (do_printenv) {
219 		if (parse_printenv_args(argc, argv))
220 			exit(EXIT_FAILURE);
221 	} else {
222 		if (parse_setenv_args(argc, argv))
223 			exit(EXIT_FAILURE);
224 	}
225 
226 	/* shift parsed flags, jump to non-option arguments */
227 	argc -= optind;
228 	argv += optind;
229 
230 	lockfd = open(lockname, O_WRONLY | O_CREAT | O_TRUNC, 0666);
231 	if (-1 == lockfd) {
232 		fprintf(stderr, "Error opening lock file %s\n", lockname);
233 		return EXIT_FAILURE;
234 	}
235 
236 	if (-1 == flock(lockfd, LOCK_EX)) {
237 		fprintf(stderr, "Error locking file %s\n", lockname);
238 		close(lockfd);
239 		return EXIT_FAILURE;
240 	}
241 
242 	if (do_printenv) {
243 		if (fw_printenv(argc, argv) != 0)
244 			retval = EXIT_FAILURE;
245 	} else {
246 		if (!setenv_args.script_file) {
247 			if (fw_setenv(argc, argv) != 0)
248 				retval = EXIT_FAILURE;
249 		} else {
250 			if (fw_parse_script(setenv_args.script_file) != 0)
251 				retval = EXIT_FAILURE;
252 		}
253 	}
254 
255 	flock(lockfd, LOCK_UN);
256 	close(lockfd);
257 	return retval;
258 }
259