2 * LowLevel function for DataFlash environment support
3 * Author : Gilles Gastaldi (Atmel)
5 * SPDX-License-Identifier: GPL-2.0+
9 #include <environment.h>
10 #include <linux/stddef.h>
11 #include <dataflash.h>
15 DECLARE_GLOBAL_DATA_PTR;
19 char *env_name_spec = "dataflash";
21 static unsigned char env_dataflash_get_char(int index)
25 read_dataflash(CONFIG_ENV_ADDR + index + offsetof(env_t, data),
30 static void env_dataflash_load(void)
34 char buf[CONFIG_ENV_SIZE];
37 read_dataflash(CONFIG_ENV_ADDR + offsetof(env_t, crc),
38 sizeof(ulong), (char *)&crc);
40 /* Read whole environment */
41 read_dataflash(CONFIG_ENV_ADDR, CONFIG_ENV_SIZE, buf);
43 /* Calculate the CRC */
44 off = offsetof(env_t, data);
45 new = crc32(new, (unsigned char *)(buf + off), ENV_SIZE);
50 set_default_env("!bad CRC");
53 #ifdef CONFIG_ENV_OFFSET_REDUND
54 #error No support for redundant environment on dataflash yet!
57 static int env_dataflash_save(void)
62 ret = env_export(&env_new);
66 return write_dataflash(CONFIG_ENV_ADDR,
67 (unsigned long)&env_new,
72 * Initialize environment use
74 * We are still running from ROM, so data use is limited.
75 * Use a (moderately small) buffer on the stack
77 int env_dataflash_init(void)
80 gd->env_addr = (ulong)&default_environment[0];
81 gd->env_valid = ENV_VALID;
86 U_BOOT_ENV_LOCATION(dataflash) = {
87 .location = ENVL_DATAFLASH,
88 .get_char = env_dataflash_get_char,
89 .load = env_dataflash_load,
90 .save = env_save_ptr(env_dataflash_save),
91 .init = env_dataflash_init,