2 * (C) Copyright 2008-2011 Freescale Semiconductor, Inc.
4 * See file CREDITS for list of people who contributed to this
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.
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.
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,
28 #include <environment.h>
29 #include <linux/stddef.h>
35 char *env_name_spec = "MMC";
37 #ifdef ENV_IS_EMBEDDED
38 env_t *env_ptr = &environment;
39 #else /* ! ENV_IS_EMBEDDED */
41 #endif /* ENV_IS_EMBEDDED */
43 DECLARE_GLOBAL_DATA_PTR;
45 #if !defined(CONFIG_ENV_OFFSET)
46 #define CONFIG_ENV_OFFSET 0
49 static int __mmc_get_env_addr(struct mmc *mmc, u32 *env_addr)
51 *env_addr = CONFIG_ENV_OFFSET;
54 int mmc_get_env_addr(struct mmc *mmc, u32 *env_addr)
55 __attribute__((weak, alias("__mmc_get_env_addr")));
60 gd->env_addr = (ulong)&default_environment[0];
66 static int init_mmc_for_env(struct mmc *mmc)
69 puts("No MMC card found\n");
74 puts("MMC init failed\n");
78 #ifdef CONFIG_SYS_MMC_ENV_PART
79 if (CONFIG_SYS_MMC_ENV_PART != mmc->part_num) {
80 if (mmc_switch_part(CONFIG_SYS_MMC_ENV_DEV,
81 CONFIG_SYS_MMC_ENV_PART)) {
82 puts("MMC partition switch failed\n");
91 static void fini_mmc_for_env(struct mmc *mmc)
93 #ifdef CONFIG_SYS_MMC_ENV_PART
94 if (CONFIG_SYS_MMC_ENV_PART != mmc->part_num)
95 mmc_switch_part(CONFIG_SYS_MMC_ENV_DEV,
100 #ifdef CONFIG_CMD_SAVEENV
101 static inline int write_env(struct mmc *mmc, unsigned long size,
102 unsigned long offset, const void *buffer)
104 uint blk_start, blk_cnt, n;
106 blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len;
107 blk_cnt = ALIGN(size, mmc->write_bl_len) / mmc->write_bl_len;
109 n = mmc->block_dev.block_write(CONFIG_SYS_MMC_ENV_DEV, blk_start,
110 blk_cnt, (u_char *)buffer);
112 return (n == blk_cnt) ? 0 : -1;
117 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
120 struct mmc *mmc = find_mmc_device(CONFIG_SYS_MMC_ENV_DEV);
124 if (init_mmc_for_env(mmc))
127 if (mmc_get_env_addr(mmc, &offset)) {
132 res = (char *)&env_new->data;
133 len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL);
135 error("Cannot export environment: errno = %d\n", errno);
140 env_new->crc = crc32(0, &env_new->data[0], ENV_SIZE);
141 printf("Writing to MMC(%d)... ", CONFIG_SYS_MMC_ENV_DEV);
142 if (write_env(mmc, CONFIG_ENV_SIZE, offset, (u_char *)env_new)) {
152 fini_mmc_for_env(mmc);
155 #endif /* CONFIG_CMD_SAVEENV */
157 static inline int read_env(struct mmc *mmc, unsigned long size,
158 unsigned long offset, const void *buffer)
160 uint blk_start, blk_cnt, n;
162 blk_start = ALIGN(offset, mmc->read_bl_len) / mmc->read_bl_len;
163 blk_cnt = ALIGN(size, mmc->read_bl_len) / mmc->read_bl_len;
165 n = mmc->block_dev.block_read(CONFIG_SYS_MMC_ENV_DEV, blk_start,
166 blk_cnt, (uchar *)buffer);
168 return (n == blk_cnt) ? 0 : -1;
171 void env_relocate_spec(void)
173 #if !defined(ENV_IS_EMBEDDED)
174 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
175 struct mmc *mmc = find_mmc_device(CONFIG_SYS_MMC_ENV_DEV);
179 if (init_mmc_for_env(mmc)) {
184 if (mmc_get_env_addr(mmc, &offset)) {
189 if (read_env(mmc, CONFIG_ENV_SIZE, offset, buf)) {
198 fini_mmc_for_env(mmc);
201 set_default_env(NULL);