common.c 8.0 KB

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