blob: 584379ebd26e3673ffd2d4055640160d24108582 [file] [log] [blame]
wdenk0bc4a1a2002-09-18 11:09:59 +00001/*
Wolfgang Denkea882ba2010-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 Denkea882ba2010-06-20 23:33:59 +02007 *
Wolfgang Denk3765b3e2013-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 Schocher548738b2010-01-07 08:55:40 +010015#if defined(CONFIG_I2C_ENV_EEPROM_BUS)
16#include <i2c.h>
17#endif
Wolfgang Denkea882ba2010-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 Denkd87080b2006-03-31 18:32:53 +020022DECLARE_GLOBAL_DATA_PTR;
23
Wolfgang Denkea882ba2010-06-20 23:33:59 +020024static int eeprom_bus_read(unsigned dev_addr, unsigned offset,
Igor Grinbergdd2a2332011-11-07 01:14:07 +000025 uchar *buffer, unsigned cnt)
Heiko Schocher548738b2010-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 Schocher3f4978c2012-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 Schocher548738b2010-01-07 08:55:40 +010033#endif
34
Igor Grinbergdd2a2332011-11-07 01:14:07 +000035 rcode = eeprom_read(dev_addr, offset, buffer, cnt);
Wolfgang Denkea882ba2010-06-20 23:33:59 +020036
Heiko Schocher548738b2010-01-07 08:55:40 +010037#if defined(CONFIG_I2C_ENV_EEPROM_BUS)
Alexey Brodkin6d001e72013-12-11 18:04:40 +040038 i2c_set_bus_num(old_bus);
Heiko Schocher548738b2010-01-07 08:55:40 +010039#endif
Heiko Schocher9a2accb2012-10-25 10:32:14 +020040
Heiko Schocher548738b2010-01-07 08:55:40 +010041 return rcode;
42}
43
Wolfgang Denkea882ba2010-06-20 23:33:59 +020044static int eeprom_bus_write(unsigned dev_addr, unsigned offset,
Igor Grinbergdd2a2332011-11-07 01:14:07 +000045 uchar *buffer, unsigned cnt)
Heiko Schocher548738b2010-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 Schocher3f4978c2012-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 Schocher548738b2010-01-07 08:55:40 +010053#endif
Heiko Schocher9a2accb2012-10-25 10:32:14 +020054
Wolfgang Denkea882ba2010-06-20 23:33:59 +020055 rcode = eeprom_write(dev_addr, offset, buffer, cnt);
Heiko Schocher9a2accb2012-10-25 10:32:14 +020056
Heiko Schocher548738b2010-01-07 08:55:40 +010057#if defined(CONFIG_I2C_ENV_EEPROM_BUS)
58 i2c_set_bus_num(old_bus);
59#endif
Alexey Brodkin6d001e72013-12-11 18:04:40 +040060
Heiko Schocher548738b2010-01-07 08:55:40 +010061 return rcode;
62}
wdenk0bc4a1a2002-09-18 11:09:59 +000063
Simon Glass21f63942017-08-03 12:22:16 -060064static int env_eeprom_get_char(int index)
wdenk0bc4a1a2002-09-18 11:09:59 +000065{
66 uchar c;
Igor Grinbergdd2a2332011-11-07 01:14:07 +000067 unsigned int off = CONFIG_ENV_OFFSET;
Wolfgang Denkea882ba2010-06-20 23:33:59 +020068
Heiko Schocher1567b592010-01-07 08:55:44 +010069#ifdef CONFIG_ENV_OFFSET_REDUND
Simon Glass203e94f2017-08-03 12:21:56 -060070 if (gd->env_valid == ENV_REDUND)
Heiko Schocher1567b592010-01-07 08:55:44 +010071 off = CONFIG_ENV_OFFSET_REDUND;
72#endif
Wolfgang Denkea882ba2010-06-20 23:33:59 +020073 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
Igor Grinbergdd2a2332011-11-07 01:14:07 +000074 off + index + offsetof(env_t, data), &c, 1);
wdenk0bc4a1a2002-09-18 11:09:59 +000075
Igor Grinbergdd2a2332011-11-07 01:14:07 +000076 return c;
wdenk0bc4a1a2002-09-18 11:09:59 +000077}
78
Simon Glassc5951992017-08-03 12:22:17 -060079static int env_eeprom_load(void)
wdenk0bc4a1a2002-09-18 11:09:59 +000080{
Ludger Dreiere3cc5bc2015-10-12 13:34:24 +020081 char buf_env[CONFIG_ENV_SIZE];
Heiko Schocher1567b592010-01-07 08:55:44 +010082 unsigned int off = CONFIG_ENV_OFFSET;
Wolfgang Denkea882ba2010-06-20 23:33:59 +020083
Heiko Schocher1567b592010-01-07 08:55:44 +010084#ifdef CONFIG_ENV_OFFSET_REDUND
Ludger Dreiere3cc5bc2015-10-12 13:34:24 +020085 ulong len, crc[2], crc_tmp;
86 unsigned int off_env[2];
87 uchar rdbuf[64], flags[2];
88 int i, crc_ok[2] = {0, 0};
89
Marek Vasut354e3ed2015-11-10 20:53:31 +010090 eeprom_init(-1); /* prepare for EEPROM read/write */
Ludger Dreiere3cc5bc2015-10-12 13:34:24 +020091
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 */
97 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
98 off_env[i] + offsetof(env_t, crc),
99 (uchar *)&crc[i], sizeof(ulong));
100 /* read FLAGS */
101 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
102 off_env[i] + offsetof(env_t, flags),
103 (uchar *)&flags[i], sizeof(uchar));
104
105 crc_tmp = 0;
106 len = ENV_SIZE;
107 off = off_env[i] + offsetof(env_t, data);
108 while (len > 0) {
109 int n = (len > sizeof(rdbuf)) ? sizeof(rdbuf) : len;
110
111 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR, off,
112 rdbuf, n);
113
114 crc_tmp = crc32(crc_tmp, rdbuf, n);
115 len -= n;
116 off += n;
117 }
118
119 if (crc_tmp == crc[i])
120 crc_ok[i] = 1;
121 }
122
123 if (!crc_ok[0] && !crc_ok[1]) {
124 gd->env_addr = 0;
Simon Glass2d7cb5b2017-08-20 04:45:15 -0600125 gd->env_valid = ENV_INVALID;
Ludger Dreiere3cc5bc2015-10-12 13:34:24 +0200126 } else if (crc_ok[0] && !crc_ok[1]) {
Simon Glass203e94f2017-08-03 12:21:56 -0600127 gd->env_valid = ENV_VALID;
Ludger Dreiere3cc5bc2015-10-12 13:34:24 +0200128 } else if (!crc_ok[0] && crc_ok[1]) {
Simon Glass203e94f2017-08-03 12:21:56 -0600129 gd->env_valid = ENV_REDUND;
Ludger Dreiere3cc5bc2015-10-12 13:34:24 +0200130 } else {
131 /* both ok - check serial */
132 if (flags[0] == ACTIVE_FLAG && flags[1] == OBSOLETE_FLAG)
Simon Glass203e94f2017-08-03 12:21:56 -0600133 gd->env_valid = ENV_VALID;
Ludger Dreiere3cc5bc2015-10-12 13:34:24 +0200134 else if (flags[0] == OBSOLETE_FLAG && flags[1] == ACTIVE_FLAG)
Simon Glass203e94f2017-08-03 12:21:56 -0600135 gd->env_valid = ENV_REDUND;
Ludger Dreiere3cc5bc2015-10-12 13:34:24 +0200136 else if (flags[0] == 0xFF && flags[1] == 0)
Simon Glass203e94f2017-08-03 12:21:56 -0600137 gd->env_valid = ENV_REDUND;
Ludger Dreiere3cc5bc2015-10-12 13:34:24 +0200138 else if (flags[1] == 0xFF && flags[0] == 0)
Simon Glass203e94f2017-08-03 12:21:56 -0600139 gd->env_valid = ENV_VALID;
Ludger Dreiere3cc5bc2015-10-12 13:34:24 +0200140 else /* flags are equal - almost impossible */
Simon Glass203e94f2017-08-03 12:21:56 -0600141 gd->env_valid = ENV_VALID;
Ludger Dreiere3cc5bc2015-10-12 13:34:24 +0200142 }
143
Ludger Dreiere3cc5bc2015-10-12 13:34:24 +0200144#else /* CONFIG_ENV_OFFSET_REDUND */
145 ulong crc, len, new;
146 uchar rdbuf[64];
147
Marek Vasut354e3ed2015-11-10 20:53:31 +0100148 eeprom_init(-1); /* prepare for EEPROM read/write */
Ludger Dreiere3cc5bc2015-10-12 13:34:24 +0200149
150 /* read old CRC */
151 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
152 CONFIG_ENV_OFFSET + offsetof(env_t, crc),
153 (uchar *)&crc, sizeof(ulong));
154
155 new = 0;
156 len = ENV_SIZE;
157 off = offsetof(env_t, data);
158 while (len > 0) {
159 int n = (len > sizeof(rdbuf)) ? sizeof(rdbuf) : len;
160
161 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
162 CONFIG_ENV_OFFSET + off, rdbuf, n);
163 new = crc32(new, rdbuf, n);
164 len -= n;
165 off += n;
166 }
167
168 if (crc == new) {
Simon Glass2d7cb5b2017-08-20 04:45:15 -0600169 gd->env_valid = ENV_VALID;
Ludger Dreiere3cc5bc2015-10-12 13:34:24 +0200170 } else {
Simon Glass2d7cb5b2017-08-20 04:45:15 -0600171 gd->env_valid = ENV_INVALID;
Ludger Dreiere3cc5bc2015-10-12 13:34:24 +0200172 }
173#endif /* CONFIG_ENV_OFFSET_REDUND */
174
175 off = CONFIG_ENV_OFFSET;
176#ifdef CONFIG_ENV_OFFSET_REDUND
Simon Glass203e94f2017-08-03 12:21:56 -0600177 if (gd->env_valid == ENV_REDUND)
Heiko Schocher1567b592010-01-07 08:55:44 +0100178 off = CONFIG_ENV_OFFSET_REDUND;
179#endif
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200180
Ludger Dreiere3cc5bc2015-10-12 13:34:24 +0200181 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
182 off, (uchar *)buf_env, CONFIG_ENV_SIZE);
183
184 env_import(buf_env, 1);
Simon Glassc5951992017-08-03 12:22:17 -0600185
186 return 0;
wdenk0bc4a1a2002-09-18 11:09:59 +0000187}
188
Simon Glasse5bce242017-08-03 12:22:01 -0600189static int env_eeprom_save(void)
wdenk0bc4a1a2002-09-18 11:09:59 +0000190{
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400191 env_t env_new;
Igor Grinbergdd2a2332011-11-07 01:14:07 +0000192 int rc;
193 unsigned int off = CONFIG_ENV_OFFSET;
Heiko Schocher1567b592010-01-07 08:55:44 +0100194#ifdef CONFIG_ENV_OFFSET_REDUND
Igor Grinbergdd2a2332011-11-07 01:14:07 +0000195 unsigned int off_red = CONFIG_ENV_OFFSET_REDUND;
196 char flag_obsolete = OBSOLETE_FLAG;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200197#endif
198
Marek Vasut7ce15262014-03-05 19:59:50 +0100199 rc = env_export(&env_new);
200 if (rc)
201 return rc;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200202
203#ifdef CONFIG_ENV_OFFSET_REDUND
Simon Glass203e94f2017-08-03 12:21:56 -0600204 if (gd->env_valid == ENV_VALID) {
Igor Grinbergdd2a2332011-11-07 01:14:07 +0000205 off = CONFIG_ENV_OFFSET_REDUND;
206 off_red = CONFIG_ENV_OFFSET;
Heiko Schocher1567b592010-01-07 08:55:44 +0100207 }
208
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400209 env_new.flags = ACTIVE_FLAG;
Heiko Schocher1567b592010-01-07 08:55:44 +0100210#endif
211
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200212 rc = eeprom_bus_write(CONFIG_SYS_DEF_EEPROM_ADDR,
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400213 off, (uchar *)&env_new, CONFIG_ENV_SIZE);
Heiko Schocher1567b592010-01-07 08:55:44 +0100214
215#ifdef CONFIG_ENV_OFFSET_REDUND
216 if (rc == 0) {
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200217 eeprom_bus_write(CONFIG_SYS_DEF_EEPROM_ADDR,
Igor Grinbergdd2a2332011-11-07 01:14:07 +0000218 off_red + offsetof(env_t, flags),
219 (uchar *)&flag_obsolete, 1);
220
Simon Glass203e94f2017-08-03 12:21:56 -0600221 if (gd->env_valid == ENV_VALID)
222 gd->env_valid = ENV_REDUND;
Heiko Schocher1567b592010-01-07 08:55:44 +0100223 else
Simon Glass203e94f2017-08-03 12:21:56 -0600224 gd->env_valid = ENV_VALID;
Heiko Schocher1567b592010-01-07 08:55:44 +0100225 }
226#endif
Heiko Schocher1567b592010-01-07 08:55:44 +0100227 return rc;
wdenk0bc4a1a2002-09-18 11:09:59 +0000228}
229
Simon Glass4415f1d2017-08-03 12:21:58 -0600230U_BOOT_ENV_LOCATION(eeprom) = {
231 .location = ENVL_EEPROM,
Simon Glassac358be2017-08-03 12:22:03 -0600232 ENV_NAME("EEPROM")
Simon Glasse5bce242017-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 Glass4415f1d2017-08-03 12:21:58 -0600236};