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