ubi.c 4.4 KB

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