blob: 3f3092d975607a48161e3e74819d98a67a16ad64 [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>
Philipp Tomsich8ab507b2017-05-16 00:16:31 +020013#include <fdtdec.h>
Terry Lvb4eceac2010-05-17 10:57:01 +080014#include <linux/stddef.h>
15#include <malloc.h>
Simon Glass2dd337a2015-09-02 17:24:58 -060016#include <memalign.h>
Terry Lvb4eceac2010-05-17 10:57:01 +080017#include <mmc.h>
Lei Wen9f84f212010-11-10 07:39:23 +080018#include <search.h>
Lei Wenff833ed2010-10-13 11:07:21 +080019#include <errno.h>
Terry Lvb4eceac2010-05-17 10:57:01 +080020
Michael Heimpolda1d25a32013-04-10 10:36:19 +000021#if defined(CONFIG_ENV_SIZE_REDUND) && \
22 (CONFIG_ENV_SIZE_REDUND != CONFIG_ENV_SIZE)
23#error CONFIG_ENV_SIZE_REDUND should be the same as CONFIG_ENV_SIZE
24#endif
25
Terry Lvb4eceac2010-05-17 10:57:01 +080026DECLARE_GLOBAL_DATA_PTR;
27
Mingkai Hua55fce32011-01-24 17:09:55 +000028#if !defined(CONFIG_ENV_OFFSET)
29#define CONFIG_ENV_OFFSET 0
30#endif
31
Philipp Tomsich8ab507b2017-05-16 00:16:31 +020032#if CONFIG_IS_ENABLED(OF_CONTROL)
33static inline s64 mmc_offset(int copy)
34{
35 const char *propname = "u-boot,mmc-env-offset";
36 s64 defvalue = CONFIG_ENV_OFFSET;
37
38#if defined(CONFIG_ENV_OFFSET_REDUND)
39 if (copy) {
40 propname = "u-boot,mmc-env-offset-redundant";
41 defvalue = CONFIG_ENV_OFFSET_REDUND;
42 }
43#endif
44
45 return fdtdec_get_config_int(gd->fdt_blob, propname, defvalue);
46}
47#else
48static inline s64 mmc_offset(int copy)
Mingkai Hua55fce32011-01-24 17:09:55 +000049{
Philipp Tomsich8ab507b2017-05-16 00:16:31 +020050 s64 offset = CONFIG_ENV_OFFSET;
Stephen Warren24dc2032013-06-11 15:14:02 -060051
Philipp Tomsich8ab507b2017-05-16 00:16:31 +020052#if defined(CONFIG_ENV_OFFSET_REDUND)
Michael Heimpolda1d25a32013-04-10 10:36:19 +000053 if (copy)
Stephen Warren24dc2032013-06-11 15:14:02 -060054 offset = CONFIG_ENV_OFFSET_REDUND;
Michael Heimpolda1d25a32013-04-10 10:36:19 +000055#endif
Philipp Tomsich8ab507b2017-05-16 00:16:31 +020056 return offset;
57}
58#endif
59
60__weak int mmc_get_env_addr(struct mmc *mmc, int copy, u32 *env_addr)
61{
62 s64 offset = mmc_offset(copy);
Stephen Warren24dc2032013-06-11 15:14:02 -060063
64 if (offset < 0)
65 offset += mmc->capacity;
66
67 *env_addr = offset;
68
Mingkai Hua55fce32011-01-24 17:09:55 +000069 return 0;
70}
Mingkai Hua55fce32011-01-24 17:09:55 +000071
Clemens Gruberf7b61442016-01-20 15:43:37 +010072__weak int mmc_get_env_dev(void)
73{
74 return CONFIG_SYS_MMC_ENV_DEV;
75}
76
Tom Rinic929f822014-03-28 12:03:34 -040077#ifdef CONFIG_SYS_MMC_ENV_PART
Dmitry Lifshitz97d2fb92014-07-30 13:19:06 +030078__weak uint mmc_get_env_part(struct mmc *mmc)
79{
80 return CONFIG_SYS_MMC_ENV_PART;
81}
82
Stephen Warren1e0f92a2015-12-07 11:38:49 -070083static unsigned char env_mmc_orig_hwpart;
84
Dmitry Lifshitz97d2fb92014-07-30 13:19:06 +030085static int mmc_set_env_part(struct mmc *mmc)
86{
87 uint part = mmc_get_env_part(mmc);
Clemens Gruberf7b61442016-01-20 15:43:37 +010088 int dev = mmc_get_env_dev();
Dmitry Lifshitz97d2fb92014-07-30 13:19:06 +030089 int ret = 0;
Tom Rinic929f822014-03-28 12:03:34 -040090
Simon Glass11f2bb62016-05-01 13:52:29 -060091 env_mmc_orig_hwpart = mmc_get_blk_desc(mmc)->hwpart;
92 ret = blk_select_hwpart_devnum(IF_TYPE_MMC, dev, part);
Stephen Warren1e0f92a2015-12-07 11:38:49 -070093 if (ret)
94 puts("MMC partition switch failed\n");
Dmitry Lifshitz97d2fb92014-07-30 13:19:06 +030095
96 return ret;
97}
98#else
99static inline int mmc_set_env_part(struct mmc *mmc) {return 0; };
Tom Rinic929f822014-03-28 12:03:34 -0400100#endif
101
Tim Harveyeda75c32015-05-08 14:52:09 -0700102static const char *init_mmc_for_env(struct mmc *mmc)
Dmitry Lifshitz97d2fb92014-07-30 13:19:06 +0300103{
Tim Harveyeda75c32015-05-08 14:52:09 -0700104 if (!mmc)
Hans de Goede8764fd62015-08-15 20:05:01 +0200105 return "!No MMC card found";
Terry Lvb4eceac2010-05-17 10:57:01 +0800106
Simon Glass1d9907c2017-05-27 11:37:18 -0600107#ifdef CONFIG_BLK
108 struct udevice *dev;
109
110 if (blk_get_from_parent(mmc->dev, &dev))
111 return "!No block device";
112#else
Tim Harveyeda75c32015-05-08 14:52:09 -0700113 if (mmc_init(mmc))
Hans de Goede8764fd62015-08-15 20:05:01 +0200114 return "!MMC init failed";
Simon Glass86d78c72017-04-23 20:02:04 -0600115#endif
Tim Harveyeda75c32015-05-08 14:52:09 -0700116 if (mmc_set_env_part(mmc))
Hans de Goede8764fd62015-08-15 20:05:01 +0200117 return "!MMC partition switch failed";
Tim Harveyeda75c32015-05-08 14:52:09 -0700118
119 return NULL;
Terry Lvb4eceac2010-05-17 10:57:01 +0800120}
121
Stephen Warren27ee0f32012-07-30 10:55:44 +0000122static void fini_mmc_for_env(struct mmc *mmc)
123{
124#ifdef CONFIG_SYS_MMC_ENV_PART
Clemens Gruberf7b61442016-01-20 15:43:37 +0100125 int dev = mmc_get_env_dev();
Tom Rinic929f822014-03-28 12:03:34 -0400126
Simon Glass11f2bb62016-05-01 13:52:29 -0600127 blk_select_hwpart_devnum(IF_TYPE_MMC, dev, env_mmc_orig_hwpart);
Stephen Warren27ee0f32012-07-30 10:55:44 +0000128#endif
129}
130
Simon Glass082af922017-08-03 12:22:01 -0600131#if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_SPL_BUILD)
Igor Grinbergecbfb622011-11-07 01:14:05 +0000132static inline int write_env(struct mmc *mmc, unsigned long size,
133 unsigned long offset, const void *buffer)
Terry Lvb4eceac2010-05-17 10:57:01 +0800134{
135 uint blk_start, blk_cnt, n;
Simon Glassda898922016-05-14 14:03:03 -0600136 struct blk_desc *desc = mmc_get_blk_desc(mmc);
Terry Lvb4eceac2010-05-17 10:57:01 +0800137
Igor Grinbergecbfb622011-11-07 01:14:05 +0000138 blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len;
139 blk_cnt = ALIGN(size, mmc->write_bl_len) / mmc->write_bl_len;
Terry Lvb4eceac2010-05-17 10:57:01 +0800140
Simon Glassda898922016-05-14 14:03:03 -0600141 n = blk_dwrite(desc, blk_start, blk_cnt, (u_char *)buffer);
Terry Lvb4eceac2010-05-17 10:57:01 +0800142
143 return (n == blk_cnt) ? 0 : -1;
144}
145
Simon Glass082af922017-08-03 12:22:01 -0600146static int env_mmc_save(void)
Terry Lvb4eceac2010-05-17 10:57:01 +0800147{
Tom Rinia840b8e2013-04-05 14:55:21 -0400148 ALLOC_CACHE_ALIGN_BUFFER(env_t, env_new, 1);
Clemens Gruberf7b61442016-01-20 15:43:37 +0100149 int dev = mmc_get_env_dev();
150 struct mmc *mmc = find_mmc_device(dev);
Igor Grinbergecbfb622011-11-07 01:14:05 +0000151 u32 offset;
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000152 int ret, copy = 0;
Tim Harveyeda75c32015-05-08 14:52:09 -0700153 const char *errmsg;
Terry Lvb4eceac2010-05-17 10:57:01 +0800154
Tim Harveyeda75c32015-05-08 14:52:09 -0700155 errmsg = init_mmc_for_env(mmc);
156 if (errmsg) {
157 printf("%s\n", errmsg);
Terry Lvb4eceac2010-05-17 10:57:01 +0800158 return 1;
Tim Harveyeda75c32015-05-08 14:52:09 -0700159 }
Terry Lvb4eceac2010-05-17 10:57:01 +0800160
Marek Vasutd73c1292014-03-05 19:59:50 +0100161 ret = env_export(env_new);
162 if (ret)
Stephen Warren27ee0f32012-07-30 10:55:44 +0000163 goto fini;
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000164
165#ifdef CONFIG_ENV_OFFSET_REDUND
Simon Glass4bc2ad22017-08-03 12:21:56 -0600166 if (gd->env_valid == ENV_VALID)
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000167 copy = 1;
168#endif
169
170 if (mmc_get_env_addr(mmc, copy, &offset)) {
171 ret = 1;
172 goto fini;
173 }
174
Clemens Gruberf7b61442016-01-20 15:43:37 +0100175 printf("Writing to %sMMC(%d)... ", copy ? "redundant " : "", dev);
Stephen Warren42a3c442012-05-24 11:38:33 +0000176 if (write_env(mmc, CONFIG_ENV_SIZE, offset, (u_char *)env_new)) {
Terry Lvb4eceac2010-05-17 10:57:01 +0800177 puts("failed\n");
Stephen Warren27ee0f32012-07-30 10:55:44 +0000178 ret = 1;
179 goto fini;
Terry Lvb4eceac2010-05-17 10:57:01 +0800180 }
181
182 puts("done\n");
Stephen Warren27ee0f32012-07-30 10:55:44 +0000183 ret = 0;
184
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000185#ifdef CONFIG_ENV_OFFSET_REDUND
Simon Glass4bc2ad22017-08-03 12:21:56 -0600186 gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000187#endif
188
Stephen Warren27ee0f32012-07-30 10:55:44 +0000189fini:
190 fini_mmc_for_env(mmc);
191 return ret;
Terry Lvb4eceac2010-05-17 10:57:01 +0800192}
Simon Glass082af922017-08-03 12:22:01 -0600193#endif /* CONFIG_CMD_SAVEENV && !CONFIG_SPL_BUILD */
Terry Lvb4eceac2010-05-17 10:57:01 +0800194
Igor Grinbergecbfb622011-11-07 01:14:05 +0000195static inline int read_env(struct mmc *mmc, unsigned long size,
196 unsigned long offset, const void *buffer)
Terry Lvb4eceac2010-05-17 10:57:01 +0800197{
198 uint blk_start, blk_cnt, n;
Simon Glassda898922016-05-14 14:03:03 -0600199 struct blk_desc *desc = mmc_get_blk_desc(mmc);
Terry Lvb4eceac2010-05-17 10:57:01 +0800200
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
Simon Glassda898922016-05-14 14:03:03 -0600204 n = blk_dread(desc, blk_start, blk_cnt, (uchar *)buffer);
Terry Lvb4eceac2010-05-17 10:57:01 +0800205
206 return (n == blk_cnt) ? 0 : -1;
207}
208
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000209#ifdef CONFIG_ENV_OFFSET_REDUND
Simon Glass99778492017-08-03 12:22:17 -0600210static int env_mmc_load(void)
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000211{
212#if !defined(ENV_IS_EMBEDDED)
Tom Rinic929f822014-03-28 12:03:34 -0400213 struct mmc *mmc;
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000214 u32 offset1, offset2;
215 int read1_fail = 0, read2_fail = 0;
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000216 int ret;
Clemens Gruberf7b61442016-01-20 15:43:37 +0100217 int dev = mmc_get_env_dev();
Tim Harveyeda75c32015-05-08 14:52:09 -0700218 const char *errmsg = NULL;
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000219
Markus Niebel6ef41fd2013-10-04 15:48:03 +0200220 ALLOC_CACHE_ALIGN_BUFFER(env_t, tmp_env1, 1);
221 ALLOC_CACHE_ALIGN_BUFFER(env_t, tmp_env2, 1);
222
Tom Rinic929f822014-03-28 12:03:34 -0400223 mmc = find_mmc_device(dev);
224
Tim Harveyeda75c32015-05-08 14:52:09 -0700225 errmsg = init_mmc_for_env(mmc);
226 if (errmsg) {
Simon Glass99778492017-08-03 12:22:17 -0600227 ret = -EIO;
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000228 goto err;
229 }
230
231 if (mmc_get_env_addr(mmc, 0, &offset1) ||
232 mmc_get_env_addr(mmc, 1, &offset2)) {
Simon Glass99778492017-08-03 12:22:17 -0600233 ret = -EIO;
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000234 goto fini;
235 }
236
237 read1_fail = read_env(mmc, CONFIG_ENV_SIZE, offset1, tmp_env1);
238 read2_fail = read_env(mmc, CONFIG_ENV_SIZE, offset2, tmp_env2);
239
240 if (read1_fail && read2_fail)
241 puts("*** Error - No Valid Environment Area found\n");
242 else if (read1_fail || read2_fail)
243 puts("*** Warning - some problems detected "
244 "reading environment; recovered successfully\n");
245
Fiach Antaw0a2e1762017-01-25 18:53:12 +1000246 if (read1_fail && read2_fail) {
Tim Harveyeda75c32015-05-08 14:52:09 -0700247 errmsg = "!bad CRC";
Simon Glass99778492017-08-03 12:22:17 -0600248 ret = -EIO;
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000249 goto fini;
Fiach Antaw0a2e1762017-01-25 18:53:12 +1000250 } else if (!read1_fail && read2_fail) {
Simon Glass4bc2ad22017-08-03 12:21:56 -0600251 gd->env_valid = ENV_VALID;
Fiach Antaw0a2e1762017-01-25 18:53:12 +1000252 env_import((char *)tmp_env1, 1);
253 } else if (read1_fail && !read2_fail) {
Simon Glass4bc2ad22017-08-03 12:21:56 -0600254 gd->env_valid = ENV_REDUND;
Fiach Antaw0a2e1762017-01-25 18:53:12 +1000255 env_import((char *)tmp_env2, 1);
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000256 } else {
Fiach Antaw0a2e1762017-01-25 18:53:12 +1000257 env_import_redund((char *)tmp_env1, (char *)tmp_env2);
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000258 }
259
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000260 ret = 0;
261
262fini:
263 fini_mmc_for_env(mmc);
264err:
265 if (ret)
Tim Harveyeda75c32015-05-08 14:52:09 -0700266 set_default_env(errmsg);
Simon Glass99778492017-08-03 12:22:17 -0600267
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000268#endif
Simon Glass99778492017-08-03 12:22:17 -0600269 return ret;
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000270}
271#else /* ! CONFIG_ENV_OFFSET_REDUND */
Simon Glass99778492017-08-03 12:22:17 -0600272static int env_mmc_load(void)
Terry Lvb4eceac2010-05-17 10:57:01 +0800273{
274#if !defined(ENV_IS_EMBEDDED)
Tom Rinia840b8e2013-04-05 14:55:21 -0400275 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
Tom Rinic929f822014-03-28 12:03:34 -0400276 struct mmc *mmc;
Mingkai Hua55fce32011-01-24 17:09:55 +0000277 u32 offset;
Stephen Warren27ee0f32012-07-30 10:55:44 +0000278 int ret;
Clemens Gruberf7b61442016-01-20 15:43:37 +0100279 int dev = mmc_get_env_dev();
Tim Harveyeda75c32015-05-08 14:52:09 -0700280 const char *errmsg;
Tom Rinic929f822014-03-28 12:03:34 -0400281
Tom Rinic929f822014-03-28 12:03:34 -0400282 mmc = find_mmc_device(dev);
Stephen Warren27ee0f32012-07-30 10:55:44 +0000283
Tim Harveyeda75c32015-05-08 14:52:09 -0700284 errmsg = init_mmc_for_env(mmc);
285 if (errmsg) {
Simon Glass99778492017-08-03 12:22:17 -0600286 ret = -EIO;
Stephen Warren27ee0f32012-07-30 10:55:44 +0000287 goto err;
288 }
Terry Lvb4eceac2010-05-17 10:57:01 +0800289
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000290 if (mmc_get_env_addr(mmc, 0, &offset)) {
Simon Glass99778492017-08-03 12:22:17 -0600291 ret = -EIO;
Stephen Warren27ee0f32012-07-30 10:55:44 +0000292 goto fini;
293 }
Terry Lvb4eceac2010-05-17 10:57:01 +0800294
Tom Rinia840b8e2013-04-05 14:55:21 -0400295 if (read_env(mmc, CONFIG_ENV_SIZE, offset, buf)) {
Tim Harveyeda75c32015-05-08 14:52:09 -0700296 errmsg = "!read failed";
Simon Glass99778492017-08-03 12:22:17 -0600297 ret = -EIO;
Stephen Warren27ee0f32012-07-30 10:55:44 +0000298 goto fini;
299 }
Terry Lvb4eceac2010-05-17 10:57:01 +0800300
Tom Rinia840b8e2013-04-05 14:55:21 -0400301 env_import(buf, 1);
Stephen Warren27ee0f32012-07-30 10:55:44 +0000302 ret = 0;
303
304fini:
305 fini_mmc_for_env(mmc);
306err:
307 if (ret)
Tim Harveyeda75c32015-05-08 14:52:09 -0700308 set_default_env(errmsg);
Terry Lvb4eceac2010-05-17 10:57:01 +0800309#endif
Simon Glass99778492017-08-03 12:22:17 -0600310 return ret;
Terry Lvb4eceac2010-05-17 10:57:01 +0800311}
Michael Heimpolda1d25a32013-04-10 10:36:19 +0000312#endif /* CONFIG_ENV_OFFSET_REDUND */
Simon Glassc10a88e2017-08-03 12:21:58 -0600313
314U_BOOT_ENV_LOCATION(mmc) = {
315 .location = ENVL_MMC,
Simon Glassd8273ed2017-08-03 12:22:03 -0600316 ENV_NAME("MMC")
Simon Glass082af922017-08-03 12:22:01 -0600317 .load = env_mmc_load,
Simon Glassc10a88e2017-08-03 12:21:58 -0600318#ifndef CONFIG_SPL_BUILD
Simon Glass082af922017-08-03 12:22:01 -0600319 .save = env_save_ptr(env_mmc_save),
Simon Glassc10a88e2017-08-03 12:21:58 -0600320#endif
Simon Glassc10a88e2017-08-03 12:21:58 -0600321};