common.c 7.4 KB

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