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