fw_env_main.c 6.7 KB

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