Simon Glass | af95f20 | 2019-08-01 09:46:40 -0600 | [diff] [blame] | 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> |
Simon Glass | c7694dd | 2019-08-01 09:46:46 -0600 | [diff] [blame] | 13 | #include <linux/types.h> |
Simon Glass | af95f20 | 2019-08-01 09:46:40 -0600 | [diff] [blame] | 14 | |
| 15 | /** |
Simon Glass | f1f0ae6 | 2019-08-01 09:46:41 -0600 | [diff] [blame] | 16 | * 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 | */ |
| 23 | int env_get_id(void); |
| 24 | |
| 25 | /** |
Simon Glass | 4bfd1f5 | 2019-08-01 09:46:43 -0600 | [diff] [blame] | 26 | * 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 | */ |
| 33 | int env_init(void); |
| 34 | |
| 35 | /** |
Simon Glass | 3f989e7b | 2019-08-01 09:46:44 -0600 | [diff] [blame] | 36 | * 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 | */ |
| 41 | void env_relocate(void); |
| 42 | |
| 43 | /** |
Simon Glass | b9ca02c | 2019-08-01 09:46:45 -0600 | [diff] [blame] | 44 | * 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 | */ |
| 52 | int env_match(unsigned char *name, int index); |
| 53 | |
| 54 | /** |
Simon Glass | 7b51b57 | 2019-08-01 09:46:52 -0600 | [diff] [blame] | 55 | * env_get() - Look up the value of an environment variable |
| 56 | * |
| 57 | * In U-Boot proper this can be called before relocation (which is when the |
| 58 | * environment is loaded from storage, i.e. GD_FLG_ENV_READY is 0). In that |
| 59 | * case this function calls env_get_f(). |
| 60 | * |
| 61 | * @varname: Variable to look up |
| 62 | * @return value of variable, or NULL if not found |
| 63 | */ |
| 64 | char *env_get(const char *varname); |
| 65 | |
| 66 | /** |
Simon Glass | 3a7d557 | 2019-08-01 09:46:42 -0600 | [diff] [blame] | 67 | * env_get_f() - Look up the value of an environment variable (early) |
| 68 | * |
| 69 | * This function is called from env_get() if the environment has not been |
| 70 | * loaded yet (GD_FLG_ENV_READY flag is 0). Some environment locations will |
| 71 | * support reading the value (slowly) and some will not. |
| 72 | * |
| 73 | * @varname: Variable to look up |
| 74 | * @return value of variable, or NULL if not found |
| 75 | */ |
| 76 | int env_get_f(const char *name, char *buf, unsigned int len); |
| 77 | |
| 78 | /** |
Simon Glass | 6bf6dbe | 2019-08-01 09:46:49 -0600 | [diff] [blame] | 79 | * env_get_yesno() - Read an environment variable as a boolean |
| 80 | * |
| 81 | * @return 1 if yes/true (Y/y/T/t), -1 if variable does not exist (i.e. default |
| 82 | * to true), 0 if otherwise |
| 83 | */ |
| 84 | int env_get_yesno(const char *var); |
| 85 | |
| 86 | /** |
Simon Glass | 9fb625c | 2019-08-01 09:46:51 -0600 | [diff] [blame] | 87 | * env_set() - set an environment variable |
| 88 | * |
| 89 | * This sets or deletes the value of an environment variable. For setting the |
| 90 | * value the variable is created if it does not already exist. |
| 91 | * |
| 92 | * @varname: Variable to adjust |
| 93 | * @value: Value to set for the variable, or NULL or "" to delete the variable |
| 94 | * @return 0 if OK, 1 on error |
| 95 | */ |
| 96 | int env_set(const char *varname, const char *value); |
| 97 | |
| 98 | /** |
Simon Glass | 9eef56d | 2019-08-01 09:46:48 -0600 | [diff] [blame] | 99 | * env_get_ulong() - Return an environment variable as an integer value |
| 100 | * |
| 101 | * Most U-Boot environment variables store hex values. For those which store |
| 102 | * (e.g.) base-10 integers, this function can be used to read the value. |
| 103 | * |
| 104 | * @name: Variable to look up |
| 105 | * @base: Base to use (e.g. 10 for base 10, 2 for binary) |
| 106 | * @default_val: Default value to return if no value is found |
| 107 | * @return the value found, or @default_val if none |
| 108 | */ |
| 109 | ulong env_get_ulong(const char *name, int base, ulong default_val); |
| 110 | |
| 111 | /** |
Simon Glass | 168068f | 2019-08-01 09:46:47 -0600 | [diff] [blame] | 112 | * env_set_ulong() - set an environment variable to an integer |
| 113 | * |
| 114 | * @varname: Variable to adjust |
| 115 | * @value: Value to set for the variable (will be converted to a string) |
| 116 | * @return 0 if OK, 1 on error |
| 117 | */ |
| 118 | int env_set_ulong(const char *varname, ulong value); |
| 119 | |
| 120 | /** |
Simon Glass | cdbff9f | 2019-08-01 09:46:50 -0600 | [diff] [blame] | 121 | * env_get_hex() - Return an environment variable as a hex value |
| 122 | * |
| 123 | * Decode an environment as a hex number (it may or may not have a 0x |
| 124 | * prefix). If the environment variable cannot be found, or does not start |
| 125 | * with hex digits, the default value is returned. |
| 126 | * |
| 127 | * @varname: Variable to decode |
| 128 | * @default_val: Value to return on error |
| 129 | */ |
| 130 | ulong env_get_hex(const char *varname, ulong default_val); |
| 131 | |
| 132 | /** |
Simon Glass | c7694dd | 2019-08-01 09:46:46 -0600 | [diff] [blame] | 133 | * env_set_hex() - set an environment variable to a hex value |
| 134 | * |
| 135 | * @varname: Variable to adjust |
| 136 | * @value: Value to set for the variable (will be converted to a hex string) |
| 137 | * @return 0 if OK, 1 on error |
| 138 | */ |
| 139 | int env_set_hex(const char *varname, ulong value); |
| 140 | |
| 141 | /** |
| 142 | * env_set_addr - Set an environment variable to an address in hex |
| 143 | * |
| 144 | * @varname: Environment variable to set |
| 145 | * @addr: Value to set it to |
| 146 | * @return 0 if ok, 1 on error |
| 147 | */ |
| 148 | static inline int env_set_addr(const char *varname, const void *addr) |
| 149 | { |
| 150 | return env_set_hex(varname, (ulong)addr); |
| 151 | } |
| 152 | |
| 153 | /** |
Simon Glass | af95f20 | 2019-08-01 09:46:40 -0600 | [diff] [blame] | 154 | * env_complete() - return an auto-complete for environment variables |
| 155 | * |
| 156 | * @var: partial name to auto-complete |
| 157 | * @maxv: Maximum number of matches to return |
| 158 | * @cmdv: Returns a list of possible matches |
| 159 | * @maxsz: Size of buffer to use for matches |
| 160 | * @buf: Buffer to use for matches |
| 161 | * @dollar_comp: non-zero to wrap each match in ${...} |
| 162 | * @return number of matches found (in @cmdv) |
| 163 | */ |
| 164 | int env_complete(char *var, int maxv, char *cmdv[], int maxsz, char *buf, |
| 165 | bool dollar_comp); |
| 166 | |
Simon Glass | b79cf1a | 2019-08-01 09:46:53 -0600 | [diff] [blame] | 167 | /** |
| 168 | * eth_env_get_enetaddr() - Get an ethernet address from the environmnet |
| 169 | * |
| 170 | * @name: Environment variable to get (e.g. "ethaddr") |
| 171 | * @enetaddr: Place to put MAC address (6 bytes) |
| 172 | * @return 0 if OK, 1 on error |
| 173 | */ |
| 174 | int eth_env_get_enetaddr(const char *name, uint8_t *enetaddr); |
| 175 | |
| 176 | /** |
| 177 | * eth_env_set_enetaddr() - Set an ethernet address in the environmnet |
| 178 | * |
| 179 | * @name: Environment variable to set (e.g. "ethaddr") |
| 180 | * @enetaddr: Pointer to MAC address to put into the variable (6 bytes) |
| 181 | * @return 0 if OK, 1 on error |
| 182 | */ |
| 183 | int eth_env_set_enetaddr(const char *name, const uint8_t *enetaddr); |
| 184 | |
Simon Glass | 03ed918 | 2019-08-01 09:46:55 -0600 | [diff] [blame] | 185 | /** |
| 186 | * env_fix_drivers() - Updates envdriver as per relocation |
| 187 | */ |
| 188 | void env_fix_drivers(void); |
| 189 | |
Simon Glass | 0b9d8a0 | 2019-08-01 09:46:56 -0600 | [diff] [blame^] | 190 | /** |
| 191 | * env_set_default_vars() - reset variables to their default value |
| 192 | * |
| 193 | * This resets individual variables to their value in the default environment |
| 194 | * |
| 195 | * @nvars: Number of variables to set/reset |
| 196 | * @vars: List of variables to set/reset |
| 197 | * @flags: Flags controlling matching (H_... - see search.h) |
| 198 | */ |
| 199 | int env_set_default_vars(int nvars, char *const vars[], int flags); |
| 200 | |
Simon Glass | af95f20 | 2019-08-01 09:46:40 -0600 | [diff] [blame] | 201 | #endif |