Merge tag 'efi-2022-01-rc1-2' of https://source.denx.de/u-boot/custodians/u-boot-efi
[platform/kernel/u-boot.git] / env / flash.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 /* #define DEBUG */
11
12 #include <common.h>
13 #include <command.h>
14 #include <env.h>
15 #include <env_internal.h>
16 #include <flash.h>
17 #include <log.h>
18 #include <asm/global_data.h>
19 #include <linux/stddef.h>
20 #include <malloc.h>
21 #include <search.h>
22 #include <errno.h>
23 #include <u-boot/crc.h>
24
25 DECLARE_GLOBAL_DATA_PTR;
26
27 #ifndef CONFIG_SPL_BUILD
28 # if defined(CONFIG_CMD_SAVEENV) && defined(CONFIG_CMD_FLASH)
29 #  define CMD_SAVEENV
30 # elif defined(CONFIG_ENV_ADDR_REDUND)
31 #  error CONFIG_ENV_ADDR_REDUND must have CONFIG_CMD_SAVEENV & CONFIG_CMD_FLASH
32 # endif
33 #endif
34
35 /* TODO(sjg@chromium.org): Figure out all these special cases */
36 #if (!defined(CONFIG_MICROBLAZE) && !defined(CONFIG_ARCH_ZYNQ) && \
37         !defined(CONFIG_TARGET_MCCMON6) && !defined(CONFIG_TARGET_X600) && \
38         !defined(CONFIG_TARGET_EDMINIV2)) || \
39         !defined(CONFIG_SPL_BUILD)
40 #define LOADENV
41 #endif
42
43 #if !defined(CONFIG_TARGET_X600) || !defined(CONFIG_SPL_BUILD)
44 #define INITENV
45 #endif
46
47 #if defined(CONFIG_ENV_ADDR_REDUND) && defined(CMD_SAVEENV) || \
48         !defined(CONFIG_ENV_ADDR_REDUND) && defined(INITENV)
49 #ifdef ENV_IS_EMBEDDED
50 static env_t *env_ptr = &embedded_environment;
51 #else /* ! ENV_IS_EMBEDDED */
52
53 static env_t *env_ptr = (env_t *)CONFIG_ENV_ADDR;
54 #endif /* ENV_IS_EMBEDDED */
55 #endif
56 static __maybe_unused env_t *flash_addr = (env_t *)CONFIG_ENV_ADDR;
57
58 /* CONFIG_ENV_ADDR is supposed to be on sector boundary */
59 static ulong __maybe_unused end_addr =
60                 CONFIG_ENV_ADDR + CONFIG_ENV_SECT_SIZE - 1;
61
62 #ifdef CONFIG_ENV_ADDR_REDUND
63
64 static env_t __maybe_unused *flash_addr_new = (env_t *)CONFIG_ENV_ADDR_REDUND;
65
66 /* CONFIG_ENV_ADDR_REDUND is supposed to be on sector boundary */
67 static ulong __maybe_unused end_addr_new =
68                 CONFIG_ENV_ADDR_REDUND + CONFIG_ENV_SECT_SIZE - 1;
69 #endif /* CONFIG_ENV_ADDR_REDUND */
70
71 #ifdef CONFIG_ENV_ADDR_REDUND
72 #ifdef INITENV
73 static int env_flash_init(void)
74 {
75         int crc1_ok = 0, crc2_ok = 0;
76
77         uchar flag1 = flash_addr->flags;
78         uchar flag2 = flash_addr_new->flags;
79
80         ulong addr_default = (ulong)&default_environment[0];
81         ulong addr1 = (ulong)&(flash_addr->data);
82         ulong addr2 = (ulong)&(flash_addr_new->data);
83
84         crc1_ok = crc32(0, flash_addr->data, ENV_SIZE) == flash_addr->crc;
85         crc2_ok =
86                 crc32(0, flash_addr_new->data, ENV_SIZE) == flash_addr_new->crc;
87
88         if (crc1_ok && !crc2_ok) {
89                 gd->env_addr    = addr1;
90                 gd->env_valid   = ENV_VALID;
91         } else if (!crc1_ok && crc2_ok) {
92                 gd->env_addr    = addr2;
93                 gd->env_valid   = ENV_VALID;
94         } else if (!crc1_ok && !crc2_ok) {
95                 gd->env_addr    = addr_default;
96                 gd->env_valid   = ENV_INVALID;
97         } else if (flag1 == ENV_REDUND_ACTIVE &&
98                    flag2 == ENV_REDUND_OBSOLETE) {
99                 gd->env_addr    = addr1;
100                 gd->env_valid   = ENV_VALID;
101         } else if (flag1 == ENV_REDUND_OBSOLETE &&
102                    flag2 == ENV_REDUND_ACTIVE) {
103                 gd->env_addr    = addr2;
104                 gd->env_valid   = ENV_VALID;
105         } else if (flag1 == flag2) {
106                 gd->env_addr    = addr1;
107                 gd->env_valid   = ENV_REDUND;
108         } else if (flag1 == 0xFF) {
109                 gd->env_addr    = addr1;
110                 gd->env_valid   = ENV_REDUND;
111         } else if (flag2 == 0xFF) {
112                 gd->env_addr    = addr2;
113                 gd->env_valid   = ENV_REDUND;
114         }
115
116         return 0;
117 }
118 #endif
119
120 #ifdef CMD_SAVEENV
121 static int env_flash_save(void)
122 {
123         env_t   env_new;
124         char    *saved_data = NULL;
125         char    flag = ENV_REDUND_OBSOLETE, new_flag = ENV_REDUND_ACTIVE;
126         int     rc = 1;
127 #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
128         ulong   up_data = 0;
129 #endif
130
131         debug("Protect off %08lX ... %08lX\n", (ulong)flash_addr, end_addr);
132
133         if (flash_sect_protect(0, (ulong)flash_addr, end_addr))
134                 goto done;
135
136         debug("Protect off %08lX ... %08lX\n",
137                 (ulong)flash_addr_new, end_addr_new);
138
139         if (flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new))
140                 goto done;
141
142         rc = env_export(&env_new);
143         if (rc)
144                 return rc;
145         env_new.flags   = new_flag;
146
147 #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
148         up_data = end_addr_new + 1 - ((long)flash_addr_new + CONFIG_ENV_SIZE);
149         debug("Data to save 0x%lX\n", up_data);
150         if (up_data) {
151                 saved_data = malloc(up_data);
152                 if (saved_data == NULL) {
153                         printf("Unable to save the rest of sector (%ld)\n",
154                                 up_data);
155                         goto done;
156                 }
157                 memcpy(saved_data,
158                         (void *)((long)flash_addr_new + CONFIG_ENV_SIZE),
159                         up_data);
160                 debug("Data (start 0x%lX, len 0x%lX) saved at 0x%p\n",
161                         (long)flash_addr_new + CONFIG_ENV_SIZE,
162                         up_data, saved_data);
163         }
164 #endif
165         puts("Erasing Flash...");
166         debug(" %08lX ... %08lX ...", (ulong)flash_addr_new, end_addr_new);
167
168         if (flash_sect_erase((ulong)flash_addr_new, end_addr_new))
169                 goto done;
170
171         puts("Writing to Flash... ");
172         debug(" %08lX ... %08lX ...",
173                 (ulong)&(flash_addr_new->data),
174                 sizeof(env_ptr->data) + (ulong)&(flash_addr_new->data));
175         rc = flash_write((char *)&env_new, (ulong)flash_addr_new,
176                          sizeof(env_new));
177         if (rc)
178                 goto perror;
179
180         rc = flash_write(&flag, (ulong)&(flash_addr->flags),
181                          sizeof(flash_addr->flags));
182         if (rc)
183                 goto perror;
184
185 #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
186         if (up_data) { /* restore the rest of sector */
187                 debug("Restoring the rest of data to 0x%lX len 0x%lX\n",
188                         (long)flash_addr_new + CONFIG_ENV_SIZE, up_data);
189                 if (flash_write(saved_data,
190                                 (long)flash_addr_new + CONFIG_ENV_SIZE,
191                                 up_data))
192                         goto perror;
193         }
194 #endif
195         puts("done\n");
196
197         {
198                 env_t *etmp = flash_addr;
199                 ulong ltmp = end_addr;
200
201                 flash_addr = flash_addr_new;
202                 flash_addr_new = etmp;
203
204                 end_addr = end_addr_new;
205                 end_addr_new = ltmp;
206         }
207
208         rc = 0;
209         goto done;
210 perror:
211         flash_perror(rc);
212 done:
213         free(saved_data);
214         /* try to re-protect */
215         flash_sect_protect(1, (ulong)flash_addr, end_addr);
216         flash_sect_protect(1, (ulong)flash_addr_new, end_addr_new);
217
218         return rc;
219 }
220 #endif /* CMD_SAVEENV */
221
222 #else /* ! CONFIG_ENV_ADDR_REDUND */
223
224 #ifdef INITENV
225 static int env_flash_init(void)
226 {
227         if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
228                 gd->env_addr    = (ulong)&(env_ptr->data);
229                 gd->env_valid   = ENV_VALID;
230                 return 0;
231         }
232
233         gd->env_addr    = (ulong)&default_environment[0];
234         gd->env_valid   = ENV_INVALID;
235         return 0;
236 }
237 #endif
238
239 #ifdef CMD_SAVEENV
240 static int env_flash_save(void)
241 {
242         env_t   env_new;
243         int     rc = 1;
244         char    *saved_data = NULL;
245 #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
246         ulong   up_data = 0;
247
248         up_data = end_addr + 1 - ((long)flash_addr + CONFIG_ENV_SIZE);
249         debug("Data to save 0x%lx\n", up_data);
250         if (up_data) {
251                 saved_data = malloc(up_data);
252                 if (saved_data == NULL) {
253                         printf("Unable to save the rest of sector (%ld)\n",
254                                 up_data);
255                         goto done;
256                 }
257                 memcpy(saved_data,
258                         (void *)((long)flash_addr + CONFIG_ENV_SIZE), up_data);
259                 debug("Data (start 0x%lx, len 0x%lx) saved at 0x%lx\n",
260                         (ulong)flash_addr + CONFIG_ENV_SIZE,
261                         up_data,
262                         (ulong)saved_data);
263         }
264 #endif  /* CONFIG_ENV_SECT_SIZE */
265
266         debug("Protect off %08lX ... %08lX\n", (ulong)flash_addr, end_addr);
267
268         if (flash_sect_protect(0, (long)flash_addr, end_addr))
269                 goto done;
270
271         rc = env_export(&env_new);
272         if (rc)
273                 goto done;
274
275         puts("Erasing Flash...");
276         if (flash_sect_erase((long)flash_addr, end_addr))
277                 goto done;
278
279         puts("Writing to Flash... ");
280         rc = flash_write((char *)&env_new, (long)flash_addr, CONFIG_ENV_SIZE);
281         if (rc != 0)
282                 goto perror;
283
284 #if CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE
285         if (up_data) {  /* restore the rest of sector */
286                 debug("Restoring the rest of data to 0x%lx len 0x%lx\n",
287                         (ulong)flash_addr + CONFIG_ENV_SIZE, up_data);
288                 if (flash_write(saved_data,
289                                 (long)flash_addr + CONFIG_ENV_SIZE,
290                                 up_data))
291                         goto perror;
292         }
293 #endif
294         puts("done\n");
295         rc = 0;
296         goto done;
297 perror:
298         flash_perror(rc);
299 done:
300         free(saved_data);
301         /* try to re-protect */
302         flash_sect_protect(1, (long)flash_addr, end_addr);
303         return rc;
304 }
305 #endif /* CMD_SAVEENV */
306
307 #endif /* CONFIG_ENV_ADDR_REDUND */
308
309 #ifdef LOADENV
310 static int env_flash_load(void)
311 {
312 #ifdef CONFIG_ENV_ADDR_REDUND
313         if (gd->env_addr != (ulong)&(flash_addr->data)) {
314                 env_t *etmp = flash_addr;
315                 ulong ltmp = end_addr;
316
317                 flash_addr = flash_addr_new;
318                 flash_addr_new = etmp;
319
320                 end_addr = end_addr_new;
321                 end_addr_new = ltmp;
322         }
323
324         if (flash_addr_new->flags != ENV_REDUND_OBSOLETE &&
325             crc32(0, flash_addr_new->data, ENV_SIZE) == flash_addr_new->crc) {
326                 char flag = ENV_REDUND_OBSOLETE;
327
328                 gd->env_valid = ENV_REDUND;
329                 flash_sect_protect(0, (ulong)flash_addr_new, end_addr_new);
330                 flash_write(&flag,
331                             (ulong)&(flash_addr_new->flags),
332                             sizeof(flash_addr_new->flags));
333                 flash_sect_protect(1, (ulong)flash_addr_new, end_addr_new);
334         }
335
336         if (flash_addr->flags != ENV_REDUND_ACTIVE &&
337             (flash_addr->flags & ENV_REDUND_ACTIVE) == ENV_REDUND_ACTIVE) {
338                 char flag = ENV_REDUND_ACTIVE;
339
340                 gd->env_valid = ENV_REDUND;
341                 flash_sect_protect(0, (ulong)flash_addr, end_addr);
342                 flash_write(&flag,
343                             (ulong)&(flash_addr->flags),
344                             sizeof(flash_addr->flags));
345                 flash_sect_protect(1, (ulong)flash_addr, end_addr);
346         }
347
348         if (gd->env_valid == ENV_REDUND)
349                 puts("*** Warning - some problems detected "
350                      "reading environment; recovered successfully\n\n");
351 #endif /* CONFIG_ENV_ADDR_REDUND */
352
353         return env_import((char *)flash_addr, 1, H_EXTERNAL);
354 }
355 #endif /* LOADENV */
356
357 U_BOOT_ENV_LOCATION(flash) = {
358         .location       = ENVL_FLASH,
359         ENV_NAME("Flash")
360 #ifdef LOADENV
361         .load           = env_flash_load,
362 #endif
363 #ifdef CMD_SAVEENV
364         .save           = env_save_ptr(env_flash_save),
365 #endif
366 #ifdef INITENV
367         .init           = env_flash_init,
368 #endif
369 };