common.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * (C) Copyright 2000-2010
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  6. * Andreas Heppel <aheppel@sysgo.de>
  7. *
  8. * SPDX-License-Identifier: GPL-2.0+
  9. */
  10. #include <common.h>
  11. #include <command.h>
  12. #include <environment.h>
  13. #include <linux/stddef.h>
  14. #include <search.h>
  15. #include <errno.h>
  16. #include <malloc.h>
  17. DECLARE_GLOBAL_DATA_PTR;
  18. /************************************************************************
  19. * Default settings to be used when no valid environment is found
  20. */
  21. #include <env_default.h>
  22. struct hsearch_data env_htab = {
  23. .change_ok = env_flags_validate,
  24. };
  25. /*
  26. * Read an environment variable as a boolean
  27. * Return -1 if variable does not exist (default to true)
  28. */
  29. int env_get_yesno(const char *var)
  30. {
  31. char *s = env_get(var);
  32. if (s == NULL)
  33. return -1;
  34. return (*s == '1' || *s == 'y' || *s == 'Y' || *s == 't' || *s == 'T') ?
  35. 1 : 0;
  36. }
  37. /*
  38. * Look up the variable from the default environment
  39. */
  40. char *env_get_default(const char *name)
  41. {
  42. char *ret_val;
  43. unsigned long really_valid = gd->env_valid;
  44. unsigned long real_gd_flags = gd->flags;
  45. /* Pretend that the image is bad. */
  46. gd->flags &= ~GD_FLG_ENV_READY;
  47. gd->env_valid = ENV_INVALID;
  48. ret_val = env_get(name);
  49. gd->env_valid = really_valid;
  50. gd->flags = real_gd_flags;
  51. return ret_val;
  52. }
  53. void set_default_env(const char *s)
  54. {
  55. int flags = 0;
  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 (*s == '!') {
  62. printf("*** Warning - %s, "
  63. "using default environment\n\n",
  64. s + 1);
  65. } else {
  66. flags = H_INTERACTIVE;
  67. puts(s);
  68. }
  69. } else {
  70. puts("Using default environment\n\n");
  71. }
  72. if (himport_r(&env_htab, (char *)default_environment,
  73. sizeof(default_environment), '\0', flags, 0,
  74. 0, NULL) == 0)
  75. pr_err("Environment import failed: errno = %d\n", errno);
  76. gd->flags |= GD_FLG_ENV_READY;
  77. gd->flags |= GD_FLG_ENV_DEFAULT;
  78. }
  79. /* [re]set individual variables to their value in the default environment */
  80. int set_default_vars(int nvars, char * const vars[])
  81. {
  82. /*
  83. * Special use-case: import from default environment
  84. * (and use \0 as a separator)
  85. */
  86. return himport_r(&env_htab, (const char *)default_environment,
  87. sizeof(default_environment), '\0',
  88. H_NOCLEAR | H_INTERACTIVE, 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. set_default_env("!bad CRC");
  102. return 0;
  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 1;
  109. }
  110. pr_err("Cannot import environment: errno = %d\n", errno);
  111. set_default_env("!import failed");
  112. return 0;
  113. }
  114. #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
  115. static unsigned char env_flags;
  116. int env_import_redund(const char *buf1, const char *buf2)
  117. {
  118. int crc1_ok, crc2_ok;
  119. env_t *ep, *tmp_env1, *tmp_env2;
  120. tmp_env1 = (env_t *)buf1;
  121. tmp_env2 = (env_t *)buf2;
  122. crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) ==
  123. tmp_env1->crc;
  124. crc2_ok = crc32(0, tmp_env2->data, ENV_SIZE) ==
  125. tmp_env2->crc;
  126. if (!crc1_ok && !crc2_ok) {
  127. set_default_env("!bad CRC");
  128. return 0;
  129. } else if (crc1_ok && !crc2_ok) {
  130. gd->env_valid = ENV_VALID;
  131. } else if (!crc1_ok && crc2_ok) {
  132. gd->env_valid = ENV_REDUND;
  133. } else {
  134. /* both ok - check serial */
  135. if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
  136. gd->env_valid = ENV_REDUND;
  137. else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
  138. gd->env_valid = ENV_VALID;
  139. else if (tmp_env1->flags > tmp_env2->flags)
  140. gd->env_valid = ENV_VALID;
  141. else if (tmp_env2->flags > tmp_env1->flags)
  142. gd->env_valid = ENV_REDUND;
  143. else /* flags are equal - almost impossible */
  144. gd->env_valid = ENV_VALID;
  145. }
  146. if (gd->env_valid == ENV_VALID)
  147. ep = tmp_env1;
  148. else
  149. ep = tmp_env2;
  150. env_flags = ep->flags;
  151. return env_import((char *)ep, 0);
  152. }
  153. #endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */
  154. /* Export the environment and generate CRC for it. */
  155. int env_export(env_t *env_out)
  156. {
  157. char *res;
  158. ssize_t len;
  159. res = (char *)env_out->data;
  160. len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
  161. if (len < 0) {
  162. pr_err("Cannot export environment: errno = %d\n", errno);
  163. return 1;
  164. }
  165. env_out->crc = crc32(0, env_out->data, ENV_SIZE);
  166. #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
  167. env_out->flags = ++env_flags; /* increase the serial */
  168. #endif
  169. return 0;
  170. }
  171. void env_relocate(void)
  172. {
  173. #if defined(CONFIG_NEEDS_MANUAL_RELOC)
  174. env_reloc();
  175. env_htab.change_ok += gd->reloc_off;
  176. #endif
  177. if (gd->env_valid == ENV_INVALID) {
  178. #if defined(CONFIG_ENV_IS_NOWHERE) || defined(CONFIG_SPL_BUILD)
  179. /* Environment not changable */
  180. set_default_env(NULL);
  181. #else
  182. bootstage_error(BOOTSTAGE_ID_NET_CHECKSUM);
  183. set_default_env("!bad CRC");
  184. #endif
  185. } else {
  186. env_load();
  187. }
  188. }
  189. #if defined(CONFIG_AUTO_COMPLETE) && !defined(CONFIG_SPL_BUILD)
  190. int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf)
  191. {
  192. ENTRY *match;
  193. int found, idx;
  194. idx = 0;
  195. found = 0;
  196. cmdv[0] = NULL;
  197. while ((idx = hmatch_r(var, idx, &match, &env_htab))) {
  198. int vallen = strlen(match->key) + 1;
  199. if (found >= maxv - 2 || bufsz < vallen)
  200. break;
  201. cmdv[found++] = buf;
  202. memcpy(buf, match->key, vallen);
  203. buf += vallen;
  204. bufsz -= vallen;
  205. }
  206. qsort(cmdv, found, sizeof(cmdv[0]), strcmp_compar);
  207. if (idx)
  208. cmdv[found++] = "...";
  209. cmdv[found] = NULL;
  210. return found;
  211. }
  212. #endif