ubi.c 4.5 KB

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