environment.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. #else
  127. # define ENV_HEADER_SIZE (sizeof(uint32_t))
  128. #endif
  129. #define ENV_SIZE (CONFIG_ENV_SIZE - ENV_HEADER_SIZE)
  130. /*
  131. * If the environment is in RAM, allocate extra space for it in the malloc
  132. * region.
  133. */
  134. #if defined(CONFIG_ENV_IS_EMBEDDED)
  135. #define TOTAL_MALLOC_LEN CONFIG_SYS_MALLOC_LEN
  136. #elif (CONFIG_ENV_ADDR + CONFIG_ENV_SIZE < CONFIG_SYS_MONITOR_BASE) || \
  137. (CONFIG_ENV_ADDR >= CONFIG_SYS_MONITOR_BASE + CONFIG_SYS_MONITOR_LEN) || \
  138. defined(CONFIG_ENV_IS_IN_NVRAM)
  139. #define TOTAL_MALLOC_LEN (CONFIG_SYS_MALLOC_LEN + CONFIG_ENV_SIZE)
  140. #else
  141. #define TOTAL_MALLOC_LEN CONFIG_SYS_MALLOC_LEN
  142. #endif
  143. typedef struct environment_s {
  144. uint32_t crc; /* CRC32 over data bytes */
  145. #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
  146. unsigned char flags; /* active/obsolete flags ENVF_REDUND_ */
  147. #endif
  148. unsigned char data[ENV_SIZE]; /* Environment data */
  149. } env_t;
  150. #ifdef ENV_IS_EMBEDDED
  151. extern env_t embedded_environment;
  152. #endif /* ENV_IS_EMBEDDED */
  153. extern const unsigned char default_environment[];
  154. #ifndef DO_DEPS_ONLY
  155. #include <env_attr.h>
  156. #include <env_callback.h>
  157. #include <env_flags.h>
  158. #include <search.h>
  159. enum env_location {
  160. ENVL_UNKNOWN,
  161. ENVL_EEPROM,
  162. ENVL_EXT4,
  163. ENVL_FAT,
  164. ENVL_FLASH,
  165. ENVL_MMC,
  166. ENVL_NAND,
  167. ENVL_NVRAM,
  168. ENVL_ONENAND,
  169. ENVL_REMOTE,
  170. ENVL_SPI_FLASH,
  171. ENVL_UBI,
  172. ENVL_NOWHERE,
  173. ENVL_COUNT,
  174. };
  175. /* value for the various operations we want to perform on the env */
  176. enum env_operation {
  177. ENVOP_GET_CHAR, /* we want to call the get_char function */
  178. ENVOP_INIT, /* we want to call the init function */
  179. ENVOP_LOAD, /* we want to call the load function */
  180. ENVOP_SAVE, /* we want to call the save function */
  181. ENVOP_ERASE, /* we want to call the erase function */
  182. };
  183. struct env_driver {
  184. const char *name;
  185. enum env_location location;
  186. /**
  187. * load() - Load the environment from storage
  188. *
  189. * This method is optional. If not provided, no environment will be
  190. * loaded.
  191. *
  192. * @return 0 if OK, -ve on error
  193. */
  194. int (*load)(void);
  195. /**
  196. * save() - Save the environment to storage
  197. *
  198. * This method is required for 'saveenv' to work.
  199. *
  200. * @return 0 if OK, -ve on error
  201. */
  202. int (*save)(void);
  203. /**
  204. * erase() - Erase the environment on storage
  205. *
  206. * This method is optional and required for 'eraseenv' to work.
  207. *
  208. * @return 0 if OK, -ve on error
  209. */
  210. int (*erase)(void);
  211. /**
  212. * init() - Set up the initial pre-relocation environment
  213. *
  214. * This method is optional.
  215. *
  216. * @return 0 if OK, -ENOENT if no initial environment could be found,
  217. * other -ve on error
  218. */
  219. int (*init)(void);
  220. };
  221. /* Declare a new environment location driver */
  222. #define U_BOOT_ENV_LOCATION(__name) \
  223. ll_entry_declare(struct env_driver, __name, env_driver)
  224. /* Declare the name of a location */
  225. #ifdef CONFIG_CMD_SAVEENV
  226. #define ENV_NAME(_name) .name = _name,
  227. #else
  228. #define ENV_NAME(_name)
  229. #endif
  230. #ifdef CONFIG_CMD_SAVEENV
  231. #define env_save_ptr(x) x
  232. #else
  233. #define env_save_ptr(x) NULL
  234. #endif
  235. extern struct hsearch_data env_htab;
  236. #endif /* DO_DEPS_ONLY */
  237. #endif /* _ENVIRONMENT_H_ */