common.c 7.2 KB

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