1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26#include <common.h>
27#include <command.h>
28#include <environment.h>
29#include <linux/stddef.h>
30#include <search.h>
31#include <errno.h>
32
33DECLARE_GLOBAL_DATA_PTR;
34
35#ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE
36extern void *nvram_read(void *dest, const long src, size_t count);
37extern void nvram_write(long dest, const void *src, size_t count);
38#else
39env_t *env_ptr = (env_t *)CONFIG_ENV_ADDR;
40#endif
41
42#ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE
43
44
45
46int env_nvram_get_char(int index)
47{
48 uchar c;
49
50 nvram_read(&c, CONFIG_ENV_ADDR + index, 1);
51
52 return c;
53}
54#endif
55
56static int env_nvram_load(void)
57{
58 char buf[CONFIG_ENV_SIZE];
59
60#if defined(CONFIG_SYS_NVRAM_ACCESS_ROUTINE)
61 nvram_read(buf, CONFIG_ENV_ADDR, CONFIG_ENV_SIZE);
62#else
63 memcpy(buf, (void *)CONFIG_ENV_ADDR, CONFIG_ENV_SIZE);
64#endif
65 return env_import(buf, 1);
66}
67
68static int env_nvram_save(void)
69{
70 env_t env_new;
71 int rcode = 0;
72
73 rcode = env_export(&env_new);
74 if (rcode)
75 return rcode;
76
77#ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE
78 nvram_write(CONFIG_ENV_ADDR, &env_new, CONFIG_ENV_SIZE);
79#else
80 if (memcpy((char *)CONFIG_ENV_ADDR, &env_new, CONFIG_ENV_SIZE) == NULL)
81 rcode = 1;
82#endif
83 return rcode;
84}
85
86
87
88
89
90
91static int env_nvram_init(void)
92{
93#if defined(CONFIG_SYS_NVRAM_ACCESS_ROUTINE)
94 ulong crc;
95 uchar data[ENV_SIZE];
96
97 nvram_read(&crc, CONFIG_ENV_ADDR, sizeof(ulong));
98 nvram_read(data, CONFIG_ENV_ADDR + sizeof(ulong), ENV_SIZE);
99
100 if (crc32(0, data, ENV_SIZE) == crc) {
101 gd->env_addr = (ulong)CONFIG_ENV_ADDR + sizeof(long);
102#else
103 if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
104 gd->env_addr = (ulong)&env_ptr->data;
105#endif
106 gd->env_valid = ENV_VALID;
107 } else {
108 gd->env_addr = (ulong)&default_environment[0];
109 gd->env_valid = ENV_INVALID;
110 }
111
112 return 0;
113}
114
115U_BOOT_ENV_LOCATION(nvram) = {
116 .location = ENVL_NVRAM,
117 ENV_NAME("NVRAM")
118 .load = env_nvram_load,
119 .save = env_save_ptr(env_nvram_save),
120 .init = env_nvram_init,
121};
122