env_mmc.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. extern uchar environment[];
  34. env_t *env_ptr = (env_t *)(&environment[0]);
  35. #else /* ! ENV_IS_EMBEDDED */
  36. env_t *env_ptr = NULL;
  37. #endif /* ENV_IS_EMBEDDED */
  38. /* local functions */
  39. #if !defined(ENV_IS_EMBEDDED)
  40. static void use_default(void);
  41. #endif
  42. DECLARE_GLOBAL_DATA_PTR;
  43. #if !defined(CONFIG_ENV_OFFSET)
  44. #define CONFIG_ENV_OFFSET 0
  45. #endif
  46. static int __mmc_get_env_addr(struct mmc *mmc, u32 *env_addr)
  47. {
  48. *env_addr = CONFIG_ENV_OFFSET;
  49. return 0;
  50. }
  51. __attribute__((weak, alias("__mmc_get_env_addr")))
  52. int mmc_get_env_addr(struct mmc *mmc, u32 *env_addr);
  53. uchar env_get_char_spec(int index)
  54. {
  55. return *((uchar *)(gd->env_addr + index));
  56. }
  57. int env_init(void)
  58. {
  59. /* use default */
  60. gd->env_addr = (ulong)&default_environment[0];
  61. gd->env_valid = 1;
  62. return 0;
  63. }
  64. int init_mmc_for_env(struct mmc *mmc)
  65. {
  66. if (!mmc) {
  67. puts("No MMC card found\n");
  68. return -1;
  69. }
  70. if (mmc_init(mmc)) {
  71. puts("MMC init failed\n");
  72. return -1;
  73. }
  74. return 0;
  75. }
  76. #ifdef CONFIG_CMD_SAVEENV
  77. inline int write_env(struct mmc *mmc, unsigned long size,
  78. unsigned long offset, const void *buffer)
  79. {
  80. uint blk_start, blk_cnt, n;
  81. blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len;
  82. blk_cnt = ALIGN(size, mmc->write_bl_len) / mmc->write_bl_len;
  83. n = mmc->block_dev.block_write(CONFIG_SYS_MMC_ENV_DEV, blk_start,
  84. blk_cnt, (u_char *)buffer);
  85. return (n == blk_cnt) ? 0 : -1;
  86. }
  87. int saveenv(void)
  88. {
  89. env_t env_new;
  90. ssize_t len;
  91. char *res;
  92. struct mmc *mmc = find_mmc_device(CONFIG_SYS_MMC_ENV_DEV);
  93. u32 offset;
  94. if (init_mmc_for_env(mmc))
  95. return 1;
  96. if(mmc_get_env_addr(mmc, &offset))
  97. return 1;
  98. res = (char *)&env_new.data;
  99. len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL);
  100. if (len < 0) {
  101. error("Cannot export environment: errno = %d\n", errno);
  102. return 1;
  103. }
  104. env_new.crc = crc32(0, env_new.data, ENV_SIZE);
  105. printf("Writing to MMC(%d)... ", CONFIG_SYS_MMC_ENV_DEV);
  106. if (write_env(mmc, CONFIG_ENV_SIZE, offset, (u_char *)&env_new)) {
  107. puts("failed\n");
  108. return 1;
  109. }
  110. puts("done\n");
  111. return 0;
  112. }
  113. #endif /* CONFIG_CMD_SAVEENV */
  114. inline int read_env(struct mmc *mmc, unsigned long size,
  115. unsigned long offset, const void *buffer)
  116. {
  117. uint blk_start, blk_cnt, n;
  118. blk_start = ALIGN(offset, mmc->read_bl_len) / mmc->read_bl_len;
  119. blk_cnt = ALIGN(size, mmc->read_bl_len) / mmc->read_bl_len;
  120. n = mmc->block_dev.block_read(CONFIG_SYS_MMC_ENV_DEV, blk_start,
  121. blk_cnt, (uchar *)buffer);
  122. return (n == blk_cnt) ? 0 : -1;
  123. }
  124. void env_relocate_spec(void)
  125. {
  126. #if !defined(ENV_IS_EMBEDDED)
  127. char buf[CONFIG_ENV_SIZE];
  128. struct mmc *mmc = find_mmc_device(CONFIG_SYS_MMC_ENV_DEV);
  129. u32 offset;
  130. if (init_mmc_for_env(mmc)) {
  131. use_default();
  132. return;
  133. }
  134. if(mmc_get_env_addr(mmc, &offset)) {
  135. use_default();
  136. return ;
  137. }
  138. if (read_env(mmc, CONFIG_ENV_SIZE, offset, buf)) {
  139. use_default();
  140. return;
  141. }
  142. env_import(buf, 1);
  143. #endif
  144. }
  145. #if !defined(ENV_IS_EMBEDDED)
  146. static void use_default()
  147. {
  148. set_default_env(NULL);
  149. }
  150. #endif