env: Fix documentation for env_get_f()
[platform/kernel/u-boot.git] / include / env.h
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * Common environment functions and definitions
4  *
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
12 #include <compiler.h>
13 #include <stdbool.h>
14 #include <linux/types.h>
15
16 struct environment_s;
17
18 /* Value for environment validity */
19 enum 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
25 /** enum env_op - environment callback operation */
26 enum 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 */
33 struct 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
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,
63 };
64
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  *
71  * @return environment ID
72  */
73 int env_get_id(void);
74
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  *
81  * @return 0 if OK, -ENODEV if no environment drivers are enabled
82  */
83 int env_init(void);
84
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  */
91 void env_relocate(void);
92
93 /**
94  * 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  */
102 int env_match(unsigned char *name, int index);
103
104 /**
105  * 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  */
114 char *env_get(const char *varname);
115
116 /*
117  * Like env_get, but prints an error if envvar isn't defined in the
118  * environment.  It always returns what env_get does, so it can be used in
119  * place of env_get without changing error handling otherwise.
120  *
121  * @varname:    Variable to look up
122  * @return value of variable, or NULL if not found
123  */
124 char *from_env(const char *envvar);
125
126 /**
127  * env_get_f() - Look up the value of an environment variable (early)
128  *
129  * This function is called from env_get() if the environment has not been
130  * loaded yet (GD_FLG_ENV_READY flag is 0). Some environment locations will
131  * support reading the value (slowly) and some will not.
132  *
133  * @varname:    Variable to look up
134  * @return number of bytes written into @buf, excluding the terminating
135  *      NULL-byte if there was enough space in @buf, and including the
136  *      terminating NULL-byte if there wasn't enough space, or -1 if the
137  *      variable is not found
138  */
139 int env_get_f(const char *name, char *buf, unsigned int len);
140
141 /**
142  * env_get_yesno() - Read an environment variable as a boolean
143  *
144  * @return 1 if yes/true (Y/y/T/t), -1 if variable does not exist (i.e. default
145  *      to true), 0 if otherwise
146  */
147 int env_get_yesno(const char *var);
148
149 /**
150  * env_set() - set an environment variable
151  *
152  * This sets or deletes the value of an environment variable. For setting the
153  * value the variable is created if it does not already exist.
154  *
155  * @varname: Variable to adjust
156  * @value: Value to set for the variable, or NULL or "" to delete the variable
157  * @return 0 if OK, 1 on error
158  */
159 int env_set(const char *varname, const char *value);
160
161 /**
162  * env_get_ulong() - Return an environment variable as an integer value
163  *
164  * Most U-Boot environment variables store hex values. For those which store
165  * (e.g.) base-10 integers, this function can be used to read the value.
166  *
167  * @name:       Variable to look up
168  * @base:       Base to use (e.g. 10 for base 10, 2 for binary)
169  * @default_val: Default value to return if no value is found
170  * @return the value found, or @default_val if none
171  */
172 ulong env_get_ulong(const char *name, int base, ulong default_val);
173
174 /**
175  * env_set_ulong() - set an environment variable to an integer
176  *
177  * @varname: Variable to adjust
178  * @value: Value to set for the variable (will be converted to a string)
179  * @return 0 if OK, 1 on error
180  */
181 int env_set_ulong(const char *varname, ulong value);
182
183 /**
184  * env_get_hex() - Return an environment variable as a hex value
185  *
186  * Decode an environment as a hex number (it may or may not have a 0x
187  * prefix). If the environment variable cannot be found, or does not start
188  * with hex digits, the default value is returned.
189  *
190  * @varname:            Variable to decode
191  * @default_val:        Value to return on error
192  */
193 ulong env_get_hex(const char *varname, ulong default_val);
194
195 /**
196  * env_set_hex() - set an environment variable to a hex value
197  *
198  * @varname: Variable to adjust
199  * @value: Value to set for the variable (will be converted to a hex string)
200  * @return 0 if OK, 1 on error
201  */
202 int env_set_hex(const char *varname, ulong value);
203
204 /**
205  * env_set_addr - Set an environment variable to an address in hex
206  *
207  * @varname:    Environment variable to set
208  * @addr:       Value to set it to
209  * @return 0 if ok, 1 on error
210  */
211 static inline int env_set_addr(const char *varname, const void *addr)
212 {
213         return env_set_hex(varname, (ulong)addr);
214 }
215
216 /**
217  * env_complete() - return an auto-complete for environment variables
218  *
219  * @var: partial name to auto-complete
220  * @maxv: Maximum number of matches to return
221  * @cmdv: Returns a list of possible matches
222  * @maxsz: Size of buffer to use for matches
223  * @buf: Buffer to use for matches
224  * @dollar_comp: non-zero to wrap each match in ${...}
225  * @return number of matches found (in @cmdv)
226  */
227 int env_complete(char *var, int maxv, char *cmdv[], int maxsz, char *buf,
228                  bool dollar_comp);
229
230 /**
231  * eth_env_get_enetaddr() - Get an ethernet address from the environmnet
232  *
233  * @name: Environment variable to get (e.g. "ethaddr")
234  * @enetaddr: Place to put MAC address (6 bytes)
235  * @return 0 if OK, 1 on error
236  */
237 int eth_env_get_enetaddr(const char *name, uint8_t *enetaddr);
238
239 /**
240  * eth_env_set_enetaddr() - Set an ethernet address in the environmnet
241  *
242  * @name: Environment variable to set (e.g. "ethaddr")
243  * @enetaddr: Pointer to MAC address to put into the variable (6 bytes)
244  * @return 0 if OK, 1 on error
245  */
246 int eth_env_set_enetaddr(const char *name, const uint8_t *enetaddr);
247
248 /**
249  * env_fix_drivers() - Updates envdriver as per relocation
250  */
251 void env_fix_drivers(void);
252
253 /**
254  * env_set_default_vars() - reset variables to their default value
255  *
256  * This resets individual variables to their value in the default environment
257  *
258  * @nvars: Number of variables to set/reset
259  * @vars: List of variables to set/reset
260  * @flags: Flags controlling matching (H_... - see search.h)
261  */
262 int env_set_default_vars(int nvars, char *const vars[], int flags);
263
264 /**
265  * env_load() - Load the environment from storage
266  *
267  * @return 0 if OK, -ve on error
268  */
269 int env_load(void);
270
271 /**
272  * env_reload() - Re-Load the environment from current storage
273  *
274  * @return 0 if OK, -ve on error
275  */
276 int env_reload(void);
277
278 /**
279  * env_save() - Save the environment to storage
280  *
281  * @return 0 if OK, -ve on error
282  */
283 int env_save(void);
284
285 /**
286  * env_erase() - Erase the environment on storage
287  *
288  * @return 0 if OK, -ve on error
289  */
290 int env_erase(void);
291
292 /**
293  * env_select() - Select the environment storage
294  *
295  * @return 0 if OK, -ve on error
296  */
297 int env_select(const char *name);
298
299 /**
300  * env_import() - Import from a binary representation into hash table
301  *
302  * This imports the environment from a buffer. The format for each variable is
303  * var=value\0 with a double \0 at the end of the buffer.
304  *
305  * @buf: Buffer containing the environment (struct environemnt_s *)
306  * @check: non-zero to check the CRC at the start of the environment, 0 to
307  *      ignore it
308  * @flags: Flags controlling matching (H_... - see search.h)
309  * @return 0 if imported successfully, -ENOMSG if the CRC was bad, -EIO if
310  *      something else went wrong
311  */
312 int env_import(const char *buf, int check, int flags);
313
314 /**
315  * env_export() - Export the environment to a buffer
316  *
317  * Export from hash table into binary representation
318  *
319  * @env_out: Buffer to contain the environment (must be large enough!)
320  * @return 0 if OK, 1 on error
321  */
322 int env_export(struct environment_s *env_out);
323
324 /**
325  * env_check_redund() - check the two redundant environments
326  *   and find out, which is the valid one.
327  *
328  * @buf1: First environment (struct environemnt_s *)
329  * @buf1_read_fail: 0 if buf1 is valid, non-zero if invalid
330  * @buf2: Second environment (struct environemnt_s *)
331  * @buf2_read_fail: 0 if buf2 is valid, non-zero if invalid
332  * @return 0 if OK,
333  *      -EIO if no environment is valid,
334  *      -ENOMSG if the CRC was bad
335  */
336
337 int env_check_redund(const char *buf1, int buf1_read_fail,
338                      const char *buf2, int buf2_read_fail);
339
340 /**
341  * env_import_redund() - Select and import one of two redundant environments
342  *
343  * @buf1: First environment (struct environemnt_s *)
344  * @buf1_read_fail: 0 if buf1 is valid, non-zero if invalid
345  * @buf2: Second environment (struct environemnt_s *)
346  * @buf2_read_fail: 0 if buf2 is valid, non-zero if invalid
347  * @flags: Flags controlling matching (H_... - see search.h)
348  * @return 0 if OK, -EIO if no environment is valid, -ENOMSG if the CRC was bad
349  */
350 int env_import_redund(const char *buf1, int buf1_read_fail,
351                       const char *buf2, int buf2_read_fail,
352                       int flags);
353
354 /**
355  * env_get_default() - Look up a variable from the default environment
356  *
357  * @name: Variable to look up
358  * @return value if found, NULL if not found in default environment
359  */
360 char *env_get_default(const char *name);
361
362 /* [re]set to the default environment */
363 void env_set_default(const char *s, int flags);
364
365 /**
366  * env_get_char() - Get a character from the early environment
367  *
368  * This reads from the pre-relocation environment
369  *
370  * @index: Index of character to read (0 = first)
371  * @return character read, or -ve on error
372  */
373 int env_get_char(int index);
374
375 /**
376  * env_reloc() - Relocate the 'env' sub-commands
377  *
378  * This is used for those unfortunate archs with crappy toolchains
379  */
380 void env_reloc(void);
381
382
383 /**
384  * env_import_fdt() - Import environment values from device tree blob
385  *
386  * This uses the value of the environment variable "env_fdt_path" as a
387  * path to an fdt node, whose property/value pairs are added to the
388  * environment.
389  */
390 #ifdef CONFIG_ENV_IMPORT_FDT
391 void env_import_fdt(void);
392 #else
393 static inline void env_import_fdt(void) {}
394 #endif
395
396 #endif