xref: /openbmc/u-boot/tools/env/fw_env_main.c (revision 02b3bf39)
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 [[ -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 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 
26 #include <fcntl.h>
27 #include <getopt.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <stdlib.h>
31 #include <sys/file.h>
32 #include <unistd.h>
33 #include "fw_env.h"
34 
35 #define	CMD_PRINTENV	"fw_printenv"
36 #define CMD_SETENV	"fw_setenv"
37 
38 static struct option long_options[] = {
39 	{"script", required_argument, NULL, 's'},
40 	{"help", no_argument, NULL, 'h'},
41 	{NULL, 0, NULL, 0}
42 };
43 
44 void usage(void)
45 {
46 
47 	fprintf(stderr, "fw_printenv/fw_setenv, "
48 		"a command line interface to U-Boot environment\n\n"
49 		"usage:\tfw_printenv [-n] [variable name]\n"
50 		"\tfw_setenv [variable name] [variable value]\n"
51 		"\tfw_setenv -s [ file ]\n"
52 		"\tfw_setenv -s - < [ file ]\n\n"
53 		"The file passed as argument contains only pairs "
54 		"name / value\n"
55 		"Example:\n"
56 		"# Any line starting with # is treated as comment\n"
57 		"\n"
58 		"\t      netdev         eth0\n"
59 		"\t      kernel_addr    400000\n"
60 		"\t      var1\n"
61 		"\t      var2          The quick brown fox jumps over the "
62 		"lazy dog\n"
63 		"\n"
64 		"A variable without value will be dropped. It is possible\n"
65 		"to put any number of spaces between the fields, but any\n"
66 		"space inside the value is treated as part of the value "
67 		"itself.\n\n"
68 	);
69 }
70 
71 int main(int argc, char *argv[])
72 {
73 	char *p;
74 	char *cmdname = *argv;
75 	char *script_file = NULL;
76 	int c;
77 	const char *lockname = "/var/lock/" CMD_PRINTENV ".lock";
78 	int lockfd = -1;
79 	int retval = EXIT_SUCCESS;
80 
81 	lockfd = open(lockname, O_WRONLY | O_CREAT | O_TRUNC, 0666);
82 	if (-1 == lockfd) {
83 		fprintf(stderr, "Error opening lock file %s\n", lockname);
84 		return EXIT_FAILURE;
85 	}
86 
87 	if (-1 == flock(lockfd, LOCK_EX)) {
88 		fprintf(stderr, "Error locking file %s\n", lockname);
89 		close(lockfd);
90 		return EXIT_FAILURE;
91 	}
92 
93 	if ((p = strrchr (cmdname, '/')) != NULL) {
94 		cmdname = p + 1;
95 	}
96 
97 	while ((c = getopt_long (argc, argv, "ns:h",
98 		long_options, NULL)) != EOF) {
99 		switch (c) {
100 		case 'n':
101 			/* handled in fw_printenv */
102 			break;
103 		case 's':
104 			script_file = optarg;
105 			break;
106 		case 'h':
107 			usage();
108 			goto exit;
109 		default: /* '?' */
110 			fprintf(stderr, "Try `%s --help' for more information."
111 				"\n", cmdname);
112 			retval = EXIT_FAILURE;
113 			goto exit;
114 		}
115 	}
116 
117 	if (strcmp(cmdname, CMD_PRINTENV) == 0) {
118 		if (fw_printenv(argc, argv) != 0)
119 			retval = EXIT_FAILURE;
120 	} else if (strcmp(cmdname, CMD_SETENV) == 0) {
121 		if (!script_file) {
122 			if (fw_setenv(argc, argv) != 0)
123 				retval = EXIT_FAILURE;
124 		} else {
125 			if (fw_parse_script(script_file) != 0)
126 				retval = EXIT_FAILURE;
127 		}
128 	} else {
129 		fprintf(stderr,
130 			"Identity crisis - may be called as `" CMD_PRINTENV
131 			"' or as `" CMD_SETENV "' but not as `%s'\n",
132 			cmdname);
133 		retval = EXIT_FAILURE;
134 	}
135 
136 exit:
137 	flock(lockfd, LOCK_UN);
138 	close(lockfd);
139 	return retval;
140 }
141