env_common.c 5.0 KB

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