1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * Common environment functions
5 * (C) Copyright 2000-2009
6 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
13 #include <linux/types.h>
18 * env_get_id() - Gets a sequence number for the environment
20 * This value increments every time the environment changes, so can be used an
21 * an indication of this
23 * @return environment ID
28 * env_init() - Set up the pre-relocation environment
30 * This locates the environment or uses the default if nothing is available.
31 * This must be called before env_get() will work.
33 * @return 0 if OK, -ENODEV if no environment drivers are enabled
38 * env_relocate() - Set up the post-relocation environment
40 * This loads the environment into RAM so that it can be modified. This is
41 * called after relocation, before the environment is used
43 void env_relocate(void);
46 * env_match() - Match a name / name=value pair
48 * This is used prior to relocation for finding envrionment variables
50 * @name: A simple 'name', or a 'name=value' pair.
51 * @index: The environment index for a 'name2=value2' pair.
52 * @return index for the value if the names match, else -1.
54 int env_match(unsigned char *name, int index);
57 * env_get() - Look up the value of an environment variable
59 * In U-Boot proper this can be called before relocation (which is when the
60 * environment is loaded from storage, i.e. GD_FLG_ENV_READY is 0). In that
61 * case this function calls env_get_f().
63 * @varname: Variable to look up
64 * @return value of variable, or NULL if not found
66 char *env_get(const char *varname);
69 * env_get_f() - Look up the value of an environment variable (early)
71 * This function is called from env_get() if the environment has not been
72 * loaded yet (GD_FLG_ENV_READY flag is 0). Some environment locations will
73 * support reading the value (slowly) and some will not.
75 * @varname: Variable to look up
76 * @return value of variable, or NULL if not found
78 int env_get_f(const char *name, char *buf, unsigned int len);
81 * env_get_yesno() - Read an environment variable as a boolean
83 * @return 1 if yes/true (Y/y/T/t), -1 if variable does not exist (i.e. default
84 * to true), 0 if otherwise
86 int env_get_yesno(const char *var);
89 * env_set() - set an environment variable
91 * This sets or deletes the value of an environment variable. For setting the
92 * value the variable is created if it does not already exist.
94 * @varname: Variable to adjust
95 * @value: Value to set for the variable, or NULL or "" to delete the variable
96 * @return 0 if OK, 1 on error
98 int env_set(const char *varname, const char *value);
101 * env_get_ulong() - Return an environment variable as an integer value
103 * Most U-Boot environment variables store hex values. For those which store
104 * (e.g.) base-10 integers, this function can be used to read the value.
106 * @name: Variable to look up
107 * @base: Base to use (e.g. 10 for base 10, 2 for binary)
108 * @default_val: Default value to return if no value is found
109 * @return the value found, or @default_val if none
111 ulong env_get_ulong(const char *name, int base, ulong default_val);
114 * env_set_ulong() - set an environment variable to an integer
116 * @varname: Variable to adjust
117 * @value: Value to set for the variable (will be converted to a string)
118 * @return 0 if OK, 1 on error
120 int env_set_ulong(const char *varname, ulong value);
123 * env_get_hex() - Return an environment variable as a hex value
125 * Decode an environment as a hex number (it may or may not have a 0x
126 * prefix). If the environment variable cannot be found, or does not start
127 * with hex digits, the default value is returned.
129 * @varname: Variable to decode
130 * @default_val: Value to return on error
132 ulong env_get_hex(const char *varname, ulong default_val);
135 * env_set_hex() - set an environment variable to a hex value
137 * @varname: Variable to adjust
138 * @value: Value to set for the variable (will be converted to a hex string)
139 * @return 0 if OK, 1 on error
141 int env_set_hex(const char *varname, ulong value);
144 * env_set_addr - Set an environment variable to an address in hex
146 * @varname: Environment variable to set
147 * @addr: Value to set it to
148 * @return 0 if ok, 1 on error
150 static inline int env_set_addr(const char *varname, const void *addr)
152 return env_set_hex(varname, (ulong)addr);
156 * env_complete() - return an auto-complete for environment variables
158 * @var: partial name to auto-complete
159 * @maxv: Maximum number of matches to return
160 * @cmdv: Returns a list of possible matches
161 * @maxsz: Size of buffer to use for matches
162 * @buf: Buffer to use for matches
163 * @dollar_comp: non-zero to wrap each match in ${...}
164 * @return number of matches found (in @cmdv)
166 int env_complete(char *var, int maxv, char *cmdv[], int maxsz, char *buf,
170 * eth_env_get_enetaddr() - Get an ethernet address from the environmnet
172 * @name: Environment variable to get (e.g. "ethaddr")
173 * @enetaddr: Place to put MAC address (6 bytes)
174 * @return 0 if OK, 1 on error
176 int eth_env_get_enetaddr(const char *name, uint8_t *enetaddr);
179 * eth_env_set_enetaddr() - Set an ethernet address in the environmnet
181 * @name: Environment variable to set (e.g. "ethaddr")
182 * @enetaddr: Pointer to MAC address to put into the variable (6 bytes)
183 * @return 0 if OK, 1 on error
185 int eth_env_set_enetaddr(const char *name, const uint8_t *enetaddr);
188 * env_fix_drivers() - Updates envdriver as per relocation
190 void env_fix_drivers(void);
193 * env_set_default_vars() - reset variables to their default value
195 * This resets individual variables to their value in the default environment
197 * @nvars: Number of variables to set/reset
198 * @vars: List of variables to set/reset
199 * @flags: Flags controlling matching (H_... - see search.h)
201 int env_set_default_vars(int nvars, char *const vars[], int flags);
204 * env_load() - Load the environment from storage
206 * @return 0 if OK, -ve on error
211 * env_save() - Save the environment to storage
213 * @return 0 if OK, -ve on error
218 * env_erase() - Erase the environment on storage
220 * @return 0 if OK, -ve on error
225 * env_import() - Import from a binary representation into hash table
227 * This imports the environment from a buffer. The format for each variable is
228 * var=value\0 with a double \0 at the end of the buffer.
230 * @buf: Buffer containing the environment (struct environemnt_s *)
231 * @check: non-zero to check the CRC at the start of the environment, 0 to
233 * @return 0 if imported successfully, -ENOMSG if the CRC was bad, -EIO if
234 * something else went wrong
236 int env_import(const char *buf, int check);
239 * env_export() - Export the environment to a buffer
241 * Export from hash table into binary representation
243 * @env_out: Buffer to contain the environment (must be large enough!)
244 * @return 0 if OK, 1 on error
246 int env_export(struct environment_s *env_out);
249 * env_import_redund() - Select and import one of two redundant environments
251 * @buf1: First environment (struct environemnt_s *)
252 * @buf1_read_fail: 0 if buf1 is valid, non-zero if invalid
253 * @buf2: Second environment (struct environemnt_s *)
254 * @buf2_read_fail: 0 if buf2 is valid, non-zero if invalid
255 * @return 0 if OK, -EIO if no environment is valid, -ENOMSG if the CRC was bad
257 int env_import_redund(const char *buf1, int buf1_read_fail,
258 const char *buf2, int buf2_read_fail);