common.c 7.3 KB

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