fw_env_main.c 6.7 KB

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