env_common.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. * See file CREDITS for list of people who contributed to this
  9. * project.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation; either version 2 of
  14. * the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  24. * MA 02111-1307 USA
  25. */
  26. #include <common.h>
  27. #include <command.h>
  28. #include <environment.h>
  29. #include <linux/stddef.h>
  30. #include <search.h>
  31. #include <errno.h>
  32. #include <malloc.h>
  33. DECLARE_GLOBAL_DATA_PTR;
  34. /************************************************************************
  35. * Default settings to be used when no valid environment is found
  36. */
  37. #include <env_default.h>
  38. struct hsearch_data env_htab = {
  39. .change_ok = env_flags_validate,
  40. };
  41. static uchar __env_get_char_spec(int index)
  42. {
  43. return *((uchar *)(gd->env_addr + index));
  44. }
  45. uchar env_get_char_spec(int)
  46. __attribute__((weak, alias("__env_get_char_spec")));
  47. static uchar env_get_char_init(int index)
  48. {
  49. /* if crc was bad, use the default environment */
  50. if (gd->env_valid)
  51. return env_get_char_spec(index);
  52. else
  53. return default_environment[index];
  54. }
  55. uchar env_get_char_memory(int index)
  56. {
  57. return *env_get_addr(index);
  58. }
  59. uchar env_get_char(int index)
  60. {
  61. /* if relocated to RAM */
  62. if (gd->flags & GD_FLG_RELOC)
  63. return env_get_char_memory(index);
  64. else
  65. return env_get_char_init(index);
  66. }
  67. const uchar *env_get_addr(int index)
  68. {
  69. if (gd->env_valid)
  70. return (uchar *)(gd->env_addr + index);
  71. else
  72. return &default_environment[index];
  73. }
  74. /*
  75. * Read an environment variable as a boolean
  76. * Return -1 if variable does not exist (default to true)
  77. */
  78. int getenv_yesno(const char *var)
  79. {
  80. char *s = getenv(var);
  81. if (s == NULL)
  82. return -1;
  83. return (*s == '1' || *s == 'y' || *s == 'Y' || *s == 't' || *s == 'T') ?
  84. 1 : 0;
  85. }
  86. /*
  87. * Look up the variable from the default environment
  88. */
  89. char *getenv_default(const char *name)
  90. {
  91. char *ret_val;
  92. unsigned long really_valid = gd->env_valid;
  93. unsigned long real_gd_flags = gd->flags;
  94. /* Pretend that the image is bad. */
  95. gd->flags &= ~GD_FLG_ENV_READY;
  96. gd->env_valid = 0;
  97. ret_val = getenv(name);
  98. gd->env_valid = really_valid;
  99. gd->flags = real_gd_flags;
  100. return ret_val;
  101. }
  102. void set_default_env(const char *s)
  103. {
  104. int flags = 0;
  105. if (sizeof(default_environment) > ENV_SIZE) {
  106. puts("*** Error - default environment is too large\n\n");
  107. return;
  108. }
  109. if (s) {
  110. if (*s == '!') {
  111. printf("*** Warning - %s, "
  112. "using default environment\n\n",
  113. s + 1);
  114. } else {
  115. flags = H_INTERACTIVE;
  116. puts(s);
  117. }
  118. } else {
  119. puts("Using default environment\n\n");
  120. }
  121. if (himport_r(&env_htab, (char *)default_environment,
  122. sizeof(default_environment), '\0', flags,
  123. 0, NULL) == 0)
  124. error("Environment import failed: errno = %d\n", errno);
  125. gd->flags |= GD_FLG_ENV_READY;
  126. }
  127. /* [re]set individual variables to their value in the default environment */
  128. int set_default_vars(int nvars, char * const vars[])
  129. {
  130. /*
  131. * Special use-case: import from default environment
  132. * (and use \0 as a separator)
  133. */
  134. return himport_r(&env_htab, (const char *)default_environment,
  135. sizeof(default_environment), '\0',
  136. H_NOCLEAR | H_INTERACTIVE, nvars, vars);
  137. }
  138. #ifndef CONFIG_SPL_BUILD
  139. /*
  140. * Check if CRC is valid and (if yes) import the environment.
  141. * Note that "buf" may or may not be aligned.
  142. */
  143. int env_import(const char *buf, int check)
  144. {
  145. env_t *ep = (env_t *)buf;
  146. if (check) {
  147. uint32_t crc;
  148. memcpy(&crc, &ep->crc, sizeof(crc));
  149. if (crc32(0, ep->data, ENV_SIZE) != crc) {
  150. set_default_env("!bad CRC");
  151. return 0;
  152. }
  153. }
  154. if (himport_r(&env_htab, (char *)ep->data, ENV_SIZE, '\0', 0,
  155. 0, NULL)) {
  156. gd->flags |= GD_FLG_ENV_READY;
  157. return 1;
  158. }
  159. error("Cannot import environment: errno = %d\n", errno);
  160. set_default_env("!import failed");
  161. return 0;
  162. }
  163. #endif
  164. void env_relocate(void)
  165. {
  166. #if defined(CONFIG_NEEDS_MANUAL_RELOC)
  167. env_reloc();
  168. env_htab.change_ok += gd->reloc_off;
  169. #endif
  170. if (gd->env_valid == 0) {
  171. #if defined(CONFIG_ENV_IS_NOWHERE) || defined(CONFIG_SPL_BUILD)
  172. /* Environment not changable */
  173. set_default_env(NULL);
  174. #else
  175. bootstage_error(BOOTSTAGE_ID_NET_CHECKSUM);
  176. set_default_env("!bad CRC");
  177. #endif
  178. } else {
  179. env_relocate_spec();
  180. }
  181. }
  182. #if defined(CONFIG_AUTO_COMPLETE) && !defined(CONFIG_SPL_BUILD)
  183. int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf)
  184. {
  185. ENTRY *match;
  186. int found, idx;
  187. idx = 0;
  188. found = 0;
  189. cmdv[0] = NULL;
  190. while ((idx = hmatch_r(var, idx, &match, &env_htab))) {
  191. int vallen = strlen(match->key) + 1;
  192. if (found >= maxv - 2 || bufsz < vallen)
  193. break;
  194. cmdv[found++] = buf;
  195. memcpy(buf, match->key, vallen);
  196. buf += vallen;
  197. bufsz -= vallen;
  198. }
  199. qsort(cmdv, found, sizeof(cmdv[0]), strcmp_compar);
  200. if (idx)
  201. cmdv[found++] = "...";
  202. cmdv[found] = NULL;
  203. return found;
  204. }
  205. #endif