common.c 7.3 KB

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