blob: b59c3c36f20238ccd61f38cb1a3772c45e925b24 [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
12#include <stdbool.h>
Simon Glassc7694dd2019-08-01 09:46:46 -060013#include <linux/types.h>
Simon Glassaf95f202019-08-01 09:46:40 -060014
Simon Glass4e9ce8a2019-08-01 09:46:58 -060015struct environment_s;
16
Simon Glassdb197012019-08-01 09:47:04 -060017/* Value for environment validity */
18enum 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
Simon Glassaf95f202019-08-01 09:46:40 -060024/**
Simon Glassf1f0ae62019-08-01 09:46:41 -060025 * env_get_id() - Gets a sequence number for the environment
26 *
27 * This value increments every time the environment changes, so can be used an
28 * an indication of this
29 *
30 * @return environment ID
31 */
32int env_get_id(void);
33
34/**
Simon Glass4bfd1f52019-08-01 09:46:43 -060035 * env_init() - Set up the pre-relocation environment
36 *
37 * This locates the environment or uses the default if nothing is available.
38 * This must be called before env_get() will work.
39 *
40 * @return 0 if OK, -ENODEV if no environment drivers are enabled
41 */
42int env_init(void);
43
44/**
Simon Glass3f989e7b2019-08-01 09:46:44 -060045 * env_relocate() - Set up the post-relocation environment
46 *
47 * This loads the environment into RAM so that it can be modified. This is
48 * called after relocation, before the environment is used
49 */
50void env_relocate(void);
51
52/**
Simon Glassb9ca02c2019-08-01 09:46:45 -060053 * env_match() - Match a name / name=value pair
54 *
55 * This is used prior to relocation for finding envrionment variables
56 *
57 * @name: A simple 'name', or a 'name=value' pair.
58 * @index: The environment index for a 'name2=value2' pair.
59 * @return index for the value if the names match, else -1.
60 */
61int env_match(unsigned char *name, int index);
62
63/**
Simon Glass7b51b572019-08-01 09:46:52 -060064 * env_get() - Look up the value of an environment variable
65 *
66 * In U-Boot proper this can be called before relocation (which is when the
67 * environment is loaded from storage, i.e. GD_FLG_ENV_READY is 0). In that
68 * case this function calls env_get_f().
69 *
70 * @varname: Variable to look up
71 * @return value of variable, or NULL if not found
72 */
73char *env_get(const char *varname);
74
75/**
Simon Glass3a7d5572019-08-01 09:46:42 -060076 * env_get_f() - Look up the value of an environment variable (early)
77 *
78 * This function is called from env_get() if the environment has not been
79 * loaded yet (GD_FLG_ENV_READY flag is 0). Some environment locations will
80 * support reading the value (slowly) and some will not.
81 *
82 * @varname: Variable to look up
83 * @return value of variable, or NULL if not found
84 */
85int env_get_f(const char *name, char *buf, unsigned int len);
86
87/**
Simon Glass6bf6dbe2019-08-01 09:46:49 -060088 * env_get_yesno() - Read an environment variable as a boolean
89 *
90 * @return 1 if yes/true (Y/y/T/t), -1 if variable does not exist (i.e. default
91 * to true), 0 if otherwise
92 */
93int env_get_yesno(const char *var);
94
95/**
Simon Glass9fb625c2019-08-01 09:46:51 -060096 * env_set() - set an environment variable
97 *
98 * This sets or deletes the value of an environment variable. For setting the
99 * value the variable is created if it does not already exist.
100 *
101 * @varname: Variable to adjust
102 * @value: Value to set for the variable, or NULL or "" to delete the variable
103 * @return 0 if OK, 1 on error
104 */
105int env_set(const char *varname, const char *value);
106
107/**
Simon Glass9eef56d2019-08-01 09:46:48 -0600108 * env_get_ulong() - Return an environment variable as an integer value
109 *
110 * Most U-Boot environment variables store hex values. For those which store
111 * (e.g.) base-10 integers, this function can be used to read the value.
112 *
113 * @name: Variable to look up
114 * @base: Base to use (e.g. 10 for base 10, 2 for binary)
115 * @default_val: Default value to return if no value is found
116 * @return the value found, or @default_val if none
117 */
118ulong env_get_ulong(const char *name, int base, ulong default_val);
119
120/**
Simon Glass168068f2019-08-01 09:46:47 -0600121 * env_set_ulong() - set an environment variable to an integer
122 *
123 * @varname: Variable to adjust
124 * @value: Value to set for the variable (will be converted to a string)
125 * @return 0 if OK, 1 on error
126 */
127int env_set_ulong(const char *varname, ulong value);
128
129/**
Simon Glasscdbff9f2019-08-01 09:46:50 -0600130 * env_get_hex() - Return an environment variable as a hex value
131 *
132 * Decode an environment as a hex number (it may or may not have a 0x
133 * prefix). If the environment variable cannot be found, or does not start
134 * with hex digits, the default value is returned.
135 *
136 * @varname: Variable to decode
137 * @default_val: Value to return on error
138 */
139ulong env_get_hex(const char *varname, ulong default_val);
140
141/**
Simon Glassc7694dd2019-08-01 09:46:46 -0600142 * env_set_hex() - set an environment variable to a hex value
143 *
144 * @varname: Variable to adjust
145 * @value: Value to set for the variable (will be converted to a hex string)
146 * @return 0 if OK, 1 on error
147 */
148int env_set_hex(const char *varname, ulong value);
149
150/**
151 * env_set_addr - Set an environment variable to an address in hex
152 *
153 * @varname: Environment variable to set
154 * @addr: Value to set it to
155 * @return 0 if ok, 1 on error
156 */
157static inline int env_set_addr(const char *varname, const void *addr)
158{
159 return env_set_hex(varname, (ulong)addr);
160}
161
162/**
Simon Glassaf95f202019-08-01 09:46:40 -0600163 * env_complete() - return an auto-complete for environment variables
164 *
165 * @var: partial name to auto-complete
166 * @maxv: Maximum number of matches to return
167 * @cmdv: Returns a list of possible matches
168 * @maxsz: Size of buffer to use for matches
169 * @buf: Buffer to use for matches
170 * @dollar_comp: non-zero to wrap each match in ${...}
171 * @return number of matches found (in @cmdv)
172 */
173int env_complete(char *var, int maxv, char *cmdv[], int maxsz, char *buf,
174 bool dollar_comp);
175
Simon Glassb79cf1a2019-08-01 09:46:53 -0600176/**
177 * eth_env_get_enetaddr() - Get an ethernet address from the environmnet
178 *
179 * @name: Environment variable to get (e.g. "ethaddr")
180 * @enetaddr: Place to put MAC address (6 bytes)
181 * @return 0 if OK, 1 on error
182 */
183int eth_env_get_enetaddr(const char *name, uint8_t *enetaddr);
184
185/**
186 * eth_env_set_enetaddr() - Set an ethernet address in the environmnet
187 *
188 * @name: Environment variable to set (e.g. "ethaddr")
189 * @enetaddr: Pointer to MAC address to put into the variable (6 bytes)
190 * @return 0 if OK, 1 on error
191 */
192int eth_env_set_enetaddr(const char *name, const uint8_t *enetaddr);
193
Simon Glass03ed9182019-08-01 09:46:55 -0600194/**
195 * env_fix_drivers() - Updates envdriver as per relocation
196 */
197void env_fix_drivers(void);
198
Simon Glass0b9d8a02019-08-01 09:46:56 -0600199/**
200 * env_set_default_vars() - reset variables to their default value
201 *
202 * This resets individual variables to their value in the default environment
203 *
204 * @nvars: Number of variables to set/reset
205 * @vars: List of variables to set/reset
206 * @flags: Flags controlling matching (H_... - see search.h)
207 */
208int env_set_default_vars(int nvars, char *const vars[], int flags);
209
Simon Glass4be490a2019-08-01 09:46:57 -0600210/**
211 * env_load() - Load the environment from storage
212 *
213 * @return 0 if OK, -ve on error
214 */
215int env_load(void);
216
217/**
218 * env_save() - Save the environment to storage
219 *
220 * @return 0 if OK, -ve on error
221 */
222int env_save(void);
223
224/**
225 * env_erase() - Erase the environment on storage
226 *
227 * @return 0 if OK, -ve on error
228 */
229int env_erase(void);
230
Simon Glass4e9ce8a2019-08-01 09:46:58 -0600231/**
232 * env_import() - Import from a binary representation into hash table
233 *
234 * This imports the environment from a buffer. The format for each variable is
235 * var=value\0 with a double \0 at the end of the buffer.
236 *
237 * @buf: Buffer containing the environment (struct environemnt_s *)
238 * @check: non-zero to check the CRC at the start of the environment, 0 to
239 * ignore it
240 * @return 0 if imported successfully, -ENOMSG if the CRC was bad, -EIO if
241 * something else went wrong
242 */
243int env_import(const char *buf, int check);
244
245/**
246 * env_export() - Export the environment to a buffer
247 *
248 * Export from hash table into binary representation
249 *
250 * @env_out: Buffer to contain the environment (must be large enough!)
251 * @return 0 if OK, 1 on error
252 */
253int env_export(struct environment_s *env_out);
254
255/**
256 * env_import_redund() - Select and import one of two redundant environments
257 *
258 * @buf1: First environment (struct environemnt_s *)
259 * @buf1_read_fail: 0 if buf1 is valid, non-zero if invalid
260 * @buf2: Second environment (struct environemnt_s *)
261 * @buf2_read_fail: 0 if buf2 is valid, non-zero if invalid
262 * @return 0 if OK, -EIO if no environment is valid, -ENOMSG if the CRC was bad
263 */
264int env_import_redund(const char *buf1, int buf1_read_fail,
265 const char *buf2, int buf2_read_fail);
266
Simon Glass0ac7d722019-08-01 09:47:00 -0600267/**
268 * env_get_default() - Look up a variable from the default environment
269 *
270 * @name: Variable to look up
271 * @return value if found, NULL if not found in default environment
272 */
273char *env_get_default(const char *name);
274
275/* [re]set to the default environment */
276void env_set_default(const char *s, int flags);
277
Simon Glassb5f449e2019-08-01 09:47:01 -0600278/**
279 * env_get_char() - Get a character from the early environment
280 *
281 * This reads from the pre-relocation environment
282 *
283 * @index: Index of character to read (0 = first)
284 * @return character read, or -ve on error
285 */
286int env_get_char(int index);
287
Simon Glassc62f62b2019-08-01 09:47:02 -0600288/**
289 * env_reloc() - Relocate the 'env' sub-commands
290 *
291 * This is used for those unfortunate archs with crappy toolchains
292 */
293void env_reloc(void);
294
Simon Glassaf95f202019-08-01 09:46:40 -0600295#endif