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