1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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>
32#include <search.h>
33#include <errno.h>
34
35char *env_name_spec = "MMC";
36
37#ifdef ENV_IS_EMBEDDED
38env_t *env_ptr = &environment;
39#else
40env_t *env_ptr;
41#endif
42
43DECLARE_GLOBAL_DATA_PTR;
44
45#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}
54int mmc_get_env_addr(struct mmc *mmc, u32 *env_addr)
55 __attribute__((weak, alias("__mmc_get_env_addr")));
56
57int env_init(void)
58{
59
60 gd->env_addr = (ulong)&default_environment[0];
61 gd->env_valid = 1;
62
63 return 0;
64}
65
66static int init_mmc_for_env(struct mmc *mmc)
67{
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");
75 return -1;
76 }
77
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");
83 return -1;
84 }
85 }
86#endif
87
88 return 0;
89}
90
91static 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
100#ifdef CONFIG_CMD_SAVEENV
101static inline int write_env(struct mmc *mmc, unsigned long size,
102 unsigned long offset, const void *buffer)
103{
104 uint blk_start, blk_cnt, n;
105
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;
108
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{
117 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
118 ssize_t len;
119 char *res;
120 struct mmc *mmc = find_mmc_device(CONFIG_SYS_MMC_ENV_DEV);
121 u32 offset;
122 int ret;
123
124 if (init_mmc_for_env(mmc))
125 return 1;
126
127 if (mmc_get_env_addr(mmc, &offset)) {
128 ret = 1;
129 goto fini;
130 }
131
132 res = (char *)&env_new->data;
133 len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL);
134 if (len < 0) {
135 error("Cannot export environment: errno = %d\n", errno);
136 ret = 1;
137 goto fini;
138 }
139
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)) {
143 puts("failed\n");
144 ret = 1;
145 goto fini;
146 }
147
148 puts("done\n");
149 ret = 0;
150
151fini:
152 fini_mmc_for_env(mmc);
153 return ret;
154}
155#endif
156
157static inline int read_env(struct mmc *mmc, unsigned long size,
158 unsigned long offset, const void *buffer)
159{
160 uint blk_start, blk_cnt, n;
161
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;
164
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)
174 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
175 struct mmc *mmc = find_mmc_device(CONFIG_SYS_MMC_ENV_DEV);
176 u32 offset;
177 int ret;
178
179 if (init_mmc_for_env(mmc)) {
180 ret = 1;
181 goto err;
182 }
183
184 if (mmc_get_env_addr(mmc, &offset)) {
185 ret = 1;
186 goto fini;
187 }
188
189 if (read_env(mmc, CONFIG_ENV_SIZE, offset, buf)) {
190 ret = 1;
191 goto fini;
192 }
193
194 env_import(buf, 1);
195 ret = 0;
196
197fini:
198 fini_mmc_for_env(mmc);
199err:
200 if (ret)
201 set_default_env(NULL);
202#endif
203}
204