env_common.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * (C) Copyright 2000-2002
  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. * See file CREDITS for list of people who contributed to this
  8. * project.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation; either version 2 of
  13. * the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  23. * MA 02111-1307 USA
  24. */
  25. #include <common.h>
  26. #include <command.h>
  27. #include <environment.h>
  28. #include <cmd_nvedit.h>
  29. #include <linux/stddef.h>
  30. #include <malloc.h>
  31. #ifdef CONFIG_SHOW_BOOT_PROGRESS
  32. # include <status_led.h>
  33. # define SHOW_BOOT_PROGRESS(arg) show_boot_progress(arg)
  34. #else
  35. # define SHOW_BOOT_PROGRESS(arg)
  36. #endif
  37. #undef DEBUG_ENV
  38. #ifdef DEBUG_ENV
  39. #define DEBUGF(fmt,args...) printf(fmt ,##args)
  40. #else
  41. #define DEBUGF(fmt,args...)
  42. #endif
  43. extern env_t *env_ptr;
  44. extern void env_relocate_spec (void);
  45. extern uchar env_get_char_spec(int);
  46. static uchar env_get_char_init (int index);
  47. uchar (*env_get_char)(int) = env_get_char_init;
  48. /************************************************************************
  49. * Default settings to be used when no valid environment is found
  50. */
  51. #define XMK_STR(x) #x
  52. #define MK_STR(x) XMK_STR(x)
  53. uchar default_environment[] = {
  54. #ifdef CONFIG_BOOTARGS
  55. "bootargs=" CONFIG_BOOTARGS "\0"
  56. #endif
  57. #ifdef CONFIG_BOOTCOMMAND
  58. "bootcmd=" CONFIG_BOOTCOMMAND "\0"
  59. #endif
  60. #ifdef CONFIG_RAMBOOTCOMMAND
  61. "ramboot=" CONFIG_RAMBOOTCOMMAND "\0"
  62. #endif
  63. #ifdef CONFIG_NFSBOOTCOMMAND
  64. "nfsboot=" CONFIG_NFSBOOTCOMMAND "\0"
  65. #endif
  66. #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
  67. "bootdelay=" MK_STR(CONFIG_BOOTDELAY) "\0"
  68. #endif
  69. #if defined(CONFIG_BAUDRATE) && (CONFIG_BAUDRATE >= 0)
  70. "baudrate=" MK_STR(CONFIG_BAUDRATE) "\0"
  71. #endif
  72. #ifdef CONFIG_LOADS_ECHO
  73. "loads_echo=" MK_STR(CONFIG_LOADS_ECHO) "\0"
  74. #endif
  75. #ifdef CONFIG_ETHADDR
  76. "ethaddr=" MK_STR(CONFIG_ETHADDR) "\0"
  77. #endif
  78. #ifdef CONFIG_ETH1ADDR
  79. "eth1addr=" MK_STR(CONFIG_ETH1ADDR) "\0"
  80. #endif
  81. #ifdef CONFIG_ETH2ADDR
  82. "eth2addr=" MK_STR(CONFIG_ETH2ADDR) "\0"
  83. #endif
  84. #ifdef CONFIG_IPADDR
  85. "ipaddr=" MK_STR(CONFIG_IPADDR) "\0"
  86. #endif
  87. #ifdef CONFIG_SERVERIP
  88. "serverip=" MK_STR(CONFIG_SERVERIP) "\0"
  89. #endif
  90. #ifdef CFG_AUTOLOAD
  91. "autoload=" CFG_AUTOLOAD "\0"
  92. #endif
  93. #ifdef CONFIG_PREBOOT
  94. "preboot=" CONFIG_PREBOOT "\0"
  95. #endif
  96. #ifdef CONFIG_ROOTPATH
  97. "rootpath=" MK_STR(CONFIG_ROOTPATH) "\0"
  98. #endif
  99. #ifdef CONFIG_GATEWAYIP
  100. "gatewayip=" MK_STR(CONFIG_GATEWAYIP) "\0"
  101. #endif
  102. #ifdef CONFIG_NETMASK
  103. "netmask=" MK_STR(CONFIG_NETMASK) "\0"
  104. #endif
  105. #ifdef CONFIG_HOSTNAME
  106. "hostname=" MK_STR(CONFIG_HOSTNAME) "\0"
  107. #endif
  108. #ifdef CONFIG_BOOTFILE
  109. "bootfile=" MK_STR(CONFIG_BOOTFILE) "\0"
  110. #endif
  111. #ifdef CONFIG_LOADADDR
  112. "loadaddr=" MK_STR(CONFIG_LOADADDR) "\0"
  113. #endif
  114. #ifdef CONFIG_CLOCKS_IN_MHZ
  115. "clocks_in_mhz=1\0"
  116. #endif
  117. #ifdef CONFIG_EXTRA_ENV_SETTINGS
  118. CONFIG_EXTRA_ENV_SETTINGS
  119. #endif
  120. "\0"
  121. };
  122. void env_crc_update (void)
  123. {
  124. env_ptr->crc = crc32(0, env_ptr->data, ENV_SIZE);
  125. }
  126. static uchar env_get_char_init (int index)
  127. {
  128. DECLARE_GLOBAL_DATA_PTR;
  129. uchar c;
  130. /* if crc was bad, use the default environment */
  131. if (gd->env_valid)
  132. {
  133. c = env_get_char_spec(index);
  134. } else {
  135. c = default_environment[index];
  136. }
  137. return (c);
  138. }
  139. uchar env_get_char_memory (int index)
  140. {
  141. DECLARE_GLOBAL_DATA_PTR;
  142. if (gd->env_valid) {
  143. return ( *((uchar *)(gd->env_addr + index)) );
  144. } else {
  145. return ( default_environment[index] );
  146. }
  147. }
  148. uchar *env_get_addr (int index)
  149. {
  150. DECLARE_GLOBAL_DATA_PTR;
  151. if (gd->env_valid) {
  152. return ( ((uchar *)(gd->env_addr + index)) );
  153. } else {
  154. return (&default_environment[index]);
  155. }
  156. }
  157. void env_relocate (void)
  158. {
  159. DECLARE_GLOBAL_DATA_PTR;
  160. DEBUGF ("%s[%d] offset = 0x%lx\n", __FUNCTION__,__LINE__,
  161. gd->reloc_off);
  162. #ifdef ENV_IS_EMBEDDED
  163. /*
  164. * The environment buffer is embedded with the text segment,
  165. * just relocate the environment pointer
  166. */
  167. env_ptr = (env_t *)((ulong)env_ptr + gd->reloc_off);
  168. DEBUGF ("%s[%d] embedded ENV at %p\n", __FUNCTION__,__LINE__,env_ptr);
  169. #else
  170. /*
  171. * We must allocate a buffer for the environment
  172. */
  173. env_ptr = (env_t *)malloc (CFG_ENV_SIZE);
  174. DEBUGF ("%s[%d] malloced ENV at %p\n", __FUNCTION__,__LINE__,env_ptr);
  175. #endif
  176. /*
  177. * After relocation to RAM, we can always use the "memory" functions
  178. */
  179. env_get_char = env_get_char_memory;
  180. if (gd->env_valid == 0) {
  181. #if defined(CONFIG_GTH) || defined(CFG_ENV_IS_NOWHERE) /* Environment not changable */
  182. puts ("Using default environment\n\n");
  183. #else
  184. puts ("*** Warning - bad CRC, using default environment\n\n");
  185. SHOW_BOOT_PROGRESS (-1);
  186. #endif
  187. if (sizeof(default_environment) > ENV_SIZE)
  188. {
  189. puts ("*** Error - default environment is too large\n\n");
  190. return;
  191. }
  192. memset (env_ptr, 0, sizeof(env_t));
  193. memcpy (env_ptr->data,
  194. default_environment,
  195. sizeof(default_environment));
  196. #ifdef CFG_REDUNDAND_ENVIRONMENT
  197. env_ptr->flags = 0xFF;
  198. #endif
  199. env_crc_update ();
  200. gd->env_valid = 1;
  201. }
  202. else {
  203. env_relocate_spec ();
  204. }
  205. gd->env_addr = (ulong)&(env_ptr->data);
  206. }