2002-12-17 01:51:00 +00:00
|
|
|
/*
|
2008-05-07 03:16:15 +00:00
|
|
|
* (C) Copyright 2000-2008
|
2002-12-17 01:51:00 +00:00
|
|
|
* Wolfgang Denk, DENX Software Engineering, wd@denx.de.
|
|
|
|
*
|
2013-10-07 11:07:26 +00:00
|
|
|
* SPDX-License-Identifier: GPL-2.0+
|
2002-12-17 01:51:00 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2003-03-12 10:41:04 +00:00
|
|
|
* Command line user interface to firmware (=U-Boot) environment.
|
2002-12-17 01:51:00 +00:00
|
|
|
*
|
|
|
|
* Implements:
|
2014-03-05 18:59:52 +00:00
|
|
|
* fw_printenv [ -a key ] [[ -n name ] | [ name ... ]]
|
2008-05-07 03:16:15 +00:00
|
|
|
* - prints the value of a single environment variable
|
|
|
|
* "name", the ``name=value'' pairs of one or more
|
|
|
|
* environment variables "name", or the whole
|
|
|
|
* environment if no names are specified.
|
2014-03-05 18:59:52 +00:00
|
|
|
* fw_setenv [ -a key ] name [ value ... ]
|
2002-12-17 01:51:00 +00:00
|
|
|
* - If a name without any values is given, the variable
|
|
|
|
* with this name is deleted from the environment;
|
|
|
|
* otherwise, all "value" arguments are concatenated,
|
2008-05-07 03:16:15 +00:00
|
|
|
* separated by single blank characters, and the
|
2002-12-17 01:51:00 +00:00
|
|
|
* resulting string is assigned to the environment
|
|
|
|
* variable "name"
|
2014-03-05 18:59:52 +00:00
|
|
|
*
|
|
|
|
* If '-a key' is specified, the env block is encrypted with AES 128 CBC.
|
|
|
|
* The 'key' argument is in the format of 32 hexadecimal numbers (16 bytes
|
|
|
|
* of AES key), eg. '-a aabbccddeeff00112233445566778899'.
|
2002-12-17 01:51:00 +00:00
|
|
|
*/
|
|
|
|
|
2012-10-03 09:38:49 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <getopt.h>
|
2002-12-17 01:51:00 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2012-10-03 09:38:49 +00:00
|
|
|
#include <sys/file.h>
|
|
|
|
#include <unistd.h>
|
2002-12-17 01:51:00 +00:00
|
|
|
#include "fw_env.h"
|
|
|
|
|
2015-12-09 12:13:27 +00:00
|
|
|
#define CMD_PRINTENV "fw_printenv"
|
2002-12-17 01:51:00 +00:00
|
|
|
#define CMD_SETENV "fw_setenv"
|
2015-12-09 12:13:27 +00:00
|
|
|
static int do_printenv;
|
2002-12-17 01:51:00 +00:00
|
|
|
|
2010-05-24 10:08:16 +00:00
|
|
|
static struct option long_options[] = {
|
2015-12-09 12:13:27 +00:00
|
|
|
{"aes", required_argument, NULL, 'a'},
|
|
|
|
{"config", required_argument, NULL, 'c'},
|
2010-05-24 10:08:16 +00:00
|
|
|
{"help", no_argument, NULL, 'h'},
|
2015-12-09 12:13:27 +00:00
|
|
|
{"script", required_argument, NULL, 's'},
|
|
|
|
{"noheader", required_argument, NULL, 'n'},
|
2010-05-24 10:08:16 +00:00
|
|
|
{NULL, 0, NULL, 0}
|
|
|
|
};
|
|
|
|
|
2015-12-09 12:13:27 +00:00
|
|
|
void usage_printenv(void)
|
2010-05-24 10:08:16 +00:00
|
|
|
{
|
|
|
|
|
2015-12-09 12:13:27 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"Usage: fw_printenv [OPTIONS]... [VARIABLE]...\n"
|
|
|
|
"Print variables from U-Boot environment\n"
|
|
|
|
"\n"
|
|
|
|
" -h, --help print this help.\n"
|
|
|
|
#ifdef CONFIG_ENV_AES
|
|
|
|
" -a, --aes aes key to access environment\n"
|
2015-10-27 21:34:49 +00:00
|
|
|
#endif
|
2015-12-09 12:13:27 +00:00
|
|
|
#ifdef CONFIG_FILE
|
|
|
|
" -c, --config configuration file, default:" CONFIG_FILE "\n"
|
|
|
|
#endif
|
|
|
|
" -n, --noheader do not repeat variable name in output\n"
|
|
|
|
"\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
void usage_setenv(void)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
"Usage: fw_setenv [OPTIONS]... [VARIABLE]...\n"
|
|
|
|
"Modify variables in U-Boot environment\n"
|
|
|
|
"\n"
|
|
|
|
" -h, --help print this help.\n"
|
|
|
|
#ifdef CONFIG_ENV_AES
|
|
|
|
" -a, --aes aes key to access environment\n"
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_FILE
|
|
|
|
" -c, --config configuration file, default:" CONFIG_FILE "\n"
|
|
|
|
#endif
|
|
|
|
" -s, --script batch mode to minimize writes\n"
|
|
|
|
"\n"
|
|
|
|
"Examples:\n"
|
|
|
|
" fw_setenv foo bar set variable foo equal bar\n"
|
|
|
|
" fw_setenv foo clear variable foo\n"
|
|
|
|
" fw_setenv --script file run batch script\n"
|
2010-05-24 10:08:16 +00:00
|
|
|
"\n"
|
2015-12-09 12:13:27 +00:00
|
|
|
"Script Syntax:\n"
|
|
|
|
" key [space] value\n"
|
2016-03-15 19:11:12 +00:00
|
|
|
" lines starting with '#' are treated as comment\n"
|
2010-05-24 10:08:16 +00:00
|
|
|
"\n"
|
2015-12-09 12:13:27 +00:00
|
|
|
" A variable without value will be deleted. Any number of spaces are\n"
|
|
|
|
" allowed between key and value. Space inside of the value is treated\n"
|
|
|
|
" as part of the value itself.\n"
|
|
|
|
"\n"
|
|
|
|
"Script Example:\n"
|
|
|
|
" netdev eth0\n"
|
|
|
|
" kernel_addr 400000\n"
|
|
|
|
" foo empty empty empty empty empty empty\n"
|
|
|
|
" bar\n"
|
|
|
|
"\n");
|
2010-05-24 10:08:16 +00:00
|
|
|
}
|
|
|
|
|
2015-12-09 12:13:26 +00:00
|
|
|
static void parse_common_args(int argc, char *argv[])
|
2002-12-17 01:51:00 +00:00
|
|
|
{
|
2010-05-24 10:08:16 +00:00
|
|
|
int c;
|
2002-12-17 01:51:00 +00:00
|
|
|
|
2015-12-09 12:13:24 +00:00
|
|
|
#ifdef CONFIG_FILE
|
|
|
|
common_args.config_file = CONFIG_FILE;
|
|
|
|
#endif
|
|
|
|
|
2015-12-09 12:13:26 +00:00
|
|
|
while ((c = getopt_long(argc, argv, ":a:c:h", long_options, NULL)) !=
|
|
|
|
EOF) {
|
2010-05-24 10:08:16 +00:00
|
|
|
switch (c) {
|
2014-03-05 18:59:52 +00:00
|
|
|
case 'a':
|
2015-12-09 12:13:24 +00:00
|
|
|
if (parse_aes_key(optarg, common_args.aes_key)) {
|
|
|
|
fprintf(stderr, "AES key parse error\n");
|
2015-12-09 12:13:26 +00:00
|
|
|
exit(EXIT_FAILURE);
|
2015-12-09 12:13:24 +00:00
|
|
|
}
|
|
|
|
common_args.aes_flag = 1;
|
2014-03-05 18:59:52 +00:00
|
|
|
break;
|
2015-12-09 12:13:24 +00:00
|
|
|
#ifdef CONFIG_FILE
|
2015-10-27 21:34:49 +00:00
|
|
|
case 'c':
|
2015-12-09 12:13:24 +00:00
|
|
|
common_args.config_file = optarg;
|
2015-10-27 21:34:49 +00:00
|
|
|
break;
|
2015-12-09 12:13:24 +00:00
|
|
|
#endif
|
2015-12-09 12:13:26 +00:00
|
|
|
case 'h':
|
2015-12-09 12:13:27 +00:00
|
|
|
do_printenv ? usage_printenv() : usage_setenv();
|
2015-12-09 12:13:26 +00:00
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* ignore unknown options */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Reset getopt for the next pass. */
|
|
|
|
opterr = 1;
|
|
|
|
optind = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int parse_printenv_args(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
int c;
|
|
|
|
|
|
|
|
parse_common_args(argc, argv);
|
|
|
|
|
|
|
|
while ((c = getopt_long(argc, argv, "a:c:ns:h", long_options, NULL)) !=
|
|
|
|
EOF) {
|
|
|
|
switch (c) {
|
2010-09-15 17:46:26 +00:00
|
|
|
case 'n':
|
2015-12-09 12:13:24 +00:00
|
|
|
printenv_args.name_suppress = 1;
|
2010-09-15 17:46:26 +00:00
|
|
|
break;
|
2015-12-09 12:13:26 +00:00
|
|
|
case 'a':
|
|
|
|
case 'c':
|
2015-12-09 12:13:23 +00:00
|
|
|
case 'h':
|
2015-12-09 12:13:26 +00:00
|
|
|
/* ignore common options */
|
2015-12-09 12:13:23 +00:00
|
|
|
break;
|
|
|
|
default: /* '?' */
|
2015-12-09 12:13:27 +00:00
|
|
|
usage_printenv();
|
2015-12-09 12:13:23 +00:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int parse_setenv_args(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
int c;
|
|
|
|
|
2015-12-09 12:13:26 +00:00
|
|
|
parse_common_args(argc, argv);
|
2015-12-09 12:13:24 +00:00
|
|
|
|
2015-12-09 12:13:26 +00:00
|
|
|
while ((c = getopt_long(argc, argv, "a:c:ns:h", long_options, NULL)) !=
|
|
|
|
EOF) {
|
2015-12-09 12:13:23 +00:00
|
|
|
switch (c) {
|
2010-05-24 10:08:16 +00:00
|
|
|
case 's':
|
2015-12-09 12:13:23 +00:00
|
|
|
setenv_args.script_file = optarg;
|
2010-05-24 10:08:16 +00:00
|
|
|
break;
|
2015-12-09 12:13:26 +00:00
|
|
|
case 'a':
|
|
|
|
case 'c':
|
2010-05-24 10:08:16 +00:00
|
|
|
case 'h':
|
2015-12-09 12:13:26 +00:00
|
|
|
/* ignore common options */
|
2015-12-09 12:13:23 +00:00
|
|
|
break;
|
2010-09-16 12:36:09 +00:00
|
|
|
default: /* '?' */
|
2015-12-09 12:13:27 +00:00
|
|
|
usage_setenv();
|
2015-12-09 12:13:23 +00:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
break;
|
2010-05-24 10:08:16 +00:00
|
|
|
}
|
|
|
|
}
|
2015-12-09 12:13:23 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
const char *lockname = "/var/lock/" CMD_PRINTENV ".lock";
|
|
|
|
int lockfd = -1;
|
|
|
|
int retval = EXIT_SUCCESS;
|
2015-12-09 12:13:27 +00:00
|
|
|
char *_cmdname;
|
2015-12-09 12:13:23 +00:00
|
|
|
|
2015-12-09 12:13:27 +00:00
|
|
|
_cmdname = *argv;
|
|
|
|
if (strrchr(_cmdname, '/') != NULL)
|
|
|
|
_cmdname = strrchr(_cmdname, '/') + 1;
|
2015-12-09 12:13:23 +00:00
|
|
|
|
2015-12-09 12:13:27 +00:00
|
|
|
if (strcmp(_cmdname, CMD_PRINTENV) == 0) {
|
|
|
|
do_printenv = 1;
|
|
|
|
} else if (strcmp(_cmdname, CMD_SETENV) == 0) {
|
|
|
|
do_printenv = 0;
|
2015-12-09 12:13:23 +00:00
|
|
|
} else {
|
|
|
|
fprintf(stderr,
|
|
|
|
"Identity crisis - may be called as `%s' or as `%s' but not as `%s'\n",
|
2015-12-09 12:13:27 +00:00
|
|
|
CMD_PRINTENV, CMD_SETENV, _cmdname);
|
2015-12-09 12:13:23 +00:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2015-12-09 12:13:27 +00:00
|
|
|
if (do_printenv) {
|
|
|
|
if (parse_printenv_args(argc, argv))
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
} else {
|
|
|
|
if (parse_setenv_args(argc, argv))
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2015-12-09 12:13:25 +00:00
|
|
|
/* shift parsed flags, jump to non-option arguments */
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
2015-12-09 12:13:23 +00:00
|
|
|
lockfd = open(lockname, O_WRONLY | O_CREAT | O_TRUNC, 0666);
|
|
|
|
if (-1 == lockfd) {
|
|
|
|
fprintf(stderr, "Error opening lock file %s\n", lockname);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (-1 == flock(lockfd, LOCK_EX)) {
|
|
|
|
fprintf(stderr, "Error locking file %s\n", lockname);
|
|
|
|
close(lockfd);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2010-05-24 10:08:16 +00:00
|
|
|
|
2015-12-09 12:13:27 +00:00
|
|
|
if (do_printenv) {
|
2012-10-03 09:38:49 +00:00
|
|
|
if (fw_printenv(argc, argv) != 0)
|
|
|
|
retval = EXIT_FAILURE;
|
2015-12-09 12:13:27 +00:00
|
|
|
} else {
|
2015-12-09 12:13:23 +00:00
|
|
|
if (!setenv_args.script_file) {
|
2010-05-24 10:08:16 +00:00
|
|
|
if (fw_setenv(argc, argv) != 0)
|
2012-10-03 09:38:49 +00:00
|
|
|
retval = EXIT_FAILURE;
|
2010-05-24 10:08:16 +00:00
|
|
|
} else {
|
2015-12-09 12:13:23 +00:00
|
|
|
if (fw_parse_script(setenv_args.script_file) != 0)
|
2012-10-03 09:38:49 +00:00
|
|
|
retval = EXIT_FAILURE;
|
2010-05-24 10:08:16 +00:00
|
|
|
}
|
2002-12-17 01:51:00 +00:00
|
|
|
}
|
|
|
|
|
2012-10-03 09:38:49 +00:00
|
|
|
flock(lockfd, LOCK_UN);
|
|
|
|
close(lockfd);
|
|
|
|
return retval;
|
2002-12-17 01:51:00 +00:00
|
|
|
}
|