env_nand.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. * (C) Copyright 2004
  3. * Jian Zhang, Texas Instruments, jzhang@ti.com.
  4. * (C) Copyright 2000-2006
  5. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  6. *
  7. * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  8. * Andreas Heppel <aheppel@sysgo.de>
  9. * See file CREDITS for list of people who contributed to this
  10. * project.
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License as
  14. * published by the Free Software Foundation; either version 2 of
  15. * the License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  25. * MA 02111-1307 USA
  26. */
  27. /* #define DEBUG */
  28. #include <common.h>
  29. #if defined(CFG_ENV_IS_IN_NAND) /* Environment is in Nand Flash */
  30. #include <command.h>
  31. #include <environment.h>
  32. #include <linux/stddef.h>
  33. #include <malloc.h>
  34. #include <nand.h>
  35. #if ((CONFIG_COMMANDS&(CFG_CMD_ENV|CFG_CMD_NAND)) == (CFG_CMD_ENV|CFG_CMD_NAND))
  36. #define CMD_SAVEENV
  37. #elif defined(CFG_ENV_OFFSET_REDUND)
  38. #error Cannot use CFG_ENV_OFFSET_REDUND without CFG_CMD_ENV & CFG_CMD_NAND
  39. #endif
  40. #if defined(CFG_ENV_SIZE_REDUND) && (CFG_ENV_SIZE_REDUND != CFG_ENV_SIZE)
  41. #error CFG_ENV_SIZE_REDUND should be the same as CFG_ENV_SIZE
  42. #endif
  43. #ifdef CONFIG_INFERNO
  44. #error CONFIG_INFERNO not supported yet
  45. #endif
  46. int nand_legacy_rw (struct nand_chip* nand, int cmd,
  47. size_t start, size_t len,
  48. size_t * retlen, u_char * buf);
  49. /* info for NAND chips, defined in drivers/nand/nand.c */
  50. extern nand_info_t nand_info[];
  51. /* references to names in env_common.c */
  52. extern uchar default_environment[];
  53. extern int default_environment_size;
  54. char * env_name_spec = "NAND";
  55. #ifdef ENV_IS_EMBEDDED
  56. extern uchar environment[];
  57. env_t *env_ptr = (env_t *)(&environment[0]);
  58. #else /* ! ENV_IS_EMBEDDED */
  59. env_t *env_ptr = 0;
  60. #endif /* ENV_IS_EMBEDDED */
  61. /* local functions */
  62. #if !defined(ENV_IS_EMBEDDED)
  63. static void use_default(void);
  64. #endif
  65. DECLARE_GLOBAL_DATA_PTR;
  66. uchar env_get_char_spec (int index)
  67. {
  68. return ( *((uchar *)(gd->env_addr + index)) );
  69. }
  70. /* this is called before nand_init()
  71. * so we can't read Nand to validate env data.
  72. * Mark it OK for now. env_relocate() in env_common.c
  73. * will call our relocate function which will does
  74. * the real validation.
  75. *
  76. * When using a NAND boot image (like sequoia_nand), the environment
  77. * can be embedded or attached to the U-Boot image in NAND flash. This way
  78. * the SPL loads not only the U-Boot image from NAND but also the
  79. * environment.
  80. */
  81. int env_init(void)
  82. {
  83. #if defined(ENV_IS_EMBEDDED)
  84. ulong total;
  85. int crc1_ok = 0, crc2_ok = 0;
  86. env_t *tmp_env1, *tmp_env2;
  87. total = CFG_ENV_SIZE;
  88. tmp_env1 = env_ptr;
  89. tmp_env2 = (env_t *)((ulong)env_ptr + CFG_ENV_SIZE);
  90. crc1_ok = (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc);
  91. crc2_ok = (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc);
  92. if (!crc1_ok && !crc2_ok)
  93. gd->env_valid = 0;
  94. else if(crc1_ok && !crc2_ok)
  95. gd->env_valid = 1;
  96. else if(!crc1_ok && crc2_ok)
  97. gd->env_valid = 2;
  98. else {
  99. /* both ok - check serial */
  100. if(tmp_env1->flags == 255 && tmp_env2->flags == 0)
  101. gd->env_valid = 2;
  102. else if(tmp_env2->flags == 255 && tmp_env1->flags == 0)
  103. gd->env_valid = 1;
  104. else if(tmp_env1->flags > tmp_env2->flags)
  105. gd->env_valid = 1;
  106. else if(tmp_env2->flags > tmp_env1->flags)
  107. gd->env_valid = 2;
  108. else /* flags are equal - almost impossible */
  109. gd->env_valid = 1;
  110. }
  111. if (gd->env_valid == 1)
  112. env_ptr = tmp_env1;
  113. else if (gd->env_valid == 2)
  114. env_ptr = tmp_env2;
  115. #else /* ENV_IS_EMBEDDED */
  116. gd->env_addr = (ulong)&default_environment[0];
  117. gd->env_valid = 1;
  118. #endif /* ENV_IS_EMBEDDED */
  119. return (0);
  120. }
  121. #ifdef CMD_SAVEENV
  122. /*
  123. * The legacy NAND code saved the environment in the first NAND device i.e.,
  124. * nand_dev_desc + 0. This is also the behaviour using the new NAND code.
  125. */
  126. #ifdef CFG_ENV_OFFSET_REDUND
  127. int saveenv(void)
  128. {
  129. ulong total;
  130. int ret = 0;
  131. env_ptr->flags++;
  132. total = CFG_ENV_SIZE;
  133. if(gd->env_valid == 1) {
  134. puts ("Erasing redundant Nand...");
  135. if (nand_erase(&nand_info[0],
  136. CFG_ENV_OFFSET_REDUND, CFG_ENV_SIZE))
  137. return 1;
  138. puts ("Writing to redundant Nand... ");
  139. ret = nand_write(&nand_info[0], CFG_ENV_OFFSET_REDUND, &total,
  140. (u_char*) env_ptr);
  141. } else {
  142. puts ("Erasing Nand...");
  143. if (nand_erase(&nand_info[0],
  144. CFG_ENV_OFFSET, CFG_ENV_SIZE))
  145. return 1;
  146. puts ("Writing to Nand... ");
  147. ret = nand_write(&nand_info[0], CFG_ENV_OFFSET, &total,
  148. (u_char*) env_ptr);
  149. }
  150. if (ret || total != CFG_ENV_SIZE)
  151. return 1;
  152. puts ("done\n");
  153. gd->env_valid = (gd->env_valid == 2 ? 1 : 2);
  154. return ret;
  155. }
  156. #else /* ! CFG_ENV_OFFSET_REDUND */
  157. int saveenv(void)
  158. {
  159. ulong total;
  160. int ret = 0;
  161. puts ("Erasing Nand...");
  162. if (nand_erase(&nand_info[0], CFG_ENV_OFFSET, CFG_ENV_SIZE))
  163. return 1;
  164. puts ("Writing to Nand... ");
  165. total = CFG_ENV_SIZE;
  166. ret = nand_write(&nand_info[0], CFG_ENV_OFFSET, &total, (u_char*)env_ptr);
  167. if (ret || total != CFG_ENV_SIZE)
  168. return 1;
  169. puts ("done\n");
  170. return ret;
  171. }
  172. #endif /* CFG_ENV_OFFSET_REDUND */
  173. #endif /* CMD_SAVEENV */
  174. #ifdef CFG_ENV_OFFSET_REDUND
  175. void env_relocate_spec (void)
  176. {
  177. #if !defined(ENV_IS_EMBEDDED)
  178. ulong total;
  179. int crc1_ok = 0, crc2_ok = 0;
  180. env_t *tmp_env1, *tmp_env2;
  181. total = CFG_ENV_SIZE;
  182. tmp_env1 = (env_t *) malloc(CFG_ENV_SIZE);
  183. tmp_env2 = (env_t *) malloc(CFG_ENV_SIZE);
  184. nand_read(&nand_info[0], CFG_ENV_OFFSET, &total,
  185. (u_char*) tmp_env1);
  186. nand_read(&nand_info[0], CFG_ENV_OFFSET_REDUND, &total,
  187. (u_char*) tmp_env2);
  188. crc1_ok = (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc);
  189. crc2_ok = (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc);
  190. if(!crc1_ok && !crc2_ok)
  191. return use_default();
  192. else if(crc1_ok && !crc2_ok)
  193. gd->env_valid = 1;
  194. else if(!crc1_ok && crc2_ok)
  195. gd->env_valid = 2;
  196. else {
  197. /* both ok - check serial */
  198. if(tmp_env1->flags == 255 && tmp_env2->flags == 0)
  199. gd->env_valid = 2;
  200. else if(tmp_env2->flags == 255 && tmp_env1->flags == 0)
  201. gd->env_valid = 1;
  202. else if(tmp_env1->flags > tmp_env2->flags)
  203. gd->env_valid = 1;
  204. else if(tmp_env2->flags > tmp_env1->flags)
  205. gd->env_valid = 2;
  206. else /* flags are equal - almost impossible */
  207. gd->env_valid = 1;
  208. }
  209. free(env_ptr);
  210. if(gd->env_valid == 1) {
  211. env_ptr = tmp_env1;
  212. free(tmp_env2);
  213. } else {
  214. env_ptr = tmp_env2;
  215. free(tmp_env1);
  216. }
  217. #endif /* ! ENV_IS_EMBEDDED */
  218. }
  219. #else /* ! CFG_ENV_OFFSET_REDUND */
  220. /*
  221. * The legacy NAND code saved the environment in the first NAND device i.e.,
  222. * nand_dev_desc + 0. This is also the behaviour using the new NAND code.
  223. */
  224. void env_relocate_spec (void)
  225. {
  226. #if !defined(ENV_IS_EMBEDDED)
  227. ulong total;
  228. int ret;
  229. total = CFG_ENV_SIZE;
  230. ret = nand_read(&nand_info[0], CFG_ENV_OFFSET, &total, (u_char*)env_ptr);
  231. if (ret || total != CFG_ENV_SIZE)
  232. return use_default();
  233. if (crc32(0, env_ptr->data, ENV_SIZE) != env_ptr->crc)
  234. return use_default();
  235. #endif /* ! ENV_IS_EMBEDDED */
  236. }
  237. #endif /* CFG_ENV_OFFSET_REDUND */
  238. #if !defined(ENV_IS_EMBEDDED)
  239. static void use_default()
  240. {
  241. puts ("*** Warning - bad CRC or NAND, using default environment\n\n");
  242. if (default_environment_size > CFG_ENV_SIZE){
  243. puts ("*** Error - default environment is too large\n\n");
  244. return;
  245. }
  246. memset (env_ptr, 0, sizeof(env_t));
  247. memcpy (env_ptr->data,
  248. default_environment,
  249. default_environment_size);
  250. env_ptr->crc = crc32(0, env_ptr->data, ENV_SIZE);
  251. gd->env_valid = 1;
  252. }
  253. #endif
  254. #endif /* CFG_ENV_IS_IN_NAND */