cyclic: use a flag in gd->flags for recursion protection
[platform/kernel/u-boot.git] / env / eeprom.c
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
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
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 static int env_eeprom_load(void)
68 {
69         char buf_env[CONFIG_ENV_SIZE];
70         unsigned int off = CONFIG_ENV_OFFSET;
71
72 #ifdef CONFIG_ENV_OFFSET_REDUND
73         ulong len, crc[2], crc_tmp;
74         unsigned int off_env[2];
75         uchar rdbuf[64], flags[2];
76         int i, crc_ok[2] = {0, 0};
77
78         eeprom_init(-1);        /* prepare for EEPROM read/write */
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 */
85                 eeprom_bus_read(CONFIG_SYS_I2C_EEPROM_ADDR,
86                                 off_env[i] + offsetof(env_t, crc),
87                                 (uchar *)&crc[i], sizeof(ulong));
88                 /* read FLAGS */
89                 eeprom_bus_read(CONFIG_SYS_I2C_EEPROM_ADDR,
90                                 off_env[i] + offsetof(env_t, flags),
91                                 (uchar *)&flags[i], sizeof(uchar));
92
93                 crc_tmp = 0;
94                 len = ENV_SIZE;
95                 off = off_env[i] + offsetof(env_t, data);
96                 while (len > 0) {
97                         int n = (len > sizeof(rdbuf)) ? sizeof(rdbuf) : len;
98
99                         eeprom_bus_read(CONFIG_SYS_I2C_EEPROM_ADDR, off,
100                                         rdbuf, n);
101
102                         crc_tmp = crc32(crc_tmp, rdbuf, n);
103                         len -= n;
104                         off += n;
105                 }
106
107                 if (crc_tmp == crc[i])
108                         crc_ok[i] = 1;
109         }
110
111         if (!crc_ok[0] && !crc_ok[1]) {
112                 gd->env_addr    = 0;
113                 gd->env_valid = ENV_INVALID;
114         } else if (crc_ok[0] && !crc_ok[1]) {
115                 gd->env_valid = ENV_VALID;
116         } else if (!crc_ok[0] && crc_ok[1]) {
117                 gd->env_valid = ENV_REDUND;
118         } else {
119                 /* both ok - check serial */
120                 if (flags[0] == ENV_REDUND_ACTIVE &&
121                     flags[1] == ENV_REDUND_OBSOLETE)
122                         gd->env_valid = ENV_VALID;
123                 else if (flags[0] == ENV_REDUND_OBSOLETE &&
124                          flags[1] == ENV_REDUND_ACTIVE)
125                         gd->env_valid = ENV_REDUND;
126                 else if (flags[0] == 0xFF && flags[1] == 0)
127                         gd->env_valid = ENV_REDUND;
128                 else if (flags[1] == 0xFF && flags[0] == 0)
129                         gd->env_valid = ENV_VALID;
130                 else /* flags are equal - almost impossible */
131                         gd->env_valid = ENV_VALID;
132         }
133
134 #else /* CONFIG_ENV_OFFSET_REDUND */
135         ulong crc, len, new;
136         uchar rdbuf[64];
137
138         eeprom_init(-1);        /* prepare for EEPROM read/write */
139
140         /* read old CRC */
141         eeprom_bus_read(CONFIG_SYS_I2C_EEPROM_ADDR,
142                         CONFIG_ENV_OFFSET + offsetof(env_t, crc),
143                         (uchar *)&crc, sizeof(ulong));
144
145         new = 0;
146         len = ENV_SIZE;
147         off = offsetof(env_t, data);
148         while (len > 0) {
149                 int n = (len > sizeof(rdbuf)) ? sizeof(rdbuf) : len;
150
151                 eeprom_bus_read(CONFIG_SYS_I2C_EEPROM_ADDR,
152                                 CONFIG_ENV_OFFSET + off, rdbuf, n);
153                 new = crc32(new, rdbuf, n);
154                 len -= n;
155                 off += n;
156         }
157
158         if (crc == new) {
159                 gd->env_valid = ENV_VALID;
160         } else {
161                 gd->env_valid = ENV_INVALID;
162         }
163 #endif /* CONFIG_ENV_OFFSET_REDUND */
164
165         off = CONFIG_ENV_OFFSET;
166 #ifdef CONFIG_ENV_OFFSET_REDUND
167         if (gd->env_valid == ENV_REDUND)
168                 off = CONFIG_ENV_OFFSET_REDUND;
169 #endif
170
171         eeprom_bus_read(CONFIG_SYS_I2C_EEPROM_ADDR,
172                 off, (uchar *)buf_env, CONFIG_ENV_SIZE);
173
174         return env_import(buf_env, 1, H_EXTERNAL);
175 }
176
177 static int env_eeprom_save(void)
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;
184         char flag_obsolete      = ENV_REDUND_OBSOLETE;
185 #endif
186
187         rc = env_export(&env_new);
188         if (rc)
189                 return rc;
190
191 #ifdef CONFIG_ENV_OFFSET_REDUND
192         if (gd->env_valid == ENV_VALID) {
193                 off     = CONFIG_ENV_OFFSET_REDUND;
194                 off_red = CONFIG_ENV_OFFSET;
195         }
196
197         env_new.flags = ENV_REDUND_ACTIVE;
198 #endif
199
200         rc = eeprom_bus_write(CONFIG_SYS_I2C_EEPROM_ADDR,
201                               off, (uchar *)&env_new, CONFIG_ENV_SIZE);
202
203 #ifdef CONFIG_ENV_OFFSET_REDUND
204         if (rc == 0) {
205                 eeprom_bus_write(CONFIG_SYS_I2C_EEPROM_ADDR,
206                                  off_red + offsetof(env_t, flags),
207                                  (uchar *)&flag_obsolete, 1);
208
209                 if (gd->env_valid == ENV_VALID)
210                         gd->env_valid = ENV_REDUND;
211                 else
212                         gd->env_valid = ENV_VALID;
213         }
214 #endif
215         return rc;
216 }
217
218 U_BOOT_ENV_LOCATION(eeprom) = {
219         .location       = ENVL_EEPROM,
220         ENV_NAME("EEPROM")
221         .load           = env_eeprom_load,
222         .save           = env_save_ptr(env_eeprom_save),
223 };