env_fat.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * (c) Copyright 2011 by Tigris Elektronik GmbH
  3. *
  4. * Author:
  5. * Maximilian Schwerin <mvs@tigris.de>
  6. *
  7. * See file CREDITS for list of people who contributed to this
  8. * project.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation; either version 2 of
  13. * the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  23. * MA 02111-1307 USA
  24. */
  25. #include <common.h>
  26. #include <command.h>
  27. #include <environment.h>
  28. #include <linux/stddef.h>
  29. #include <malloc.h>
  30. #include <search.h>
  31. #include <errno.h>
  32. #include <fat.h>
  33. #include <mmc.h>
  34. char *env_name_spec = "FAT";
  35. env_t *env_ptr;
  36. DECLARE_GLOBAL_DATA_PTR;
  37. int env_init(void)
  38. {
  39. /* use default */
  40. gd->env_addr = (ulong)&default_environment[0];
  41. gd->env_valid = 1;
  42. return 0;
  43. }
  44. #ifdef CONFIG_CMD_SAVEENV
  45. int saveenv(void)
  46. {
  47. env_t env_new;
  48. ssize_t len;
  49. char *res;
  50. block_dev_desc_t *dev_desc = NULL;
  51. int dev = FAT_ENV_DEVICE;
  52. int part = FAT_ENV_PART;
  53. int err;
  54. res = (char *)&env_new.data;
  55. len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
  56. if (len < 0) {
  57. error("Cannot export environment: errno = %d\n", errno);
  58. return 1;
  59. }
  60. #ifdef CONFIG_MMC
  61. if (strcmp(FAT_ENV_INTERFACE, "mmc") == 0) {
  62. struct mmc *mmc = find_mmc_device(dev);
  63. if (!mmc) {
  64. printf("no mmc device at slot %x\n", dev);
  65. return 1;
  66. }
  67. mmc->has_init = 0;
  68. mmc_init(mmc);
  69. }
  70. #endif /* CONFIG_MMC */
  71. dev_desc = get_dev(FAT_ENV_INTERFACE, dev);
  72. if (dev_desc == NULL) {
  73. printf("Failed to find %s%d\n",
  74. FAT_ENV_INTERFACE, dev);
  75. return 1;
  76. }
  77. err = fat_register_device(dev_desc, part);
  78. if (err) {
  79. printf("Failed to register %s%d:%d\n",
  80. FAT_ENV_INTERFACE, dev, part);
  81. return 1;
  82. }
  83. env_new.crc = crc32(0, env_new.data, ENV_SIZE);
  84. err = file_fat_write(FAT_ENV_FILE, (void *)&env_new, sizeof(env_t));
  85. if (err == -1) {
  86. printf("\n** Unable to write \"%s\" from %s%d:%d **\n",
  87. FAT_ENV_FILE, FAT_ENV_INTERFACE, dev, part);
  88. return 1;
  89. }
  90. puts("done\n");
  91. return 0;
  92. }
  93. #endif /* CONFIG_CMD_SAVEENV */
  94. void env_relocate_spec(void)
  95. {
  96. char buf[CONFIG_ENV_SIZE];
  97. block_dev_desc_t *dev_desc = NULL;
  98. int dev = FAT_ENV_DEVICE;
  99. int part = FAT_ENV_PART;
  100. int err;
  101. #ifdef CONFIG_MMC
  102. if (strcmp(FAT_ENV_INTERFACE, "mmc") == 0) {
  103. struct mmc *mmc = find_mmc_device(dev);
  104. if (!mmc) {
  105. printf("no mmc device at slot %x\n", dev);
  106. set_default_env(NULL);
  107. return;
  108. }
  109. mmc->has_init = 0;
  110. mmc_init(mmc);
  111. }
  112. #endif /* CONFIG_MMC */
  113. dev_desc = get_dev(FAT_ENV_INTERFACE, dev);
  114. if (dev_desc == NULL) {
  115. printf("Failed to find %s%d\n",
  116. FAT_ENV_INTERFACE, dev);
  117. set_default_env(NULL);
  118. return;
  119. }
  120. err = fat_register_device(dev_desc, part);
  121. if (err) {
  122. printf("Failed to register %s%d:%d\n",
  123. FAT_ENV_INTERFACE, dev, part);
  124. set_default_env(NULL);
  125. return;
  126. }
  127. err = file_fat_read(FAT_ENV_FILE, (uchar *)&buf, CONFIG_ENV_SIZE);
  128. if (err == -1) {
  129. printf("\n** Unable to read \"%s\" from %s%d:%d **\n",
  130. FAT_ENV_FILE, FAT_ENV_INTERFACE, dev, part);
  131. set_default_env(NULL);
  132. return;
  133. }
  134. env_import(buf, 1);
  135. }