blob: 584379ebd26e3673ffd2d4055640160d24108582 [file] [log] [blame]
wdenk0bc4a1a2002-09-18 11:09:59 +00001/*
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02002 * (C) Copyright 2000-2010
wdenk0bc4a1a2002-09-18 11:09:59 +00003 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
6 * Andreas Heppel <aheppel@sysgo.de>
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02007 *
Wolfgang Denkbd8ec7e2013-10-07 13:07:26 +02008 * SPDX-License-Identifier: GPL-2.0+
wdenk0bc4a1a2002-09-18 11:09:59 +00009 */
10
11#include <common.h>
wdenk0bc4a1a2002-09-18 11:09:59 +000012#include <command.h>
13#include <environment.h>
wdenk0bc4a1a2002-09-18 11:09:59 +000014#include <linux/stddef.h>
Heiko Schocher9bb0dd52010-01-07 08:55:40 +010015#if defined(CONFIG_I2C_ENV_EEPROM_BUS)
16#include <i2c.h>
17#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020018#include <search.h>
19#include <errno.h>
20#include <linux/compiler.h> /* for BUG_ON */
wdenk0bc4a1a2002-09-18 11:09:59 +000021
Wolfgang Denk6405a152006-03-31 18:32:53 +020022DECLARE_GLOBAL_DATA_PTR;
23
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020024static int eeprom_bus_read(unsigned dev_addr, unsigned offset,
Igor Grinberg13f851f2011-11-07 01:14:07 +000025 uchar *buffer, unsigned cnt)
Heiko Schocher9bb0dd52010-01-07 08:55:40 +010026{
27 int rcode;
28#if defined(CONFIG_I2C_ENV_EEPROM_BUS)
29 int old_bus = i2c_get_bus_num();
30
Heiko Schochere0e55bc2012-01-16 21:12:24 +000031 if (old_bus != CONFIG_I2C_ENV_EEPROM_BUS)
32 i2c_set_bus_num(CONFIG_I2C_ENV_EEPROM_BUS);
Heiko Schocher9bb0dd52010-01-07 08:55:40 +010033#endif
34
Igor Grinberg13f851f2011-11-07 01:14:07 +000035 rcode = eeprom_read(dev_addr, offset, buffer, cnt);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020036
Heiko Schocher9bb0dd52010-01-07 08:55:40 +010037#if defined(CONFIG_I2C_ENV_EEPROM_BUS)
Alexey Brodkinf234b1e2013-12-11 18:04:40 +040038 i2c_set_bus_num(old_bus);
Heiko Schocher9bb0dd52010-01-07 08:55:40 +010039#endif
Heiko Schochera4ea4452012-10-25 10:32:14 +020040
Heiko Schocher9bb0dd52010-01-07 08:55:40 +010041 return rcode;
42}
43
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020044static int eeprom_bus_write(unsigned dev_addr, unsigned offset,
Igor Grinberg13f851f2011-11-07 01:14:07 +000045 uchar *buffer, unsigned cnt)
Heiko Schocher9bb0dd52010-01-07 08:55:40 +010046{
47 int rcode;
48#if defined(CONFIG_I2C_ENV_EEPROM_BUS)
49 int old_bus = i2c_get_bus_num();
50
Heiko Schochere0e55bc2012-01-16 21:12:24 +000051 if (old_bus != CONFIG_I2C_ENV_EEPROM_BUS)
52 i2c_set_bus_num(CONFIG_I2C_ENV_EEPROM_BUS);
Heiko Schocher9bb0dd52010-01-07 08:55:40 +010053#endif
Heiko Schochera4ea4452012-10-25 10:32:14 +020054
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020055 rcode = eeprom_write(dev_addr, offset, buffer, cnt);
Heiko Schochera4ea4452012-10-25 10:32:14 +020056
Heiko Schocher9bb0dd52010-01-07 08:55:40 +010057#if defined(CONFIG_I2C_ENV_EEPROM_BUS)
58 i2c_set_bus_num(old_bus);
59#endif
Alexey Brodkinf234b1e2013-12-11 18:04:40 +040060
Heiko Schocher9bb0dd52010-01-07 08:55:40 +010061 return rcode;
62}
wdenk0bc4a1a2002-09-18 11:09:59 +000063
Simon Glass3fa4c222017-08-03 12:22:16 -060064static int env_eeprom_get_char(int index)
wdenk0bc4a1a2002-09-18 11:09:59 +000065{
66 uchar c;
Igor Grinberg13f851f2011-11-07 01:14:07 +000067 unsigned int off = CONFIG_ENV_OFFSET;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020068
Heiko Schochere7193632010-01-07 08:55:44 +010069#ifdef CONFIG_ENV_OFFSET_REDUND
Simon Glass4bc2ad22017-08-03 12:21:56 -060070 if (gd->env_valid == ENV_REDUND)
Heiko Schochere7193632010-01-07 08:55:44 +010071 off = CONFIG_ENV_OFFSET_REDUND;
72#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020073 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
Igor Grinberg13f851f2011-11-07 01:14:07 +000074 off + index + offsetof(env_t, data), &c, 1);
wdenk0bc4a1a2002-09-18 11:09:59 +000075
Igor Grinberg13f851f2011-11-07 01:14:07 +000076 return c;
wdenk0bc4a1a2002-09-18 11:09:59 +000077}
78
Simon Glass99778492017-08-03 12:22:17 -060079static int env_eeprom_load(void)
wdenk0bc4a1a2002-09-18 11:09:59 +000080{
Ludger Dreierbf84e642015-10-12 13:34:24 +020081 char buf_env[CONFIG_ENV_SIZE];
Heiko Schochere7193632010-01-07 08:55:44 +010082 unsigned int off = CONFIG_ENV_OFFSET;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020083
Heiko Schochere7193632010-01-07 08:55:44 +010084#ifdef CONFIG_ENV_OFFSET_REDUND
Igor Grinberg13f851f2011-11-07 01:14:07 +000085 ulong len, crc[2], crc_tmp;
Ludger Dreierbf84e642015-10-12 13:34:24 +020086 unsigned int off_env[2];
87 uchar rdbuf[64], flags[2];
Igor Grinberg13f851f2011-11-07 01:14:07 +000088 int i, crc_ok[2] = {0, 0};
Heiko Schochere7193632010-01-07 08:55:44 +010089
Marek Vasutf422ae72015-11-10 20:53:31 +010090 eeprom_init(-1); /* prepare for EEPROM read/write */
Heiko Schochere7193632010-01-07 08:55:44 +010091
92 off_env[0] = CONFIG_ENV_OFFSET;
93 off_env[1] = CONFIG_ENV_OFFSET_REDUND;
94
95 for (i = 0; i < 2; i++) {
96 /* read CRC */
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020097 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
Igor Grinberg13f851f2011-11-07 01:14:07 +000098 off_env[i] + offsetof(env_t, crc),
99 (uchar *)&crc[i], sizeof(ulong));
Heiko Schochere7193632010-01-07 08:55:44 +0100100 /* read FLAGS */
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200101 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
Igor Grinberg13f851f2011-11-07 01:14:07 +0000102 off_env[i] + offsetof(env_t, flags),
103 (uchar *)&flags[i], sizeof(uchar));
Heiko Schochere7193632010-01-07 08:55:44 +0100104
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200105 crc_tmp = 0;
Heiko Schochere7193632010-01-07 08:55:44 +0100106 len = ENV_SIZE;
Igor Grinberg13f851f2011-11-07 01:14:07 +0000107 off = off_env[i] + offsetof(env_t, data);
Heiko Schochere7193632010-01-07 08:55:44 +0100108 while (len > 0) {
Ludger Dreierbf84e642015-10-12 13:34:24 +0200109 int n = (len > sizeof(rdbuf)) ? sizeof(rdbuf) : len;
Heiko Schochere7193632010-01-07 08:55:44 +0100110
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200111 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR, off,
Ludger Dreierbf84e642015-10-12 13:34:24 +0200112 rdbuf, n);
Heiko Schochere7193632010-01-07 08:55:44 +0100113
Ludger Dreierbf84e642015-10-12 13:34:24 +0200114 crc_tmp = crc32(crc_tmp, rdbuf, n);
Heiko Schochere7193632010-01-07 08:55:44 +0100115 len -= n;
116 off += n;
117 }
Igor Grinberg13f851f2011-11-07 01:14:07 +0000118
Heiko Schochere7193632010-01-07 08:55:44 +0100119 if (crc_tmp == crc[i])
120 crc_ok[i] = 1;
121 }
122
123 if (!crc_ok[0] && !crc_ok[1]) {
Igor Grinberg13f851f2011-11-07 01:14:07 +0000124 gd->env_addr = 0;
Simon Glass1f39d0b2017-08-20 04:45:15 -0600125 gd->env_valid = ENV_INVALID;
Heiko Schochere7193632010-01-07 08:55:44 +0100126 } else if (crc_ok[0] && !crc_ok[1]) {
Simon Glass4bc2ad22017-08-03 12:21:56 -0600127 gd->env_valid = ENV_VALID;
Igor Grinberg13f851f2011-11-07 01:14:07 +0000128 } else if (!crc_ok[0] && crc_ok[1]) {
Simon Glass4bc2ad22017-08-03 12:21:56 -0600129 gd->env_valid = ENV_REDUND;
Heiko Schochere7193632010-01-07 08:55:44 +0100130 } else {
131 /* both ok - check serial */
132 if (flags[0] == ACTIVE_FLAG && flags[1] == OBSOLETE_FLAG)
Simon Glass4bc2ad22017-08-03 12:21:56 -0600133 gd->env_valid = ENV_VALID;
Heiko Schochere7193632010-01-07 08:55:44 +0100134 else if (flags[0] == OBSOLETE_FLAG && flags[1] == ACTIVE_FLAG)
Simon Glass4bc2ad22017-08-03 12:21:56 -0600135 gd->env_valid = ENV_REDUND;
Heiko Schochere7193632010-01-07 08:55:44 +0100136 else if (flags[0] == 0xFF && flags[1] == 0)
Simon Glass4bc2ad22017-08-03 12:21:56 -0600137 gd->env_valid = ENV_REDUND;
Igor Grinberg13f851f2011-11-07 01:14:07 +0000138 else if (flags[1] == 0xFF && flags[0] == 0)
Simon Glass4bc2ad22017-08-03 12:21:56 -0600139 gd->env_valid = ENV_VALID;
Heiko Schochere7193632010-01-07 08:55:44 +0100140 else /* flags are equal - almost impossible */
Simon Glass4bc2ad22017-08-03 12:21:56 -0600141 gd->env_valid = ENV_VALID;
Heiko Schochere7193632010-01-07 08:55:44 +0100142 }
143
Ludger Dreierbf84e642015-10-12 13:34:24 +0200144#else /* CONFIG_ENV_OFFSET_REDUND */
wdenk0bc4a1a2002-09-18 11:09:59 +0000145 ulong crc, len, new;
Ludger Dreierbf84e642015-10-12 13:34:24 +0200146 uchar rdbuf[64];
wdenk0bc4a1a2002-09-18 11:09:59 +0000147
Marek Vasutf422ae72015-11-10 20:53:31 +0100148 eeprom_init(-1); /* prepare for EEPROM read/write */
wdenk0bc4a1a2002-09-18 11:09:59 +0000149
150 /* read old CRC */
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200151 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
Igor Grinberg13f851f2011-11-07 01:14:07 +0000152 CONFIG_ENV_OFFSET + offsetof(env_t, crc),
153 (uchar *)&crc, sizeof(ulong));
wdenk0bc4a1a2002-09-18 11:09:59 +0000154
155 new = 0;
156 len = ENV_SIZE;
Igor Grinberg13f851f2011-11-07 01:14:07 +0000157 off = offsetof(env_t, data);
wdenk0bc4a1a2002-09-18 11:09:59 +0000158 while (len > 0) {
Ludger Dreierbf84e642015-10-12 13:34:24 +0200159 int n = (len > sizeof(rdbuf)) ? sizeof(rdbuf) : len;
wdenk0bc4a1a2002-09-18 11:09:59 +0000160
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200161 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
Ludger Dreierbf84e642015-10-12 13:34:24 +0200162 CONFIG_ENV_OFFSET + off, rdbuf, n);
163 new = crc32(new, rdbuf, n);
wdenk0bc4a1a2002-09-18 11:09:59 +0000164 len -= n;
165 off += n;
166 }
167
168 if (crc == new) {
Simon Glass1f39d0b2017-08-20 04:45:15 -0600169 gd->env_valid = ENV_VALID;
wdenk0bc4a1a2002-09-18 11:09:59 +0000170 } else {
Simon Glass1f39d0b2017-08-20 04:45:15 -0600171 gd->env_valid = ENV_INVALID;
wdenk0bc4a1a2002-09-18 11:09:59 +0000172 }
Ludger Dreierbf84e642015-10-12 13:34:24 +0200173#endif /* CONFIG_ENV_OFFSET_REDUND */
174
175 off = CONFIG_ENV_OFFSET;
176#ifdef CONFIG_ENV_OFFSET_REDUND
Simon Glass4bc2ad22017-08-03 12:21:56 -0600177 if (gd->env_valid == ENV_REDUND)
Ludger Dreierbf84e642015-10-12 13:34:24 +0200178 off = CONFIG_ENV_OFFSET_REDUND;
179#endif
180
181 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
182 off, (uchar *)buf_env, CONFIG_ENV_SIZE);
183
184 env_import(buf_env, 1);
Simon Glass99778492017-08-03 12:22:17 -0600185
186 return 0;
Ludger Dreierbf84e642015-10-12 13:34:24 +0200187}
188
Simon Glass082af922017-08-03 12:22:01 -0600189static int env_eeprom_save(void)
Ludger Dreierbf84e642015-10-12 13:34:24 +0200190{
191 env_t env_new;
192 int rc;
193 unsigned int off = CONFIG_ENV_OFFSET;
194#ifdef CONFIG_ENV_OFFSET_REDUND
195 unsigned int off_red = CONFIG_ENV_OFFSET_REDUND;
196 char flag_obsolete = OBSOLETE_FLAG;
197#endif
198
Ludger Dreierbf84e642015-10-12 13:34:24 +0200199 rc = env_export(&env_new);
200 if (rc)
201 return rc;
202
203#ifdef CONFIG_ENV_OFFSET_REDUND
Simon Glass4bc2ad22017-08-03 12:21:56 -0600204 if (gd->env_valid == ENV_VALID) {
Ludger Dreierbf84e642015-10-12 13:34:24 +0200205 off = CONFIG_ENV_OFFSET_REDUND;
206 off_red = CONFIG_ENV_OFFSET;
207 }
208
209 env_new.flags = ACTIVE_FLAG;
210#endif
211
212 rc = eeprom_bus_write(CONFIG_SYS_DEF_EEPROM_ADDR,
213 off, (uchar *)&env_new, CONFIG_ENV_SIZE);
214
215#ifdef CONFIG_ENV_OFFSET_REDUND
216 if (rc == 0) {
217 eeprom_bus_write(CONFIG_SYS_DEF_EEPROM_ADDR,
218 off_red + offsetof(env_t, flags),
219 (uchar *)&flag_obsolete, 1);
220
Simon Glass4bc2ad22017-08-03 12:21:56 -0600221 if (gd->env_valid == ENV_VALID)
222 gd->env_valid = ENV_REDUND;
Ludger Dreierbf84e642015-10-12 13:34:24 +0200223 else
Simon Glass4bc2ad22017-08-03 12:21:56 -0600224 gd->env_valid = ENV_VALID;
Ludger Dreierbf84e642015-10-12 13:34:24 +0200225 }
226#endif
227 return rc;
228}
229
Simon Glassc10a88e2017-08-03 12:21:58 -0600230U_BOOT_ENV_LOCATION(eeprom) = {
231 .location = ENVL_EEPROM,
Simon Glassd8273ed2017-08-03 12:22:03 -0600232 ENV_NAME("EEPROM")
Simon Glass082af922017-08-03 12:22:01 -0600233 .get_char = env_eeprom_get_char,
234 .load = env_eeprom_load,
235 .save = env_save_ptr(env_eeprom_save),
Simon Glassc10a88e2017-08-03 12:21:58 -0600236};