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