blob: 77bc595e0debc4c65a4518900237d80022166731 [file] [log] [blame]
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02001/*
2 * LowLevel function for DataFlash environment support
wdenk86765902003-12-06 23:55:10 +00003 * Author : Gilles Gastaldi (Atmel)
4 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenk86765902003-12-06 23:55:10 +00006 */
7#include <common.h>
wdenk86765902003-12-06 23:55:10 +00008#include <command.h>
9#include <environment.h>
10#include <linux/stddef.h>
wdenk86765902003-12-06 23:55:10 +000011#include <dataflash.h>
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020012#include <search.h>
13#include <errno.h>
wdenk86765902003-12-06 23:55:10 +000014
Wolfgang Denk6405a152006-03-31 18:32:53 +020015DECLARE_GLOBAL_DATA_PTR;
16
Simon Glass3fa4c222017-08-03 12:22:16 -060017static int env_dataflash_get_char(int index)
wdenk86765902003-12-06 23:55:10 +000018{
19 uchar c;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020020
Igor Grinbergb90c1fc2011-11-07 01:14:02 +000021 read_dataflash(CONFIG_ENV_ADDR + index + offsetof(env_t, data),
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020022 1, (char *)&c);
Igor Grinbergb90c1fc2011-11-07 01:14:02 +000023 return c;
wdenk86765902003-12-06 23:55:10 +000024}
25
Simon Glass99778492017-08-03 12:22:17 -060026static int env_dataflash_load(void)
wdenk86765902003-12-06 23:55:10 +000027{
Bo Shen5c7bad52013-10-08 16:30:21 +080028 ulong crc, new = 0;
29 unsigned off;
Tom Rinia840b8e2013-04-05 14:55:21 -040030 char buf[CONFIG_ENV_SIZE];
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020031
Bo Shen5c7bad52013-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 Rinia840b8e2013-04-05 14:55:21 -040037 read_dataflash(CONFIG_ENV_ADDR, CONFIG_ENV_SIZE, buf);
38
Bo Shen5c7bad52013-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 Glass99778492017-08-03 12:22:17 -060047
48 return 0;
wdenk86765902003-12-06 23:55:10 +000049}
50
Wolfgang Denk460a9ff2010-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 Glass082af922017-08-03 12:22:01 -060055static int env_dataflash_save(void)
wdenk86765902003-12-06 23:55:10 +000056{
Marek Vasutd73c1292014-03-05 19:59:50 +010057 env_t env_new;
58 int ret;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020059
Marek Vasutd73c1292014-03-05 19:59:50 +010060 ret = env_export(&env_new);
61 if (ret)
62 return ret;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020063
64 return write_dataflash(CONFIG_ENV_ADDR,
Tom Rinia840b8e2013-04-05 14:55:21 -040065 (unsigned long)&env_new,
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020066 CONFIG_ENV_SIZE);
wdenk86765902003-12-06 23:55:10 +000067}
68
Simon Glassc10a88e2017-08-03 12:21:58 -060069U_BOOT_ENV_LOCATION(dataflash) = {
70 .location = ENVL_DATAFLASH,
Simon Glassd8273ed2017-08-03 12:22:03 -060071 ENV_NAME("dataflash")
Simon Glass082af922017-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 Glassc10a88e2017-08-03 12:21:58 -060075};