blob: 149370f8348046d2b87a305aa0af503a306c0123 [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 Denkd79de1d2013-07-08 09:37:19 +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
Igor Grinberg13f851f2011-11-07 01:14:07 +000024env_t *env_ptr;
wdenk0bc4a1a2002-09-18 11:09:59 +000025
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020026char *env_name_spec = "EEPROM";
Heiko Schocher9bb0dd52010-01-07 08:55:40 +010027int env_eeprom_bus = -1;
28
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020029static int eeprom_bus_read(unsigned dev_addr, unsigned offset,
Igor Grinberg13f851f2011-11-07 01:14:07 +000030 uchar *buffer, unsigned cnt)
Heiko Schocher9bb0dd52010-01-07 08:55:40 +010031{
32 int rcode;
33#if defined(CONFIG_I2C_ENV_EEPROM_BUS)
34 int old_bus = i2c_get_bus_num();
35
Heiko Schochere0e55bc2012-01-16 21:12:24 +000036 if (old_bus != CONFIG_I2C_ENV_EEPROM_BUS)
37 i2c_set_bus_num(CONFIG_I2C_ENV_EEPROM_BUS);
Heiko Schocher9bb0dd52010-01-07 08:55:40 +010038#endif
39
Igor Grinberg13f851f2011-11-07 01:14:07 +000040 rcode = eeprom_read(dev_addr, offset, buffer, cnt);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020041
Heiko Schocher9bb0dd52010-01-07 08:55:40 +010042#if defined(CONFIG_I2C_ENV_EEPROM_BUS)
43 if (old_bus != env_eeprom_bus)
44 i2c_set_bus_num(old_bus);
45#endif
Heiko Schochera4ea4452012-10-25 10:32:14 +020046
Heiko Schocher9bb0dd52010-01-07 08:55:40 +010047 return rcode;
48}
49
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020050static int eeprom_bus_write(unsigned dev_addr, unsigned offset,
Igor Grinberg13f851f2011-11-07 01:14:07 +000051 uchar *buffer, unsigned cnt)
Heiko Schocher9bb0dd52010-01-07 08:55:40 +010052{
53 int rcode;
54#if defined(CONFIG_I2C_ENV_EEPROM_BUS)
55 int old_bus = i2c_get_bus_num();
56
Heiko Schochere0e55bc2012-01-16 21:12:24 +000057 if (old_bus != CONFIG_I2C_ENV_EEPROM_BUS)
58 i2c_set_bus_num(CONFIG_I2C_ENV_EEPROM_BUS);
Heiko Schocher9bb0dd52010-01-07 08:55:40 +010059#endif
Heiko Schochera4ea4452012-10-25 10:32:14 +020060
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020061 rcode = eeprom_write(dev_addr, offset, buffer, cnt);
Heiko Schochera4ea4452012-10-25 10:32:14 +020062
Heiko Schocher9bb0dd52010-01-07 08:55:40 +010063#if defined(CONFIG_I2C_ENV_EEPROM_BUS)
64 i2c_set_bus_num(old_bus);
65#endif
66 return rcode;
67}
wdenk0bc4a1a2002-09-18 11:09:59 +000068
Igor Grinberg13f851f2011-11-07 01:14:07 +000069uchar env_get_char_spec(int index)
wdenk0bc4a1a2002-09-18 11:09:59 +000070{
71 uchar c;
Igor Grinberg13f851f2011-11-07 01:14:07 +000072 unsigned int off = CONFIG_ENV_OFFSET;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020073
Heiko Schochere7193632010-01-07 08:55:44 +010074#ifdef CONFIG_ENV_OFFSET_REDUND
75 if (gd->env_valid == 2)
76 off = CONFIG_ENV_OFFSET_REDUND;
77#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020078 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
Igor Grinberg13f851f2011-11-07 01:14:07 +000079 off + index + offsetof(env_t, data), &c, 1);
wdenk0bc4a1a2002-09-18 11:09:59 +000080
Igor Grinberg13f851f2011-11-07 01:14:07 +000081 return c;
wdenk0bc4a1a2002-09-18 11:09:59 +000082}
83
Igor Grinberg13f851f2011-11-07 01:14:07 +000084void env_relocate_spec(void)
wdenk0bc4a1a2002-09-18 11:09:59 +000085{
Tom Rinia840b8e2013-04-05 14:55:21 -040086 char buf[CONFIG_ENV_SIZE];
Heiko Schochere7193632010-01-07 08:55:44 +010087 unsigned int off = CONFIG_ENV_OFFSET;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020088
Heiko Schochere7193632010-01-07 08:55:44 +010089#ifdef CONFIG_ENV_OFFSET_REDUND
90 if (gd->env_valid == 2)
91 off = CONFIG_ENV_OFFSET_REDUND;
92#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020093 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
Igor Grinberg13f851f2011-11-07 01:14:07 +000094 off, (uchar *)buf, CONFIG_ENV_SIZE);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020095
96 env_import(buf, 1);
wdenk0bc4a1a2002-09-18 11:09:59 +000097}
98
99int saveenv(void)
100{
Tom Rinia840b8e2013-04-05 14:55:21 -0400101 env_t env_new;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200102 ssize_t len;
103 char *res;
Igor Grinberg13f851f2011-11-07 01:14:07 +0000104 int rc;
105 unsigned int off = CONFIG_ENV_OFFSET;
Heiko Schochere7193632010-01-07 08:55:44 +0100106#ifdef CONFIG_ENV_OFFSET_REDUND
Igor Grinberg13f851f2011-11-07 01:14:07 +0000107 unsigned int off_red = CONFIG_ENV_OFFSET_REDUND;
108 char flag_obsolete = OBSOLETE_FLAG;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200109#endif
110
111 BUG_ON(env_ptr != NULL);
112
Tom Rinia840b8e2013-04-05 14:55:21 -0400113 res = (char *)&env_new.data;
Joe Hershberger79a905e2012-12-11 22:16:23 -0600114 len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200115 if (len < 0) {
116 error("Cannot export environment: errno = %d\n", errno);
117 return 1;
118 }
Tom Rinia840b8e2013-04-05 14:55:21 -0400119 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200120
121#ifdef CONFIG_ENV_OFFSET_REDUND
Heiko Schochere7193632010-01-07 08:55:44 +0100122 if (gd->env_valid == 1) {
Igor Grinberg13f851f2011-11-07 01:14:07 +0000123 off = CONFIG_ENV_OFFSET_REDUND;
124 off_red = CONFIG_ENV_OFFSET;
Heiko Schochere7193632010-01-07 08:55:44 +0100125 }
126
Tom Rinia840b8e2013-04-05 14:55:21 -0400127 env_new.flags = ACTIVE_FLAG;
Heiko Schochere7193632010-01-07 08:55:44 +0100128#endif
129
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200130 rc = eeprom_bus_write(CONFIG_SYS_DEF_EEPROM_ADDR,
Tom Rinia840b8e2013-04-05 14:55:21 -0400131 off, (uchar *)&env_new, CONFIG_ENV_SIZE);
Heiko Schochere7193632010-01-07 08:55:44 +0100132
133#ifdef CONFIG_ENV_OFFSET_REDUND
134 if (rc == 0) {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200135 eeprom_bus_write(CONFIG_SYS_DEF_EEPROM_ADDR,
Igor Grinberg13f851f2011-11-07 01:14:07 +0000136 off_red + offsetof(env_t, flags),
137 (uchar *)&flag_obsolete, 1);
138
Heiko Schochere7193632010-01-07 08:55:44 +0100139 if (gd->env_valid == 1)
140 gd->env_valid = 2;
141 else
142 gd->env_valid = 1;
Heiko Schochere7193632010-01-07 08:55:44 +0100143 }
144#endif
Heiko Schochere7193632010-01-07 08:55:44 +0100145 return rc;
wdenk0bc4a1a2002-09-18 11:09:59 +0000146}
147
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200148/*
wdenk0bc4a1a2002-09-18 11:09:59 +0000149 * Initialize Environment use
150 *
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200151 * We are still running from ROM, so data use is limited.
wdenk0bc4a1a2002-09-18 11:09:59 +0000152 * Use a (moderately small) buffer on the stack
153 */
Heiko Schochere7193632010-01-07 08:55:44 +0100154#ifdef CONFIG_ENV_OFFSET_REDUND
155int env_init(void)
156{
Igor Grinberg13f851f2011-11-07 01:14:07 +0000157 ulong len, crc[2], crc_tmp;
Heiko Schochere7193632010-01-07 08:55:44 +0100158 unsigned int off, off_env[2];
Igor Grinberg13f851f2011-11-07 01:14:07 +0000159 uchar buf[64], flags[2];
160 int i, crc_ok[2] = {0, 0};
Heiko Schochere7193632010-01-07 08:55:44 +0100161
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200162 eeprom_init(); /* prepare for EEPROM read/write */
Heiko Schochere7193632010-01-07 08:55:44 +0100163
164 off_env[0] = CONFIG_ENV_OFFSET;
165 off_env[1] = CONFIG_ENV_OFFSET_REDUND;
166
167 for (i = 0; i < 2; i++) {
168 /* read CRC */
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200169 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
Igor Grinberg13f851f2011-11-07 01:14:07 +0000170 off_env[i] + offsetof(env_t, crc),
171 (uchar *)&crc[i], sizeof(ulong));
Heiko Schochere7193632010-01-07 08:55:44 +0100172 /* read FLAGS */
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200173 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
Igor Grinberg13f851f2011-11-07 01:14:07 +0000174 off_env[i] + offsetof(env_t, flags),
175 (uchar *)&flags[i], sizeof(uchar));
Heiko Schochere7193632010-01-07 08:55:44 +0100176
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200177 crc_tmp = 0;
Heiko Schochere7193632010-01-07 08:55:44 +0100178 len = ENV_SIZE;
Igor Grinberg13f851f2011-11-07 01:14:07 +0000179 off = off_env[i] + offsetof(env_t, data);
Heiko Schochere7193632010-01-07 08:55:44 +0100180 while (len > 0) {
181 int n = (len > sizeof(buf)) ? sizeof(buf) : len;
182
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200183 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR, off,
Igor Grinberg13f851f2011-11-07 01:14:07 +0000184 buf, n);
Heiko Schochere7193632010-01-07 08:55:44 +0100185
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200186 crc_tmp = crc32(crc_tmp, buf, n);
Heiko Schochere7193632010-01-07 08:55:44 +0100187 len -= n;
188 off += n;
189 }
Igor Grinberg13f851f2011-11-07 01:14:07 +0000190
Heiko Schochere7193632010-01-07 08:55:44 +0100191 if (crc_tmp == crc[i])
192 crc_ok[i] = 1;
193 }
194
195 if (!crc_ok[0] && !crc_ok[1]) {
Igor Grinberg13f851f2011-11-07 01:14:07 +0000196 gd->env_addr = 0;
197 gd->env_valid = 0;
Heiko Schochere7193632010-01-07 08:55:44 +0100198
199 return 0;
200 } else if (crc_ok[0] && !crc_ok[1]) {
201 gd->env_valid = 1;
Igor Grinberg13f851f2011-11-07 01:14:07 +0000202 } else if (!crc_ok[0] && crc_ok[1]) {
Heiko Schochere7193632010-01-07 08:55:44 +0100203 gd->env_valid = 2;
204 } else {
205 /* both ok - check serial */
206 if (flags[0] == ACTIVE_FLAG && flags[1] == OBSOLETE_FLAG)
207 gd->env_valid = 1;
208 else if (flags[0] == OBSOLETE_FLAG && flags[1] == ACTIVE_FLAG)
209 gd->env_valid = 2;
210 else if (flags[0] == 0xFF && flags[1] == 0)
211 gd->env_valid = 2;
Igor Grinberg13f851f2011-11-07 01:14:07 +0000212 else if (flags[1] == 0xFF && flags[0] == 0)
Heiko Schochere7193632010-01-07 08:55:44 +0100213 gd->env_valid = 1;
214 else /* flags are equal - almost impossible */
215 gd->env_valid = 1;
216 }
217
218 if (gd->env_valid == 2)
Igor Grinberg13f851f2011-11-07 01:14:07 +0000219 gd->env_addr = off_env[1] + offsetof(env_t, data);
Heiko Schochere7193632010-01-07 08:55:44 +0100220 else if (gd->env_valid == 1)
Igor Grinberg13f851f2011-11-07 01:14:07 +0000221 gd->env_addr = off_env[0] + offsetof(env_t, data);
Heiko Schochere7193632010-01-07 08:55:44 +0100222
Igor Grinberg13f851f2011-11-07 01:14:07 +0000223 return 0;
Heiko Schochere7193632010-01-07 08:55:44 +0100224}
225#else
wdenk0bc4a1a2002-09-18 11:09:59 +0000226int env_init(void)
227{
wdenk0bc4a1a2002-09-18 11:09:59 +0000228 ulong crc, len, new;
229 unsigned off;
230 uchar buf[64];
231
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200232 eeprom_init(); /* prepare for EEPROM read/write */
wdenk0bc4a1a2002-09-18 11:09:59 +0000233
234 /* read old CRC */
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200235 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
Igor Grinberg13f851f2011-11-07 01:14:07 +0000236 CONFIG_ENV_OFFSET + offsetof(env_t, crc),
237 (uchar *)&crc, sizeof(ulong));
wdenk0bc4a1a2002-09-18 11:09:59 +0000238
239 new = 0;
240 len = ENV_SIZE;
Igor Grinberg13f851f2011-11-07 01:14:07 +0000241 off = offsetof(env_t, data);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200242
wdenk0bc4a1a2002-09-18 11:09:59 +0000243 while (len > 0) {
244 int n = (len > sizeof(buf)) ? sizeof(buf) : len;
245
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200246 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
Heiko Schocher9bb0dd52010-01-07 08:55:40 +0100247 CONFIG_ENV_OFFSET + off, buf, n);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200248 new = crc32(new, buf, n);
wdenk0bc4a1a2002-09-18 11:09:59 +0000249 len -= n;
250 off += n;
251 }
252
253 if (crc == new) {
Igor Grinberg13f851f2011-11-07 01:14:07 +0000254 gd->env_addr = offsetof(env_t, data);
255 gd->env_valid = 1;
wdenk0bc4a1a2002-09-18 11:09:59 +0000256 } else {
Igor Grinberg13f851f2011-11-07 01:14:07 +0000257 gd->env_addr = 0;
258 gd->env_valid = 0;
wdenk0bc4a1a2002-09-18 11:09:59 +0000259 }
260
Igor Grinberg13f851f2011-11-07 01:14:07 +0000261 return 0;
wdenk0bc4a1a2002-09-18 11:09:59 +0000262}
Heiko Schochere7193632010-01-07 08:55:44 +0100263#endif