fw_env.h 4.4 KB

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