env_common.c 6.2 KB

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