env_nand.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. #include <common.h>
  33. #include <command.h>
  34. #include <environment.h>
  35. #include <linux/stddef.h>
  36. #include <malloc.h>
  37. #include <nand.h>
  38. #include <search.h>
  39. #include <errno.h>
  40. #if defined(CONFIG_CMD_SAVEENV) && defined(CONFIG_CMD_NAND)
  41. #define CMD_SAVEENV
  42. #elif defined(CONFIG_ENV_OFFSET_REDUND)
  43. #error CONFIG_ENV_OFFSET_REDUND must have CONFIG_CMD_SAVEENV & CONFIG_CMD_NAND
  44. #endif
  45. #if defined(CONFIG_ENV_SIZE_REDUND) && \
  46. (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;
  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, 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. static unsigned char env_flags;
  155. int saveenv(void)
  156. {
  157. env_t env_new;
  158. ssize_t len;
  159. char *res;
  160. int ret = 0;
  161. nand_erase_options_t nand_erase_options;
  162. memset(&nand_erase_options, 0, sizeof(nand_erase_options));
  163. nand_erase_options.length = CONFIG_ENV_RANGE;
  164. if (CONFIG_ENV_RANGE < CONFIG_ENV_SIZE)
  165. return 1;
  166. res = (char *)&env_new.data;
  167. len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL);
  168. if (len < 0) {
  169. error("Cannot export environment: errno = %d\n", errno);
  170. return 1;
  171. }
  172. env_new.crc = crc32(0, env_new.data, ENV_SIZE);
  173. env_new.flags = ++env_flags; /* increase the serial */
  174. if (gd->env_valid == 1) {
  175. puts("Erasing redundant NAND...\n");
  176. nand_erase_options.offset = CONFIG_ENV_OFFSET_REDUND;
  177. if (nand_erase_opts(&nand_info[0], &nand_erase_options))
  178. return 1;
  179. puts("Writing to redundant NAND... ");
  180. ret = writeenv(CONFIG_ENV_OFFSET_REDUND, (u_char *)&env_new);
  181. } else {
  182. puts("Erasing NAND...\n");
  183. nand_erase_options.offset = CONFIG_ENV_OFFSET;
  184. if (nand_erase_opts(&nand_info[0], &nand_erase_options))
  185. return 1;
  186. puts("Writing to NAND... ");
  187. ret = writeenv(CONFIG_ENV_OFFSET, (u_char *)&env_new);
  188. }
  189. if (ret) {
  190. puts("FAILED!\n");
  191. return 1;
  192. }
  193. puts("done\n");
  194. gd->env_valid = gd->env_valid == 2 ? 1 : 2;
  195. return ret;
  196. }
  197. #else /* ! CONFIG_ENV_OFFSET_REDUND */
  198. int saveenv(void)
  199. {
  200. int ret = 0;
  201. env_t env_new;
  202. ssize_t len;
  203. char *res;
  204. nand_erase_options_t nand_erase_options;
  205. memset(&nand_erase_options, 0, sizeof(nand_erase_options));
  206. nand_erase_options.length = CONFIG_ENV_RANGE;
  207. nand_erase_options.offset = CONFIG_ENV_OFFSET;
  208. if (CONFIG_ENV_RANGE < CONFIG_ENV_SIZE)
  209. return 1;
  210. res = (char *)&env_new.data;
  211. len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL);
  212. if (len < 0) {
  213. error("Cannot export environment: errno = %d\n", errno);
  214. return 1;
  215. }
  216. env_new.crc = crc32(0, env_new.data, ENV_SIZE);
  217. puts("Erasing Nand...\n");
  218. if (nand_erase_opts(&nand_info[0], &nand_erase_options))
  219. return 1;
  220. puts("Writing to Nand... ");
  221. if (writeenv(CONFIG_ENV_OFFSET, (u_char *)&env_new)) {
  222. puts("FAILED!\n");
  223. return 1;
  224. }
  225. puts("done\n");
  226. return ret;
  227. }
  228. #endif /* CONFIG_ENV_OFFSET_REDUND */
  229. #endif /* CMD_SAVEENV */
  230. int readenv(size_t offset, u_char *buf)
  231. {
  232. size_t end = offset + CONFIG_ENV_RANGE;
  233. size_t amount_loaded = 0;
  234. size_t blocksize, len;
  235. u_char *char_ptr;
  236. blocksize = nand_info[0].erasesize;
  237. if (!blocksize)
  238. return 1;
  239. len = min(blocksize, CONFIG_ENV_SIZE);
  240. while (amount_loaded < CONFIG_ENV_SIZE && offset < end) {
  241. if (nand_block_isbad(&nand_info[0], offset)) {
  242. offset += blocksize;
  243. } else {
  244. char_ptr = &buf[amount_loaded];
  245. if (nand_read_skip_bad(&nand_info[0], offset,
  246. &len, char_ptr))
  247. return 1;
  248. offset += blocksize;
  249. amount_loaded += len;
  250. }
  251. }
  252. if (amount_loaded != CONFIG_ENV_SIZE)
  253. return 1;
  254. return 0;
  255. }
  256. #ifdef CONFIG_ENV_OFFSET_OOB
  257. int get_nand_env_oob(nand_info_t *nand, unsigned long *result)
  258. {
  259. struct mtd_oob_ops ops;
  260. uint32_t oob_buf[ENV_OFFSET_SIZE / sizeof(uint32_t)];
  261. int ret;
  262. ops.datbuf = NULL;
  263. ops.mode = MTD_OOB_AUTO;
  264. ops.ooboffs = 0;
  265. ops.ooblen = ENV_OFFSET_SIZE;
  266. ops.oobbuf = (void *)oob_buf;
  267. ret = nand->read_oob(nand, ENV_OFFSET_SIZE, &ops);
  268. if (ret) {
  269. printf("error reading OOB block 0\n");
  270. return ret;
  271. }
  272. if (oob_buf[0] == ENV_OOB_MARKER) {
  273. *result = oob_buf[1] * nand->erasesize;
  274. } else if (oob_buf[0] == ENV_OOB_MARKER_OLD) {
  275. *result = oob_buf[1];
  276. } else {
  277. printf("No dynamic environment marker in OOB block 0\n");
  278. return -ENOENT;
  279. }
  280. return 0;
  281. }
  282. #endif
  283. #ifdef CONFIG_ENV_OFFSET_REDUND
  284. void env_relocate_spec(void)
  285. {
  286. #if !defined(ENV_IS_EMBEDDED)
  287. int crc1_ok = 0, crc2_ok = 0;
  288. env_t *ep, *tmp_env1, *tmp_env2;
  289. tmp_env1 = (env_t *)malloc(CONFIG_ENV_SIZE);
  290. tmp_env2 = (env_t *)malloc(CONFIG_ENV_SIZE);
  291. if (tmp_env1 == NULL || tmp_env2 == NULL) {
  292. puts("Can't allocate buffers for environment\n");
  293. set_default_env("!malloc() failed");
  294. goto done;
  295. }
  296. if (readenv(CONFIG_ENV_OFFSET, (u_char *) tmp_env1))
  297. puts("No Valid Environment Area found\n");
  298. if (readenv(CONFIG_ENV_OFFSET_REDUND, (u_char *) tmp_env2))
  299. puts("No Valid Redundant Environment Area found\n");
  300. crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc;
  301. crc2_ok = crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc;
  302. if (!crc1_ok && !crc2_ok) {
  303. set_default_env("!bad CRC");
  304. goto done;
  305. } else if (crc1_ok && !crc2_ok) {
  306. gd->env_valid = 1;
  307. } else if (!crc1_ok && crc2_ok) {
  308. gd->env_valid = 2;
  309. } else {
  310. /* both ok - check serial */
  311. if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
  312. gd->env_valid = 2;
  313. else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
  314. gd->env_valid = 1;
  315. else if (tmp_env1->flags > tmp_env2->flags)
  316. gd->env_valid = 1;
  317. else if (tmp_env2->flags > tmp_env1->flags)
  318. gd->env_valid = 2;
  319. else /* flags are equal - almost impossible */
  320. gd->env_valid = 1;
  321. }
  322. free(env_ptr);
  323. if (gd->env_valid == 1)
  324. ep = tmp_env1;
  325. else
  326. ep = tmp_env2;
  327. env_flags = ep->flags;
  328. env_import((char *)ep, 0);
  329. done:
  330. free(tmp_env1);
  331. free(tmp_env2);
  332. #endif /* ! ENV_IS_EMBEDDED */
  333. }
  334. #else /* ! CONFIG_ENV_OFFSET_REDUND */
  335. /*
  336. * The legacy NAND code saved the environment in the first NAND
  337. * device i.e., nand_dev_desc + 0. This is also the behaviour using
  338. * the new NAND code.
  339. */
  340. void env_relocate_spec(void)
  341. {
  342. #if !defined(ENV_IS_EMBEDDED)
  343. int ret;
  344. char buf[CONFIG_ENV_SIZE];
  345. #if defined(CONFIG_ENV_OFFSET_OOB)
  346. ret = get_nand_env_oob(&nand_info[0], &nand_env_oob_offset);
  347. /*
  348. * If unable to read environment offset from NAND OOB then fall through
  349. * to the normal environment reading code below
  350. */
  351. if (!ret) {
  352. printf("Found Environment offset in OOB..\n");
  353. } else {
  354. set_default_env("!no env offset in OOB");
  355. return;
  356. }
  357. #endif
  358. ret = readenv(CONFIG_ENV_OFFSET, (u_char *)buf);
  359. if (ret) {
  360. set_default_env("!readenv() failed");
  361. return;
  362. }
  363. env_import(buf, 1);
  364. #endif /* ! ENV_IS_EMBEDDED */
  365. }
  366. #endif /* CONFIG_ENV_OFFSET_REDUND */