wdenk | 0bc4a1a | 2002-09-18 11:09:59 +0000 | [diff] [blame] | 1 | /* |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 2 | * (C) Copyright 2000-2010 |
wdenk | 0bc4a1a | 2002-09-18 11:09:59 +0000 | [diff] [blame] | 3 | * 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 Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 7 | * |
wdenk | 0bc4a1a | 2002-09-18 11:09:59 +0000 | [diff] [blame] | 8 | * See file CREDITS for list of people who contributed to this |
| 9 | * project. |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or |
| 12 | * modify it under the terms of the GNU General Public License as |
| 13 | * published by the Free Software Foundation; either version 2 of |
| 14 | * the License, or (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with this program; if not, write to the Free Software |
| 23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 24 | * MA 02111-1307 USA |
| 25 | */ |
| 26 | |
| 27 | #include <common.h> |
wdenk | 0bc4a1a | 2002-09-18 11:09:59 +0000 | [diff] [blame] | 28 | #include <command.h> |
| 29 | #include <environment.h> |
wdenk | 0bc4a1a | 2002-09-18 11:09:59 +0000 | [diff] [blame] | 30 | #include <linux/stddef.h> |
Heiko Schocher | 9bb0dd5 | 2010-01-07 08:55:40 +0100 | [diff] [blame] | 31 | #if defined(CONFIG_I2C_ENV_EEPROM_BUS) |
| 32 | #include <i2c.h> |
| 33 | #endif |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 34 | #include <search.h> |
| 35 | #include <errno.h> |
| 36 | #include <linux/compiler.h> /* for BUG_ON */ |
wdenk | 0bc4a1a | 2002-09-18 11:09:59 +0000 | [diff] [blame] | 37 | |
Wolfgang Denk | 6405a15 | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 38 | DECLARE_GLOBAL_DATA_PTR; |
| 39 | |
Igor Grinberg | 13f851f | 2011-11-07 01:14:07 +0000 | [diff] [blame] | 40 | env_t *env_ptr; |
wdenk | 0bc4a1a | 2002-09-18 11:09:59 +0000 | [diff] [blame] | 41 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 42 | char *env_name_spec = "EEPROM"; |
Heiko Schocher | 9bb0dd5 | 2010-01-07 08:55:40 +0100 | [diff] [blame] | 43 | int env_eeprom_bus = -1; |
| 44 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 45 | static int eeprom_bus_read(unsigned dev_addr, unsigned offset, |
Igor Grinberg | 13f851f | 2011-11-07 01:14:07 +0000 | [diff] [blame] | 46 | uchar *buffer, unsigned cnt) |
Heiko Schocher | 9bb0dd5 | 2010-01-07 08:55:40 +0100 | [diff] [blame] | 47 | { |
| 48 | int rcode; |
| 49 | #if defined(CONFIG_I2C_ENV_EEPROM_BUS) |
| 50 | int old_bus = i2c_get_bus_num(); |
| 51 | |
| 52 | if (gd->flags & GD_FLG_RELOC) { |
| 53 | if (env_eeprom_bus == -1) { |
| 54 | I2C_MUX_DEVICE *dev = NULL; |
| 55 | dev = i2c_mux_ident_muxstring( |
| 56 | (uchar *)CONFIG_I2C_ENV_EEPROM_BUS); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 57 | if (dev != NULL) |
Heiko Schocher | 9bb0dd5 | 2010-01-07 08:55:40 +0100 | [diff] [blame] | 58 | env_eeprom_bus = dev->busid; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 59 | else |
Igor Grinberg | 13f851f | 2011-11-07 01:14:07 +0000 | [diff] [blame] | 60 | printf("error adding env eeprom bus.\n"); |
Heiko Schocher | 9bb0dd5 | 2010-01-07 08:55:40 +0100 | [diff] [blame] | 61 | } |
| 62 | if (old_bus != env_eeprom_bus) { |
| 63 | i2c_set_bus_num(env_eeprom_bus); |
| 64 | old_bus = env_eeprom_bus; |
| 65 | } |
| 66 | } else { |
| 67 | rcode = i2c_mux_ident_muxstring_f( |
| 68 | (uchar *)CONFIG_I2C_ENV_EEPROM_BUS); |
| 69 | } |
| 70 | #endif |
| 71 | |
Igor Grinberg | 13f851f | 2011-11-07 01:14:07 +0000 | [diff] [blame] | 72 | rcode = eeprom_read(dev_addr, offset, buffer, cnt); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 73 | |
Heiko Schocher | 9bb0dd5 | 2010-01-07 08:55:40 +0100 | [diff] [blame] | 74 | #if defined(CONFIG_I2C_ENV_EEPROM_BUS) |
| 75 | if (old_bus != env_eeprom_bus) |
| 76 | i2c_set_bus_num(old_bus); |
| 77 | #endif |
| 78 | return rcode; |
| 79 | } |
| 80 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 81 | static int eeprom_bus_write(unsigned dev_addr, unsigned offset, |
Igor Grinberg | 13f851f | 2011-11-07 01:14:07 +0000 | [diff] [blame] | 82 | uchar *buffer, unsigned cnt) |
Heiko Schocher | 9bb0dd5 | 2010-01-07 08:55:40 +0100 | [diff] [blame] | 83 | { |
| 84 | int rcode; |
| 85 | #if defined(CONFIG_I2C_ENV_EEPROM_BUS) |
| 86 | int old_bus = i2c_get_bus_num(); |
| 87 | |
| 88 | rcode = i2c_mux_ident_muxstring_f((uchar *)CONFIG_I2C_ENV_EEPROM_BUS); |
| 89 | #endif |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 90 | rcode = eeprom_write(dev_addr, offset, buffer, cnt); |
Heiko Schocher | 9bb0dd5 | 2010-01-07 08:55:40 +0100 | [diff] [blame] | 91 | #if defined(CONFIG_I2C_ENV_EEPROM_BUS) |
| 92 | i2c_set_bus_num(old_bus); |
| 93 | #endif |
| 94 | return rcode; |
| 95 | } |
wdenk | 0bc4a1a | 2002-09-18 11:09:59 +0000 | [diff] [blame] | 96 | |
Igor Grinberg | 13f851f | 2011-11-07 01:14:07 +0000 | [diff] [blame] | 97 | uchar env_get_char_spec(int index) |
wdenk | 0bc4a1a | 2002-09-18 11:09:59 +0000 | [diff] [blame] | 98 | { |
| 99 | uchar c; |
Igor Grinberg | 13f851f | 2011-11-07 01:14:07 +0000 | [diff] [blame] | 100 | unsigned int off = CONFIG_ENV_OFFSET; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 101 | |
Heiko Schocher | e719363 | 2010-01-07 08:55:44 +0100 | [diff] [blame] | 102 | #ifdef CONFIG_ENV_OFFSET_REDUND |
| 103 | if (gd->env_valid == 2) |
| 104 | off = CONFIG_ENV_OFFSET_REDUND; |
| 105 | #endif |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 106 | eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR, |
Igor Grinberg | 13f851f | 2011-11-07 01:14:07 +0000 | [diff] [blame] | 107 | off + index + offsetof(env_t, data), &c, 1); |
wdenk | 0bc4a1a | 2002-09-18 11:09:59 +0000 | [diff] [blame] | 108 | |
Igor Grinberg | 13f851f | 2011-11-07 01:14:07 +0000 | [diff] [blame] | 109 | return c; |
wdenk | 0bc4a1a | 2002-09-18 11:09:59 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Igor Grinberg | 13f851f | 2011-11-07 01:14:07 +0000 | [diff] [blame] | 112 | void env_relocate_spec(void) |
wdenk | 0bc4a1a | 2002-09-18 11:09:59 +0000 | [diff] [blame] | 113 | { |
Tom Rini | a840b8e | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 114 | char buf[CONFIG_ENV_SIZE]; |
Heiko Schocher | e719363 | 2010-01-07 08:55:44 +0100 | [diff] [blame] | 115 | unsigned int off = CONFIG_ENV_OFFSET; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 116 | |
Heiko Schocher | e719363 | 2010-01-07 08:55:44 +0100 | [diff] [blame] | 117 | #ifdef CONFIG_ENV_OFFSET_REDUND |
| 118 | if (gd->env_valid == 2) |
| 119 | off = CONFIG_ENV_OFFSET_REDUND; |
| 120 | #endif |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 121 | eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR, |
Igor Grinberg | 13f851f | 2011-11-07 01:14:07 +0000 | [diff] [blame] | 122 | off, (uchar *)buf, CONFIG_ENV_SIZE); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 123 | |
| 124 | env_import(buf, 1); |
wdenk | 0bc4a1a | 2002-09-18 11:09:59 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | int saveenv(void) |
| 128 | { |
Tom Rini | a840b8e | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 129 | env_t env_new; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 130 | ssize_t len; |
| 131 | char *res; |
Igor Grinberg | 13f851f | 2011-11-07 01:14:07 +0000 | [diff] [blame] | 132 | int rc; |
| 133 | unsigned int off = CONFIG_ENV_OFFSET; |
Heiko Schocher | e719363 | 2010-01-07 08:55:44 +0100 | [diff] [blame] | 134 | #ifdef CONFIG_ENV_OFFSET_REDUND |
Igor Grinberg | 13f851f | 2011-11-07 01:14:07 +0000 | [diff] [blame] | 135 | unsigned int off_red = CONFIG_ENV_OFFSET_REDUND; |
| 136 | char flag_obsolete = OBSOLETE_FLAG; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 137 | #endif |
| 138 | |
| 139 | BUG_ON(env_ptr != NULL); |
| 140 | |
Tom Rini | a840b8e | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 141 | res = (char *)&env_new.data; |
Joe Hershberger | 79a905e | 2012-12-11 22:16:23 -0600 | [diff] [blame] | 142 | len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 143 | if (len < 0) { |
| 144 | error("Cannot export environment: errno = %d\n", errno); |
| 145 | return 1; |
| 146 | } |
Tom Rini | a840b8e | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 147 | env_new.crc = crc32(0, env_new.data, ENV_SIZE); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 148 | |
| 149 | #ifdef CONFIG_ENV_OFFSET_REDUND |
Heiko Schocher | e719363 | 2010-01-07 08:55:44 +0100 | [diff] [blame] | 150 | if (gd->env_valid == 1) { |
Igor Grinberg | 13f851f | 2011-11-07 01:14:07 +0000 | [diff] [blame] | 151 | off = CONFIG_ENV_OFFSET_REDUND; |
| 152 | off_red = CONFIG_ENV_OFFSET; |
Heiko Schocher | e719363 | 2010-01-07 08:55:44 +0100 | [diff] [blame] | 153 | } |
| 154 | |
Tom Rini | a840b8e | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 155 | env_new.flags = ACTIVE_FLAG; |
Heiko Schocher | e719363 | 2010-01-07 08:55:44 +0100 | [diff] [blame] | 156 | #endif |
| 157 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 158 | rc = eeprom_bus_write(CONFIG_SYS_DEF_EEPROM_ADDR, |
Tom Rini | a840b8e | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 159 | off, (uchar *)&env_new, CONFIG_ENV_SIZE); |
Heiko Schocher | e719363 | 2010-01-07 08:55:44 +0100 | [diff] [blame] | 160 | |
| 161 | #ifdef CONFIG_ENV_OFFSET_REDUND |
| 162 | if (rc == 0) { |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 163 | eeprom_bus_write(CONFIG_SYS_DEF_EEPROM_ADDR, |
Igor Grinberg | 13f851f | 2011-11-07 01:14:07 +0000 | [diff] [blame] | 164 | off_red + offsetof(env_t, flags), |
| 165 | (uchar *)&flag_obsolete, 1); |
| 166 | |
Heiko Schocher | e719363 | 2010-01-07 08:55:44 +0100 | [diff] [blame] | 167 | if (gd->env_valid == 1) |
| 168 | gd->env_valid = 2; |
| 169 | else |
| 170 | gd->env_valid = 1; |
Heiko Schocher | e719363 | 2010-01-07 08:55:44 +0100 | [diff] [blame] | 171 | } |
| 172 | #endif |
Heiko Schocher | e719363 | 2010-01-07 08:55:44 +0100 | [diff] [blame] | 173 | return rc; |
wdenk | 0bc4a1a | 2002-09-18 11:09:59 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 176 | /* |
wdenk | 0bc4a1a | 2002-09-18 11:09:59 +0000 | [diff] [blame] | 177 | * Initialize Environment use |
| 178 | * |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 179 | * We are still running from ROM, so data use is limited. |
wdenk | 0bc4a1a | 2002-09-18 11:09:59 +0000 | [diff] [blame] | 180 | * Use a (moderately small) buffer on the stack |
| 181 | */ |
Heiko Schocher | e719363 | 2010-01-07 08:55:44 +0100 | [diff] [blame] | 182 | #ifdef CONFIG_ENV_OFFSET_REDUND |
| 183 | int env_init(void) |
| 184 | { |
Igor Grinberg | 13f851f | 2011-11-07 01:14:07 +0000 | [diff] [blame] | 185 | ulong len, crc[2], crc_tmp; |
Heiko Schocher | e719363 | 2010-01-07 08:55:44 +0100 | [diff] [blame] | 186 | unsigned int off, off_env[2]; |
Igor Grinberg | 13f851f | 2011-11-07 01:14:07 +0000 | [diff] [blame] | 187 | uchar buf[64], flags[2]; |
| 188 | int i, crc_ok[2] = {0, 0}; |
Heiko Schocher | e719363 | 2010-01-07 08:55:44 +0100 | [diff] [blame] | 189 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 190 | eeprom_init(); /* prepare for EEPROM read/write */ |
Heiko Schocher | e719363 | 2010-01-07 08:55:44 +0100 | [diff] [blame] | 191 | |
| 192 | off_env[0] = CONFIG_ENV_OFFSET; |
| 193 | off_env[1] = CONFIG_ENV_OFFSET_REDUND; |
| 194 | |
| 195 | for (i = 0; i < 2; i++) { |
| 196 | /* read CRC */ |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 197 | eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR, |
Igor Grinberg | 13f851f | 2011-11-07 01:14:07 +0000 | [diff] [blame] | 198 | off_env[i] + offsetof(env_t, crc), |
| 199 | (uchar *)&crc[i], sizeof(ulong)); |
Heiko Schocher | e719363 | 2010-01-07 08:55:44 +0100 | [diff] [blame] | 200 | /* read FLAGS */ |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 201 | eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR, |
Igor Grinberg | 13f851f | 2011-11-07 01:14:07 +0000 | [diff] [blame] | 202 | off_env[i] + offsetof(env_t, flags), |
| 203 | (uchar *)&flags[i], sizeof(uchar)); |
Heiko Schocher | e719363 | 2010-01-07 08:55:44 +0100 | [diff] [blame] | 204 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 205 | crc_tmp = 0; |
Heiko Schocher | e719363 | 2010-01-07 08:55:44 +0100 | [diff] [blame] | 206 | len = ENV_SIZE; |
Igor Grinberg | 13f851f | 2011-11-07 01:14:07 +0000 | [diff] [blame] | 207 | off = off_env[i] + offsetof(env_t, data); |
Heiko Schocher | e719363 | 2010-01-07 08:55:44 +0100 | [diff] [blame] | 208 | while (len > 0) { |
| 209 | int n = (len > sizeof(buf)) ? sizeof(buf) : len; |
| 210 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 211 | eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR, off, |
Igor Grinberg | 13f851f | 2011-11-07 01:14:07 +0000 | [diff] [blame] | 212 | buf, n); |
Heiko Schocher | e719363 | 2010-01-07 08:55:44 +0100 | [diff] [blame] | 213 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 214 | crc_tmp = crc32(crc_tmp, buf, n); |
Heiko Schocher | e719363 | 2010-01-07 08:55:44 +0100 | [diff] [blame] | 215 | len -= n; |
| 216 | off += n; |
| 217 | } |
Igor Grinberg | 13f851f | 2011-11-07 01:14:07 +0000 | [diff] [blame] | 218 | |
Heiko Schocher | e719363 | 2010-01-07 08:55:44 +0100 | [diff] [blame] | 219 | if (crc_tmp == crc[i]) |
| 220 | crc_ok[i] = 1; |
| 221 | } |
| 222 | |
| 223 | if (!crc_ok[0] && !crc_ok[1]) { |
Igor Grinberg | 13f851f | 2011-11-07 01:14:07 +0000 | [diff] [blame] | 224 | gd->env_addr = 0; |
| 225 | gd->env_valid = 0; |
Heiko Schocher | e719363 | 2010-01-07 08:55:44 +0100 | [diff] [blame] | 226 | |
| 227 | return 0; |
| 228 | } else if (crc_ok[0] && !crc_ok[1]) { |
| 229 | gd->env_valid = 1; |
Igor Grinberg | 13f851f | 2011-11-07 01:14:07 +0000 | [diff] [blame] | 230 | } else if (!crc_ok[0] && crc_ok[1]) { |
Heiko Schocher | e719363 | 2010-01-07 08:55:44 +0100 | [diff] [blame] | 231 | gd->env_valid = 2; |
| 232 | } else { |
| 233 | /* both ok - check serial */ |
| 234 | if (flags[0] == ACTIVE_FLAG && flags[1] == OBSOLETE_FLAG) |
| 235 | gd->env_valid = 1; |
| 236 | else if (flags[0] == OBSOLETE_FLAG && flags[1] == ACTIVE_FLAG) |
| 237 | gd->env_valid = 2; |
| 238 | else if (flags[0] == 0xFF && flags[1] == 0) |
| 239 | gd->env_valid = 2; |
Igor Grinberg | 13f851f | 2011-11-07 01:14:07 +0000 | [diff] [blame] | 240 | else if (flags[1] == 0xFF && flags[0] == 0) |
Heiko Schocher | e719363 | 2010-01-07 08:55:44 +0100 | [diff] [blame] | 241 | gd->env_valid = 1; |
| 242 | else /* flags are equal - almost impossible */ |
| 243 | gd->env_valid = 1; |
| 244 | } |
| 245 | |
| 246 | if (gd->env_valid == 2) |
Igor Grinberg | 13f851f | 2011-11-07 01:14:07 +0000 | [diff] [blame] | 247 | gd->env_addr = off_env[1] + offsetof(env_t, data); |
Heiko Schocher | e719363 | 2010-01-07 08:55:44 +0100 | [diff] [blame] | 248 | else if (gd->env_valid == 1) |
Igor Grinberg | 13f851f | 2011-11-07 01:14:07 +0000 | [diff] [blame] | 249 | gd->env_addr = off_env[0] + offsetof(env_t, data); |
Heiko Schocher | e719363 | 2010-01-07 08:55:44 +0100 | [diff] [blame] | 250 | |
Igor Grinberg | 13f851f | 2011-11-07 01:14:07 +0000 | [diff] [blame] | 251 | return 0; |
Heiko Schocher | e719363 | 2010-01-07 08:55:44 +0100 | [diff] [blame] | 252 | } |
| 253 | #else |
wdenk | 0bc4a1a | 2002-09-18 11:09:59 +0000 | [diff] [blame] | 254 | int env_init(void) |
| 255 | { |
wdenk | 0bc4a1a | 2002-09-18 11:09:59 +0000 | [diff] [blame] | 256 | ulong crc, len, new; |
| 257 | unsigned off; |
| 258 | uchar buf[64]; |
| 259 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 260 | eeprom_init(); /* prepare for EEPROM read/write */ |
wdenk | 0bc4a1a | 2002-09-18 11:09:59 +0000 | [diff] [blame] | 261 | |
| 262 | /* read old CRC */ |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 263 | eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR, |
Igor Grinberg | 13f851f | 2011-11-07 01:14:07 +0000 | [diff] [blame] | 264 | CONFIG_ENV_OFFSET + offsetof(env_t, crc), |
| 265 | (uchar *)&crc, sizeof(ulong)); |
wdenk | 0bc4a1a | 2002-09-18 11:09:59 +0000 | [diff] [blame] | 266 | |
| 267 | new = 0; |
| 268 | len = ENV_SIZE; |
Igor Grinberg | 13f851f | 2011-11-07 01:14:07 +0000 | [diff] [blame] | 269 | off = offsetof(env_t, data); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 270 | |
wdenk | 0bc4a1a | 2002-09-18 11:09:59 +0000 | [diff] [blame] | 271 | while (len > 0) { |
| 272 | int n = (len > sizeof(buf)) ? sizeof(buf) : len; |
| 273 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 274 | eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR, |
Heiko Schocher | 9bb0dd5 | 2010-01-07 08:55:40 +0100 | [diff] [blame] | 275 | CONFIG_ENV_OFFSET + off, buf, n); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 276 | new = crc32(new, buf, n); |
wdenk | 0bc4a1a | 2002-09-18 11:09:59 +0000 | [diff] [blame] | 277 | len -= n; |
| 278 | off += n; |
| 279 | } |
| 280 | |
| 281 | if (crc == new) { |
Igor Grinberg | 13f851f | 2011-11-07 01:14:07 +0000 | [diff] [blame] | 282 | gd->env_addr = offsetof(env_t, data); |
| 283 | gd->env_valid = 1; |
wdenk | 0bc4a1a | 2002-09-18 11:09:59 +0000 | [diff] [blame] | 284 | } else { |
Igor Grinberg | 13f851f | 2011-11-07 01:14:07 +0000 | [diff] [blame] | 285 | gd->env_addr = 0; |
| 286 | gd->env_valid = 0; |
wdenk | 0bc4a1a | 2002-09-18 11:09:59 +0000 | [diff] [blame] | 287 | } |
| 288 | |
Igor Grinberg | 13f851f | 2011-11-07 01:14:07 +0000 | [diff] [blame] | 289 | return 0; |
wdenk | 0bc4a1a | 2002-09-18 11:09:59 +0000 | [diff] [blame] | 290 | } |
Heiko Schocher | e719363 | 2010-01-07 08:55:44 +0100 | [diff] [blame] | 291 | #endif |