common.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2000-2010
  4. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  5. *
  6. * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  7. * Andreas Heppel <aheppel@sysgo.de>
  8. */
  9. #include <common.h>
  10. #include <bootstage.h>
  11. #include <command.h>
  12. #include <env.h>
  13. #include <env_internal.h>
  14. #include <sort.h>
  15. #include <linux/stddef.h>
  16. #include <search.h>
  17. #include <errno.h>
  18. #include <malloc.h>
  19. #include <u-boot/crc.h>
  20. DECLARE_GLOBAL_DATA_PTR;
  21. /************************************************************************
  22. * Default settings to be used when no valid environment is found
  23. */
  24. #include <env_default.h>
  25. struct hsearch_data env_htab = {
  26. .change_ok = env_flags_validate,
  27. };
  28. /*
  29. * Read an environment variable as a boolean
  30. * Return -1 if variable does not exist (default to true)
  31. */
  32. int env_get_yesno(const char *var)
  33. {
  34. char *s = env_get(var);
  35. if (s == NULL)
  36. return -1;
  37. return (*s == '1' || *s == 'y' || *s == 'Y' || *s == 't' || *s == 'T') ?
  38. 1 : 0;
  39. }
  40. /*
  41. * Look up the variable from the default environment
  42. */
  43. char *env_get_default(const char *name)
  44. {
  45. char *ret_val;
  46. unsigned long really_valid = gd->env_valid;
  47. unsigned long real_gd_flags = gd->flags;
  48. /* Pretend that the image is bad. */
  49. gd->flags &= ~GD_FLG_ENV_READY;
  50. gd->env_valid = ENV_INVALID;
  51. ret_val = env_get(name);
  52. gd->env_valid = really_valid;
  53. gd->flags = real_gd_flags;
  54. return ret_val;
  55. }
  56. void env_set_default(const char *s, int flags)
  57. {
  58. if (sizeof(default_environment) > ENV_SIZE) {
  59. puts("*** Error - default environment is too large\n\n");
  60. return;
  61. }
  62. if (s) {
  63. if ((flags & H_INTERACTIVE) == 0) {
  64. printf("*** Warning - %s, "
  65. "using default environment\n\n", s);
  66. } else {
  67. puts(s);
  68. }
  69. } else {
  70. debug("Using default environment\n");
  71. }
  72. if (himport_r(&env_htab, (char *)default_environment,
  73. sizeof(default_environment), '\0', flags, 0,
  74. 0, NULL) == 0)
  75. pr_err("## Error: Environment import failed: errno = %d\n",
  76. errno);
  77. gd->flags |= GD_FLG_ENV_READY;
  78. gd->flags |= GD_FLG_ENV_DEFAULT;
  79. }
  80. /* [re]set individual variables to their value in the default environment */
  81. int env_set_default_vars(int nvars, char * const vars[], int flags)
  82. {
  83. /*
  84. * Special use-case: import from default environment
  85. * (and use \0 as a separator)
  86. */
  87. flags |= H_NOCLEAR;
  88. return himport_r(&env_htab, (const char *)default_environment,
  89. sizeof(default_environment), '\0',
  90. flags, 0, nvars, vars);
  91. }
  92. /*
  93. * Check if CRC is valid and (if yes) import the environment.
  94. * Note that "buf" may or may not be aligned.
  95. */
  96. int env_import(const char *buf, int check)
  97. {
  98. env_t *ep = (env_t *)buf;
  99. if (check) {
  100. uint32_t crc;
  101. memcpy(&crc, &ep->crc, sizeof(crc));
  102. if (crc32(0, ep->data, ENV_SIZE) != crc) {
  103. env_set_default("bad CRC", 0);
  104. return -ENOMSG; /* needed for env_load() */
  105. }
  106. }
  107. if (himport_r(&env_htab, (char *)ep->data, ENV_SIZE, '\0', 0, 0,
  108. 0, NULL)) {
  109. gd->flags |= GD_FLG_ENV_READY;
  110. return 0;
  111. }
  112. pr_err("Cannot import environment: errno = %d\n", errno);
  113. env_set_default("import failed", 0);
  114. return -EIO;
  115. }
  116. #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
  117. static unsigned char env_flags;
  118. int env_import_redund(const char *buf1, int buf1_read_fail,
  119. const char *buf2, int buf2_read_fail)
  120. {
  121. int crc1_ok, crc2_ok;
  122. env_t *ep, *tmp_env1, *tmp_env2;
  123. tmp_env1 = (env_t *)buf1;
  124. tmp_env2 = (env_t *)buf2;
  125. if (buf1_read_fail && buf2_read_fail) {
  126. puts("*** Error - No Valid Environment Area found\n");
  127. } else if (buf1_read_fail || buf2_read_fail) {
  128. puts("*** Warning - some problems detected ");
  129. puts("reading environment; recovered successfully\n");
  130. }
  131. if (buf1_read_fail && buf2_read_fail) {
  132. env_set_default("bad env area", 0);
  133. return -EIO;
  134. } else if (!buf1_read_fail && buf2_read_fail) {
  135. gd->env_valid = ENV_VALID;
  136. return env_import((char *)tmp_env1, 1);
  137. } else if (buf1_read_fail && !buf2_read_fail) {
  138. gd->env_valid = ENV_REDUND;
  139. return env_import((char *)tmp_env2, 1);
  140. }
  141. crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) ==
  142. tmp_env1->crc;
  143. crc2_ok = crc32(0, tmp_env2->data, ENV_SIZE) ==
  144. tmp_env2->crc;
  145. if (!crc1_ok && !crc2_ok) {
  146. env_set_default("bad CRC", 0);
  147. return -ENOMSG; /* needed for env_load() */
  148. } else if (crc1_ok && !crc2_ok) {
  149. gd->env_valid = ENV_VALID;
  150. } else if (!crc1_ok && crc2_ok) {
  151. gd->env_valid = ENV_REDUND;
  152. } else {
  153. /* both ok - check serial */
  154. if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
  155. gd->env_valid = ENV_REDUND;
  156. else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
  157. gd->env_valid = ENV_VALID;
  158. else if (tmp_env1->flags > tmp_env2->flags)
  159. gd->env_valid = ENV_VALID;
  160. else if (tmp_env2->flags > tmp_env1->flags)
  161. gd->env_valid = ENV_REDUND;
  162. else /* flags are equal - almost impossible */
  163. gd->env_valid = ENV_VALID;
  164. }
  165. if (gd->env_valid == ENV_VALID)
  166. ep = tmp_env1;
  167. else
  168. ep = tmp_env2;
  169. env_flags = ep->flags;
  170. return env_import((char *)ep, 0);
  171. }
  172. #endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */
  173. /* Export the environment and generate CRC for it. */
  174. int env_export(env_t *env_out)
  175. {
  176. char *res;
  177. ssize_t len;
  178. res = (char *)env_out->data;
  179. len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
  180. if (len < 0) {
  181. pr_err("Cannot export environment: errno = %d\n", errno);
  182. return 1;
  183. }
  184. env_out->crc = crc32(0, env_out->data, ENV_SIZE);
  185. #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
  186. env_out->flags = ++env_flags; /* increase the serial */
  187. #endif
  188. return 0;
  189. }
  190. void env_relocate(void)
  191. {
  192. #if defined(CONFIG_NEEDS_MANUAL_RELOC)
  193. env_reloc();
  194. env_fix_drivers();
  195. env_htab.change_ok += gd->reloc_off;
  196. #endif
  197. if (gd->env_valid == ENV_INVALID) {
  198. #if defined(CONFIG_ENV_IS_NOWHERE) || defined(CONFIG_SPL_BUILD)
  199. /* Environment not changable */
  200. env_set_default(NULL, 0);
  201. #else
  202. bootstage_error(BOOTSTAGE_ID_NET_CHECKSUM);
  203. env_set_default("bad CRC", 0);
  204. #endif
  205. } else {
  206. env_load();
  207. }
  208. }
  209. #ifdef CONFIG_AUTO_COMPLETE
  210. int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf,
  211. bool dollar_comp)
  212. {
  213. struct env_entry *match;
  214. int found, idx;
  215. if (dollar_comp) {
  216. /*
  217. * When doing $ completion, the first character should
  218. * obviously be a '$'.
  219. */
  220. if (var[0] != '$')
  221. return 0;
  222. var++;
  223. /*
  224. * The second one, if present, should be a '{', as some
  225. * configuration of the u-boot shell expand ${var} but not
  226. * $var.
  227. */
  228. if (var[0] == '{')
  229. var++;
  230. else if (var[0] != '\0')
  231. return 0;
  232. }
  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 ||
  239. bufsz < vallen + (dollar_comp ? 3 : 0))
  240. break;
  241. cmdv[found++] = buf;
  242. /* Add the '${' prefix to each var when doing $ completion. */
  243. if (dollar_comp) {
  244. strcpy(buf, "${");
  245. buf += 2;
  246. bufsz -= 3;
  247. }
  248. memcpy(buf, match->key, vallen);
  249. buf += vallen;
  250. bufsz -= vallen;
  251. if (dollar_comp) {
  252. /*
  253. * This one is a bit odd: vallen already contains the
  254. * '\0' character but we need to add the '}' suffix,
  255. * hence the buf - 1 here. strcpy() will add the '\0'
  256. * character just after '}'. buf is then incremented
  257. * to account for the extra '}' we just added.
  258. */
  259. strcpy(buf - 1, "}");
  260. buf++;
  261. }
  262. }
  263. qsort(cmdv, found, sizeof(cmdv[0]), strcmp_compar);
  264. if (idx)
  265. cmdv[found++] = dollar_comp ? "${...}" : "...";
  266. cmdv[found] = NULL;
  267. return found;
  268. }
  269. #endif