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