ubi.c 5.8 KB

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