blob: c7fef188cd21b9a6b816ab7b8c997de788b2d822 [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 *
Wolfgang Denkbd8ec7e2013-10-07 13:07:26 +02004 * SPDX-License-Identifier: GPL-2.0+
Terry Lvb4eceac2010-05-17 10:57:01 +08005 */
6
7/* #define DEBUG */
8
9#include <common.h>
10
11#include <command.h>
12#include <environment.h>
13#include <linux/stddef.h>
14#include <malloc.h>
Simon Glass2dd337a2015-09-02 17:24:58 -060015#include <memalign.h>
Terry Lvb4eceac2010-05-17 10:57:01 +080016#include <mmc.h>
Lei Wen9f84f212010-11-10 07:39:23 +080017#include <search.h>
Lei Wenff833ed2010-10-13 11:07:21 +080018#include <errno.h>
Terry Lvb4eceac2010-05-17 10:57:01 +080019
Michael Heimpolda1d25a32013-04-10 10:36:19 +000020#if defined(CONFIG_ENV_SIZE_REDUND) && \
21 (CONFIG_ENV_SIZE_REDUND != CONFIG_ENV_SIZE)
22#error CONFIG_ENV_SIZE_REDUND should be the same as CONFIG_ENV_SIZE
23#endif
24
Terry Lvb4eceac2010-05-17 10:57:01 +080025char *env_name_spec = "MMC";
26
27#ifdef ENV_IS_EMBEDDED
Igor Grinberg23b54b92011-11-17 06:07:23 +000028env_t *env_ptr = &environment;
Terry Lvb4eceac2010-05-17 10:57:01 +080029#else /* ! ENV_IS_EMBEDDED */
Igor Grinbergecbfb622011-11-07 01:14:05 +000030env_t *env_ptr;
Terry Lvb4eceac2010-05-17 10:57:01 +080031#endif /* ENV_IS_EMBEDDED */
32
Terry Lvb4eceac2010-05-17 10:57:01 +080033DECLARE_GLOBAL_DATA_PTR;
34
Mingkai Hua55fce32011-01-24 17:09:55 +000035#if !defined(CONFIG_ENV_OFFSET)
36#define CONFIG_ENV_OFFSET 0
37#endif
38
Michael Heimpolda1d25a32013-04-10 10:36:19 +000039__weak int mmc_get_env_addr(struct mmc *mmc, int copy, u32 *env_addr)
Mingkai Hua55fce32011-01-24 17:09:55 +000040{
Stephen Warren24dc2032013-06-11 15:14:02 -060041 s64 offset;
42
43 offset = CONFIG_ENV_OFFSET;
Michael Heimpolda1d25a32013-04-10 10:36:19 +000044#ifdef CONFIG_ENV_OFFSET_REDUND
45 if (copy)
Stephen Warren24dc2032013-06-11 15:14:02 -060046 offset = CONFIG_ENV_OFFSET_REDUND;
Michael Heimpolda1d25a32013-04-10 10:36:19 +000047#endif
Stephen Warren24dc2032013-06-11 15:14:02 -060048
49 if (offset < 0)
50 offset += mmc->capacity;
51
52 *env_addr = offset;
53
Mingkai Hua55fce32011-01-24 17:09:55 +000054 return 0;
55}
Mingkai Hua55fce32011-01-24 17:09:55 +000056
Clemens Gruberf7b61442016-01-20 15:43:37 +010057__weak int mmc_get_env_dev(void)
58{
59 return CONFIG_SYS_MMC_ENV_DEV;
60}
61
Terry Lvb4eceac2010-05-17 10:57:01 +080062int env_init(void)
63{
64 /* use default */
Igor Grinbergecbfb622011-11-07 01:14:05 +000065 gd->env_addr = (ulong)&default_environment[0];
66 gd->env_valid = 1;
Terry Lvb4eceac2010-05-17 10:57:01 +080067
68 return 0;
69}
70
Tom Rinic929f822014-03-28 12:03:34 -040071#ifdef CONFIG_SYS_MMC_ENV_PART
Dmitry Lifshitz97d2fb92014-07-30 13:19:06 +030072__weak uint mmc_get_env_part(struct mmc *mmc)
73{
74 return CONFIG_SYS_MMC_ENV_PART;
75}
76
Stephen Warren1e0f92a2015-12-07 11:38:49 -070077static unsigned char env_mmc_orig_hwpart;
78
Dmitry Lifshitz97d2fb92014-07-30 13:19:06 +030079static int mmc_set_env_part(struct mmc *mmc)
80{
81 uint part = mmc_get_env_part(mmc);
Clemens Gruberf7b61442016-01-20 15:43:37 +010082 int dev = mmc_get_env_dev();
Dmitry Lifshitz97d2fb92014-07-30 13:19:06 +030083 int ret = 0;
Tom Rinic929f822014-03-28 12:03:34 -040084
85#ifdef CONFIG_SPL_BUILD
86 dev = 0;
87#endif
Dmitry Lifshitz97d2fb92014-07-30 13:19:06 +030088
Simon Glass11f2bb62016-05-01 13:52:29 -060089 env_mmc_orig_hwpart = mmc_get_blk_desc(mmc)->hwpart;
90 ret = blk_select_hwpart_devnum(IF_TYPE_MMC, dev, part);
Stephen Warren1e0f92a2015-12-07 11:38:49 -070091 if (ret)
92 puts("MMC partition switch failed\n");
Dmitry Lifshitz97d2fb92014-07-30 13:19:06 +030093
94 return ret;
95}
96#else
97static inline int mmc_set_env_part(struct mmc *mmc) {return 0; };
Tom Rinic929f822014-03-28 12:03:34 -040098#endif
99
Tim Harveyeda75c32015-05-08 14:52:09 -0700100static const char *init_mmc_for_env(struct mmc *mmc)
Dmitry Lifshitz97d2fb92014-07-30 13:19:06 +0300101{
Tim Harveyeda75c32015-05-08 14:52:09 -0700102 if (!mmc)
Hans de Goede8764fd62015-08-15 20:05:01 +0200103 return "!No MMC card found";
Terry Lvb4eceac2010-05-17 10:57:01 +0800104
Tim Harveyeda75c32015-05-08 14:52:09 -0700105 if (mmc_init(mmc))
Hans de Goede8764fd62015-08-15 20:05:01 +0200106 return "!MMC init failed";
Stephen Warren27ee0f32012-07-30 10:55:44 +0000107
Tim Harveyeda75c32015-05-08 14:52:09 -0700108 if (mmc_set_env_part(mmc))
Hans de Goede8764fd62015-08-15 20:05:01 +0200109 return "!MMC partition switch failed";
Tim Harveyeda75c32015-05-08 14:52:09 -0700110
111 return NULL;
Terry Lvb4eceac2010-05-17 10:57:01 +0800112}
113
Stephen Warren27ee0f32012-07-30 10:55:44 +0000114static void fini_mmc_for_env(struct mmc *mmc)
115{
116#ifdef CONFIG_SYS_MMC_ENV_PART
Clemens Gruberf7b61442016-01-20 15:43:37 +0100117 int dev = mmc_get_env_dev();
Tom Rinic929f822014-03-28 12:03:34 -0400118
119#ifdef CONFIG_SPL_BUILD
120 dev = 0;
121#endif
Simon Glass11f2bb62016-05-01 13:52:29 -0600122 blk_select_hwpart_devnum(IF_TYPE_MMC, dev, env_mmc_orig_hwpart);
Stephen Warren27ee0f32012-07-30 10:55:44 +0000123#endif
124}
125
Terry Lvb4eceac2010-05-17 10:57:01 +0800126#ifdef CONFIG_CMD_SAVEENV
Igor Grinbergecbfb622011-11-07 01:14:05 +0000127static inline int write_env(struct mmc *mmc, unsigned long size,
128 unsigned long offset, const void *buffer)
Terry Lvb4eceac2010-05-17 10:57:01 +0800129{
130 uint blk_start, blk_cnt, n;
131
Igor Grinbergecbfb622011-11-07 01:14:05 +0000132 blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len;
133 blk_cnt = ALIGN(size, mmc->write_bl_len) / mmc->write_bl_len;
Terry Lvb4eceac2010-05-17 10:57:01 +0800134
Stephen Warrene73f2962015-12-07 11:38:48 -0700135 n = mmc->block_dev.block_write(&mmc->block_dev, blk_start,
Terry Lvb4eceac2010-05-17 10:57:01 +0800136 blk_cnt, (u_char *)buffer);
137
138 return (n == blk_cnt) ? 0 : -1;
139}
140
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000141#ifdef CONFIG_ENV_OFFSET_REDUND
142static unsigned char env_flags;
143#endif
144
Terry Lvb4eceac2010-05-17 10:57:01 +0800145int saveenv(void)
146{
Tom Rinia840b8e2013-04-05 14:55:21 -0400147 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
Clemens Gruberf7b61442016-01-20 15:43:37 +0100148 int dev = mmc_get_env_dev();
149 struct mmc *mmc = find_mmc_device(dev);
Igor Grinbergecbfb622011-11-07 01:14:05 +0000150 u32 offset;
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000151 int ret, copy = 0;
Tim Harveyeda75c32015-05-08 14:52:09 -0700152 const char *errmsg;
Terry Lvb4eceac2010-05-17 10:57:01 +0800153
Tim Harveyeda75c32015-05-08 14:52:09 -0700154 errmsg = init_mmc_for_env(mmc);
155 if (errmsg) {
156 printf("%s\n", errmsg);
Terry Lvb4eceac2010-05-17 10:57:01 +0800157 return 1;
Tim Harveyeda75c32015-05-08 14:52:09 -0700158 }
Terry Lvb4eceac2010-05-17 10:57:01 +0800159
Marek Vasutd73c1292014-03-05 19:59:50 +0100160 ret = env_export(env_new);
161 if (ret)
Stephen Warren27ee0f32012-07-30 10:55:44 +0000162 goto fini;
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000163
164#ifdef CONFIG_ENV_OFFSET_REDUND
165 env_new->flags = ++env_flags; /* increase the serial */
166
167 if (gd->env_valid == 1)
168 copy = 1;
169#endif
170
171 if (mmc_get_env_addr(mmc, copy, &offset)) {
172 ret = 1;
173 goto fini;
174 }
175
Clemens Gruberf7b61442016-01-20 15:43:37 +0100176 printf("Writing to %sMMC(%d)... ", copy ? "redundant " : "", dev);
Stephen Warren42a3c442012-05-24 11:38:33 +0000177 if (write_env(mmc, CONFIG_ENV_SIZE, offset, (u_char *)env_new)) {
Terry Lvb4eceac2010-05-17 10:57:01 +0800178 puts("failed\n");
Stephen Warren27ee0f32012-07-30 10:55:44 +0000179 ret = 1;
180 goto fini;
Terry Lvb4eceac2010-05-17 10:57:01 +0800181 }
182
183 puts("done\n");
Stephen Warren27ee0f32012-07-30 10:55:44 +0000184 ret = 0;
185
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000186#ifdef CONFIG_ENV_OFFSET_REDUND
187 gd->env_valid = gd->env_valid == 2 ? 1 : 2;
188#endif
189
Stephen Warren27ee0f32012-07-30 10:55:44 +0000190fini:
191 fini_mmc_for_env(mmc);
192 return ret;
Terry Lvb4eceac2010-05-17 10:57:01 +0800193}
194#endif /* CONFIG_CMD_SAVEENV */
195
Igor Grinbergecbfb622011-11-07 01:14:05 +0000196static inline int read_env(struct mmc *mmc, unsigned long size,
197 unsigned long offset, const void *buffer)
Terry Lvb4eceac2010-05-17 10:57:01 +0800198{
199 uint blk_start, blk_cnt, n;
200
Igor Grinbergecbfb622011-11-07 01:14:05 +0000201 blk_start = ALIGN(offset, mmc->read_bl_len) / mmc->read_bl_len;
202 blk_cnt = ALIGN(size, mmc->read_bl_len) / mmc->read_bl_len;
Terry Lvb4eceac2010-05-17 10:57:01 +0800203
Stephen Warrene73f2962015-12-07 11:38:48 -0700204 n = mmc->block_dev.block_read(&mmc->block_dev, blk_start, blk_cnt,
205 (uchar *)buffer);
Terry Lvb4eceac2010-05-17 10:57:01 +0800206
207 return (n == blk_cnt) ? 0 : -1;
208}
209
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000210#ifdef CONFIG_ENV_OFFSET_REDUND
211void env_relocate_spec(void)
212{
213#if !defined(ENV_IS_EMBEDDED)
Tom Rinic929f822014-03-28 12:03:34 -0400214 struct mmc *mmc;
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000215 u32 offset1, offset2;
216 int read1_fail = 0, read2_fail = 0;
217 int crc1_ok = 0, crc2_ok = 0;
Markus Niebel6ef41fd2013-10-04 15:48:03 +0200218 env_t *ep;
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000219 int ret;
Clemens Gruberf7b61442016-01-20 15:43:37 +0100220 int dev = mmc_get_env_dev();
Tim Harveyeda75c32015-05-08 14:52:09 -0700221 const char *errmsg = NULL;
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000222
Markus Niebel6ef41fd2013-10-04 15:48:03 +0200223 ALLOC_CACHE_ALIGN_BUFFER(env_t, tmp_env1, 1);
224 ALLOC_CACHE_ALIGN_BUFFER(env_t, tmp_env2, 1);
225
Tom Rinic929f822014-03-28 12:03:34 -0400226#ifdef CONFIG_SPL_BUILD
227 dev = 0;
228#endif
229
230 mmc = find_mmc_device(dev);
231
Tim Harveyeda75c32015-05-08 14:52:09 -0700232 errmsg = init_mmc_for_env(mmc);
233 if (errmsg) {
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000234 ret = 1;
235 goto err;
236 }
237
238 if (mmc_get_env_addr(mmc, 0, &offset1) ||
239 mmc_get_env_addr(mmc, 1, &offset2)) {
240 ret = 1;
241 goto fini;
242 }
243
244 read1_fail = read_env(mmc, CONFIG_ENV_SIZE, offset1, tmp_env1);
245 read2_fail = read_env(mmc, CONFIG_ENV_SIZE, offset2, tmp_env2);
246
247 if (read1_fail && read2_fail)
248 puts("*** Error - No Valid Environment Area found\n");
249 else if (read1_fail || read2_fail)
250 puts("*** Warning - some problems detected "
251 "reading environment; recovered successfully\n");
252
253 crc1_ok = !read1_fail &&
254 (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc);
255 crc2_ok = !read2_fail &&
256 (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc);
257
258 if (!crc1_ok && !crc2_ok) {
Tim Harveyeda75c32015-05-08 14:52:09 -0700259 errmsg = "!bad CRC";
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000260 ret = 1;
261 goto fini;
262 } else if (crc1_ok && !crc2_ok) {
263 gd->env_valid = 1;
264 } else if (!crc1_ok && crc2_ok) {
265 gd->env_valid = 2;
266 } else {
267 /* both ok - check serial */
268 if (tmp_env1->flags == 255 && tmp_env2->flags == 0)
269 gd->env_valid = 2;
270 else if (tmp_env2->flags == 255 && tmp_env1->flags == 0)
271 gd->env_valid = 1;
272 else if (tmp_env1->flags > tmp_env2->flags)
273 gd->env_valid = 1;
274 else if (tmp_env2->flags > tmp_env1->flags)
275 gd->env_valid = 2;
276 else /* flags are equal - almost impossible */
277 gd->env_valid = 1;
278 }
279
280 free(env_ptr);
281
282 if (gd->env_valid == 1)
283 ep = tmp_env1;
284 else
285 ep = tmp_env2;
286
287 env_flags = ep->flags;
288 env_import((char *)ep, 0);
289 ret = 0;
290
291fini:
292 fini_mmc_for_env(mmc);
293err:
294 if (ret)
Tim Harveyeda75c32015-05-08 14:52:09 -0700295 set_default_env(errmsg);
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000296#endif
297}
298#else /* ! CONFIG_ENV_OFFSET_REDUND */
Terry Lvb4eceac2010-05-17 10:57:01 +0800299void env_relocate_spec(void)
300{
301#if !defined(ENV_IS_EMBEDDED)
Tom Rinia840b8e2013-04-05 14:55:21 -0400302 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
Tom Rinic929f822014-03-28 12:03:34 -0400303 struct mmc *mmc;
Mingkai Hua55fce32011-01-24 17:09:55 +0000304 u32 offset;
Stephen Warren27ee0f32012-07-30 10:55:44 +0000305 int ret;
Clemens Gruberf7b61442016-01-20 15:43:37 +0100306 int dev = mmc_get_env_dev();
Tim Harveyeda75c32015-05-08 14:52:09 -0700307 const char *errmsg;
Tom Rinic929f822014-03-28 12:03:34 -0400308
309#ifdef CONFIG_SPL_BUILD
310 dev = 0;
311#endif
312
313 mmc = find_mmc_device(dev);
Stephen Warren27ee0f32012-07-30 10:55:44 +0000314
Tim Harveyeda75c32015-05-08 14:52:09 -0700315 errmsg = init_mmc_for_env(mmc);
316 if (errmsg) {
Stephen Warren27ee0f32012-07-30 10:55:44 +0000317 ret = 1;
318 goto err;
319 }
Terry Lvb4eceac2010-05-17 10:57:01 +0800320
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000321 if (mmc_get_env_addr(mmc, 0, &offset)) {
Stephen Warren27ee0f32012-07-30 10:55:44 +0000322 ret = 1;
323 goto fini;
324 }
Terry Lvb4eceac2010-05-17 10:57:01 +0800325
Tom Rinia840b8e2013-04-05 14:55:21 -0400326 if (read_env(mmc, CONFIG_ENV_SIZE, offset, buf)) {
Tim Harveyeda75c32015-05-08 14:52:09 -0700327 errmsg = "!read failed";
Stephen Warren27ee0f32012-07-30 10:55:44 +0000328 ret = 1;
329 goto fini;
330 }
Terry Lvb4eceac2010-05-17 10:57:01 +0800331
Tom Rinia840b8e2013-04-05 14:55:21 -0400332 env_import(buf, 1);
Stephen Warren27ee0f32012-07-30 10:55:44 +0000333 ret = 0;
334
335fini:
336 fini_mmc_for_env(mmc);
337err:
338 if (ret)
Tim Harveyeda75c32015-05-08 14:52:09 -0700339 set_default_env(errmsg);
Terry Lvb4eceac2010-05-17 10:57:01 +0800340#endif
341}
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000342#endif /* CONFIG_ENV_OFFSET_REDUND */