]> git.ipfire.org Git - people/ms/u-boot.git/blob - env/dataflash.c
env: Drop common init() functions
[people/ms/u-boot.git] / env / dataflash.c
1 /*
2 * LowLevel function for DataFlash environment support
3 * Author : Gilles Gastaldi (Atmel)
4 *
5 * SPDX-License-Identifier: GPL-2.0+
6 */
7 #include <common.h>
8 #include <command.h>
9 #include <environment.h>
10 #include <linux/stddef.h>
11 #include <dataflash.h>
12 #include <search.h>
13 #include <errno.h>
14
15 DECLARE_GLOBAL_DATA_PTR;
16
17 env_t *env_ptr;
18
19 char *env_name_spec = "dataflash";
20
21 static unsigned char env_dataflash_get_char(int index)
22 {
23 uchar c;
24
25 read_dataflash(CONFIG_ENV_ADDR + index + offsetof(env_t, data),
26 1, (char *)&c);
27 return c;
28 }
29
30 static void env_dataflash_load(void)
31 {
32 ulong crc, new = 0;
33 unsigned off;
34 char buf[CONFIG_ENV_SIZE];
35
36 /* Read old CRC */
37 read_dataflash(CONFIG_ENV_ADDR + offsetof(env_t, crc),
38 sizeof(ulong), (char *)&crc);
39
40 /* Read whole environment */
41 read_dataflash(CONFIG_ENV_ADDR, CONFIG_ENV_SIZE, buf);
42
43 /* Calculate the CRC */
44 off = offsetof(env_t, data);
45 new = crc32(new, (unsigned char *)(buf + off), ENV_SIZE);
46
47 if (crc == new)
48 env_import(buf, 1);
49 else
50 set_default_env("!bad CRC");
51 }
52
53 #ifdef CONFIG_ENV_OFFSET_REDUND
54 #error No support for redundant environment on dataflash yet!
55 #endif
56
57 static int env_dataflash_save(void)
58 {
59 env_t env_new;
60 int ret;
61
62 ret = env_export(&env_new);
63 if (ret)
64 return ret;
65
66 return write_dataflash(CONFIG_ENV_ADDR,
67 (unsigned long)&env_new,
68 CONFIG_ENV_SIZE);
69 }
70
71 U_BOOT_ENV_LOCATION(dataflash) = {
72 .location = ENVL_DATAFLASH,
73 .get_char = env_dataflash_get_char,
74 .load = env_dataflash_load,
75 .save = env_save_ptr(env_dataflash_save),
76 };