env_common.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. * (C) Copyright 2000-2010
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  6. * Andreas Heppel <aheppel@sysgo.de>
  7. *
  8. * SPDX-License-Identifier: GPL-2.0+
  9. */
  10. #include <common.h>
  11. #include <command.h>
  12. #include <environment.h>
  13. #include <linux/stddef.h>
  14. #include <search.h>
  15. #include <errno.h>
  16. #include <malloc.h>
  17. DECLARE_GLOBAL_DATA_PTR;
  18. /************************************************************************
  19. * Default settings to be used when no valid environment is found
  20. */
  21. #include <env_default.h>
  22. struct hsearch_data env_htab = {
  23. .change_ok = env_flags_validate,
  24. };
  25. __weak uchar env_get_char_spec(int index)
  26. {
  27. return *((uchar *)(gd->env_addr + index));
  28. }
  29. static uchar env_get_char_init(int index)
  30. {
  31. /* if crc was bad, use the default environment */
  32. if (gd->env_valid)
  33. return env_get_char_spec(index);
  34. else
  35. return default_environment[index];
  36. }
  37. uchar env_get_char_memory(int index)
  38. {
  39. return *env_get_addr(index);
  40. }
  41. uchar env_get_char(int index)
  42. {
  43. /* if relocated to RAM */
  44. if (gd->flags & GD_FLG_RELOC)
  45. return env_get_char_memory(index);
  46. else
  47. return env_get_char_init(index);
  48. }
  49. const uchar *env_get_addr(int index)
  50. {
  51. if (gd->env_valid)
  52. return (uchar *)(gd->env_addr + index);
  53. else
  54. return &default_environment[index];
  55. }
  56. /*
  57. * Read an environment variable as a boolean
  58. * Return -1 if variable does not exist (default to true)
  59. */
  60. int getenv_yesno(const char *var)
  61. {
  62. char *s = getenv(var);
  63. if (s == NULL)
  64. return -1;
  65. return (*s == '1' || *s == 'y' || *s == 'Y' || *s == 't' || *s == 'T') ?
  66. 1 : 0;
  67. }
  68. /*
  69. * Look up the variable from the default environment
  70. */
  71. char *getenv_default(const char *name)
  72. {
  73. char *ret_val;
  74. unsigned long really_valid = gd->env_valid;
  75. unsigned long real_gd_flags = gd->flags;
  76. /* Pretend that the image is bad. */
  77. gd->flags &= ~GD_FLG_ENV_READY;
  78. gd->env_valid = 0;
  79. ret_val = getenv(name);
  80. gd->env_valid = really_valid;
  81. gd->flags = real_gd_flags;
  82. return ret_val;
  83. }
  84. void set_default_env(const char *s)
  85. {
  86. int flags = 0;
  87. if (sizeof(default_environment) > ENV_SIZE) {
  88. puts("*** Error - default environment is too large\n\n");
  89. return;
  90. }
  91. if (s) {
  92. if (*s == '!') {
  93. printf("*** Warning - %s, "
  94. "using default environment\n\n",
  95. s + 1);
  96. } else {
  97. flags = H_INTERACTIVE;
  98. puts(s);
  99. }
  100. } else {
  101. puts("Using default environment\n\n");
  102. }
  103. if (himport_r(&env_htab, (char *)default_environment,
  104. sizeof(default_environment), '\0', flags, 0,
  105. 0, NULL) == 0)
  106. error("Environment import failed: errno = %d\n", errno);
  107. gd->flags |= GD_FLG_ENV_READY;
  108. gd->flags |= GD_FLG_ENV_DEFAULT;
  109. }
  110. /* [re]set individual variables to their value in the default environment */
  111. int set_default_vars(int nvars, char * const vars[])
  112. {
  113. /*
  114. * Special use-case: import from default environment
  115. * (and use \0 as a separator)
  116. */
  117. return himport_r(&env_htab, (const char *)default_environment,
  118. sizeof(default_environment), '\0',
  119. H_NOCLEAR | H_INTERACTIVE, 0, nvars, vars);
  120. }
  121. #ifdef CONFIG_ENV_AES
  122. #include <aes.h>
  123. /**
  124. * env_aes_cbc_get_key() - Get AES-128-CBC key for the environment
  125. *
  126. * This function shall return 16-byte array containing AES-128 key used
  127. * to encrypt and decrypt the environment. This function must be overridden
  128. * by the implementer as otherwise the environment encryption will not
  129. * work.
  130. */
  131. __weak uint8_t *env_aes_cbc_get_key(void)
  132. {
  133. return NULL;
  134. }
  135. static int env_aes_cbc_crypt(env_t *env, const int enc)
  136. {
  137. unsigned char *data = env->data;
  138. uint8_t *key;
  139. uint8_t key_exp[AES_EXPAND_KEY_LENGTH];
  140. uint32_t aes_blocks;
  141. key = env_aes_cbc_get_key();
  142. if (!key)
  143. return -EINVAL;
  144. /* First we expand the key. */
  145. aes_expand_key(key, key_exp);
  146. /* Calculate the number of AES blocks to encrypt. */
  147. aes_blocks = ENV_SIZE / AES_KEY_LENGTH;
  148. if (enc)
  149. aes_cbc_encrypt_blocks(key_exp, data, data, aes_blocks);
  150. else
  151. aes_cbc_decrypt_blocks(key_exp, data, data, aes_blocks);
  152. return 0;
  153. }
  154. #else
  155. static inline int env_aes_cbc_crypt(env_t *env, const int enc)
  156. {
  157. return 0;
  158. }
  159. #endif
  160. /*
  161. * Check if CRC is valid and (if yes) import the environment.
  162. * Note that "buf" may or may not be aligned.
  163. */
  164. int env_import(const char *buf, int check)
  165. {
  166. env_t *ep = (env_t *)buf;
  167. int ret;
  168. if (check) {
  169. uint32_t crc;
  170. memcpy(&crc, &ep->crc, sizeof(crc));
  171. if (crc32(0, ep->data, ENV_SIZE) != crc) {
  172. set_default_env("!bad CRC");
  173. return 0;
  174. }
  175. }
  176. /* Decrypt the env if desired. */
  177. ret = env_aes_cbc_crypt(ep, 0);
  178. if (ret) {
  179. error("Failed to decrypt env!\n");
  180. set_default_env("!import failed");
  181. return ret;
  182. }
  183. if (himport_r(&env_htab, (char *)ep->data, ENV_SIZE, '\0', 0, 0,
  184. 0, NULL)) {
  185. gd->flags |= GD_FLG_ENV_READY;
  186. return 1;
  187. }
  188. error("Cannot import environment: errno = %d\n", errno);
  189. set_default_env("!import failed");
  190. return 0;
  191. }
  192. /* Emport the environment and generate CRC for it. */
  193. int env_export(env_t *env_out)
  194. {
  195. char *res;
  196. ssize_t len;
  197. int ret;
  198. res = (char *)env_out->data;
  199. len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
  200. if (len < 0) {
  201. error("Cannot export environment: errno = %d\n", errno);
  202. return 1;
  203. }
  204. /* Encrypt the env if desired. */
  205. ret = env_aes_cbc_crypt(env_out, 1);
  206. if (ret)
  207. return ret;
  208. env_out->crc = crc32(0, env_out->data, ENV_SIZE);
  209. return 0;
  210. }
  211. void env_relocate(void)
  212. {
  213. #if defined(CONFIG_NEEDS_MANUAL_RELOC)
  214. env_reloc();
  215. env_htab.change_ok += gd->reloc_off;
  216. #endif
  217. if (gd->env_valid == 0) {
  218. #if defined(CONFIG_ENV_IS_NOWHERE) || defined(CONFIG_SPL_BUILD)
  219. /* Environment not changable */
  220. set_default_env(NULL);
  221. #else
  222. bootstage_error(BOOTSTAGE_ID_NET_CHECKSUM);
  223. set_default_env("!bad CRC");
  224. #endif
  225. } else {
  226. env_relocate_spec();
  227. }
  228. }
  229. #if defined(CONFIG_AUTO_COMPLETE) && !defined(CONFIG_SPL_BUILD)
  230. int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf)
  231. {
  232. ENTRY *match;
  233. int found, idx;
  234. idx = 0;
  235. found = 0;
  236. cmdv[0] = NULL;
  237. while ((idx = hmatch_r(var, idx, &match, &env_htab))) {
  238. int vallen = strlen(match->key) + 1;
  239. if (found >= maxv - 2 || bufsz < vallen)
  240. break;
  241. cmdv[found++] = buf;
  242. memcpy(buf, match->key, vallen);
  243. buf += vallen;
  244. bufsz -= vallen;
  245. }
  246. qsort(cmdv, found, sizeof(cmdv[0]), strcmp_compar);
  247. if (idx)
  248. cmdv[found++] = "...";
  249. cmdv[found] = NULL;
  250. return found;
  251. }
  252. #endif