env_ubi.c 4.9 KB

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