1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * Common environment functions and definitions
5 * (C) Copyright 2000-2009
6 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
14 #include <linux/types.h>
18 /* Value for environment validity */
20 ENV_INVALID, /* No valid environment */
21 ENV_VALID, /* First or only environment is valid */
22 ENV_REDUND, /* Redundant environment is valid */
25 /** enum env_op - environment callback operation */
32 /** struct env_clbk_tbl - declares a new callback */
34 const char *name; /* Callback name */
35 int (*callback)(const char *name, const char *value, enum env_op op,
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.
44 * For SPL these are silently dropped to reduce code size, since environment
45 * callbacks are not supported with SPL.
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) \
54 #define U_BOOT_ENV_CALLBACK(name, callback) \
55 ll_entry_declare(struct env_clbk_tbl, name, env_clbk) = \
59 /** enum env_redund_flags - Flags for the redundand_environment */
60 enum env_redund_flags {
61 ENV_REDUND_OBSOLETE = 0,
62 ENV_REDUND_ACTIVE = 1,
66 * env_get_id() - Gets a sequence number for the environment
68 * This value increments every time the environment changes, so can be used an
69 * an indication of this
71 * @return environment ID
76 * env_init() - Set up the pre-relocation environment
78 * This locates the environment or uses the default if nothing is available.
79 * This must be called before env_get() will work.
81 * @return 0 if OK, -ENODEV if no environment drivers are enabled
86 * env_relocate() - Set up the post-relocation environment
88 * This loads the environment into RAM so that it can be modified. This is
89 * called after relocation, before the environment is used
91 void env_relocate(void);
94 * env_get() - Look up the value of an environment variable
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().
100 * @varname: Variable to look up
101 * @return value of variable, or NULL if not found
103 char *env_get(const char *varname);
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.
110 * @varname: Variable to look up
111 * @return value of variable, or NULL if not found
113 char *from_env(const char *envvar);
116 * env_get_f() - Look up the value of an environment variable (early)
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.
122 * @varname: Variable to look up
123 * @return actual length of the variable value excluding the terminating
124 * NULL-byte, or -1 if the variable is not found
126 int env_get_f(const char *name, char *buf, unsigned int len);
129 * env_get_yesno() - Read an environment variable as a boolean
131 * @return 1 if yes/true (Y/y/T/t), -1 if variable does not exist (i.e. default
132 * to true), 0 if otherwise
134 int env_get_yesno(const char *var);
137 * env_set() - set an environment variable
139 * This sets or deletes the value of an environment variable. For setting the
140 * value the variable is created if it does not already exist.
142 * @varname: Variable to adjust
143 * @value: Value to set for the variable, or NULL or "" to delete the variable
144 * @return 0 if OK, 1 on error
146 int env_set(const char *varname, const char *value);
149 * env_get_ulong() - Return an environment variable as an integer value
151 * Most U-Boot environment variables store hex values. For those which store
152 * (e.g.) base-10 integers, this function can be used to read the value.
154 * @name: Variable to look up
155 * @base: Base to use (e.g. 10 for base 10, 2 for binary)
156 * @default_val: Default value to return if no value is found
157 * @return the value found, or @default_val if none
159 ulong env_get_ulong(const char *name, int base, ulong default_val);
162 * env_set_ulong() - set an environment variable to an integer
164 * @varname: Variable to adjust
165 * @value: Value to set for the variable (will be converted to a string)
166 * @return 0 if OK, 1 on error
168 int env_set_ulong(const char *varname, ulong value);
171 * env_get_hex() - Return an environment variable as a hex value
173 * Decode an environment as a hex number (it may or may not have a 0x
174 * prefix). If the environment variable cannot be found, or does not start
175 * with hex digits, the default value is returned.
177 * @varname: Variable to decode
178 * @default_val: Value to return on error
180 ulong env_get_hex(const char *varname, ulong default_val);
183 * env_set_hex() - set an environment variable to a hex value
185 * @varname: Variable to adjust
186 * @value: Value to set for the variable (will be converted to a hex string)
187 * @return 0 if OK, 1 on error
189 int env_set_hex(const char *varname, ulong value);
192 * env_set_addr - Set an environment variable to an address in hex
194 * @varname: Environment variable to set
195 * @addr: Value to set it to
196 * @return 0 if ok, 1 on error
198 static inline int env_set_addr(const char *varname, const void *addr)
200 return env_set_hex(varname, (ulong)addr);
204 * env_complete() - return an auto-complete for environment variables
206 * @var: partial name to auto-complete
207 * @maxv: Maximum number of matches to return
208 * @cmdv: Returns a list of possible matches
209 * @maxsz: Size of buffer to use for matches
210 * @buf: Buffer to use for matches
211 * @dollar_comp: non-zero to wrap each match in ${...}
212 * @return number of matches found (in @cmdv)
214 int env_complete(char *var, int maxv, char *cmdv[], int maxsz, char *buf,
218 * eth_env_get_enetaddr() - Get an ethernet address from the environmnet
220 * @name: Environment variable to get (e.g. "ethaddr")
221 * @enetaddr: Place to put MAC address (6 bytes)
222 * @return 0 if OK, 1 on error
224 int eth_env_get_enetaddr(const char *name, uint8_t *enetaddr);
227 * eth_env_set_enetaddr() - Set an ethernet address in the environmnet
229 * @name: Environment variable to set (e.g. "ethaddr")
230 * @enetaddr: Pointer to MAC address to put into the variable (6 bytes)
231 * @return 0 if OK, 1 on error
233 int eth_env_set_enetaddr(const char *name, const uint8_t *enetaddr);
236 * env_fix_drivers() - Updates envdriver as per relocation
238 void env_fix_drivers(void);
241 * env_set_default_vars() - reset variables to their default value
243 * This resets individual variables to their value in the default environment
245 * @nvars: Number of variables to set/reset
246 * @vars: List of variables to set/reset
247 * @flags: Flags controlling matching (H_... - see search.h)
249 int env_set_default_vars(int nvars, char *const vars[], int flags);
252 * env_load() - Load the environment from storage
254 * @return 0 if OK, -ve on error
259 * env_reload() - Re-Load the environment from current storage
261 * @return 0 if OK, -ve on error
263 int env_reload(void);
266 * env_save() - Save the environment to storage
268 * @return 0 if OK, -ve on error
273 * env_erase() - Erase the environment on storage
275 * @return 0 if OK, -ve on error
280 * env_select() - Select the environment storage
282 * @return 0 if OK, -ve on error
284 int env_select(const char *name);
287 * env_import() - Import from a binary representation into hash table
289 * This imports the environment from a buffer. The format for each variable is
290 * var=value\0 with a double \0 at the end of the buffer.
292 * @buf: Buffer containing the environment (struct environemnt_s *)
293 * @check: non-zero to check the CRC at the start of the environment, 0 to
295 * @flags: Flags controlling matching (H_... - see search.h)
296 * @return 0 if imported successfully, -ENOMSG if the CRC was bad, -EIO if
297 * something else went wrong
299 int env_import(const char *buf, int check, int flags);
302 * env_export() - Export the environment to a buffer
304 * Export from hash table into binary representation
306 * @env_out: Buffer to contain the environment (must be large enough!)
307 * @return 0 if OK, 1 on error
309 int env_export(struct environment_s *env_out);
312 * env_check_redund() - check the two redundant environments
313 * and find out, which is the valid one.
315 * @buf1: First environment (struct environemnt_s *)
316 * @buf1_read_fail: 0 if buf1 is valid, non-zero if invalid
317 * @buf2: Second environment (struct environemnt_s *)
318 * @buf2_read_fail: 0 if buf2 is valid, non-zero if invalid
320 * -EIO if no environment is valid,
321 * -ENOMSG if the CRC was bad
324 int env_check_redund(const char *buf1, int buf1_read_fail,
325 const char *buf2, int buf2_read_fail);
328 * env_import_redund() - Select and import one of two redundant environments
330 * @buf1: First environment (struct environemnt_s *)
331 * @buf1_read_fail: 0 if buf1 is valid, non-zero if invalid
332 * @buf2: Second environment (struct environemnt_s *)
333 * @buf2_read_fail: 0 if buf2 is valid, non-zero if invalid
334 * @flags: Flags controlling matching (H_... - see search.h)
335 * @return 0 if OK, -EIO if no environment is valid, -ENOMSG if the CRC was bad
337 int env_import_redund(const char *buf1, int buf1_read_fail,
338 const char *buf2, int buf2_read_fail,
342 * env_get_default() - Look up a variable from the default environment
344 * @name: Variable to look up
345 * @return value if found, NULL if not found in default environment
347 char *env_get_default(const char *name);
349 /* [re]set to the default environment */
350 void env_set_default(const char *s, int flags);
353 * env_reloc() - Relocate the 'env' sub-commands
355 * This is used for those unfortunate archs with crappy toolchains
357 void env_reloc(void);
361 * env_import_fdt() - Import environment values from device tree blob
363 * This uses the value of the environment variable "env_fdt_path" as a
364 * path to an fdt node, whose property/value pairs are added to the
367 #ifdef CONFIG_ENV_IMPORT_FDT
368 void env_import_fdt(void);
370 static inline void env_import_fdt(void) {}