blob: 0db2bb63fe1d9671b7e586a88e9b4f08063fae37 [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
Igor Grinbergdd2a2332011-11-07 01:14:07 +000024env_t *env_ptr;
wdenk0bc4a1a2002-09-18 11:09:59 +000025
Wolfgang Denkea882ba2010-06-20 23:33:59 +020026char *env_name_spec = "EEPROM";
Heiko Schocher548738b2010-01-07 08:55:40 +010027
Wolfgang Denkea882ba2010-06-20 23:33:59 +020028static int eeprom_bus_read(unsigned dev_addr, unsigned offset,
Igor Grinbergdd2a2332011-11-07 01:14:07 +000029 uchar *buffer, unsigned cnt)
Heiko Schocher548738b2010-01-07 08:55:40 +010030{
31 int rcode;
32#if defined(CONFIG_I2C_ENV_EEPROM_BUS)
33 int old_bus = i2c_get_bus_num();
34
Heiko Schocher3f4978c2012-01-16 21:12:24 +000035 if (old_bus != CONFIG_I2C_ENV_EEPROM_BUS)
36 i2c_set_bus_num(CONFIG_I2C_ENV_EEPROM_BUS);
Heiko Schocher548738b2010-01-07 08:55:40 +010037#endif
38
Igor Grinbergdd2a2332011-11-07 01:14:07 +000039 rcode = eeprom_read(dev_addr, offset, buffer, cnt);
Wolfgang Denkea882ba2010-06-20 23:33:59 +020040
Heiko Schocher548738b2010-01-07 08:55:40 +010041#if defined(CONFIG_I2C_ENV_EEPROM_BUS)
Alexey Brodkin6d001e72013-12-11 18:04:40 +040042 i2c_set_bus_num(old_bus);
Heiko Schocher548738b2010-01-07 08:55:40 +010043#endif
Heiko Schocher9a2accb2012-10-25 10:32:14 +020044
Heiko Schocher548738b2010-01-07 08:55:40 +010045 return rcode;
46}
47
Wolfgang Denkea882ba2010-06-20 23:33:59 +020048static int eeprom_bus_write(unsigned dev_addr, unsigned offset,
Igor Grinbergdd2a2332011-11-07 01:14:07 +000049 uchar *buffer, unsigned cnt)
Heiko Schocher548738b2010-01-07 08:55:40 +010050{
51 int rcode;
52#if defined(CONFIG_I2C_ENV_EEPROM_BUS)
53 int old_bus = i2c_get_bus_num();
54
Heiko Schocher3f4978c2012-01-16 21:12:24 +000055 if (old_bus != CONFIG_I2C_ENV_EEPROM_BUS)
56 i2c_set_bus_num(CONFIG_I2C_ENV_EEPROM_BUS);
Heiko Schocher548738b2010-01-07 08:55:40 +010057#endif
Heiko Schocher9a2accb2012-10-25 10:32:14 +020058
Wolfgang Denkea882ba2010-06-20 23:33:59 +020059 rcode = eeprom_write(dev_addr, offset, buffer, cnt);
Heiko Schocher9a2accb2012-10-25 10:32:14 +020060
Heiko Schocher548738b2010-01-07 08:55:40 +010061#if defined(CONFIG_I2C_ENV_EEPROM_BUS)
62 i2c_set_bus_num(old_bus);
63#endif
Alexey Brodkin6d001e72013-12-11 18:04:40 +040064
Heiko Schocher548738b2010-01-07 08:55:40 +010065 return rcode;
66}
wdenk0bc4a1a2002-09-18 11:09:59 +000067
Igor Grinbergdd2a2332011-11-07 01:14:07 +000068uchar env_get_char_spec(int index)
wdenk0bc4a1a2002-09-18 11:09:59 +000069{
70 uchar c;
Igor Grinbergdd2a2332011-11-07 01:14:07 +000071 unsigned int off = CONFIG_ENV_OFFSET;
Wolfgang Denkea882ba2010-06-20 23:33:59 +020072
Heiko Schocher1567b592010-01-07 08:55:44 +010073#ifdef CONFIG_ENV_OFFSET_REDUND
74 if (gd->env_valid == 2)
75 off = CONFIG_ENV_OFFSET_REDUND;
76#endif
Wolfgang Denkea882ba2010-06-20 23:33:59 +020077 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
Igor Grinbergdd2a2332011-11-07 01:14:07 +000078 off + index + offsetof(env_t, data), &c, 1);
wdenk0bc4a1a2002-09-18 11:09:59 +000079
Igor Grinbergdd2a2332011-11-07 01:14:07 +000080 return c;
wdenk0bc4a1a2002-09-18 11:09:59 +000081}
82
Igor Grinbergdd2a2332011-11-07 01:14:07 +000083void env_relocate_spec(void)
wdenk0bc4a1a2002-09-18 11:09:59 +000084{
Tom Rinicd0f4fa2013-04-05 14:55:21 -040085 char buf[CONFIG_ENV_SIZE];
Heiko Schocher1567b592010-01-07 08:55:44 +010086 unsigned int off = CONFIG_ENV_OFFSET;
Wolfgang Denkea882ba2010-06-20 23:33:59 +020087
Heiko Schocher1567b592010-01-07 08:55:44 +010088#ifdef CONFIG_ENV_OFFSET_REDUND
89 if (gd->env_valid == 2)
90 off = CONFIG_ENV_OFFSET_REDUND;
91#endif
Wolfgang Denkea882ba2010-06-20 23:33:59 +020092 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
Igor Grinbergdd2a2332011-11-07 01:14:07 +000093 off, (uchar *)buf, CONFIG_ENV_SIZE);
Wolfgang Denkea882ba2010-06-20 23:33:59 +020094
95 env_import(buf, 1);
wdenk0bc4a1a2002-09-18 11:09:59 +000096}
97
98int saveenv(void)
99{
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400100 env_t env_new;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200101 ssize_t len;
102 char *res;
Igor Grinbergdd2a2332011-11-07 01:14:07 +0000103 int rc;
104 unsigned int off = CONFIG_ENV_OFFSET;
Heiko Schocher1567b592010-01-07 08:55:44 +0100105#ifdef CONFIG_ENV_OFFSET_REDUND
Igor Grinbergdd2a2332011-11-07 01:14:07 +0000106 unsigned int off_red = CONFIG_ENV_OFFSET_REDUND;
107 char flag_obsolete = OBSOLETE_FLAG;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200108#endif
109
110 BUG_ON(env_ptr != NULL);
111
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400112 res = (char *)&env_new.data;
Joe Hershbergerbe112352012-12-11 22:16:23 -0600113 len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200114 if (len < 0) {
115 error("Cannot export environment: errno = %d\n", errno);
116 return 1;
117 }
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400118 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200119
120#ifdef CONFIG_ENV_OFFSET_REDUND
Heiko Schocher1567b592010-01-07 08:55:44 +0100121 if (gd->env_valid == 1) {
Igor Grinbergdd2a2332011-11-07 01:14:07 +0000122 off = CONFIG_ENV_OFFSET_REDUND;
123 off_red = CONFIG_ENV_OFFSET;
Heiko Schocher1567b592010-01-07 08:55:44 +0100124 }
125
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400126 env_new.flags = ACTIVE_FLAG;
Heiko Schocher1567b592010-01-07 08:55:44 +0100127#endif
128
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200129 rc = eeprom_bus_write(CONFIG_SYS_DEF_EEPROM_ADDR,
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400130 off, (uchar *)&env_new, CONFIG_ENV_SIZE);
Heiko Schocher1567b592010-01-07 08:55:44 +0100131
132#ifdef CONFIG_ENV_OFFSET_REDUND
133 if (rc == 0) {
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200134 eeprom_bus_write(CONFIG_SYS_DEF_EEPROM_ADDR,
Igor Grinbergdd2a2332011-11-07 01:14:07 +0000135 off_red + offsetof(env_t, flags),
136 (uchar *)&flag_obsolete, 1);
137
Heiko Schocher1567b592010-01-07 08:55:44 +0100138 if (gd->env_valid == 1)
139 gd->env_valid = 2;
140 else
141 gd->env_valid = 1;
Heiko Schocher1567b592010-01-07 08:55:44 +0100142 }
143#endif
Heiko Schocher1567b592010-01-07 08:55:44 +0100144 return rc;
wdenk0bc4a1a2002-09-18 11:09:59 +0000145}
146
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200147/*
wdenk0bc4a1a2002-09-18 11:09:59 +0000148 * Initialize Environment use
149 *
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200150 * We are still running from ROM, so data use is limited.
wdenk0bc4a1a2002-09-18 11:09:59 +0000151 * Use a (moderately small) buffer on the stack
152 */
Heiko Schocher1567b592010-01-07 08:55:44 +0100153#ifdef CONFIG_ENV_OFFSET_REDUND
154int env_init(void)
155{
Igor Grinbergdd2a2332011-11-07 01:14:07 +0000156 ulong len, crc[2], crc_tmp;
Heiko Schocher1567b592010-01-07 08:55:44 +0100157 unsigned int off, off_env[2];
Igor Grinbergdd2a2332011-11-07 01:14:07 +0000158 uchar buf[64], flags[2];
159 int i, crc_ok[2] = {0, 0};
Heiko Schocher1567b592010-01-07 08:55:44 +0100160
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200161 eeprom_init(); /* prepare for EEPROM read/write */
Heiko Schocher1567b592010-01-07 08:55:44 +0100162
163 off_env[0] = CONFIG_ENV_OFFSET;
164 off_env[1] = CONFIG_ENV_OFFSET_REDUND;
165
166 for (i = 0; i < 2; i++) {
167 /* read CRC */
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200168 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
Igor Grinbergdd2a2332011-11-07 01:14:07 +0000169 off_env[i] + offsetof(env_t, crc),
170 (uchar *)&crc[i], sizeof(ulong));
Heiko Schocher1567b592010-01-07 08:55:44 +0100171 /* read FLAGS */
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200172 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
Igor Grinbergdd2a2332011-11-07 01:14:07 +0000173 off_env[i] + offsetof(env_t, flags),
174 (uchar *)&flags[i], sizeof(uchar));
Heiko Schocher1567b592010-01-07 08:55:44 +0100175
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200176 crc_tmp = 0;
Heiko Schocher1567b592010-01-07 08:55:44 +0100177 len = ENV_SIZE;
Igor Grinbergdd2a2332011-11-07 01:14:07 +0000178 off = off_env[i] + offsetof(env_t, data);
Heiko Schocher1567b592010-01-07 08:55:44 +0100179 while (len > 0) {
180 int n = (len > sizeof(buf)) ? sizeof(buf) : len;
181
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200182 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR, off,
Igor Grinbergdd2a2332011-11-07 01:14:07 +0000183 buf, n);
Heiko Schocher1567b592010-01-07 08:55:44 +0100184
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200185 crc_tmp = crc32(crc_tmp, buf, n);
Heiko Schocher1567b592010-01-07 08:55:44 +0100186 len -= n;
187 off += n;
188 }
Igor Grinbergdd2a2332011-11-07 01:14:07 +0000189
Heiko Schocher1567b592010-01-07 08:55:44 +0100190 if (crc_tmp == crc[i])
191 crc_ok[i] = 1;
192 }
193
194 if (!crc_ok[0] && !crc_ok[1]) {
Igor Grinbergdd2a2332011-11-07 01:14:07 +0000195 gd->env_addr = 0;
196 gd->env_valid = 0;
Heiko Schocher1567b592010-01-07 08:55:44 +0100197
198 return 0;
199 } else if (crc_ok[0] && !crc_ok[1]) {
200 gd->env_valid = 1;
Igor Grinbergdd2a2332011-11-07 01:14:07 +0000201 } else if (!crc_ok[0] && crc_ok[1]) {
Heiko Schocher1567b592010-01-07 08:55:44 +0100202 gd->env_valid = 2;
203 } else {
204 /* both ok - check serial */
205 if (flags[0] == ACTIVE_FLAG && flags[1] == OBSOLETE_FLAG)
206 gd->env_valid = 1;
207 else if (flags[0] == OBSOLETE_FLAG && flags[1] == ACTIVE_FLAG)
208 gd->env_valid = 2;
209 else if (flags[0] == 0xFF && flags[1] == 0)
210 gd->env_valid = 2;
Igor Grinbergdd2a2332011-11-07 01:14:07 +0000211 else if (flags[1] == 0xFF && flags[0] == 0)
Heiko Schocher1567b592010-01-07 08:55:44 +0100212 gd->env_valid = 1;
213 else /* flags are equal - almost impossible */
214 gd->env_valid = 1;
215 }
216
217 if (gd->env_valid == 2)
Igor Grinbergdd2a2332011-11-07 01:14:07 +0000218 gd->env_addr = off_env[1] + offsetof(env_t, data);
Heiko Schocher1567b592010-01-07 08:55:44 +0100219 else if (gd->env_valid == 1)
Igor Grinbergdd2a2332011-11-07 01:14:07 +0000220 gd->env_addr = off_env[0] + offsetof(env_t, data);
Heiko Schocher1567b592010-01-07 08:55:44 +0100221
Igor Grinbergdd2a2332011-11-07 01:14:07 +0000222 return 0;
Heiko Schocher1567b592010-01-07 08:55:44 +0100223}
224#else
wdenk0bc4a1a2002-09-18 11:09:59 +0000225int env_init(void)
226{
wdenk0bc4a1a2002-09-18 11:09:59 +0000227 ulong crc, len, new;
228 unsigned off;
229 uchar buf[64];
230
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200231 eeprom_init(); /* prepare for EEPROM read/write */
wdenk0bc4a1a2002-09-18 11:09:59 +0000232
233 /* read old CRC */
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200234 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
Igor Grinbergdd2a2332011-11-07 01:14:07 +0000235 CONFIG_ENV_OFFSET + offsetof(env_t, crc),
236 (uchar *)&crc, sizeof(ulong));
wdenk0bc4a1a2002-09-18 11:09:59 +0000237
238 new = 0;
239 len = ENV_SIZE;
Igor Grinbergdd2a2332011-11-07 01:14:07 +0000240 off = offsetof(env_t, data);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200241
wdenk0bc4a1a2002-09-18 11:09:59 +0000242 while (len > 0) {
243 int n = (len > sizeof(buf)) ? sizeof(buf) : len;
244
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200245 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
Heiko Schocher548738b2010-01-07 08:55:40 +0100246 CONFIG_ENV_OFFSET + off, buf, n);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200247 new = crc32(new, buf, n);
wdenk0bc4a1a2002-09-18 11:09:59 +0000248 len -= n;
249 off += n;
250 }
251
252 if (crc == new) {
Igor Grinbergdd2a2332011-11-07 01:14:07 +0000253 gd->env_addr = offsetof(env_t, data);
254 gd->env_valid = 1;
wdenk0bc4a1a2002-09-18 11:09:59 +0000255 } else {
Igor Grinbergdd2a2332011-11-07 01:14:07 +0000256 gd->env_addr = 0;
257 gd->env_valid = 0;
wdenk0bc4a1a2002-09-18 11:09:59 +0000258 }
259
Igor Grinbergdd2a2332011-11-07 01:14:07 +0000260 return 0;
wdenk0bc4a1a2002-09-18 11:09:59 +0000261}
Heiko Schocher1567b592010-01-07 08:55:44 +0100262#endif