2 * (C) Copyright 2008-2010 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>
33 /* references to names in env_common.c */
34 extern uchar default_environment[];
36 char *env_name_spec = "MMC";
38 #ifdef ENV_IS_EMBEDDED
39 extern uchar environment[];
40 env_t *env_ptr = (env_t *)(&environment[0]);
41 #else /* ! ENV_IS_EMBEDDED */
42 env_t *env_ptr = NULL;
43 #endif /* ENV_IS_EMBEDDED */
46 #if !defined(ENV_IS_EMBEDDED)
47 static void use_default(void);
50 DECLARE_GLOBAL_DATA_PTR;
52 uchar env_get_char_spec(int index)
54 return *((uchar *)(gd->env_addr + index));
60 gd->env_addr = (ulong)&default_environment[0];
66 int init_mmc_for_env(struct mmc *mmc)
69 puts("No MMC card found\n");
74 puts("MMC init failed\n");
81 #ifdef CONFIG_CMD_SAVEENV
83 inline int write_env(struct mmc *mmc, unsigned long size,
84 unsigned long offset, const void *buffer)
86 uint blk_start, blk_cnt, n;
88 blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len;
89 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);
94 return (n == blk_cnt) ? 0 : -1;
99 struct mmc *mmc = find_mmc_device(CONFIG_SYS_MMC_ENV_DEV);
101 if (init_mmc_for_env(mmc))
104 printf("Writing to MMC(%d)... ", CONFIG_SYS_MMC_ENV_DEV);
105 if (write_env(mmc, CONFIG_ENV_SIZE, CONFIG_ENV_OFFSET, env_ptr)) {
113 #endif /* CONFIG_CMD_SAVEENV */
115 inline int read_env(struct mmc *mmc, unsigned long size,
116 unsigned long offset, const void *buffer)
118 uint blk_start, blk_cnt, n;
120 blk_start = ALIGN(offset, mmc->read_bl_len) / mmc->read_bl_len;
121 blk_cnt = ALIGN(size, mmc->read_bl_len) / mmc->read_bl_len;
123 n = mmc->block_dev.block_read(CONFIG_SYS_MMC_ENV_DEV, blk_start,
124 blk_cnt, (uchar *)buffer);
126 return (n == blk_cnt) ? 0 : -1;
129 void env_relocate_spec(void)
131 #if !defined(ENV_IS_EMBEDDED)
132 struct mmc *mmc = find_mmc_device(CONFIG_SYS_MMC_ENV_DEV);
134 if (init_mmc_for_env(mmc))
137 if (read_env(mmc, CONFIG_ENV_SIZE, CONFIG_ENV_OFFSET, env_ptr))
138 return use_default();
140 if (crc32(0, env_ptr->data, ENV_SIZE) != env_ptr->crc)
141 return use_default();
147 #if !defined(ENV_IS_EMBEDDED)
148 static void use_default()
150 set_default_env(NULL);