blob: 2b85d78864561d9414e085cfbd8081fa305112b9 [file] [log] [blame]
wdenk6aff3112002-12-17 01:51:00 +00001/*
Grant Ericksonbc117562008-05-06 20:16:15 -07002 * (C) Copyright 2000-2008
wdenk6aff3112002-12-17 01:51:00 +00003 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
Wolfgang Denk3765b3e2013-10-07 13:07:26 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenk6aff3112002-12-17 01:51:00 +00006 */
7
8/*
wdenk3bac3512003-03-12 10:41:04 +00009 * Command line user interface to firmware (=U-Boot) environment.
wdenk6aff3112002-12-17 01:51:00 +000010 *
11 * Implements:
Grant Ericksonbc117562008-05-06 20:16:15 -070012 * 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.
wdenk6aff3112002-12-17 01:51:00 +000017 * 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,
Grant Ericksonbc117562008-05-06 20:16:15 -070021 * separated by single blank characters, and the
wdenk6aff3112002-12-17 01:51:00 +000022 * resulting string is assigned to the environment
23 * variable "name"
24 */
25
Joe Hershbergere4a223f2012-10-03 09:38:49 +000026#include <fcntl.h>
27#include <getopt.h>
wdenk6aff3112002-12-17 01:51:00 +000028#include <stdio.h>
29#include <string.h>
30#include <stdlib.h>
Joe Hershbergere4a223f2012-10-03 09:38:49 +000031#include <sys/file.h>
32#include <unistd.h>
wdenk6aff3112002-12-17 01:51:00 +000033#include "fw_env.h"
34
35#define CMD_PRINTENV "fw_printenv"
36#define CMD_SETENV "fw_setenv"
37
Stefano Babicbd7b26f2010-05-24 12:08:16 +020038static struct option long_options[] = {
39 {"script", required_argument, NULL, 's'},
40 {"help", no_argument, NULL, 'h'},
41 {NULL, 0, NULL, 0}
42};
43
44void usage(void)
45{
46
47 fprintf(stderr, "fw_printenv/fw_setenv, "
48 "a command line interface to U-Boot environment\n\n"
Daniel Hobi122bc082010-09-15 19:46:26 +020049 "usage:\tfw_printenv [-n] [variable name]\n"
Stefano Babicbd7b26f2010-05-24 12:08:16 +020050 "\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
Joe Hershbergere4a223f2012-10-03 09:38:49 +000071int main(int argc, char *argv[])
wdenk6aff3112002-12-17 01:51:00 +000072{
73 char *p;
74 char *cmdname = *argv;
Stefano Babicbd7b26f2010-05-24 12:08:16 +020075 char *script_file = NULL;
76 int c;
Joe Hershbergere4a223f2012-10-03 09:38:49 +000077 const char *lockname = "/var/lock/" CMD_PRINTENV ".lock";
78 int lockfd = -1;
79 int retval = EXIT_SUCCESS;
80
Mike Frysinger7a546db2012-11-10 19:47:46 +000081 lockfd = open(lockname, O_WRONLY | O_CREAT | O_TRUNC, 0666);
Joe Hershbergere4a223f2012-10-03 09:38:49 +000082 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 }
wdenk6aff3112002-12-17 01:51:00 +000092
93 if ((p = strrchr (cmdname, '/')) != NULL) {
94 cmdname = p + 1;
95 }
96
Daniel Hobi122bc082010-09-15 19:46:26 +020097 while ((c = getopt_long (argc, argv, "ns:h",
Stefano Babicbd7b26f2010-05-24 12:08:16 +020098 long_options, NULL)) != EOF) {
99 switch (c) {
Daniel Hobi122bc082010-09-15 19:46:26 +0200100 case 'n':
101 /* handled in fw_printenv */
102 break;
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200103 case 's':
104 script_file = optarg;
105 break;
106 case 'h':
107 usage();
Joe Hershbergere4a223f2012-10-03 09:38:49 +0000108 goto exit;
Daniel Hobi29ccd7c2010-09-16 14:36:09 +0200109 default: /* '?' */
110 fprintf(stderr, "Try `%s --help' for more information."
111 "\n", cmdname);
Joe Hershbergere4a223f2012-10-03 09:38:49 +0000112 retval = EXIT_FAILURE;
113 goto exit;
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200114 }
115 }
116
wdenk6aff3112002-12-17 01:51:00 +0000117 if (strcmp(cmdname, CMD_PRINTENV) == 0) {
Joe Hershbergere4a223f2012-10-03 09:38:49 +0000118 if (fw_printenv(argc, argv) != 0)
119 retval = EXIT_FAILURE;
wdenk6aff3112002-12-17 01:51:00 +0000120 } else if (strcmp(cmdname, CMD_SETENV) == 0) {
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200121 if (!script_file) {
122 if (fw_setenv(argc, argv) != 0)
Joe Hershbergere4a223f2012-10-03 09:38:49 +0000123 retval = EXIT_FAILURE;
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200124 } else {
125 if (fw_parse_script(script_file) != 0)
Joe Hershbergere4a223f2012-10-03 09:38:49 +0000126 retval = EXIT_FAILURE;
Stefano Babicbd7b26f2010-05-24 12:08:16 +0200127 }
Joe Hershbergere4a223f2012-10-03 09:38:49 +0000128 } 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;
wdenk6aff3112002-12-17 01:51:00 +0000134 }
135
Joe Hershbergere4a223f2012-10-03 09:38:49 +0000136exit:
137 flock(lockfd, LOCK_UN);
138 close(lockfd);
139 return retval;
wdenk6aff3112002-12-17 01:51:00 +0000140}