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: |
Grant Erickson | bc11756 | 2008-05-06 20:16:15 -0700 | [diff] [blame] | 12 | * 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. |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 17 | * 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 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" |
| 24 | */ |
| 25 | |
Joe Hershberger | e4a223f | 2012-10-03 09:38:49 +0000 | [diff] [blame] | 26 | #include <fcntl.h> |
| 27 | #include <getopt.h> |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 28 | #include <stdio.h> |
| 29 | #include <string.h> |
| 30 | #include <stdlib.h> |
Joe Hershberger | e4a223f | 2012-10-03 09:38:49 +0000 | [diff] [blame] | 31 | #include <sys/file.h> |
| 32 | #include <unistd.h> |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 33 | #include "fw_env.h" |
| 34 | |
| 35 | #define CMD_PRINTENV "fw_printenv" |
| 36 | #define CMD_SETENV "fw_setenv" |
| 37 | |
Stefano Babic | bd7b26f | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 38 | static struct option long_options[] = { |
| 39 | {"script", required_argument, NULL, 's'}, |
| 40 | {"help", no_argument, NULL, 'h'}, |
| 41 | {NULL, 0, NULL, 0} |
| 42 | }; |
| 43 | |
| 44 | void usage(void) |
| 45 | { |
| 46 | |
| 47 | fprintf(stderr, "fw_printenv/fw_setenv, " |
| 48 | "a command line interface to U-Boot environment\n\n" |
Daniel Hobi | 122bc08 | 2010-09-15 19:46:26 +0200 | [diff] [blame] | 49 | "usage:\tfw_printenv [-n] [variable name]\n" |
Stefano Babic | bd7b26f | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 50 | "\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 Hershberger | e4a223f | 2012-10-03 09:38:49 +0000 | [diff] [blame] | 71 | int main(int argc, char *argv[]) |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 72 | { |
| 73 | char *p; |
| 74 | char *cmdname = *argv; |
Stefano Babic | bd7b26f | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 75 | char *script_file = NULL; |
| 76 | int c; |
Joe Hershberger | e4a223f | 2012-10-03 09:38:49 +0000 | [diff] [blame] | 77 | const char *lockname = "/var/lock/" CMD_PRINTENV ".lock"; |
| 78 | int lockfd = -1; |
| 79 | int retval = EXIT_SUCCESS; |
| 80 | |
Mike Frysinger | 7a546db | 2012-11-10 19:47:46 +0000 | [diff] [blame] | 81 | lockfd = open(lockname, O_WRONLY | O_CREAT | O_TRUNC, 0666); |
Joe Hershberger | e4a223f | 2012-10-03 09:38:49 +0000 | [diff] [blame] | 82 | 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 | } |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 92 | |
| 93 | if ((p = strrchr (cmdname, '/')) != NULL) { |
| 94 | cmdname = p + 1; |
| 95 | } |
| 96 | |
Daniel Hobi | 122bc08 | 2010-09-15 19:46:26 +0200 | [diff] [blame] | 97 | while ((c = getopt_long (argc, argv, "ns:h", |
Stefano Babic | bd7b26f | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 98 | long_options, NULL)) != EOF) { |
| 99 | switch (c) { |
Daniel Hobi | 122bc08 | 2010-09-15 19:46:26 +0200 | [diff] [blame] | 100 | case 'n': |
| 101 | /* handled in fw_printenv */ |
| 102 | break; |
Stefano Babic | bd7b26f | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 103 | case 's': |
| 104 | script_file = optarg; |
| 105 | break; |
| 106 | case 'h': |
| 107 | usage(); |
Joe Hershberger | e4a223f | 2012-10-03 09:38:49 +0000 | [diff] [blame] | 108 | goto exit; |
Daniel Hobi | 29ccd7c | 2010-09-16 14:36:09 +0200 | [diff] [blame] | 109 | default: /* '?' */ |
| 110 | fprintf(stderr, "Try `%s --help' for more information." |
| 111 | "\n", cmdname); |
Joe Hershberger | e4a223f | 2012-10-03 09:38:49 +0000 | [diff] [blame] | 112 | retval = EXIT_FAILURE; |
| 113 | goto exit; |
Stefano Babic | bd7b26f | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 117 | if (strcmp(cmdname, CMD_PRINTENV) == 0) { |
Joe Hershberger | e4a223f | 2012-10-03 09:38:49 +0000 | [diff] [blame] | 118 | if (fw_printenv(argc, argv) != 0) |
| 119 | retval = EXIT_FAILURE; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 120 | } else if (strcmp(cmdname, CMD_SETENV) == 0) { |
Stefano Babic | bd7b26f | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 121 | if (!script_file) { |
| 122 | if (fw_setenv(argc, argv) != 0) |
Joe Hershberger | e4a223f | 2012-10-03 09:38:49 +0000 | [diff] [blame] | 123 | retval = EXIT_FAILURE; |
Stefano Babic | bd7b26f | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 124 | } else { |
| 125 | if (fw_parse_script(script_file) != 0) |
Joe Hershberger | e4a223f | 2012-10-03 09:38:49 +0000 | [diff] [blame] | 126 | retval = EXIT_FAILURE; |
Stefano Babic | bd7b26f | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 127 | } |
Joe Hershberger | e4a223f | 2012-10-03 09:38:49 +0000 | [diff] [blame] | 128 | } 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; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Joe Hershberger | e4a223f | 2012-10-03 09:38:49 +0000 | [diff] [blame] | 136 | exit: |
| 137 | flock(lockfd, LOCK_UN); |
| 138 | close(lockfd); |
| 139 | return retval; |
wdenk | 6aff311 | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 140 | } |