env: sf: add support of command env erase
[platform/kernel/u-boot.git] / env / sf.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  * (C) Copyright 2008 Atmel Corporation
10  */
11 #include <common.h>
12 #include <dm.h>
13 #include <env.h>
14 #include <env_internal.h>
15 #include <flash.h>
16 #include <malloc.h>
17 #include <spi.h>
18 #include <spi_flash.h>
19 #include <search.h>
20 #include <errno.h>
21 #include <uuid.h>
22 #include <asm/cache.h>
23 #include <asm/global_data.h>
24 #include <dm/device-internal.h>
25 #include <u-boot/crc.h>
26
27 #ifndef CONFIG_SPL_BUILD
28 #define INITENV
29 #endif
30
31 #define OFFSET_INVALID          (~(u32)0)
32
33 #ifdef CONFIG_ENV_OFFSET_REDUND
34 #define ENV_OFFSET_REDUND       CONFIG_ENV_OFFSET_REDUND
35
36 static ulong env_offset         = CONFIG_ENV_OFFSET;
37 static ulong env_new_offset     = CONFIG_ENV_OFFSET_REDUND;
38
39 #else
40
41 #define ENV_OFFSET_REDUND       OFFSET_INVALID
42
43 #endif /* CONFIG_ENV_OFFSET_REDUND */
44
45 DECLARE_GLOBAL_DATA_PTR;
46
47 static struct spi_flash *env_flash;
48
49 static int setup_flash_device(void)
50 {
51 #if CONFIG_IS_ENABLED(DM_SPI_FLASH)
52         struct udevice *new;
53         int     ret;
54
55         /* speed and mode will be read from DT */
56         ret = spi_flash_probe_bus_cs(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
57                                      CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE,
58                                      &new);
59         if (ret) {
60                 env_set_default("spi_flash_probe_bus_cs() failed", 0);
61                 return ret;
62         }
63
64         env_flash = dev_get_uclass_priv(new);
65 #else
66         if (env_flash)
67                 spi_flash_free(env_flash);
68
69         env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
70                                     CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
71         if (!env_flash) {
72                 env_set_default("spi_flash_probe() failed", 0);
73                 return -EIO;
74         }
75 #endif
76         return 0;
77 }
78
79 #if defined(CONFIG_ENV_OFFSET_REDUND)
80 static int env_sf_save(void)
81 {
82         env_t   env_new;
83         char    *saved_buffer = NULL, flag = ENV_REDUND_OBSOLETE;
84         u32     saved_size = 0, saved_offset = 0, sector;
85         u32     sect_size = CONFIG_ENV_SECT_SIZE;
86         int     ret;
87
88         ret = setup_flash_device();
89         if (ret)
90                 return ret;
91
92         if (IS_ENABLED(CONFIG_ENV_SECT_SIZE_AUTO))
93                 sect_size = env_flash->mtd.erasesize;
94
95         ret = env_export(&env_new);
96         if (ret)
97                 return -EIO;
98         env_new.flags   = ENV_REDUND_ACTIVE;
99
100         if (gd->env_valid == ENV_VALID) {
101                 env_new_offset = CONFIG_ENV_OFFSET_REDUND;
102                 env_offset = CONFIG_ENV_OFFSET;
103         } else {
104                 env_new_offset = CONFIG_ENV_OFFSET;
105                 env_offset = CONFIG_ENV_OFFSET_REDUND;
106         }
107
108         /* Is the sector larger than the env (i.e. embedded) */
109         if (sect_size > CONFIG_ENV_SIZE) {
110                 saved_size = sect_size - CONFIG_ENV_SIZE;
111                 saved_offset = env_new_offset + CONFIG_ENV_SIZE;
112                 saved_buffer = memalign(ARCH_DMA_MINALIGN, saved_size);
113                 if (!saved_buffer) {
114                         ret = -ENOMEM;
115                         goto done;
116                 }
117                 ret = spi_flash_read(env_flash, saved_offset,
118                                         saved_size, saved_buffer);
119                 if (ret)
120                         goto done;
121         }
122
123         sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, sect_size);
124
125         puts("Erasing SPI flash...");
126         ret = spi_flash_erase(env_flash, env_new_offset,
127                                 sector * sect_size);
128         if (ret)
129                 goto done;
130
131         puts("Writing to SPI flash...");
132
133         ret = spi_flash_write(env_flash, env_new_offset,
134                 CONFIG_ENV_SIZE, &env_new);
135         if (ret)
136                 goto done;
137
138         if (sect_size > CONFIG_ENV_SIZE) {
139                 ret = spi_flash_write(env_flash, saved_offset,
140                                         saved_size, saved_buffer);
141                 if (ret)
142                         goto done;
143         }
144
145         ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags),
146                                 sizeof(env_new.flags), &flag);
147         if (ret)
148                 goto done;
149
150         puts("done\n");
151
152         gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
153
154         printf("Valid environment: %d\n", (int)gd->env_valid);
155
156 done:
157         if (saved_buffer)
158                 free(saved_buffer);
159
160         return ret;
161 }
162
163 static int env_sf_load(void)
164 {
165         int ret;
166         int read1_fail, read2_fail;
167         env_t *tmp_env1, *tmp_env2;
168
169         tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN,
170                         CONFIG_ENV_SIZE);
171         tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN,
172                         CONFIG_ENV_SIZE);
173         if (!tmp_env1 || !tmp_env2) {
174                 env_set_default("malloc() failed", 0);
175                 ret = -EIO;
176                 goto out;
177         }
178
179         ret = setup_flash_device();
180         if (ret)
181                 goto out;
182
183         read1_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
184                                     CONFIG_ENV_SIZE, tmp_env1);
185         read2_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND,
186                                     CONFIG_ENV_SIZE, tmp_env2);
187
188         ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
189                                 read2_fail, H_EXTERNAL);
190
191         spi_flash_free(env_flash);
192         env_flash = NULL;
193 out:
194         free(tmp_env1);
195         free(tmp_env2);
196
197         return ret;
198 }
199 #else
200 static int env_sf_save(void)
201 {
202         u32     saved_size = 0, saved_offset = 0, sector;
203         u32     sect_size = CONFIG_ENV_SECT_SIZE;
204         char    *saved_buffer = NULL;
205         int     ret = 1;
206         env_t   env_new;
207
208         ret = setup_flash_device();
209         if (ret)
210                 return ret;
211
212         if (IS_ENABLED(CONFIG_ENV_SECT_SIZE_AUTO))
213                 sect_size = env_flash->mtd.erasesize;
214
215         /* Is the sector larger than the env (i.e. embedded) */
216         if (sect_size > CONFIG_ENV_SIZE) {
217                 saved_size = sect_size - CONFIG_ENV_SIZE;
218                 saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
219                 saved_buffer = malloc(saved_size);
220                 if (!saved_buffer)
221                         goto done;
222
223                 ret = spi_flash_read(env_flash, saved_offset,
224                         saved_size, saved_buffer);
225                 if (ret)
226                         goto done;
227         }
228
229         ret = env_export(&env_new);
230         if (ret)
231                 goto done;
232
233         sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, sect_size);
234
235         puts("Erasing SPI flash...");
236         ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
237                 sector * sect_size);
238         if (ret)
239                 goto done;
240
241         puts("Writing to SPI flash...");
242         ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
243                 CONFIG_ENV_SIZE, &env_new);
244         if (ret)
245                 goto done;
246
247         if (sect_size > CONFIG_ENV_SIZE) {
248                 ret = spi_flash_write(env_flash, saved_offset,
249                         saved_size, saved_buffer);
250                 if (ret)
251                         goto done;
252         }
253
254         ret = 0;
255         puts("done\n");
256
257 done:
258         if (saved_buffer)
259                 free(saved_buffer);
260
261         return ret;
262 }
263
264 static int env_sf_load(void)
265 {
266         int ret;
267         char *buf = NULL;
268
269         buf = (char *)memalign(ARCH_DMA_MINALIGN, CONFIG_ENV_SIZE);
270         if (!buf) {
271                 env_set_default("malloc() failed", 0);
272                 return -EIO;
273         }
274
275         ret = setup_flash_device();
276         if (ret)
277                 goto out;
278
279         ret = spi_flash_read(env_flash,
280                 CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
281         if (ret) {
282                 env_set_default("spi_flash_read() failed", 0);
283                 goto err_read;
284         }
285
286         ret = env_import(buf, 1, H_EXTERNAL);
287         if (!ret)
288                 gd->env_valid = ENV_VALID;
289
290 err_read:
291         spi_flash_free(env_flash);
292         env_flash = NULL;
293 out:
294         free(buf);
295
296         return ret;
297 }
298 #endif
299
300 static int env_sf_erase(void)
301 {
302         int ret;
303         env_t env;
304
305         ret = setup_flash_device();
306         if (ret)
307                 return ret;
308
309         memset(&env, 0, sizeof(env_t));
310         ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, &env);
311         if (ret)
312                 goto done;
313
314         if (ENV_OFFSET_REDUND != OFFSET_INVALID)
315                 ret = spi_flash_write(env_flash, ENV_OFFSET_REDUND, CONFIG_ENV_SIZE, &env);
316
317 done:
318         spi_flash_free(env_flash);
319         env_flash = NULL;
320
321         return ret;
322 }
323
324 #if CONFIG_ENV_ADDR != 0x0
325 __weak void *env_sf_get_env_addr(void)
326 {
327         return (void *)CONFIG_ENV_ADDR;
328 }
329 #endif
330
331 #if defined(INITENV) && (CONFIG_ENV_ADDR != 0x0)
332 /*
333  * check if Environment on CONFIG_ENV_ADDR is valid.
334  */
335 static int env_sf_init_addr(void)
336 {
337         env_t *env_ptr = (env_t *)env_sf_get_env_addr();
338
339         if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
340                 gd->env_addr    = (ulong)&(env_ptr->data);
341                 gd->env_valid   = 1;
342         } else {
343                 gd->env_addr = (ulong)&default_environment[0];
344                 gd->env_valid = 1;
345         }
346
347         return 0;
348 }
349 #endif
350
351 #if defined(CONFIG_ENV_SPI_EARLY)
352 /*
353  * early load environment from SPI flash (before relocation)
354  * and check if it is valid.
355  */
356 static int env_sf_init_early(void)
357 {
358         int ret;
359         int read1_fail;
360         int read2_fail;
361         int crc1_ok;
362         env_t *tmp_env2 = NULL;
363         env_t *tmp_env1;
364
365         /*
366          * if malloc is not ready yet, we cannot use
367          * this part yet.
368          */
369         if (!gd->malloc_limit)
370                 return -ENOENT;
371
372         tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN,
373                         CONFIG_ENV_SIZE);
374         if (IS_ENABLED(CONFIG_SYS_REDUNDAND_ENVIRONMENT))
375                 tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN,
376                                              CONFIG_ENV_SIZE);
377
378         if (!tmp_env1 || !tmp_env2)
379                 goto out;
380
381         ret = setup_flash_device();
382         if (ret)
383                 goto out;
384
385         read1_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
386                                     CONFIG_ENV_SIZE, tmp_env1);
387
388         if (IS_ENABLED(CONFIG_SYS_REDUNDAND_ENVIRONMENT)) {
389                 read2_fail = spi_flash_read(env_flash,
390                                             CONFIG_ENV_OFFSET_REDUND,
391                                             CONFIG_ENV_SIZE, tmp_env2);
392                 ret = env_check_redund((char *)tmp_env1, read1_fail,
393                                        (char *)tmp_env2, read2_fail);
394
395                 if (ret < 0)
396                         goto err_read;
397
398                 if (gd->env_valid == ENV_VALID)
399                         gd->env_addr = (unsigned long)&tmp_env1->data;
400                 else
401                         gd->env_addr = (unsigned long)&tmp_env2->data;
402         } else {
403                 if (read1_fail)
404                         goto err_read;
405
406                 crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) ==
407                                 tmp_env1->crc;
408                 if (!crc1_ok)
409                         goto err_read;
410
411                 /* if valid -> this is our env */
412                 gd->env_valid = ENV_VALID;
413                 gd->env_addr = (unsigned long)&tmp_env1->data;
414         }
415
416         return 0;
417 err_read:
418         spi_flash_free(env_flash);
419         env_flash = NULL;
420         free(tmp_env1);
421         if (IS_ENABLED(CONFIG_SYS_REDUNDAND_ENVIRONMENT))
422                 free(tmp_env2);
423 out:
424         /* env is not valid. always return 0 */
425         gd->env_valid = ENV_INVALID;
426         return 0;
427 }
428 #endif
429
430 static int env_sf_init(void)
431 {
432 #if defined(INITENV) && (CONFIG_ENV_ADDR != 0x0)
433         return env_sf_init_addr();
434 #elif defined(CONFIG_ENV_SPI_EARLY)
435         return env_sf_init_early();
436 #endif
437         /*
438          * return here -ENOENT, so env_init()
439          * can set the init bit and later if no
440          * other Environment storage is defined
441          * can set the default environment
442          */
443         return -ENOENT;
444 }
445
446 U_BOOT_ENV_LOCATION(sf) = {
447         .location       = ENVL_SPI_FLASH,
448         ENV_NAME("SPIFlash")
449         .load           = env_sf_load,
450         .save           = ENV_SAVE_PTR(env_sf_save),
451         .erase          = ENV_ERASE_PTR(env_sf_erase),
452         .init           = env_sf_init,
453 };