env_ubi.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. int saveenv(void)
  43. {
  44. ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
  45. ssize_t len;
  46. char *res;
  47. res = (char *)&env_new->data;
  48. len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
  49. if (len < 0) {
  50. error("Cannot export environment: errno = %d\n", errno);
  51. return 1;
  52. }
  53. if (ubi_part(CONFIG_ENV_UBI_PART, NULL)) {
  54. printf("\n** Cannot find mtd partition \"%s\"\n",
  55. CONFIG_ENV_UBI_PART);
  56. return 1;
  57. }
  58. env_new->crc = crc32(0, env_new->data, ENV_SIZE);
  59. if (ubi_volume_write(CONFIG_ENV_UBI_VOLUME, (void *)env_new,
  60. CONFIG_ENV_SIZE)) {
  61. printf("\n** Unable to write env to %s:%s **\n",
  62. CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME);
  63. return 1;
  64. }
  65. puts("done\n");
  66. return 0;
  67. }
  68. #endif /* CONFIG_CMD_SAVEENV */
  69. void env_relocate_spec(void)
  70. {
  71. ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
  72. if (ubi_part(CONFIG_ENV_UBI_PART, NULL)) {
  73. printf("\n** Cannot find mtd partition \"%s\"\n",
  74. CONFIG_ENV_UBI_PART);
  75. set_default_env(NULL);
  76. return;
  77. }
  78. if (ubi_volume_read(CONFIG_ENV_UBI_VOLUME, (void *)&buf,
  79. CONFIG_ENV_SIZE)) {
  80. printf("\n** Unable to read env from %s:%s **\n",
  81. CONFIG_ENV_UBI_PART, CONFIG_ENV_UBI_VOLUME);
  82. set_default_env(NULL);
  83. return;
  84. }
  85. env_import(buf, 1);
  86. }