env_common.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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_change_ok,
  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. void set_default_env(const char *s)
  87. {
  88. int flags = 0;
  89. if (sizeof(default_environment) > ENV_SIZE) {
  90. puts("*** Error - default environment is too large\n\n");
  91. return;
  92. }
  93. if (s) {
  94. if (*s == '!') {
  95. printf("*** Warning - %s, "
  96. "using default environment\n\n",
  97. s + 1);
  98. } else {
  99. flags = H_INTERACTIVE;
  100. puts(s);
  101. }
  102. } else {
  103. puts("Using default environment\n\n");
  104. }
  105. if (himport_r(&env_htab, (char *)default_environment,
  106. sizeof(default_environment), '\0', flags,
  107. 0, NULL) == 0)
  108. error("Environment import failed: errno = %d\n", errno);
  109. gd->flags |= GD_FLG_ENV_READY;
  110. }
  111. /* [re]set individual variables to their value in the default environment */
  112. int set_default_vars(int nvars, char * const vars[])
  113. {
  114. /*
  115. * Special use-case: import from default environment
  116. * (and use \0 as a separator)
  117. */
  118. return himport_r(&env_htab, (const char *)default_environment,
  119. sizeof(default_environment), '\0',
  120. H_NOCLEAR | H_INTERACTIVE, nvars, vars);
  121. }
  122. #ifndef CONFIG_SPL_BUILD
  123. /*
  124. * Check if CRC is valid and (if yes) import the environment.
  125. * Note that "buf" may or may not be aligned.
  126. */
  127. int env_import(const char *buf, int check)
  128. {
  129. env_t *ep = (env_t *)buf;
  130. if (check) {
  131. uint32_t crc;
  132. memcpy(&crc, &ep->crc, sizeof(crc));
  133. if (crc32(0, ep->data, ENV_SIZE) != crc) {
  134. set_default_env("!bad CRC");
  135. return 0;
  136. }
  137. }
  138. if (himport_r(&env_htab, (char *)ep->data, ENV_SIZE, '\0', 0,
  139. 0, NULL)) {
  140. gd->flags |= GD_FLG_ENV_READY;
  141. return 1;
  142. }
  143. error("Cannot import environment: errno = %d\n", errno);
  144. set_default_env("!import failed");
  145. return 0;
  146. }
  147. #endif
  148. void env_relocate(void)
  149. {
  150. #if defined(CONFIG_NEEDS_MANUAL_RELOC)
  151. env_reloc();
  152. env_htab.change_ok += gd->reloc_off;
  153. #endif
  154. if (gd->env_valid == 0) {
  155. #if defined(CONFIG_ENV_IS_NOWHERE) || defined(CONFIG_SPL_BUILD)
  156. /* Environment not changable */
  157. set_default_env(NULL);
  158. #else
  159. bootstage_error(BOOTSTAGE_ID_NET_CHECKSUM);
  160. set_default_env("!bad CRC");
  161. #endif
  162. } else {
  163. env_relocate_spec();
  164. }
  165. }
  166. #if defined(CONFIG_AUTO_COMPLETE) && !defined(CONFIG_SPL_BUILD)
  167. int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf)
  168. {
  169. ENTRY *match;
  170. int found, idx;
  171. idx = 0;
  172. found = 0;
  173. cmdv[0] = NULL;
  174. while ((idx = hmatch_r(var, idx, &match, &env_htab))) {
  175. int vallen = strlen(match->key) + 1;
  176. if (found >= maxv - 2 || bufsz < vallen)
  177. break;
  178. cmdv[found++] = buf;
  179. memcpy(buf, match->key, vallen);
  180. buf += vallen;
  181. bufsz -= vallen;
  182. }
  183. qsort(cmdv, found, sizeof(cmdv[0]), strcmp_compar);
  184. if (idx)
  185. cmdv[found++] = "...";
  186. cmdv[found] = NULL;
  187. return found;
  188. }
  189. #endif