blob: 42d8bcfa5052623c1b8e957be06ccb36b73579fa [file] [log] [blame]
Simon Glassaf95f202019-08-01 09:46:40 -06001/* 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>
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 Glassaf95f202019-08-01 09:46:40 -060017/**
Simon Glassf1f0ae62019-08-01 09:46:41 -060018 * 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 */
25int env_get_id(void);
26
27/**
Simon Glass4bfd1f52019-08-01 09:46:43 -060028 * 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 */
35int env_init(void);
36
37/**
Simon Glass3f989e7b2019-08-01 09:46:44 -060038 * 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 */
43void env_relocate(void);
44
45/**
Simon Glassb9ca02c2019-08-01 09:46:45 -060046 * 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 */
54int env_match(unsigned char *name, int index);
55
56/**
Simon Glass7b51b572019-08-01 09:46:52 -060057 * 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 */
66char *env_get(const char *varname);
67
68/**
Simon Glass3a7d5572019-08-01 09:46:42 -060069 * 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 */
78int env_get_f(const char *name, char *buf, unsigned int len);
79
80/**
Simon Glass6bf6dbe2019-08-01 09:46:49 -060081 * 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 */
86int env_get_yesno(const char *var);
87
88/**
Simon Glass9fb625c2019-08-01 09:46:51 -060089 * 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 */
98int env_set(const char *varname, const char *value);
99
100/**
Simon Glass9eef56d2019-08-01 09:46:48 -0600101 * 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 */
111ulong env_get_ulong(const char *name, int base, ulong default_val);
112
113/**
Simon Glass168068f2019-08-01 09:46:47 -0600114 * 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 */
120int env_set_ulong(const char *varname, ulong value);
121
122/**
Simon Glasscdbff9f2019-08-01 09:46:50 -0600123 * 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 */
132ulong env_get_hex(const char *varname, ulong default_val);
133
134/**
Simon Glassc7694dd2019-08-01 09:46:46 -0600135 * 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 */
141int 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 */
150static inline int env_set_addr(const char *varname, const void *addr)
151{
152 return env_set_hex(varname, (ulong)addr);
153}
154
155/**
Simon Glassaf95f202019-08-01 09:46:40 -0600156 * 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 */
166int env_complete(char *var, int maxv, char *cmdv[], int maxsz, char *buf,
167 bool dollar_comp);
168
Simon Glassb79cf1a2019-08-01 09:46:53 -0600169/**
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 */
176int 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 */
185int eth_env_set_enetaddr(const char *name, const uint8_t *enetaddr);
186
Simon Glass03ed9182019-08-01 09:46:55 -0600187/**
188 * env_fix_drivers() - Updates envdriver as per relocation
189 */
190void env_fix_drivers(void);
191
Simon Glass0b9d8a02019-08-01 09:46:56 -0600192/**
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 */
201int env_set_default_vars(int nvars, char *const vars[], int flags);
202
Simon Glass4be490a2019-08-01 09:46:57 -0600203/**
204 * env_load() - Load the environment from storage
205 *
206 * @return 0 if OK, -ve on error
207 */
208int env_load(void);
209
210/**
211 * env_save() - Save the environment to storage
212 *
213 * @return 0 if OK, -ve on error
214 */
215int env_save(void);
216
217/**
218 * env_erase() - Erase the environment on storage
219 *
220 * @return 0 if OK, -ve on error
221 */
222int env_erase(void);
223
Simon Glass4e9ce8a2019-08-01 09:46:58 -0600224/**
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 */
236int 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 */
246int 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 */
257int env_import_redund(const char *buf1, int buf1_read_fail,
258 const char *buf2, int buf2_read_fail);
259
Simon Glass0ac7d722019-08-01 09:47:00 -0600260/**
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 */
266char *env_get_default(const char *name);
267
268/* [re]set to the default environment */
269void env_set_default(const char *s, int flags);
270
Simon Glassb5f449e2019-08-01 09:47:01 -0600271/**
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 */
279int env_get_char(int index);
280
Simon Glassaf95f202019-08-01 09:46:40 -0600281#endif