blob: 234c0615443c1547044be0461d132f080df368c5 [file] [log] [blame]
wdenk34b613e2002-12-17 01:51:00 +00001/*
Grant Erickson73a4e5e2008-05-06 20:16:15 -07002 * (C) Copyright 2000-2008
wdenk34b613e2002-12-17 01:51:00 +00003 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
Wolfgang Denkbd8ec7e2013-10-07 13:07:26 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenk34b613e2002-12-17 01:51:00 +00006 */
7
8/*
wdenkef5fe752003-03-12 10:41:04 +00009 * Command line user interface to firmware (=U-Boot) environment.
wdenk34b613e2002-12-17 01:51:00 +000010 *
11 * Implements:
Marek Vasut05ed2a52014-03-05 19:59:52 +010012 * fw_printenv [ -a key ] [[ -n name ] | [ name ... ]]
Grant Erickson73a4e5e2008-05-06 20:16:15 -070013 * - 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.
Marek Vasut05ed2a52014-03-05 19:59:52 +010017 * fw_setenv [ -a key ] name [ value ... ]
wdenk34b613e2002-12-17 01:51:00 +000018 * - 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 Erickson73a4e5e2008-05-06 20:16:15 -070021 * separated by single blank characters, and the
wdenk34b613e2002-12-17 01:51:00 +000022 * resulting string is assigned to the environment
23 * variable "name"
Marek Vasut05ed2a52014-03-05 19:59:52 +010024 *
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'.
wdenk34b613e2002-12-17 01:51:00 +000028 */
29
Joe Hershberger3f1f1752012-10-03 09:38:49 +000030#include <fcntl.h>
31#include <getopt.h>
wdenk34b613e2002-12-17 01:51:00 +000032#include <stdio.h>
33#include <string.h>
34#include <stdlib.h>
Joe Hershberger3f1f1752012-10-03 09:38:49 +000035#include <sys/file.h>
36#include <unistd.h>
wdenk34b613e2002-12-17 01:51:00 +000037#include "fw_env.h"
38
39#define CMD_PRINTENV "fw_printenv"
40#define CMD_SETENV "fw_setenv"
41
Stefano Babicada628b2010-05-24 12:08:16 +020042static struct option long_options[] = {
43 {"script", required_argument, NULL, 's'},
44 {"help", no_argument, NULL, 'h'},
45 {NULL, 0, NULL, 0}
46};
47
48void usage(void)
49{
50
51 fprintf(stderr, "fw_printenv/fw_setenv, "
52 "a command line interface to U-Boot environment\n\n"
Michael Heimpold211e17b2015-10-27 22:34:49 +010053#ifndef CONFIG_FILE
Marek Vasut05ed2a52014-03-05 19:59:52 +010054 "usage:\tfw_printenv [-a key] [-n] [variable name]\n"
55 "\tfw_setenv [-a key] [variable name] [variable value]\n"
Michael Heimpold211e17b2015-10-27 22:34:49 +010056#else
57 "usage:\tfw_printenv [-c /my/fw_env.config] [-a key] [-n] [variable name]\n"
58 "\tfw_setenv [-c /my/fw_env.config] [-a key] [variable name] [variable value]\n"
59#endif
Stefano Babicada628b2010-05-24 12:08:16 +020060 "\tfw_setenv -s [ file ]\n"
61 "\tfw_setenv -s - < [ file ]\n\n"
62 "The file passed as argument contains only pairs "
63 "name / value\n"
64 "Example:\n"
65 "# Any line starting with # is treated as comment\n"
66 "\n"
67 "\t netdev eth0\n"
68 "\t kernel_addr 400000\n"
69 "\t var1\n"
70 "\t var2 The quick brown fox jumps over the "
71 "lazy dog\n"
72 "\n"
73 "A variable without value will be dropped. It is possible\n"
74 "to put any number of spaces between the fields, but any\n"
75 "space inside the value is treated as part of the value "
76 "itself.\n\n"
77 );
78}
79
Joe Hershberger3f1f1752012-10-03 09:38:49 +000080int main(int argc, char *argv[])
wdenk34b613e2002-12-17 01:51:00 +000081{
82 char *p;
83 char *cmdname = *argv;
Stefano Babicada628b2010-05-24 12:08:16 +020084 char *script_file = NULL;
85 int c;
Joe Hershberger3f1f1752012-10-03 09:38:49 +000086 const char *lockname = "/var/lock/" CMD_PRINTENV ".lock";
87 int lockfd = -1;
88 int retval = EXIT_SUCCESS;
89
Mike Frysinger54512f72012-11-10 19:47:46 +000090 lockfd = open(lockname, O_WRONLY | O_CREAT | O_TRUNC, 0666);
Joe Hershberger3f1f1752012-10-03 09:38:49 +000091 if (-1 == lockfd) {
92 fprintf(stderr, "Error opening lock file %s\n", lockname);
93 return EXIT_FAILURE;
94 }
95
96 if (-1 == flock(lockfd, LOCK_EX)) {
97 fprintf(stderr, "Error locking file %s\n", lockname);
98 close(lockfd);
99 return EXIT_FAILURE;
100 }
wdenk34b613e2002-12-17 01:51:00 +0000101
102 if ((p = strrchr (cmdname, '/')) != NULL) {
103 cmdname = p + 1;
104 }
105
Michael Heimpold211e17b2015-10-27 22:34:49 +0100106 while ((c = getopt_long (argc, argv, "a:c:ns:h",
Stefano Babicada628b2010-05-24 12:08:16 +0200107 long_options, NULL)) != EOF) {
108 switch (c) {
Marek Vasut05ed2a52014-03-05 19:59:52 +0100109 case 'a':
110 /* AES key, handled later */
111 break;
Michael Heimpold211e17b2015-10-27 22:34:49 +0100112 case 'c':
113 /* handled later */
114 break;
Daniel Hobi6c554262010-09-15 19:46:26 +0200115 case 'n':
116 /* handled in fw_printenv */
117 break;
Stefano Babicada628b2010-05-24 12:08:16 +0200118 case 's':
119 script_file = optarg;
120 break;
121 case 'h':
122 usage();
Joe Hershberger3f1f1752012-10-03 09:38:49 +0000123 goto exit;
Daniel Hobi6a20a322010-09-16 14:36:09 +0200124 default: /* '?' */
125 fprintf(stderr, "Try `%s --help' for more information."
126 "\n", cmdname);
Joe Hershberger3f1f1752012-10-03 09:38:49 +0000127 retval = EXIT_FAILURE;
128 goto exit;
Stefano Babicada628b2010-05-24 12:08:16 +0200129 }
130 }
131
wdenk34b613e2002-12-17 01:51:00 +0000132 if (strcmp(cmdname, CMD_PRINTENV) == 0) {
Joe Hershberger3f1f1752012-10-03 09:38:49 +0000133 if (fw_printenv(argc, argv) != 0)
134 retval = EXIT_FAILURE;
wdenk34b613e2002-12-17 01:51:00 +0000135 } else if (strcmp(cmdname, CMD_SETENV) == 0) {
Stefano Babicada628b2010-05-24 12:08:16 +0200136 if (!script_file) {
137 if (fw_setenv(argc, argv) != 0)
Joe Hershberger3f1f1752012-10-03 09:38:49 +0000138 retval = EXIT_FAILURE;
Stefano Babicada628b2010-05-24 12:08:16 +0200139 } else {
140 if (fw_parse_script(script_file) != 0)
Joe Hershberger3f1f1752012-10-03 09:38:49 +0000141 retval = EXIT_FAILURE;
Stefano Babicada628b2010-05-24 12:08:16 +0200142 }
Joe Hershberger3f1f1752012-10-03 09:38:49 +0000143 } else {
144 fprintf(stderr,
145 "Identity crisis - may be called as `" CMD_PRINTENV
146 "' or as `" CMD_SETENV "' but not as `%s'\n",
147 cmdname);
148 retval = EXIT_FAILURE;
wdenk34b613e2002-12-17 01:51:00 +0000149 }
150
Joe Hershberger3f1f1752012-10-03 09:38:49 +0000151exit:
152 flock(lockfd, LOCK_UN);
153 close(lockfd);
154 return retval;
wdenk34b613e2002-12-17 01:51:00 +0000155}