common.c 7.2 KB

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