nand.c 9.1 KB

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