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