nand.c 8.9 KB

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