env_nand.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /*
  2. * (C) Copyright 2000-2010
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * (C) Copyright 2008
  6. * Stuart Wood, Lab X Technologies <stuart.wood@labxtechnologies.com>
  7. *
  8. * (C) Copyright 2004
  9. * Jian Zhang, Texas Instruments, jzhang@ti.com.
  10. *
  11. * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  12. * Andreas Heppel <aheppel@sysgo.de>
  13. *
  14. * SPDX-License-Identifier: GPL-2.0+
  15. */
  16. #include <common.h>
  17. #include <command.h>
  18. #include <environment.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. #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. char *env_name_spec = "NAND";
  38. #if defined(ENV_IS_EMBEDDED)
  39. env_t *env_ptr = &environment;
  40. #elif defined(CONFIG_NAND_ENV_DST)
  41. env_t *env_ptr = (env_t *)CONFIG_NAND_ENV_DST;
  42. #else /* ! ENV_IS_EMBEDDED */
  43. env_t *env_ptr;
  44. #endif /* ENV_IS_EMBEDDED */
  45. DECLARE_GLOBAL_DATA_PTR;
  46. /*
  47. * This is called before nand_init() so we can't read NAND to
  48. * validate env data.
  49. *
  50. * Mark it OK for now. env_relocate() in env_common.c will call our
  51. * relocate function which does the real validation.
  52. *
  53. * When using a NAND boot image (like sequoia_nand), the environment
  54. * can be embedded or attached to the U-Boot image in NAND flash.
  55. * This way the SPL loads not only the U-Boot image from NAND but
  56. * also the environment.
  57. */
  58. int env_init(void)
  59. {
  60. #if defined(ENV_IS_EMBEDDED) || defined(CONFIG_NAND_ENV_DST)
  61. int crc1_ok = 0, crc2_ok = 0;
  62. env_t *tmp_env1;
  63. #ifdef CONFIG_ENV_OFFSET_REDUND
  64. env_t *tmp_env2;
  65. tmp_env2 = (env_t *)((ulong)env_ptr + CONFIG_ENV_SIZE);
  66. crc2_ok = crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc;
  67. #endif
  68. tmp_env1 = env_ptr;
  69. crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc;
  70. if (!crc1_ok && !crc2_ok) {
  71. gd->env_addr = 0;
  72. gd->env_valid = 0;
  73. return 0;
  74. } else if (crc1_ok && !crc2_ok) {
  75. gd->env_valid = 1;
  76. }
  77. #ifdef CONFIG_ENV_OFFSET_REDUND
  78. else if (!crc1_ok && crc2_ok) {
  79. gd->env_valid = 2;
  80. } else {
  81. /* both ok - check serial */
  82. if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
  83. gd->env_valid = 2;
  84. else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
  85. gd->env_valid = 1;
  86. else if (tmp_env1->flags > tmp_env2->flags)
  87. gd->env_valid = 1;
  88. else if (tmp_env2->flags > tmp_env1->flags)
  89. gd->env_valid = 2;
  90. else /* flags are equal - almost impossible */
  91. gd->env_valid = 1;
  92. }
  93. if (gd->env_valid == 2)
  94. env_ptr = tmp_env2;
  95. else
  96. #endif
  97. if (gd->env_valid == 1)
  98. env_ptr = tmp_env1;
  99. gd->env_addr = (ulong)env_ptr->data;
  100. #else /* ENV_IS_EMBEDDED || CONFIG_NAND_ENV_DST */
  101. gd->env_addr = (ulong)&default_environment[0];
  102. gd->env_valid = 1;
  103. #endif /* ENV_IS_EMBEDDED || CONFIG_NAND_ENV_DST */
  104. return 0;
  105. }
  106. #ifdef CMD_SAVEENV
  107. /*
  108. * The legacy NAND code saved the environment in the first NAND device i.e.,
  109. * nand_dev_desc + 0. This is also the behaviour using the new NAND code.
  110. */
  111. static int writeenv(size_t offset, u_char *buf)
  112. {
  113. size_t end = offset + CONFIG_ENV_RANGE;
  114. size_t amount_saved = 0;
  115. size_t blocksize, len;
  116. struct mtd_info *mtd;
  117. u_char *char_ptr;
  118. mtd = get_nand_dev_by_index(0);
  119. if (!mtd)
  120. return 1;
  121. blocksize = mtd->erasesize;
  122. len = min(blocksize, (size_t)CONFIG_ENV_SIZE);
  123. while (amount_saved < CONFIG_ENV_SIZE && offset < end) {
  124. if (nand_block_isbad(mtd, offset)) {
  125. offset += blocksize;
  126. } else {
  127. char_ptr = &buf[amount_saved];
  128. if (nand_write(mtd, offset, &len, char_ptr))
  129. return 1;
  130. offset += blocksize;
  131. amount_saved += len;
  132. }
  133. }
  134. if (amount_saved != CONFIG_ENV_SIZE)
  135. return 1;
  136. return 0;
  137. }
  138. struct env_location {
  139. const char *name;
  140. const nand_erase_options_t erase_opts;
  141. };
  142. static int erase_and_write_env(const struct env_location *location,
  143. u_char *env_new)
  144. {
  145. struct mtd_info *mtd;
  146. int ret = 0;
  147. mtd = get_nand_dev_by_index(0);
  148. if (!mtd)
  149. return 1;
  150. printf("Erasing %s...\n", location->name);
  151. if (nand_erase_opts(mtd, &location->erase_opts))
  152. return 1;
  153. printf("Writing to %s... ", location->name);
  154. ret = writeenv(location->erase_opts.offset, env_new);
  155. puts(ret ? "FAILED!\n" : "OK\n");
  156. return ret;
  157. }
  158. #ifdef CONFIG_ENV_OFFSET_REDUND
  159. static unsigned char env_flags;
  160. #endif
  161. int saveenv(void)
  162. {
  163. int ret = 0;
  164. ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
  165. int env_idx = 0;
  166. static const struct env_location location[] = {
  167. {
  168. .name = "NAND",
  169. .erase_opts = {
  170. .length = CONFIG_ENV_RANGE,
  171. .offset = CONFIG_ENV_OFFSET,
  172. },
  173. },
  174. #ifdef CONFIG_ENV_OFFSET_REDUND
  175. {
  176. .name = "redundant NAND",
  177. .erase_opts = {
  178. .length = CONFIG_ENV_RANGE,
  179. .offset = CONFIG_ENV_OFFSET_REDUND,
  180. },
  181. },
  182. #endif
  183. };
  184. if (CONFIG_ENV_RANGE < CONFIG_ENV_SIZE)
  185. return 1;
  186. ret = env_export(env_new);
  187. if (ret)
  188. return ret;
  189. #ifdef CONFIG_ENV_OFFSET_REDUND
  190. env_new->flags = ++env_flags; /* increase the serial */
  191. env_idx = (gd->env_valid == 1);
  192. #endif
  193. ret = erase_and_write_env(&location[env_idx], (u_char *)env_new);
  194. #ifdef CONFIG_ENV_OFFSET_REDUND
  195. if (!ret) {
  196. /* preset other copy for next write */
  197. gd->env_valid = gd->env_valid == 2 ? 1 : 2;
  198. return ret;
  199. }
  200. env_idx = (env_idx + 1) & 1;
  201. ret = erase_and_write_env(&location[env_idx], (u_char *)env_new);
  202. if (!ret)
  203. printf("Warning: primary env write failed,"
  204. " redundancy is lost!\n");
  205. #endif
  206. return ret;
  207. }
  208. #endif /* CMD_SAVEENV */
  209. #if defined(CONFIG_SPL_BUILD)
  210. static int readenv(size_t offset, u_char *buf)
  211. {
  212. return nand_spl_load_image(offset, CONFIG_ENV_SIZE, buf);
  213. }
  214. #else
  215. static int readenv(size_t offset, u_char *buf)
  216. {
  217. size_t end = offset + CONFIG_ENV_RANGE;
  218. size_t amount_loaded = 0;
  219. size_t blocksize, len;
  220. struct mtd_info *mtd;
  221. u_char *char_ptr;
  222. mtd = get_nand_dev_by_index(0);
  223. if (!mtd)
  224. return 1;
  225. blocksize = mtd->erasesize;
  226. len = min(blocksize, (size_t)CONFIG_ENV_SIZE);
  227. while (amount_loaded < CONFIG_ENV_SIZE && offset < end) {
  228. if (nand_block_isbad(mtd, offset)) {
  229. offset += blocksize;
  230. } else {
  231. char_ptr = &buf[amount_loaded];
  232. if (nand_read_skip_bad(mtd, offset,
  233. &len, NULL,
  234. mtd->size, char_ptr))
  235. return 1;
  236. offset += blocksize;
  237. amount_loaded += len;
  238. }
  239. }
  240. if (amount_loaded != CONFIG_ENV_SIZE)
  241. return 1;
  242. return 0;
  243. }
  244. #endif /* #if defined(CONFIG_SPL_BUILD) */
  245. #ifdef CONFIG_ENV_OFFSET_OOB
  246. int get_nand_env_oob(struct mtd_info *mtd, unsigned long *result)
  247. {
  248. struct mtd_oob_ops ops;
  249. uint32_t oob_buf[ENV_OFFSET_SIZE / sizeof(uint32_t)];
  250. int ret;
  251. ops.datbuf = NULL;
  252. ops.mode = MTD_OOB_AUTO;
  253. ops.ooboffs = 0;
  254. ops.ooblen = ENV_OFFSET_SIZE;
  255. ops.oobbuf = (void *)oob_buf;
  256. ret = mtd->read_oob(mtd, ENV_OFFSET_SIZE, &ops);
  257. if (ret) {
  258. printf("error reading OOB block 0\n");
  259. return ret;
  260. }
  261. if (oob_buf[0] == ENV_OOB_MARKER) {
  262. *result = oob_buf[1] * mtd->erasesize;
  263. } else if (oob_buf[0] == ENV_OOB_MARKER_OLD) {
  264. *result = oob_buf[1];
  265. } else {
  266. printf("No dynamic environment marker in OOB block 0\n");
  267. return -ENOENT;
  268. }
  269. return 0;
  270. }
  271. #endif
  272. #ifdef CONFIG_ENV_OFFSET_REDUND
  273. void env_relocate_spec(void)
  274. {
  275. #if !defined(ENV_IS_EMBEDDED)
  276. int read1_fail = 0, read2_fail = 0;
  277. int crc1_ok = 0, crc2_ok = 0;
  278. env_t *ep, *tmp_env1, *tmp_env2;
  279. tmp_env1 = (env_t *)malloc(CONFIG_ENV_SIZE);
  280. tmp_env2 = (env_t *)malloc(CONFIG_ENV_SIZE);
  281. if (tmp_env1 == NULL || tmp_env2 == NULL) {
  282. puts("Can't allocate buffers for environment\n");
  283. set_default_env("!malloc() failed");
  284. goto done;
  285. }
  286. read1_fail = readenv(CONFIG_ENV_OFFSET, (u_char *) tmp_env1);
  287. read2_fail = readenv(CONFIG_ENV_OFFSET_REDUND, (u_char *) tmp_env2);
  288. if (read1_fail && read2_fail)
  289. puts("*** Error - No Valid Environment Area found\n");
  290. else if (read1_fail || read2_fail)
  291. puts("*** Warning - some problems detected "
  292. "reading environment; recovered successfully\n");
  293. crc1_ok = !read1_fail &&
  294. (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc);
  295. crc2_ok = !read2_fail &&
  296. (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc);
  297. if (!crc1_ok && !crc2_ok) {
  298. set_default_env("!bad CRC");
  299. goto done;
  300. } else if (crc1_ok && !crc2_ok) {
  301. gd->env_valid = 1;
  302. } else if (!crc1_ok && crc2_ok) {
  303. gd->env_valid = 2;
  304. } else {
  305. /* both ok - check serial */
  306. if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
  307. gd->env_valid = 2;
  308. else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
  309. gd->env_valid = 1;
  310. else if (tmp_env1->flags > tmp_env2->flags)
  311. gd->env_valid = 1;
  312. else if (tmp_env2->flags > tmp_env1->flags)
  313. gd->env_valid = 2;
  314. else /* flags are equal - almost impossible */
  315. gd->env_valid = 1;
  316. }
  317. free(env_ptr);
  318. if (gd->env_valid == 1)
  319. ep = tmp_env1;
  320. else
  321. ep = tmp_env2;
  322. env_flags = ep->flags;
  323. env_import((char *)ep, 0);
  324. done:
  325. free(tmp_env1);
  326. free(tmp_env2);
  327. #endif /* ! ENV_IS_EMBEDDED */
  328. }
  329. #else /* ! CONFIG_ENV_OFFSET_REDUND */
  330. /*
  331. * The legacy NAND code saved the environment in the first NAND
  332. * device i.e., nand_dev_desc + 0. This is also the behaviour using
  333. * the new NAND code.
  334. */
  335. void env_relocate_spec(void)
  336. {
  337. #if !defined(ENV_IS_EMBEDDED)
  338. int ret;
  339. ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
  340. #if defined(CONFIG_ENV_OFFSET_OOB)
  341. struct mtd_info *mtd = get_nand_dev_by_index(0);
  342. /*
  343. * If unable to read environment offset from NAND OOB then fall through
  344. * to the normal environment reading code below
  345. */
  346. if (mtd && !get_nand_env_oob(mtd, &nand_env_oob_offset)) {
  347. printf("Found Environment offset in OOB..\n");
  348. } else {
  349. set_default_env("!no env offset in OOB");
  350. return;
  351. }
  352. #endif
  353. ret = readenv(CONFIG_ENV_OFFSET, (u_char *)buf);
  354. if (ret) {
  355. set_default_env("!readenv() failed");
  356. return;
  357. }
  358. env_import(buf, 1);
  359. #endif /* ! ENV_IS_EMBEDDED */
  360. }
  361. #endif /* CONFIG_ENV_OFFSET_REDUND */