blob: b3dec82c353afea75ef183145c689b34560445e2 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Haavard Skinnemoen8c664972008-05-16 11:10:35 +02002/*
Wolfgang Denkea882ba2010-06-20 23:33:59 +02003 * (C) Copyright 2000-2010
Haavard Skinnemoen8c664972008-05-16 11:10:35 +02004 * 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
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020010 */
11#include <common.h>
Simon Glass9d922452017-05-17 17:18:03 -060012#include <dm.h>
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020013#include <environment.h>
Mike Frysinger5b3375a2008-12-11 06:23:37 -050014#include <malloc.h>
Simon Glass843c9e82014-10-13 23:41:55 -060015#include <spi.h>
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020016#include <spi_flash.h>
Wolfgang Denkea882ba2010-06-20 23:33:59 +020017#include <search.h>
18#include <errno.h>
Gong Qianyu19c31282016-01-26 15:06:42 +080019#include <dm/device-internal.h>
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020020
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020021#ifndef CONFIG_ENV_SPI_BUS
Jean-Jacques Hiblot2e4e5ad2017-02-01 14:51:47 +010022# define CONFIG_ENV_SPI_BUS CONFIG_SF_DEFAULT_BUS
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020023#endif
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020024#ifndef CONFIG_ENV_SPI_CS
Jean-Jacques Hiblot2e4e5ad2017-02-01 14:51:47 +010025# define CONFIG_ENV_SPI_CS CONFIG_SF_DEFAULT_CS
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020026#endif
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020027#ifndef CONFIG_ENV_SPI_MAX_HZ
Jean-Jacques Hiblot2e4e5ad2017-02-01 14:51:47 +010028# define CONFIG_ENV_SPI_MAX_HZ CONFIG_SF_DEFAULT_SPEED
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020029#endif
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020030#ifndef CONFIG_ENV_SPI_MODE
Jean-Jacques Hiblot2e4e5ad2017-02-01 14:51:47 +010031# define CONFIG_ENV_SPI_MODE CONFIG_SF_DEFAULT_MODE
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020032#endif
33
Simon Glass4415f1d2017-08-03 12:21:58 -060034#ifndef CONFIG_SPL_BUILD
35#define CMD_SAVEENV
Ashish Kumarb500c922017-12-14 17:37:08 +053036#define INITENV
Simon Glass4415f1d2017-08-03 12:21:58 -060037#endif
38
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020039#ifdef CONFIG_ENV_OFFSET_REDUND
Simon Glass4415f1d2017-08-03 12:21:58 -060040#ifdef CMD_SAVEENV
Igor Grinbergeb58a7f2011-11-07 01:14:08 +000041static ulong env_offset = CONFIG_ENV_OFFSET;
42static ulong env_new_offset = CONFIG_ENV_OFFSET_REDUND;
Simon Glass4415f1d2017-08-03 12:21:58 -060043#endif
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020044
Igor Grinbergeb58a7f2011-11-07 01:14:08 +000045#define ACTIVE_FLAG 1
46#define OBSOLETE_FLAG 0
Stefano Babica3110f02010-10-27 11:06:20 +020047#endif /* CONFIG_ENV_OFFSET_REDUND */
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020048
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020049DECLARE_GLOBAL_DATA_PTR;
50
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020051static struct spi_flash *env_flash;
52
Andreas Fenkartafa81a72017-04-08 11:59:31 +020053static int setup_flash_device(void)
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020054{
Gong Qianyu19c31282016-01-26 15:06:42 +080055#ifdef CONFIG_DM_SPI_FLASH
56 struct udevice *new;
Andreas Fenkartafa81a72017-04-08 11:59:31 +020057 int ret;
Gong Qianyu19c31282016-01-26 15:06:42 +080058
Vignesh R96907c02016-07-06 10:04:28 +053059 /* speed and mode will be read from DT */
Gong Qianyu19c31282016-01-26 15:06:42 +080060 ret = spi_flash_probe_bus_cs(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
Konstantin Porotchkin25a17652018-08-29 16:34:52 +030061 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE,
62 &new);
Gong Qianyu19c31282016-01-26 15:06:42 +080063 if (ret) {
Yaniv Levinskyc5d548a2018-06-24 19:16:57 +030064 set_default_env("spi_flash_probe_bus_cs() failed", 0);
Simon Glassc5951992017-08-03 12:22:17 -060065 return ret;
Gong Qianyu19c31282016-01-26 15:06:42 +080066 }
67
68 env_flash = dev_get_uclass_priv(new);
69#else
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020070
71 if (!env_flash) {
Stefano Babica3110f02010-10-27 11:06:20 +020072 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS,
73 CONFIG_ENV_SPI_CS,
74 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
75 if (!env_flash) {
Yaniv Levinskyc5d548a2018-06-24 19:16:57 +030076 set_default_env("spi_flash_probe() failed", 0);
Simon Glassc5951992017-08-03 12:22:17 -060077 return -EIO;
Stefano Babica3110f02010-10-27 11:06:20 +020078 }
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020079 }
Gong Qianyu19c31282016-01-26 15:06:42 +080080#endif
Andreas Fenkartafa81a72017-04-08 11:59:31 +020081 return 0;
82}
83
Horatiu Vultur9a9d66f2018-12-11 10:13:56 +010084static int is_end(const char *addr, size_t size)
85{
86 /* The end of env variables is marked by '\0\0' */
87 int i = 0;
88
89 for (i = 0; i < size - 1; ++i)
90 if (addr[i] == 0x0 && addr[i + 1] == 0x0)
91 return 1;
92 return 0;
93}
94
95static int spi_flash_read_env(struct spi_flash *flash, u32 offset, size_t len,
96 void *buf)
97{
98 u32 addr = 0;
99 u32 page_size = flash->page_size;
100
Tom Rini82eeb712019-01-21 11:57:58 -0500101 memset(buf, 0x0, len);
Horatiu Vultur9a9d66f2018-12-11 10:13:56 +0100102 for (int i = 0; i < len / page_size; ++i) {
103 int ret = spi_flash_read(flash, offset, page_size,
104 &((char *)buf)[addr]);
105
106 if (ret < 0)
107 return ret;
108
109 if (is_end(&((char *)buf)[addr], page_size))
110 return 0;
111
112 addr += page_size;
113 offset += page_size;
114 }
115 return 0;
116}
117
Andreas Fenkartafa81a72017-04-08 11:59:31 +0200118#if defined(CONFIG_ENV_OFFSET_REDUND)
Simon Glass4415f1d2017-08-03 12:21:58 -0600119#ifdef CMD_SAVEENV
Simon Glasse5bce242017-08-03 12:22:01 -0600120static int env_sf_save(void)
Andreas Fenkartafa81a72017-04-08 11:59:31 +0200121{
122 env_t env_new;
123 char *saved_buffer = NULL, flag = OBSOLETE_FLAG;
Andreas Fenkart0b2e5bb2017-04-08 11:59:34 +0200124 u32 saved_size, saved_offset, sector;
Andreas Fenkartafa81a72017-04-08 11:59:31 +0200125 int ret;
126
127 ret = setup_flash_device();
128 if (ret)
129 return ret;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200130
Marek Vasut7ce15262014-03-05 19:59:50 +0100131 ret = env_export(&env_new);
132 if (ret)
Simon Glassc5951992017-08-03 12:22:17 -0600133 return -EIO;
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400134 env_new.flags = ACTIVE_FLAG;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200135
Simon Glass203e94f2017-08-03 12:21:56 -0600136 if (gd->env_valid == ENV_VALID) {
Stefano Babica3110f02010-10-27 11:06:20 +0200137 env_new_offset = CONFIG_ENV_OFFSET_REDUND;
138 env_offset = CONFIG_ENV_OFFSET;
139 } else {
140 env_new_offset = CONFIG_ENV_OFFSET;
141 env_offset = CONFIG_ENV_OFFSET_REDUND;
142 }
143
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200144 /* Is the sector larger than the env (i.e. embedded) */
145 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
146 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
147 saved_offset = env_new_offset + CONFIG_ENV_SIZE;
Ravi Babu7dd01742015-08-17 13:29:49 +0530148 saved_buffer = memalign(ARCH_DMA_MINALIGN, saved_size);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200149 if (!saved_buffer) {
Simon Glassc5951992017-08-03 12:22:17 -0600150 ret = -ENOMEM;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200151 goto done;
152 }
Horatiu Vultur9a9d66f2018-12-11 10:13:56 +0100153 ret = spi_flash_read_env(env_flash, saved_offset,
154 saved_size, saved_buffer);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200155 if (ret)
156 goto done;
157 }
158
Andreas Fenkart0b2e5bb2017-04-08 11:59:34 +0200159 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200160
161 puts("Erasing SPI flash...");
162 ret = spi_flash_erase(env_flash, env_new_offset,
163 sector * CONFIG_ENV_SECT_SIZE);
164 if (ret)
165 goto done;
166
167 puts("Writing to SPI flash...");
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200168
Stefano Babica3110f02010-10-27 11:06:20 +0200169 ret = spi_flash_write(env_flash, env_new_offset,
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400170 CONFIG_ENV_SIZE, &env_new);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200171 if (ret)
172 goto done;
173
174 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
175 ret = spi_flash_write(env_flash, saved_offset,
176 saved_size, saved_buffer);
177 if (ret)
178 goto done;
179 }
180
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000181 ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags),
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400182 sizeof(env_new.flags), &flag);
Stefano Babica3110f02010-10-27 11:06:20 +0200183 if (ret)
184 goto done;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200185
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200186 puts("done\n");
187
Simon Glass203e94f2017-08-03 12:21:56 -0600188 gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
Stefano Babica3110f02010-10-27 11:06:20 +0200189
thomas.langer@lantiq.com2dc55d92011-03-29 02:35:14 +0000190 printf("Valid environment: %d\n", (int)gd->env_valid);
Stefano Babica3110f02010-10-27 11:06:20 +0200191
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200192 done:
193 if (saved_buffer)
194 free(saved_buffer);
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000195
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200196 return ret;
197}
Simon Glass4415f1d2017-08-03 12:21:58 -0600198#endif /* CMD_SAVEENV */
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200199
Simon Glassc5951992017-08-03 12:22:17 -0600200static int env_sf_load(void)
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200201{
202 int ret;
Simon Goldschmidt80719932018-01-31 14:47:13 +0100203 int read1_fail, read2_fail;
204 env_t *tmp_env1, *tmp_env2;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200205
Ravi Babu7dd01742015-08-17 13:29:49 +0530206 tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN,
207 CONFIG_ENV_SIZE);
208 tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN,
209 CONFIG_ENV_SIZE);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200210 if (!tmp_env1 || !tmp_env2) {
Yaniv Levinskyc5d548a2018-06-24 19:16:57 +0300211 set_default_env("malloc() failed", 0);
Simon Glassc5951992017-08-03 12:22:17 -0600212 ret = -EIO;
thomas.langer@lantiq.com2dc55d92011-03-29 02:35:14 +0000213 goto out;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200214 }
215
Andreas Fenkart8fee8842017-04-08 11:59:32 +0200216 ret = setup_flash_device();
217 if (ret)
thomas.langer@lantiq.com2dc55d92011-03-29 02:35:14 +0000218 goto out;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200219
Horatiu Vultur9a9d66f2018-12-11 10:13:56 +0100220 read1_fail = spi_flash_read_env(env_flash, CONFIG_ENV_OFFSET,
221 CONFIG_ENV_SIZE, tmp_env1);
222 read2_fail = spi_flash_read_env(env_flash, CONFIG_ENV_OFFSET_REDUND,
223 CONFIG_ENV_SIZE, tmp_env2);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200224
Simon Goldschmidt80719932018-01-31 14:47:13 +0100225 ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
226 read2_fail);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200227
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200228 spi_flash_free(env_flash);
229 env_flash = NULL;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200230out:
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200231 free(tmp_env1);
232 free(tmp_env2);
Simon Glassc5951992017-08-03 12:22:17 -0600233
234 return ret;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200235}
236#else
Simon Glass4415f1d2017-08-03 12:21:58 -0600237#ifdef CMD_SAVEENV
Simon Glasse5bce242017-08-03 12:22:01 -0600238static int env_sf_save(void)
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200239{
Andreas Fenkart0b2e5bb2017-04-08 11:59:34 +0200240 u32 saved_size, saved_offset, sector;
Marek Vasut7ce15262014-03-05 19:59:50 +0100241 char *saved_buffer = NULL;
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000242 int ret = 1;
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400243 env_t env_new;
Gong Qianyu19c31282016-01-26 15:06:42 +0800244
Andreas Fenkartafa81a72017-04-08 11:59:31 +0200245 ret = setup_flash_device();
246 if (ret)
247 return ret;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200248
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500249 /* Is the sector larger than the env (i.e. embedded) */
250 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
251 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
252 saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
253 saved_buffer = malloc(saved_size);
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000254 if (!saved_buffer)
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500255 goto done;
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000256
Horatiu Vultur9a9d66f2018-12-11 10:13:56 +0100257 ret = spi_flash_read_env(env_flash, saved_offset,
258 saved_size, saved_buffer);
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500259 if (ret)
260 goto done;
261 }
262
Marek Vasut7ce15262014-03-05 19:59:50 +0100263 ret = env_export(&env_new);
264 if (ret)
Stefano Babica3110f02010-10-27 11:06:20 +0200265 goto done;
Stefano Babica3110f02010-10-27 11:06:20 +0200266
Andreas Fenkart0b2e5bb2017-04-08 11:59:34 +0200267 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
268
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200269 puts("Erasing SPI flash...");
Stefano Babica3110f02010-10-27 11:06:20 +0200270 ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
271 sector * CONFIG_ENV_SECT_SIZE);
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500272 if (ret)
273 goto done;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200274
275 puts("Writing to SPI flash...");
Stefano Babica3110f02010-10-27 11:06:20 +0200276 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400277 CONFIG_ENV_SIZE, &env_new);
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500278 if (ret)
279 goto done;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200280
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500281 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
Stefano Babica3110f02010-10-27 11:06:20 +0200282 ret = spi_flash_write(env_flash, saved_offset,
283 saved_size, saved_buffer);
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500284 if (ret)
285 goto done;
286 }
287
288 ret = 0;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200289 puts("done\n");
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500290
291 done:
292 if (saved_buffer)
293 free(saved_buffer);
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000294
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500295 return ret;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200296}
Simon Glass4415f1d2017-08-03 12:21:58 -0600297#endif /* CMD_SAVEENV */
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200298
Simon Glassc5951992017-08-03 12:22:17 -0600299static int env_sf_load(void)
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200300{
301 int ret;
Ying Zhang5a89fa92014-01-24 15:50:07 +0800302 char *buf = NULL;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200303
Ravi Babu7dd01742015-08-17 13:29:49 +0530304 buf = (char *)memalign(ARCH_DMA_MINALIGN, CONFIG_ENV_SIZE);
Andreas Fenkartc041c602017-04-08 11:59:33 +0200305 if (!buf) {
Yaniv Levinskyc5d548a2018-06-24 19:16:57 +0300306 set_default_env("malloc() failed", 0);
Simon Glassc5951992017-08-03 12:22:17 -0600307 return -EIO;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200308 }
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200309
Andreas Fenkartc041c602017-04-08 11:59:33 +0200310 ret = setup_flash_device();
311 if (ret)
312 goto out;
313
Horatiu Vultur9a9d66f2018-12-11 10:13:56 +0100314 ret = spi_flash_read_env(env_flash, CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE,
315 buf);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200316 if (ret) {
Horatiu Vultur9a9d66f2018-12-11 10:13:56 +0100317 set_default_env("spi_flash_read_env() failed", 0);
Andreas Fenkartc041c602017-04-08 11:59:33 +0200318 goto err_read;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200319 }
320
321 ret = env_import(buf, 1);
Simon Goldschmidt42a18202018-01-31 14:47:10 +0100322 if (!ret)
Simon Glass203e94f2017-08-03 12:21:56 -0600323 gd->env_valid = ENV_VALID;
Andreas Fenkartc041c602017-04-08 11:59:33 +0200324
325err_read:
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200326 spi_flash_free(env_flash);
327 env_flash = NULL;
Andreas Fenkartc041c602017-04-08 11:59:33 +0200328out:
329 free(buf);
Simon Glassc5951992017-08-03 12:22:17 -0600330
331 return ret;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200332}
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200333#endif
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200334
Rajesh Bhagat119c01c22018-11-05 18:01:15 +0000335#ifdef CONFIG_ENV_ADDR
336__weak void *env_sf_get_env_addr(void)
337{
338 return (void *)CONFIG_ENV_ADDR;
339}
340#endif
341
Ashish Kumarb500c922017-12-14 17:37:08 +0530342#if defined(INITENV) && defined(CONFIG_ENV_ADDR)
343static int env_sf_init(void)
344{
Rajesh Bhagat119c01c22018-11-05 18:01:15 +0000345 env_t *env_ptr = (env_t *)env_sf_get_env_addr();
Ashish Kumarb500c922017-12-14 17:37:08 +0530346
347 if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
348 gd->env_addr = (ulong)&(env_ptr->data);
349 gd->env_valid = 1;
350 } else {
351 gd->env_addr = (ulong)&default_environment[0];
352 gd->env_valid = 1;
353 }
354
355 return 0;
356}
357#endif
358
Simon Glass4415f1d2017-08-03 12:21:58 -0600359U_BOOT_ENV_LOCATION(sf) = {
360 .location = ENVL_SPI_FLASH,
Simon Glassac358be2017-08-03 12:22:03 -0600361 ENV_NAME("SPI Flash")
Simon Glasse5bce242017-08-03 12:22:01 -0600362 .load = env_sf_load,
Simon Glass4415f1d2017-08-03 12:21:58 -0600363#ifdef CMD_SAVEENV
Simon Glasse5bce242017-08-03 12:22:01 -0600364 .save = env_save_ptr(env_sf_save),
Simon Glass4415f1d2017-08-03 12:21:58 -0600365#endif
Ashish Kumarb500c922017-12-14 17:37:08 +0530366#if defined(INITENV) && defined(CONFIG_ENV_ADDR)
367 .init = env_sf_init,
368#endif
Simon Glass4415f1d2017-08-03 12:21:58 -0600369};