]>
Commit | Line | Data |
---|---|---|
05706fdd | 1 | /* |
ea882baf | 2 | * (C) Copyright 2000-2010 |
05706fdd WD |
3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
4 | * | |
5 | * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com> | |
6 | * Andreas Heppel <aheppel@sysgo.de> | |
ea882baf | 7 | * |
05706fdd WD |
8 | * See file CREDITS for list of people who contributed to this |
9 | * project. | |
10 | * | |
11 | * This program is free software; you can redistribute it and/or | |
12 | * modify it under the terms of the GNU General Public License as | |
13 | * published by the Free Software Foundation; either version 2 of | |
14 | * the License, or (at your option) any later version. | |
15 | * | |
16 | * This program is distributed in the hope that it will be useful, | |
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | * GNU General Public License for more details. | |
20 | * | |
21 | * You should have received a copy of the GNU General Public License | |
22 | * along with this program; if not, write to the Free Software | |
23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, | |
24 | * MA 02111-1307 USA | |
25 | */ | |
26 | ||
27 | #include <common.h> | |
28 | #include <command.h> | |
29 | #include <environment.h> | |
05706fdd | 30 | #include <linux/stddef.h> |
ea882baf WD |
31 | #include <search.h> |
32 | #include <errno.h> | |
05706fdd WD |
33 | #include <malloc.h> |
34 | ||
d87080b7 WD |
35 | DECLARE_GLOBAL_DATA_PTR; |
36 | ||
05706fdd WD |
37 | /************************************************************************ |
38 | * Default settings to be used when no valid environment is found | |
39 | */ | |
ddd8418f | 40 | #include <env_default.h> |
05706fdd | 41 | |
c5983592 | 42 | struct hsearch_data env_htab = { |
7afcf3a5 | 43 | .change_ok = env_change_ok, |
c5983592 | 44 | }; |
2eb1573f | 45 | |
bf95df44 IG |
46 | static uchar __env_get_char_spec(int index) |
47 | { | |
48 | return *((uchar *)(gd->env_addr + index)); | |
49 | } | |
50 | uchar env_get_char_spec(int) | |
51 | __attribute__((weak, alias("__env_get_char_spec"))); | |
52 | ||
27aafe98 | 53 | static uchar env_get_char_init(int index) |
05706fdd | 54 | { |
05706fdd WD |
55 | /* if crc was bad, use the default environment */ |
56 | if (gd->env_valid) | |
27aafe98 | 57 | return env_get_char_spec(index); |
ea882baf | 58 | else |
27aafe98 | 59 | return default_environment[index]; |
05706fdd WD |
60 | } |
61 | ||
27aafe98 | 62 | uchar env_get_char_memory(int index) |
05706fdd | 63 | { |
ea882baf | 64 | return *env_get_addr(index); |
05706fdd WD |
65 | } |
66 | ||
27aafe98 | 67 | uchar env_get_char(int index) |
b502611b | 68 | { |
b502611b JT |
69 | /* if relocated to RAM */ |
70 | if (gd->flags & GD_FLG_RELOC) | |
27aafe98 | 71 | return env_get_char_memory(index); |
b502611b | 72 | else |
27aafe98 | 73 | return env_get_char_init(index); |
b502611b JT |
74 | } |
75 | ||
27aafe98 | 76 | const uchar *env_get_addr(int index) |
05706fdd | 77 | { |
ea882baf WD |
78 | if (gd->env_valid) |
79 | return (uchar *)(gd->env_addr + index); | |
80 | else | |
81 | return &default_environment[index]; | |
05706fdd WD |
82 | } |
83 | ||
ea882baf | 84 | void set_default_env(const char *s) |
5bb12dbd | 85 | { |
c4e0057f JH |
86 | int flags = 0; |
87 | ||
5bb12dbd | 88 | if (sizeof(default_environment) > ENV_SIZE) { |
ea882baf | 89 | puts("*** Error - default environment is too large\n\n"); |
5bb12dbd HW |
90 | return; |
91 | } | |
92 | ||
ea882baf WD |
93 | if (s) { |
94 | if (*s == '!') { | |
95 | printf("*** Warning - %s, " | |
96 | "using default environment\n\n", | |
27aafe98 | 97 | s + 1); |
ea882baf | 98 | } else { |
c4e0057f | 99 | flags = H_INTERACTIVE; |
ea882baf WD |
100 | puts(s); |
101 | } | |
102 | } else { | |
103 | puts("Using default environment\n\n"); | |
104 | } | |
105 | ||
2eb1573f | 106 | if (himport_r(&env_htab, (char *)default_environment, |
c4e0057f JH |
107 | sizeof(default_environment), '\0', flags, |
108 | 0, NULL) == 0) | |
ea882baf | 109 | error("Environment import failed: errno = %d\n", errno); |
27aafe98 | 110 | |
ea882baf | 111 | gd->flags |= GD_FLG_ENV_READY; |
5bb12dbd HW |
112 | } |
113 | ||
b64b7c3d GF |
114 | |
115 | /* [re]set individual variables to their value in the default environment */ | |
116 | int set_default_vars(int nvars, char * const vars[]) | |
117 | { | |
118 | /* | |
119 | * Special use-case: import from default environment | |
120 | * (and use \0 as a separator) | |
121 | */ | |
122 | return himport_r(&env_htab, (const char *)default_environment, | |
c4e0057f JH |
123 | sizeof(default_environment), '\0', |
124 | H_NOCLEAR | H_INTERACTIVE, nvars, vars); | |
b64b7c3d GF |
125 | } |
126 | ||
7ac2fe2d | 127 | #ifndef CONFIG_SPL_BUILD |
ea882baf WD |
128 | /* |
129 | * Check if CRC is valid and (if yes) import the environment. | |
130 | * Note that "buf" may or may not be aligned. | |
131 | */ | |
132 | int env_import(const char *buf, int check) | |
05706fdd | 133 | { |
ea882baf | 134 | env_t *ep = (env_t *)buf; |
05706fdd | 135 | |
ea882baf WD |
136 | if (check) { |
137 | uint32_t crc; | |
138 | ||
139 | memcpy(&crc, &ep->crc, sizeof(crc)); | |
140 | ||
141 | if (crc32(0, ep->data, ENV_SIZE) != crc) { | |
142 | set_default_env("!bad CRC"); | |
143 | return 0; | |
144 | } | |
145 | } | |
146 | ||
348b1f1c | 147 | if (himport_r(&env_htab, (char *)ep->data, ENV_SIZE, '\0', 0, |
c4e0057f | 148 | 0, NULL)) { |
ea882baf WD |
149 | gd->flags |= GD_FLG_ENV_READY; |
150 | return 1; | |
151 | } | |
05706fdd | 152 | |
ea882baf WD |
153 | error("Cannot import environment: errno = %d\n", errno); |
154 | ||
155 | set_default_env("!import failed"); | |
156 | ||
157 | return 0; | |
158 | } | |
7ac2fe2d | 159 | #endif |
ea882baf | 160 | |
27aafe98 | 161 | void env_relocate(void) |
ea882baf | 162 | { |
2e5167cc | 163 | #if defined(CONFIG_NEEDS_MANUAL_RELOC) |
60f7da1f | 164 | env_reloc(); |
7afcf3a5 | 165 | env_htab.change_ok += gd->reloc_off; |
60f7da1f | 166 | #endif |
05706fdd | 167 | if (gd->env_valid == 0) { |
7ac2fe2d IY |
168 | #if defined(CONFIG_ENV_IS_NOWHERE) || defined(CONFIG_SPL_BUILD) |
169 | /* Environment not changable */ | |
ea882baf | 170 | set_default_env(NULL); |
05706fdd | 171 | #else |
770605e4 | 172 | bootstage_error(BOOTSTAGE_ID_NET_CHECKSUM); |
ea882baf | 173 | set_default_env("!bad CRC"); |
d259079d | 174 | #endif |
ea882baf | 175 | } else { |
27aafe98 | 176 | env_relocate_spec(); |
05706fdd | 177 | } |
05706fdd | 178 | } |
04a85b3b | 179 | |
7ac2fe2d | 180 | #if defined(CONFIG_AUTO_COMPLETE) && !defined(CONFIG_SPL_BUILD) |
04a85b3b WD |
181 | int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf) |
182 | { | |
560d424b MF |
183 | ENTRY *match; |
184 | int found, idx; | |
04a85b3b | 185 | |
560d424b | 186 | idx = 0; |
04a85b3b WD |
187 | found = 0; |
188 | cmdv[0] = NULL; | |
189 | ||
560d424b MF |
190 | while ((idx = hmatch_r(var, idx, &match, &env_htab))) { |
191 | int vallen = strlen(match->key) + 1; | |
04a85b3b | 192 | |
560d424b | 193 | if (found >= maxv - 2 || bufsz < vallen) |
04a85b3b | 194 | break; |
560d424b | 195 | |
04a85b3b | 196 | cmdv[found++] = buf; |
560d424b MF |
197 | memcpy(buf, match->key, vallen); |
198 | buf += vallen; | |
199 | bufsz -= vallen; | |
04a85b3b WD |
200 | } |
201 | ||
560d424b MF |
202 | qsort(cmdv, found, sizeof(cmdv[0]), strcmp_compar); |
203 | ||
204 | if (idx) | |
205 | cmdv[found++] = "..."; | |
27aafe98 | 206 | |
04a85b3b WD |
207 | cmdv[found] = NULL; |
208 | return found; | |
209 | } | |
210 | #endif |