env_internal.h 7.7 KB

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