fw_env_main.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * (C) Copyright 2000-2008
  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. * Command line user interface to firmware (=U-Boot) environment.
  25. *
  26. * Implements:
  27. * fw_printenv [[ -n name ] | [ name ... ]]
  28. * - prints the value of a single environment variable
  29. * "name", the ``name=value'' pairs of one or more
  30. * environment variables "name", or the whole
  31. * environment if no names are specified.
  32. * fw_setenv name [ value ... ]
  33. * - If a name without any values is given, the variable
  34. * with this name is deleted from the environment;
  35. * otherwise, all "value" arguments are concatenated,
  36. * separated by single blank characters, and the
  37. * resulting string is assigned to the environment
  38. * variable "name"
  39. */
  40. #include <stdio.h>
  41. #include <string.h>
  42. #include <stdlib.h>
  43. #include <getopt.h>
  44. #include "fw_env.h"
  45. #define CMD_PRINTENV "fw_printenv"
  46. #define CMD_SETENV "fw_setenv"
  47. static struct option long_options[] = {
  48. {"script", required_argument, NULL, 's'},
  49. {"help", no_argument, NULL, 'h'},
  50. {NULL, 0, NULL, 0}
  51. };
  52. void usage(void)
  53. {
  54. fprintf(stderr, "fw_printenv/fw_setenv, "
  55. "a command line interface to U-Boot environment\n\n"
  56. "usage:\tfw_printenv [-n] [variable name]\n"
  57. "\tfw_setenv [variable name] [variable value]\n"
  58. "\tfw_setenv -s [ file ]\n"
  59. "\tfw_setenv -s - < [ file ]\n\n"
  60. "The file passed as argument contains only pairs "
  61. "name / value\n"
  62. "Example:\n"
  63. "# Any line starting with # is treated as comment\n"
  64. "\n"
  65. "\t netdev eth0\n"
  66. "\t kernel_addr 400000\n"
  67. "\t var1\n"
  68. "\t var2 The quick brown fox jumps over the "
  69. "lazy dog\n"
  70. "\n"
  71. "A variable without value will be dropped. It is possible\n"
  72. "to put any number of spaces between the fields, but any\n"
  73. "space inside the value is treated as part of the value "
  74. "itself.\n\n"
  75. );
  76. }
  77. int
  78. main(int argc, char *argv[])
  79. {
  80. char *p;
  81. char *cmdname = *argv;
  82. char *script_file = NULL;
  83. int c;
  84. if ((p = strrchr (cmdname, '/')) != NULL) {
  85. cmdname = p + 1;
  86. }
  87. while ((c = getopt_long (argc, argv, "ns:h",
  88. long_options, NULL)) != EOF) {
  89. switch (c) {
  90. case 'n':
  91. /* handled in fw_printenv */
  92. break;
  93. case 's':
  94. script_file = optarg;
  95. break;
  96. case 'h':
  97. usage();
  98. return EXIT_SUCCESS;
  99. default: /* '?' */
  100. fprintf(stderr, "Try `%s --help' for more information."
  101. "\n", cmdname);
  102. return EXIT_FAILURE;
  103. }
  104. }
  105. if (strcmp(cmdname, CMD_PRINTENV) == 0) {
  106. if (fw_printenv (argc, argv) != 0)
  107. return EXIT_FAILURE;
  108. return EXIT_SUCCESS;
  109. } else if (strcmp(cmdname, CMD_SETENV) == 0) {
  110. if (!script_file) {
  111. if (fw_setenv(argc, argv) != 0)
  112. return EXIT_FAILURE;
  113. } else {
  114. if (fw_parse_script(script_file) != 0)
  115. return EXIT_FAILURE;
  116. }
  117. return EXIT_SUCCESS;
  118. }
  119. fprintf (stderr,
  120. "Identity crisis - may be called as `" CMD_PRINTENV
  121. "' or as `" CMD_SETENV "' but not as `%s'\n",
  122. cmdname);
  123. return EXIT_FAILURE;
  124. }