fw_env_main.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. static struct env_opts env_opts;
  48. /* setenv options */
  49. static int noheader;
  50. /* getenv options */
  51. static char *script_file;
  52. void usage_printenv(void)
  53. {
  54. fprintf(stderr,
  55. "Usage: fw_printenv [OPTIONS]... [VARIABLE]...\n"
  56. "Print variables from U-Boot environment\n"
  57. "\n"
  58. " -h, --help print this help.\n"
  59. #ifdef CONFIG_ENV_AES
  60. " -a, --aes aes key to access environment\n"
  61. #endif
  62. #ifdef CONFIG_FILE
  63. " -c, --config configuration file, default:" CONFIG_FILE "\n"
  64. #endif
  65. " -n, --noheader do not repeat variable name in output\n"
  66. "\n");
  67. }
  68. void usage_setenv(void)
  69. {
  70. fprintf(stderr,
  71. "Usage: fw_setenv [OPTIONS]... [VARIABLE]...\n"
  72. "Modify variables in U-Boot environment\n"
  73. "\n"
  74. " -h, --help print this help.\n"
  75. #ifdef CONFIG_ENV_AES
  76. " -a, --aes aes key to access environment\n"
  77. #endif
  78. #ifdef CONFIG_FILE
  79. " -c, --config configuration file, default:" CONFIG_FILE "\n"
  80. #endif
  81. " -s, --script batch mode to minimize writes\n"
  82. "\n"
  83. "Examples:\n"
  84. " fw_setenv foo bar set variable foo equal bar\n"
  85. " fw_setenv foo clear variable foo\n"
  86. " fw_setenv --script file run batch script\n"
  87. "\n"
  88. "Script Syntax:\n"
  89. " key [space] value\n"
  90. " lines starting with '#' are treated as comment\n"
  91. "\n"
  92. " A variable without value will be deleted. Any number of spaces are\n"
  93. " allowed between key and value. Space inside of the value is treated\n"
  94. " as part of the value itself.\n"
  95. "\n"
  96. "Script Example:\n"
  97. " netdev eth0\n"
  98. " kernel_addr 400000\n"
  99. " foo empty empty empty empty empty empty\n"
  100. " bar\n"
  101. "\n");
  102. }
  103. static void parse_common_args(int argc, char *argv[])
  104. {
  105. int c;
  106. #ifdef CONFIG_FILE
  107. env_opts.config_file = CONFIG_FILE;
  108. #endif
  109. while ((c = getopt_long(argc, argv, ":a:c:h", long_options, NULL)) !=
  110. EOF) {
  111. switch (c) {
  112. case 'a':
  113. if (parse_aes_key(optarg, env_opts.aes_key)) {
  114. fprintf(stderr, "AES key parse error\n");
  115. exit(EXIT_FAILURE);
  116. }
  117. env_opts.aes_flag = 1;
  118. break;
  119. #ifdef CONFIG_FILE
  120. case 'c':
  121. env_opts.config_file = optarg;
  122. break;
  123. #endif
  124. case 'h':
  125. do_printenv ? usage_printenv() : usage_setenv();
  126. exit(EXIT_SUCCESS);
  127. break;
  128. default:
  129. /* ignore unknown options */
  130. break;
  131. }
  132. }
  133. /* Reset getopt for the next pass. */
  134. opterr = 1;
  135. optind = 1;
  136. }
  137. int parse_printenv_args(int argc, char *argv[])
  138. {
  139. int c;
  140. parse_common_args(argc, argv);
  141. while ((c = getopt_long(argc, argv, "a:c:ns:h", long_options, NULL)) !=
  142. EOF) {
  143. switch (c) {
  144. case 'n':
  145. noheader = 1;
  146. break;
  147. case 'a':
  148. case 'c':
  149. case 'h':
  150. /* ignore common options */
  151. break;
  152. default: /* '?' */
  153. usage_printenv();
  154. exit(EXIT_FAILURE);
  155. break;
  156. }
  157. }
  158. return 0;
  159. }
  160. int parse_setenv_args(int argc, char *argv[])
  161. {
  162. int c;
  163. parse_common_args(argc, argv);
  164. while ((c = getopt_long(argc, argv, "a:c:ns:h", long_options, NULL)) !=
  165. EOF) {
  166. switch (c) {
  167. case 's':
  168. script_file = optarg;
  169. break;
  170. case 'a':
  171. case 'c':
  172. case 'h':
  173. /* ignore common options */
  174. break;
  175. default: /* '?' */
  176. usage_setenv();
  177. exit(EXIT_FAILURE);
  178. break;
  179. }
  180. }
  181. return 0;
  182. }
  183. int main(int argc, char *argv[])
  184. {
  185. const char *lockname = "/var/lock/" CMD_PRINTENV ".lock";
  186. int lockfd = -1;
  187. int retval = EXIT_SUCCESS;
  188. char *_cmdname;
  189. _cmdname = *argv;
  190. if (strrchr(_cmdname, '/') != NULL)
  191. _cmdname = strrchr(_cmdname, '/') + 1;
  192. if (strcmp(_cmdname, CMD_PRINTENV) == 0) {
  193. do_printenv = 1;
  194. } else if (strcmp(_cmdname, CMD_SETENV) == 0) {
  195. do_printenv = 0;
  196. } else {
  197. fprintf(stderr,
  198. "Identity crisis - may be called as `%s' or as `%s' but not as `%s'\n",
  199. CMD_PRINTENV, CMD_SETENV, _cmdname);
  200. exit(EXIT_FAILURE);
  201. }
  202. if (do_printenv) {
  203. if (parse_printenv_args(argc, argv))
  204. exit(EXIT_FAILURE);
  205. } else {
  206. if (parse_setenv_args(argc, argv))
  207. exit(EXIT_FAILURE);
  208. }
  209. /* shift parsed flags, jump to non-option arguments */
  210. argc -= optind;
  211. argv += optind;
  212. lockfd = open(lockname, O_WRONLY | O_CREAT | O_TRUNC, 0666);
  213. if (-1 == lockfd) {
  214. fprintf(stderr, "Error opening lock file %s\n", lockname);
  215. return EXIT_FAILURE;
  216. }
  217. if (-1 == flock(lockfd, LOCK_EX)) {
  218. fprintf(stderr, "Error locking file %s\n", lockname);
  219. close(lockfd);
  220. return EXIT_FAILURE;
  221. }
  222. if (do_printenv) {
  223. if (fw_printenv(argc, argv, noheader, &env_opts) != 0)
  224. retval = EXIT_FAILURE;
  225. } else {
  226. if (!script_file) {
  227. if (fw_setenv(argc, argv, &env_opts) != 0)
  228. retval = EXIT_FAILURE;
  229. } else {
  230. if (fw_parse_script(script_file, &env_opts) != 0)
  231. retval = EXIT_FAILURE;
  232. }
  233. }
  234. flock(lockfd, LOCK_UN);
  235. close(lockfd);
  236. return retval;
  237. }