env_ubi.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. static unsigned char env_flags;
  29. int saveenv(void)
  30. {
  31. ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
  32. int ret;
  33. ret = env_export(env_new);
  34. if (ret)
  35. return ret;
  36. if (ubi_part(CONFIG_ENV_UBI_PART, NULL)) {
  37. printf("\n** Cannot find mtd partition \"%s\"\n",
  38. CONFIG_ENV_UBI_PART);
  39. return 1;
  40. }
  41. env_new->flags = ++env_flags; /* increase the serial */
  42. if (gd->env_valid == 1) {
  43. puts("Writing to redundant UBI... ");
  44. if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME_REDUND,
  45. (void *)env_new, CONFIG_ENV_SIZE)) {
  46. printf("\n** Unable to write env to %s:%s **\n",
  47. CONFIG_ENV_UBI_PART,
  48. CONFIG_ENV_UBI_VOLUME_REDUND);
  49. return 1;
  50. }
  51. } else {
  52. puts("Writing to UBI... ");
  53. if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME,
  54. (void *)env_new, CONFIG_ENV_SIZE)) {
  55. printf("\n** Unable to write env to %s:%s **\n",
  56. CONFIG_ENV_UBI_PART,
  57. CONFIG_ENV_UBI_VOLUME);
  58. return 1;
  59. }
  60. }
  61. puts("done\n");
  62. gd->env_valid = gd->env_valid == 2 ? 1 : 2;
  63. return 0;
  64. }
  65. #else /* ! CONFIG_SYS_REDUNDAND_ENVIRONMENT */
  66. int saveenv(void)
  67. {
  68. ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
  69. int ret;
  70. ret = env_export(env_new);
  71. if (ret)
  72. return ret;
  73. if (ubi_part(CONFIG_ENV_UBI_PART, NULL)) {
  74. printf("\n** Cannot find mtd partition \"%s\"\n",
  75. CONFIG_ENV_UBI_PART);
  76. return 1;
  77. }
  78. if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME, (void *)env_new,
  79. CONFIG_ENV_SIZE)) {
  80. printf("\n** Unable to write env to %s:%s **\n",
  81. CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME);
  82. return 1;
  83. }
  84. puts("done\n");
  85. return 0;
  86. }
  87. #endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */
  88. #endif /* CONFIG_CMD_SAVEENV */
  89. #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
  90. void env_relocate_spec(void)
  91. {
  92. ALLOC_CACHE_ALIGN_BUFFER(char, env1_buf, CONFIG_ENV_SIZE);
  93. ALLOC_CACHE_ALIGN_BUFFER(char, env2_buf, CONFIG_ENV_SIZE);
  94. int crc1_ok = 0, crc2_ok = 0;
  95. env_t *ep, *tmp_env1, *tmp_env2;
  96. tmp_env1 = (env_t *)env1_buf;
  97. tmp_env2 = (env_t *)env2_buf;
  98. if (ubi_part(CONFIG_ENV_UBI_PART, NULL)) {
  99. printf("\n** Cannot find mtd partition \"%s\"\n",
  100. CONFIG_ENV_UBI_PART);
  101. set_default_env(NULL);
  102. return;
  103. }
  104. if (ubi_volume_read(CONFIG_ENV_UBI_VOLUME, (void *)tmp_env1,
  105. CONFIG_ENV_SIZE)) {
  106. printf("\n** Unable to read env from %s:%s **\n",
  107. CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME);
  108. }
  109. if (ubi_volume_read(CONFIG_ENV_UBI_VOLUME_REDUND, (void *)tmp_env2,
  110. CONFIG_ENV_SIZE)) {
  111. printf("\n** Unable to read redundant env from %s:%s **\n",
  112. CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME_REDUND);
  113. }
  114. crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc;
  115. crc2_ok = crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc;
  116. if (!crc1_ok && !crc2_ok) {
  117. set_default_env("!bad CRC");
  118. return;
  119. } else if (crc1_ok && !crc2_ok) {
  120. gd->env_valid = 1;
  121. } else if (!crc1_ok && crc2_ok) {
  122. gd->env_valid = 2;
  123. } else {
  124. /* both ok - check serial */
  125. if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
  126. gd->env_valid = 2;
  127. else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
  128. gd->env_valid = 1;
  129. else if (tmp_env1->flags > tmp_env2->flags)
  130. gd->env_valid = 1;
  131. else if (tmp_env2->flags > tmp_env1->flags)
  132. gd->env_valid = 2;
  133. else /* flags are equal - almost impossible */
  134. gd->env_valid = 1;
  135. }
  136. if (gd->env_valid == 1)
  137. ep = tmp_env1;
  138. else
  139. ep = tmp_env2;
  140. env_flags = ep->flags;
  141. env_import((char *)ep, 0);
  142. }
  143. #else /* ! CONFIG_SYS_REDUNDAND_ENVIRONMENT */
  144. void env_relocate_spec(void)
  145. {
  146. ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
  147. if (ubi_part(CONFIG_ENV_UBI_PART, NULL)) {
  148. printf("\n** Cannot find mtd partition \"%s\"\n",
  149. CONFIG_ENV_UBI_PART);
  150. set_default_env(NULL);
  151. return;
  152. }
  153. if (ubi_volume_read(CONFIG_ENV_UBI_VOLUME, (void *)&buf,
  154. CONFIG_ENV_SIZE)) {
  155. printf("\n** Unable to read env from %s:%s **\n",
  156. CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME);
  157. set_default_env(NULL);
  158. return;
  159. }
  160. env_import(buf, 1);
  161. }
  162. #endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */