fw_env_main.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * (C) Copyright 2000-2008
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. /*
  8. * Command line user interface to firmware (=U-Boot) environment.
  9. *
  10. * Implements:
  11. * fw_printenv [ -a key ] [[ -n name ] | [ name ... ]]
  12. * - prints the value of a single environment variable
  13. * "name", the ``name=value'' pairs of one or more
  14. * environment variables "name", or the whole
  15. * environment if no names are specified.
  16. * fw_setenv [ -a key ] name [ value ... ]
  17. * - If a name without any values is given, the variable
  18. * with this name is deleted from the environment;
  19. * otherwise, all "value" arguments are concatenated,
  20. * separated by single blank characters, and the
  21. * resulting string is assigned to the environment
  22. * variable "name"
  23. *
  24. * If '-a key' is specified, the env block is encrypted with AES 128 CBC.
  25. * The 'key' argument is in the format of 32 hexadecimal numbers (16 bytes
  26. * of AES key), eg. '-a aabbccddeeff00112233445566778899'.
  27. */
  28. #include <fcntl.h>
  29. #include <getopt.h>
  30. #include <stdio.h>
  31. #include <string.h>
  32. #include <stdlib.h>
  33. #include <sys/file.h>
  34. #include <unistd.h>
  35. #include "fw_env.h"
  36. #define CMD_PRINTENV "fw_printenv"
  37. #define CMD_SETENV "fw_setenv"
  38. static int do_printenv;
  39. static struct option long_options[] = {
  40. {"aes", required_argument, NULL, 'a'},
  41. {"config", required_argument, NULL, 'c'},
  42. {"help", no_argument, NULL, 'h'},
  43. {"script", required_argument, NULL, 's'},
  44. {"noheader", required_argument, NULL, 'n'},
  45. {NULL, 0, NULL, 0}
  46. };
  47. void usage_printenv(void)
  48. {
  49. fprintf(stderr,
  50. "Usage: fw_printenv [OPTIONS]... [VARIABLE]...\n"
  51. "Print variables from U-Boot environment\n"
  52. "\n"
  53. " -h, --help print this help.\n"
  54. #ifdef CONFIG_ENV_AES
  55. " -a, --aes aes key to access environment\n"
  56. #endif
  57. #ifdef CONFIG_FILE
  58. " -c, --config configuration file, default:" CONFIG_FILE "\n"
  59. #endif
  60. " -n, --noheader do not repeat variable name in output\n"
  61. "\n");
  62. }
  63. void usage_setenv(void)
  64. {
  65. fprintf(stderr,
  66. "Usage: fw_setenv [OPTIONS]... [VARIABLE]...\n"
  67. "Modify variables in U-Boot environment\n"
  68. "\n"
  69. " -h, --help print this help.\n"
  70. #ifdef CONFIG_ENV_AES
  71. " -a, --aes aes key to access environment\n"
  72. #endif
  73. #ifdef CONFIG_FILE
  74. " -c, --config configuration file, default:" CONFIG_FILE "\n"
  75. #endif
  76. " -s, --script batch mode to minimize writes\n"
  77. "\n"
  78. "Examples:\n"
  79. " fw_setenv foo bar set variable foo equal bar\n"
  80. " fw_setenv foo clear variable foo\n"
  81. " fw_setenv --script file run batch script\n"
  82. "\n"
  83. "Script Syntax:\n"
  84. " key [space] value\n"
  85. " lines starting with '#' are treated as comment\n"
  86. "\n"
  87. " A variable without value will be deleted. Any number of spaces are\n"
  88. " allowed between key and value. Space inside of the value is treated\n"
  89. " as part of the value itself.\n"
  90. "\n"
  91. "Script Example:\n"
  92. " netdev eth0\n"
  93. " kernel_addr 400000\n"
  94. " foo empty empty empty empty empty empty\n"
  95. " bar\n"
  96. "\n");
  97. }
  98. static void parse_common_args(int argc, char *argv[])
  99. {
  100. int c;
  101. #ifdef CONFIG_FILE
  102. common_args.config_file = CONFIG_FILE;
  103. #endif
  104. while ((c = getopt_long(argc, argv, ":a:c:h", long_options, NULL)) !=
  105. EOF) {
  106. switch (c) {
  107. case 'a':
  108. if (parse_aes_key(optarg, common_args.aes_key)) {
  109. fprintf(stderr, "AES key parse error\n");
  110. exit(EXIT_FAILURE);
  111. }
  112. common_args.aes_flag = 1;
  113. break;
  114. #ifdef CONFIG_FILE
  115. case 'c':
  116. common_args.config_file = optarg;
  117. break;
  118. #endif
  119. case 'h':
  120. do_printenv ? usage_printenv() : usage_setenv();
  121. exit(EXIT_SUCCESS);
  122. break;
  123. default:
  124. /* ignore unknown options */
  125. break;
  126. }
  127. }
  128. /* Reset getopt for the next pass. */
  129. opterr = 1;
  130. optind = 1;
  131. }
  132. int parse_printenv_args(int argc, char *argv[])
  133. {
  134. int c;
  135. parse_common_args(argc, argv);
  136. while ((c = getopt_long(argc, argv, "a:c:ns:h", long_options, NULL)) !=
  137. EOF) {
  138. switch (c) {
  139. case 'n':
  140. printenv_args.name_suppress = 1;
  141. break;
  142. case 'a':
  143. case 'c':
  144. case 'h':
  145. /* ignore common options */
  146. break;
  147. default: /* '?' */
  148. usage_printenv();
  149. exit(EXIT_FAILURE);
  150. break;
  151. }
  152. }
  153. return 0;
  154. }
  155. int parse_setenv_args(int argc, char *argv[])
  156. {
  157. int c;
  158. parse_common_args(argc, argv);
  159. while ((c = getopt_long(argc, argv, "a:c:ns:h", long_options, NULL)) !=
  160. EOF) {
  161. switch (c) {
  162. case 's':
  163. setenv_args.script_file = optarg;
  164. break;
  165. case 'a':
  166. case 'c':
  167. case 'h':
  168. /* ignore common options */
  169. break;
  170. default: /* '?' */
  171. usage_setenv();
  172. exit(EXIT_FAILURE);
  173. break;
  174. }
  175. }
  176. return 0;
  177. }
  178. int main(int argc, char *argv[])
  179. {
  180. const char *lockname = "/var/lock/" CMD_PRINTENV ".lock";
  181. int lockfd = -1;
  182. int retval = EXIT_SUCCESS;
  183. char *_cmdname;
  184. _cmdname = *argv;
  185. if (strrchr(_cmdname, '/') != NULL)
  186. _cmdname = strrchr(_cmdname, '/') + 1;
  187. if (strcmp(_cmdname, CMD_PRINTENV) == 0) {
  188. do_printenv = 1;
  189. } else if (strcmp(_cmdname, CMD_SETENV) == 0) {
  190. do_printenv = 0;
  191. } else {
  192. fprintf(stderr,
  193. "Identity crisis - may be called as `%s' or as `%s' but not as `%s'\n",
  194. CMD_PRINTENV, CMD_SETENV, _cmdname);
  195. exit(EXIT_FAILURE);
  196. }
  197. if (do_printenv) {
  198. if (parse_printenv_args(argc, argv))
  199. exit(EXIT_FAILURE);
  200. } else {
  201. if (parse_setenv_args(argc, argv))
  202. exit(EXIT_FAILURE);
  203. }
  204. /* shift parsed flags, jump to non-option arguments */
  205. argc -= optind;
  206. argv += optind;
  207. lockfd = open(lockname, O_WRONLY | O_CREAT | O_TRUNC, 0666);
  208. if (-1 == lockfd) {
  209. fprintf(stderr, "Error opening lock file %s\n", lockname);
  210. return EXIT_FAILURE;
  211. }
  212. if (-1 == flock(lockfd, LOCK_EX)) {
  213. fprintf(stderr, "Error locking file %s\n", lockname);
  214. close(lockfd);
  215. return EXIT_FAILURE;
  216. }
  217. if (do_printenv) {
  218. if (fw_printenv(argc, argv) != 0)
  219. retval = EXIT_FAILURE;
  220. } else {
  221. if (!setenv_args.script_file) {
  222. if (fw_setenv(argc, argv) != 0)
  223. retval = EXIT_FAILURE;
  224. } else {
  225. if (fw_parse_script(setenv_args.script_file) != 0)
  226. retval = EXIT_FAILURE;
  227. }
  228. }
  229. flock(lockfd, LOCK_UN);
  230. close(lockfd);
  231. return retval;
  232. }