]> git.sur5r.net Git - u-boot/blob - env/dataflash.c
9421a6a5b236c598e900cfb66a542e11bf988a88
[u-boot] / 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 static unsigned char env_dataflash_get_char(int index)
20 {
21         uchar c;
22
23         read_dataflash(CONFIG_ENV_ADDR + index + offsetof(env_t, data),
24                         1, (char *)&c);
25         return c;
26 }
27
28 static void env_dataflash_load(void)
29 {
30         ulong crc, new = 0;
31         unsigned off;
32         char buf[CONFIG_ENV_SIZE];
33
34         /* Read old CRC */
35         read_dataflash(CONFIG_ENV_ADDR + offsetof(env_t, crc),
36                        sizeof(ulong), (char *)&crc);
37
38         /* Read whole environment */
39         read_dataflash(CONFIG_ENV_ADDR, CONFIG_ENV_SIZE, buf);
40
41         /* Calculate the CRC */
42         off = offsetof(env_t, data);
43         new = crc32(new, (unsigned char *)(buf + off), ENV_SIZE);
44
45         if (crc == new)
46                 env_import(buf, 1);
47         else
48                 set_default_env("!bad CRC");
49 }
50
51 #ifdef CONFIG_ENV_OFFSET_REDUND
52 #error No support for redundant environment on dataflash yet!
53 #endif
54
55 static int env_dataflash_save(void)
56 {
57         env_t env_new;
58         int ret;
59
60         ret = env_export(&env_new);
61         if (ret)
62                 return ret;
63
64         return write_dataflash(CONFIG_ENV_ADDR,
65                                 (unsigned long)&env_new,
66                                 CONFIG_ENV_SIZE);
67 }
68
69 U_BOOT_ENV_LOCATION(dataflash) = {
70         .location       = ENVL_DATAFLASH,
71         ENV_NAME("dataflash")
72         .get_char       = env_dataflash_get_char,
73         .load           = env_dataflash_load,
74         .save           = env_save_ptr(env_dataflash_save),
75 };