]>
Commit | Line | Data |
---|---|---|
05706fdd | 1 | /* |
ea882baf | 2 | * (C) Copyright 2000-2010 |
05706fdd WD |
3 | * Wolfgang Denk, DENX Software Engineering, [email protected]. |
4 | * | |
5 | * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com> | |
6 | * Andreas Heppel <[email protected]> | |
ea882baf | 7 | * |
3765b3e7 | 8 | * SPDX-License-Identifier: GPL-2.0+ |
05706fdd WD |
9 | */ |
10 | ||
11 | #include <common.h> | |
12 | #include <command.h> | |
13 | #include <environment.h> | |
05706fdd | 14 | #include <linux/stddef.h> |
ea882baf WD |
15 | #include <search.h> |
16 | #include <errno.h> | |
05706fdd WD |
17 | #include <malloc.h> |
18 | ||
d87080b7 WD |
19 | DECLARE_GLOBAL_DATA_PTR; |
20 | ||
05706fdd WD |
21 | /************************************************************************ |
22 | * Default settings to be used when no valid environment is found | |
23 | */ | |
ddd8418f | 24 | #include <env_default.h> |
05706fdd | 25 | |
c5983592 | 26 | struct hsearch_data env_htab = { |
2598090b | 27 | .change_ok = env_flags_validate, |
c5983592 | 28 | }; |
2eb1573f | 29 | |
bf95df44 IG |
30 | static uchar __env_get_char_spec(int index) |
31 | { | |
32 | return *((uchar *)(gd->env_addr + index)); | |
33 | } | |
34 | uchar env_get_char_spec(int) | |
35 | __attribute__((weak, alias("__env_get_char_spec"))); | |
36 | ||
27aafe98 | 37 | static uchar env_get_char_init(int index) |
05706fdd | 38 | { |
05706fdd WD |
39 | /* if crc was bad, use the default environment */ |
40 | if (gd->env_valid) | |
27aafe98 | 41 | return env_get_char_spec(index); |
ea882baf | 42 | else |
27aafe98 | 43 | return default_environment[index]; |
05706fdd WD |
44 | } |
45 | ||
27aafe98 | 46 | uchar env_get_char_memory(int index) |
05706fdd | 47 | { |
ea882baf | 48 | return *env_get_addr(index); |
05706fdd WD |
49 | } |
50 | ||
27aafe98 | 51 | uchar env_get_char(int index) |
b502611b | 52 | { |
b502611b JT |
53 | /* if relocated to RAM */ |
54 | if (gd->flags & GD_FLG_RELOC) | |
27aafe98 | 55 | return env_get_char_memory(index); |
b502611b | 56 | else |
27aafe98 | 57 | return env_get_char_init(index); |
b502611b JT |
58 | } |
59 | ||
27aafe98 | 60 | const uchar *env_get_addr(int index) |
05706fdd | 61 | { |
ea882baf WD |
62 | if (gd->env_valid) |
63 | return (uchar *)(gd->env_addr + index); | |
64 | else | |
65 | return &default_environment[index]; | |
05706fdd WD |
66 | } |
67 | ||
ec8a252c JH |
68 | /* |
69 | * Read an environment variable as a boolean | |
70 | * Return -1 if variable does not exist (default to true) | |
71 | */ | |
72 | int getenv_yesno(const char *var) | |
73 | { | |
74 | char *s = getenv(var); | |
75 | ||
76 | if (s == NULL) | |
77 | return -1; | |
78 | return (*s == '1' || *s == 'y' || *s == 'Y' || *s == 't' || *s == 'T') ? | |
79 | 1 : 0; | |
80 | } | |
81 | ||
267541f7 JH |
82 | /* |
83 | * Look up the variable from the default environment | |
84 | */ | |
85 | char *getenv_default(const char *name) | |
86 | { | |
87 | char *ret_val; | |
88 | unsigned long really_valid = gd->env_valid; | |
89 | unsigned long real_gd_flags = gd->flags; | |
90 | ||
91 | /* Pretend that the image is bad. */ | |
92 | gd->flags &= ~GD_FLG_ENV_READY; | |
93 | gd->env_valid = 0; | |
94 | ret_val = getenv(name); | |
95 | gd->env_valid = really_valid; | |
96 | gd->flags = real_gd_flags; | |
97 | return ret_val; | |
98 | } | |
99 | ||
ea882baf | 100 | void set_default_env(const char *s) |
5bb12dbd | 101 | { |
c4e0057f JH |
102 | int flags = 0; |
103 | ||
5bb12dbd | 104 | if (sizeof(default_environment) > ENV_SIZE) { |
ea882baf | 105 | puts("*** Error - default environment is too large\n\n"); |
5bb12dbd HW |
106 | return; |
107 | } | |
108 | ||
ea882baf WD |
109 | if (s) { |
110 | if (*s == '!') { | |
111 | printf("*** Warning - %s, " | |
112 | "using default environment\n\n", | |
27aafe98 | 113 | s + 1); |
ea882baf | 114 | } else { |
c4e0057f | 115 | flags = H_INTERACTIVE; |
ea882baf WD |
116 | puts(s); |
117 | } | |
118 | } else { | |
119 | puts("Using default environment\n\n"); | |
120 | } | |
121 | ||
2eb1573f | 122 | if (himport_r(&env_htab, (char *)default_environment, |
c4e0057f JH |
123 | sizeof(default_environment), '\0', flags, |
124 | 0, NULL) == 0) | |
ea882baf | 125 | error("Environment import failed: errno = %d\n", errno); |
27aafe98 | 126 | |
ea882baf | 127 | gd->flags |= GD_FLG_ENV_READY; |
5bb12dbd HW |
128 | } |
129 | ||
b64b7c3d GF |
130 | |
131 | /* [re]set individual variables to their value in the default environment */ | |
132 | int set_default_vars(int nvars, char * const vars[]) | |
133 | { | |
134 | /* | |
135 | * Special use-case: import from default environment | |
136 | * (and use \0 as a separator) | |
137 | */ | |
138 | return himport_r(&env_htab, (const char *)default_environment, | |
c4e0057f JH |
139 | sizeof(default_environment), '\0', |
140 | H_NOCLEAR | H_INTERACTIVE, nvars, vars); | |
b64b7c3d GF |
141 | } |
142 | ||
ea882baf WD |
143 | /* |
144 | * Check if CRC is valid and (if yes) import the environment. | |
145 | * Note that "buf" may or may not be aligned. | |
146 | */ | |
147 | int env_import(const char *buf, int check) | |
05706fdd | 148 | { |
ea882baf | 149 | env_t *ep = (env_t *)buf; |
05706fdd | 150 | |
ea882baf WD |
151 | if (check) { |
152 | uint32_t crc; | |
153 | ||
154 | memcpy(&crc, &ep->crc, sizeof(crc)); | |
155 | ||
156 | if (crc32(0, ep->data, ENV_SIZE) != crc) { | |
157 | set_default_env("!bad CRC"); | |
158 | return 0; | |
159 | } | |
160 | } | |
161 | ||
348b1f1c | 162 | if (himport_r(&env_htab, (char *)ep->data, ENV_SIZE, '\0', 0, |
c4e0057f | 163 | 0, NULL)) { |
ea882baf WD |
164 | gd->flags |= GD_FLG_ENV_READY; |
165 | return 1; | |
166 | } | |
05706fdd | 167 | |
ea882baf WD |
168 | error("Cannot import environment: errno = %d\n", errno); |
169 | ||
170 | set_default_env("!import failed"); | |
171 | ||
172 | return 0; | |
173 | } | |
174 | ||
27aafe98 | 175 | void env_relocate(void) |
ea882baf | 176 | { |
2e5167cc | 177 | #if defined(CONFIG_NEEDS_MANUAL_RELOC) |
60f7da1f | 178 | env_reloc(); |
7afcf3a5 | 179 | env_htab.change_ok += gd->reloc_off; |
60f7da1f | 180 | #endif |
05706fdd | 181 | if (gd->env_valid == 0) { |
7ac2fe2d IY |
182 | #if defined(CONFIG_ENV_IS_NOWHERE) || defined(CONFIG_SPL_BUILD) |
183 | /* Environment not changable */ | |
ea882baf | 184 | set_default_env(NULL); |
05706fdd | 185 | #else |
770605e4 | 186 | bootstage_error(BOOTSTAGE_ID_NET_CHECKSUM); |
ea882baf | 187 | set_default_env("!bad CRC"); |
d259079d | 188 | #endif |
ea882baf | 189 | } else { |
27aafe98 | 190 | env_relocate_spec(); |
05706fdd | 191 | } |
05706fdd | 192 | } |
04a85b3b | 193 | |
7ac2fe2d | 194 | #if defined(CONFIG_AUTO_COMPLETE) && !defined(CONFIG_SPL_BUILD) |
04a85b3b WD |
195 | int env_complete(char *var, int maxv, char *cmdv[], int bufsz, char *buf) |
196 | { | |
560d424b MF |
197 | ENTRY *match; |
198 | int found, idx; | |
04a85b3b | 199 | |
560d424b | 200 | idx = 0; |
04a85b3b WD |
201 | found = 0; |
202 | cmdv[0] = NULL; | |
203 | ||
560d424b MF |
204 | while ((idx = hmatch_r(var, idx, &match, &env_htab))) { |
205 | int vallen = strlen(match->key) + 1; | |
04a85b3b | 206 | |
560d424b | 207 | if (found >= maxv - 2 || bufsz < vallen) |
04a85b3b | 208 | break; |
560d424b | 209 | |
04a85b3b | 210 | cmdv[found++] = buf; |
560d424b MF |
211 | memcpy(buf, match->key, vallen); |
212 | buf += vallen; | |
213 | bufsz -= vallen; | |
04a85b3b WD |
214 | } |
215 | ||
560d424b MF |
216 | qsort(cmdv, found, sizeof(cmdv[0]), strcmp_compar); |
217 | ||
218 | if (idx) | |
219 | cmdv[found++] = "..."; | |
27aafe98 | 220 | |
04a85b3b WD |
221 | cmdv[found] = NULL; |
222 | return found; | |
223 | } | |
224 | #endif |