env_nand.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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_ENV) && 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_ENV & 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. size_t total;
  88. int crc1_ok = 0, crc2_ok = 0;
  89. env_t *tmp_env1, *tmp_env2;
  90. total = CONFIG_ENV_SIZE;
  91. tmp_env1 = env_ptr;
  92. tmp_env2 = (env_t *)((ulong)env_ptr + CONFIG_ENV_SIZE);
  93. crc1_ok = (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc);
  94. crc2_ok = (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc);
  95. if (!crc1_ok && !crc2_ok)
  96. gd->env_valid = 0;
  97. else if(crc1_ok && !crc2_ok)
  98. gd->env_valid = 1;
  99. else if(!crc1_ok && crc2_ok)
  100. gd->env_valid = 2;
  101. else {
  102. /* both ok - check serial */
  103. if(tmp_env1->flags == 255 && tmp_env2->flags == 0)
  104. gd->env_valid = 2;
  105. else if(tmp_env2->flags == 255 && tmp_env1->flags == 0)
  106. gd->env_valid = 1;
  107. else if(tmp_env1->flags > tmp_env2->flags)
  108. gd->env_valid = 1;
  109. else if(tmp_env2->flags > tmp_env1->flags)
  110. gd->env_valid = 2;
  111. else /* flags are equal - almost impossible */
  112. gd->env_valid = 1;
  113. }
  114. if (gd->env_valid == 1)
  115. env_ptr = tmp_env1;
  116. else if (gd->env_valid == 2)
  117. env_ptr = tmp_env2;
  118. #else /* ENV_IS_EMBEDDED */
  119. gd->env_addr = (ulong)&default_environment[0];
  120. gd->env_valid = 1;
  121. #endif /* ENV_IS_EMBEDDED */
  122. return (0);
  123. }
  124. #ifdef CMD_SAVEENV
  125. /*
  126. * The legacy NAND code saved the environment in the first NAND device i.e.,
  127. * nand_dev_desc + 0. This is also the behaviour using the new NAND code.
  128. */
  129. int writeenv(size_t offset, u_char *buf)
  130. {
  131. size_t end = offset + CONFIG_ENV_RANGE;
  132. size_t amount_saved = 0;
  133. size_t blocksize, len;
  134. u_char *char_ptr;
  135. blocksize = nand_info[0].erasesize;
  136. len = min(blocksize, CONFIG_ENV_SIZE);
  137. while (amount_saved < CONFIG_ENV_SIZE && offset < end) {
  138. if (nand_block_isbad(&nand_info[0], offset)) {
  139. offset += blocksize;
  140. } else {
  141. char_ptr = &buf[amount_saved];
  142. if (nand_write(&nand_info[0], offset, &len,
  143. char_ptr))
  144. return 1;
  145. offset += blocksize;
  146. amount_saved += len;
  147. }
  148. }
  149. if (amount_saved != CONFIG_ENV_SIZE)
  150. return 1;
  151. return 0;
  152. }
  153. #ifdef CONFIG_ENV_OFFSET_REDUND
  154. int saveenv(void)
  155. {
  156. size_t total;
  157. int ret = 0;
  158. nand_erase_options_t nand_erase_options;
  159. env_ptr->flags++;
  160. total = CONFIG_ENV_SIZE;
  161. nand_erase_options.length = CONFIG_ENV_RANGE;
  162. nand_erase_options.quiet = 0;
  163. nand_erase_options.jffs2 = 0;
  164. nand_erase_options.scrub = 0;
  165. if (CONFIG_ENV_RANGE < CONFIG_ENV_SIZE)
  166. return 1;
  167. if(gd->env_valid == 1) {
  168. puts ("Erasing redundant Nand...\n");
  169. nand_erase_options.offset = CONFIG_ENV_OFFSET_REDUND;
  170. if (nand_erase_opts(&nand_info[0], &nand_erase_options))
  171. return 1;
  172. puts ("Writing to redundant Nand... ");
  173. ret = writeenv(CONFIG_ENV_OFFSET_REDUND, (u_char *) env_ptr);
  174. } else {
  175. puts ("Erasing Nand...\n");
  176. nand_erase_options.offset = CONFIG_ENV_OFFSET;
  177. if (nand_erase_opts(&nand_info[0], &nand_erase_options))
  178. return 1;
  179. puts ("Writing to Nand... ");
  180. ret = writeenv(CONFIG_ENV_OFFSET, (u_char *) env_ptr);
  181. }
  182. if (ret) {
  183. puts("FAILED!\n");
  184. return 1;
  185. }
  186. puts ("done\n");
  187. gd->env_valid = (gd->env_valid == 2 ? 1 : 2);
  188. return ret;
  189. }
  190. #else /* ! CONFIG_ENV_OFFSET_REDUND */
  191. int saveenv(void)
  192. {
  193. size_t total;
  194. int ret = 0;
  195. nand_erase_options_t nand_erase_options;
  196. nand_erase_options.length = CONFIG_ENV_RANGE;
  197. nand_erase_options.quiet = 0;
  198. nand_erase_options.jffs2 = 0;
  199. nand_erase_options.scrub = 0;
  200. nand_erase_options.offset = CONFIG_ENV_OFFSET;
  201. if (CONFIG_ENV_RANGE < CONFIG_ENV_SIZE)
  202. return 1;
  203. puts ("Erasing Nand...\n");
  204. if (nand_erase_opts(&nand_info[0], &nand_erase_options))
  205. return 1;
  206. puts ("Writing to Nand... ");
  207. total = CONFIG_ENV_SIZE;
  208. if (writeenv(CONFIG_ENV_OFFSET, (u_char *) env_ptr)) {
  209. puts("FAILED!\n");
  210. return 1;
  211. }
  212. puts ("done\n");
  213. return ret;
  214. }
  215. #endif /* CONFIG_ENV_OFFSET_REDUND */
  216. #endif /* CMD_SAVEENV */
  217. int readenv (size_t offset, u_char * buf)
  218. {
  219. size_t end = offset + CONFIG_ENV_RANGE;
  220. size_t amount_loaded = 0;
  221. size_t blocksize, len;
  222. u_char *char_ptr;
  223. blocksize = nand_info[0].erasesize;
  224. len = min(blocksize, CONFIG_ENV_SIZE);
  225. while (amount_loaded < CONFIG_ENV_SIZE && offset < end) {
  226. if (nand_block_isbad(&nand_info[0], offset)) {
  227. offset += blocksize;
  228. } else {
  229. char_ptr = &buf[amount_loaded];
  230. if (nand_read(&nand_info[0], offset, &len, char_ptr))
  231. return 1;
  232. offset += blocksize;
  233. amount_loaded += len;
  234. }
  235. }
  236. if (amount_loaded != CONFIG_ENV_SIZE)
  237. return 1;
  238. return 0;
  239. }
  240. #ifdef CONFIG_ENV_OFFSET_REDUND
  241. void env_relocate_spec (void)
  242. {
  243. #if !defined(ENV_IS_EMBEDDED)
  244. size_t total;
  245. int crc1_ok = 0, crc2_ok = 0;
  246. env_t *tmp_env1, *tmp_env2;
  247. total = CONFIG_ENV_SIZE;
  248. tmp_env1 = (env_t *) malloc(CONFIG_ENV_SIZE);
  249. tmp_env2 = (env_t *) malloc(CONFIG_ENV_SIZE);
  250. if (readenv(CONFIG_ENV_OFFSET, (u_char *) tmp_env1))
  251. puts("No Valid Environment Area Found\n");
  252. if (readenv(CONFIG_ENV_OFFSET_REDUND, (u_char *) tmp_env2))
  253. puts("No Valid Reundant Environment Area Found\n");
  254. crc1_ok = (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc);
  255. crc2_ok = (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc);
  256. if(!crc1_ok && !crc2_ok) {
  257. free(tmp_env1);
  258. free(tmp_env2);
  259. return use_default();
  260. } else if(crc1_ok && !crc2_ok)
  261. gd->env_valid = 1;
  262. else if(!crc1_ok && crc2_ok)
  263. gd->env_valid = 2;
  264. else {
  265. /* both ok - check serial */
  266. if(tmp_env1->flags == 255 && tmp_env2->flags == 0)
  267. gd->env_valid = 2;
  268. else if(tmp_env2->flags == 255 && tmp_env1->flags == 0)
  269. gd->env_valid = 1;
  270. else if(tmp_env1->flags > tmp_env2->flags)
  271. gd->env_valid = 1;
  272. else if(tmp_env2->flags > tmp_env1->flags)
  273. gd->env_valid = 2;
  274. else /* flags are equal - almost impossible */
  275. gd->env_valid = 1;
  276. }
  277. free(env_ptr);
  278. if(gd->env_valid == 1) {
  279. env_ptr = tmp_env1;
  280. free(tmp_env2);
  281. } else {
  282. env_ptr = tmp_env2;
  283. free(tmp_env1);
  284. }
  285. #endif /* ! ENV_IS_EMBEDDED */
  286. }
  287. #else /* ! CONFIG_ENV_OFFSET_REDUND */
  288. /*
  289. * The legacy NAND code saved the environment in the first NAND device i.e.,
  290. * nand_dev_desc + 0. This is also the behaviour using the new NAND code.
  291. */
  292. void env_relocate_spec (void)
  293. {
  294. #if !defined(ENV_IS_EMBEDDED)
  295. int ret;
  296. ret = readenv(CONFIG_ENV_OFFSET, (u_char *) env_ptr);
  297. if (ret)
  298. return use_default();
  299. if (crc32(0, env_ptr->data, ENV_SIZE) != env_ptr->crc)
  300. return use_default();
  301. #endif /* ! ENV_IS_EMBEDDED */
  302. }
  303. #endif /* CONFIG_ENV_OFFSET_REDUND */
  304. #if !defined(ENV_IS_EMBEDDED)
  305. static void use_default()
  306. {
  307. puts ("*** Warning - bad CRC or NAND, using default environment\n\n");
  308. set_default_env();
  309. }
  310. #endif