env.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * Common environment functions and definitions
  4. *
  5. * (C) Copyright 2000-2009
  6. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  7. */
  8. #ifndef __ENV_H
  9. #define __ENV_H
  10. #include <compiler.h>
  11. #include <stdbool.h>
  12. #include <linux/types.h>
  13. struct environment_s;
  14. /* Value for environment validity */
  15. enum env_valid {
  16. ENV_INVALID, /* No valid environment */
  17. ENV_VALID, /* First or only environment is valid */
  18. ENV_REDUND, /* Redundant environment is valid */
  19. };
  20. /** enum env_op - environment callback operation */
  21. enum env_op {
  22. env_op_create,
  23. env_op_delete,
  24. env_op_overwrite,
  25. };
  26. /** struct env_clbk_tbl - declares a new callback */
  27. struct env_clbk_tbl {
  28. const char *name; /* Callback name */
  29. int (*callback)(const char *name, const char *value, enum env_op op,
  30. int flags);
  31. };
  32. /*
  33. * Define a callback that can be associated with variables.
  34. * when associated through the ".callbacks" environment variable, the callback
  35. * will be executed any time the variable is inserted, overwritten, or deleted.
  36. *
  37. * For SPL these are silently dropped to reduce code size, since environment
  38. * callbacks are not supported with SPL.
  39. */
  40. #ifdef CONFIG_SPL_BUILD
  41. #define U_BOOT_ENV_CALLBACK(name, callback) \
  42. static inline __maybe_unused void _u_boot_env_noop_##name(void) \
  43. { \
  44. (void)callback; \
  45. }
  46. #else
  47. #define U_BOOT_ENV_CALLBACK(name, callback) \
  48. ll_entry_declare(struct env_clbk_tbl, name, env_clbk) = \
  49. {#name, callback}
  50. #endif
  51. /** enum env_redund_flags - Flags for the redundand_environment */
  52. enum env_redund_flags {
  53. ENV_REDUND_OBSOLETE = 0,
  54. ENV_REDUND_ACTIVE = 1,
  55. };
  56. /**
  57. * env_get_id() - Gets a sequence number for the environment
  58. *
  59. * This value increments every time the environment changes, so can be used an
  60. * an indication of this
  61. *
  62. * @return environment ID
  63. */
  64. int env_get_id(void);
  65. /**
  66. * env_init() - Set up the pre-relocation environment
  67. *
  68. * This locates the environment or uses the default if nothing is available.
  69. * This must be called before env_get() will work.
  70. *
  71. * @return 0 if OK, -ENODEV if no environment drivers are enabled
  72. */
  73. int env_init(void);
  74. /**
  75. * env_relocate() - Set up the post-relocation environment
  76. *
  77. * This loads the environment into RAM so that it can be modified. This is
  78. * called after relocation, before the environment is used
  79. */
  80. void env_relocate(void);
  81. /**
  82. * env_match() - Match a name / name=value pair
  83. *
  84. * This is used prior to relocation for finding envrionment variables
  85. *
  86. * @name: A simple 'name', or a 'name=value' pair.
  87. * @index: The environment index for a 'name2=value2' pair.
  88. * @return index for the value if the names match, else -1.
  89. */
  90. int env_match(unsigned char *name, int index);
  91. /**
  92. * env_get() - Look up the value of an environment variable
  93. *
  94. * In U-Boot proper this can be called before relocation (which is when the
  95. * environment is loaded from storage, i.e. GD_FLG_ENV_READY is 0). In that
  96. * case this function calls env_get_f().
  97. *
  98. * @varname: Variable to look up
  99. * @return value of variable, or NULL if not found
  100. */
  101. char *env_get(const char *varname);
  102. /*
  103. * Like env_get, but prints an error if envvar isn't defined in the
  104. * environment. It always returns what env_get does, so it can be used in
  105. * place of env_get without changing error handling otherwise.
  106. *
  107. * @varname: Variable to look up
  108. * @return value of variable, or NULL if not found
  109. */
  110. char *from_env(const char *envvar);
  111. /**
  112. * env_get_f() - Look up the value of an environment variable (early)
  113. *
  114. * This function is called from env_get() if the environment has not been
  115. * loaded yet (GD_FLG_ENV_READY flag is 0). Some environment locations will
  116. * support reading the value (slowly) and some will not.
  117. *
  118. * @varname: Variable to look up
  119. * @return value of variable, or NULL if not found
  120. */
  121. int env_get_f(const char *name, char *buf, unsigned int len);
  122. /**
  123. * env_get_yesno() - Read an environment variable as a boolean
  124. *
  125. * @return 1 if yes/true (Y/y/T/t), -1 if variable does not exist (i.e. default
  126. * to true), 0 if otherwise
  127. */
  128. int env_get_yesno(const char *var);
  129. /**
  130. * env_set() - set an environment variable
  131. *
  132. * This sets or deletes the value of an environment variable. For setting the
  133. * value the variable is created if it does not already exist.
  134. *
  135. * @varname: Variable to adjust
  136. * @value: Value to set for the variable, or NULL or "" to delete the variable
  137. * @return 0 if OK, 1 on error
  138. */
  139. int env_set(const char *varname, const char *value);
  140. /**
  141. * env_get_ulong() - Return an environment variable as an integer value
  142. *
  143. * Most U-Boot environment variables store hex values. For those which store
  144. * (e.g.) base-10 integers, this function can be used to read the value.
  145. *
  146. * @name: Variable to look up
  147. * @base: Base to use (e.g. 10 for base 10, 2 for binary)
  148. * @default_val: Default value to return if no value is found
  149. * @return the value found, or @default_val if none
  150. */
  151. ulong env_get_ulong(const char *name, int base, ulong default_val);
  152. /**
  153. * env_set_ulong() - set an environment variable to an integer
  154. *
  155. * @varname: Variable to adjust
  156. * @value: Value to set for the variable (will be converted to a string)
  157. * @return 0 if OK, 1 on error
  158. */
  159. int env_set_ulong(const char *varname, ulong value);
  160. /**
  161. * env_get_hex() - Return an environment variable as a hex value
  162. *
  163. * Decode an environment as a hex number (it may or may not have a 0x
  164. * prefix). If the environment variable cannot be found, or does not start
  165. * with hex digits, the default value is returned.
  166. *
  167. * @varname: Variable to decode
  168. * @default_val: Value to return on error
  169. */
  170. ulong env_get_hex(const char *varname, ulong default_val);
  171. /**
  172. * env_set_hex() - set an environment variable to a hex value
  173. *
  174. * @varname: Variable to adjust
  175. * @value: Value to set for the variable (will be converted to a hex string)
  176. * @return 0 if OK, 1 on error
  177. */
  178. int env_set_hex(const char *varname, ulong value);
  179. /**
  180. * env_set_addr - Set an environment variable to an address in hex
  181. *
  182. * @varname: Environment variable to set
  183. * @addr: Value to set it to
  184. * @return 0 if ok, 1 on error
  185. */
  186. static inline int env_set_addr(const char *varname, const void *addr)
  187. {
  188. return env_set_hex(varname, (ulong)addr);
  189. }
  190. /**
  191. * env_complete() - return an auto-complete for environment variables
  192. *
  193. * @var: partial name to auto-complete
  194. * @maxv: Maximum number of matches to return
  195. * @cmdv: Returns a list of possible matches
  196. * @maxsz: Size of buffer to use for matches
  197. * @buf: Buffer to use for matches
  198. * @dollar_comp: non-zero to wrap each match in ${...}
  199. * @return number of matches found (in @cmdv)
  200. */
  201. int env_complete(char *var, int maxv, char *cmdv[], int maxsz, char *buf,
  202. bool dollar_comp);
  203. /**
  204. * eth_env_get_enetaddr() - Get an ethernet address from the environmnet
  205. *
  206. * @name: Environment variable to get (e.g. "ethaddr")
  207. * @enetaddr: Place to put MAC address (6 bytes)
  208. * @return 0 if OK, 1 on error
  209. */
  210. int eth_env_get_enetaddr(const char *name, uint8_t *enetaddr);
  211. /**
  212. * eth_env_set_enetaddr() - Set an ethernet address in the environmnet
  213. *
  214. * @name: Environment variable to set (e.g. "ethaddr")
  215. * @enetaddr: Pointer to MAC address to put into the variable (6 bytes)
  216. * @return 0 if OK, 1 on error
  217. */
  218. int eth_env_set_enetaddr(const char *name, const uint8_t *enetaddr);
  219. /**
  220. * env_fix_drivers() - Updates envdriver as per relocation
  221. */
  222. void env_fix_drivers(void);
  223. /**
  224. * env_set_default_vars() - reset variables to their default value
  225. *
  226. * This resets individual variables to their value in the default environment
  227. *
  228. * @nvars: Number of variables to set/reset
  229. * @vars: List of variables to set/reset
  230. * @flags: Flags controlling matching (H_... - see search.h)
  231. */
  232. int env_set_default_vars(int nvars, char *const vars[], int flags);
  233. /**
  234. * env_load() - Load the environment from storage
  235. *
  236. * @return 0 if OK, -ve on error
  237. */
  238. int env_load(void);
  239. /**
  240. * env_save() - Save the environment to storage
  241. *
  242. * @return 0 if OK, -ve on error
  243. */
  244. int env_save(void);
  245. /**
  246. * env_erase() - Erase the environment on storage
  247. *
  248. * @return 0 if OK, -ve on error
  249. */
  250. int env_erase(void);
  251. /**
  252. * env_import() - Import from a binary representation into hash table
  253. *
  254. * This imports the environment from a buffer. The format for each variable is
  255. * var=value\0 with a double \0 at the end of the buffer.
  256. *
  257. * @buf: Buffer containing the environment (struct environemnt_s *)
  258. * @check: non-zero to check the CRC at the start of the environment, 0 to
  259. * ignore it
  260. * @return 0 if imported successfully, -ENOMSG if the CRC was bad, -EIO if
  261. * something else went wrong
  262. */
  263. int env_import(const char *buf, int check);
  264. /**
  265. * env_export() - Export the environment to a buffer
  266. *
  267. * Export from hash table into binary representation
  268. *
  269. * @env_out: Buffer to contain the environment (must be large enough!)
  270. * @return 0 if OK, 1 on error
  271. */
  272. int env_export(struct environment_s *env_out);
  273. /**
  274. * env_import_redund() - Select and import one of two redundant environments
  275. *
  276. * @buf1: First environment (struct environemnt_s *)
  277. * @buf1_read_fail: 0 if buf1 is valid, non-zero if invalid
  278. * @buf2: Second environment (struct environemnt_s *)
  279. * @buf2_read_fail: 0 if buf2 is valid, non-zero if invalid
  280. * @return 0 if OK, -EIO if no environment is valid, -ENOMSG if the CRC was bad
  281. */
  282. int env_import_redund(const char *buf1, int buf1_read_fail,
  283. const char *buf2, int buf2_read_fail);
  284. /**
  285. * env_get_default() - Look up a variable from the default environment
  286. *
  287. * @name: Variable to look up
  288. * @return value if found, NULL if not found in default environment
  289. */
  290. char *env_get_default(const char *name);
  291. /* [re]set to the default environment */
  292. void env_set_default(const char *s, int flags);
  293. /**
  294. * env_get_char() - Get a character from the early environment
  295. *
  296. * This reads from the pre-relocation environment
  297. *
  298. * @index: Index of character to read (0 = first)
  299. * @return character read, or -ve on error
  300. */
  301. int env_get_char(int index);
  302. /**
  303. * env_reloc() - Relocate the 'env' sub-commands
  304. *
  305. * This is used for those unfortunate archs with crappy toolchains
  306. */
  307. void env_reloc(void);
  308. #endif