env_common.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. }
  109. /* [re]set individual variables to their value in the default environment */
  110. int set_default_vars(int nvars, char * const vars[])
  111. {
  112. /*
  113. * Special use-case: import from default environment
  114. * (and use \0 as a separator)
  115. */
  116. return himport_r(&env_htab, (const char *)default_environment,
  117. sizeof(default_environment), '\0',
  118. H_NOCLEAR | H_INTERACTIVE, 0, nvars, vars);
  119. }
  120. #ifdef CONFIG_ENV_AES
  121. #include <aes.h>
  122. /**
  123. * env_aes_cbc_get_key() - Get AES-128-CBC key for the environment
  124. *
  125. * This function shall return 16-byte array containing AES-128 key used
  126. * to encrypt and decrypt the environment. This function must be overriden
  127. * by the implementer as otherwise the environment encryption will not
  128. * work.
  129. */
  130. __weak uint8_t *env_aes_cbc_get_key(void)
  131. {
  132. return NULL;
  133. }
  134. static int env_aes_cbc_crypt(env_t *env, const int enc)
  135. {
  136. unsigned char *data = env->data;
  137. uint8_t *key;
  138. uint8_t key_exp[AES_EXPAND_KEY_LENGTH];
  139. uint32_t aes_blocks;
  140. key = env_aes_cbc_get_key();
  141. if (!key)
  142. return -EINVAL;
  143. /* First we expand the key. */
  144. aes_expand_key(key, key_exp);
  145. /* Calculate the number of AES blocks to encrypt. */
  146. aes_blocks = ENV_SIZE / AES_KEY_LENGTH;
  147. if (enc)
  148. aes_cbc_encrypt_blocks(key_exp, data, data, aes_blocks);
  149. else
  150. aes_cbc_decrypt_blocks(key_exp, data, data, aes_blocks);
  151. return 0;
  152. }
  153. #else
  154. static inline int env_aes_cbc_crypt(env_t *env, const int enc)
  155. {
  156. return 0;
  157. }
  158. #endif
  159. /*
  160. * Check if CRC is valid and (if yes) import the environment.
  161. * Note that "buf" may or may not be aligned.
  162. */
  163. int env_import(const char *buf, int check)
  164. {
  165. env_t *ep = (env_t *)buf;
  166. int ret;
  167. if (check) {
  168. uint32_t crc;
  169. memcpy(&crc, &ep->crc, sizeof(crc));
  170. if (crc32(0, ep->data, ENV_SIZE) != crc) {
  171. set_default_env("!bad CRC");
  172. return 0;
  173. }
  174. }
  175. /* Decrypt the env if desired. */
  176. ret = env_aes_cbc_crypt(ep, 0);
  177. if (ret) {
  178. error("Failed to decrypt env!\n");
  179. set_default_env("!import failed");
  180. return ret;
  181. }
  182. if (himport_r(&env_htab, (char *)ep->data, ENV_SIZE, '\0', 0, 0,
  183. 0, NULL)) {
  184. gd->flags |= GD_FLG_ENV_READY;
  185. return 1;
  186. }
  187. error("Cannot import environment: errno = %d\n", errno);
  188. set_default_env("!import failed");
  189. return 0;
  190. }
  191. /* Emport the environment and generate CRC for it. */
  192. int env_export(env_t *env_out)
  193. {
  194. char *res;
  195. ssize_t len;
  196. int ret;
  197. res = (char *)env_out->data;
  198. len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
  199. if (len < 0) {
  200. error("Cannot export environment: errno = %d\n", errno);
  201. return 1;
  202. }
  203. /* Encrypt the env if desired. */
  204. ret = env_aes_cbc_crypt(env_out, 1);
  205. if (ret)
  206. return ret;
  207. env_out->crc = crc32(0, env_out->data, ENV_SIZE);
  208. return 0;
  209. }
  210. void env_relocate(void)
  211. {
  212. #if defined(CONFIG_NEEDS_MANUAL_RELOC)
  213. env_reloc();
  214. env_htab.change_ok += gd->reloc_off;
  215. #endif
  216. if (gd->env_valid == 0) {
  217. #if defined(CONFIG_ENV_IS_NOWHERE) || defined(CONFIG_SPL_BUILD)
  218. /* Environment not changable */
  219. set_default_env(NULL);
  220. #else
  221. bootstage_error(BOOTSTAGE_ID_NET_CHECKSUM);
  222. set_default_env("!bad CRC");
  223. #endif
  224. } else {
  225. env_relocate_spec();
  226. }
  227. }
  228. #if defined(CONFIG_AUTO_COMPLETE) && !defined(CONFIG_SPL_BUILD)
  229. int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf)
  230. {
  231. ENTRY *match;
  232. int found, idx;
  233. idx = 0;
  234. found = 0;
  235. cmdv[0] = NULL;
  236. while ((idx = hmatch_r(var, idx, &match, &env_htab))) {
  237. int vallen = strlen(match->key) + 1;
  238. if (found >= maxv - 2 || bufsz < vallen)
  239. break;
  240. cmdv[found++] = buf;
  241. memcpy(buf, match->key, vallen);
  242. buf += vallen;
  243. bufsz -= vallen;
  244. }
  245. qsort(cmdv, found, sizeof(cmdv[0]), strcmp_compar);
  246. if (idx)
  247. cmdv[found++] = "...";
  248. cmdv[found] = NULL;
  249. return found;
  250. }
  251. #endif