blob: ce2167121004303ac4972f64cd31db37d16c6ccd [file] [log] [blame]
Terry Lvb4eceac2010-05-17 10:57:01 +08001/*
Mingkai Hua55fce32011-01-24 17:09:55 +00002 * (C) Copyright 2008-2011 Freescale Semiconductor, Inc.
Terry Lvb4eceac2010-05-17 10:57:01 +08003 *
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
23/* #define DEBUG */
24
25#include <common.h>
26
27#include <command.h>
28#include <environment.h>
29#include <linux/stddef.h>
30#include <malloc.h>
31#include <mmc.h>
Lei Wen9f84f212010-11-10 07:39:23 +080032#include <search.h>
Lei Wenff833ed2010-10-13 11:07:21 +080033#include <errno.h>
Terry Lvb4eceac2010-05-17 10:57:01 +080034
Terry Lvb4eceac2010-05-17 10:57:01 +080035char *env_name_spec = "MMC";
36
37#ifdef ENV_IS_EMBEDDED
Igor Grinberg23b54b92011-11-17 06:07:23 +000038env_t *env_ptr = &environment;
Terry Lvb4eceac2010-05-17 10:57:01 +080039#else /* ! ENV_IS_EMBEDDED */
Igor Grinbergecbfb622011-11-07 01:14:05 +000040env_t *env_ptr;
Terry Lvb4eceac2010-05-17 10:57:01 +080041#endif /* ENV_IS_EMBEDDED */
42
Terry Lvb4eceac2010-05-17 10:57:01 +080043DECLARE_GLOBAL_DATA_PTR;
44
Mingkai Hua55fce32011-01-24 17:09:55 +000045#if !defined(CONFIG_ENV_OFFSET)
46#define CONFIG_ENV_OFFSET 0
47#endif
48
49static int __mmc_get_env_addr(struct mmc *mmc, u32 *env_addr)
50{
51 *env_addr = CONFIG_ENV_OFFSET;
52 return 0;
53}
Igor Grinbergecbfb622011-11-07 01:14:05 +000054int mmc_get_env_addr(struct mmc *mmc, u32 *env_addr)
55 __attribute__((weak, alias("__mmc_get_env_addr")));
Mingkai Hua55fce32011-01-24 17:09:55 +000056
Terry Lvb4eceac2010-05-17 10:57:01 +080057int env_init(void)
58{
59 /* use default */
Igor Grinbergecbfb622011-11-07 01:14:05 +000060 gd->env_addr = (ulong)&default_environment[0];
61 gd->env_valid = 1;
Terry Lvb4eceac2010-05-17 10:57:01 +080062
63 return 0;
64}
65
Igor Grinbergecbfb622011-11-07 01:14:05 +000066static int init_mmc_for_env(struct mmc *mmc)
Terry Lvb4eceac2010-05-17 10:57:01 +080067{
68 if (!mmc) {
69 puts("No MMC card found\n");
70 return -1;
71 }
72
73 if (mmc_init(mmc)) {
74 puts("MMC init failed\n");
Igor Grinbergecbfb622011-11-07 01:14:05 +000075 return -1;
Terry Lvb4eceac2010-05-17 10:57:01 +080076 }
77
Stephen Warren27ee0f32012-07-30 10:55:44 +000078#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");
83 return -1;
84 }
85 }
86#endif
87
Terry Lvb4eceac2010-05-17 10:57:01 +080088 return 0;
89}
90
Stephen Warren27ee0f32012-07-30 10:55:44 +000091static void fini_mmc_for_env(struct mmc *mmc)
92{
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,
96 mmc->part_num);
97#endif
98}
99
Terry Lvb4eceac2010-05-17 10:57:01 +0800100#ifdef CONFIG_CMD_SAVEENV
Igor Grinbergecbfb622011-11-07 01:14:05 +0000101static inline int write_env(struct mmc *mmc, unsigned long size,
102 unsigned long offset, const void *buffer)
Terry Lvb4eceac2010-05-17 10:57:01 +0800103{
104 uint blk_start, blk_cnt, n;
105
Igor Grinbergecbfb622011-11-07 01:14:05 +0000106 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;
Terry Lvb4eceac2010-05-17 10:57:01 +0800108
109 n = mmc->block_dev.block_write(CONFIG_SYS_MMC_ENV_DEV, blk_start,
110 blk_cnt, (u_char *)buffer);
111
112 return (n == blk_cnt) ? 0 : -1;
113}
114
115int saveenv(void)
116{
Stephen Warren42a3c442012-05-24 11:38:33 +0000117 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
Lei Wenff833ed2010-10-13 11:07:21 +0800118 ssize_t len;
119 char *res;
Terry Lvb4eceac2010-05-17 10:57:01 +0800120 struct mmc *mmc = find_mmc_device(CONFIG_SYS_MMC_ENV_DEV);
Igor Grinbergecbfb622011-11-07 01:14:05 +0000121 u32 offset;
Stephen Warren27ee0f32012-07-30 10:55:44 +0000122 int ret;
Terry Lvb4eceac2010-05-17 10:57:01 +0800123
Stephen Warren27ee0f32012-07-30 10:55:44 +0000124 if (init_mmc_for_env(mmc))
Terry Lvb4eceac2010-05-17 10:57:01 +0800125 return 1;
126
Stephen Warren27ee0f32012-07-30 10:55:44 +0000127 if (mmc_get_env_addr(mmc, &offset)) {
128 ret = 1;
129 goto fini;
130 }
131
Stephen Warren42a3c442012-05-24 11:38:33 +0000132 res = (char *)&env_new->data;
Joe Hershberger79a905e2012-12-11 22:16:23 -0600133 len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
Lei Wenff833ed2010-10-13 11:07:21 +0800134 if (len < 0) {
135 error("Cannot export environment: errno = %d\n", errno);
Stephen Warren27ee0f32012-07-30 10:55:44 +0000136 ret = 1;
137 goto fini;
Lei Wenff833ed2010-10-13 11:07:21 +0800138 }
Igor Grinbergecbfb622011-11-07 01:14:05 +0000139
Stephen Warren42a3c442012-05-24 11:38:33 +0000140 env_new->crc = crc32(0, &env_new->data[0], ENV_SIZE);
Terry Lvb4eceac2010-05-17 10:57:01 +0800141 printf("Writing to MMC(%d)... ", CONFIG_SYS_MMC_ENV_DEV);
Stephen Warren42a3c442012-05-24 11:38:33 +0000142 if (write_env(mmc, CONFIG_ENV_SIZE, offset, (u_char *)env_new)) {
Terry Lvb4eceac2010-05-17 10:57:01 +0800143 puts("failed\n");
Stephen Warren27ee0f32012-07-30 10:55:44 +0000144 ret = 1;
145 goto fini;
Terry Lvb4eceac2010-05-17 10:57:01 +0800146 }
147
148 puts("done\n");
Stephen Warren27ee0f32012-07-30 10:55:44 +0000149 ret = 0;
150
151fini:
152 fini_mmc_for_env(mmc);
153 return ret;
Terry Lvb4eceac2010-05-17 10:57:01 +0800154}
155#endif /* CONFIG_CMD_SAVEENV */
156
Igor Grinbergecbfb622011-11-07 01:14:05 +0000157static inline int read_env(struct mmc *mmc, unsigned long size,
158 unsigned long offset, const void *buffer)
Terry Lvb4eceac2010-05-17 10:57:01 +0800159{
160 uint blk_start, blk_cnt, n;
161
Igor Grinbergecbfb622011-11-07 01:14:05 +0000162 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;
Terry Lvb4eceac2010-05-17 10:57:01 +0800164
165 n = mmc->block_dev.block_read(CONFIG_SYS_MMC_ENV_DEV, blk_start,
166 blk_cnt, (uchar *)buffer);
167
168 return (n == blk_cnt) ? 0 : -1;
169}
170
171void env_relocate_spec(void)
172{
173#if !defined(ENV_IS_EMBEDDED)
Stephen Warren42a3c442012-05-24 11:38:33 +0000174 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
Terry Lvb4eceac2010-05-17 10:57:01 +0800175 struct mmc *mmc = find_mmc_device(CONFIG_SYS_MMC_ENV_DEV);
Mingkai Hua55fce32011-01-24 17:09:55 +0000176 u32 offset;
Stephen Warren27ee0f32012-07-30 10:55:44 +0000177 int ret;
178
179 if (init_mmc_for_env(mmc)) {
180 ret = 1;
181 goto err;
182 }
Terry Lvb4eceac2010-05-17 10:57:01 +0800183
Stephen Warren27ee0f32012-07-30 10:55:44 +0000184 if (mmc_get_env_addr(mmc, &offset)) {
185 ret = 1;
186 goto fini;
187 }
Terry Lvb4eceac2010-05-17 10:57:01 +0800188
Stephen Warren27ee0f32012-07-30 10:55:44 +0000189 if (read_env(mmc, CONFIG_ENV_SIZE, offset, buf)) {
190 ret = 1;
191 goto fini;
192 }
Terry Lvb4eceac2010-05-17 10:57:01 +0800193
Steve Sakomana93ec572010-10-11 05:51:39 -0700194 env_import(buf, 1);
Stephen Warren27ee0f32012-07-30 10:55:44 +0000195 ret = 0;
196
197fini:
198 fini_mmc_for_env(mmc);
199err:
200 if (ret)
201 set_default_env(NULL);
Terry Lvb4eceac2010-05-17 10:57:01 +0800202#endif
203}