xref: /rk3399_rockchip-uboot/tools/env/fw_env_main.c (revision e4a223f04de2e271682f26d7b981c0012f6a459f)
16aff3115Swdenk /*
2bc11756dSGrant Erickson  * (C) Copyright 2000-2008
36aff3115Swdenk  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
46aff3115Swdenk  *
56aff3115Swdenk  * See file CREDITS for list of people who contributed to this
66aff3115Swdenk  * project.
76aff3115Swdenk  *
86aff3115Swdenk  * This program is free software; you can redistribute it and/or
96aff3115Swdenk  * modify it under the terms of the GNU General Public License as
106aff3115Swdenk  * published by the Free Software Foundation; either version 2 of
116aff3115Swdenk  * the License, or (at your option) any later version.
126aff3115Swdenk  *
136aff3115Swdenk  * This program is distributed in the hope that it will be useful,
146aff3115Swdenk  * but WITHOUT ANY WARRANTY; without even the implied warranty of
156aff3115Swdenk  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the
166aff3115Swdenk  * GNU General Public License for more details.
176aff3115Swdenk  *
186aff3115Swdenk  * You should have received a copy of the GNU General Public License
196aff3115Swdenk  * along with this program; if not, write to the Free Software
206aff3115Swdenk  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
216aff3115Swdenk  * MA 02111-1307 USA
226aff3115Swdenk  */
236aff3115Swdenk 
246aff3115Swdenk /*
253bac3513Swdenk  * Command line user interface to firmware (=U-Boot) environment.
266aff3115Swdenk  *
276aff3115Swdenk  * Implements:
28bc11756dSGrant Erickson  *	fw_printenv [[ -n name ] | [ name ... ]]
29bc11756dSGrant Erickson  *              - prints the value of a single environment variable
30bc11756dSGrant Erickson  *                "name", the ``name=value'' pairs of one or more
31bc11756dSGrant Erickson  *                environment variables "name", or the whole
32bc11756dSGrant Erickson  *                environment if no names are specified.
336aff3115Swdenk  *	fw_setenv name [ value ... ]
346aff3115Swdenk  *		- If a name without any values is given, the variable
356aff3115Swdenk  *		  with this name is deleted from the environment;
366aff3115Swdenk  *		  otherwise, all "value" arguments are concatenated,
37bc11756dSGrant Erickson  *		  separated by single blank characters, and the
386aff3115Swdenk  *		  resulting string is assigned to the environment
396aff3115Swdenk  *		  variable "name"
406aff3115Swdenk  */
416aff3115Swdenk 
42*e4a223f0SJoe Hershberger #include <fcntl.h>
43*e4a223f0SJoe Hershberger #include <getopt.h>
446aff3115Swdenk #include <stdio.h>
456aff3115Swdenk #include <string.h>
466aff3115Swdenk #include <stdlib.h>
47*e4a223f0SJoe Hershberger #include <sys/file.h>
48*e4a223f0SJoe Hershberger #include <unistd.h>
496aff3115Swdenk #include "fw_env.h"
506aff3115Swdenk 
516aff3115Swdenk #define	CMD_PRINTENV	"fw_printenv"
526aff3115Swdenk #define CMD_SETENV	"fw_setenv"
536aff3115Swdenk 
54bd7b26f8SStefano Babic static struct option long_options[] = {
55bd7b26f8SStefano Babic 	{"script", required_argument, NULL, 's'},
56bd7b26f8SStefano Babic 	{"help", no_argument, NULL, 'h'},
57bd7b26f8SStefano Babic 	{NULL, 0, NULL, 0}
58bd7b26f8SStefano Babic };
59bd7b26f8SStefano Babic 
60bd7b26f8SStefano Babic void usage(void)
61bd7b26f8SStefano Babic {
62bd7b26f8SStefano Babic 
63bd7b26f8SStefano Babic 	fprintf(stderr, "fw_printenv/fw_setenv, "
64bd7b26f8SStefano Babic 		"a command line interface to U-Boot environment\n\n"
65122bc088SDaniel Hobi 		"usage:\tfw_printenv [-n] [variable name]\n"
66bd7b26f8SStefano Babic 		"\tfw_setenv [variable name] [variable value]\n"
67bd7b26f8SStefano Babic 		"\tfw_setenv -s [ file ]\n"
68bd7b26f8SStefano Babic 		"\tfw_setenv -s - < [ file ]\n\n"
69bd7b26f8SStefano Babic 		"The file passed as argument contains only pairs "
70bd7b26f8SStefano Babic 		"name / value\n"
71bd7b26f8SStefano Babic 		"Example:\n"
72bd7b26f8SStefano Babic 		"# Any line starting with # is treated as comment\n"
73bd7b26f8SStefano Babic 		"\n"
74bd7b26f8SStefano Babic 		"\t      netdev         eth0\n"
75bd7b26f8SStefano Babic 		"\t      kernel_addr    400000\n"
76bd7b26f8SStefano Babic 		"\t      var1\n"
77bd7b26f8SStefano Babic 		"\t      var2          The quick brown fox jumps over the "
78bd7b26f8SStefano Babic 		"lazy dog\n"
79bd7b26f8SStefano Babic 		"\n"
80bd7b26f8SStefano Babic 		"A variable without value will be dropped. It is possible\n"
81bd7b26f8SStefano Babic 		"to put any number of spaces between the fields, but any\n"
82bd7b26f8SStefano Babic 		"space inside the value is treated as part of the value "
83bd7b26f8SStefano Babic 		"itself.\n\n"
84bd7b26f8SStefano Babic 	);
85bd7b26f8SStefano Babic }
86bd7b26f8SStefano Babic 
87*e4a223f0SJoe Hershberger int main(int argc, char *argv[])
886aff3115Swdenk {
896aff3115Swdenk 	char *p;
906aff3115Swdenk 	char *cmdname = *argv;
91bd7b26f8SStefano Babic 	char *script_file = NULL;
92bd7b26f8SStefano Babic 	int c;
93*e4a223f0SJoe Hershberger 	const char *lockname = "/var/lock/" CMD_PRINTENV ".lock";
94*e4a223f0SJoe Hershberger 	int lockfd = -1;
95*e4a223f0SJoe Hershberger 	int retval = EXIT_SUCCESS;
96*e4a223f0SJoe Hershberger 
97*e4a223f0SJoe Hershberger 	lockfd = open(lockname, O_WRONLY | O_CREAT | O_TRUNC);
98*e4a223f0SJoe Hershberger 	if (-1 == lockfd) {
99*e4a223f0SJoe Hershberger 		fprintf(stderr, "Error opening lock file %s\n", lockname);
100*e4a223f0SJoe Hershberger 		return EXIT_FAILURE;
101*e4a223f0SJoe Hershberger 	}
102*e4a223f0SJoe Hershberger 
103*e4a223f0SJoe Hershberger 	if (-1 == flock(lockfd, LOCK_EX)) {
104*e4a223f0SJoe Hershberger 		fprintf(stderr, "Error locking file %s\n", lockname);
105*e4a223f0SJoe Hershberger 		close(lockfd);
106*e4a223f0SJoe Hershberger 		return EXIT_FAILURE;
107*e4a223f0SJoe Hershberger 	}
1086aff3115Swdenk 
1096aff3115Swdenk 	if ((p = strrchr (cmdname, '/')) != NULL) {
1106aff3115Swdenk 		cmdname = p + 1;
1116aff3115Swdenk 	}
1126aff3115Swdenk 
113122bc088SDaniel Hobi 	while ((c = getopt_long (argc, argv, "ns:h",
114bd7b26f8SStefano Babic 		long_options, NULL)) != EOF) {
115bd7b26f8SStefano Babic 		switch (c) {
116122bc088SDaniel Hobi 		case 'n':
117122bc088SDaniel Hobi 			/* handled in fw_printenv */
118122bc088SDaniel Hobi 			break;
119bd7b26f8SStefano Babic 		case 's':
120bd7b26f8SStefano Babic 			script_file = optarg;
121bd7b26f8SStefano Babic 			break;
122bd7b26f8SStefano Babic 		case 'h':
123bd7b26f8SStefano Babic 			usage();
124*e4a223f0SJoe Hershberger 			goto exit;
12529ccd7c3SDaniel Hobi 		default: /* '?' */
12629ccd7c3SDaniel Hobi 			fprintf(stderr, "Try `%s --help' for more information."
12729ccd7c3SDaniel Hobi 				"\n", cmdname);
128*e4a223f0SJoe Hershberger 			retval = EXIT_FAILURE;
129*e4a223f0SJoe Hershberger 			goto exit;
130bd7b26f8SStefano Babic 		}
131bd7b26f8SStefano Babic 	}
132bd7b26f8SStefano Babic 
1336aff3115Swdenk 	if (strcmp(cmdname, CMD_PRINTENV) == 0) {
134bc11756dSGrant Erickson 		if (fw_printenv(argc, argv) != 0)
135*e4a223f0SJoe Hershberger 			retval = EXIT_FAILURE;
1366aff3115Swdenk 	} else if (strcmp(cmdname, CMD_SETENV) == 0) {
137bd7b26f8SStefano Babic 		if (!script_file) {
1386aff3115Swdenk 			if (fw_setenv(argc, argv) != 0)
139*e4a223f0SJoe Hershberger 				retval = EXIT_FAILURE;
140bd7b26f8SStefano Babic 		} else {
141bd7b26f8SStefano Babic 			if (fw_parse_script(script_file) != 0)
142*e4a223f0SJoe Hershberger 				retval = EXIT_FAILURE;
143bd7b26f8SStefano Babic 		}
144*e4a223f0SJoe Hershberger 	} else {
1456aff3115Swdenk 		fprintf(stderr,
1466aff3115Swdenk 			"Identity crisis - may be called as `" CMD_PRINTENV
1476aff3115Swdenk 			"' or as `" CMD_SETENV "' but not as `%s'\n",
1486aff3115Swdenk 			cmdname);
149*e4a223f0SJoe Hershberger 		retval = EXIT_FAILURE;
150*e4a223f0SJoe Hershberger 	}
151*e4a223f0SJoe Hershberger 
152*e4a223f0SJoe Hershberger exit:
153*e4a223f0SJoe Hershberger 	flock(lockfd, LOCK_UN);
154*e4a223f0SJoe Hershberger 	close(lockfd);
155*e4a223f0SJoe Hershberger 	return retval;
1566aff3115Swdenk }
157