fw_env.h 4.1 KB

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