env_nand.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. #if defined(ENV_IS_EMBEDDED)
  59. extern uchar environment[];
  60. env_t *env_ptr = (env_t *)(&environment[0]);
  61. #elif defined(CONFIG_NAND_ENV_DST)
  62. env_t *env_ptr = (env_t *)CONFIG_NAND_ENV_DST;
  63. #else /* ! ENV_IS_EMBEDDED */
  64. env_t *env_ptr = 0;
  65. #endif /* ENV_IS_EMBEDDED */
  66. /* local functions */
  67. #if !defined(ENV_IS_EMBEDDED)
  68. static void use_default(void);
  69. #endif
  70. DECLARE_GLOBAL_DATA_PTR;
  71. uchar env_get_char_spec (int index)
  72. {
  73. return ( *((uchar *)(gd->env_addr + index)) );
  74. }
  75. /* this is called before nand_init()
  76. * so we can't read Nand to validate env data.
  77. * Mark it OK for now. env_relocate() in env_common.c
  78. * will call our relocate function which does the real
  79. * validation.
  80. *
  81. * When using a NAND boot image (like sequoia_nand), the environment
  82. * can be embedded or attached to the U-Boot image in NAND flash. This way
  83. * the SPL loads not only the U-Boot image from NAND but also the
  84. * environment.
  85. */
  86. int env_init(void)
  87. {
  88. #if defined(ENV_IS_EMBEDDED) || defined(CONFIG_NAND_ENV_DST)
  89. int crc1_ok = 0, crc2_ok = 0;
  90. env_t *tmp_env1;
  91. #ifdef CONFIG_ENV_OFFSET_REDUND
  92. env_t *tmp_env2;
  93. tmp_env2 = (env_t *)((ulong)env_ptr + CONFIG_ENV_SIZE);
  94. crc2_ok = (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc);
  95. #endif
  96. tmp_env1 = env_ptr;
  97. crc1_ok = (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc);
  98. if (!crc1_ok && !crc2_ok) {
  99. gd->env_addr = 0;
  100. gd->env_valid = 0;
  101. return 0;
  102. } else if (crc1_ok && !crc2_ok) {
  103. gd->env_valid = 1;
  104. }
  105. #ifdef CONFIG_ENV_OFFSET_REDUND
  106. else if (!crc1_ok && crc2_ok) {
  107. gd->env_valid = 2;
  108. } else {
  109. /* both ok - check serial */
  110. if(tmp_env1->flags == 255 && tmp_env2->flags == 0)
  111. gd->env_valid = 2;
  112. else if(tmp_env2->flags == 255 && tmp_env1->flags == 0)
  113. gd->env_valid = 1;
  114. else if(tmp_env1->flags > tmp_env2->flags)
  115. gd->env_valid = 1;
  116. else if(tmp_env2->flags > tmp_env1->flags)
  117. gd->env_valid = 2;
  118. else /* flags are equal - almost impossible */
  119. gd->env_valid = 1;
  120. }
  121. if (gd->env_valid == 2)
  122. env_ptr = tmp_env2;
  123. else
  124. #endif
  125. if (gd->env_valid == 1)
  126. env_ptr = tmp_env1;
  127. gd->env_addr = (ulong)env_ptr->data;
  128. #else /* ENV_IS_EMBEDDED || CONFIG_NAND_ENV_DST */
  129. gd->env_addr = (ulong)&default_environment[0];
  130. gd->env_valid = 1;
  131. #endif /* ENV_IS_EMBEDDED || CONFIG_NAND_ENV_DST */
  132. return (0);
  133. }
  134. #ifdef CMD_SAVEENV
  135. /*
  136. * The legacy NAND code saved the environment in the first NAND device i.e.,
  137. * nand_dev_desc + 0. This is also the behaviour using the new NAND code.
  138. */
  139. int writeenv(size_t offset, u_char *buf)
  140. {
  141. size_t end = offset + CONFIG_ENV_RANGE;
  142. size_t amount_saved = 0;
  143. size_t blocksize, len;
  144. u_char *char_ptr;
  145. blocksize = nand_info[0].erasesize;
  146. len = min(blocksize, CONFIG_ENV_SIZE);
  147. while (amount_saved < CONFIG_ENV_SIZE && offset < end) {
  148. if (nand_block_isbad(&nand_info[0], offset)) {
  149. offset += blocksize;
  150. } else {
  151. char_ptr = &buf[amount_saved];
  152. if (nand_write(&nand_info[0], offset, &len,
  153. char_ptr))
  154. return 1;
  155. offset += blocksize;
  156. amount_saved += len;
  157. }
  158. }
  159. if (amount_saved != CONFIG_ENV_SIZE)
  160. return 1;
  161. return 0;
  162. }
  163. #ifdef CONFIG_ENV_OFFSET_REDUND
  164. int saveenv(void)
  165. {
  166. int ret = 0;
  167. nand_erase_options_t nand_erase_options;
  168. env_ptr->flags++;
  169. nand_erase_options.length = CONFIG_ENV_RANGE;
  170. nand_erase_options.quiet = 0;
  171. nand_erase_options.jffs2 = 0;
  172. nand_erase_options.scrub = 0;
  173. if (CONFIG_ENV_RANGE < CONFIG_ENV_SIZE)
  174. return 1;
  175. if(gd->env_valid == 1) {
  176. puts ("Erasing redundant Nand...\n");
  177. nand_erase_options.offset = CONFIG_ENV_OFFSET_REDUND;
  178. if (nand_erase_opts(&nand_info[0], &nand_erase_options))
  179. return 1;
  180. puts ("Writing to redundant Nand... ");
  181. ret = writeenv(CONFIG_ENV_OFFSET_REDUND, (u_char *) env_ptr);
  182. } else {
  183. puts ("Erasing Nand...\n");
  184. nand_erase_options.offset = CONFIG_ENV_OFFSET;
  185. if (nand_erase_opts(&nand_info[0], &nand_erase_options))
  186. return 1;
  187. puts ("Writing to Nand... ");
  188. ret = writeenv(CONFIG_ENV_OFFSET, (u_char *) env_ptr);
  189. }
  190. if (ret) {
  191. puts("FAILED!\n");
  192. return 1;
  193. }
  194. puts ("done\n");
  195. gd->env_valid = (gd->env_valid == 2 ? 1 : 2);
  196. return ret;
  197. }
  198. #else /* ! CONFIG_ENV_OFFSET_REDUND */
  199. int saveenv(void)
  200. {
  201. int ret = 0;
  202. nand_erase_options_t nand_erase_options;
  203. nand_erase_options.length = CONFIG_ENV_RANGE;
  204. nand_erase_options.quiet = 0;
  205. nand_erase_options.jffs2 = 0;
  206. nand_erase_options.scrub = 0;
  207. nand_erase_options.offset = CONFIG_ENV_OFFSET;
  208. if (CONFIG_ENV_RANGE < CONFIG_ENV_SIZE)
  209. return 1;
  210. puts ("Erasing Nand...\n");
  211. if (nand_erase_opts(&nand_info[0], &nand_erase_options))
  212. return 1;
  213. puts ("Writing to Nand... ");
  214. if (writeenv(CONFIG_ENV_OFFSET, (u_char *) env_ptr)) {
  215. puts("FAILED!\n");
  216. return 1;
  217. }
  218. puts ("done\n");
  219. return ret;
  220. }
  221. #endif /* CONFIG_ENV_OFFSET_REDUND */
  222. #endif /* CMD_SAVEENV */
  223. int readenv (size_t offset, u_char * buf)
  224. {
  225. size_t end = offset + CONFIG_ENV_RANGE;
  226. size_t amount_loaded = 0;
  227. size_t blocksize, len;
  228. u_char *char_ptr;
  229. blocksize = nand_info[0].erasesize;
  230. len = min(blocksize, CONFIG_ENV_SIZE);
  231. while (amount_loaded < CONFIG_ENV_SIZE && offset < end) {
  232. if (nand_block_isbad(&nand_info[0], offset)) {
  233. offset += blocksize;
  234. } else {
  235. char_ptr = &buf[amount_loaded];
  236. if (nand_read(&nand_info[0], offset, &len, char_ptr))
  237. return 1;
  238. offset += blocksize;
  239. amount_loaded += len;
  240. }
  241. }
  242. if (amount_loaded != CONFIG_ENV_SIZE)
  243. return 1;
  244. return 0;
  245. }
  246. #ifdef CONFIG_ENV_OFFSET_REDUND
  247. void env_relocate_spec (void)
  248. {
  249. #if !defined(ENV_IS_EMBEDDED)
  250. int crc1_ok = 0, crc2_ok = 0;
  251. env_t *tmp_env1, *tmp_env2;
  252. tmp_env1 = (env_t *) malloc(CONFIG_ENV_SIZE);
  253. tmp_env2 = (env_t *) malloc(CONFIG_ENV_SIZE);
  254. if (readenv(CONFIG_ENV_OFFSET, (u_char *) tmp_env1))
  255. puts("No Valid Environment Area Found\n");
  256. if (readenv(CONFIG_ENV_OFFSET_REDUND, (u_char *) tmp_env2))
  257. puts("No Valid Reundant Environment Area Found\n");
  258. crc1_ok = (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc);
  259. crc2_ok = (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc);
  260. if(!crc1_ok && !crc2_ok) {
  261. free(tmp_env1);
  262. free(tmp_env2);
  263. return use_default();
  264. } else if(crc1_ok && !crc2_ok)
  265. gd->env_valid = 1;
  266. else if(!crc1_ok && crc2_ok)
  267. gd->env_valid = 2;
  268. else {
  269. /* both ok - check serial */
  270. if(tmp_env1->flags == 255 && tmp_env2->flags == 0)
  271. gd->env_valid = 2;
  272. else if(tmp_env2->flags == 255 && tmp_env1->flags == 0)
  273. gd->env_valid = 1;
  274. else if(tmp_env1->flags > tmp_env2->flags)
  275. gd->env_valid = 1;
  276. else if(tmp_env2->flags > tmp_env1->flags)
  277. gd->env_valid = 2;
  278. else /* flags are equal - almost impossible */
  279. gd->env_valid = 1;
  280. }
  281. free(env_ptr);
  282. if(gd->env_valid == 1) {
  283. env_ptr = tmp_env1;
  284. free(tmp_env2);
  285. } else {
  286. env_ptr = tmp_env2;
  287. free(tmp_env1);
  288. }
  289. #endif /* ! ENV_IS_EMBEDDED */
  290. }
  291. #else /* ! CONFIG_ENV_OFFSET_REDUND */
  292. /*
  293. * The legacy NAND code saved the environment in the first NAND device i.e.,
  294. * nand_dev_desc + 0. This is also the behaviour using the new NAND code.
  295. */
  296. void env_relocate_spec (void)
  297. {
  298. #if !defined(ENV_IS_EMBEDDED)
  299. int ret;
  300. ret = readenv(CONFIG_ENV_OFFSET, (u_char *) env_ptr);
  301. if (ret)
  302. return use_default();
  303. if (crc32(0, env_ptr->data, ENV_SIZE) != env_ptr->crc)
  304. return use_default();
  305. #endif /* ! ENV_IS_EMBEDDED */
  306. }
  307. #endif /* CONFIG_ENV_OFFSET_REDUND */
  308. #if !defined(ENV_IS_EMBEDDED)
  309. static void use_default()
  310. {
  311. puts ("*** Warning - bad CRC or NAND, using default environment\n\n");
  312. set_default_env();
  313. }
  314. #endif