blob: ef8186c2c7acb7631822aef429b4267e6a8e52ff [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 */
40env_t *env_ptr = NULL;
41#endif /* ENV_IS_EMBEDDED */
42
43/* local functions */
44#if !defined(ENV_IS_EMBEDDED)
45static void use_default(void);
46#endif
47
48DECLARE_GLOBAL_DATA_PTR;
49
Mingkai Hua55fce32011-01-24 17:09:55 +000050#if !defined(CONFIG_ENV_OFFSET)
51#define CONFIG_ENV_OFFSET 0
52#endif
53
54static int __mmc_get_env_addr(struct mmc *mmc, u32 *env_addr)
55{
56 *env_addr = CONFIG_ENV_OFFSET;
57 return 0;
58}
59__attribute__((weak, alias("__mmc_get_env_addr")))
60int mmc_get_env_addr(struct mmc *mmc, u32 *env_addr);
61
62
Terry Lvb4eceac2010-05-17 10:57:01 +080063uchar env_get_char_spec(int index)
64{
65 return *((uchar *)(gd->env_addr + index));
66}
67
68int env_init(void)
69{
70 /* use default */
71 gd->env_addr = (ulong)&default_environment[0];
72 gd->env_valid = 1;
73
74 return 0;
75}
76
77int init_mmc_for_env(struct mmc *mmc)
78{
79 if (!mmc) {
80 puts("No MMC card found\n");
81 return -1;
82 }
83
84 if (mmc_init(mmc)) {
85 puts("MMC init failed\n");
86 return -1;
87 }
88
89 return 0;
90}
91
92#ifdef CONFIG_CMD_SAVEENV
93
94inline int write_env(struct mmc *mmc, unsigned long size,
95 unsigned long offset, const void *buffer)
96{
97 uint blk_start, blk_cnt, n;
98
99 blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len;
100 blk_cnt = ALIGN(size, mmc->write_bl_len) / mmc->write_bl_len;
101
102 n = mmc->block_dev.block_write(CONFIG_SYS_MMC_ENV_DEV, blk_start,
103 blk_cnt, (u_char *)buffer);
104
105 return (n == blk_cnt) ? 0 : -1;
106}
107
108int saveenv(void)
109{
Lei Wenff833ed2010-10-13 11:07:21 +0800110 env_t env_new;
111 ssize_t len;
112 char *res;
Terry Lvb4eceac2010-05-17 10:57:01 +0800113 struct mmc *mmc = find_mmc_device(CONFIG_SYS_MMC_ENV_DEV);
Mingkai Hua55fce32011-01-24 17:09:55 +0000114 u32 offset;
Terry Lvb4eceac2010-05-17 10:57:01 +0800115
116 if (init_mmc_for_env(mmc))
117 return 1;
118
Mingkai Hua55fce32011-01-24 17:09:55 +0000119 if(mmc_get_env_addr(mmc, &offset))
120 return 1;
121
Lei Wenff833ed2010-10-13 11:07:21 +0800122 res = (char *)&env_new.data;
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100123 len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL);
Lei Wenff833ed2010-10-13 11:07:21 +0800124 if (len < 0) {
125 error("Cannot export environment: errno = %d\n", errno);
126 return 1;
127 }
128 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
Terry Lvb4eceac2010-05-17 10:57:01 +0800129 printf("Writing to MMC(%d)... ", CONFIG_SYS_MMC_ENV_DEV);
Mingkai Hua55fce32011-01-24 17:09:55 +0000130 if (write_env(mmc, CONFIG_ENV_SIZE, offset, (u_char *)&env_new)) {
Terry Lvb4eceac2010-05-17 10:57:01 +0800131 puts("failed\n");
132 return 1;
133 }
134
135 puts("done\n");
136 return 0;
137}
138#endif /* CONFIG_CMD_SAVEENV */
139
140inline int read_env(struct mmc *mmc, unsigned long size,
141 unsigned long offset, const void *buffer)
142{
143 uint blk_start, blk_cnt, n;
144
145 blk_start = ALIGN(offset, mmc->read_bl_len) / mmc->read_bl_len;
146 blk_cnt = ALIGN(size, mmc->read_bl_len) / mmc->read_bl_len;
147
148 n = mmc->block_dev.block_read(CONFIG_SYS_MMC_ENV_DEV, blk_start,
149 blk_cnt, (uchar *)buffer);
150
151 return (n == blk_cnt) ? 0 : -1;
152}
153
154void env_relocate_spec(void)
155{
156#if !defined(ENV_IS_EMBEDDED)
Mingkai Hua55fce32011-01-24 17:09:55 +0000157 char buf[CONFIG_ENV_SIZE];
Steve Sakomana93ec572010-10-11 05:51:39 -0700158
Terry Lvb4eceac2010-05-17 10:57:01 +0800159 struct mmc *mmc = find_mmc_device(CONFIG_SYS_MMC_ENV_DEV);
Mingkai Hua55fce32011-01-24 17:09:55 +0000160 u32 offset;
Terry Lvb4eceac2010-05-17 10:57:01 +0800161
Steve Sakomana93ec572010-10-11 05:51:39 -0700162 if (init_mmc_for_env(mmc)) {
163 use_default();
Terry Lvb4eceac2010-05-17 10:57:01 +0800164 return;
Steve Sakomana93ec572010-10-11 05:51:39 -0700165 }
Terry Lvb4eceac2010-05-17 10:57:01 +0800166
Mingkai Hua55fce32011-01-24 17:09:55 +0000167 if(mmc_get_env_addr(mmc, &offset)) {
168 use_default();
169 return ;
170 }
171
172 if (read_env(mmc, CONFIG_ENV_SIZE, offset, buf)) {
Steve Sakomana93ec572010-10-11 05:51:39 -0700173 use_default();
174 return;
175 }
Terry Lvb4eceac2010-05-17 10:57:01 +0800176
Steve Sakomana93ec572010-10-11 05:51:39 -0700177 env_import(buf, 1);
Terry Lvb4eceac2010-05-17 10:57:01 +0800178#endif
179}
180
181#if !defined(ENV_IS_EMBEDDED)
182static void use_default()
183{
Steve Sakoman0e657e82010-10-05 15:31:38 -0700184 set_default_env(NULL);
Terry Lvb4eceac2010-05-17 10:57:01 +0800185}
186#endif