env_common.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. extern env_t *env_ptr;
  35. extern void env_relocate_spec (void);
  36. extern uchar env_get_char_spec(int);
  37. static uchar env_get_char_init (int index);
  38. /************************************************************************
  39. * Default settings to be used when no valid environment is found
  40. */
  41. #define XMK_STR(x) #x
  42. #define MK_STR(x) XMK_STR(x)
  43. const uchar default_environment[] = {
  44. #ifdef CONFIG_BOOTARGS
  45. "bootargs=" CONFIG_BOOTARGS "\0"
  46. #endif
  47. #ifdef CONFIG_BOOTCOMMAND
  48. "bootcmd=" CONFIG_BOOTCOMMAND "\0"
  49. #endif
  50. #ifdef CONFIG_RAMBOOTCOMMAND
  51. "ramboot=" CONFIG_RAMBOOTCOMMAND "\0"
  52. #endif
  53. #ifdef CONFIG_NFSBOOTCOMMAND
  54. "nfsboot=" CONFIG_NFSBOOTCOMMAND "\0"
  55. #endif
  56. #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
  57. "bootdelay=" MK_STR(CONFIG_BOOTDELAY) "\0"
  58. #endif
  59. #if defined(CONFIG_BAUDRATE) && (CONFIG_BAUDRATE >= 0)
  60. "baudrate=" MK_STR(CONFIG_BAUDRATE) "\0"
  61. #endif
  62. #ifdef CONFIG_LOADS_ECHO
  63. "loads_echo=" MK_STR(CONFIG_LOADS_ECHO) "\0"
  64. #endif
  65. #ifdef CONFIG_ETHADDR
  66. "ethaddr=" MK_STR(CONFIG_ETHADDR) "\0"
  67. #endif
  68. #ifdef CONFIG_ETH1ADDR
  69. "eth1addr=" MK_STR(CONFIG_ETH1ADDR) "\0"
  70. #endif
  71. #ifdef CONFIG_ETH2ADDR
  72. "eth2addr=" MK_STR(CONFIG_ETH2ADDR) "\0"
  73. #endif
  74. #ifdef CONFIG_ETH3ADDR
  75. "eth3addr=" MK_STR(CONFIG_ETH3ADDR) "\0"
  76. #endif
  77. #ifdef CONFIG_ETH4ADDR
  78. "eth4addr=" MK_STR(CONFIG_ETH4ADDR) "\0"
  79. #endif
  80. #ifdef CONFIG_ETH5ADDR
  81. "eth5addr=" MK_STR(CONFIG_ETH5ADDR) "\0"
  82. #endif
  83. #ifdef CONFIG_IPADDR
  84. "ipaddr=" MK_STR(CONFIG_IPADDR) "\0"
  85. #endif
  86. #ifdef CONFIG_SERVERIP
  87. "serverip=" MK_STR(CONFIG_SERVERIP) "\0"
  88. #endif
  89. #ifdef CONFIG_SYS_AUTOLOAD
  90. "autoload=" CONFIG_SYS_AUTOLOAD "\0"
  91. #endif
  92. #ifdef CONFIG_PREBOOT
  93. "preboot=" CONFIG_PREBOOT "\0"
  94. #endif
  95. #ifdef CONFIG_ROOTPATH
  96. "rootpath=" CONFIG_ROOTPATH "\0"
  97. #endif
  98. #ifdef CONFIG_GATEWAYIP
  99. "gatewayip=" MK_STR(CONFIG_GATEWAYIP) "\0"
  100. #endif
  101. #ifdef CONFIG_NETMASK
  102. "netmask=" MK_STR(CONFIG_NETMASK) "\0"
  103. #endif
  104. #ifdef CONFIG_HOSTNAME
  105. "hostname=" MK_STR(CONFIG_HOSTNAME) "\0"
  106. #endif
  107. #ifdef CONFIG_BOOTFILE
  108. "bootfile=" MK_STR(CONFIG_BOOTFILE) "\0"
  109. #endif
  110. #ifdef CONFIG_LOADADDR
  111. "loadaddr=" MK_STR(CONFIG_LOADADDR) "\0"
  112. #endif
  113. #ifdef CONFIG_CLOCKS_IN_MHZ
  114. "clocks_in_mhz=1\0"
  115. #endif
  116. #if defined(CONFIG_PCI_BOOTDELAY) && (CONFIG_PCI_BOOTDELAY > 0)
  117. "pcidelay=" MK_STR(CONFIG_PCI_BOOTDELAY) "\0"
  118. #endif
  119. #ifdef CONFIG_EXTRA_ENV_SETTINGS
  120. CONFIG_EXTRA_ENV_SETTINGS
  121. #endif
  122. "\0"
  123. };
  124. struct hsearch_data env_htab;
  125. static uchar env_get_char_init (int index)
  126. {
  127. uchar c;
  128. /* if crc was bad, use the default environment */
  129. if (gd->env_valid)
  130. c = env_get_char_spec(index);
  131. else
  132. c = default_environment[index];
  133. return (c);
  134. }
  135. uchar env_get_char_memory (int index)
  136. {
  137. return *env_get_addr(index);
  138. }
  139. uchar env_get_char (int index)
  140. {
  141. uchar c;
  142. /* if relocated to RAM */
  143. if (gd->flags & GD_FLG_RELOC)
  144. c = env_get_char_memory(index);
  145. else
  146. c = env_get_char_init(index);
  147. return (c);
  148. }
  149. const uchar *env_get_addr (int index)
  150. {
  151. if (gd->env_valid)
  152. return (uchar *)(gd->env_addr + index);
  153. else
  154. return &default_environment[index];
  155. }
  156. void set_default_env(const char *s)
  157. {
  158. if (sizeof(default_environment) > ENV_SIZE) {
  159. puts("*** Error - default environment is too large\n\n");
  160. return;
  161. }
  162. if (s) {
  163. if (*s == '!') {
  164. printf("*** Warning - %s, "
  165. "using default environment\n\n",
  166. s+1);
  167. } else {
  168. puts(s);
  169. }
  170. } else {
  171. puts("Using default environment\n\n");
  172. }
  173. if (himport_r(&env_htab, (char *)default_environment,
  174. sizeof(default_environment), '\0', 0) == 0) {
  175. error("Environment import failed: errno = %d\n", errno);
  176. }
  177. gd->flags |= GD_FLG_ENV_READY;
  178. }
  179. /*
  180. * Check if CRC is valid and (if yes) import the environment.
  181. * Note that "buf" may or may not be aligned.
  182. */
  183. int env_import(const char *buf, int check)
  184. {
  185. env_t *ep = (env_t *)buf;
  186. if (check) {
  187. uint32_t crc;
  188. memcpy(&crc, &ep->crc, sizeof(crc));
  189. if (crc32(0, ep->data, ENV_SIZE) != crc) {
  190. set_default_env("!bad CRC");
  191. return 0;
  192. }
  193. }
  194. if (himport_r(&env_htab, (char *)ep->data, ENV_SIZE, '\0', 0)) {
  195. gd->flags |= GD_FLG_ENV_READY;
  196. return 1;
  197. }
  198. error("Cannot import environment: errno = %d\n", errno);
  199. set_default_env("!import failed");
  200. return 0;
  201. }
  202. void env_relocate (void)
  203. {
  204. #if defined(CONFIG_NEEDS_MANUAL_RELOC)
  205. extern void env_reloc(void);
  206. env_reloc();
  207. #endif
  208. if (gd->env_valid == 0) {
  209. #if defined(CONFIG_ENV_IS_NOWHERE) /* Environment not changable */
  210. set_default_env(NULL);
  211. #else
  212. show_boot_progress (-60);
  213. set_default_env("!bad CRC");
  214. #endif
  215. } else {
  216. env_relocate_spec ();
  217. }
  218. }
  219. #ifdef CONFIG_AUTO_COMPLETE
  220. int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf)
  221. {
  222. ENTRY *match;
  223. int found, idx;
  224. idx = 0;
  225. found = 0;
  226. cmdv[0] = NULL;
  227. while ((idx = hmatch_r(var, idx, &match, &env_htab))) {
  228. int vallen = strlen(match->key) + 1;
  229. if (found >= maxv - 2 || bufsz < vallen)
  230. break;
  231. cmdv[found++] = buf;
  232. memcpy(buf, match->key, vallen);
  233. buf += vallen;
  234. bufsz -= vallen;
  235. }
  236. qsort(cmdv, found, sizeof(cmdv[0]), strcmp_compar);
  237. if (idx)
  238. cmdv[found++] = "...";
  239. cmdv[found] = NULL;
  240. return found;
  241. }
  242. #endif