env_nand.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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 <nand.h>
  22. #include <search.h>
  23. #include <errno.h>
  24. #if defined(CONFIG_CMD_SAVEENV) && defined(CONFIG_CMD_NAND)
  25. #define CMD_SAVEENV
  26. #elif defined(CONFIG_ENV_OFFSET_REDUND)
  27. #error CONFIG_ENV_OFFSET_REDUND must have CONFIG_CMD_SAVEENV & CONFIG_CMD_NAND
  28. #endif
  29. #if defined(CONFIG_ENV_SIZE_REDUND) && \
  30. (CONFIG_ENV_SIZE_REDUND != CONFIG_ENV_SIZE)
  31. #error CONFIG_ENV_SIZE_REDUND should be the same as CONFIG_ENV_SIZE
  32. #endif
  33. #ifndef CONFIG_ENV_RANGE
  34. #define CONFIG_ENV_RANGE CONFIG_ENV_SIZE
  35. #endif
  36. char *env_name_spec = "NAND";
  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. int env_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 = 0;
  72. return 0;
  73. } else if (crc1_ok && !crc2_ok) {
  74. gd->env_valid = 1;
  75. }
  76. #ifdef CONFIG_ENV_OFFSET_REDUND
  77. else if (!crc1_ok && crc2_ok) {
  78. gd->env_valid = 2;
  79. } else {
  80. /* both ok - check serial */
  81. if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
  82. gd->env_valid = 2;
  83. else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
  84. gd->env_valid = 1;
  85. else if (tmp_env1->flags > tmp_env2->flags)
  86. gd->env_valid = 1;
  87. else if (tmp_env2->flags > tmp_env1->flags)
  88. gd->env_valid = 2;
  89. else /* flags are equal - almost impossible */
  90. gd->env_valid = 1;
  91. }
  92. if (gd->env_valid == 2)
  93. env_ptr = tmp_env2;
  94. else
  95. #endif
  96. if (gd->env_valid == 1)
  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 = 1;
  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. u_char *char_ptr;
  116. blocksize = nand_info[0].erasesize;
  117. len = min(blocksize, (size_t)CONFIG_ENV_SIZE);
  118. while (amount_saved < CONFIG_ENV_SIZE && offset < end) {
  119. if (nand_block_isbad(&nand_info[0], offset)) {
  120. offset += blocksize;
  121. } else {
  122. char_ptr = &buf[amount_saved];
  123. if (nand_write(&nand_info[0], offset, &len, char_ptr))
  124. return 1;
  125. offset += blocksize;
  126. amount_saved += len;
  127. }
  128. }
  129. if (amount_saved != CONFIG_ENV_SIZE)
  130. return 1;
  131. return 0;
  132. }
  133. struct env_location {
  134. const char *name;
  135. const nand_erase_options_t erase_opts;
  136. };
  137. static int erase_and_write_env(const struct env_location *location,
  138. u_char *env_new)
  139. {
  140. int ret = 0;
  141. printf("Erasing %s...\n", location->name);
  142. if (nand_erase_opts(&nand_info[0], &location->erase_opts))
  143. return 1;
  144. printf("Writing to %s... ", location->name);
  145. ret = writeenv(location->erase_opts.offset, env_new);
  146. puts(ret ? "FAILED!\n" : "OK\n");
  147. return ret;
  148. }
  149. #ifdef CONFIG_ENV_OFFSET_REDUND
  150. static unsigned char env_flags;
  151. #endif
  152. int saveenv(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 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_new->flags = ++env_flags; /* increase the serial */
  182. env_idx = (gd->env_valid == 1);
  183. #endif
  184. ret = erase_and_write_env(&location[env_idx], (u_char *)env_new);
  185. #ifdef CONFIG_ENV_OFFSET_REDUND
  186. if (!ret) {
  187. /* preset other copy for next write */
  188. gd->env_valid = gd->env_valid == 2 ? 1 : 2;
  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. static int readenv(size_t offset, u_char *buf)
  201. {
  202. size_t end = offset + CONFIG_ENV_RANGE;
  203. size_t amount_loaded = 0;
  204. size_t blocksize, len;
  205. u_char *char_ptr;
  206. blocksize = nand_info[0].erasesize;
  207. if (!blocksize)
  208. return 1;
  209. len = min(blocksize, (size_t)CONFIG_ENV_SIZE);
  210. while (amount_loaded < CONFIG_ENV_SIZE && offset < end) {
  211. if (nand_block_isbad(&nand_info[0], offset)) {
  212. offset += blocksize;
  213. } else {
  214. char_ptr = &buf[amount_loaded];
  215. if (nand_read_skip_bad(&nand_info[0], offset,
  216. &len, NULL,
  217. nand_info[0].size, char_ptr))
  218. return 1;
  219. offset += blocksize;
  220. amount_loaded += len;
  221. }
  222. }
  223. if (amount_loaded != CONFIG_ENV_SIZE)
  224. return 1;
  225. return 0;
  226. }
  227. #ifdef CONFIG_ENV_OFFSET_OOB
  228. int get_nand_env_oob(nand_info_t *nand, unsigned long *result)
  229. {
  230. struct mtd_oob_ops ops;
  231. uint32_t oob_buf[ENV_OFFSET_SIZE / sizeof(uint32_t)];
  232. int ret;
  233. ops.datbuf = NULL;
  234. ops.mode = MTD_OOB_AUTO;
  235. ops.ooboffs = 0;
  236. ops.ooblen = ENV_OFFSET_SIZE;
  237. ops.oobbuf = (void *)oob_buf;
  238. ret = nand->read_oob(nand, ENV_OFFSET_SIZE, &ops);
  239. if (ret) {
  240. printf("error reading OOB block 0\n");
  241. return ret;
  242. }
  243. if (oob_buf[0] == ENV_OOB_MARKER) {
  244. *result = oob_buf[1] * nand->erasesize;
  245. } else if (oob_buf[0] == ENV_OOB_MARKER_OLD) {
  246. *result = oob_buf[1];
  247. } else {
  248. printf("No dynamic environment marker in OOB block 0\n");
  249. return -ENOENT;
  250. }
  251. return 0;
  252. }
  253. #endif
  254. #ifdef CONFIG_ENV_OFFSET_REDUND
  255. void env_relocate_spec(void)
  256. {
  257. #if !defined(ENV_IS_EMBEDDED)
  258. int read1_fail = 0, read2_fail = 0;
  259. int crc1_ok = 0, crc2_ok = 0;
  260. env_t *ep, *tmp_env1, *tmp_env2;
  261. tmp_env1 = (env_t *)malloc(CONFIG_ENV_SIZE);
  262. tmp_env2 = (env_t *)malloc(CONFIG_ENV_SIZE);
  263. if (tmp_env1 == NULL || tmp_env2 == NULL) {
  264. puts("Can't allocate buffers for environment\n");
  265. set_default_env("!malloc() failed");
  266. goto done;
  267. }
  268. read1_fail = readenv(CONFIG_ENV_OFFSET, (u_char *) tmp_env1);
  269. read2_fail = readenv(CONFIG_ENV_OFFSET_REDUND, (u_char *) tmp_env2);
  270. if (read1_fail && read2_fail)
  271. puts("*** Error - No Valid Environment Area found\n");
  272. else if (read1_fail || read2_fail)
  273. puts("*** Warning - some problems detected "
  274. "reading environment; recovered successfully\n");
  275. crc1_ok = !read1_fail &&
  276. (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc);
  277. crc2_ok = !read2_fail &&
  278. (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc);
  279. if (!crc1_ok && !crc2_ok) {
  280. set_default_env("!bad CRC");
  281. goto done;
  282. } else if (crc1_ok && !crc2_ok) {
  283. gd->env_valid = 1;
  284. } else if (!crc1_ok && crc2_ok) {
  285. gd->env_valid = 2;
  286. } else {
  287. /* both ok - check serial */
  288. if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
  289. gd->env_valid = 2;
  290. else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
  291. gd->env_valid = 1;
  292. else if (tmp_env1->flags > tmp_env2->flags)
  293. gd->env_valid = 1;
  294. else if (tmp_env2->flags > tmp_env1->flags)
  295. gd->env_valid = 2;
  296. else /* flags are equal - almost impossible */
  297. gd->env_valid = 1;
  298. }
  299. free(env_ptr);
  300. if (gd->env_valid == 1)
  301. ep = tmp_env1;
  302. else
  303. ep = tmp_env2;
  304. env_flags = ep->flags;
  305. env_import((char *)ep, 0);
  306. done:
  307. free(tmp_env1);
  308. free(tmp_env2);
  309. #endif /* ! ENV_IS_EMBEDDED */
  310. }
  311. #else /* ! CONFIG_ENV_OFFSET_REDUND */
  312. /*
  313. * The legacy NAND code saved the environment in the first NAND
  314. * device i.e., nand_dev_desc + 0. This is also the behaviour using
  315. * the new NAND code.
  316. */
  317. void env_relocate_spec(void)
  318. {
  319. #if !defined(ENV_IS_EMBEDDED)
  320. int ret;
  321. ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
  322. #if defined(CONFIG_ENV_OFFSET_OOB)
  323. ret = get_nand_env_oob(&nand_info[0], &nand_env_oob_offset);
  324. /*
  325. * If unable to read environment offset from NAND OOB then fall through
  326. * to the normal environment reading code below
  327. */
  328. if (!ret) {
  329. printf("Found Environment offset in OOB..\n");
  330. } else {
  331. set_default_env("!no env offset in OOB");
  332. return;
  333. }
  334. #endif
  335. ret = readenv(CONFIG_ENV_OFFSET, (u_char *)buf);
  336. if (ret) {
  337. set_default_env("!readenv() failed");
  338. return;
  339. }
  340. env_import(buf, 1);
  341. #endif /* ! ENV_IS_EMBEDDED */
  342. }
  343. #endif /* CONFIG_ENV_OFFSET_REDUND */