nand.c 9.0 KB

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