ubi.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. DECLARE_GLOBAL_DATA_PTR;
  17. #ifdef CONFIG_CMD_SAVEENV
  18. #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
  19. static int env_ubi_save(void)
  20. {
  21. ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
  22. int ret;
  23. ret = env_export(env_new);
  24. if (ret)
  25. return ret;
  26. if (ubi_part(CONFIG_ENV_UBI_PART, NULL)) {
  27. printf("\n** Cannot find mtd partition \"%s\"\n",
  28. CONFIG_ENV_UBI_PART);
  29. return 1;
  30. }
  31. if (gd->env_valid == ENV_VALID) {
  32. puts("Writing to redundant UBI... ");
  33. if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME_REDUND,
  34. (void *)env_new, CONFIG_ENV_SIZE)) {
  35. printf("\n** Unable to write env to %s:%s **\n",
  36. CONFIG_ENV_UBI_PART,
  37. CONFIG_ENV_UBI_VOLUME_REDUND);
  38. return 1;
  39. }
  40. } else {
  41. puts("Writing to UBI... ");
  42. if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME,
  43. (void *)env_new, CONFIG_ENV_SIZE)) {
  44. printf("\n** Unable to write env to %s:%s **\n",
  45. CONFIG_ENV_UBI_PART,
  46. CONFIG_ENV_UBI_VOLUME);
  47. return 1;
  48. }
  49. }
  50. puts("done\n");
  51. gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
  52. return 0;
  53. }
  54. #else /* ! CONFIG_SYS_REDUNDAND_ENVIRONMENT */
  55. static int env_ubi_save(void)
  56. {
  57. ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
  58. int ret;
  59. ret = env_export(env_new);
  60. if (ret)
  61. return ret;
  62. if (ubi_part(CONFIG_ENV_UBI_PART, NULL)) {
  63. printf("\n** Cannot find mtd partition \"%s\"\n",
  64. CONFIG_ENV_UBI_PART);
  65. return 1;
  66. }
  67. if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME, (void *)env_new,
  68. CONFIG_ENV_SIZE)) {
  69. printf("\n** Unable to write env to %s:%s **\n",
  70. CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME);
  71. return 1;
  72. }
  73. puts("done\n");
  74. return 0;
  75. }
  76. #endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */
  77. #endif /* CONFIG_CMD_SAVEENV */
  78. #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
  79. static int env_ubi_load(void)
  80. {
  81. ALLOC_CACHE_ALIGN_BUFFER(char, env1_buf, CONFIG_ENV_SIZE);
  82. ALLOC_CACHE_ALIGN_BUFFER(char, env2_buf, CONFIG_ENV_SIZE);
  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);
  100. return -EIO;
  101. }
  102. if (ubi_volume_read(CONFIG_ENV_UBI_VOLUME, (void *)tmp_env1,
  103. CONFIG_ENV_SIZE)) {
  104. printf("\n** Unable to read env from %s:%s **\n",
  105. CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME);
  106. }
  107. if (ubi_volume_read(CONFIG_ENV_UBI_VOLUME_REDUND, (void *)tmp_env2,
  108. CONFIG_ENV_SIZE)) {
  109. printf("\n** Unable to read redundant env from %s:%s **\n",
  110. CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME_REDUND);
  111. }
  112. env_import_redund((char *)tmp_env1, (char *)tmp_env2);
  113. return 0;
  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);
  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);
  138. return -EIO;
  139. }
  140. env_import(buf, 1);
  141. return 0;
  142. }
  143. #endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */
  144. U_BOOT_ENV_LOCATION(ubi) = {
  145. .location = ENVL_UBI,
  146. .load = env_ubi_load,
  147. .save = env_save_ptr(env_ubi_save),
  148. };