env: Rename getenv_hex(), getenv_yesno(), getenv_ulong()
[platform/kernel/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 static unsigned char env_dataflash_get_char(int index)
18 {
19         uchar c;
20
21         read_dataflash(CONFIG_ENV_ADDR + index + offsetof(env_t, data),
22                         1, (char *)&c);
23         return c;
24 }
25
26 static void env_dataflash_load(void)
27 {
28         ulong crc, new = 0;
29         unsigned off;
30         char buf[CONFIG_ENV_SIZE];
31
32         /* Read old CRC */
33         read_dataflash(CONFIG_ENV_ADDR + offsetof(env_t, crc),
34                        sizeof(ulong), (char *)&crc);
35
36         /* Read whole environment */
37         read_dataflash(CONFIG_ENV_ADDR, CONFIG_ENV_SIZE, buf);
38
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");
47 }
48
49 #ifdef CONFIG_ENV_OFFSET_REDUND
50 #error No support for redundant environment on dataflash yet!
51 #endif
52
53 static int env_dataflash_save(void)
54 {
55         env_t env_new;
56         int ret;
57
58         ret = env_export(&env_new);
59         if (ret)
60                 return ret;
61
62         return write_dataflash(CONFIG_ENV_ADDR,
63                                 (unsigned long)&env_new,
64                                 CONFIG_ENV_SIZE);
65 }
66
67 U_BOOT_ENV_LOCATION(dataflash) = {
68         .location       = ENVL_DATAFLASH,
69         ENV_NAME("dataflash")
70         .get_char       = env_dataflash_get_char,
71         .load           = env_dataflash_load,
72         .save           = env_save_ptr(env_dataflash_save),
73 };