blob: a74a261337e640024f1a6ebac5eb4d9014a37d2b [file] [log] [blame]
Simon Glassaf95f202019-08-01 09:46:40 -06001/* SPDX-License-Identifier: GPL-2.0+ */
2/*
Simon Glassdb197012019-08-01 09:47:04 -06003 * Common environment functions and definitions
Simon Glassaf95f202019-08-01 09:46:40 -06004 *
5 * (C) Copyright 2000-2009
6 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
7 */
8
9#ifndef __ENV_H
10#define __ENV_H
11
12#include <stdbool.h>
Simon Glassc7694dd2019-08-01 09:46:46 -060013#include <linux/types.h>
Simon Glassaf95f202019-08-01 09:46:40 -060014
Simon Glass4e9ce8a2019-08-01 09:46:58 -060015struct environment_s;
16
Simon Glassdb197012019-08-01 09:47:04 -060017/* Value for environment validity */
18enum env_valid {
19 ENV_INVALID, /* No valid environment */
20 ENV_VALID, /* First or only environment is valid */
21 ENV_REDUND, /* Redundant environment is valid */
22};
23
Simon Glass02cf9332019-08-01 09:47:05 -060024/** enum env_op - environment callback operation */
25enum env_op {
26 env_op_create,
27 env_op_delete,
28 env_op_overwrite,
29};
30
31/** struct env_clbk_tbl - declares a new callback */
32struct env_clbk_tbl {
33 const char *name; /* Callback name */
34 int (*callback)(const char *name, const char *value, enum env_op op,
35 int flags);
36};
37
38/*
39 * Define a callback that can be associated with variables.
40 * when associated through the ".callbacks" environment variable, the callback
41 * will be executed any time the variable is inserted, overwritten, or deleted.
42 *
43 * For SPL these are silently dropped to reduce code size, since environment
44 * callbacks are not supported with SPL.
45 */
46#ifdef CONFIG_SPL_BUILD
47#define U_BOOT_ENV_CALLBACK(name, callback) \
48 static inline __maybe_unused void _u_boot_env_noop_##name(void) \
49 { \
50 (void)callback; \
51 }
52#else
53#define U_BOOT_ENV_CALLBACK(name, callback) \
54 ll_entry_declare(struct env_clbk_tbl, name, env_clbk) = \
55 {#name, callback}
56#endif
57
Simon Glassd3716dd2019-08-01 09:47:08 -060058/** enum env_redund_flags - Flags for the redundand_environment */
59enum env_redund_flags {
60 ENV_REDUND_OBSOLETE = 0,
61 ENV_REDUND_ACTIVE = 1,
62};
63
Simon Glassaf95f202019-08-01 09:46:40 -060064/**
Simon Glassf1f0ae62019-08-01 09:46:41 -060065 * env_get_id() - Gets a sequence number for the environment
66 *
67 * This value increments every time the environment changes, so can be used an
68 * an indication of this
69 *
70 * @return environment ID
71 */
72int env_get_id(void);
73
74/**
Simon Glass4bfd1f52019-08-01 09:46:43 -060075 * env_init() - Set up the pre-relocation environment
76 *
77 * This locates the environment or uses the default if nothing is available.
78 * This must be called before env_get() will work.
79 *
80 * @return 0 if OK, -ENODEV if no environment drivers are enabled
81 */
82int env_init(void);
83
84/**
Simon Glass3f989e7b2019-08-01 09:46:44 -060085 * env_relocate() - Set up the post-relocation environment
86 *
87 * This loads the environment into RAM so that it can be modified. This is
88 * called after relocation, before the environment is used
89 */
90void env_relocate(void);
91
92/**
Simon Glassb9ca02c2019-08-01 09:46:45 -060093 * env_match() - Match a name / name=value pair
94 *
95 * This is used prior to relocation for finding envrionment variables
96 *
97 * @name: A simple 'name', or a 'name=value' pair.
98 * @index: The environment index for a 'name2=value2' pair.
99 * @return index for the value if the names match, else -1.
100 */
101int env_match(unsigned char *name, int index);
102
103/**
Simon Glass7b51b572019-08-01 09:46:52 -0600104 * env_get() - Look up the value of an environment variable
105 *
106 * In U-Boot proper this can be called before relocation (which is when the
107 * environment is loaded from storage, i.e. GD_FLG_ENV_READY is 0). In that
108 * case this function calls env_get_f().
109 *
110 * @varname: Variable to look up
111 * @return value of variable, or NULL if not found
112 */
113char *env_get(const char *varname);
114
115/**
Simon Glass3a7d5572019-08-01 09:46:42 -0600116 * env_get_f() - Look up the value of an environment variable (early)
117 *
118 * This function is called from env_get() if the environment has not been
119 * loaded yet (GD_FLG_ENV_READY flag is 0). Some environment locations will
120 * support reading the value (slowly) and some will not.
121 *
122 * @varname: Variable to look up
123 * @return value of variable, or NULL if not found
124 */
125int env_get_f(const char *name, char *buf, unsigned int len);
126
127/**
Simon Glass6bf6dbe2019-08-01 09:46:49 -0600128 * env_get_yesno() - Read an environment variable as a boolean
129 *
130 * @return 1 if yes/true (Y/y/T/t), -1 if variable does not exist (i.e. default
131 * to true), 0 if otherwise
132 */
133int env_get_yesno(const char *var);
134
135/**
Simon Glass9fb625c2019-08-01 09:46:51 -0600136 * env_set() - set an environment variable
137 *
138 * This sets or deletes the value of an environment variable. For setting the
139 * value the variable is created if it does not already exist.
140 *
141 * @varname: Variable to adjust
142 * @value: Value to set for the variable, or NULL or "" to delete the variable
143 * @return 0 if OK, 1 on error
144 */
145int env_set(const char *varname, const char *value);
146
147/**
Simon Glass9eef56d2019-08-01 09:46:48 -0600148 * env_get_ulong() - Return an environment variable as an integer value
149 *
150 * Most U-Boot environment variables store hex values. For those which store
151 * (e.g.) base-10 integers, this function can be used to read the value.
152 *
153 * @name: Variable to look up
154 * @base: Base to use (e.g. 10 for base 10, 2 for binary)
155 * @default_val: Default value to return if no value is found
156 * @return the value found, or @default_val if none
157 */
158ulong env_get_ulong(const char *name, int base, ulong default_val);
159
160/**
Simon Glass168068f2019-08-01 09:46:47 -0600161 * env_set_ulong() - set an environment variable to an integer
162 *
163 * @varname: Variable to adjust
164 * @value: Value to set for the variable (will be converted to a string)
165 * @return 0 if OK, 1 on error
166 */
167int env_set_ulong(const char *varname, ulong value);
168
169/**
Simon Glasscdbff9f2019-08-01 09:46:50 -0600170 * env_get_hex() - Return an environment variable as a hex value
171 *
172 * Decode an environment as a hex number (it may or may not have a 0x
173 * prefix). If the environment variable cannot be found, or does not start
174 * with hex digits, the default value is returned.
175 *
176 * @varname: Variable to decode
177 * @default_val: Value to return on error
178 */
179ulong env_get_hex(const char *varname, ulong default_val);
180
181/**
Simon Glassc7694dd2019-08-01 09:46:46 -0600182 * env_set_hex() - set an environment variable to a hex value
183 *
184 * @varname: Variable to adjust
185 * @value: Value to set for the variable (will be converted to a hex string)
186 * @return 0 if OK, 1 on error
187 */
188int env_set_hex(const char *varname, ulong value);
189
190/**
191 * env_set_addr - Set an environment variable to an address in hex
192 *
193 * @varname: Environment variable to set
194 * @addr: Value to set it to
195 * @return 0 if ok, 1 on error
196 */
197static inline int env_set_addr(const char *varname, const void *addr)
198{
199 return env_set_hex(varname, (ulong)addr);
200}
201
202/**
Simon Glassaf95f202019-08-01 09:46:40 -0600203 * env_complete() - return an auto-complete for environment variables
204 *
205 * @var: partial name to auto-complete
206 * @maxv: Maximum number of matches to return
207 * @cmdv: Returns a list of possible matches
208 * @maxsz: Size of buffer to use for matches
209 * @buf: Buffer to use for matches
210 * @dollar_comp: non-zero to wrap each match in ${...}
211 * @return number of matches found (in @cmdv)
212 */
213int env_complete(char *var, int maxv, char *cmdv[], int maxsz, char *buf,
214 bool dollar_comp);
215
Simon Glassb79cf1a2019-08-01 09:46:53 -0600216/**
217 * eth_env_get_enetaddr() - Get an ethernet address from the environmnet
218 *
219 * @name: Environment variable to get (e.g. "ethaddr")
220 * @enetaddr: Place to put MAC address (6 bytes)
221 * @return 0 if OK, 1 on error
222 */
223int eth_env_get_enetaddr(const char *name, uint8_t *enetaddr);
224
225/**
226 * eth_env_set_enetaddr() - Set an ethernet address in the environmnet
227 *
228 * @name: Environment variable to set (e.g. "ethaddr")
229 * @enetaddr: Pointer to MAC address to put into the variable (6 bytes)
230 * @return 0 if OK, 1 on error
231 */
232int eth_env_set_enetaddr(const char *name, const uint8_t *enetaddr);
233
Simon Glass03ed9182019-08-01 09:46:55 -0600234/**
235 * env_fix_drivers() - Updates envdriver as per relocation
236 */
237void env_fix_drivers(void);
238
Simon Glass0b9d8a02019-08-01 09:46:56 -0600239/**
240 * env_set_default_vars() - reset variables to their default value
241 *
242 * This resets individual variables to their value in the default environment
243 *
244 * @nvars: Number of variables to set/reset
245 * @vars: List of variables to set/reset
246 * @flags: Flags controlling matching (H_... - see search.h)
247 */
248int env_set_default_vars(int nvars, char *const vars[], int flags);
249
Simon Glass4be490a2019-08-01 09:46:57 -0600250/**
251 * env_load() - Load the environment from storage
252 *
253 * @return 0 if OK, -ve on error
254 */
255int env_load(void);
256
257/**
258 * env_save() - Save the environment to storage
259 *
260 * @return 0 if OK, -ve on error
261 */
262int env_save(void);
263
264/**
265 * env_erase() - Erase the environment on storage
266 *
267 * @return 0 if OK, -ve on error
268 */
269int env_erase(void);
270
Simon Glass4e9ce8a2019-08-01 09:46:58 -0600271/**
272 * env_import() - Import from a binary representation into hash table
273 *
274 * This imports the environment from a buffer. The format for each variable is
275 * var=value\0 with a double \0 at the end of the buffer.
276 *
277 * @buf: Buffer containing the environment (struct environemnt_s *)
278 * @check: non-zero to check the CRC at the start of the environment, 0 to
279 * ignore it
280 * @return 0 if imported successfully, -ENOMSG if the CRC was bad, -EIO if
281 * something else went wrong
282 */
283int env_import(const char *buf, int check);
284
285/**
286 * env_export() - Export the environment to a buffer
287 *
288 * Export from hash table into binary representation
289 *
290 * @env_out: Buffer to contain the environment (must be large enough!)
291 * @return 0 if OK, 1 on error
292 */
293int env_export(struct environment_s *env_out);
294
295/**
296 * env_import_redund() - Select and import one of two redundant environments
297 *
298 * @buf1: First environment (struct environemnt_s *)
299 * @buf1_read_fail: 0 if buf1 is valid, non-zero if invalid
300 * @buf2: Second environment (struct environemnt_s *)
301 * @buf2_read_fail: 0 if buf2 is valid, non-zero if invalid
302 * @return 0 if OK, -EIO if no environment is valid, -ENOMSG if the CRC was bad
303 */
304int env_import_redund(const char *buf1, int buf1_read_fail,
305 const char *buf2, int buf2_read_fail);
306
Simon Glass0ac7d722019-08-01 09:47:00 -0600307/**
308 * env_get_default() - Look up a variable from the default environment
309 *
310 * @name: Variable to look up
311 * @return value if found, NULL if not found in default environment
312 */
313char *env_get_default(const char *name);
314
315/* [re]set to the default environment */
316void env_set_default(const char *s, int flags);
317
Simon Glassb5f449e2019-08-01 09:47:01 -0600318/**
319 * env_get_char() - Get a character from the early environment
320 *
321 * This reads from the pre-relocation environment
322 *
323 * @index: Index of character to read (0 = first)
324 * @return character read, or -ve on error
325 */
326int env_get_char(int index);
327
Simon Glassc62f62b2019-08-01 09:47:02 -0600328/**
329 * env_reloc() - Relocate the 'env' sub-commands
330 *
331 * This is used for those unfortunate archs with crappy toolchains
332 */
333void env_reloc(void);
334
Simon Glassaf95f202019-08-01 09:46:40 -0600335#endif