]> git.ipfire.org Git - people/ms/u-boot.git/blob - common/env_dataflash.c
Merge branch 'u-boot/master' into u-boot-arm/master
[people/ms/u-boot.git] / common / 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 uchar env_get_char_spec(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 void env_relocate_spec(void)
31 {
32 char buf[CONFIG_ENV_SIZE];
33
34 read_dataflash(CONFIG_ENV_ADDR, CONFIG_ENV_SIZE, buf);
35
36 env_import(buf, 1);
37 }
38
39 #ifdef CONFIG_ENV_OFFSET_REDUND
40 #error No support for redundant environment on dataflash yet!
41 #endif
42
43 int saveenv(void)
44 {
45 env_t env_new;
46 ssize_t len;
47 char *res;
48
49 res = (char *)&env_new.data;
50 len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
51 if (len < 0) {
52 error("Cannot export environment: errno = %d\n", errno);
53 return 1;
54 }
55 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
56
57 return write_dataflash(CONFIG_ENV_ADDR,
58 (unsigned long)&env_new,
59 CONFIG_ENV_SIZE);
60 }
61
62 /*
63 * Initialize environment use
64 *
65 * We are still running from ROM, so data use is limited.
66 * Use a (moderately small) buffer on the stack
67 */
68 int env_init(void)
69 {
70 ulong crc, len = ENV_SIZE, new = 0;
71 unsigned off;
72 uchar buf[64];
73
74 if (gd->env_valid)
75 return 0;
76
77 AT91F_DataflashInit(); /* prepare for DATAFLASH read/write */
78
79 /* read old CRC */
80 read_dataflash(CONFIG_ENV_ADDR + offsetof(env_t, crc),
81 sizeof(ulong), (char *)&crc);
82
83 off = offsetof(env_t, data);
84 while (len > 0) {
85 int n = (len > sizeof(buf)) ? sizeof(buf) : len;
86
87 read_dataflash(CONFIG_ENV_ADDR + off, n, (char *)buf);
88
89 new = crc32(new, buf, n);
90 len -= n;
91 off += n;
92 }
93
94 if (crc == new) {
95 gd->env_addr = offsetof(env_t, data);
96 gd->env_valid = 1;
97 } else {
98 gd->env_addr = (ulong)&default_environment[0];
99 gd->env_valid = 0;
100 }
101
102 return 0;
103 }