common.c 7.8 KB

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