env_mmc.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * (C) Copyright 2008-2011 Freescale Semiconductor, Inc.
  3. *
  4. * See file CREDITS for list of people who contributed to this
  5. * project.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  20. * MA 02111-1307 USA
  21. */
  22. /* #define DEBUG */
  23. #include <common.h>
  24. #include <command.h>
  25. #include <environment.h>
  26. #include <linux/stddef.h>
  27. #include <malloc.h>
  28. #include <mmc.h>
  29. #include <search.h>
  30. #include <errno.h>
  31. char *env_name_spec = "MMC";
  32. #ifdef ENV_IS_EMBEDDED
  33. env_t *env_ptr = &environment;
  34. #else /* ! ENV_IS_EMBEDDED */
  35. env_t *env_ptr;
  36. #endif /* ENV_IS_EMBEDDED */
  37. DECLARE_GLOBAL_DATA_PTR;
  38. #if !defined(CONFIG_ENV_OFFSET)
  39. #define CONFIG_ENV_OFFSET 0
  40. #endif
  41. static int __mmc_get_env_addr(struct mmc *mmc, u32 *env_addr)
  42. {
  43. *env_addr = CONFIG_ENV_OFFSET;
  44. return 0;
  45. }
  46. int mmc_get_env_addr(struct mmc *mmc, u32 *env_addr)
  47. __attribute__((weak, alias("__mmc_get_env_addr")));
  48. int env_init(void)
  49. {
  50. /* use default */
  51. gd->env_addr = (ulong)&default_environment[0];
  52. gd->env_valid = 1;
  53. return 0;
  54. }
  55. static int init_mmc_for_env(struct mmc *mmc)
  56. {
  57. if (!mmc) {
  58. puts("No MMC card found\n");
  59. return -1;
  60. }
  61. if (mmc_init(mmc)) {
  62. puts("MMC init failed\n");
  63. return -1;
  64. }
  65. #ifdef CONFIG_SYS_MMC_ENV_PART
  66. if (CONFIG_SYS_MMC_ENV_PART != mmc->part_num) {
  67. if (mmc_switch_part(CONFIG_SYS_MMC_ENV_DEV,
  68. CONFIG_SYS_MMC_ENV_PART)) {
  69. puts("MMC partition switch failed\n");
  70. return -1;
  71. }
  72. }
  73. #endif
  74. return 0;
  75. }
  76. static void fini_mmc_for_env(struct mmc *mmc)
  77. {
  78. #ifdef CONFIG_SYS_MMC_ENV_PART
  79. if (CONFIG_SYS_MMC_ENV_PART != mmc->part_num)
  80. mmc_switch_part(CONFIG_SYS_MMC_ENV_DEV,
  81. mmc->part_num);
  82. #endif
  83. }
  84. #ifdef CONFIG_CMD_SAVEENV
  85. static inline int write_env(struct mmc *mmc, unsigned long size,
  86. unsigned long offset, const void *buffer)
  87. {
  88. uint blk_start, blk_cnt, n;
  89. blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len;
  90. blk_cnt = ALIGN(size, mmc->write_bl_len) / mmc->write_bl_len;
  91. n = mmc->block_dev.block_write(CONFIG_SYS_MMC_ENV_DEV, blk_start,
  92. blk_cnt, (u_char *)buffer);
  93. return (n == blk_cnt) ? 0 : -1;
  94. }
  95. int saveenv(void)
  96. {
  97. ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
  98. ssize_t len;
  99. char *res;
  100. struct mmc *mmc = find_mmc_device(CONFIG_SYS_MMC_ENV_DEV);
  101. u32 offset;
  102. int ret;
  103. if (init_mmc_for_env(mmc))
  104. return 1;
  105. if (mmc_get_env_addr(mmc, &offset)) {
  106. ret = 1;
  107. goto fini;
  108. }
  109. res = (char *)&env_new->data;
  110. len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL);
  111. if (len < 0) {
  112. error("Cannot export environment: errno = %d\n", errno);
  113. ret = 1;
  114. goto fini;
  115. }
  116. env_new->crc = crc32(0, &env_new->data[0], ENV_SIZE);
  117. printf("Writing to MMC(%d)... ", CONFIG_SYS_MMC_ENV_DEV);
  118. if (write_env(mmc, CONFIG_ENV_SIZE, offset, (u_char *)env_new)) {
  119. puts("failed\n");
  120. ret = 1;
  121. goto fini;
  122. }
  123. puts("done\n");
  124. ret = 0;
  125. fini:
  126. fini_mmc_for_env(mmc);
  127. return ret;
  128. }
  129. #endif /* CONFIG_CMD_SAVEENV */
  130. static inline int read_env(struct mmc *mmc, unsigned long size,
  131. unsigned long offset, const void *buffer)
  132. {
  133. uint blk_start, blk_cnt, n;
  134. blk_start = ALIGN(offset, mmc->read_bl_len) / mmc->read_bl_len;
  135. blk_cnt = ALIGN(size, mmc->read_bl_len) / mmc->read_bl_len;
  136. n = mmc->block_dev.block_read(CONFIG_SYS_MMC_ENV_DEV, blk_start,
  137. blk_cnt, (uchar *)buffer);
  138. return (n == blk_cnt) ? 0 : -1;
  139. }
  140. void env_relocate_spec(void)
  141. {
  142. #if !defined(ENV_IS_EMBEDDED)
  143. ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
  144. struct mmc *mmc = find_mmc_device(CONFIG_SYS_MMC_ENV_DEV);
  145. u32 offset;
  146. int ret;
  147. if (init_mmc_for_env(mmc)) {
  148. ret = 1;
  149. goto err;
  150. }
  151. if (mmc_get_env_addr(mmc, &offset)) {
  152. ret = 1;
  153. goto fini;
  154. }
  155. if (read_env(mmc, CONFIG_ENV_SIZE, offset, buf)) {
  156. ret = 1;
  157. goto fini;
  158. }
  159. env_import(buf, 1);
  160. ret = 0;
  161. fini:
  162. fini_mmc_for_env(mmc);
  163. err:
  164. if (ret)
  165. set_default_env(NULL);
  166. #endif
  167. }