nand.c 9.0 KB

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