blob: b72239f6a58c915e0d133107be8108fbec15adf5 [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 Glassb9ca02c2019-08-01 09:46:45 -060094 * env_match() - Match a name / name=value pair
95 *
96 * This is used prior to relocation for finding envrionment variables
97 *
98 * @name: A simple 'name', or a 'name=value' pair.
99 * @index: The environment index for a 'name2=value2' pair.
100 * @return index for the value if the names match, else -1.
101 */
102int env_match(unsigned char *name, int index);
103
104/**
Simon Glass7b51b572019-08-01 09:46:52 -0600105 * env_get() - Look up the value of an environment variable
106 *
107 * In U-Boot proper this can be called before relocation (which is when the
108 * environment is loaded from storage, i.e. GD_FLG_ENV_READY is 0). In that
109 * case this function calls env_get_f().
110 *
111 * @varname: Variable to look up
112 * @return value of variable, or NULL if not found
113 */
114char *env_get(const char *varname);
115
116/**
Simon Glass3a7d5572019-08-01 09:46:42 -0600117 * env_get_f() - Look up the value of an environment variable (early)
118 *
119 * This function is called from env_get() if the environment has not been
120 * loaded yet (GD_FLG_ENV_READY flag is 0). Some environment locations will
121 * support reading the value (slowly) and some will not.
122 *
123 * @varname: Variable to look up
124 * @return value of variable, or NULL if not found
125 */
126int env_get_f(const char *name, char *buf, unsigned int len);
127
128/**
Simon Glass6bf6dbe2019-08-01 09:46:49 -0600129 * env_get_yesno() - Read an environment variable as a boolean
130 *
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
133 */
134int env_get_yesno(const char *var);
135
136/**
Simon Glass9fb625c2019-08-01 09:46:51 -0600137 * env_set() - set an environment variable
138 *
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.
141 *
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
145 */
146int env_set(const char *varname, const char *value);
147
148/**
Simon Glass9eef56d2019-08-01 09:46:48 -0600149 * env_get_ulong() - Return an environment variable as an integer value
150 *
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.
153 *
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
158 */
159ulong env_get_ulong(const char *name, int base, ulong default_val);
160
161/**
Simon Glass168068f2019-08-01 09:46:47 -0600162 * env_set_ulong() - set an environment variable to an integer
163 *
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
167 */
168int env_set_ulong(const char *varname, ulong value);
169
170/**
Simon Glasscdbff9f2019-08-01 09:46:50 -0600171 * env_get_hex() - Return an environment variable as a hex value
172 *
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.
176 *
177 * @varname: Variable to decode
178 * @default_val: Value to return on error
179 */
180ulong env_get_hex(const char *varname, ulong default_val);
181
182/**
Simon Glassc7694dd2019-08-01 09:46:46 -0600183 * env_set_hex() - set an environment variable to a hex value
184 *
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
188 */
189int env_set_hex(const char *varname, ulong value);
190
191/**
192 * env_set_addr - Set an environment variable to an address in hex
193 *
194 * @varname: Environment variable to set
195 * @addr: Value to set it to
196 * @return 0 if ok, 1 on error
197 */
198static inline int env_set_addr(const char *varname, const void *addr)
199{
200 return env_set_hex(varname, (ulong)addr);
201}
202
203/**
Simon Glassaf95f202019-08-01 09:46:40 -0600204 * env_complete() - return an auto-complete for environment variables
205 *
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)
213 */
214int env_complete(char *var, int maxv, char *cmdv[], int maxsz, char *buf,
215 bool dollar_comp);
216
Simon Glassb79cf1a2019-08-01 09:46:53 -0600217/**
218 * eth_env_get_enetaddr() - Get an ethernet address from the environmnet
219 *
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
223 */
224int eth_env_get_enetaddr(const char *name, uint8_t *enetaddr);
225
226/**
227 * eth_env_set_enetaddr() - Set an ethernet address in the environmnet
228 *
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
232 */
233int eth_env_set_enetaddr(const char *name, const uint8_t *enetaddr);
234
Simon Glass03ed9182019-08-01 09:46:55 -0600235/**
236 * env_fix_drivers() - Updates envdriver as per relocation
237 */
238void env_fix_drivers(void);
239
Simon Glass0b9d8a02019-08-01 09:46:56 -0600240/**
241 * env_set_default_vars() - reset variables to their default value
242 *
243 * This resets individual variables to their value in the default environment
244 *
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)
248 */
249int env_set_default_vars(int nvars, char *const vars[], int flags);
250
Simon Glass4be490a2019-08-01 09:46:57 -0600251/**
252 * env_load() - Load the environment from storage
253 *
254 * @return 0 if OK, -ve on error
255 */
256int env_load(void);
257
258/**
259 * env_save() - Save the environment to storage
260 *
261 * @return 0 if OK, -ve on error
262 */
263int env_save(void);
264
265/**
266 * env_erase() - Erase the environment on storage
267 *
268 * @return 0 if OK, -ve on error
269 */
270int env_erase(void);
271
Simon Glass4e9ce8a2019-08-01 09:46:58 -0600272/**
273 * env_import() - Import from a binary representation into hash table
274 *
275 * This imports the environment from a buffer. The format for each variable is
276 * var=value\0 with a double \0 at the end of the buffer.
277 *
278 * @buf: Buffer containing the environment (struct environemnt_s *)
279 * @check: non-zero to check the CRC at the start of the environment, 0 to
280 * ignore it
281 * @return 0 if imported successfully, -ENOMSG if the CRC was bad, -EIO if
282 * something else went wrong
283 */
284int env_import(const char *buf, int check);
285
286/**
287 * env_export() - Export the environment to a buffer
288 *
289 * Export from hash table into binary representation
290 *
291 * @env_out: Buffer to contain the environment (must be large enough!)
292 * @return 0 if OK, 1 on error
293 */
294int env_export(struct environment_s *env_out);
295
296/**
297 * env_import_redund() - Select and import one of two redundant environments
298 *
299 * @buf1: First environment (struct environemnt_s *)
300 * @buf1_read_fail: 0 if buf1 is valid, non-zero if invalid
301 * @buf2: Second environment (struct environemnt_s *)
302 * @buf2_read_fail: 0 if buf2 is valid, non-zero if invalid
303 * @return 0 if OK, -EIO if no environment is valid, -ENOMSG if the CRC was bad
304 */
305int env_import_redund(const char *buf1, int buf1_read_fail,
306 const char *buf2, int buf2_read_fail);
307
Simon Glass0ac7d722019-08-01 09:47:00 -0600308/**
309 * env_get_default() - Look up a variable from the default environment
310 *
311 * @name: Variable to look up
312 * @return value if found, NULL if not found in default environment
313 */
314char *env_get_default(const char *name);
315
316/* [re]set to the default environment */
317void env_set_default(const char *s, int flags);
318
Simon Glassb5f449e2019-08-01 09:47:01 -0600319/**
320 * env_get_char() - Get a character from the early environment
321 *
322 * This reads from the pre-relocation environment
323 *
324 * @index: Index of character to read (0 = first)
325 * @return character read, or -ve on error
326 */
327int env_get_char(int index);
328
Simon Glassc62f62b2019-08-01 09:47:02 -0600329/**
330 * env_reloc() - Relocate the 'env' sub-commands
331 *
332 * This is used for those unfortunate archs with crappy toolchains
333 */
334void env_reloc(void);
335
Simon Glassaf95f202019-08-01 09:46:40 -0600336#endif