ubi.c 4.8 KB

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