env_ubi.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. char *env_name_spec = "UBI";
  17. env_t *env_ptr;
  18. DECLARE_GLOBAL_DATA_PTR;
  19. int env_init(void)
  20. {
  21. /* use default */
  22. gd->env_addr = (ulong)&default_environment[0];
  23. gd->env_valid = 1;
  24. return 0;
  25. }
  26. #ifdef CONFIG_CMD_SAVEENV
  27. #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
  28. int saveenv(void)
  29. {
  30. ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
  31. int ret;
  32. ret = env_export(env_new);
  33. if (ret)
  34. return ret;
  35. if (ubi_part(CONFIG_ENV_UBI_PART, NULL)) {
  36. printf("\n** Cannot find mtd partition \"%s\"\n",
  37. CONFIG_ENV_UBI_PART);
  38. return 1;
  39. }
  40. if (gd->env_valid == 1) {
  41. puts("Writing to redundant UBI... ");
  42. if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME_REDUND,
  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_REDUND);
  47. return 1;
  48. }
  49. } else {
  50. puts("Writing to UBI... ");
  51. if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME,
  52. (void *)env_new, CONFIG_ENV_SIZE)) {
  53. printf("\n** Unable to write env to %s:%s **\n",
  54. CONFIG_ENV_UBI_PART,
  55. CONFIG_ENV_UBI_VOLUME);
  56. return 1;
  57. }
  58. }
  59. puts("done\n");
  60. gd->env_valid = gd->env_valid == 2 ? 1 : 2;
  61. return 0;
  62. }
  63. #else /* ! CONFIG_SYS_REDUNDAND_ENVIRONMENT */
  64. int saveenv(void)
  65. {
  66. ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
  67. int ret;
  68. ret = env_export(env_new);
  69. if (ret)
  70. return ret;
  71. if (ubi_part(CONFIG_ENV_UBI_PART, NULL)) {
  72. printf("\n** Cannot find mtd partition \"%s\"\n",
  73. CONFIG_ENV_UBI_PART);
  74. return 1;
  75. }
  76. if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME, (void *)env_new,
  77. CONFIG_ENV_SIZE)) {
  78. printf("\n** Unable to write env to %s:%s **\n",
  79. CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME);
  80. return 1;
  81. }
  82. puts("done\n");
  83. return 0;
  84. }
  85. #endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */
  86. #endif /* CONFIG_CMD_SAVEENV */
  87. #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
  88. void env_relocate_spec(void)
  89. {
  90. ALLOC_CACHE_ALIGN_BUFFER(char, env1_buf, CONFIG_ENV_SIZE);
  91. ALLOC_CACHE_ALIGN_BUFFER(char, env2_buf, CONFIG_ENV_SIZE);
  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, NULL)) {
  106. printf("\n** Cannot find mtd partition \"%s\"\n",
  107. CONFIG_ENV_UBI_PART);
  108. set_default_env(NULL);
  109. return;
  110. }
  111. if (ubi_volume_read(CONFIG_ENV_UBI_VOLUME, (void *)tmp_env1,
  112. CONFIG_ENV_SIZE)) {
  113. printf("\n** Unable to read env from %s:%s **\n",
  114. CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME);
  115. }
  116. if (ubi_volume_read(CONFIG_ENV_UBI_VOLUME_REDUND, (void *)tmp_env2,
  117. CONFIG_ENV_SIZE)) {
  118. printf("\n** Unable to read redundant env from %s:%s **\n",
  119. CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME_REDUND);
  120. }
  121. env_import_redund((char *)tmp_env1, (char *)tmp_env2);
  122. }
  123. #else /* ! CONFIG_SYS_REDUNDAND_ENVIRONMENT */
  124. void env_relocate_spec(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, NULL)) {
  137. printf("\n** Cannot find mtd partition \"%s\"\n",
  138. CONFIG_ENV_UBI_PART);
  139. set_default_env(NULL);
  140. return;
  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. set_default_env(NULL);
  146. return;
  147. }
  148. env_import(buf, 1);
  149. }
  150. #endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */