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