env_mmc.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. #if defined(CONFIG_ENV_SIZE_REDUND) && \
  32. (CONFIG_ENV_SIZE_REDUND != CONFIG_ENV_SIZE)
  33. #error CONFIG_ENV_SIZE_REDUND should be the same as CONFIG_ENV_SIZE
  34. #endif
  35. char *env_name_spec = "MMC";
  36. #ifdef ENV_IS_EMBEDDED
  37. env_t *env_ptr = &environment;
  38. #else /* ! ENV_IS_EMBEDDED */
  39. env_t *env_ptr;
  40. #endif /* ENV_IS_EMBEDDED */
  41. DECLARE_GLOBAL_DATA_PTR;
  42. #if !defined(CONFIG_ENV_OFFSET)
  43. #define CONFIG_ENV_OFFSET 0
  44. #endif
  45. __weak int mmc_get_env_addr(struct mmc *mmc, int copy, u32 *env_addr)
  46. {
  47. *env_addr = CONFIG_ENV_OFFSET;
  48. #ifdef CONFIG_ENV_OFFSET_REDUND
  49. if (copy)
  50. *env_addr = CONFIG_ENV_OFFSET_REDUND;
  51. #endif
  52. return 0;
  53. }
  54. int env_init(void)
  55. {
  56. /* use default */
  57. gd->env_addr = (ulong)&default_environment[0];
  58. gd->env_valid = 1;
  59. return 0;
  60. }
  61. static int init_mmc_for_env(struct mmc *mmc)
  62. {
  63. if (!mmc) {
  64. puts("No MMC card found\n");
  65. return -1;
  66. }
  67. if (mmc_init(mmc)) {
  68. puts("MMC init failed\n");
  69. return -1;
  70. }
  71. #ifdef CONFIG_SYS_MMC_ENV_PART
  72. if (CONFIG_SYS_MMC_ENV_PART != mmc->part_num) {
  73. if (mmc_switch_part(CONFIG_SYS_MMC_ENV_DEV,
  74. CONFIG_SYS_MMC_ENV_PART)) {
  75. puts("MMC partition switch failed\n");
  76. return -1;
  77. }
  78. }
  79. #endif
  80. return 0;
  81. }
  82. static void fini_mmc_for_env(struct mmc *mmc)
  83. {
  84. #ifdef CONFIG_SYS_MMC_ENV_PART
  85. if (CONFIG_SYS_MMC_ENV_PART != mmc->part_num)
  86. mmc_switch_part(CONFIG_SYS_MMC_ENV_DEV,
  87. mmc->part_num);
  88. #endif
  89. }
  90. #ifdef CONFIG_CMD_SAVEENV
  91. static inline int write_env(struct mmc *mmc, unsigned long size,
  92. unsigned long offset, const void *buffer)
  93. {
  94. uint blk_start, blk_cnt, n;
  95. blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len;
  96. blk_cnt = ALIGN(size, mmc->write_bl_len) / mmc->write_bl_len;
  97. n = mmc->block_dev.block_write(CONFIG_SYS_MMC_ENV_DEV, blk_start,
  98. blk_cnt, (u_char *)buffer);
  99. return (n == blk_cnt) ? 0 : -1;
  100. }
  101. #ifdef CONFIG_ENV_OFFSET_REDUND
  102. static unsigned char env_flags;
  103. #endif
  104. int saveenv(void)
  105. {
  106. ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
  107. ssize_t len;
  108. char *res;
  109. struct mmc *mmc = find_mmc_device(CONFIG_SYS_MMC_ENV_DEV);
  110. u32 offset;
  111. int ret, copy = 0;
  112. if (init_mmc_for_env(mmc))
  113. return 1;
  114. res = (char *)&env_new->data;
  115. len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
  116. if (len < 0) {
  117. error("Cannot export environment: errno = %d\n", errno);
  118. ret = 1;
  119. goto fini;
  120. }
  121. env_new->crc = crc32(0, &env_new->data[0], ENV_SIZE);
  122. #ifdef CONFIG_ENV_OFFSET_REDUND
  123. env_new->flags = ++env_flags; /* increase the serial */
  124. if (gd->env_valid == 1)
  125. copy = 1;
  126. #endif
  127. if (mmc_get_env_addr(mmc, copy, &offset)) {
  128. ret = 1;
  129. goto fini;
  130. }
  131. printf("Writing to %sMMC(%d)... ", copy ? "redundant " : "",
  132. CONFIG_SYS_MMC_ENV_DEV);
  133. if (write_env(mmc, CONFIG_ENV_SIZE, offset, (u_char *)env_new)) {
  134. puts("failed\n");
  135. ret = 1;
  136. goto fini;
  137. }
  138. puts("done\n");
  139. ret = 0;
  140. #ifdef CONFIG_ENV_OFFSET_REDUND
  141. gd->env_valid = gd->env_valid == 2 ? 1 : 2;
  142. #endif
  143. fini:
  144. fini_mmc_for_env(mmc);
  145. return ret;
  146. }
  147. #endif /* CONFIG_CMD_SAVEENV */
  148. static inline int read_env(struct mmc *mmc, unsigned long size,
  149. unsigned long offset, const void *buffer)
  150. {
  151. uint blk_start, blk_cnt, n;
  152. blk_start = ALIGN(offset, mmc->read_bl_len) / mmc->read_bl_len;
  153. blk_cnt = ALIGN(size, mmc->read_bl_len) / mmc->read_bl_len;
  154. n = mmc->block_dev.block_read(CONFIG_SYS_MMC_ENV_DEV, blk_start,
  155. blk_cnt, (uchar *)buffer);
  156. return (n == blk_cnt) ? 0 : -1;
  157. }
  158. #ifdef CONFIG_ENV_OFFSET_REDUND
  159. void env_relocate_spec(void)
  160. {
  161. #if !defined(ENV_IS_EMBEDDED)
  162. struct mmc *mmc = find_mmc_device(CONFIG_SYS_MMC_ENV_DEV);
  163. u32 offset1, offset2;
  164. int read1_fail = 0, read2_fail = 0;
  165. int crc1_ok = 0, crc2_ok = 0;
  166. env_t *ep, *tmp_env1, *tmp_env2;
  167. int ret;
  168. tmp_env1 = (env_t *)malloc(CONFIG_ENV_SIZE);
  169. tmp_env2 = (env_t *)malloc(CONFIG_ENV_SIZE);
  170. if (tmp_env1 == NULL || tmp_env2 == NULL) {
  171. puts("Can't allocate buffers for environment\n");
  172. ret = 1;
  173. goto err;
  174. }
  175. if (init_mmc_for_env(mmc)) {
  176. ret = 1;
  177. goto err;
  178. }
  179. if (mmc_get_env_addr(mmc, 0, &offset1) ||
  180. mmc_get_env_addr(mmc, 1, &offset2)) {
  181. ret = 1;
  182. goto fini;
  183. }
  184. read1_fail = read_env(mmc, CONFIG_ENV_SIZE, offset1, tmp_env1);
  185. read2_fail = read_env(mmc, CONFIG_ENV_SIZE, offset2, tmp_env2);
  186. if (read1_fail && read2_fail)
  187. puts("*** Error - No Valid Environment Area found\n");
  188. else if (read1_fail || read2_fail)
  189. puts("*** Warning - some problems detected "
  190. "reading environment; recovered successfully\n");
  191. crc1_ok = !read1_fail &&
  192. (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc);
  193. crc2_ok = !read2_fail &&
  194. (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc);
  195. if (!crc1_ok && !crc2_ok) {
  196. ret = 1;
  197. goto fini;
  198. } else if (crc1_ok && !crc2_ok) {
  199. gd->env_valid = 1;
  200. } else if (!crc1_ok && crc2_ok) {
  201. gd->env_valid = 2;
  202. } else {
  203. /* both ok - check serial */
  204. if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
  205. gd->env_valid = 2;
  206. else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
  207. gd->env_valid = 1;
  208. else if (tmp_env1->flags > tmp_env2->flags)
  209. gd->env_valid = 1;
  210. else if (tmp_env2->flags > tmp_env1->flags)
  211. gd->env_valid = 2;
  212. else /* flags are equal - almost impossible */
  213. gd->env_valid = 1;
  214. }
  215. free(env_ptr);
  216. if (gd->env_valid == 1)
  217. ep = tmp_env1;
  218. else
  219. ep = tmp_env2;
  220. env_flags = ep->flags;
  221. env_import((char *)ep, 0);
  222. ret = 0;
  223. fini:
  224. fini_mmc_for_env(mmc);
  225. err:
  226. if (ret)
  227. set_default_env(NULL);
  228. free(tmp_env1);
  229. free(tmp_env2);
  230. #endif
  231. }
  232. #else /* ! CONFIG_ENV_OFFSET_REDUND */
  233. void env_relocate_spec(void)
  234. {
  235. #if !defined(ENV_IS_EMBEDDED)
  236. ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
  237. struct mmc *mmc = find_mmc_device(CONFIG_SYS_MMC_ENV_DEV);
  238. u32 offset;
  239. int ret;
  240. if (init_mmc_for_env(mmc)) {
  241. ret = 1;
  242. goto err;
  243. }
  244. if (mmc_get_env_addr(mmc, 0, &offset)) {
  245. ret = 1;
  246. goto fini;
  247. }
  248. if (read_env(mmc, CONFIG_ENV_SIZE, offset, buf)) {
  249. ret = 1;
  250. goto fini;
  251. }
  252. env_import(buf, 1);
  253. ret = 0;
  254. fini:
  255. fini_mmc_for_env(mmc);
  256. err:
  257. if (ret)
  258. set_default_env(NULL);
  259. #endif
  260. }
  261. #endif /* CONFIG_ENV_OFFSET_REDUND */