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