blob: 220ab979d96cd369064be4d38b8ad4a5c7a32934 [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
Pierre-Jean Texier664689f2019-08-26 13:06:17 +020012#include <compiler.h>
Simon Glassaf95f202019-08-01 09:46:40 -060013#include <stdbool.h>
Simon Glassc7694dd2019-08-01 09:46:46 -060014#include <linux/types.h>
Simon Glassaf95f202019-08-01 09:46:40 -060015
Simon Glass4e9ce8a2019-08-01 09:46:58 -060016struct environment_s;
17
Simon Glassdb197012019-08-01 09:47:04 -060018/* Value for environment validity */
19enum env_valid {
20 ENV_INVALID, /* No valid environment */
21 ENV_VALID, /* First or only environment is valid */
22 ENV_REDUND, /* Redundant environment is valid */
23};
24
Simon Glass02cf9332019-08-01 09:47:05 -060025/** enum env_op - environment callback operation */
26enum env_op {
27 env_op_create,
28 env_op_delete,
29 env_op_overwrite,
30};
31
32/** struct env_clbk_tbl - declares a new callback */
33struct env_clbk_tbl {
34 const char *name; /* Callback name */
35 int (*callback)(const char *name, const char *value, enum env_op op,
36 int flags);
37};
38
39/*
40 * Define a callback that can be associated with variables.
41 * when associated through the ".callbacks" environment variable, the callback
42 * will be executed any time the variable is inserted, overwritten, or deleted.
43 *
44 * For SPL these are silently dropped to reduce code size, since environment
45 * callbacks are not supported with SPL.
46 */
47#ifdef CONFIG_SPL_BUILD
48#define U_BOOT_ENV_CALLBACK(name, callback) \
49 static inline __maybe_unused void _u_boot_env_noop_##name(void) \
50 { \
51 (void)callback; \
52 }
53#else
54#define U_BOOT_ENV_CALLBACK(name, callback) \
55 ll_entry_declare(struct env_clbk_tbl, name, env_clbk) = \
56 {#name, callback}
57#endif
58
Simon Glassd3716dd2019-08-01 09:47:08 -060059/** enum env_redund_flags - Flags for the redundand_environment */
60enum env_redund_flags {
61 ENV_REDUND_OBSOLETE = 0,
62 ENV_REDUND_ACTIVE = 1,
63};
64
Simon Glassaf95f202019-08-01 09:46:40 -060065/**
Simon Glassf1f0ae62019-08-01 09:46:41 -060066 * env_get_id() - Gets a sequence number for the environment
67 *
68 * This value increments every time the environment changes, so can be used an
69 * an indication of this
70 *
71 * @return environment ID
72 */
73int env_get_id(void);
74
75/**
Simon Glass4bfd1f52019-08-01 09:46:43 -060076 * env_init() - Set up the pre-relocation environment
77 *
78 * This locates the environment or uses the default if nothing is available.
79 * This must be called before env_get() will work.
80 *
81 * @return 0 if OK, -ENODEV if no environment drivers are enabled
82 */
83int env_init(void);
84
85/**
Simon Glass3f989e7b2019-08-01 09:46:44 -060086 * env_relocate() - Set up the post-relocation environment
87 *
88 * This loads the environment into RAM so that it can be modified. This is
89 * called after relocation, before the environment is used
90 */
91void env_relocate(void);
92
93/**
Simon Glass7b51b572019-08-01 09:46:52 -060094 * env_get() - Look up the value of an environment variable
95 *
96 * In U-Boot proper this can be called before relocation (which is when the
97 * environment is loaded from storage, i.e. GD_FLG_ENV_READY is 0). In that
98 * case this function calls env_get_f().
99 *
100 * @varname: Variable to look up
101 * @return value of variable, or NULL if not found
102 */
103char *env_get(const char *varname);
104
Patrice Chotard1ac2cb92019-11-25 09:07:36 +0100105/*
106 * Like env_get, but prints an error if envvar isn't defined in the
107 * environment. It always returns what env_get does, so it can be used in
108 * place of env_get without changing error handling otherwise.
109 *
110 * @varname: Variable to look up
111 * @return value of variable, or NULL if not found
112 */
113char *from_env(const char *envvar);
114
Simon Glass7b51b572019-08-01 09:46:52 -0600115/**
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
Marek BehĂșn6dc1e2f2021-10-17 17:36:26 +0200123 * @return number of bytes written into @buf, excluding the terminating
124 * NULL-byte if there was enough space in @buf, and including the
125 * terminating NULL-byte if there wasn't enough space, or -1 if the
126 * variable is not found
Simon Glass3a7d5572019-08-01 09:46:42 -0600127 */
128int env_get_f(const char *name, char *buf, unsigned int len);
129
130/**
Simon Glass6bf6dbe2019-08-01 09:46:49 -0600131 * env_get_yesno() - Read an environment variable as a boolean
132 *
133 * @return 1 if yes/true (Y/y/T/t), -1 if variable does not exist (i.e. default
134 * to true), 0 if otherwise
135 */
136int env_get_yesno(const char *var);
137
138/**
Simon Glass9fb625c2019-08-01 09:46:51 -0600139 * env_set() - set an environment variable
140 *
141 * This sets or deletes the value of an environment variable. For setting the
142 * value the variable is created if it does not already exist.
143 *
144 * @varname: Variable to adjust
145 * @value: Value to set for the variable, or NULL or "" to delete the variable
146 * @return 0 if OK, 1 on error
147 */
148int env_set(const char *varname, const char *value);
149
150/**
Simon Glass9eef56d2019-08-01 09:46:48 -0600151 * env_get_ulong() - Return an environment variable as an integer value
152 *
153 * Most U-Boot environment variables store hex values. For those which store
154 * (e.g.) base-10 integers, this function can be used to read the value.
155 *
156 * @name: Variable to look up
157 * @base: Base to use (e.g. 10 for base 10, 2 for binary)
158 * @default_val: Default value to return if no value is found
159 * @return the value found, or @default_val if none
160 */
161ulong env_get_ulong(const char *name, int base, ulong default_val);
162
163/**
Simon Glass168068f2019-08-01 09:46:47 -0600164 * env_set_ulong() - set an environment variable to an integer
165 *
166 * @varname: Variable to adjust
167 * @value: Value to set for the variable (will be converted to a string)
168 * @return 0 if OK, 1 on error
169 */
170int env_set_ulong(const char *varname, ulong value);
171
172/**
Simon Glasscdbff9f2019-08-01 09:46:50 -0600173 * env_get_hex() - Return an environment variable as a hex value
174 *
175 * Decode an environment as a hex number (it may or may not have a 0x
176 * prefix). If the environment variable cannot be found, or does not start
177 * with hex digits, the default value is returned.
178 *
179 * @varname: Variable to decode
180 * @default_val: Value to return on error
181 */
182ulong env_get_hex(const char *varname, ulong default_val);
183
184/**
Simon Glassc7694dd2019-08-01 09:46:46 -0600185 * env_set_hex() - set an environment variable to a hex value
186 *
187 * @varname: Variable to adjust
188 * @value: Value to set for the variable (will be converted to a hex string)
189 * @return 0 if OK, 1 on error
190 */
191int env_set_hex(const char *varname, ulong value);
192
193/**
194 * env_set_addr - Set an environment variable to an address in hex
195 *
196 * @varname: Environment variable to set
197 * @addr: Value to set it to
198 * @return 0 if ok, 1 on error
199 */
200static inline int env_set_addr(const char *varname, const void *addr)
201{
202 return env_set_hex(varname, (ulong)addr);
203}
204
205/**
Simon Glassaf95f202019-08-01 09:46:40 -0600206 * env_complete() - return an auto-complete for environment variables
207 *
208 * @var: partial name to auto-complete
209 * @maxv: Maximum number of matches to return
210 * @cmdv: Returns a list of possible matches
211 * @maxsz: Size of buffer to use for matches
212 * @buf: Buffer to use for matches
213 * @dollar_comp: non-zero to wrap each match in ${...}
214 * @return number of matches found (in @cmdv)
215 */
216int env_complete(char *var, int maxv, char *cmdv[], int maxsz, char *buf,
217 bool dollar_comp);
218
Simon Glassb79cf1a2019-08-01 09:46:53 -0600219/**
220 * eth_env_get_enetaddr() - Get an ethernet address from the environmnet
221 *
222 * @name: Environment variable to get (e.g. "ethaddr")
223 * @enetaddr: Place to put MAC address (6 bytes)
224 * @return 0 if OK, 1 on error
225 */
226int eth_env_get_enetaddr(const char *name, uint8_t *enetaddr);
227
228/**
229 * eth_env_set_enetaddr() - Set an ethernet address in the environmnet
230 *
231 * @name: Environment variable to set (e.g. "ethaddr")
232 * @enetaddr: Pointer to MAC address to put into the variable (6 bytes)
233 * @return 0 if OK, 1 on error
234 */
235int eth_env_set_enetaddr(const char *name, const uint8_t *enetaddr);
236
Simon Glass03ed9182019-08-01 09:46:55 -0600237/**
238 * env_fix_drivers() - Updates envdriver as per relocation
239 */
240void env_fix_drivers(void);
241
Simon Glass0b9d8a02019-08-01 09:46:56 -0600242/**
243 * env_set_default_vars() - reset variables to their default value
244 *
245 * This resets individual variables to their value in the default environment
246 *
247 * @nvars: Number of variables to set/reset
248 * @vars: List of variables to set/reset
249 * @flags: Flags controlling matching (H_... - see search.h)
250 */
251int env_set_default_vars(int nvars, char *const vars[], int flags);
252
Simon Glass4be490a2019-08-01 09:46:57 -0600253/**
254 * env_load() - Load the environment from storage
255 *
256 * @return 0 if OK, -ve on error
257 */
258int env_load(void);
259
260/**
Patrick Delaunay0115dd32020-07-28 11:51:20 +0200261 * env_reload() - Re-Load the environment from current storage
262 *
263 * @return 0 if OK, -ve on error
264 */
265int env_reload(void);
266
267/**
Simon Glass4be490a2019-08-01 09:46:57 -0600268 * env_save() - Save the environment to storage
269 *
270 * @return 0 if OK, -ve on error
271 */
272int env_save(void);
273
274/**
275 * env_erase() - Erase the environment on storage
276 *
277 * @return 0 if OK, -ve on error
278 */
279int env_erase(void);
280
Simon Glass4e9ce8a2019-08-01 09:46:58 -0600281/**
Patrick Delaunaya97d22e2020-07-28 11:51:21 +0200282 * env_select() - Select the environment storage
283 *
284 * @return 0 if OK, -ve on error
285 */
286int env_select(const char *name);
287
288/**
Simon Glass4e9ce8a2019-08-01 09:46:58 -0600289 * env_import() - Import from a binary representation into hash table
290 *
291 * This imports the environment from a buffer. The format for each variable is
292 * var=value\0 with a double \0 at the end of the buffer.
293 *
294 * @buf: Buffer containing the environment (struct environemnt_s *)
295 * @check: non-zero to check the CRC at the start of the environment, 0 to
296 * ignore it
Marek Vasut890feec2020-07-07 20:51:35 +0200297 * @flags: Flags controlling matching (H_... - see search.h)
Simon Glass4e9ce8a2019-08-01 09:46:58 -0600298 * @return 0 if imported successfully, -ENOMSG if the CRC was bad, -EIO if
299 * something else went wrong
300 */
Marek Vasut890feec2020-07-07 20:51:35 +0200301int env_import(const char *buf, int check, int flags);
Simon Glass4e9ce8a2019-08-01 09:46:58 -0600302
303/**
304 * env_export() - Export the environment to a buffer
305 *
306 * Export from hash table into binary representation
307 *
308 * @env_out: Buffer to contain the environment (must be large enough!)
309 * @return 0 if OK, 1 on error
310 */
311int env_export(struct environment_s *env_out);
312
313/**
Heiko Schocher12295332020-10-10 10:28:04 +0200314 * env_check_redund() - check the two redundant environments
315 * and find out, which is the valid one.
316 *
317 * @buf1: First environment (struct environemnt_s *)
318 * @buf1_read_fail: 0 if buf1 is valid, non-zero if invalid
319 * @buf2: Second environment (struct environemnt_s *)
320 * @buf2_read_fail: 0 if buf2 is valid, non-zero if invalid
321 * @return 0 if OK,
322 * -EIO if no environment is valid,
Heiko Schocher12295332020-10-10 10:28:04 +0200323 * -ENOMSG if the CRC was bad
324 */
325
326int env_check_redund(const char *buf1, int buf1_read_fail,
327 const char *buf2, int buf2_read_fail);
328
329/**
Simon Glass4e9ce8a2019-08-01 09:46:58 -0600330 * env_import_redund() - Select and import one of two redundant environments
331 *
332 * @buf1: First environment (struct environemnt_s *)
333 * @buf1_read_fail: 0 if buf1 is valid, non-zero if invalid
334 * @buf2: Second environment (struct environemnt_s *)
335 * @buf2_read_fail: 0 if buf2 is valid, non-zero if invalid
Marek Vasut890feec2020-07-07 20:51:35 +0200336 * @flags: Flags controlling matching (H_... - see search.h)
Simon Glass4e9ce8a2019-08-01 09:46:58 -0600337 * @return 0 if OK, -EIO if no environment is valid, -ENOMSG if the CRC was bad
338 */
339int env_import_redund(const char *buf1, int buf1_read_fail,
Marek Vasut890feec2020-07-07 20:51:35 +0200340 const char *buf2, int buf2_read_fail,
341 int flags);
Simon Glass4e9ce8a2019-08-01 09:46:58 -0600342
Simon Glass0ac7d722019-08-01 09:47:00 -0600343/**
344 * env_get_default() - Look up a variable from the default environment
345 *
346 * @name: Variable to look up
347 * @return value if found, NULL if not found in default environment
348 */
349char *env_get_default(const char *name);
350
351/* [re]set to the default environment */
352void env_set_default(const char *s, int flags);
353
Simon Glassb5f449e2019-08-01 09:47:01 -0600354/**
Simon Glassc62f62b2019-08-01 09:47:02 -0600355 * env_reloc() - Relocate the 'env' sub-commands
356 *
357 * This is used for those unfortunate archs with crappy toolchains
358 */
359void env_reloc(void);
Rasmus Villemoes95fd9772021-04-21 11:06:54 +0200360
361
362/**
363 * env_import_fdt() - Import environment values from device tree blob
364 *
365 * This uses the value of the environment variable "env_fdt_path" as a
366 * path to an fdt node, whose property/value pairs are added to the
367 * environment.
368 */
369#ifdef CONFIG_ENV_IMPORT_FDT
370void env_import_fdt(void);
371#else
372static inline void env_import_fdt(void) {}
373#endif
374
Simon Glassaf95f202019-08-01 09:46:40 -0600375#endif