fw_env.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * (C) Copyright 2002-2008
  4. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  5. */
  6. #include <env.h>
  7. #include <stdint.h>
  8. /*
  9. * Programs using the library must check which API is available,
  10. * that varies depending on the U-Boot version.
  11. * This can be changed in future
  12. */
  13. #define FW_ENV_API_VERSION 1
  14. struct env_opts {
  15. #ifdef CONFIG_FILE
  16. char *config_file;
  17. #endif
  18. char *lockname;
  19. };
  20. /**
  21. * fw_printenv() - print one or several environment variables
  22. *
  23. * @argc: number of variables names to be printed, prints all if 0
  24. * @argv: array of variable names to be printed, if argc != 0
  25. * @value_only: do not repeat the variable name, print the bare value,
  26. * only one variable allowed with this option, argc must be 1
  27. * @opts: encryption key, configuration file, defaults are used if NULL
  28. *
  29. * Description:
  30. * Uses fw_env_open, fw_getenv
  31. *
  32. * Return:
  33. * 0 on success, -1 on failure (modifies errno)
  34. */
  35. int fw_printenv(int argc, char *argv[], int value_only, struct env_opts *opts);
  36. /**
  37. * fw_env_set() - adds or removes one variable to the environment
  38. *
  39. * @argc: number of strings in argv, argv[0] is variable name,
  40. * argc==1 means erase variable, argc > 1 means add a variable
  41. * @argv: argv[0] is variable name, argv[1..argc-1] are concatenated separated
  42. * by single blank and set as the new value of the variable
  43. * @opts: how to retrieve environment from flash, defaults are used if NULL
  44. *
  45. * Description:
  46. * Uses fw_env_open, fw_env_write, fw_env_flush
  47. *
  48. * Return:
  49. * 0 on success, -1 on failure (modifies errno)
  50. *
  51. * ERRORS:
  52. * EROFS - some variables ("ethaddr", "serial#") cannot be modified
  53. */
  54. int fw_env_set(int argc, char *argv[], struct env_opts *opts);
  55. /**
  56. * fw_parse_script() - adds or removes multiple variables with a batch script
  57. *
  58. * @fname: batch script file name
  59. * @opts: encryption key, configuration file, defaults are used if NULL
  60. *
  61. * Description:
  62. * Uses fw_env_open, fw_env_write, fw_env_flush
  63. *
  64. * Return:
  65. * 0 success, -1 on failure (modifies errno)
  66. *
  67. * Script Syntax:
  68. *
  69. * key [ [space]+ value]
  70. *
  71. * lines starting with '#' treated as comment
  72. *
  73. * A variable without value will be deleted. Any number of spaces are allowed
  74. * between key and value. The value starts with the first non-space character
  75. * and ends with newline. No comments allowed on these lines. Spaces inside
  76. * the value are preserved verbatim.
  77. *
  78. * Script Example:
  79. *
  80. * netdev eth0
  81. *
  82. * kernel_addr 400000
  83. *
  84. * foo spaces are copied verbatim
  85. *
  86. * # delete variable bar
  87. *
  88. * bar
  89. */
  90. int fw_parse_script(char *fname, struct env_opts *opts);
  91. /**
  92. * fw_env_open() - read enviroment from flash into RAM cache
  93. *
  94. * @opts: encryption key, configuration file, defaults are used if NULL
  95. *
  96. * Return:
  97. * 0 on success, -1 on failure (modifies errno)
  98. */
  99. int fw_env_open(struct env_opts *opts);
  100. /**
  101. * fw_getenv() - lookup variable in the RAM cache
  102. *
  103. * @name: variable to be searched
  104. * Return:
  105. * pointer to start of value, NULL if not found
  106. */
  107. char *fw_getenv(char *name);
  108. /**
  109. * fw_env_write() - modify a variable held in the RAM cache
  110. *
  111. * @name: variable
  112. * @value: delete variable if NULL, otherwise create or overwrite the variable
  113. *
  114. * This is called in sequence to update the environment in RAM without updating
  115. * the copy in flash after each set
  116. *
  117. * Return:
  118. * 0 on success, -1 on failure (modifies errno)
  119. *
  120. * ERRORS:
  121. * EROFS - some variables ("ethaddr", "serial#") cannot be modified
  122. */
  123. int fw_env_write(char *name, char *value);
  124. /**
  125. * fw_env_flush - write the environment from RAM cache back to flash
  126. *
  127. * @opts: encryption key, configuration file, defaults are used if NULL
  128. *
  129. * Return:
  130. * 0 on success, -1 on failure (modifies errno)
  131. */
  132. int fw_env_flush(struct env_opts *opts);
  133. /**
  134. * fw_env_close - free allocated structure and close env
  135. *
  136. * @opts: encryption key, configuration file, defaults are used if NULL
  137. *
  138. * Return:
  139. * 0 on success, -1 on failure (modifies errno)
  140. */
  141. int fw_env_close(struct env_opts *opts);
  142. /**
  143. * fw_env_version - return the current version of the library
  144. *
  145. * Return:
  146. * version string of the library
  147. */
  148. char *fw_env_version(void);