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