]> git.ipfire.org Git - people/ms/u-boot.git/blame - env/dataflash.c
Merge git://git.denx.de/u-boot-fsl-qoriq
[people/ms/u-boot.git] / env / dataflash.c
CommitLineData
ea882baf
WD
1/*
2 * LowLevel function for DataFlash environment support
5779d8d9
WD
3 * Author : Gilles Gastaldi (Atmel)
4 *
1a459660 5 * SPDX-License-Identifier: GPL-2.0+
5779d8d9
WD
6 */
7#include <common.h>
5779d8d9
WD
8#include <command.h>
9#include <environment.h>
10#include <linux/stddef.h>
5779d8d9 11#include <dataflash.h>
ea882baf
WD
12#include <search.h>
13#include <errno.h>
5779d8d9 14
d87080b7
WD
15DECLARE_GLOBAL_DATA_PTR;
16
21f63944 17static int env_dataflash_get_char(int index)
5779d8d9
WD
18{
19 uchar c;
ea882baf 20
0901d9f8 21 read_dataflash(CONFIG_ENV_ADDR + index + offsetof(env_t, data),
ea882baf 22 1, (char *)&c);
0901d9f8 23 return c;
5779d8d9
WD
24}
25
c5951991 26static int env_dataflash_load(void)
5779d8d9 27{
cca2011e
BS
28 ulong crc, new = 0;
29 unsigned off;
cd0f4fa1 30 char buf[CONFIG_ENV_SIZE];
ea882baf 31
cca2011e
BS
32 /* Read old CRC */
33 read_dataflash(CONFIG_ENV_ADDR + offsetof(env_t, crc),
34 sizeof(ulong), (char *)&crc);
35
36 /* Read whole environment */
cd0f4fa1
TR
37 read_dataflash(CONFIG_ENV_ADDR, CONFIG_ENV_SIZE, buf);
38
cca2011e
BS
39 /* 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");
c5951991
SG
47
48 return 0;
5779d8d9
WD
49}
50
ea882baf
WD
51#ifdef CONFIG_ENV_OFFSET_REDUND
52#error No support for redundant environment on dataflash yet!
53#endif
54
e5bce247 55static int env_dataflash_save(void)
5779d8d9 56{
7ce1526e
MV
57 env_t env_new;
58 int ret;
59
60 ret = env_export(&env_new);
61 if (ret)
62 return ret;
ea882baf
WD
63
64 return write_dataflash(CONFIG_ENV_ADDR,
cd0f4fa1 65 (unsigned long)&env_new,
ea882baf 66 CONFIG_ENV_SIZE);
5779d8d9
WD
67}
68
4415f1d1
SG
69U_BOOT_ENV_LOCATION(dataflash) = {
70 .location = ENVL_DATAFLASH,
ac358beb 71 ENV_NAME("dataflash")
e5bce247
SG
72 .get_char = env_dataflash_get_char,
73 .load = env_dataflash_load,
74 .save = env_save_ptr(env_dataflash_save),
4415f1d1 75};