]> Git Repo - J-u-boot.git/blame - env/eeprom.c
env: mmc: Add missing eMMC bootpart restoration to env erase
[J-u-boot.git] / env / eeprom.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
0bc4a1ac 2/*
ea882baf 3 * (C) Copyright 2000-2010
0bc4a1ac
WD
4 * Wolfgang Denk, DENX Software Engineering, [email protected].
5 *
6 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
7 * Andreas Heppel <[email protected]>
0bc4a1ac
WD
8 */
9
10#include <common.h>
0bc4a1ac 11#include <command.h>
cb3ef681 12#include <eeprom.h>
7b51b576 13#include <env.h>
f3998fdc 14#include <env_internal.h>
401d1c4f 15#include <asm/global_data.h>
0bc4a1ac 16#include <linux/stddef.h>
3db71108 17#include <u-boot/crc.h>
548738b4
HS
18#if defined(CONFIG_I2C_ENV_EEPROM_BUS)
19#include <i2c.h>
20#endif
ea882baf
WD
21#include <search.h>
22#include <errno.h>
23#include <linux/compiler.h> /* for BUG_ON */
0bc4a1ac 24
d87080b7
WD
25DECLARE_GLOBAL_DATA_PTR;
26
ea882baf 27static int eeprom_bus_read(unsigned dev_addr, unsigned offset,
dd2a233c 28 uchar *buffer, unsigned cnt)
548738b4
HS
29{
30 int rcode;
31#if defined(CONFIG_I2C_ENV_EEPROM_BUS)
32 int old_bus = i2c_get_bus_num();
33
3f4978c7
HS
34 if (old_bus != CONFIG_I2C_ENV_EEPROM_BUS)
35 i2c_set_bus_num(CONFIG_I2C_ENV_EEPROM_BUS);
548738b4
HS
36#endif
37
dd2a233c 38 rcode = eeprom_read(dev_addr, offset, buffer, cnt);
ea882baf 39
548738b4 40#if defined(CONFIG_I2C_ENV_EEPROM_BUS)
6d001e7d 41 i2c_set_bus_num(old_bus);
548738b4 42#endif
9a2accb4 43
548738b4
HS
44 return rcode;
45}
46
ea882baf 47static int eeprom_bus_write(unsigned dev_addr, unsigned offset,
dd2a233c 48 uchar *buffer, unsigned cnt)
548738b4
HS
49{
50 int rcode;
51#if defined(CONFIG_I2C_ENV_EEPROM_BUS)
52 int old_bus = i2c_get_bus_num();
53
3f4978c7
HS
54 if (old_bus != CONFIG_I2C_ENV_EEPROM_BUS)
55 i2c_set_bus_num(CONFIG_I2C_ENV_EEPROM_BUS);
548738b4 56#endif
9a2accb4 57
ea882baf 58 rcode = eeprom_write(dev_addr, offset, buffer, cnt);
9a2accb4 59
548738b4
HS
60#if defined(CONFIG_I2C_ENV_EEPROM_BUS)
61 i2c_set_bus_num(old_bus);
62#endif
6d001e7d 63
548738b4
HS
64 return rcode;
65}
0bc4a1ac 66
c5951991 67static int env_eeprom_load(void)
0bc4a1ac 68{
e3cc5bc5 69 char buf_env[CONFIG_ENV_SIZE];
1567b596 70 unsigned int off = CONFIG_ENV_OFFSET;
ea882baf 71
1567b596 72#ifdef CONFIG_ENV_OFFSET_REDUND
dd2a233c 73 ulong len, crc[2], crc_tmp;
e3cc5bc5
LD
74 unsigned int off_env[2];
75 uchar rdbuf[64], flags[2];
dd2a233c 76 int i, crc_ok[2] = {0, 0};
1567b596 77
354e3ed7 78 eeprom_init(-1); /* prepare for EEPROM read/write */
1567b596
HS
79
80 off_env[0] = CONFIG_ENV_OFFSET;
81 off_env[1] = CONFIG_ENV_OFFSET_REDUND;
82
83 for (i = 0; i < 2; i++) {
84 /* read CRC */
88cd7d0e 85 eeprom_bus_read(CONFIG_SYS_I2C_EEPROM_ADDR,
dd2a233c
IG
86 off_env[i] + offsetof(env_t, crc),
87 (uchar *)&crc[i], sizeof(ulong));
1567b596 88 /* read FLAGS */
88cd7d0e 89 eeprom_bus_read(CONFIG_SYS_I2C_EEPROM_ADDR,
dd2a233c
IG
90 off_env[i] + offsetof(env_t, flags),
91 (uchar *)&flags[i], sizeof(uchar));
1567b596 92
ea882baf 93 crc_tmp = 0;
1567b596 94 len = ENV_SIZE;
dd2a233c 95 off = off_env[i] + offsetof(env_t, data);
1567b596 96 while (len > 0) {
e3cc5bc5 97 int n = (len > sizeof(rdbuf)) ? sizeof(rdbuf) : len;
1567b596 98
88cd7d0e 99 eeprom_bus_read(CONFIG_SYS_I2C_EEPROM_ADDR, off,
e3cc5bc5 100 rdbuf, n);
1567b596 101
e3cc5bc5 102 crc_tmp = crc32(crc_tmp, rdbuf, n);
1567b596
HS
103 len -= n;
104 off += n;
105 }
dd2a233c 106
1567b596
HS
107 if (crc_tmp == crc[i])
108 crc_ok[i] = 1;
109 }
110
111 if (!crc_ok[0] && !crc_ok[1]) {
dd2a233c 112 gd->env_addr = 0;
2d7cb5b4 113 gd->env_valid = ENV_INVALID;
1567b596 114 } else if (crc_ok[0] && !crc_ok[1]) {
203e94f6 115 gd->env_valid = ENV_VALID;
dd2a233c 116 } else if (!crc_ok[0] && crc_ok[1]) {
203e94f6 117 gd->env_valid = ENV_REDUND;
1567b596
HS
118 } else {
119 /* both ok - check serial */
d3716dd6
SG
120 if (flags[0] == ENV_REDUND_ACTIVE &&
121 flags[1] == ENV_REDUND_OBSOLETE)
203e94f6 122 gd->env_valid = ENV_VALID;
d3716dd6
SG
123 else if (flags[0] == ENV_REDUND_OBSOLETE &&
124 flags[1] == ENV_REDUND_ACTIVE)
203e94f6 125 gd->env_valid = ENV_REDUND;
1567b596 126 else if (flags[0] == 0xFF && flags[1] == 0)
203e94f6 127 gd->env_valid = ENV_REDUND;
dd2a233c 128 else if (flags[1] == 0xFF && flags[0] == 0)
203e94f6 129 gd->env_valid = ENV_VALID;
1567b596 130 else /* flags are equal - almost impossible */
203e94f6 131 gd->env_valid = ENV_VALID;
1567b596
HS
132 }
133
e3cc5bc5 134#else /* CONFIG_ENV_OFFSET_REDUND */
0bc4a1ac 135 ulong crc, len, new;
e3cc5bc5 136 uchar rdbuf[64];
0bc4a1ac 137
354e3ed7 138 eeprom_init(-1); /* prepare for EEPROM read/write */
0bc4a1ac
WD
139
140 /* read old CRC */
88cd7d0e 141 eeprom_bus_read(CONFIG_SYS_I2C_EEPROM_ADDR,
dd2a233c
IG
142 CONFIG_ENV_OFFSET + offsetof(env_t, crc),
143 (uchar *)&crc, sizeof(ulong));
0bc4a1ac
WD
144
145 new = 0;
146 len = ENV_SIZE;
dd2a233c 147 off = offsetof(env_t, data);
0bc4a1ac 148 while (len > 0) {
e3cc5bc5 149 int n = (len > sizeof(rdbuf)) ? sizeof(rdbuf) : len;
0bc4a1ac 150
88cd7d0e 151 eeprom_bus_read(CONFIG_SYS_I2C_EEPROM_ADDR,
e3cc5bc5
LD
152 CONFIG_ENV_OFFSET + off, rdbuf, n);
153 new = crc32(new, rdbuf, n);
0bc4a1ac
WD
154 len -= n;
155 off += n;
156 }
157
158 if (crc == new) {
2d7cb5b4 159 gd->env_valid = ENV_VALID;
0bc4a1ac 160 } else {
2d7cb5b4 161 gd->env_valid = ENV_INVALID;
0bc4a1ac 162 }
e3cc5bc5
LD
163#endif /* CONFIG_ENV_OFFSET_REDUND */
164
165 off = CONFIG_ENV_OFFSET;
166#ifdef CONFIG_ENV_OFFSET_REDUND
203e94f6 167 if (gd->env_valid == ENV_REDUND)
e3cc5bc5
LD
168 off = CONFIG_ENV_OFFSET_REDUND;
169#endif
170
88cd7d0e 171 eeprom_bus_read(CONFIG_SYS_I2C_EEPROM_ADDR,
e3cc5bc5
LD
172 off, (uchar *)buf_env, CONFIG_ENV_SIZE);
173
890feeca 174 return env_import(buf_env, 1, H_EXTERNAL);
e3cc5bc5
LD
175}
176
e5bce247 177static int env_eeprom_save(void)
e3cc5bc5
LD
178{
179 env_t env_new;
180 int rc;
181 unsigned int off = CONFIG_ENV_OFFSET;
182#ifdef CONFIG_ENV_OFFSET_REDUND
183 unsigned int off_red = CONFIG_ENV_OFFSET_REDUND;
d3716dd6 184 char flag_obsolete = ENV_REDUND_OBSOLETE;
e3cc5bc5
LD
185#endif
186
e3cc5bc5
LD
187 rc = env_export(&env_new);
188 if (rc)
189 return rc;
190
191#ifdef CONFIG_ENV_OFFSET_REDUND
203e94f6 192 if (gd->env_valid == ENV_VALID) {
e3cc5bc5
LD
193 off = CONFIG_ENV_OFFSET_REDUND;
194 off_red = CONFIG_ENV_OFFSET;
195 }
196
d3716dd6 197 env_new.flags = ENV_REDUND_ACTIVE;
e3cc5bc5
LD
198#endif
199
88cd7d0e 200 rc = eeprom_bus_write(CONFIG_SYS_I2C_EEPROM_ADDR,
e3cc5bc5
LD
201 off, (uchar *)&env_new, CONFIG_ENV_SIZE);
202
203#ifdef CONFIG_ENV_OFFSET_REDUND
204 if (rc == 0) {
88cd7d0e 205 eeprom_bus_write(CONFIG_SYS_I2C_EEPROM_ADDR,
e3cc5bc5
LD
206 off_red + offsetof(env_t, flags),
207 (uchar *)&flag_obsolete, 1);
208
203e94f6
SG
209 if (gd->env_valid == ENV_VALID)
210 gd->env_valid = ENV_REDUND;
e3cc5bc5 211 else
203e94f6 212 gd->env_valid = ENV_VALID;
e3cc5bc5
LD
213 }
214#endif
215 return rc;
216}
217
4415f1d1
SG
218U_BOOT_ENV_LOCATION(eeprom) = {
219 .location = ENVL_EEPROM,
ac358beb 220 ENV_NAME("EEPROM")
e5bce247
SG
221 .load = env_eeprom_load,
222 .save = env_save_ptr(env_eeprom_save),
4415f1d1 223};
This page took 0.546264 seconds and 4 git commands to generate.