ubi.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. #ifdef CONFIG_CMD_SAVEENV
  26. #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
  27. static int env_ubi_save(void)
  28. {
  29. ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
  30. int ret;
  31. ret = env_export(env_new);
  32. if (ret)
  33. return ret;
  34. if (ubi_part(CONFIG_ENV_UBI_PART, UBI_VID_OFFSET)) {
  35. printf("\n** Cannot find mtd partition \"%s\"\n",
  36. CONFIG_ENV_UBI_PART);
  37. return 1;
  38. }
  39. if (gd->env_valid == ENV_VALID) {
  40. puts("Writing to redundant UBI... ");
  41. if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME_REDUND,
  42. (void *)env_new, CONFIG_ENV_SIZE)) {
  43. printf("\n** Unable to write env to %s:%s **\n",
  44. CONFIG_ENV_UBI_PART,
  45. CONFIG_ENV_UBI_VOLUME_REDUND);
  46. return 1;
  47. }
  48. } else {
  49. puts("Writing to UBI... ");
  50. if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME,
  51. (void *)env_new, CONFIG_ENV_SIZE)) {
  52. printf("\n** Unable to write env to %s:%s **\n",
  53. CONFIG_ENV_UBI_PART,
  54. CONFIG_ENV_UBI_VOLUME);
  55. return 1;
  56. }
  57. }
  58. puts("done\n");
  59. gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
  60. return 0;
  61. }
  62. #else /* ! CONFIG_SYS_REDUNDAND_ENVIRONMENT */
  63. static int env_ubi_save(void)
  64. {
  65. ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
  66. int ret;
  67. ret = env_export(env_new);
  68. if (ret)
  69. return ret;
  70. if (ubi_part(CONFIG_ENV_UBI_PART, UBI_VID_OFFSET)) {
  71. printf("\n** Cannot find mtd partition \"%s\"\n",
  72. CONFIG_ENV_UBI_PART);
  73. return 1;
  74. }
  75. if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME, (void *)env_new,
  76. CONFIG_ENV_SIZE)) {
  77. printf("\n** Unable to write env to %s:%s **\n",
  78. CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME);
  79. return 1;
  80. }
  81. puts("done\n");
  82. return 0;
  83. }
  84. #endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */
  85. #endif /* CONFIG_CMD_SAVEENV */
  86. #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
  87. static int env_ubi_load(void)
  88. {
  89. ALLOC_CACHE_ALIGN_BUFFER(char, env1_buf, CONFIG_ENV_SIZE);
  90. ALLOC_CACHE_ALIGN_BUFFER(char, env2_buf, CONFIG_ENV_SIZE);
  91. int read1_fail, read2_fail;
  92. env_t *tmp_env1, *tmp_env2;
  93. /*
  94. * In case we have restarted u-boot there is a chance that buffer
  95. * contains old environment (from the previous boot).
  96. * If UBI volume is zero size, ubi_volume_read() doesn't modify the
  97. * buffer.
  98. * We need to clear buffer manually here, so the invalid CRC will
  99. * cause setting default environment as expected.
  100. */
  101. memset(env1_buf, 0x0, CONFIG_ENV_SIZE);
  102. memset(env2_buf, 0x0, CONFIG_ENV_SIZE);
  103. tmp_env1 = (env_t *)env1_buf;
  104. tmp_env2 = (env_t *)env2_buf;
  105. if (ubi_part(CONFIG_ENV_UBI_PART, UBI_VID_OFFSET)) {
  106. printf("\n** Cannot find mtd partition \"%s\"\n",
  107. CONFIG_ENV_UBI_PART);
  108. env_set_default(NULL, 0);
  109. return -EIO;
  110. }
  111. read1_fail = ubi_volume_read(CONFIG_ENV_UBI_VOLUME, (void *)tmp_env1,
  112. CONFIG_ENV_SIZE);
  113. if (read1_fail)
  114. printf("\n** Unable to read env from %s:%s **\n",
  115. CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME);
  116. read2_fail = ubi_volume_read(CONFIG_ENV_UBI_VOLUME_REDUND,
  117. (void *)tmp_env2, CONFIG_ENV_SIZE);
  118. if (read2_fail)
  119. printf("\n** Unable to read redundant env from %s:%s **\n",
  120. CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME_REDUND);
  121. return env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
  122. read2_fail, H_EXTERNAL);
  123. }
  124. #else /* ! CONFIG_SYS_REDUNDAND_ENVIRONMENT */
  125. static int env_ubi_load(void)
  126. {
  127. ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
  128. /*
  129. * In case we have restarted u-boot there is a chance that buffer
  130. * contains old environment (from the previous boot).
  131. * If UBI volume is zero size, ubi_volume_read() doesn't modify the
  132. * buffer.
  133. * We need to clear buffer manually here, so the invalid CRC will
  134. * cause setting default environment as expected.
  135. */
  136. memset(buf, 0x0, CONFIG_ENV_SIZE);
  137. if (ubi_part(CONFIG_ENV_UBI_PART, UBI_VID_OFFSET)) {
  138. printf("\n** Cannot find mtd partition \"%s\"\n",
  139. CONFIG_ENV_UBI_PART);
  140. env_set_default(NULL, 0);
  141. return -EIO;
  142. }
  143. if (ubi_volume_read(CONFIG_ENV_UBI_VOLUME, buf, CONFIG_ENV_SIZE)) {
  144. printf("\n** Unable to read env from %s:%s **\n",
  145. CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME);
  146. env_set_default(NULL, 0);
  147. return -EIO;
  148. }
  149. return env_import(buf, 1, H_EXTERNAL);
  150. }
  151. #endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */
  152. U_BOOT_ENV_LOCATION(ubi) = {
  153. .location = ENVL_UBI,
  154. ENV_NAME("UBI")
  155. .load = env_ubi_load,
  156. .save = env_save_ptr(env_ubi_save),
  157. };