env: Move callback definitions to env.h
[oweals/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 <stdbool.h>
13 #include <linux/types.h>
14
15 struct environment_s;
16
17 /* Value for environment validity */
18 enum env_valid {
19         ENV_INVALID,    /* No valid environment */
20         ENV_VALID,      /* First or only environment is valid */
21         ENV_REDUND,     /* Redundant environment is valid */
22 };
23
24 /** enum env_op - environment callback operation */
25 enum env_op {
26         env_op_create,
27         env_op_delete,
28         env_op_overwrite,
29 };
30
31 /** struct env_clbk_tbl - declares a new callback */
32 struct env_clbk_tbl {
33         const char *name;               /* Callback name */
34         int (*callback)(const char *name, const char *value, enum env_op op,
35                         int flags);
36 };
37
38 /*
39  * Define a callback that can be associated with variables.
40  * when associated through the ".callbacks" environment variable, the callback
41  * will be executed any time the variable is inserted, overwritten, or deleted.
42  *
43  * For SPL these are silently dropped to reduce code size, since environment
44  * callbacks are not supported with SPL.
45  */
46 #ifdef CONFIG_SPL_BUILD
47 #define U_BOOT_ENV_CALLBACK(name, callback) \
48         static inline __maybe_unused void _u_boot_env_noop_##name(void) \
49         { \
50                 (void)callback; \
51         }
52 #else
53 #define U_BOOT_ENV_CALLBACK(name, callback) \
54         ll_entry_declare(struct env_clbk_tbl, name, env_clbk) = \
55         {#name, callback}
56 #endif
57
58 /**
59  * env_get_id() - Gets a sequence number for the environment
60  *
61  * This value increments every time the environment changes, so can be used an
62  * an indication of this
63  *
64  * @return environment ID
65  */
66 int env_get_id(void);
67
68 /**
69  * env_init() - Set up the pre-relocation environment
70  *
71  * This locates the environment or uses the default if nothing is available.
72  * This must be called before env_get() will work.
73  *
74  * @return 0 if OK, -ENODEV if no environment drivers are enabled
75  */
76 int env_init(void);
77
78 /**
79  * env_relocate() - Set up the post-relocation environment
80  *
81  * This loads the environment into RAM so that it can be modified. This is
82  * called after relocation, before the environment is used
83  */
84 void env_relocate(void);
85
86 /**
87  * env_match() - Match a name / name=value pair
88  *
89  * This is used prior to relocation for finding envrionment variables
90  *
91  * @name: A simple 'name', or a 'name=value' pair.
92  * @index: The environment index for a 'name2=value2' pair.
93  * @return index for the value if the names match, else -1.
94  */
95 int env_match(unsigned char *name, int index);
96
97 /**
98  * env_get() - Look up the value of an environment variable
99  *
100  * In U-Boot proper this can be called before relocation (which is when the
101  * environment is loaded from storage, i.e. GD_FLG_ENV_READY is 0). In that
102  * case this function calls env_get_f().
103  *
104  * @varname:    Variable to look up
105  * @return value of variable, or NULL if not found
106  */
107 char *env_get(const char *varname);
108
109 /**
110  * env_get_f() - Look up the value of an environment variable (early)
111  *
112  * This function is called from env_get() if the environment has not been
113  * loaded yet (GD_FLG_ENV_READY flag is 0). Some environment locations will
114  * support reading the value (slowly) and some will not.
115  *
116  * @varname:    Variable to look up
117  * @return value of variable, or NULL if not found
118  */
119 int env_get_f(const char *name, char *buf, unsigned int len);
120
121 /**
122  * env_get_yesno() - Read an environment variable as a boolean
123  *
124  * @return 1 if yes/true (Y/y/T/t), -1 if variable does not exist (i.e. default
125  *      to true), 0 if otherwise
126  */
127 int env_get_yesno(const char *var);
128
129 /**
130  * env_set() - set an environment variable
131  *
132  * This sets or deletes the value of an environment variable. For setting the
133  * value the variable is created if it does not already exist.
134  *
135  * @varname: Variable to adjust
136  * @value: Value to set for the variable, or NULL or "" to delete the variable
137  * @return 0 if OK, 1 on error
138  */
139 int env_set(const char *varname, const char *value);
140
141 /**
142  * env_get_ulong() - Return an environment variable as an integer value
143  *
144  * Most U-Boot environment variables store hex values. For those which store
145  * (e.g.) base-10 integers, this function can be used to read the value.
146  *
147  * @name:       Variable to look up
148  * @base:       Base to use (e.g. 10 for base 10, 2 for binary)
149  * @default_val: Default value to return if no value is found
150  * @return the value found, or @default_val if none
151  */
152 ulong env_get_ulong(const char *name, int base, ulong default_val);
153
154 /**
155  * env_set_ulong() - set an environment variable to an integer
156  *
157  * @varname: Variable to adjust
158  * @value: Value to set for the variable (will be converted to a string)
159  * @return 0 if OK, 1 on error
160  */
161 int env_set_ulong(const char *varname, ulong value);
162
163 /**
164  * env_get_hex() - Return an environment variable as a hex value
165  *
166  * Decode an environment as a hex number (it may or may not have a 0x
167  * prefix). If the environment variable cannot be found, or does not start
168  * with hex digits, the default value is returned.
169  *
170  * @varname:            Variable to decode
171  * @default_val:        Value to return on error
172  */
173 ulong env_get_hex(const char *varname, ulong default_val);
174
175 /**
176  * env_set_hex() - set an environment variable to a hex value
177  *
178  * @varname: Variable to adjust
179  * @value: Value to set for the variable (will be converted to a hex string)
180  * @return 0 if OK, 1 on error
181  */
182 int env_set_hex(const char *varname, ulong value);
183
184 /**
185  * env_set_addr - Set an environment variable to an address in hex
186  *
187  * @varname:    Environment variable to set
188  * @addr:       Value to set it to
189  * @return 0 if ok, 1 on error
190  */
191 static inline int env_set_addr(const char *varname, const void *addr)
192 {
193         return env_set_hex(varname, (ulong)addr);
194 }
195
196 /**
197  * env_complete() - return an auto-complete for environment variables
198  *
199  * @var: partial name to auto-complete
200  * @maxv: Maximum number of matches to return
201  * @cmdv: Returns a list of possible matches
202  * @maxsz: Size of buffer to use for matches
203  * @buf: Buffer to use for matches
204  * @dollar_comp: non-zero to wrap each match in ${...}
205  * @return number of matches found (in @cmdv)
206  */
207 int env_complete(char *var, int maxv, char *cmdv[], int maxsz, char *buf,
208                  bool dollar_comp);
209
210 /**
211  * eth_env_get_enetaddr() - Get an ethernet address from the environmnet
212  *
213  * @name: Environment variable to get (e.g. "ethaddr")
214  * @enetaddr: Place to put MAC address (6 bytes)
215  * @return 0 if OK, 1 on error
216  */
217 int eth_env_get_enetaddr(const char *name, uint8_t *enetaddr);
218
219 /**
220  * eth_env_set_enetaddr() - Set an ethernet address in the environmnet
221  *
222  * @name: Environment variable to set (e.g. "ethaddr")
223  * @enetaddr: Pointer to MAC address to put into the variable (6 bytes)
224  * @return 0 if OK, 1 on error
225  */
226 int eth_env_set_enetaddr(const char *name, const uint8_t *enetaddr);
227
228 /**
229  * env_fix_drivers() - Updates envdriver as per relocation
230  */
231 void env_fix_drivers(void);
232
233 /**
234  * env_set_default_vars() - reset variables to their default value
235  *
236  * This resets individual variables to their value in the default environment
237  *
238  * @nvars: Number of variables to set/reset
239  * @vars: List of variables to set/reset
240  * @flags: Flags controlling matching (H_... - see search.h)
241  */
242 int env_set_default_vars(int nvars, char *const vars[], int flags);
243
244 /**
245  * env_load() - Load the environment from storage
246  *
247  * @return 0 if OK, -ve on error
248  */
249 int env_load(void);
250
251 /**
252  * env_save() - Save the environment to storage
253  *
254  * @return 0 if OK, -ve on error
255  */
256 int env_save(void);
257
258 /**
259  * env_erase() - Erase the environment on storage
260  *
261  * @return 0 if OK, -ve on error
262  */
263 int env_erase(void);
264
265 /**
266  * env_import() - Import from a binary representation into hash table
267  *
268  * This imports the environment from a buffer. The format for each variable is
269  * var=value\0 with a double \0 at the end of the buffer.
270  *
271  * @buf: Buffer containing the environment (struct environemnt_s *)
272  * @check: non-zero to check the CRC at the start of the environment, 0 to
273  *      ignore it
274  * @return 0 if imported successfully, -ENOMSG if the CRC was bad, -EIO if
275  *      something else went wrong
276  */
277 int env_import(const char *buf, int check);
278
279 /**
280  * env_export() - Export the environment to a buffer
281  *
282  * Export from hash table into binary representation
283  *
284  * @env_out: Buffer to contain the environment (must be large enough!)
285  * @return 0 if OK, 1 on error
286  */
287 int env_export(struct environment_s *env_out);
288
289 /**
290  * env_import_redund() - Select and import one of two redundant environments
291  *
292  * @buf1: First environment (struct environemnt_s *)
293  * @buf1_read_fail: 0 if buf1 is valid, non-zero if invalid
294  * @buf2: Second environment (struct environemnt_s *)
295  * @buf2_read_fail: 0 if buf2 is valid, non-zero if invalid
296  * @return 0 if OK, -EIO if no environment is valid, -ENOMSG if the CRC was bad
297  */
298 int env_import_redund(const char *buf1, int buf1_read_fail,
299                       const char *buf2, int buf2_read_fail);
300
301 /**
302  * env_get_default() - Look up a variable from the default environment
303  *
304  * @name: Variable to look up
305  * @return value if found, NULL if not found in default environment
306  */
307 char *env_get_default(const char *name);
308
309 /* [re]set to the default environment */
310 void env_set_default(const char *s, int flags);
311
312 /**
313  * env_get_char() - Get a character from the early environment
314  *
315  * This reads from the pre-relocation environment
316  *
317  * @index: Index of character to read (0 = first)
318  * @return character read, or -ve on error
319  */
320 int env_get_char(int index);
321
322 /**
323  * env_reloc() - Relocate the 'env' sub-commands
324  *
325  * This is used for those unfortunate archs with crappy toolchains
326  */
327 void env_reloc(void);
328
329 #endif