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