env_nand.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /*
  2. * (C) Copyright 2000-2010
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * (C) Copyright 2008
  6. * Stuart Wood, Lab X Technologies <stuart.wood@labxtechnologies.com>
  7. *
  8. * (C) Copyright 2004
  9. * Jian Zhang, Texas Instruments, jzhang@ti.com.
  10. *
  11. * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  12. * Andreas Heppel <aheppel@sysgo.de>
  13. *
  14. * See file CREDITS for list of people who contributed to this
  15. * project.
  16. *
  17. * This program is free software; you can redistribute it and/or
  18. * modify it under the terms of the GNU General Public License as
  19. * published by the Free Software Foundation; either version 2 of
  20. * the License, or (at your option) any later version.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU General Public License
  28. * along with this program; if not, write to the Free Software
  29. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  30. * MA 02111-1307 USA
  31. */
  32. #define DEBUG
  33. #include <common.h>
  34. #include <command.h>
  35. #include <environment.h>
  36. #include <linux/stddef.h>
  37. #include <malloc.h>
  38. #include <nand.h>
  39. #include <search.h>
  40. #include <errno.h>
  41. #if defined(CONFIG_CMD_SAVEENV) && defined(CONFIG_CMD_NAND)
  42. #define CMD_SAVEENV
  43. #elif defined(CONFIG_ENV_OFFSET_REDUND)
  44. #error Cannot use CONFIG_ENV_OFFSET_REDUND without CONFIG_CMD_SAVEENV & CONFIG_CMD_NAND
  45. #endif
  46. #if defined(CONFIG_ENV_SIZE_REDUND) && (CONFIG_ENV_SIZE_REDUND != CONFIG_ENV_SIZE)
  47. #error CONFIG_ENV_SIZE_REDUND should be the same as CONFIG_ENV_SIZE
  48. #endif
  49. #ifndef CONFIG_ENV_RANGE
  50. #define CONFIG_ENV_RANGE CONFIG_ENV_SIZE
  51. #endif
  52. char *env_name_spec = "NAND";
  53. #if defined(ENV_IS_EMBEDDED)
  54. env_t *env_ptr = &environment;
  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. DECLARE_GLOBAL_DATA_PTR;
  61. uchar env_get_char_spec (int index)
  62. {
  63. return ( *((uchar *)(gd->env_addr + index)) );
  64. }
  65. /*
  66. * This is called before nand_init() so we can't read NAND to
  67. * validate env data.
  68. *
  69. * Mark it OK for now. env_relocate() in env_common.c will call our
  70. * relocate function which does the real validation.
  71. *
  72. * When using a NAND boot image (like sequoia_nand), the environment
  73. * can be embedded or attached to the U-Boot image in NAND flash.
  74. * This way the SPL loads not only the U-Boot image from NAND but
  75. * also the environment.
  76. */
  77. int env_init(void)
  78. {
  79. #if defined(ENV_IS_EMBEDDED) || defined(CONFIG_NAND_ENV_DST)
  80. int crc1_ok = 0, crc2_ok = 0;
  81. env_t *tmp_env1;
  82. #ifdef CONFIG_ENV_OFFSET_REDUND
  83. env_t *tmp_env2;
  84. tmp_env2 = (env_t *)((ulong)env_ptr + CONFIG_ENV_SIZE);
  85. crc2_ok = (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc);
  86. #endif
  87. tmp_env1 = env_ptr;
  88. crc1_ok = (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc);
  89. if (!crc1_ok && !crc2_ok) {
  90. gd->env_addr = 0;
  91. gd->env_valid = 0;
  92. return 0;
  93. } else if (crc1_ok && !crc2_ok) {
  94. gd->env_valid = 1;
  95. }
  96. #ifdef CONFIG_ENV_OFFSET_REDUND
  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 == 2)
  113. env_ptr = tmp_env2;
  114. else
  115. #endif
  116. if (gd->env_valid == 1)
  117. env_ptr = tmp_env1;
  118. gd->env_addr = (ulong)env_ptr->data;
  119. #else /* ENV_IS_EMBEDDED || CONFIG_NAND_ENV_DST */
  120. gd->env_addr = (ulong)&default_environment[0];
  121. gd->env_valid = 1;
  122. #endif /* ENV_IS_EMBEDDED || CONFIG_NAND_ENV_DST */
  123. return (0);
  124. }
  125. #ifdef CMD_SAVEENV
  126. /*
  127. * The legacy NAND code saved the environment in the first NAND device i.e.,
  128. * nand_dev_desc + 0. This is also the behaviour using the new NAND code.
  129. */
  130. int writeenv(size_t offset, u_char *buf)
  131. {
  132. size_t end = offset + CONFIG_ENV_RANGE;
  133. size_t amount_saved = 0;
  134. size_t blocksize, len;
  135. u_char *char_ptr;
  136. blocksize = nand_info[0].erasesize;
  137. len = min(blocksize, CONFIG_ENV_SIZE);
  138. while (amount_saved < CONFIG_ENV_SIZE && offset < end) {
  139. if (nand_block_isbad(&nand_info[0], offset)) {
  140. offset += blocksize;
  141. } else {
  142. char_ptr = &buf[amount_saved];
  143. if (nand_write(&nand_info[0], offset, &len,
  144. char_ptr))
  145. return 1;
  146. offset += blocksize;
  147. amount_saved += len;
  148. }
  149. }
  150. if (amount_saved != CONFIG_ENV_SIZE)
  151. return 1;
  152. return 0;
  153. }
  154. #ifdef CONFIG_ENV_OFFSET_REDUND
  155. static unsigned char env_flags;
  156. int saveenv(void)
  157. {
  158. env_t env_new;
  159. ssize_t len;
  160. char *res;
  161. int ret = 0;
  162. nand_erase_options_t nand_erase_options;
  163. memset(&nand_erase_options, 0, sizeof(nand_erase_options));
  164. nand_erase_options.length = CONFIG_ENV_RANGE;
  165. if (CONFIG_ENV_RANGE < CONFIG_ENV_SIZE)
  166. return 1;
  167. res = (char *)&env_new.data;
  168. len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL);
  169. if (len < 0) {
  170. error("Cannot export environment: errno = %d\n", errno);
  171. return 1;
  172. }
  173. env_new.crc = crc32(0, env_new.data, ENV_SIZE);
  174. env_new.flags = ++env_flags; /* increase the serial */
  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,
  182. (u_char *)&env_new);
  183. } else {
  184. puts("Erasing NAND...\n");
  185. nand_erase_options.offset = CONFIG_ENV_OFFSET;
  186. if (nand_erase_opts(&nand_info[0], &nand_erase_options))
  187. return 1;
  188. puts("Writing to NAND... ");
  189. ret = writeenv(CONFIG_ENV_OFFSET,
  190. (u_char *)&env_new);
  191. }
  192. if (ret) {
  193. puts("FAILED!\n");
  194. return 1;
  195. }
  196. puts("done\n");
  197. gd->env_valid = (gd->env_valid == 2 ? 1 : 2);
  198. return ret;
  199. }
  200. #else /* ! CONFIG_ENV_OFFSET_REDUND */
  201. int saveenv(void)
  202. {
  203. int ret = 0;
  204. env_t env_new;
  205. ssize_t len;
  206. char *res;
  207. nand_erase_options_t nand_erase_options;
  208. memset(&nand_erase_options, 0, sizeof(nand_erase_options));
  209. nand_erase_options.length = CONFIG_ENV_RANGE;
  210. nand_erase_options.offset = CONFIG_ENV_OFFSET;
  211. if (CONFIG_ENV_RANGE < CONFIG_ENV_SIZE)
  212. return 1;
  213. res = (char *)&env_new.data;
  214. len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL);
  215. if (len < 0) {
  216. error("Cannot export environment: errno = %d\n", errno);
  217. return 1;
  218. }
  219. env_new.crc = crc32(0, env_new.data, ENV_SIZE);
  220. puts("Erasing Nand...\n");
  221. if (nand_erase_opts(&nand_info[0], &nand_erase_options))
  222. return 1;
  223. puts("Writing to Nand... ");
  224. if (writeenv(CONFIG_ENV_OFFSET, (u_char *)&env_new)) {
  225. puts("FAILED!\n");
  226. return 1;
  227. }
  228. puts("done\n");
  229. return ret;
  230. }
  231. #endif /* CONFIG_ENV_OFFSET_REDUND */
  232. #endif /* CMD_SAVEENV */
  233. int readenv(size_t offset, u_char * buf)
  234. {
  235. size_t end = offset + CONFIG_ENV_RANGE;
  236. size_t amount_loaded = 0;
  237. size_t blocksize, len;
  238. u_char *char_ptr;
  239. blocksize = nand_info[0].erasesize;
  240. if (!blocksize)
  241. return 1;
  242. len = min(blocksize, CONFIG_ENV_SIZE);
  243. while (amount_loaded < CONFIG_ENV_SIZE && offset < end) {
  244. if (nand_block_isbad(&nand_info[0], offset)) {
  245. offset += blocksize;
  246. } else {
  247. char_ptr = &buf[amount_loaded];
  248. if (nand_read_skip_bad(&nand_info[0], offset, &len, char_ptr))
  249. return 1;
  250. offset += blocksize;
  251. amount_loaded += len;
  252. }
  253. }
  254. if (amount_loaded != CONFIG_ENV_SIZE)
  255. return 1;
  256. return 0;
  257. }
  258. #ifdef CONFIG_ENV_OFFSET_OOB
  259. int get_nand_env_oob(nand_info_t *nand, unsigned long *result)
  260. {
  261. struct mtd_oob_ops ops;
  262. uint32_t oob_buf[ENV_OFFSET_SIZE/sizeof(uint32_t)];
  263. int ret;
  264. ops.datbuf = NULL;
  265. ops.mode = MTD_OOB_AUTO;
  266. ops.ooboffs = 0;
  267. ops.ooblen = ENV_OFFSET_SIZE;
  268. ops.oobbuf = (void *) oob_buf;
  269. ret = nand->read_oob(nand, ENV_OFFSET_SIZE, &ops);
  270. if (ret) {
  271. printf("error reading OOB block 0\n");
  272. return ret;
  273. }
  274. if (oob_buf[0] == ENV_OOB_MARKER) {
  275. *result = oob_buf[1] * nand->erasesize;
  276. } else if (oob_buf[0] == ENV_OOB_MARKER_OLD) {
  277. *result = oob_buf[1];
  278. } else {
  279. printf("No dynamic environment marker in OOB block 0\n");
  280. return -ENOENT;
  281. }
  282. return 0;
  283. }
  284. #endif
  285. #ifdef CONFIG_ENV_OFFSET_REDUND
  286. void env_relocate_spec(void)
  287. {
  288. #if !defined(ENV_IS_EMBEDDED)
  289. int crc1_ok = 0, crc2_ok = 0;
  290. env_t *ep, *tmp_env1, *tmp_env2;
  291. tmp_env1 = (env_t *)malloc(CONFIG_ENV_SIZE);
  292. tmp_env2 = (env_t *)malloc(CONFIG_ENV_SIZE);
  293. if ((tmp_env1 == NULL) || (tmp_env2 == NULL)) {
  294. puts("Can't allocate buffers for environment\n");
  295. free(tmp_env1);
  296. free(tmp_env2);
  297. set_default_env("!malloc() failed");
  298. return;
  299. }
  300. if (readenv(CONFIG_ENV_OFFSET, (u_char *) tmp_env1))
  301. puts("No Valid Environment Area found\n");
  302. if (readenv(CONFIG_ENV_OFFSET_REDUND, (u_char *) tmp_env2))
  303. puts("No Valid Redundant Environment Area found\n");
  304. crc1_ok = (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc);
  305. crc2_ok = (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc);
  306. if (!crc1_ok && !crc2_ok) {
  307. free(tmp_env1);
  308. free(tmp_env2);
  309. set_default_env("!bad CRC");
  310. return;
  311. } else if (crc1_ok && !crc2_ok) {
  312. gd->env_valid = 1;
  313. } else if (!crc1_ok && crc2_ok) {
  314. gd->env_valid = 2;
  315. } else {
  316. /* both ok - check serial */
  317. if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
  318. gd->env_valid = 2;
  319. else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
  320. gd->env_valid = 1;
  321. else if (tmp_env1->flags > tmp_env2->flags)
  322. gd->env_valid = 1;
  323. else if (tmp_env2->flags > tmp_env1->flags)
  324. gd->env_valid = 2;
  325. else /* flags are equal - almost impossible */
  326. gd->env_valid = 1;
  327. }
  328. free(env_ptr);
  329. if (gd->env_valid == 1)
  330. ep = tmp_env1;
  331. else
  332. ep = tmp_env2;
  333. env_flags = ep->flags;
  334. env_import((char *)ep, 0);
  335. free(tmp_env1);
  336. free(tmp_env2);
  337. #endif /* ! ENV_IS_EMBEDDED */
  338. }
  339. #else /* ! CONFIG_ENV_OFFSET_REDUND */
  340. /*
  341. * The legacy NAND code saved the environment in the first NAND
  342. * device i.e., nand_dev_desc + 0. This is also the behaviour using
  343. * the new NAND code.
  344. */
  345. void env_relocate_spec (void)
  346. {
  347. #if !defined(ENV_IS_EMBEDDED)
  348. int ret;
  349. char buf[CONFIG_ENV_SIZE];
  350. #if defined(CONFIG_ENV_OFFSET_OOB)
  351. ret = get_nand_env_oob(&nand_info[0], &nand_env_oob_offset);
  352. /*
  353. * If unable to read environment offset from NAND OOB then fall through
  354. * to the normal environment reading code below
  355. */
  356. if (!ret) {
  357. printf("Found Environment offset in OOB..\n");
  358. } else {
  359. set_default_env("!no env offset in OOB");
  360. return;
  361. }
  362. #endif
  363. ret = readenv(CONFIG_ENV_OFFSET, (u_char *)buf);
  364. if (ret) {
  365. set_default_env("!readenv() failed");
  366. return;
  367. }
  368. env_import(buf, 1);
  369. #endif /* ! ENV_IS_EMBEDDED */
  370. }
  371. #endif /* CONFIG_ENV_OFFSET_REDUND */