environment.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * (C) Copyright 2002
  4. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  5. */
  6. #ifndef _ENVIRONMENT_H_
  7. #define _ENVIRONMENT_H_
  8. #include <linux/kconfig.h>
  9. /**************************************************************************
  10. *
  11. * The "environment" is stored as a list of '\0' terminated
  12. * "name=value" strings. The end of the list is marked by a double
  13. * '\0'. New entries are always added at the end. Deleting an entry
  14. * shifts the remaining entries to the front. Replacing an entry is a
  15. * combination of deleting the old value and adding the new one.
  16. *
  17. * The environment is preceded by a 32 bit CRC over the data part.
  18. *
  19. *************************************************************************/
  20. #if defined(CONFIG_ENV_IS_IN_FLASH)
  21. # ifndef CONFIG_ENV_ADDR
  22. # define CONFIG_ENV_ADDR (CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET)
  23. # endif
  24. # ifndef CONFIG_ENV_OFFSET
  25. # define CONFIG_ENV_OFFSET (CONFIG_ENV_ADDR - CONFIG_SYS_FLASH_BASE)
  26. # endif
  27. # if !defined(CONFIG_ENV_ADDR_REDUND) && defined(CONFIG_ENV_OFFSET_REDUND)
  28. # define CONFIG_ENV_ADDR_REDUND \
  29. (CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET_REDUND)
  30. # endif
  31. # if defined(CONFIG_ENV_SECT_SIZE) || defined(CONFIG_ENV_SIZE)
  32. # ifndef CONFIG_ENV_SECT_SIZE
  33. # define CONFIG_ENV_SECT_SIZE CONFIG_ENV_SIZE
  34. # endif
  35. # ifndef CONFIG_ENV_SIZE
  36. # define CONFIG_ENV_SIZE CONFIG_ENV_SECT_SIZE
  37. # endif
  38. # else
  39. # error "Both CONFIG_ENV_SECT_SIZE and CONFIG_ENV_SIZE undefined"
  40. # endif
  41. # if defined(CONFIG_ENV_ADDR_REDUND) && !defined(CONFIG_ENV_SIZE_REDUND)
  42. # define CONFIG_ENV_SIZE_REDUND CONFIG_ENV_SIZE
  43. # endif
  44. # if (CONFIG_ENV_ADDR >= CONFIG_SYS_MONITOR_BASE) && \
  45. (CONFIG_ENV_ADDR + CONFIG_ENV_SIZE) <= \
  46. (CONFIG_SYS_MONITOR_BASE + CONFIG_SYS_MONITOR_LEN)
  47. # define ENV_IS_EMBEDDED
  48. # endif
  49. # if defined(CONFIG_ENV_ADDR_REDUND) || defined(CONFIG_ENV_OFFSET_REDUND)
  50. # define CONFIG_SYS_REDUNDAND_ENVIRONMENT
  51. # endif
  52. # ifdef CONFIG_ENV_IS_EMBEDDED
  53. # error "do not define CONFIG_ENV_IS_EMBEDDED in your board config"
  54. # error "it is calculated automatically for you"
  55. # endif
  56. #endif /* CONFIG_ENV_IS_IN_FLASH */
  57. #if defined(CONFIG_ENV_IS_IN_MMC)
  58. # ifdef CONFIG_ENV_OFFSET_REDUND
  59. # define CONFIG_SYS_REDUNDAND_ENVIRONMENT
  60. # endif
  61. #endif
  62. #if defined(CONFIG_ENV_IS_IN_NAND)
  63. # if defined(CONFIG_ENV_OFFSET_OOB)
  64. # ifdef CONFIG_ENV_OFFSET_REDUND
  65. # error "CONFIG_ENV_OFFSET_REDUND is not supported when CONFIG_ENV_OFFSET_OOB"
  66. # error "is set"
  67. # endif
  68. extern unsigned long nand_env_oob_offset;
  69. # define CONFIG_ENV_OFFSET nand_env_oob_offset
  70. # else
  71. # ifndef CONFIG_ENV_OFFSET
  72. # error "Need to define CONFIG_ENV_OFFSET when using CONFIG_ENV_IS_IN_NAND"
  73. # endif
  74. # ifdef CONFIG_ENV_OFFSET_REDUND
  75. # define CONFIG_SYS_REDUNDAND_ENVIRONMENT
  76. # endif
  77. # endif /* CONFIG_ENV_OFFSET_OOB */
  78. # ifndef CONFIG_ENV_SIZE
  79. # error "Need to define CONFIG_ENV_SIZE when using CONFIG_ENV_IS_IN_NAND"
  80. # endif
  81. #endif /* CONFIG_ENV_IS_IN_NAND */
  82. #if defined(CONFIG_ENV_IS_IN_UBI)
  83. # ifndef CONFIG_ENV_UBI_PART
  84. # error "Need to define CONFIG_ENV_UBI_PART when using CONFIG_ENV_IS_IN_UBI"
  85. # endif
  86. # ifndef CONFIG_ENV_UBI_VOLUME
  87. # error "Need to define CONFIG_ENV_UBI_VOLUME when using CONFIG_ENV_IS_IN_UBI"
  88. # endif
  89. # if defined(CONFIG_ENV_UBI_VOLUME_REDUND)
  90. # define CONFIG_SYS_REDUNDAND_ENVIRONMENT
  91. # endif
  92. # ifndef CONFIG_ENV_SIZE
  93. # error "Need to define CONFIG_ENV_SIZE when using CONFIG_ENV_IS_IN_UBI"
  94. # endif
  95. # ifndef CONFIG_CMD_UBI
  96. # error "Need to define CONFIG_CMD_UBI when using CONFIG_ENV_IS_IN_UBI"
  97. # endif
  98. #endif /* CONFIG_ENV_IS_IN_UBI */
  99. /* Embedded env is only supported for some flash types */
  100. #ifdef CONFIG_ENV_IS_EMBEDDED
  101. # if !defined(CONFIG_ENV_IS_IN_FLASH) && \
  102. !defined(CONFIG_ENV_IS_IN_NAND) && \
  103. !defined(CONFIG_ENV_IS_IN_ONENAND) && \
  104. !defined(CONFIG_ENV_IS_IN_SPI_FLASH)
  105. # error "CONFIG_ENV_IS_EMBEDDED not supported for your flash type"
  106. # endif
  107. #endif
  108. /*
  109. * For the flash types where embedded env is supported, but it cannot be
  110. * calculated automatically (i.e. NAND), take the board opt-in.
  111. */
  112. #if defined(CONFIG_ENV_IS_EMBEDDED) && !defined(ENV_IS_EMBEDDED)
  113. # define ENV_IS_EMBEDDED
  114. #endif
  115. /* The build system likes to know if the env is embedded */
  116. #ifdef DO_DEPS_ONLY
  117. # ifdef ENV_IS_EMBEDDED
  118. # ifndef CONFIG_ENV_IS_EMBEDDED
  119. # define CONFIG_ENV_IS_EMBEDDED
  120. # endif
  121. # endif
  122. #endif
  123. #include "compiler.h"
  124. #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
  125. # define ENV_HEADER_SIZE (sizeof(uint32_t) + 1)
  126. # define ACTIVE_FLAG 1
  127. # define OBSOLETE_FLAG 0
  128. #else
  129. # define ENV_HEADER_SIZE (sizeof(uint32_t))
  130. #endif
  131. #define ENV_SIZE (CONFIG_ENV_SIZE - ENV_HEADER_SIZE)
  132. typedef struct environment_s {
  133. uint32_t crc; /* CRC32 over data bytes */
  134. #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
  135. unsigned char flags; /* active/obsolete flags */
  136. #endif
  137. unsigned char data[ENV_SIZE]; /* Environment data */
  138. } env_t;
  139. #ifdef ENV_IS_EMBEDDED
  140. extern env_t embedded_environment;
  141. #endif /* ENV_IS_EMBEDDED */
  142. extern const unsigned char default_environment[];
  143. #ifndef DO_DEPS_ONLY
  144. #include <env_attr.h>
  145. #include <env_callback.h>
  146. #include <env_flags.h>
  147. #include <search.h>
  148. /* Value for environment validity */
  149. enum env_valid {
  150. ENV_INVALID, /* No valid environment */
  151. ENV_VALID, /* First or only environment is valid */
  152. ENV_REDUND, /* Redundant environment is valid */
  153. };
  154. enum env_location {
  155. ENVL_UNKNOWN,
  156. ENVL_EEPROM,
  157. ENVL_EXT4,
  158. ENVL_FAT,
  159. ENVL_FLASH,
  160. ENVL_MMC,
  161. ENVL_NAND,
  162. ENVL_NVRAM,
  163. ENVL_ONENAND,
  164. ENVL_REMOTE,
  165. ENVL_SPI_FLASH,
  166. ENVL_UBI,
  167. ENVL_NOWHERE,
  168. ENVL_COUNT,
  169. };
  170. /* value for the various operations we want to perform on the env */
  171. enum env_operation {
  172. ENVOP_GET_CHAR, /* we want to call the get_char function */
  173. ENVOP_INIT, /* we want to call the init function */
  174. ENVOP_LOAD, /* we want to call the load function */
  175. ENVOP_SAVE, /* we want to call the save function */
  176. ENVOP_ERASE, /* we want to call the erase function */
  177. };
  178. struct env_driver {
  179. const char *name;
  180. enum env_location location;
  181. /**
  182. * load() - Load the environment from storage
  183. *
  184. * This method is optional. If not provided, no environment will be
  185. * loaded.
  186. *
  187. * @return 0 if OK, -ve on error
  188. */
  189. int (*load)(void);
  190. /**
  191. * save() - Save the environment to storage
  192. *
  193. * This method is required for 'saveenv' to work.
  194. *
  195. * @return 0 if OK, -ve on error
  196. */
  197. int (*save)(void);
  198. /**
  199. * erase() - Erase the environment on storage
  200. *
  201. * This method is optional and required for 'eraseenv' to work.
  202. *
  203. * @return 0 if OK, -ve on error
  204. */
  205. int (*erase)(void);
  206. /**
  207. * init() - Set up the initial pre-relocation environment
  208. *
  209. * This method is optional.
  210. *
  211. * @return 0 if OK, -ENOENT if no initial environment could be found,
  212. * other -ve on error
  213. */
  214. int (*init)(void);
  215. };
  216. /* Declare a new environment location driver */
  217. #define U_BOOT_ENV_LOCATION(__name) \
  218. ll_entry_declare(struct env_driver, __name, env_driver)
  219. /* Declare the name of a location */
  220. #ifdef CONFIG_CMD_SAVEENV
  221. #define ENV_NAME(_name) .name = _name,
  222. #else
  223. #define ENV_NAME(_name)
  224. #endif
  225. #ifdef CONFIG_CMD_SAVEENV
  226. #define env_save_ptr(x) x
  227. #else
  228. #define env_save_ptr(x) NULL
  229. #endif
  230. extern struct hsearch_data env_htab;
  231. #endif /* DO_DEPS_ONLY */
  232. #endif /* _ENVIRONMENT_H_ */