env_fat.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. uchar env_get_char_spec(int index)
  38. {
  39. return *((uchar *)(gd->env_addr + index));
  40. }
  41. int env_init(void)
  42. {
  43. /* use default */
  44. gd->env_addr = (ulong)&default_environment[0];
  45. gd->env_valid = 1;
  46. return 0;
  47. }
  48. #ifdef CONFIG_CMD_SAVEENV
  49. int saveenv(void)
  50. {
  51. env_t env_new;
  52. ssize_t len;
  53. char *res;
  54. block_dev_desc_t *dev_desc = NULL;
  55. int dev = FAT_ENV_DEVICE;
  56. int part = FAT_ENV_PART;
  57. res = (char *)&env_new.data;
  58. len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL);
  59. if (len < 0) {
  60. error("Cannot export environment: errno = %d\n", errno);
  61. return 1;
  62. }
  63. #ifdef CONFIG_MMC
  64. if (strcmp (FAT_ENV_INTERFACE, "mmc") == 0) {
  65. struct mmc *mmc = find_mmc_device(dev);
  66. if (!mmc) {
  67. printf("no mmc device at slot %x\n", dev);
  68. return 1;
  69. }
  70. mmc->has_init = 0;
  71. mmc_init(mmc);
  72. }
  73. #endif /* CONFIG_MMC */
  74. dev_desc = get_dev(FAT_ENV_INTERFACE, dev);
  75. if (dev_desc == NULL) {
  76. printf("Failed to find %s%d\n",
  77. FAT_ENV_INTERFACE, dev);
  78. return 1;
  79. }
  80. if (fat_register_device(dev_desc, part) != 0) {
  81. printf("Failed to register %s%d:%d\n",
  82. FAT_ENV_INTERFACE, dev, part);
  83. return 1;
  84. }
  85. env_new.crc = crc32(0, env_new.data, ENV_SIZE);
  86. if (file_fat_write(FAT_ENV_FILE, (void *)&env_new, sizeof(env_t)) == -1) {
  87. printf("\n** Unable to write \"%s\" from %s%d:%d **\n",
  88. FAT_ENV_FILE, FAT_ENV_INTERFACE, dev, part);
  89. return 1;
  90. }
  91. puts("done\n");
  92. return 0;
  93. }
  94. #endif /* CONFIG_CMD_SAVEENV */
  95. void env_relocate_spec(void)
  96. {
  97. char buf[CONFIG_ENV_SIZE];
  98. block_dev_desc_t *dev_desc = NULL;
  99. int dev = FAT_ENV_DEVICE;
  100. int part = FAT_ENV_PART;
  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. if (fat_register_device(dev_desc, part) != 0) {
  121. printf("Failed to register %s%d:%d\n",
  122. FAT_ENV_INTERFACE, dev, part);
  123. set_default_env(NULL);
  124. return;
  125. }
  126. if (file_fat_read(FAT_ENV_FILE, (unsigned char *)&buf, CONFIG_ENV_SIZE) == -1) {
  127. printf("\n** Unable to read \"%s\" from %s%d:%d **\n",
  128. FAT_ENV_FILE, FAT_ENV_INTERFACE, dev, part);
  129. set_default_env(NULL);
  130. return;
  131. }
  132. env_import(buf, 1);
  133. }