1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2000-2010
4  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5  *
6  * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
7  * Andreas Heppel <aheppel@sysgo.de>
8  */
9 
10 #include <common.h>
11 #include <command.h>
12 #include <eeprom.h>
13 #include <env.h>
14 #include <env_internal.h>
15 #include <asm/global_data.h>
16 #include <linux/stddef.h>
17 #include <u-boot/crc.h>
18 #if defined(CONFIG_I2C_ENV_EEPROM_BUS)
19 #include <i2c.h>
20 #endif
21 #include <search.h>
22 #include <errno.h>
23 #include <linux/compiler.h>	/* for BUG_ON */
24 
25 DECLARE_GLOBAL_DATA_PTR;
26 
eeprom_bus_read(unsigned dev_addr,unsigned offset,uchar * buffer,unsigned cnt)27 static int eeprom_bus_read(unsigned dev_addr, unsigned offset,
28 			   uchar *buffer, unsigned cnt)
29 {
30 	int rcode;
31 #if defined(CONFIG_I2C_ENV_EEPROM_BUS)
32 	int old_bus = i2c_get_bus_num();
33 
34 	if (old_bus != CONFIG_I2C_ENV_EEPROM_BUS)
35 		i2c_set_bus_num(CONFIG_I2C_ENV_EEPROM_BUS);
36 #endif
37 
38 	rcode = eeprom_read(dev_addr, offset, buffer, cnt);
39 
40 #if defined(CONFIG_I2C_ENV_EEPROM_BUS)
41 	i2c_set_bus_num(old_bus);
42 #endif
43 
44 	return rcode;
45 }
46 
eeprom_bus_write(unsigned dev_addr,unsigned offset,uchar * buffer,unsigned cnt)47 static int eeprom_bus_write(unsigned dev_addr, unsigned offset,
48 			    uchar *buffer, unsigned cnt)
49 {
50 	int rcode;
51 #if defined(CONFIG_I2C_ENV_EEPROM_BUS)
52 	int old_bus = i2c_get_bus_num();
53 
54 	if (old_bus != CONFIG_I2C_ENV_EEPROM_BUS)
55 		i2c_set_bus_num(CONFIG_I2C_ENV_EEPROM_BUS);
56 #endif
57 
58 	rcode = eeprom_write(dev_addr, offset, buffer, cnt);
59 
60 #if defined(CONFIG_I2C_ENV_EEPROM_BUS)
61 	i2c_set_bus_num(old_bus);
62 #endif
63 
64 	return rcode;
65 }
66 
67 /** Call this function from overridden env_get_char_spec() if you need
68  * this functionality.
69  */
env_eeprom_get_char(int index)70 int env_eeprom_get_char(int index)
71 {
72 	uchar c;
73 	unsigned int off = CONFIG_ENV_OFFSET;
74 
75 #ifdef CONFIG_ENV_OFFSET_REDUND
76 	if (gd->env_valid == ENV_REDUND)
77 		off = CONFIG_ENV_OFFSET_REDUND;
78 #endif
79 	eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
80 			off + index + offsetof(env_t, data), &c, 1);
81 
82 	return c;
83 }
84 
env_eeprom_load(void)85 static int env_eeprom_load(void)
86 {
87 	char buf_env[CONFIG_ENV_SIZE];
88 	unsigned int off = CONFIG_ENV_OFFSET;
89 
90 #ifdef CONFIG_ENV_OFFSET_REDUND
91 	ulong len, crc[2], crc_tmp;
92 	unsigned int off_env[2];
93 	uchar rdbuf[64], flags[2];
94 	int i, crc_ok[2] = {0, 0};
95 
96 	eeprom_init(-1);	/* prepare for EEPROM read/write */
97 
98 	off_env[0] = CONFIG_ENV_OFFSET;
99 	off_env[1] = CONFIG_ENV_OFFSET_REDUND;
100 
101 	for (i = 0; i < 2; i++) {
102 		/* read CRC */
103 		eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
104 				off_env[i] + offsetof(env_t, crc),
105 				(uchar *)&crc[i], sizeof(ulong));
106 		/* read FLAGS */
107 		eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
108 				off_env[i] + offsetof(env_t, flags),
109 				(uchar *)&flags[i], sizeof(uchar));
110 
111 		crc_tmp = 0;
112 		len = ENV_SIZE;
113 		off = off_env[i] + offsetof(env_t, data);
114 		while (len > 0) {
115 			int n = (len > sizeof(rdbuf)) ? sizeof(rdbuf) : len;
116 
117 			eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR, off,
118 					rdbuf, n);
119 
120 			crc_tmp = crc32(crc_tmp, rdbuf, n);
121 			len -= n;
122 			off += n;
123 		}
124 
125 		if (crc_tmp == crc[i])
126 			crc_ok[i] = 1;
127 	}
128 
129 	if (!crc_ok[0] && !crc_ok[1]) {
130 		gd->env_addr	= 0;
131 		gd->env_valid = ENV_INVALID;
132 	} else if (crc_ok[0] && !crc_ok[1]) {
133 		gd->env_valid = ENV_VALID;
134 	} else if (!crc_ok[0] && crc_ok[1]) {
135 		gd->env_valid = ENV_REDUND;
136 	} else {
137 		/* both ok - check serial */
138 		if (flags[0] == ENV_REDUND_ACTIVE &&
139 		    flags[1] == ENV_REDUND_OBSOLETE)
140 			gd->env_valid = ENV_VALID;
141 		else if (flags[0] == ENV_REDUND_OBSOLETE &&
142 			 flags[1] == ENV_REDUND_ACTIVE)
143 			gd->env_valid = ENV_REDUND;
144 		else if (flags[0] == 0xFF && flags[1] == 0)
145 			gd->env_valid = ENV_REDUND;
146 		else if (flags[1] == 0xFF && flags[0] == 0)
147 			gd->env_valid = ENV_VALID;
148 		else /* flags are equal - almost impossible */
149 			gd->env_valid = ENV_VALID;
150 	}
151 
152 #else /* CONFIG_ENV_OFFSET_REDUND */
153 	ulong crc, len, new;
154 	uchar rdbuf[64];
155 
156 	eeprom_init(-1);	/* prepare for EEPROM read/write */
157 
158 	/* read old CRC */
159 	eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
160 			CONFIG_ENV_OFFSET + offsetof(env_t, crc),
161 			(uchar *)&crc, sizeof(ulong));
162 
163 	new = 0;
164 	len = ENV_SIZE;
165 	off = offsetof(env_t, data);
166 	while (len > 0) {
167 		int n = (len > sizeof(rdbuf)) ? sizeof(rdbuf) : len;
168 
169 		eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
170 				CONFIG_ENV_OFFSET + off, rdbuf, n);
171 		new = crc32(new, rdbuf, n);
172 		len -= n;
173 		off += n;
174 	}
175 
176 	if (crc == new) {
177 		gd->env_valid = ENV_VALID;
178 	} else {
179 		gd->env_valid = ENV_INVALID;
180 	}
181 #endif /* CONFIG_ENV_OFFSET_REDUND */
182 
183 	off = CONFIG_ENV_OFFSET;
184 #ifdef CONFIG_ENV_OFFSET_REDUND
185 	if (gd->env_valid == ENV_REDUND)
186 		off = CONFIG_ENV_OFFSET_REDUND;
187 #endif
188 
189 	eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
190 		off, (uchar *)buf_env, CONFIG_ENV_SIZE);
191 
192 	return env_import(buf_env, 1, H_EXTERNAL);
193 }
194 
env_eeprom_save(void)195 static int env_eeprom_save(void)
196 {
197 	env_t	env_new;
198 	int	rc;
199 	unsigned int off	= CONFIG_ENV_OFFSET;
200 #ifdef CONFIG_ENV_OFFSET_REDUND
201 	unsigned int off_red	= CONFIG_ENV_OFFSET_REDUND;
202 	char flag_obsolete	= ENV_REDUND_OBSOLETE;
203 #endif
204 
205 	rc = env_export(&env_new);
206 	if (rc)
207 		return rc;
208 
209 #ifdef CONFIG_ENV_OFFSET_REDUND
210 	if (gd->env_valid == ENV_VALID) {
211 		off	= CONFIG_ENV_OFFSET_REDUND;
212 		off_red	= CONFIG_ENV_OFFSET;
213 	}
214 
215 	env_new.flags = ENV_REDUND_ACTIVE;
216 #endif
217 
218 	rc = eeprom_bus_write(CONFIG_SYS_DEF_EEPROM_ADDR,
219 			      off, (uchar *)&env_new, CONFIG_ENV_SIZE);
220 
221 #ifdef CONFIG_ENV_OFFSET_REDUND
222 	if (rc == 0) {
223 		eeprom_bus_write(CONFIG_SYS_DEF_EEPROM_ADDR,
224 				 off_red + offsetof(env_t, flags),
225 				 (uchar *)&flag_obsolete, 1);
226 
227 		if (gd->env_valid == ENV_VALID)
228 			gd->env_valid = ENV_REDUND;
229 		else
230 			gd->env_valid = ENV_VALID;
231 	}
232 #endif
233 	return rc;
234 }
235 
236 U_BOOT_ENV_LOCATION(eeprom) = {
237 	.location	= ENVL_EEPROM,
238 	ENV_NAME("EEPROM")
239 	.load		= env_eeprom_load,
240 	.save		= env_save_ptr(env_eeprom_save),
241 };
242