env_ubi.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * (c) Copyright 2012 by National Instruments,
  3. * Joe Hershberger <joe.hershberger@ni.com>
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #include <command.h>
  25. #include <environment.h>
  26. #include <errno.h>
  27. #include <malloc.h>
  28. #include <search.h>
  29. #include <ubi_uboot.h>
  30. #undef crc32
  31. char *env_name_spec = "UBI";
  32. env_t *env_ptr;
  33. DECLARE_GLOBAL_DATA_PTR;
  34. int env_init(void)
  35. {
  36. /* use default */
  37. gd->env_addr = (ulong)&default_environment[0];
  38. gd->env_valid = 1;
  39. return 0;
  40. }
  41. #ifdef CONFIG_CMD_SAVEENV
  42. #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
  43. static unsigned char env_flags;
  44. int saveenv(void)
  45. {
  46. ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
  47. ssize_t len;
  48. char *res;
  49. res = (char *)&env_new->data;
  50. len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
  51. if (len < 0) {
  52. error("Cannot export environment: errno = %d\n", errno);
  53. return 1;
  54. }
  55. if (ubi_part(CONFIG_ENV_UBI_PART, NULL)) {
  56. printf("\n** Cannot find mtd partition \"%s\"\n",
  57. CONFIG_ENV_UBI_PART);
  58. return 1;
  59. }
  60. env_new->crc = crc32(0, env_new->data, ENV_SIZE);
  61. env_new->flags = ++env_flags; /* increase the serial */
  62. if (gd->env_valid == 1) {
  63. puts("Writing to redundant UBI... ");
  64. if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME_REDUND,
  65. (void *)env_new, CONFIG_ENV_SIZE)) {
  66. printf("\n** Unable to write env to %s:%s **\n",
  67. CONFIG_ENV_UBI_PART,
  68. CONFIG_ENV_UBI_VOLUME_REDUND);
  69. return 1;
  70. }
  71. } else {
  72. puts("Writing to UBI... ");
  73. if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME,
  74. (void *)env_new, CONFIG_ENV_SIZE)) {
  75. printf("\n** Unable to write env to %s:%s **\n",
  76. CONFIG_ENV_UBI_PART,
  77. CONFIG_ENV_UBI_VOLUME);
  78. return 1;
  79. }
  80. }
  81. puts("done\n");
  82. gd->env_valid = gd->env_valid == 2 ? 1 : 2;
  83. return 0;
  84. }
  85. #else /* ! CONFIG_SYS_REDUNDAND_ENVIRONMENT */
  86. int saveenv(void)
  87. {
  88. ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
  89. ssize_t len;
  90. char *res;
  91. res = (char *)&env_new->data;
  92. len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
  93. if (len < 0) {
  94. error("Cannot export environment: errno = %d\n", errno);
  95. return 1;
  96. }
  97. if (ubi_part(CONFIG_ENV_UBI_PART, NULL)) {
  98. printf("\n** Cannot find mtd partition \"%s\"\n",
  99. CONFIG_ENV_UBI_PART);
  100. return 1;
  101. }
  102. env_new->crc = crc32(0, env_new->data, ENV_SIZE);
  103. if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME, (void *)env_new,
  104. CONFIG_ENV_SIZE)) {
  105. printf("\n** Unable to write env to %s:%s **\n",
  106. CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME);
  107. return 1;
  108. }
  109. puts("done\n");
  110. return 0;
  111. }
  112. #endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */
  113. #endif /* CONFIG_CMD_SAVEENV */
  114. #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
  115. void env_relocate_spec(void)
  116. {
  117. ALLOC_CACHE_ALIGN_BUFFER(char, env1_buf, CONFIG_ENV_SIZE);
  118. ALLOC_CACHE_ALIGN_BUFFER(char, env2_buf, CONFIG_ENV_SIZE);
  119. int crc1_ok = 0, crc2_ok = 0;
  120. env_t *ep, *tmp_env1, *tmp_env2;
  121. tmp_env1 = (env_t *)env1_buf;
  122. tmp_env2 = (env_t *)env2_buf;
  123. if (ubi_part(CONFIG_ENV_UBI_PART, NULL)) {
  124. printf("\n** Cannot find mtd partition \"%s\"\n",
  125. CONFIG_ENV_UBI_PART);
  126. set_default_env(NULL);
  127. return;
  128. }
  129. if (ubi_volume_read(CONFIG_ENV_UBI_VOLUME, (void *)tmp_env1,
  130. CONFIG_ENV_SIZE)) {
  131. printf("\n** Unable to read env from %s:%s **\n",
  132. CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME);
  133. }
  134. if (ubi_volume_read(CONFIG_ENV_UBI_VOLUME_REDUND, (void *)tmp_env2,
  135. CONFIG_ENV_SIZE)) {
  136. printf("\n** Unable to read redundant env from %s:%s **\n",
  137. CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME_REDUND);
  138. }
  139. crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc;
  140. crc2_ok = crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc;
  141. if (!crc1_ok && !crc2_ok) {
  142. set_default_env("!bad CRC");
  143. return;
  144. } else if (crc1_ok && !crc2_ok) {
  145. gd->env_valid = 1;
  146. } else if (!crc1_ok && crc2_ok) {
  147. gd->env_valid = 2;
  148. } else {
  149. /* both ok - check serial */
  150. if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
  151. gd->env_valid = 2;
  152. else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
  153. gd->env_valid = 1;
  154. else if (tmp_env1->flags > tmp_env2->flags)
  155. gd->env_valid = 1;
  156. else if (tmp_env2->flags > tmp_env1->flags)
  157. gd->env_valid = 2;
  158. else /* flags are equal - almost impossible */
  159. gd->env_valid = 1;
  160. }
  161. if (gd->env_valid == 1)
  162. ep = tmp_env1;
  163. else
  164. ep = tmp_env2;
  165. env_flags = ep->flags;
  166. env_import((char *)ep, 0);
  167. }
  168. #else /* ! CONFIG_SYS_REDUNDAND_ENVIRONMENT */
  169. void env_relocate_spec(void)
  170. {
  171. ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
  172. if (ubi_part(CONFIG_ENV_UBI_PART, NULL)) {
  173. printf("\n** Cannot find mtd partition \"%s\"\n",
  174. CONFIG_ENV_UBI_PART);
  175. set_default_env(NULL);
  176. return;
  177. }
  178. if (ubi_volume_read(CONFIG_ENV_UBI_VOLUME, (void *)&buf,
  179. CONFIG_ENV_SIZE)) {
  180. printf("\n** Unable to read env from %s:%s **\n",
  181. CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME);
  182. set_default_env(NULL);
  183. return;
  184. }
  185. env_import(buf, 1);
  186. }
  187. #endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */