16aff3115Swdenk /* 2bc11756dSGrant Erickson * (C) Copyright 2000-2008 36aff3115Swdenk * Wolfgang Denk, DENX Software Engineering, wd@denx.de. 46aff3115Swdenk * 56aff3115Swdenk * See file CREDITS for list of people who contributed to this 66aff3115Swdenk * project. 76aff3115Swdenk * 86aff3115Swdenk * This program is free software; you can redistribute it and/or 96aff3115Swdenk * modify it under the terms of the GNU General Public License as 106aff3115Swdenk * published by the Free Software Foundation; either version 2 of 116aff3115Swdenk * the License, or (at your option) any later version. 126aff3115Swdenk * 136aff3115Swdenk * This program is distributed in the hope that it will be useful, 146aff3115Swdenk * but WITHOUT ANY WARRANTY; without even the implied warranty of 156aff3115Swdenk * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 166aff3115Swdenk * GNU General Public License for more details. 176aff3115Swdenk * 186aff3115Swdenk * You should have received a copy of the GNU General Public License 196aff3115Swdenk * along with this program; if not, write to the Free Software 206aff3115Swdenk * Foundation, Inc., 59 Temple Place, Suite 330, Boston, 216aff3115Swdenk * MA 02111-1307 USA 226aff3115Swdenk */ 236aff3115Swdenk 246aff3115Swdenk /* 253bac3513Swdenk * Command line user interface to firmware (=U-Boot) environment. 266aff3115Swdenk * 276aff3115Swdenk * Implements: 28bc11756dSGrant Erickson * fw_printenv [[ -n name ] | [ name ... ]] 29bc11756dSGrant Erickson * - prints the value of a single environment variable 30bc11756dSGrant Erickson * "name", the ``name=value'' pairs of one or more 31bc11756dSGrant Erickson * environment variables "name", or the whole 32bc11756dSGrant Erickson * environment if no names are specified. 336aff3115Swdenk * fw_setenv name [ value ... ] 346aff3115Swdenk * - If a name without any values is given, the variable 356aff3115Swdenk * with this name is deleted from the environment; 366aff3115Swdenk * otherwise, all "value" arguments are concatenated, 37bc11756dSGrant Erickson * separated by single blank characters, and the 386aff3115Swdenk * resulting string is assigned to the environment 396aff3115Swdenk * variable "name" 406aff3115Swdenk */ 416aff3115Swdenk 426aff3115Swdenk #include <stdio.h> 436aff3115Swdenk #include <string.h> 446aff3115Swdenk #include <stdlib.h> 45*bd7b26f8SStefano Babic #include <getopt.h> 466aff3115Swdenk #include "fw_env.h" 476aff3115Swdenk 486aff3115Swdenk #define CMD_PRINTENV "fw_printenv" 496aff3115Swdenk #define CMD_SETENV "fw_setenv" 506aff3115Swdenk 51*bd7b26f8SStefano Babic static struct option long_options[] = { 52*bd7b26f8SStefano Babic {"script", required_argument, NULL, 's'}, 53*bd7b26f8SStefano Babic {"help", no_argument, NULL, 'h'}, 54*bd7b26f8SStefano Babic {NULL, 0, NULL, 0} 55*bd7b26f8SStefano Babic }; 56*bd7b26f8SStefano Babic 57*bd7b26f8SStefano Babic void usage(void) 58*bd7b26f8SStefano Babic { 59*bd7b26f8SStefano Babic 60*bd7b26f8SStefano Babic fprintf(stderr, "fw_printenv/fw_setenv, " 61*bd7b26f8SStefano Babic "a command line interface to U-Boot environment\n\n" 62*bd7b26f8SStefano Babic "usage:\tfw_printenv\n" 63*bd7b26f8SStefano Babic "\tfw_setenv [variable name] [variable value]\n" 64*bd7b26f8SStefano Babic "\tfw_setenv -s [ file ]\n" 65*bd7b26f8SStefano Babic "\tfw_setenv -s - < [ file ]\n\n" 66*bd7b26f8SStefano Babic "The file passed as argument contains only pairs " 67*bd7b26f8SStefano Babic "name / value\n" 68*bd7b26f8SStefano Babic "Example:\n" 69*bd7b26f8SStefano Babic "# Any line starting with # is treated as comment\n" 70*bd7b26f8SStefano Babic "\n" 71*bd7b26f8SStefano Babic "\t netdev eth0\n" 72*bd7b26f8SStefano Babic "\t kernel_addr 400000\n" 73*bd7b26f8SStefano Babic "\t var1\n" 74*bd7b26f8SStefano Babic "\t var2 The quick brown fox jumps over the " 75*bd7b26f8SStefano Babic "lazy dog\n" 76*bd7b26f8SStefano Babic "\n" 77*bd7b26f8SStefano Babic "A variable without value will be dropped. It is possible\n" 78*bd7b26f8SStefano Babic "to put any number of spaces between the fields, but any\n" 79*bd7b26f8SStefano Babic "space inside the value is treated as part of the value " 80*bd7b26f8SStefano Babic "itself.\n\n" 81*bd7b26f8SStefano Babic ); 82*bd7b26f8SStefano Babic } 83*bd7b26f8SStefano Babic 846aff3115Swdenk int 856aff3115Swdenk main(int argc, char *argv[]) 866aff3115Swdenk { 876aff3115Swdenk char *p; 886aff3115Swdenk char *cmdname = *argv; 89*bd7b26f8SStefano Babic char *script_file = NULL; 90*bd7b26f8SStefano Babic int c; 916aff3115Swdenk 926aff3115Swdenk if ((p = strrchr (cmdname, '/')) != NULL) { 936aff3115Swdenk cmdname = p + 1; 946aff3115Swdenk } 956aff3115Swdenk 96*bd7b26f8SStefano Babic while ((c = getopt_long (argc, argv, "s:h", 97*bd7b26f8SStefano Babic long_options, NULL)) != EOF) { 98*bd7b26f8SStefano Babic switch (c) { 99*bd7b26f8SStefano Babic case 's': 100*bd7b26f8SStefano Babic script_file = optarg; 101*bd7b26f8SStefano Babic break; 102*bd7b26f8SStefano Babic case 'h': 103*bd7b26f8SStefano Babic usage(); 104*bd7b26f8SStefano Babic return EXIT_SUCCESS; 105*bd7b26f8SStefano Babic } 106*bd7b26f8SStefano Babic } 107*bd7b26f8SStefano Babic 108*bd7b26f8SStefano Babic 1096aff3115Swdenk if (strcmp(cmdname, CMD_PRINTENV) == 0) { 1106aff3115Swdenk 111bc11756dSGrant Erickson if (fw_printenv (argc, argv) != 0) 112*bd7b26f8SStefano Babic return EXIT_FAILURE; 1136aff3115Swdenk 114*bd7b26f8SStefano Babic return EXIT_SUCCESS; 1156aff3115Swdenk 1166aff3115Swdenk } else if (strcmp(cmdname, CMD_SETENV) == 0) { 117*bd7b26f8SStefano Babic if (!script_file) { 1186aff3115Swdenk if (fw_setenv(argc, argv) != 0) 119*bd7b26f8SStefano Babic return EXIT_FAILURE; 120*bd7b26f8SStefano Babic } else { 121*bd7b26f8SStefano Babic if (fw_parse_script(script_file) != 0) 122*bd7b26f8SStefano Babic return EXIT_FAILURE; 123*bd7b26f8SStefano Babic } 1246aff3115Swdenk 125*bd7b26f8SStefano Babic return EXIT_SUCCESS; 126bc11756dSGrant Erickson 1276aff3115Swdenk } 1286aff3115Swdenk 1296aff3115Swdenk fprintf (stderr, 1306aff3115Swdenk "Identity crisis - may be called as `" CMD_PRINTENV 1316aff3115Swdenk "' or as `" CMD_SETENV "' but not as `%s'\n", 1326aff3115Swdenk cmdname); 133*bd7b26f8SStefano Babic return EXIT_FAILURE; 1346aff3115Swdenk } 135