wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1 | /* |
Grant Erickson | 73a4e5e | 2008-05-06 20:16:15 -0700 | [diff] [blame] | 2 | * (C) Copyright 2000-2008 |
wdenk | 34b613e | 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 | ef5fe75 | 2003-03-12 10:41:04 +0000 | [diff] [blame] | 25 | * Command line user interface to firmware (=U-Boot) environment. |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 26 | * |
| 27 | * Implements: |
Grant Erickson | 73a4e5e | 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 | 34b613e | 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 | 73a4e5e | 2008-05-06 20:16:15 -0700 | [diff] [blame] | 37 | * separated by single blank characters, and the |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 38 | * resulting string is assigned to the environment |
| 39 | * variable "name" |
| 40 | */ |
| 41 | |
| 42 | #include <stdio.h> |
| 43 | #include <string.h> |
| 44 | #include <stdlib.h> |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 45 | #include <getopt.h> |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 46 | #include "fw_env.h" |
| 47 | |
| 48 | #define CMD_PRINTENV "fw_printenv" |
| 49 | #define CMD_SETENV "fw_setenv" |
| 50 | |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 51 | static struct option long_options[] = { |
| 52 | {"script", required_argument, NULL, 's'}, |
| 53 | {"help", no_argument, NULL, 'h'}, |
| 54 | {NULL, 0, NULL, 0} |
| 55 | }; |
| 56 | |
| 57 | void usage(void) |
| 58 | { |
| 59 | |
| 60 | fprintf(stderr, "fw_printenv/fw_setenv, " |
| 61 | "a command line interface to U-Boot environment\n\n" |
Daniel Hobi | 6c55426 | 2010-09-15 19:46:26 +0200 | [diff] [blame] | 62 | "usage:\tfw_printenv [-n] [variable name]\n" |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 63 | "\tfw_setenv [variable name] [variable value]\n" |
| 64 | "\tfw_setenv -s [ file ]\n" |
| 65 | "\tfw_setenv -s - < [ file ]\n\n" |
| 66 | "The file passed as argument contains only pairs " |
| 67 | "name / value\n" |
| 68 | "Example:\n" |
| 69 | "# Any line starting with # is treated as comment\n" |
| 70 | "\n" |
| 71 | "\t netdev eth0\n" |
| 72 | "\t kernel_addr 400000\n" |
| 73 | "\t var1\n" |
| 74 | "\t var2 The quick brown fox jumps over the " |
| 75 | "lazy dog\n" |
| 76 | "\n" |
| 77 | "A variable without value will be dropped. It is possible\n" |
| 78 | "to put any number of spaces between the fields, but any\n" |
| 79 | "space inside the value is treated as part of the value " |
| 80 | "itself.\n\n" |
| 81 | ); |
| 82 | } |
| 83 | |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 84 | int |
| 85 | main(int argc, char *argv[]) |
| 86 | { |
| 87 | char *p; |
| 88 | char *cmdname = *argv; |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 89 | char *script_file = NULL; |
| 90 | int c; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 91 | |
| 92 | if ((p = strrchr (cmdname, '/')) != NULL) { |
| 93 | cmdname = p + 1; |
| 94 | } |
| 95 | |
Daniel Hobi | 6c55426 | 2010-09-15 19:46:26 +0200 | [diff] [blame] | 96 | while ((c = getopt_long (argc, argv, "ns:h", |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 97 | long_options, NULL)) != EOF) { |
| 98 | switch (c) { |
Daniel Hobi | 6c55426 | 2010-09-15 19:46:26 +0200 | [diff] [blame] | 99 | case 'n': |
| 100 | /* handled in fw_printenv */ |
| 101 | break; |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 102 | case 's': |
| 103 | script_file = optarg; |
| 104 | break; |
| 105 | case 'h': |
| 106 | usage(); |
| 107 | return EXIT_SUCCESS; |
Daniel Hobi | 6a20a32 | 2010-09-16 14:36:09 +0200 | [diff] [blame] | 108 | default: /* '?' */ |
| 109 | fprintf(stderr, "Try `%s --help' for more information." |
| 110 | "\n", cmdname); |
| 111 | return EXIT_FAILURE; |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | |
| 115 | |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 116 | if (strcmp(cmdname, CMD_PRINTENV) == 0) { |
| 117 | |
Grant Erickson | 73a4e5e | 2008-05-06 20:16:15 -0700 | [diff] [blame] | 118 | if (fw_printenv (argc, argv) != 0) |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 119 | return EXIT_FAILURE; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 120 | |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 121 | return EXIT_SUCCESS; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 122 | |
| 123 | } else if (strcmp(cmdname, CMD_SETENV) == 0) { |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 124 | if (!script_file) { |
| 125 | if (fw_setenv(argc, argv) != 0) |
| 126 | return EXIT_FAILURE; |
| 127 | } else { |
| 128 | if (fw_parse_script(script_file) != 0) |
| 129 | return EXIT_FAILURE; |
| 130 | } |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 131 | |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 132 | return EXIT_SUCCESS; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 133 | |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | fprintf (stderr, |
| 137 | "Identity crisis - may be called as `" CMD_PRINTENV |
| 138 | "' or as `" CMD_SETENV "' but not as `%s'\n", |
| 139 | cmdname); |
Stefano Babic | ada628b | 2010-05-24 12:08:16 +0200 | [diff] [blame] | 140 | return EXIT_FAILURE; |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 141 | } |