blob: 6770a6176dfcb36e6c48e6a1b080057cbc101181 [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
15/**
Simon Glassf1f0ae62019-08-01 09:46:41 -060016 * env_get_id() - Gets a sequence number for the environment
17 *
18 * This value increments every time the environment changes, so can be used an
19 * an indication of this
20 *
21 * @return environment ID
22 */
23int env_get_id(void);
24
25/**
Simon Glass4bfd1f52019-08-01 09:46:43 -060026 * env_init() - Set up the pre-relocation environment
27 *
28 * This locates the environment or uses the default if nothing is available.
29 * This must be called before env_get() will work.
30 *
31 * @return 0 if OK, -ENODEV if no environment drivers are enabled
32 */
33int env_init(void);
34
35/**
Simon Glass3f989e7b2019-08-01 09:46:44 -060036 * env_relocate() - Set up the post-relocation environment
37 *
38 * This loads the environment into RAM so that it can be modified. This is
39 * called after relocation, before the environment is used
40 */
41void env_relocate(void);
42
43/**
Simon Glassb9ca02c2019-08-01 09:46:45 -060044 * env_match() - Match a name / name=value pair
45 *
46 * This is used prior to relocation for finding envrionment variables
47 *
48 * @name: A simple 'name', or a 'name=value' pair.
49 * @index: The environment index for a 'name2=value2' pair.
50 * @return index for the value if the names match, else -1.
51 */
52int env_match(unsigned char *name, int index);
53
54/**
Simon Glass3a7d5572019-08-01 09:46:42 -060055 * env_get_f() - Look up the value of an environment variable (early)
56 *
57 * This function is called from env_get() if the environment has not been
58 * loaded yet (GD_FLG_ENV_READY flag is 0). Some environment locations will
59 * support reading the value (slowly) and some will not.
60 *
61 * @varname: Variable to look up
62 * @return value of variable, or NULL if not found
63 */
64int env_get_f(const char *name, char *buf, unsigned int len);
65
66/**
Simon Glass6bf6dbe2019-08-01 09:46:49 -060067 * env_get_yesno() - Read an environment variable as a boolean
68 *
69 * @return 1 if yes/true (Y/y/T/t), -1 if variable does not exist (i.e. default
70 * to true), 0 if otherwise
71 */
72int env_get_yesno(const char *var);
73
74/**
Simon Glass9fb625c2019-08-01 09:46:51 -060075 * env_set() - set an environment variable
76 *
77 * This sets or deletes the value of an environment variable. For setting the
78 * value the variable is created if it does not already exist.
79 *
80 * @varname: Variable to adjust
81 * @value: Value to set for the variable, or NULL or "" to delete the variable
82 * @return 0 if OK, 1 on error
83 */
84int env_set(const char *varname, const char *value);
85
86/**
Simon Glass9eef56d2019-08-01 09:46:48 -060087 * env_get_ulong() - Return an environment variable as an integer value
88 *
89 * Most U-Boot environment variables store hex values. For those which store
90 * (e.g.) base-10 integers, this function can be used to read the value.
91 *
92 * @name: Variable to look up
93 * @base: Base to use (e.g. 10 for base 10, 2 for binary)
94 * @default_val: Default value to return if no value is found
95 * @return the value found, or @default_val if none
96 */
97ulong env_get_ulong(const char *name, int base, ulong default_val);
98
99/**
Simon Glass168068f2019-08-01 09:46:47 -0600100 * env_set_ulong() - set an environment variable to an integer
101 *
102 * @varname: Variable to adjust
103 * @value: Value to set for the variable (will be converted to a string)
104 * @return 0 if OK, 1 on error
105 */
106int env_set_ulong(const char *varname, ulong value);
107
108/**
Simon Glasscdbff9f2019-08-01 09:46:50 -0600109 * env_get_hex() - Return an environment variable as a hex value
110 *
111 * Decode an environment as a hex number (it may or may not have a 0x
112 * prefix). If the environment variable cannot be found, or does not start
113 * with hex digits, the default value is returned.
114 *
115 * @varname: Variable to decode
116 * @default_val: Value to return on error
117 */
118ulong env_get_hex(const char *varname, ulong default_val);
119
120/**
Simon Glassc7694dd2019-08-01 09:46:46 -0600121 * env_set_hex() - set an environment variable to a hex value
122 *
123 * @varname: Variable to adjust
124 * @value: Value to set for the variable (will be converted to a hex string)
125 * @return 0 if OK, 1 on error
126 */
127int env_set_hex(const char *varname, ulong value);
128
129/**
130 * env_set_addr - Set an environment variable to an address in hex
131 *
132 * @varname: Environment variable to set
133 * @addr: Value to set it to
134 * @return 0 if ok, 1 on error
135 */
136static inline int env_set_addr(const char *varname, const void *addr)
137{
138 return env_set_hex(varname, (ulong)addr);
139}
140
141/**
Simon Glassaf95f202019-08-01 09:46:40 -0600142 * env_complete() - return an auto-complete for environment variables
143 *
144 * @var: partial name to auto-complete
145 * @maxv: Maximum number of matches to return
146 * @cmdv: Returns a list of possible matches
147 * @maxsz: Size of buffer to use for matches
148 * @buf: Buffer to use for matches
149 * @dollar_comp: non-zero to wrap each match in ${...}
150 * @return number of matches found (in @cmdv)
151 */
152int env_complete(char *var, int maxv, char *cmdv[], int maxsz, char *buf,
153 bool dollar_comp);
154
155#endif