nand.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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. * SPDX-License-Identifier: GPL-2.0+
  15. */
  16. #include <common.h>
  17. #include <command.h>
  18. #include <environment.h>
  19. #include <linux/stddef.h>
  20. #include <malloc.h>
  21. #include <memalign.h>
  22. #include <nand.h>
  23. #include <search.h>
  24. #include <errno.h>
  25. #if defined(CONFIG_CMD_SAVEENV) && defined(CONFIG_CMD_NAND) && \
  26. !defined(CONFIG_SPL_BUILD)
  27. #define CMD_SAVEENV
  28. #elif defined(CONFIG_ENV_OFFSET_REDUND)
  29. #error CONFIG_ENV_OFFSET_REDUND must have CONFIG_CMD_SAVEENV & CONFIG_CMD_NAND
  30. #endif
  31. #if defined(CONFIG_ENV_SIZE_REDUND) && \
  32. (CONFIG_ENV_SIZE_REDUND != CONFIG_ENV_SIZE)
  33. #error CONFIG_ENV_SIZE_REDUND should be the same as CONFIG_ENV_SIZE
  34. #endif
  35. #ifndef CONFIG_ENV_RANGE
  36. #define CONFIG_ENV_RANGE CONFIG_ENV_SIZE
  37. #endif
  38. char *env_name_spec = "NAND";
  39. #if defined(ENV_IS_EMBEDDED)
  40. env_t *env_ptr = &environment;
  41. #elif defined(CONFIG_NAND_ENV_DST)
  42. env_t *env_ptr = (env_t *)CONFIG_NAND_ENV_DST;
  43. #else /* ! ENV_IS_EMBEDDED */
  44. env_t *env_ptr;
  45. #endif /* ENV_IS_EMBEDDED */
  46. DECLARE_GLOBAL_DATA_PTR;
  47. /*
  48. * This is called before nand_init() so we can't read NAND to
  49. * validate env data.
  50. *
  51. * Mark it OK for now. env_relocate() in env_common.c will call our
  52. * relocate function which does the real validation.
  53. *
  54. * When using a NAND boot image (like sequoia_nand), the environment
  55. * can be embedded or attached to the U-Boot image in NAND flash.
  56. * This way the SPL loads not only the U-Boot image from NAND but
  57. * also the environment.
  58. */
  59. static int env_nand_init(void)
  60. {
  61. #if defined(ENV_IS_EMBEDDED) || defined(CONFIG_NAND_ENV_DST)
  62. int crc1_ok = 0, crc2_ok = 0;
  63. env_t *tmp_env1;
  64. #ifdef CONFIG_ENV_OFFSET_REDUND
  65. env_t *tmp_env2;
  66. tmp_env2 = (env_t *)((ulong)env_ptr + CONFIG_ENV_SIZE);
  67. crc2_ok = crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc;
  68. #endif
  69. tmp_env1 = env_ptr;
  70. crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc;
  71. if (!crc1_ok && !crc2_ok) {
  72. gd->env_addr = 0;
  73. gd->env_valid = 0;
  74. return 0;
  75. } else if (crc1_ok && !crc2_ok) {
  76. gd->env_valid = ENV_VALID;
  77. }
  78. #ifdef CONFIG_ENV_OFFSET_REDUND
  79. else if (!crc1_ok && crc2_ok) {
  80. gd->env_valid = ENV_REDUND;
  81. } else {
  82. /* both ok - check serial */
  83. if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
  84. gd->env_valid = ENV_REDUND;
  85. else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
  86. gd->env_valid = ENV_VALID;
  87. else if (tmp_env1->flags > tmp_env2->flags)
  88. gd->env_valid = ENV_VALID;
  89. else if (tmp_env2->flags > tmp_env1->flags)
  90. gd->env_valid = ENV_REDUND;
  91. else /* flags are equal - almost impossible */
  92. gd->env_valid = ENV_VALID;
  93. }
  94. if (gd->env_valid == ENV_REDUND)
  95. env_ptr = tmp_env2;
  96. else
  97. #endif
  98. if (gd->env_valid == ENV_VALID)
  99. env_ptr = tmp_env1;
  100. gd->env_addr = (ulong)env_ptr->data;
  101. #else /* ENV_IS_EMBEDDED || CONFIG_NAND_ENV_DST */
  102. gd->env_addr = (ulong)&default_environment[0];
  103. gd->env_valid = ENV_VALID;
  104. #endif /* ENV_IS_EMBEDDED || CONFIG_NAND_ENV_DST */
  105. return 0;
  106. }
  107. #ifdef CMD_SAVEENV
  108. /*
  109. * The legacy NAND code saved the environment in the first NAND device i.e.,
  110. * nand_dev_desc + 0. This is also the behaviour using the new NAND code.
  111. */
  112. static int writeenv(size_t offset, u_char *buf)
  113. {
  114. size_t end = offset + CONFIG_ENV_RANGE;
  115. size_t amount_saved = 0;
  116. size_t blocksize, len;
  117. struct mtd_info *mtd;
  118. u_char *char_ptr;
  119. mtd = get_nand_dev_by_index(0);
  120. if (!mtd)
  121. return 1;
  122. blocksize = mtd->erasesize;
  123. len = min(blocksize, (size_t)CONFIG_ENV_SIZE);
  124. while (amount_saved < CONFIG_ENV_SIZE && offset < end) {
  125. if (nand_block_isbad(mtd, offset)) {
  126. offset += blocksize;
  127. } else {
  128. char_ptr = &buf[amount_saved];
  129. if (nand_write(mtd, offset, &len, char_ptr))
  130. return 1;
  131. offset += blocksize;
  132. amount_saved += len;
  133. }
  134. }
  135. if (amount_saved != CONFIG_ENV_SIZE)
  136. return 1;
  137. return 0;
  138. }
  139. struct nand_env_location {
  140. const char *name;
  141. const nand_erase_options_t erase_opts;
  142. };
  143. static int erase_and_write_env(const struct nand_env_location *location,
  144. u_char *env_new)
  145. {
  146. struct mtd_info *mtd;
  147. int ret = 0;
  148. mtd = get_nand_dev_by_index(0);
  149. if (!mtd)
  150. return 1;
  151. printf("Erasing %s...\n", location->name);
  152. if (nand_erase_opts(mtd, &location->erase_opts))
  153. return 1;
  154. printf("Writing to %s... ", location->name);
  155. ret = writeenv(location->erase_opts.offset, env_new);
  156. puts(ret ? "FAILED!\n" : "OK\n");
  157. return ret;
  158. }
  159. static int env_nand_save(void)
  160. {
  161. int ret = 0;
  162. ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
  163. int env_idx = 0;
  164. static const struct nand_env_location location[] = {
  165. {
  166. .name = "NAND",
  167. .erase_opts = {
  168. .length = CONFIG_ENV_RANGE,
  169. .offset = CONFIG_ENV_OFFSET,
  170. },
  171. },
  172. #ifdef CONFIG_ENV_OFFSET_REDUND
  173. {
  174. .name = "redundant NAND",
  175. .erase_opts = {
  176. .length = CONFIG_ENV_RANGE,
  177. .offset = CONFIG_ENV_OFFSET_REDUND,
  178. },
  179. },
  180. #endif
  181. };
  182. if (CONFIG_ENV_RANGE < CONFIG_ENV_SIZE)
  183. return 1;
  184. ret = env_export(env_new);
  185. if (ret)
  186. return ret;
  187. #ifdef CONFIG_ENV_OFFSET_REDUND
  188. env_idx = (gd->env_valid == ENV_VALID);
  189. #endif
  190. ret = erase_and_write_env(&location[env_idx], (u_char *)env_new);
  191. #ifdef CONFIG_ENV_OFFSET_REDUND
  192. if (!ret) {
  193. /* preset other copy for next write */
  194. gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID :
  195. ENV_REDUND;
  196. return ret;
  197. }
  198. env_idx = (env_idx + 1) & 1;
  199. ret = erase_and_write_env(&location[env_idx], (u_char *)env_new);
  200. if (!ret)
  201. printf("Warning: primary env write failed,"
  202. " redundancy is lost!\n");
  203. #endif
  204. return ret;
  205. }
  206. #endif /* CMD_SAVEENV */
  207. #if defined(CONFIG_SPL_BUILD)
  208. static int readenv(size_t offset, u_char *buf)
  209. {
  210. return nand_spl_load_image(offset, CONFIG_ENV_SIZE, buf);
  211. }
  212. #else
  213. static int readenv(size_t offset, u_char *buf)
  214. {
  215. size_t end = offset + CONFIG_ENV_RANGE;
  216. size_t amount_loaded = 0;
  217. size_t blocksize, len;
  218. struct mtd_info *mtd;
  219. u_char *char_ptr;
  220. mtd = get_nand_dev_by_index(0);
  221. if (!mtd)
  222. return 1;
  223. blocksize = mtd->erasesize;
  224. len = min(blocksize, (size_t)CONFIG_ENV_SIZE);
  225. while (amount_loaded < CONFIG_ENV_SIZE && offset < end) {
  226. if (nand_block_isbad(mtd, offset)) {
  227. offset += blocksize;
  228. } else {
  229. char_ptr = &buf[amount_loaded];
  230. if (nand_read_skip_bad(mtd, offset,
  231. &len, NULL,
  232. mtd->size, 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. #endif /* #if defined(CONFIG_SPL_BUILD) */
  243. #ifdef CONFIG_ENV_OFFSET_OOB
  244. int get_nand_env_oob(struct mtd_info *mtd, unsigned long *result)
  245. {
  246. struct mtd_oob_ops ops;
  247. uint32_t oob_buf[ENV_OFFSET_SIZE / sizeof(uint32_t)];
  248. int ret;
  249. ops.datbuf = NULL;
  250. ops.mode = MTD_OOB_AUTO;
  251. ops.ooboffs = 0;
  252. ops.ooblen = ENV_OFFSET_SIZE;
  253. ops.oobbuf = (void *)oob_buf;
  254. ret = mtd->read_oob(mtd, ENV_OFFSET_SIZE, &ops);
  255. if (ret) {
  256. printf("error reading OOB block 0\n");
  257. return ret;
  258. }
  259. if (oob_buf[0] == ENV_OOB_MARKER) {
  260. *result = oob_buf[1] * mtd->erasesize;
  261. } else if (oob_buf[0] == ENV_OOB_MARKER_OLD) {
  262. *result = oob_buf[1];
  263. } else {
  264. printf("No dynamic environment marker in OOB block 0\n");
  265. return -ENOENT;
  266. }
  267. return 0;
  268. }
  269. #endif
  270. #ifdef CONFIG_ENV_OFFSET_REDUND
  271. static void env_nand_load(void)
  272. {
  273. #if !defined(ENV_IS_EMBEDDED)
  274. int read1_fail = 0, read2_fail = 0;
  275. env_t *tmp_env1, *tmp_env2;
  276. tmp_env1 = (env_t *)malloc(CONFIG_ENV_SIZE);
  277. tmp_env2 = (env_t *)malloc(CONFIG_ENV_SIZE);
  278. if (tmp_env1 == NULL || tmp_env2 == NULL) {
  279. puts("Can't allocate buffers for environment\n");
  280. set_default_env("!malloc() failed");
  281. goto done;
  282. }
  283. read1_fail = readenv(CONFIG_ENV_OFFSET, (u_char *) tmp_env1);
  284. read2_fail = readenv(CONFIG_ENV_OFFSET_REDUND, (u_char *) tmp_env2);
  285. if (read1_fail && read2_fail)
  286. puts("*** Error - No Valid Environment Area found\n");
  287. else if (read1_fail || read2_fail)
  288. puts("*** Warning - some problems detected "
  289. "reading environment; recovered successfully\n");
  290. if (read1_fail && read2_fail) {
  291. set_default_env("!bad env area");
  292. goto done;
  293. } else if (!read1_fail && read2_fail) {
  294. gd->env_valid = ENV_VALID;
  295. env_import((char *)tmp_env1, 1);
  296. } else if (read1_fail && !read2_fail) {
  297. gd->env_valid = ENV_REDUND;
  298. env_import((char *)tmp_env2, 1);
  299. } else {
  300. env_import_redund((char *)tmp_env1, (char *)tmp_env2);
  301. }
  302. done:
  303. free(tmp_env1);
  304. free(tmp_env2);
  305. #endif /* ! ENV_IS_EMBEDDED */
  306. }
  307. #else /* ! CONFIG_ENV_OFFSET_REDUND */
  308. /*
  309. * The legacy NAND code saved the environment in the first NAND
  310. * device i.e., nand_dev_desc + 0. This is also the behaviour using
  311. * the new NAND code.
  312. */
  313. static void env_nand_load(void)
  314. {
  315. #if !defined(ENV_IS_EMBEDDED)
  316. int ret;
  317. ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
  318. #if defined(CONFIG_ENV_OFFSET_OOB)
  319. struct mtd_info *mtd = get_nand_dev_by_index(0);
  320. /*
  321. * If unable to read environment offset from NAND OOB then fall through
  322. * to the normal environment reading code below
  323. */
  324. if (mtd && !get_nand_env_oob(mtd, &nand_env_oob_offset)) {
  325. printf("Found Environment offset in OOB..\n");
  326. } else {
  327. set_default_env("!no env offset in OOB");
  328. return;
  329. }
  330. #endif
  331. ret = readenv(CONFIG_ENV_OFFSET, (u_char *)buf);
  332. if (ret) {
  333. set_default_env("!readenv() failed");
  334. return;
  335. }
  336. env_import(buf, 1);
  337. #endif /* ! ENV_IS_EMBEDDED */
  338. }
  339. #endif /* CONFIG_ENV_OFFSET_REDUND */
  340. U_BOOT_ENV_LOCATION(nand) = {
  341. .location = ENVL_NAND,
  342. .load = env_nand_load,
  343. #if defined(CMD_SAVEENV)
  344. .save = env_save_ptr(env_nand_save),
  345. #endif
  346. .init = env_nand_init,
  347. };