common.c 6.8 KB

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