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 /* references to names in env_common.c */
36 extern uchar default_environment[];
38 char *env_name_spec = "MMC";
40 #ifdef ENV_IS_EMBEDDED
41 extern uchar environment[];
42 env_t *env_ptr = (env_t *)(&environment[0]);
43 #else /* ! ENV_IS_EMBEDDED */
44 env_t *env_ptr = NULL;
45 #endif /* ENV_IS_EMBEDDED */
48 #if !defined(ENV_IS_EMBEDDED)
49 static void use_default(void);
52 DECLARE_GLOBAL_DATA_PTR;
54 #if !defined(CONFIG_ENV_OFFSET)
55 #define CONFIG_ENV_OFFSET 0
58 static int __mmc_get_env_addr(struct mmc *mmc, u32 *env_addr)
60 *env_addr = CONFIG_ENV_OFFSET;
63 __attribute__((weak, alias("__mmc_get_env_addr")))
64 int mmc_get_env_addr(struct mmc *mmc, u32 *env_addr);
67 uchar env_get_char_spec(int index)
69 return *((uchar *)(gd->env_addr + index));
75 gd->env_addr = (ulong)&default_environment[0];
81 int init_mmc_for_env(struct mmc *mmc)
84 puts("No MMC card found\n");
89 puts("MMC init failed\n");
96 #ifdef CONFIG_CMD_SAVEENV
98 inline int write_env(struct mmc *mmc, unsigned long size,
99 unsigned long offset, const void *buffer)
101 uint blk_start, blk_cnt, n;
103 blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len;
104 blk_cnt = ALIGN(size, mmc->write_bl_len) / mmc->write_bl_len;
106 n = mmc->block_dev.block_write(CONFIG_SYS_MMC_ENV_DEV, blk_start,
107 blk_cnt, (u_char *)buffer);
109 return (n == blk_cnt) ? 0 : -1;
117 struct mmc *mmc = find_mmc_device(CONFIG_SYS_MMC_ENV_DEV);
120 if (init_mmc_for_env(mmc))
123 if(mmc_get_env_addr(mmc, &offset))
126 res = (char *)&env_new.data;
127 len = hexport_r(&env_htab, '\0', &res, ENV_SIZE);
129 error("Cannot export environment: errno = %d\n", errno);
132 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
133 printf("Writing to MMC(%d)... ", CONFIG_SYS_MMC_ENV_DEV);
134 if (write_env(mmc, CONFIG_ENV_SIZE, offset, (u_char *)&env_new)) {
142 #endif /* CONFIG_CMD_SAVEENV */
144 inline int read_env(struct mmc *mmc, unsigned long size,
145 unsigned long offset, const void *buffer)
147 uint blk_start, blk_cnt, n;
149 blk_start = ALIGN(offset, mmc->read_bl_len) / mmc->read_bl_len;
150 blk_cnt = ALIGN(size, mmc->read_bl_len) / mmc->read_bl_len;
152 n = mmc->block_dev.block_read(CONFIG_SYS_MMC_ENV_DEV, blk_start,
153 blk_cnt, (uchar *)buffer);
155 return (n == blk_cnt) ? 0 : -1;
158 void env_relocate_spec(void)
160 #if !defined(ENV_IS_EMBEDDED)
161 char buf[CONFIG_ENV_SIZE];
163 struct mmc *mmc = find_mmc_device(CONFIG_SYS_MMC_ENV_DEV);
166 if (init_mmc_for_env(mmc)) {
171 if(mmc_get_env_addr(mmc, &offset)) {
176 if (read_env(mmc, CONFIG_ENV_SIZE, offset, buf)) {
185 #if !defined(ENV_IS_EMBEDDED)
186 static void use_default()
188 set_default_env(NULL);