fw_env_main.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2000-2008
  4. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  5. */
  6. /*
  7. * Command line user interface to firmware (=U-Boot) environment.
  8. *
  9. * Implements:
  10. * fw_printenv [ -a key ] [[ -n name ] | [ name ... ]]
  11. * - prints the value of a single environment variable
  12. * "name", the ``name=value'' pairs of one or more
  13. * environment variables "name", or the whole
  14. * environment if no names are specified.
  15. * fw_setenv [ -a key ] name [ value ... ]
  16. * - If a name without any values is given, the variable
  17. * with this name is deleted from the environment;
  18. * otherwise, all "value" arguments are concatenated,
  19. * separated by single blank characters, and the
  20. * resulting string is assigned to the environment
  21. * variable "name"
  22. *
  23. * If '-a key' is specified, the env block is encrypted with AES 128 CBC.
  24. * The 'key' argument is in the format of 32 hexadecimal numbers (16 bytes
  25. * of AES key), eg. '-a aabbccddeeff00112233445566778899'.
  26. */
  27. #include <fcntl.h>
  28. #include <getopt.h>
  29. #include <stdio.h>
  30. #include <string.h>
  31. #include <stdlib.h>
  32. #include <sys/file.h>
  33. #include <unistd.h>
  34. #include <version.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. {"config", required_argument, NULL, 'c'},
  42. {"help", no_argument, NULL, 'h'},
  43. {"script", required_argument, NULL, 's'},
  44. {"noheader", no_argument, NULL, 'n'},
  45. {"lock", required_argument, NULL, 'l'},
  46. {"version", no_argument, NULL, 'v'},
  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. " -v, --version display version\n"
  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. " -l, --lock lock node, default:/var/lock\n"
  67. "\n");
  68. }
  69. void usage_env_set(void)
  70. {
  71. fprintf(stderr,
  72. "Usage: fw_setenv [OPTIONS]... [VARIABLE]...\n"
  73. "Modify variables in U-Boot environment\n"
  74. "\n"
  75. " -h, --help print this help.\n"
  76. " -v, --version display version\n"
  77. #ifdef CONFIG_FILE
  78. " -c, --config configuration file, default:" CONFIG_FILE "\n"
  79. #endif
  80. " -l, --lock lock node, default:/var/lock\n"
  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:l:h:v", long_options, NULL)) !=
  110. EOF) {
  111. switch (c) {
  112. #ifdef CONFIG_FILE
  113. case 'c':
  114. env_opts.config_file = optarg;
  115. break;
  116. #endif
  117. case 'l':
  118. env_opts.lockname = optarg;
  119. break;
  120. case 'h':
  121. do_printenv ? usage_printenv() : usage_env_set();
  122. exit(EXIT_SUCCESS);
  123. break;
  124. case 'v':
  125. fprintf(stderr, "Compiled with " U_BOOT_VERSION "\n");
  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:l:h:v", 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. case 'l':
  151. /* ignore common options */
  152. break;
  153. default: /* '?' */
  154. usage_printenv();
  155. exit(EXIT_FAILURE);
  156. break;
  157. }
  158. }
  159. return 0;
  160. }
  161. int parse_setenv_args(int argc, char *argv[])
  162. {
  163. int c;
  164. parse_common_args(argc, argv);
  165. while ((c = getopt_long(argc, argv, "a:c:ns:l:h:v", long_options, NULL))
  166. != EOF) {
  167. switch (c) {
  168. case 's':
  169. script_file = optarg;
  170. break;
  171. case 'a':
  172. case 'c':
  173. case 'h':
  174. case 'l':
  175. /* ignore common options */
  176. break;
  177. default: /* '?' */
  178. usage_env_set();
  179. exit(EXIT_FAILURE);
  180. break;
  181. }
  182. }
  183. return 0;
  184. }
  185. int main(int argc, char *argv[])
  186. {
  187. char *lockname = "/var/lock/" CMD_PRINTENV ".lock";
  188. int lockfd = -1;
  189. int retval = EXIT_SUCCESS;
  190. char *_cmdname;
  191. _cmdname = *argv;
  192. if (strrchr(_cmdname, '/') != NULL)
  193. _cmdname = strrchr(_cmdname, '/') + 1;
  194. if (strcmp(_cmdname, CMD_PRINTENV) == 0) {
  195. do_printenv = 1;
  196. } else if (strcmp(_cmdname, CMD_SETENV) == 0) {
  197. do_printenv = 0;
  198. } else {
  199. fprintf(stderr,
  200. "Identity crisis - may be called as `%s' or as `%s' but not as `%s'\n",
  201. CMD_PRINTENV, CMD_SETENV, _cmdname);
  202. exit(EXIT_FAILURE);
  203. }
  204. if (do_printenv) {
  205. if (parse_printenv_args(argc, argv))
  206. exit(EXIT_FAILURE);
  207. } else {
  208. if (parse_setenv_args(argc, argv))
  209. exit(EXIT_FAILURE);
  210. }
  211. /* shift parsed flags, jump to non-option arguments */
  212. argc -= optind;
  213. argv += optind;
  214. if (env_opts.lockname) {
  215. lockname = malloc(strlen(env_opts.lockname) +
  216. sizeof(CMD_PRINTENV) + 10);
  217. if (!lockname) {
  218. fprintf(stderr, "Unable allocate memory");
  219. exit(EXIT_FAILURE);
  220. }
  221. sprintf(lockname, "%s/%s.lock",
  222. env_opts.lockname, CMD_PRINTENV);
  223. }
  224. lockfd = open(lockname, O_WRONLY | O_CREAT | O_TRUNC, 0666);
  225. if (-1 == lockfd) {
  226. fprintf(stderr, "Error opening lock file %s\n", lockname);
  227. return EXIT_FAILURE;
  228. }
  229. if (-1 == flock(lockfd, LOCK_EX)) {
  230. fprintf(stderr, "Error locking file %s\n", lockname);
  231. close(lockfd);
  232. return EXIT_FAILURE;
  233. }
  234. if (do_printenv) {
  235. if (fw_printenv(argc, argv, noheader, &env_opts) != 0)
  236. retval = EXIT_FAILURE;
  237. } else {
  238. if (!script_file) {
  239. if (fw_env_set(argc, argv, &env_opts) != 0)
  240. retval = EXIT_FAILURE;
  241. } else {
  242. if (fw_parse_script(script_file, &env_opts) != 0)
  243. retval = EXIT_FAILURE;
  244. }
  245. }
  246. if (env_opts.lockname)
  247. free(lockname);
  248. flock(lockfd, LOCK_UN);
  249. close(lockfd);
  250. return retval;
  251. }