env_nand.c 8.9 KB

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