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
28#include <command.h>
29#include <environment.h>
30#include <linux/stddef.h>
31#include <malloc.h>
32#include <search.h>
33#include <errno.h>
34#include <fat.h>
35#include <mmc.h>
36
37char *env_name_spec = "FAT";
38
39env_t *env_ptr;
40
41DECLARE_GLOBAL_DATA_PTR;
42
43uchar env_get_char_spec(int index)
44{
45 return *((uchar *)(gd->env_addr + index));
46}
47
48int env_init(void)
49{
50
51 gd->env_addr = (ulong)&default_environment[0];
52 gd->env_valid = 1;
53
54 return 0;
55}
56
57#ifdef CONFIG_CMD_SAVEENV
58int saveenv(void)
59{
60 env_t env_new;
61 ssize_t len;
62 char *res;
63 block_dev_desc_t *dev_desc = NULL;
64 int dev = FAT_ENV_DEVICE;
65 int part = FAT_ENV_PART;
66
67 res = (char *)&env_new.data;
68 len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL);
69 if (len < 0) {
70 error("Cannot export environment: errno = %d\n", errno);
71 return 1;
72 }
73
74#ifdef CONFIG_MMC
75 if (strcmp (FAT_ENV_INTERFACE, "mmc") == 0) {
76 struct mmc *mmc = find_mmc_device(dev);
77
78 if (!mmc) {
79 printf("no mmc device at slot %x\n", dev);
80 return 1;
81 }
82
83 mmc->has_init = 0;
84 mmc_init(mmc);
85 }
86#endif
87
88 dev_desc = get_dev(FAT_ENV_INTERFACE, dev);
89 if (dev_desc == NULL) {
90 printf("Failed to find %s%d\n",
91 FAT_ENV_INTERFACE, dev);
92 return 1;
93 }
94 if (fat_register_device(dev_desc, part) != 0) {
95 printf("Failed to register %s%d:%d\n",
96 FAT_ENV_INTERFACE, dev, part);
97 return 1;
98 }
99
100 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
101 if (file_fat_write(FAT_ENV_FILE, (void *)&env_new, sizeof(env_t)) == -1) {
102 printf("\n** Unable to write \"%s\" from %s%d:%d **\n",
103 FAT_ENV_FILE, FAT_ENV_INTERFACE, dev, part);
104 return 1;
105 }
106
107 puts("done\n");
108 return 0;
109}
110#endif
111
112void env_relocate_spec(void)
113{
114 char buf[CONFIG_ENV_SIZE];
115 block_dev_desc_t *dev_desc = NULL;
116 int dev = FAT_ENV_DEVICE;
117 int part = FAT_ENV_PART;
118
119#ifdef CONFIG_MMC
120 if (strcmp (FAT_ENV_INTERFACE, "mmc") == 0) {
121 struct mmc *mmc = find_mmc_device(dev);
122
123 if (!mmc) {
124 printf("no mmc device at slot %x\n", dev);
125 set_default_env(NULL);
126 return;
127 }
128
129 mmc->has_init = 0;
130 mmc_init(mmc);
131 }
132#endif
133
134 dev_desc = get_dev(FAT_ENV_INTERFACE, dev);
135 if (dev_desc == NULL) {
136 printf("Failed to find %s%d\n",
137 FAT_ENV_INTERFACE, dev);
138 set_default_env(NULL);
139 return;
140 }
141 if (fat_register_device(dev_desc, part) != 0) {
142 printf("Failed to register %s%d:%d\n",
143 FAT_ENV_INTERFACE, dev, part);
144 set_default_env(NULL);
145 return;
146 }
147
148 if (file_fat_read(FAT_ENV_FILE, (unsigned char *)&buf, CONFIG_ENV_SIZE) == -1) {
149 printf("\n** Unable to read \"%s\" from %s%d:%d **\n",
150 FAT_ENV_FILE, FAT_ENV_INTERFACE, dev, part);
151 set_default_env(NULL);
152 return;
153 }
154
155 env_import(buf, 1);
156}
157