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