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> 45bd7b26f8SStefano Babic #include <getopt.h> 466aff3115Swdenk #include "fw_env.h" 476aff3115Swdenk 486aff3115Swdenk #define CMD_PRINTENV "fw_printenv" 496aff3115Swdenk #define CMD_SETENV "fw_setenv" 506aff3115Swdenk 51bd7b26f8SStefano Babic static struct option long_options[] = { 52bd7b26f8SStefano Babic {"script", required_argument, NULL, 's'}, 53bd7b26f8SStefano Babic {"help", no_argument, NULL, 'h'}, 54bd7b26f8SStefano Babic {NULL, 0, NULL, 0} 55bd7b26f8SStefano Babic }; 56bd7b26f8SStefano Babic 57bd7b26f8SStefano Babic void usage(void) 58bd7b26f8SStefano Babic { 59bd7b26f8SStefano Babic 60bd7b26f8SStefano Babic fprintf(stderr, "fw_printenv/fw_setenv, " 61bd7b26f8SStefano Babic "a command line interface to U-Boot environment\n\n" 62122bc088SDaniel Hobi "usage:\tfw_printenv [-n] [variable name]\n" 63bd7b26f8SStefano Babic "\tfw_setenv [variable name] [variable value]\n" 64bd7b26f8SStefano Babic "\tfw_setenv -s [ file ]\n" 65bd7b26f8SStefano Babic "\tfw_setenv -s - < [ file ]\n\n" 66bd7b26f8SStefano Babic "The file passed as argument contains only pairs " 67bd7b26f8SStefano Babic "name / value\n" 68bd7b26f8SStefano Babic "Example:\n" 69bd7b26f8SStefano Babic "# Any line starting with # is treated as comment\n" 70bd7b26f8SStefano Babic "\n" 71bd7b26f8SStefano Babic "\t netdev eth0\n" 72bd7b26f8SStefano Babic "\t kernel_addr 400000\n" 73bd7b26f8SStefano Babic "\t var1\n" 74bd7b26f8SStefano Babic "\t var2 The quick brown fox jumps over the " 75bd7b26f8SStefano Babic "lazy dog\n" 76bd7b26f8SStefano Babic "\n" 77bd7b26f8SStefano Babic "A variable without value will be dropped. It is possible\n" 78bd7b26f8SStefano Babic "to put any number of spaces between the fields, but any\n" 79bd7b26f8SStefano Babic "space inside the value is treated as part of the value " 80bd7b26f8SStefano Babic "itself.\n\n" 81bd7b26f8SStefano Babic ); 82bd7b26f8SStefano Babic } 83bd7b26f8SStefano Babic 846aff3115Swdenk int 856aff3115Swdenk main(int argc, char *argv[]) 866aff3115Swdenk { 876aff3115Swdenk char *p; 886aff3115Swdenk char *cmdname = *argv; 89bd7b26f8SStefano Babic char *script_file = NULL; 90bd7b26f8SStefano Babic int c; 916aff3115Swdenk 926aff3115Swdenk if ((p = strrchr (cmdname, '/')) != NULL) { 936aff3115Swdenk cmdname = p + 1; 946aff3115Swdenk } 956aff3115Swdenk 96122bc088SDaniel Hobi while ((c = getopt_long (argc, argv, "ns:h", 97bd7b26f8SStefano Babic long_options, NULL)) != EOF) { 98bd7b26f8SStefano Babic switch (c) { 99122bc088SDaniel Hobi case 'n': 100122bc088SDaniel Hobi /* handled in fw_printenv */ 101122bc088SDaniel Hobi break; 102bd7b26f8SStefano Babic case 's': 103bd7b26f8SStefano Babic script_file = optarg; 104bd7b26f8SStefano Babic break; 105bd7b26f8SStefano Babic case 'h': 106bd7b26f8SStefano Babic usage(); 107bd7b26f8SStefano Babic return EXIT_SUCCESS; 108*29ccd7c3SDaniel Hobi default: /* '?' */ 109*29ccd7c3SDaniel Hobi fprintf(stderr, "Try `%s --help' for more information." 110*29ccd7c3SDaniel Hobi "\n", cmdname); 111*29ccd7c3SDaniel Hobi return EXIT_FAILURE; 112bd7b26f8SStefano Babic } 113bd7b26f8SStefano Babic } 114bd7b26f8SStefano Babic 115bd7b26f8SStefano Babic 1166aff3115Swdenk if (strcmp(cmdname, CMD_PRINTENV) == 0) { 1176aff3115Swdenk 118bc11756dSGrant Erickson if (fw_printenv (argc, argv) != 0) 119bd7b26f8SStefano Babic return EXIT_FAILURE; 1206aff3115Swdenk 121bd7b26f8SStefano Babic return EXIT_SUCCESS; 1226aff3115Swdenk 1236aff3115Swdenk } else if (strcmp(cmdname, CMD_SETENV) == 0) { 124bd7b26f8SStefano Babic if (!script_file) { 1256aff3115Swdenk if (fw_setenv(argc, argv) != 0) 126bd7b26f8SStefano Babic return EXIT_FAILURE; 127bd7b26f8SStefano Babic } else { 128bd7b26f8SStefano Babic if (fw_parse_script(script_file) != 0) 129bd7b26f8SStefano Babic return EXIT_FAILURE; 130bd7b26f8SStefano Babic } 1316aff3115Swdenk 132bd7b26f8SStefano Babic return EXIT_SUCCESS; 133bc11756dSGrant Erickson 1346aff3115Swdenk } 1356aff3115Swdenk 1366aff3115Swdenk fprintf (stderr, 1376aff3115Swdenk "Identity crisis - may be called as `" CMD_PRINTENV 1386aff3115Swdenk "' or as `" CMD_SETENV "' but not as `%s'\n", 1396aff3115Swdenk cmdname); 140bd7b26f8SStefano Babic return EXIT_FAILURE; 1416aff3115Swdenk } 142