env_ubi.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * (c) Copyright 2012 by National Instruments,
  3. * Joe Hershberger <joe.hershberger@ni.com>
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <command.h>
  9. #include <environment.h>
  10. #include <errno.h>
  11. #include <malloc.h>
  12. #include <memalign.h>
  13. #include <search.h>
  14. #include <ubi_uboot.h>
  15. #undef crc32
  16. char *env_name_spec = "UBI";
  17. env_t *env_ptr;
  18. DECLARE_GLOBAL_DATA_PTR;
  19. int env_init(void)
  20. {
  21. /* use default */
  22. gd->env_addr = (ulong)&default_environment[0];
  23. gd->env_valid = 1;
  24. return 0;
  25. }
  26. #ifdef CONFIG_CMD_SAVEENV
  27. #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
  28. static unsigned char env_flags;
  29. int saveenv(void)
  30. {
  31. ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
  32. int ret;
  33. ret = env_export(env_new);
  34. if (ret)
  35. return ret;
  36. if (ubi_part(CONFIG_ENV_UBI_PART, NULL)) {
  37. printf("\n** Cannot find mtd partition \"%s\"\n",
  38. CONFIG_ENV_UBI_PART);
  39. return 1;
  40. }
  41. env_new->flags = ++env_flags; /* increase the serial */
  42. if (gd->env_valid == 1) {
  43. puts("Writing to redundant UBI... ");
  44. if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME_REDUND,
  45. (void *)env_new, CONFIG_ENV_SIZE)) {
  46. printf("\n** Unable to write env to %s:%s **\n",
  47. CONFIG_ENV_UBI_PART,
  48. CONFIG_ENV_UBI_VOLUME_REDUND);
  49. return 1;
  50. }
  51. } else {
  52. puts("Writing to UBI... ");
  53. if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME,
  54. (void *)env_new, CONFIG_ENV_SIZE)) {
  55. printf("\n** Unable to write env to %s:%s **\n",
  56. CONFIG_ENV_UBI_PART,
  57. CONFIG_ENV_UBI_VOLUME);
  58. return 1;
  59. }
  60. }
  61. puts("done\n");
  62. gd->env_valid = gd->env_valid == 2 ? 1 : 2;
  63. return 0;
  64. }
  65. #else /* ! CONFIG_SYS_REDUNDAND_ENVIRONMENT */
  66. int saveenv(void)
  67. {
  68. ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
  69. int ret;
  70. ret = env_export(env_new);
  71. if (ret)
  72. return ret;
  73. if (ubi_part(CONFIG_ENV_UBI_PART, NULL)) {
  74. printf("\n** Cannot find mtd partition \"%s\"\n",
  75. CONFIG_ENV_UBI_PART);
  76. return 1;
  77. }
  78. if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME, (void *)env_new,
  79. CONFIG_ENV_SIZE)) {
  80. printf("\n** Unable to write env to %s:%s **\n",
  81. CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME);
  82. return 1;
  83. }
  84. puts("done\n");
  85. return 0;
  86. }
  87. #endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */
  88. #endif /* CONFIG_CMD_SAVEENV */
  89. #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
  90. void env_relocate_spec(void)
  91. {
  92. ALLOC_CACHE_ALIGN_BUFFER(char, env1_buf, CONFIG_ENV_SIZE);
  93. ALLOC_CACHE_ALIGN_BUFFER(char, env2_buf, CONFIG_ENV_SIZE);
  94. int crc1_ok = 0, crc2_ok = 0;
  95. env_t *ep, *tmp_env1, *tmp_env2;
  96. /*
  97. * In case we have restarted u-boot there is a chance that buffer
  98. * contains old environment (from the previous boot).
  99. * If UBI volume is zero size, ubi_volume_read() doesn't modify the
  100. * buffer.
  101. * We need to clear buffer manually here, so the invalid CRC will
  102. * cause setting default environment as expected.
  103. */
  104. memset(env1_buf, 0x0, CONFIG_ENV_SIZE);
  105. memset(env2_buf, 0x0, CONFIG_ENV_SIZE);
  106. tmp_env1 = (env_t *)env1_buf;
  107. tmp_env2 = (env_t *)env2_buf;
  108. if (ubi_part(CONFIG_ENV_UBI_PART, NULL)) {
  109. printf("\n** Cannot find mtd partition \"%s\"\n",
  110. CONFIG_ENV_UBI_PART);
  111. set_default_env(NULL);
  112. return;
  113. }
  114. if (ubi_volume_read(CONFIG_ENV_UBI_VOLUME, (void *)tmp_env1,
  115. CONFIG_ENV_SIZE)) {
  116. printf("\n** Unable to read env from %s:%s **\n",
  117. CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME);
  118. }
  119. if (ubi_volume_read(CONFIG_ENV_UBI_VOLUME_REDUND, (void *)tmp_env2,
  120. CONFIG_ENV_SIZE)) {
  121. printf("\n** Unable to read redundant env from %s:%s **\n",
  122. CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME_REDUND);
  123. }
  124. crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc;
  125. crc2_ok = crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc;
  126. if (!crc1_ok && !crc2_ok) {
  127. set_default_env("!bad CRC");
  128. return;
  129. } else if (crc1_ok && !crc2_ok) {
  130. gd->env_valid = 1;
  131. } else if (!crc1_ok && crc2_ok) {
  132. gd->env_valid = 2;
  133. } else {
  134. /* both ok - check serial */
  135. if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
  136. gd->env_valid = 2;
  137. else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
  138. gd->env_valid = 1;
  139. else if (tmp_env1->flags > tmp_env2->flags)
  140. gd->env_valid = 1;
  141. else if (tmp_env2->flags > tmp_env1->flags)
  142. gd->env_valid = 2;
  143. else /* flags are equal - almost impossible */
  144. gd->env_valid = 1;
  145. }
  146. if (gd->env_valid == 1)
  147. ep = tmp_env1;
  148. else
  149. ep = tmp_env2;
  150. env_flags = ep->flags;
  151. env_import((char *)ep, 0);
  152. }
  153. #else /* ! CONFIG_SYS_REDUNDAND_ENVIRONMENT */
  154. void env_relocate_spec(void)
  155. {
  156. ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
  157. /*
  158. * In case we have restarted u-boot there is a chance that buffer
  159. * contains old environment (from the previous boot).
  160. * If UBI volume is zero size, ubi_volume_read() doesn't modify the
  161. * buffer.
  162. * We need to clear buffer manually here, so the invalid CRC will
  163. * cause setting default environment as expected.
  164. */
  165. memset(buf, 0x0, CONFIG_ENV_SIZE);
  166. if (ubi_part(CONFIG_ENV_UBI_PART, NULL)) {
  167. printf("\n** Cannot find mtd partition \"%s\"\n",
  168. CONFIG_ENV_UBI_PART);
  169. set_default_env(NULL);
  170. return;
  171. }
  172. if (ubi_volume_read(CONFIG_ENV_UBI_VOLUME, buf, CONFIG_ENV_SIZE)) {
  173. printf("\n** Unable to read env from %s:%s **\n",
  174. CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME);
  175. set_default_env(NULL);
  176. return;
  177. }
  178. env_import(buf, 1);
  179. }
  180. #endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */