nand.c 9.1 KB

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