env_common.c 4.7 KB

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