]> Git Repo - u-boot.git/blame - include/env.h
Merge tag 'efi-2023-01-rc3' of https://source.denx.de/u-boot/custodians/u-boot-efi
[u-boot.git] / include / env.h
CommitLineData
af95f206
SG
1/* SPDX-License-Identifier: GPL-2.0+ */
2/*
db197010 3 * Common environment functions and definitions
af95f206
SG
4 *
5 * (C) Copyright 2000-2009
6 * Wolfgang Denk, DENX Software Engineering, [email protected].
7 */
8
9#ifndef __ENV_H
10#define __ENV_H
11
664689f1 12#include <compiler.h>
af95f206 13#include <stdbool.h>
c7694dd4 14#include <linux/types.h>
af95f206 15
4e9ce8a6
SG
16struct environment_s;
17
db197010
SG
18/* 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
02cf9334
SG
25/** 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
d3716dd6
SG
59/** 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
f1f0ae6a
SG
65/**
66 * 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 *
185f812c 71 * Return: environment ID
f1f0ae6a
SG
72 */
73int env_get_id(void);
74
4bfd1f5d
SG
75/**
76 * 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 *
185f812c 81 * Return: 0 if OK, -ENODEV if no environment drivers are enabled
4bfd1f5d
SG
82 */
83int env_init(void);
84
3f989e7b
SG
85/**
86 * 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
7b51b576
SG
93/**
94 * 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
185f812c 101 * Return: value of variable, or NULL if not found
7b51b576
SG
102 */
103char *env_get(const char *varname);
104
1ac2cb97
PC
105/*
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
185f812c 111 * Return: value of variable, or NULL if not found
1ac2cb97
PC
112 */
113char *from_env(const char *envvar);
114
3a7d5571
SG
115/**
116 * 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
185f812c 123 * Return: actual length of the variable value excluding the terminating
3112ce0c 124 * NULL-byte, or -1 if the variable is not found
3a7d5571
SG
125 */
126int env_get_f(const char *name, char *buf, unsigned int len);
127
6bf6dbee
SG
128/**
129 * env_get_yesno() - Read an environment variable as a boolean
130 *
185f812c 131 * Return: 1 if yes/true (Y/y/T/t), -1 if variable does not exist (i.e. default
6bf6dbee
SG
132 * to true), 0 if otherwise
133 */
134int env_get_yesno(const char *var);
135
78398652
SG
136/**
137 * env_get_autostart() - Check if autostart is enabled
138 *
185f812c 139 * Return: true if the "autostart" env var exists and is set to "yes"
78398652
SG
140 */
141bool env_get_autostart(void);
142
9fb625ce
SG
143/**
144 * env_set() - set an environment variable
145 *
146 * This sets or deletes the value of an environment variable. For setting the
147 * value the variable is created if it does not already exist.
148 *
149 * @varname: Variable to adjust
150 * @value: Value to set for the variable, or NULL or "" to delete the variable
185f812c 151 * Return: 0 if OK, 1 on error
9fb625ce
SG
152 */
153int env_set(const char *varname, const char *value);
154
9eef56db
SG
155/**
156 * env_get_ulong() - Return an environment variable as an integer value
157 *
158 * Most U-Boot environment variables store hex values. For those which store
159 * (e.g.) base-10 integers, this function can be used to read the value.
160 *
161 * @name: Variable to look up
162 * @base: Base to use (e.g. 10 for base 10, 2 for binary)
163 * @default_val: Default value to return if no value is found
185f812c 164 * Return: the value found, or @default_val if none
9eef56db
SG
165 */
166ulong env_get_ulong(const char *name, int base, ulong default_val);
167
168068fb
SG
168/**
169 * env_set_ulong() - set an environment variable to an integer
170 *
171 * @varname: Variable to adjust
172 * @value: Value to set for the variable (will be converted to a string)
185f812c 173 * Return: 0 if OK, 1 on error
168068fb
SG
174 */
175int env_set_ulong(const char *varname, ulong value);
176
cdbff9fc
SG
177/**
178 * env_get_hex() - Return an environment variable as a hex value
179 *
180 * Decode an environment as a hex number (it may or may not have a 0x
181 * prefix). If the environment variable cannot be found, or does not start
182 * with hex digits, the default value is returned.
183 *
184 * @varname: Variable to decode
185 * @default_val: Value to return on error
186 */
187ulong env_get_hex(const char *varname, ulong default_val);
188
c7694dd4
SG
189/**
190 * env_set_hex() - set an environment variable to a hex value
191 *
192 * @varname: Variable to adjust
193 * @value: Value to set for the variable (will be converted to a hex string)
185f812c 194 * Return: 0 if OK, 1 on error
c7694dd4
SG
195 */
196int env_set_hex(const char *varname, ulong value);
197
198/**
199 * env_set_addr - Set an environment variable to an address in hex
200 *
201 * @varname: Environment variable to set
202 * @addr: Value to set it to
185f812c 203 * Return: 0 if ok, 1 on error
c7694dd4
SG
204 */
205static inline int env_set_addr(const char *varname, const void *addr)
206{
207 return env_set_hex(varname, (ulong)addr);
208}
209
af95f206
SG
210/**
211 * env_complete() - return an auto-complete for environment variables
212 *
213 * @var: partial name to auto-complete
214 * @maxv: Maximum number of matches to return
215 * @cmdv: Returns a list of possible matches
216 * @maxsz: Size of buffer to use for matches
217 * @buf: Buffer to use for matches
218 * @dollar_comp: non-zero to wrap each match in ${...}
185f812c 219 * Return: number of matches found (in @cmdv)
af95f206
SG
220 */
221int env_complete(char *var, int maxv, char *cmdv[], int maxsz, char *buf,
222 bool dollar_comp);
223
b79cf1af
SG
224/**
225 * eth_env_get_enetaddr() - Get an ethernet address from the environmnet
226 *
227 * @name: Environment variable to get (e.g. "ethaddr")
228 * @enetaddr: Place to put MAC address (6 bytes)
185f812c 229 * Return: 0 if OK, 1 on error
b79cf1af
SG
230 */
231int eth_env_get_enetaddr(const char *name, uint8_t *enetaddr);
232
233/**
234 * eth_env_set_enetaddr() - Set an ethernet address in the environmnet
235 *
236 * @name: Environment variable to set (e.g. "ethaddr")
237 * @enetaddr: Pointer to MAC address to put into the variable (6 bytes)
185f812c 238 * Return: 0 if OK, 1 on error
b79cf1af
SG
239 */
240int eth_env_set_enetaddr(const char *name, const uint8_t *enetaddr);
241
03ed9188
SG
242/**
243 * env_fix_drivers() - Updates envdriver as per relocation
244 */
245void env_fix_drivers(void);
246
0b9d8a05
SG
247/**
248 * env_set_default_vars() - reset variables to their default value
249 *
250 * This resets individual variables to their value in the default environment
251 *
252 * @nvars: Number of variables to set/reset
253 * @vars: List of variables to set/reset
254 * @flags: Flags controlling matching (H_... - see search.h)
255 */
256int env_set_default_vars(int nvars, char *const vars[], int flags);
257
4be490ae
SG
258/**
259 * env_load() - Load the environment from storage
260 *
185f812c 261 * Return: 0 if OK, -ve on error
4be490ae
SG
262 */
263int env_load(void);
264
0115dd3a
PD
265/**
266 * env_reload() - Re-Load the environment from current storage
267 *
185f812c 268 * Return: 0 if OK, -ve on error
0115dd3a
PD
269 */
270int env_reload(void);
271
4be490ae
SG
272/**
273 * env_save() - Save the environment to storage
274 *
185f812c 275 * Return: 0 if OK, -ve on error
4be490ae
SG
276 */
277int env_save(void);
278
279/**
280 * env_erase() - Erase the environment on storage
281 *
185f812c 282 * Return: 0 if OK, -ve on error
4be490ae
SG
283 */
284int env_erase(void);
285
a97d22eb
PD
286/**
287 * env_select() - Select the environment storage
288 *
185f812c 289 * Return: 0 if OK, -ve on error
a97d22eb
PD
290 */
291int env_select(const char *name);
292
4e9ce8a6
SG
293/**
294 * env_import() - Import from a binary representation into hash table
295 *
296 * This imports the environment from a buffer. The format for each variable is
297 * var=value\0 with a double \0 at the end of the buffer.
298 *
299 * @buf: Buffer containing the environment (struct environemnt_s *)
300 * @check: non-zero to check the CRC at the start of the environment, 0 to
301 * ignore it
890feeca 302 * @flags: Flags controlling matching (H_... - see search.h)
185f812c 303 * Return: 0 if imported successfully, -ENOMSG if the CRC was bad, -EIO if
4e9ce8a6
SG
304 * something else went wrong
305 */
890feeca 306int env_import(const char *buf, int check, int flags);
4e9ce8a6
SG
307
308/**
309 * env_export() - Export the environment to a buffer
310 *
311 * Export from hash table into binary representation
312 *
313 * @env_out: Buffer to contain the environment (must be large enough!)
185f812c 314 * Return: 0 if OK, 1 on error
4e9ce8a6
SG
315 */
316int env_export(struct environment_s *env_out);
317
1229533a
HS
318/**
319 * env_check_redund() - check the two redundant environments
320 * and find out, which is the valid one.
321 *
322 * @buf1: First environment (struct environemnt_s *)
323 * @buf1_read_fail: 0 if buf1 is valid, non-zero if invalid
324 * @buf2: Second environment (struct environemnt_s *)
325 * @buf2_read_fail: 0 if buf2 is valid, non-zero if invalid
185f812c 326 * Return: 0 if OK,
1229533a 327 * -EIO if no environment is valid,
1229533a
HS
328 * -ENOMSG if the CRC was bad
329 */
330
331int env_check_redund(const char *buf1, int buf1_read_fail,
332 const char *buf2, int buf2_read_fail);
333
4e9ce8a6
SG
334/**
335 * env_import_redund() - Select and import one of two redundant environments
336 *
337 * @buf1: First environment (struct environemnt_s *)
338 * @buf1_read_fail: 0 if buf1 is valid, non-zero if invalid
339 * @buf2: Second environment (struct environemnt_s *)
340 * @buf2_read_fail: 0 if buf2 is valid, non-zero if invalid
890feeca 341 * @flags: Flags controlling matching (H_... - see search.h)
185f812c 342 * Return: 0 if OK, -EIO if no environment is valid, -ENOMSG if the CRC was bad
4e9ce8a6
SG
343 */
344int env_import_redund(const char *buf1, int buf1_read_fail,
890feeca
MV
345 const char *buf2, int buf2_read_fail,
346 int flags);
4e9ce8a6 347
0ac7d722
SG
348/**
349 * env_get_default() - Look up a variable from the default environment
350 *
351 * @name: Variable to look up
185f812c 352 * Return: value if found, NULL if not found in default environment
0ac7d722
SG
353 */
354char *env_get_default(const char *name);
355
356/* [re]set to the default environment */
357void env_set_default(const char *s, int flags);
358
c62f62b0
SG
359/**
360 * env_reloc() - Relocate the 'env' sub-commands
361 *
362 * This is used for those unfortunate archs with crappy toolchains
363 */
364void env_reloc(void);
95fd9772
RV
365
366
367/**
368 * env_import_fdt() - Import environment values from device tree blob
369 *
370 * This uses the value of the environment variable "env_fdt_path" as a
371 * path to an fdt node, whose property/value pairs are added to the
372 * environment.
373 */
374#ifdef CONFIG_ENV_IMPORT_FDT
375void env_import_fdt(void);
376#else
377static inline void env_import_fdt(void) {}
378#endif
379
af95f206 380#endif
This page took 0.185197 seconds and 4 git commands to generate.