727f58528f9d3f3d4b9f9d01cbfbcbcd49b1098b
[oweals/u-boot.git] / include / env.h
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * Common environment functions
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 /**
18  * env_get_id() - Gets a sequence number for the environment
19  *
20  * This value increments every time the environment changes, so can be used an
21  * an indication of this
22  *
23  * @return environment ID
24  */
25 int env_get_id(void);
26
27 /**
28  * env_init() - Set up the pre-relocation environment
29  *
30  * This locates the environment or uses the default if nothing is available.
31  * This must be called before env_get() will work.
32  *
33  * @return 0 if OK, -ENODEV if no environment drivers are enabled
34  */
35 int env_init(void);
36
37 /**
38  * env_relocate() - Set up the post-relocation environment
39  *
40  * This loads the environment into RAM so that it can be modified. This is
41  * called after relocation, before the environment is used
42  */
43 void env_relocate(void);
44
45 /**
46  * env_match() - Match a name / name=value pair
47  *
48  * This is used prior to relocation for finding envrionment variables
49  *
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.
53  */
54 int env_match(unsigned char *name, int index);
55
56 /**
57  * env_get() - Look up the value of an environment variable
58  *
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().
62  *
63  * @varname:    Variable to look up
64  * @return value of variable, or NULL if not found
65  */
66 char *env_get(const char *varname);
67
68 /**
69  * env_get_f() - Look up the value of an environment variable (early)
70  *
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.
74  *
75  * @varname:    Variable to look up
76  * @return value of variable, or NULL if not found
77  */
78 int env_get_f(const char *name, char *buf, unsigned int len);
79
80 /**
81  * env_get_yesno() - Read an environment variable as a boolean
82  *
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
85  */
86 int env_get_yesno(const char *var);
87
88 /**
89  * env_set() - set an environment variable
90  *
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.
93  *
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
97  */
98 int env_set(const char *varname, const char *value);
99
100 /**
101  * env_get_ulong() - Return an environment variable as an integer value
102  *
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.
105  *
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
110  */
111 ulong env_get_ulong(const char *name, int base, ulong default_val);
112
113 /**
114  * env_set_ulong() - set an environment variable to an integer
115  *
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
119  */
120 int env_set_ulong(const char *varname, ulong value);
121
122 /**
123  * env_get_hex() - Return an environment variable as a hex value
124  *
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.
128  *
129  * @varname:            Variable to decode
130  * @default_val:        Value to return on error
131  */
132 ulong env_get_hex(const char *varname, ulong default_val);
133
134 /**
135  * env_set_hex() - set an environment variable to a hex value
136  *
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
140  */
141 int env_set_hex(const char *varname, ulong value);
142
143 /**
144  * env_set_addr - Set an environment variable to an address in hex
145  *
146  * @varname:    Environment variable to set
147  * @addr:       Value to set it to
148  * @return 0 if ok, 1 on error
149  */
150 static inline int env_set_addr(const char *varname, const void *addr)
151 {
152         return env_set_hex(varname, (ulong)addr);
153 }
154
155 /**
156  * env_complete() - return an auto-complete for environment variables
157  *
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)
165  */
166 int env_complete(char *var, int maxv, char *cmdv[], int maxsz, char *buf,
167                  bool dollar_comp);
168
169 /**
170  * eth_env_get_enetaddr() - Get an ethernet address from the environmnet
171  *
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
175  */
176 int eth_env_get_enetaddr(const char *name, uint8_t *enetaddr);
177
178 /**
179  * eth_env_set_enetaddr() - Set an ethernet address in the environmnet
180  *
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
184  */
185 int eth_env_set_enetaddr(const char *name, const uint8_t *enetaddr);
186
187 /**
188  * env_fix_drivers() - Updates envdriver as per relocation
189  */
190 void env_fix_drivers(void);
191
192 /**
193  * env_set_default_vars() - reset variables to their default value
194  *
195  * This resets individual variables to their value in the default environment
196  *
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)
200  */
201 int env_set_default_vars(int nvars, char *const vars[], int flags);
202
203 /**
204  * env_load() - Load the environment from storage
205  *
206  * @return 0 if OK, -ve on error
207  */
208 int env_load(void);
209
210 /**
211  * env_save() - Save the environment to storage
212  *
213  * @return 0 if OK, -ve on error
214  */
215 int env_save(void);
216
217 /**
218  * env_erase() - Erase the environment on storage
219  *
220  * @return 0 if OK, -ve on error
221  */
222 int env_erase(void);
223
224 /**
225  * env_import() - Import from a binary representation into hash table
226  *
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.
229  *
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
232  *      ignore it
233  * @return 0 if imported successfully, -ENOMSG if the CRC was bad, -EIO if
234  *      something else went wrong
235  */
236 int env_import(const char *buf, int check);
237
238 /**
239  * env_export() - Export the environment to a buffer
240  *
241  * Export from hash table into binary representation
242  *
243  * @env_out: Buffer to contain the environment (must be large enough!)
244  * @return 0 if OK, 1 on error
245  */
246 int env_export(struct environment_s *env_out);
247
248 /**
249  * env_import_redund() - Select and import one of two redundant environments
250  *
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
256  */
257 int env_import_redund(const char *buf1, int buf1_read_fail,
258                       const char *buf2, int buf2_read_fail);
259
260 /**
261  * env_get_default() - Look up a variable from the default environment
262  *
263  * @name: Variable to look up
264  * @return value if found, NULL if not found in default environment
265  */
266 char *env_get_default(const char *name);
267
268 /* [re]set to the default environment */
269 void env_set_default(const char *s, int flags);
270
271 /**
272  * env_get_char() - Get a character from the early environment
273  *
274  * This reads from the pre-relocation environment
275  *
276  * @index: Index of character to read (0 = first)
277  * @return character read, or -ve on error
278  */
279 int env_get_char(int index);
280
281 /**
282  * env_reloc() - Relocate the 'env' sub-commands
283  *
284  * This is used for those unfortunate archs with crappy toolchains
285  */
286 void env_reloc(void);
287
288 #endif