env_nand.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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. #include <asm/errno.h>
  38. #if defined(CONFIG_CMD_SAVEENV) && defined(CONFIG_CMD_NAND)
  39. #define CMD_SAVEENV
  40. #elif defined(CONFIG_ENV_OFFSET_REDUND)
  41. #error Cannot use CONFIG_ENV_OFFSET_REDUND without CONFIG_CMD_SAVEENV & CONFIG_CMD_NAND
  42. #endif
  43. #if defined(CONFIG_ENV_SIZE_REDUND) && (CONFIG_ENV_SIZE_REDUND != CONFIG_ENV_SIZE)
  44. #error CONFIG_ENV_SIZE_REDUND should be the same as CONFIG_ENV_SIZE
  45. #endif
  46. #ifndef CONFIG_ENV_RANGE
  47. #define CONFIG_ENV_RANGE CONFIG_ENV_SIZE
  48. #endif
  49. /* references to names in env_common.c */
  50. extern uchar default_environment[];
  51. char * env_name_spec = "NAND";
  52. #if defined(ENV_IS_EMBEDDED)
  53. extern uchar environment[];
  54. env_t *env_ptr = (env_t *)(&environment[0]);
  55. #elif defined(CONFIG_NAND_ENV_DST)
  56. env_t *env_ptr = (env_t *)CONFIG_NAND_ENV_DST;
  57. #else /* ! ENV_IS_EMBEDDED */
  58. env_t *env_ptr = 0;
  59. #endif /* ENV_IS_EMBEDDED */
  60. /* local functions */
  61. #if !defined(ENV_IS_EMBEDDED)
  62. static void use_default(void);
  63. #endif
  64. DECLARE_GLOBAL_DATA_PTR;
  65. uchar env_get_char_spec (int index)
  66. {
  67. return ( *((uchar *)(gd->env_addr + index)) );
  68. }
  69. /* this is called before nand_init()
  70. * so we can't read Nand to validate env data.
  71. * Mark it OK for now. env_relocate() in env_common.c
  72. * will call our relocate function which does the real
  73. * validation.
  74. *
  75. * When using a NAND boot image (like sequoia_nand), the environment
  76. * can be embedded or attached to the U-Boot image in NAND flash. This way
  77. * the SPL loads not only the U-Boot image from NAND but also the
  78. * environment.
  79. */
  80. int env_init(void)
  81. {
  82. #if defined(ENV_IS_EMBEDDED) || defined(CONFIG_NAND_ENV_DST)
  83. int crc1_ok = 0, crc2_ok = 0;
  84. env_t *tmp_env1;
  85. #ifdef CONFIG_ENV_OFFSET_REDUND
  86. env_t *tmp_env2;
  87. tmp_env2 = (env_t *)((ulong)env_ptr + CONFIG_ENV_SIZE);
  88. crc2_ok = (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc);
  89. #endif
  90. tmp_env1 = env_ptr;
  91. crc1_ok = (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc);
  92. if (!crc1_ok && !crc2_ok) {
  93. gd->env_addr = 0;
  94. gd->env_valid = 0;
  95. return 0;
  96. } else if (crc1_ok && !crc2_ok) {
  97. gd->env_valid = 1;
  98. }
  99. #ifdef CONFIG_ENV_OFFSET_REDUND
  100. else if (!crc1_ok && crc2_ok) {
  101. gd->env_valid = 2;
  102. } else {
  103. /* both ok - check serial */
  104. if(tmp_env1->flags == 255 && tmp_env2->flags == 0)
  105. gd->env_valid = 2;
  106. else if(tmp_env2->flags == 255 && tmp_env1->flags == 0)
  107. gd->env_valid = 1;
  108. else if(tmp_env1->flags > tmp_env2->flags)
  109. gd->env_valid = 1;
  110. else if(tmp_env2->flags > tmp_env1->flags)
  111. gd->env_valid = 2;
  112. else /* flags are equal - almost impossible */
  113. gd->env_valid = 1;
  114. }
  115. if (gd->env_valid == 2)
  116. env_ptr = tmp_env2;
  117. else
  118. #endif
  119. if (gd->env_valid == 1)
  120. env_ptr = tmp_env1;
  121. gd->env_addr = (ulong)env_ptr->data;
  122. #else /* ENV_IS_EMBEDDED || CONFIG_NAND_ENV_DST */
  123. gd->env_addr = (ulong)&default_environment[0];
  124. gd->env_valid = 1;
  125. #endif /* ENV_IS_EMBEDDED || CONFIG_NAND_ENV_DST */
  126. return (0);
  127. }
  128. #ifdef CMD_SAVEENV
  129. /*
  130. * The legacy NAND code saved the environment in the first NAND device i.e.,
  131. * nand_dev_desc + 0. This is also the behaviour using the new NAND code.
  132. */
  133. int writeenv(size_t offset, u_char *buf)
  134. {
  135. size_t end = offset + CONFIG_ENV_RANGE;
  136. size_t amount_saved = 0;
  137. size_t blocksize, len;
  138. u_char *char_ptr;
  139. blocksize = nand_info[0].erasesize;
  140. len = min(blocksize, CONFIG_ENV_SIZE);
  141. while (amount_saved < CONFIG_ENV_SIZE && offset < end) {
  142. if (nand_block_isbad(&nand_info[0], offset)) {
  143. offset += blocksize;
  144. } else {
  145. char_ptr = &buf[amount_saved];
  146. if (nand_write(&nand_info[0], offset, &len,
  147. char_ptr))
  148. return 1;
  149. offset += blocksize;
  150. amount_saved += len;
  151. }
  152. }
  153. if (amount_saved != CONFIG_ENV_SIZE)
  154. return 1;
  155. return 0;
  156. }
  157. #ifdef CONFIG_ENV_OFFSET_REDUND
  158. int saveenv(void)
  159. {
  160. int ret = 0;
  161. nand_erase_options_t nand_erase_options;
  162. env_ptr->flags++;
  163. nand_erase_options.length = CONFIG_ENV_RANGE;
  164. nand_erase_options.quiet = 0;
  165. nand_erase_options.jffs2 = 0;
  166. nand_erase_options.scrub = 0;
  167. if (CONFIG_ENV_RANGE < CONFIG_ENV_SIZE)
  168. return 1;
  169. if(gd->env_valid == 1) {
  170. puts ("Erasing redundant Nand...\n");
  171. nand_erase_options.offset = CONFIG_ENV_OFFSET_REDUND;
  172. if (nand_erase_opts(&nand_info[0], &nand_erase_options))
  173. return 1;
  174. puts ("Writing to redundant Nand... ");
  175. ret = writeenv(CONFIG_ENV_OFFSET_REDUND, (u_char *) env_ptr);
  176. } else {
  177. puts ("Erasing Nand...\n");
  178. nand_erase_options.offset = CONFIG_ENV_OFFSET;
  179. if (nand_erase_opts(&nand_info[0], &nand_erase_options))
  180. return 1;
  181. puts ("Writing to Nand... ");
  182. ret = writeenv(CONFIG_ENV_OFFSET, (u_char *) env_ptr);
  183. }
  184. if (ret) {
  185. puts("FAILED!\n");
  186. return 1;
  187. }
  188. puts ("done\n");
  189. gd->env_valid = (gd->env_valid == 2 ? 1 : 2);
  190. return ret;
  191. }
  192. #else /* ! CONFIG_ENV_OFFSET_REDUND */
  193. int saveenv(void)
  194. {
  195. int ret = 0;
  196. nand_erase_options_t nand_erase_options;
  197. nand_erase_options.length = CONFIG_ENV_RANGE;
  198. nand_erase_options.quiet = 0;
  199. nand_erase_options.jffs2 = 0;
  200. nand_erase_options.scrub = 0;
  201. nand_erase_options.offset = CONFIG_ENV_OFFSET;
  202. if (CONFIG_ENV_RANGE < CONFIG_ENV_SIZE)
  203. return 1;
  204. puts ("Erasing Nand...\n");
  205. if (nand_erase_opts(&nand_info[0], &nand_erase_options))
  206. return 1;
  207. puts ("Writing to Nand... ");
  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. if (!blocksize)
  225. return 1;
  226. len = min(blocksize, CONFIG_ENV_SIZE);
  227. while (amount_loaded < CONFIG_ENV_SIZE && offset < end) {
  228. if (nand_block_isbad(&nand_info[0], offset)) {
  229. offset += blocksize;
  230. } else {
  231. char_ptr = &buf[amount_loaded];
  232. if (nand_read(&nand_info[0], offset, &len, char_ptr))
  233. return 1;
  234. offset += blocksize;
  235. amount_loaded += len;
  236. }
  237. }
  238. if (amount_loaded != CONFIG_ENV_SIZE)
  239. return 1;
  240. return 0;
  241. }
  242. #ifdef CONFIG_ENV_OFFSET_OOB
  243. int get_nand_env_oob(nand_info_t *nand, unsigned long *result)
  244. {
  245. struct mtd_oob_ops ops;
  246. uint32_t oob_buf[ENV_OFFSET_SIZE/sizeof(uint32_t)];
  247. int ret;
  248. ops.datbuf = NULL;
  249. ops.mode = MTD_OOB_AUTO;
  250. ops.ooboffs = 0;
  251. ops.ooblen = ENV_OFFSET_SIZE;
  252. ops.oobbuf = (void *) oob_buf;
  253. ret = nand->read_oob(nand, ENV_OFFSET_SIZE, &ops);
  254. if (ret) {
  255. printf("error reading OOB block 0\n");
  256. return ret;
  257. }
  258. if (oob_buf[0] == ENV_OOB_MARKER) {
  259. *result = oob_buf[1] * nand->erasesize;
  260. } else if (oob_buf[0] == ENV_OOB_MARKER_OLD) {
  261. *result = oob_buf[1];
  262. } else {
  263. printf("No dynamic environment marker in OOB block 0\n");
  264. return -ENOENT;
  265. }
  266. return 0;
  267. }
  268. #endif
  269. #ifdef CONFIG_ENV_OFFSET_REDUND
  270. void env_relocate_spec (void)
  271. {
  272. #if !defined(ENV_IS_EMBEDDED)
  273. int crc1_ok = 0, crc2_ok = 0;
  274. env_t *tmp_env1, *tmp_env2;
  275. tmp_env1 = (env_t *) malloc(CONFIG_ENV_SIZE);
  276. tmp_env2 = (env_t *) malloc(CONFIG_ENV_SIZE);
  277. if ((tmp_env1 == NULL) || (tmp_env2 == NULL)) {
  278. puts("Can't allocate buffers for environment\n");
  279. free (tmp_env1);
  280. free (tmp_env2);
  281. return use_default();
  282. }
  283. if (readenv(CONFIG_ENV_OFFSET, (u_char *) tmp_env1))
  284. puts("No Valid Environment Area Found\n");
  285. if (readenv(CONFIG_ENV_OFFSET_REDUND, (u_char *) tmp_env2))
  286. puts("No Valid Reundant Environment Area Found\n");
  287. crc1_ok = (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc);
  288. crc2_ok = (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc);
  289. if(!crc1_ok && !crc2_ok) {
  290. free(tmp_env1);
  291. free(tmp_env2);
  292. return use_default();
  293. } else if(crc1_ok && !crc2_ok)
  294. gd->env_valid = 1;
  295. else if(!crc1_ok && crc2_ok)
  296. gd->env_valid = 2;
  297. else {
  298. /* both ok - check serial */
  299. if(tmp_env1->flags == 255 && tmp_env2->flags == 0)
  300. gd->env_valid = 2;
  301. else if(tmp_env2->flags == 255 && tmp_env1->flags == 0)
  302. gd->env_valid = 1;
  303. else if(tmp_env1->flags > tmp_env2->flags)
  304. gd->env_valid = 1;
  305. else if(tmp_env2->flags > tmp_env1->flags)
  306. gd->env_valid = 2;
  307. else /* flags are equal - almost impossible */
  308. gd->env_valid = 1;
  309. }
  310. free(env_ptr);
  311. if(gd->env_valid == 1) {
  312. env_ptr = tmp_env1;
  313. free(tmp_env2);
  314. } else {
  315. env_ptr = tmp_env2;
  316. free(tmp_env1);
  317. }
  318. #endif /* ! ENV_IS_EMBEDDED */
  319. }
  320. #else /* ! CONFIG_ENV_OFFSET_REDUND */
  321. /*
  322. * The legacy NAND code saved the environment in the first NAND device i.e.,
  323. * nand_dev_desc + 0. This is also the behaviour using the new NAND code.
  324. */
  325. void env_relocate_spec (void)
  326. {
  327. #if !defined(ENV_IS_EMBEDDED)
  328. int ret;
  329. #if defined(CONFIG_ENV_OFFSET_OOB)
  330. ret = get_nand_env_oob(&nand_info[0], &nand_env_oob_offset);
  331. /* If unable to read environment offset from NAND OOB then fall through
  332. * to the normal environment reading code below
  333. */
  334. if (!ret)
  335. printf("Found Environment offset in OOB..\n");
  336. else
  337. return use_default();
  338. #endif
  339. ret = readenv(CONFIG_ENV_OFFSET, (u_char *) env_ptr);
  340. if (ret)
  341. return use_default();
  342. if (crc32(0, env_ptr->data, ENV_SIZE) != env_ptr->crc)
  343. return use_default();
  344. #endif /* ! ENV_IS_EMBEDDED */
  345. }
  346. #endif /* CONFIG_ENV_OFFSET_REDUND */
  347. #if !defined(ENV_IS_EMBEDDED)
  348. static void use_default()
  349. {
  350. puts ("*** Warning - bad CRC or NAND, using default environment\n\n");
  351. set_default_env();
  352. }
  353. #endif