blob: 77bc595e0debc4c65a4518900237d80022166731 [file] [log] [blame]
Wolfgang Denkea882ba2010-06-20 23:33:59 +02001/*
2 * LowLevel function for DataFlash environment support
wdenk5779d8d2003-12-06 23:55:10 +00003 * Author : Gilles Gastaldi (Atmel)
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenk5779d8d2003-12-06 23:55:10 +00006 */
7#include <common.h>
wdenk5779d8d2003-12-06 23:55:10 +00008#include <command.h>
9#include <environment.h>
10#include <linux/stddef.h>
wdenk5779d8d2003-12-06 23:55:10 +000011#include <dataflash.h>
Wolfgang Denkea882ba2010-06-20 23:33:59 +020012#include <search.h>
13#include <errno.h>
wdenk5779d8d2003-12-06 23:55:10 +000014
Wolfgang Denkd87080b2006-03-31 18:32:53 +020015DECLARE_GLOBAL_DATA_PTR;
16
Simon Glass21f63942017-08-03 12:22:16 -060017static int env_dataflash_get_char(int index)
wdenk5779d8d2003-12-06 23:55:10 +000018{
19 uchar c;
Wolfgang Denkea882ba2010-06-20 23:33:59 +020020
Igor Grinberg0901d9f2011-11-07 01:14:02 +000021 read_dataflash(CONFIG_ENV_ADDR + index + offsetof(env_t, data),
Wolfgang Denkea882ba2010-06-20 23:33:59 +020022 1, (char *)&c);
Igor Grinberg0901d9f2011-11-07 01:14:02 +000023 return c;
wdenk5779d8d2003-12-06 23:55:10 +000024}
25
Simon Glassc5951992017-08-03 12:22:17 -060026static int env_dataflash_load(void)
wdenk5779d8d2003-12-06 23:55:10 +000027{
Bo Shencca20112013-10-08 16:30:21 +080028 ulong crc, new = 0;
29 unsigned off;
Tom Rinicd0f4fa2013-04-05 14:55:21 -040030 char buf[CONFIG_ENV_SIZE];
Wolfgang Denkea882ba2010-06-20 23:33:59 +020031
Bo Shencca20112013-10-08 16:30:21 +080032 /* Read old CRC */
33 read_dataflash(CONFIG_ENV_ADDR + offsetof(env_t, crc),
34 sizeof(ulong), (char *)&crc);
35
36 /* Read whole environment */
Tom Rinicd0f4fa2013-04-05 14:55:21 -040037 read_dataflash(CONFIG_ENV_ADDR, CONFIG_ENV_SIZE, buf);
38
Bo Shencca20112013-10-08 16:30:21 +080039 /* Calculate the CRC */
40 off = offsetof(env_t, data);
41 new = crc32(new, (unsigned char *)(buf + off), ENV_SIZE);
42
43 if (crc == new)
44 env_import(buf, 1);
45 else
46 set_default_env("!bad CRC");
Simon Glassc5951992017-08-03 12:22:17 -060047
48 return 0;
wdenk5779d8d2003-12-06 23:55:10 +000049}
50
Wolfgang Denkea882ba2010-06-20 23:33:59 +020051#ifdef CONFIG_ENV_OFFSET_REDUND
52#error No support for redundant environment on dataflash yet!
53#endif
54
Simon Glasse5bce242017-08-03 12:22:01 -060055static int env_dataflash_save(void)
wdenk5779d8d2003-12-06 23:55:10 +000056{
Marek Vasut7ce15262014-03-05 19:59:50 +010057 env_t env_new;
58 int ret;
Wolfgang Denkea882ba2010-06-20 23:33:59 +020059
Marek Vasut7ce15262014-03-05 19:59:50 +010060 ret = env_export(&env_new);
61 if (ret)
62 return ret;
Wolfgang Denkea882ba2010-06-20 23:33:59 +020063
64 return write_dataflash(CONFIG_ENV_ADDR,
Tom Rinicd0f4fa2013-04-05 14:55:21 -040065 (unsigned long)&env_new,
Wolfgang Denkea882ba2010-06-20 23:33:59 +020066 CONFIG_ENV_SIZE);
wdenk5779d8d2003-12-06 23:55:10 +000067}
68
Simon Glass4415f1d2017-08-03 12:21:58 -060069U_BOOT_ENV_LOCATION(dataflash) = {
70 .location = ENVL_DATAFLASH,
Simon Glassac358be2017-08-03 12:22:03 -060071 ENV_NAME("dataflash")
Simon Glasse5bce242017-08-03 12:22:01 -060072 .get_char = env_dataflash_get_char,
73 .load = env_dataflash_load,
74 .save = env_save_ptr(env_dataflash_save),
Simon Glass4415f1d2017-08-03 12:21:58 -060075};