nand.c 8.8 KB

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