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 | * |
| 5 | * See file CREDITS for list of people who contributed to this |
| 6 | * project. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License as |
| 10 | * published by the Free Software Foundation; either version 2 of |
| 11 | * the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 21 | * MA 02111-1307 USA |
| 22 | */ |
| 23 | |
| 24 | /* |
wdenk | 3bac351 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 25 | * Command line user interface to firmware (=U-Boot) environment. |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 26 | * |
| 27 | * Implements: |
Grant Erickson | bc11756 | 2008-05-06 20:16:15 -0700 | [diff] [blame] | 28 | * fw_printenv [[ -n name ] | [ name ... ]] |
| 29 | * - prints the value of a single environment variable |
| 30 | * "name", the ``name=value'' pairs of one or more |
| 31 | * environment variables "name", or the whole |
| 32 | * environment if no names are specified. |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 33 | * fw_setenv name [ value ... ] |
| 34 | * - If a name without any values is given, the variable |
| 35 | * with this name is deleted from the environment; |
| 36 | * otherwise, all "value" arguments are concatenated, |
Grant Erickson | bc11756 | 2008-05-06 20:16:15 -0700 | [diff] [blame] | 37 | * separated by single blank characters, and the |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 38 | * resulting string is assigned to the environment |
| 39 | * variable "name" |
| 40 | */ |
| 41 | |
Joe Hershberger | e4a223f | 2012-10-03 09:38:49 +0000 | [diff] [blame] | 42 | #include <fcntl.h> |
| 43 | #include <getopt.h> |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 44 | #include <stdio.h> |
| 45 | #include <string.h> |
| 46 | #include <stdlib.h> |
Joe Hershberger | e4a223f | 2012-10-03 09:38:49 +0000 | [diff] [blame] | 47 | #include <sys/file.h> |
| 48 | #include <unistd.h> |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 49 | #include "fw_env.h" |
| 50 | |
| 51 | #define CMD_PRINTENV "fw_printenv" |
| 52 | #define CMD_SETENV "fw_setenv" |
| 53 | |
Stefano Babic | bd7b26f | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 54 | static struct option long_options[] = { |
| 55 | {"script", required_argument, NULL, 's'}, |
| 56 | {"help", no_argument, NULL, 'h'}, |
| 57 | {NULL, 0, NULL, 0} |
| 58 | }; |
| 59 | |
| 60 | void usage(void) |
| 61 | { |
| 62 | |
| 63 | fprintf(stderr, "fw_printenv/fw_setenv, " |
| 64 | "a command line interface to U-Boot environment\n\n" |
Daniel Hobi | 122bc08 | 2010-09-15 19:46:26 +0200 | [diff] [blame] | 65 | "usage:\tfw_printenv [-n] [variable name]\n" |
Stefano Babic | bd7b26f | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 66 | "\tfw_setenv [variable name] [variable value]\n" |
| 67 | "\tfw_setenv -s [ file ]\n" |
| 68 | "\tfw_setenv -s - < [ file ]\n\n" |
| 69 | "The file passed as argument contains only pairs " |
| 70 | "name / value\n" |
| 71 | "Example:\n" |
| 72 | "# Any line starting with # is treated as comment\n" |
| 73 | "\n" |
| 74 | "\t netdev eth0\n" |
| 75 | "\t kernel_addr 400000\n" |
| 76 | "\t var1\n" |
| 77 | "\t var2 The quick brown fox jumps over the " |
| 78 | "lazy dog\n" |
| 79 | "\n" |
| 80 | "A variable without value will be dropped. It is possible\n" |
| 81 | "to put any number of spaces between the fields, but any\n" |
| 82 | "space inside the value is treated as part of the value " |
| 83 | "itself.\n\n" |
| 84 | ); |
| 85 | } |
| 86 | |
Joe Hershberger | e4a223f | 2012-10-03 09:38:49 +0000 | [diff] [blame] | 87 | int main(int argc, char *argv[]) |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 88 | { |
| 89 | char *p; |
| 90 | char *cmdname = *argv; |
Stefano Babic | bd7b26f | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 91 | char *script_file = NULL; |
| 92 | int c; |
Joe Hershberger | e4a223f | 2012-10-03 09:38:49 +0000 | [diff] [blame] | 93 | const char *lockname = "/var/lock/" CMD_PRINTENV ".lock"; |
| 94 | int lockfd = -1; |
| 95 | int retval = EXIT_SUCCESS; |
| 96 | |
Mike Frysinger | 7a546db | 2012-11-10 19:47:46 +0000 | [diff] [blame] | 97 | lockfd = open(lockname, O_WRONLY | O_CREAT | O_TRUNC, 0666); |
Joe Hershberger | e4a223f | 2012-10-03 09:38:49 +0000 | [diff] [blame] | 98 | if (-1 == lockfd) { |
| 99 | fprintf(stderr, "Error opening lock file %s\n", lockname); |
| 100 | return EXIT_FAILURE; |
| 101 | } |
| 102 | |
| 103 | if (-1 == flock(lockfd, LOCK_EX)) { |
| 104 | fprintf(stderr, "Error locking file %s\n", lockname); |
| 105 | close(lockfd); |
| 106 | return EXIT_FAILURE; |
| 107 | } |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 108 | |
| 109 | if ((p = strrchr (cmdname, '/')) != NULL) { |
| 110 | cmdname = p + 1; |
| 111 | } |
| 112 | |
Daniel Hobi | 122bc08 | 2010-09-15 19:46:26 +0200 | [diff] [blame] | 113 | while ((c = getopt_long (argc, argv, "ns:h", |
Stefano Babic | bd7b26f | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 114 | long_options, NULL)) != EOF) { |
| 115 | switch (c) { |
Daniel Hobi | 122bc08 | 2010-09-15 19:46:26 +0200 | [diff] [blame] | 116 | case 'n': |
| 117 | /* handled in fw_printenv */ |
| 118 | break; |
Stefano Babic | bd7b26f | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 119 | case 's': |
| 120 | script_file = optarg; |
| 121 | break; |
| 122 | case 'h': |
| 123 | usage(); |
Joe Hershberger | e4a223f | 2012-10-03 09:38:49 +0000 | [diff] [blame] | 124 | goto exit; |
Daniel Hobi | 29ccd7c | 2010-09-16 14:36:09 +0200 | [diff] [blame] | 125 | default: /* '?' */ |
| 126 | fprintf(stderr, "Try `%s --help' for more information." |
| 127 | "\n", cmdname); |
Joe Hershberger | e4a223f | 2012-10-03 09:38:49 +0000 | [diff] [blame] | 128 | retval = EXIT_FAILURE; |
| 129 | goto exit; |
Stefano Babic | bd7b26f | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 133 | if (strcmp(cmdname, CMD_PRINTENV) == 0) { |
Joe Hershberger | e4a223f | 2012-10-03 09:38:49 +0000 | [diff] [blame] | 134 | if (fw_printenv(argc, argv) != 0) |
| 135 | retval = EXIT_FAILURE; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 136 | } else if (strcmp(cmdname, CMD_SETENV) == 0) { |
Stefano Babic | bd7b26f | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 137 | if (!script_file) { |
| 138 | if (fw_setenv(argc, argv) != 0) |
Joe Hershberger | e4a223f | 2012-10-03 09:38:49 +0000 | [diff] [blame] | 139 | retval = EXIT_FAILURE; |
Stefano Babic | bd7b26f | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 140 | } else { |
| 141 | if (fw_parse_script(script_file) != 0) |
Joe Hershberger | e4a223f | 2012-10-03 09:38:49 +0000 | [diff] [blame] | 142 | retval = EXIT_FAILURE; |
Stefano Babic | bd7b26f | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 143 | } |
Joe Hershberger | e4a223f | 2012-10-03 09:38:49 +0000 | [diff] [blame] | 144 | } else { |
| 145 | fprintf(stderr, |
| 146 | "Identity crisis - may be called as `" CMD_PRINTENV |
| 147 | "' or as `" CMD_SETENV "' but not as `%s'\n", |
| 148 | cmdname); |
| 149 | retval = EXIT_FAILURE; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Joe Hershberger | e4a223f | 2012-10-03 09:38:49 +0000 | [diff] [blame] | 152 | exit: |
| 153 | flock(lockfd, LOCK_UN); |
| 154 | close(lockfd); |
| 155 | return retval; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 156 | } |