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