wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1 | /* |
Grant Erickson | bc11756 | 2008-05-06 20:16:15 -0700 | [diff] [blame] | 2 | * (C) Copyright 2000-2008 |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
Wolfgang Denk | 3765b3e | 2013-10-07 13:07:26 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | /* |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 9 | * Command line user interface to firmware (=U-Boot) environment. |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 10 | * |
| 11 | * Implements: |
Marek Vasut | a8a752c | 2014-03-05 19:59:52 +0100 | [diff] [blame] | 12 | * fw_printenv [ -a key ] [[ -n name ] | [ name ... ]] |
Grant Erickson | bc11756 | 2008-05-06 20:16:15 -0700 | [diff] [blame] | 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. |
Marek Vasut | a8a752c | 2014-03-05 19:59:52 +0100 | [diff] [blame] | 17 | * fw_setenv [ -a key ] name [ value ... ] |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 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 Erickson | bc11756 | 2008-05-06 20:16:15 -0700 | [diff] [blame] | 21 | * separated by single blank characters, and the |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 22 | * resulting string is assigned to the environment |
| 23 | * variable "name" |
Marek Vasut | a8a752c | 2014-03-05 19:59:52 +0100 | [diff] [blame] | 24 | * |
| 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'. |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 28 | */ |
| 29 | |
Joe Hershberger | e4a223f | 2012-10-03 09:38:49 +0000 | [diff] [blame] | 30 | #include <fcntl.h> |
| 31 | #include <getopt.h> |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 32 | #include <stdio.h> |
| 33 | #include <string.h> |
| 34 | #include <stdlib.h> |
Joe Hershberger | e4a223f | 2012-10-03 09:38:49 +0000 | [diff] [blame] | 35 | #include <sys/file.h> |
| 36 | #include <unistd.h> |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 37 | #include "fw_env.h" |
| 38 | |
| 39 | #define CMD_PRINTENV "fw_printenv" |
| 40 | #define CMD_SETENV "fw_setenv" |
| 41 | |
Stefano Babic | bd7b26f | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 42 | static struct option long_options[] = { |
| 43 | {"script", required_argument, NULL, 's'}, |
| 44 | {"help", no_argument, NULL, 'h'}, |
| 45 | {NULL, 0, NULL, 0} |
| 46 | }; |
| 47 | |
| 48 | void usage(void) |
| 49 | { |
| 50 | |
| 51 | fprintf(stderr, "fw_printenv/fw_setenv, " |
| 52 | "a command line interface to U-Boot environment\n\n" |
Marek Vasut | a8a752c | 2014-03-05 19:59:52 +0100 | [diff] [blame] | 53 | "usage:\tfw_printenv [-a key] [-n] [variable name]\n" |
| 54 | "\tfw_setenv [-a key] [variable name] [variable value]\n" |
Stefano Babic | bd7b26f | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 55 | "\tfw_setenv -s [ file ]\n" |
| 56 | "\tfw_setenv -s - < [ file ]\n\n" |
| 57 | "The file passed as argument contains only pairs " |
| 58 | "name / value\n" |
| 59 | "Example:\n" |
| 60 | "# Any line starting with # is treated as comment\n" |
| 61 | "\n" |
| 62 | "\t netdev eth0\n" |
| 63 | "\t kernel_addr 400000\n" |
| 64 | "\t var1\n" |
| 65 | "\t var2 The quick brown fox jumps over the " |
| 66 | "lazy dog\n" |
| 67 | "\n" |
| 68 | "A variable without value will be dropped. It is possible\n" |
| 69 | "to put any number of spaces between the fields, but any\n" |
| 70 | "space inside the value is treated as part of the value " |
| 71 | "itself.\n\n" |
| 72 | ); |
| 73 | } |
| 74 | |
Joe Hershberger | e4a223f | 2012-10-03 09:38:49 +0000 | [diff] [blame] | 75 | int main(int argc, char *argv[]) |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 76 | { |
| 77 | char *p; |
| 78 | char *cmdname = *argv; |
Stefano Babic | bd7b26f | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 79 | char *script_file = NULL; |
| 80 | int c; |
Joe Hershberger | e4a223f | 2012-10-03 09:38:49 +0000 | [diff] [blame] | 81 | const char *lockname = "/var/lock/" CMD_PRINTENV ".lock"; |
| 82 | int lockfd = -1; |
| 83 | int retval = EXIT_SUCCESS; |
| 84 | |
Mike Frysinger | 7a546db | 2012-11-10 19:47:46 +0000 | [diff] [blame] | 85 | lockfd = open(lockname, O_WRONLY | O_CREAT | O_TRUNC, 0666); |
Joe Hershberger | e4a223f | 2012-10-03 09:38:49 +0000 | [diff] [blame] | 86 | if (-1 == lockfd) { |
| 87 | fprintf(stderr, "Error opening lock file %s\n", lockname); |
| 88 | return EXIT_FAILURE; |
| 89 | } |
| 90 | |
| 91 | if (-1 == flock(lockfd, LOCK_EX)) { |
| 92 | fprintf(stderr, "Error locking file %s\n", lockname); |
| 93 | close(lockfd); |
| 94 | return EXIT_FAILURE; |
| 95 | } |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 96 | |
| 97 | if ((p = strrchr (cmdname, '/')) != NULL) { |
| 98 | cmdname = p + 1; |
| 99 | } |
| 100 | |
Marek Vasut | a8a752c | 2014-03-05 19:59:52 +0100 | [diff] [blame] | 101 | while ((c = getopt_long (argc, argv, "a:ns:h", |
Stefano Babic | bd7b26f | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 102 | long_options, NULL)) != EOF) { |
| 103 | switch (c) { |
Marek Vasut | a8a752c | 2014-03-05 19:59:52 +0100 | [diff] [blame] | 104 | case 'a': |
| 105 | /* AES key, handled later */ |
| 106 | break; |
Daniel Hobi | 122bc08 | 2010-09-15 19:46:26 +0200 | [diff] [blame] | 107 | case 'n': |
| 108 | /* handled in fw_printenv */ |
| 109 | break; |
Stefano Babic | bd7b26f | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 110 | case 's': |
| 111 | script_file = optarg; |
| 112 | break; |
| 113 | case 'h': |
| 114 | usage(); |
Joe Hershberger | e4a223f | 2012-10-03 09:38:49 +0000 | [diff] [blame] | 115 | goto exit; |
Daniel Hobi | 29ccd7c | 2010-09-16 14:36:09 +0200 | [diff] [blame] | 116 | default: /* '?' */ |
| 117 | fprintf(stderr, "Try `%s --help' for more information." |
| 118 | "\n", cmdname); |
Joe Hershberger | e4a223f | 2012-10-03 09:38:49 +0000 | [diff] [blame] | 119 | retval = EXIT_FAILURE; |
| 120 | goto exit; |
Stefano Babic | bd7b26f | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 124 | if (strcmp(cmdname, CMD_PRINTENV) == 0) { |
Joe Hershberger | e4a223f | 2012-10-03 09:38:49 +0000 | [diff] [blame] | 125 | if (fw_printenv(argc, argv) != 0) |
| 126 | retval = EXIT_FAILURE; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 127 | } else if (strcmp(cmdname, CMD_SETENV) == 0) { |
Stefano Babic | bd7b26f | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 128 | if (!script_file) { |
| 129 | if (fw_setenv(argc, argv) != 0) |
Joe Hershberger | e4a223f | 2012-10-03 09:38:49 +0000 | [diff] [blame] | 130 | retval = EXIT_FAILURE; |
Stefano Babic | bd7b26f | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 131 | } else { |
| 132 | if (fw_parse_script(script_file) != 0) |
Joe Hershberger | e4a223f | 2012-10-03 09:38:49 +0000 | [diff] [blame] | 133 | retval = EXIT_FAILURE; |
Stefano Babic | bd7b26f | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 134 | } |
Joe Hershberger | e4a223f | 2012-10-03 09:38:49 +0000 | [diff] [blame] | 135 | } else { |
| 136 | fprintf(stderr, |
| 137 | "Identity crisis - may be called as `" CMD_PRINTENV |
| 138 | "' or as `" CMD_SETENV "' but not as `%s'\n", |
| 139 | cmdname); |
| 140 | retval = EXIT_FAILURE; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Joe Hershberger | e4a223f | 2012-10-03 09:38:49 +0000 | [diff] [blame] | 143 | exit: |
| 144 | flock(lockfd, LOCK_UN); |
| 145 | close(lockfd); |
| 146 | return retval; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 147 | } |