env_internal.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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) && \
  50. ((CONFIG_ENV_ADDR >= CONFIG_SYS_MONITOR_BASE) && \
  51. (CONFIG_ENV_ADDR_REDUND + CONFIG_ENV_SIZE) <= \
  52. (CONFIG_SYS_MONITOR_BASE + CONFIG_SYS_MONITOR_LEN))
  53. # define ENV_IS_EMBEDDED
  54. # endif
  55. # if (CONFIG_ENV_ADDR >= CONFIG_SYS_MONITOR_BASE) && \
  56. (CONFIG_ENV_ADDR + CONFIG_ENV_SIZE) <= \
  57. (CONFIG_SYS_MONITOR_BASE + CONFIG_SYS_MONITOR_LEN)
  58. # define ENV_IS_EMBEDDED
  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_NAND)
  66. # if defined(CONFIG_ENV_OFFSET_OOB)
  67. # ifdef CONFIG_ENV_OFFSET_REDUND
  68. # error "CONFIG_ENV_OFFSET_REDUND is not supported when CONFIG_ENV_OFFSET_OOB"
  69. # error "is set"
  70. # endif
  71. extern unsigned long nand_env_oob_offset;
  72. # define CONFIG_ENV_OFFSET nand_env_oob_offset
  73. # else
  74. # ifndef CONFIG_ENV_OFFSET
  75. # error "Need to define CONFIG_ENV_OFFSET when using CONFIG_ENV_IS_IN_NAND"
  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. # ifndef CONFIG_ENV_SIZE
  90. # error "Need to define CONFIG_ENV_SIZE when using CONFIG_ENV_IS_IN_UBI"
  91. # endif
  92. # ifndef CONFIG_CMD_UBI
  93. # error "Need to define CONFIG_CMD_UBI when using CONFIG_ENV_IS_IN_UBI"
  94. # endif
  95. #endif /* CONFIG_ENV_IS_IN_UBI */
  96. /* Embedded env is only supported for some flash types */
  97. #ifdef CONFIG_ENV_IS_EMBEDDED
  98. # if !defined(CONFIG_ENV_IS_IN_FLASH) && \
  99. !defined(CONFIG_ENV_IS_IN_NAND) && \
  100. !defined(CONFIG_ENV_IS_IN_ONENAND) && \
  101. !defined(CONFIG_ENV_IS_IN_SPI_FLASH)
  102. # error "CONFIG_ENV_IS_EMBEDDED not supported for your flash type"
  103. # endif
  104. #endif
  105. /*
  106. * For the flash types where embedded env is supported, but it cannot be
  107. * calculated automatically (i.e. NAND), take the board opt-in.
  108. */
  109. #if defined(CONFIG_ENV_IS_EMBEDDED) && !defined(ENV_IS_EMBEDDED)
  110. # define ENV_IS_EMBEDDED
  111. #endif
  112. /* The build system likes to know if the env is embedded */
  113. #ifdef DO_DEPS_ONLY
  114. # ifdef ENV_IS_EMBEDDED
  115. # ifndef CONFIG_ENV_IS_EMBEDDED
  116. # define CONFIG_ENV_IS_EMBEDDED
  117. # endif
  118. # endif
  119. #endif
  120. #include "compiler.h"
  121. #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
  122. # define ENV_HEADER_SIZE (sizeof(uint32_t) + 1)
  123. #else
  124. # define ENV_HEADER_SIZE (sizeof(uint32_t))
  125. #endif
  126. #define ENV_SIZE (CONFIG_ENV_SIZE - ENV_HEADER_SIZE)
  127. /*
  128. * If the environment is in RAM, allocate extra space for it in the malloc
  129. * region.
  130. */
  131. #if defined(CONFIG_ENV_IS_EMBEDDED)
  132. #define TOTAL_MALLOC_LEN CONFIG_SYS_MALLOC_LEN
  133. #elif (CONFIG_ENV_ADDR + CONFIG_ENV_SIZE < CONFIG_SYS_MONITOR_BASE) || \
  134. (CONFIG_ENV_ADDR >= CONFIG_SYS_MONITOR_BASE + CONFIG_SYS_MONITOR_LEN) || \
  135. defined(CONFIG_ENV_IS_IN_NVRAM)
  136. #define TOTAL_MALLOC_LEN (CONFIG_SYS_MALLOC_LEN + CONFIG_ENV_SIZE)
  137. #else
  138. #define TOTAL_MALLOC_LEN CONFIG_SYS_MALLOC_LEN
  139. #endif
  140. typedef struct environment_s {
  141. uint32_t crc; /* CRC32 over data bytes */
  142. #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
  143. unsigned char flags; /* active/obsolete flags ENVF_REDUND_ */
  144. #endif
  145. unsigned char data[ENV_SIZE]; /* Environment data */
  146. } env_t;
  147. #ifdef ENV_IS_EMBEDDED
  148. extern env_t embedded_environment;
  149. #endif /* ENV_IS_EMBEDDED */
  150. extern const unsigned char default_environment[];
  151. #ifndef DO_DEPS_ONLY
  152. #include <env_attr.h>
  153. #include <env_callback.h>
  154. #include <env_flags.h>
  155. #include <search.h>
  156. enum env_location {
  157. ENVL_UNKNOWN,
  158. ENVL_EEPROM,
  159. ENVL_EXT4,
  160. ENVL_FAT,
  161. ENVL_FLASH,
  162. ENVL_MMC,
  163. ENVL_NAND,
  164. ENVL_NVRAM,
  165. ENVL_ONENAND,
  166. ENVL_REMOTE,
  167. ENVL_SPI_FLASH,
  168. ENVL_UBI,
  169. ENVL_NOWHERE,
  170. ENVL_COUNT,
  171. };
  172. /* value for the various operations we want to perform on the env */
  173. enum env_operation {
  174. ENVOP_GET_CHAR, /* we want to call the get_char function */
  175. ENVOP_INIT, /* we want to call the init function */
  176. ENVOP_LOAD, /* we want to call the load function */
  177. ENVOP_SAVE, /* we want to call the save function */
  178. ENVOP_ERASE, /* we want to call the erase function */
  179. };
  180. struct env_driver {
  181. const char *name;
  182. enum env_location location;
  183. /**
  184. * load() - Load the environment from storage
  185. *
  186. * This method is optional. If not provided, no environment will be
  187. * loaded.
  188. *
  189. * @return 0 if OK, -ve on error
  190. */
  191. int (*load)(void);
  192. /**
  193. * save() - Save the environment to storage
  194. *
  195. * This method is required for 'saveenv' to work.
  196. *
  197. * @return 0 if OK, -ve on error
  198. */
  199. int (*save)(void);
  200. /**
  201. * erase() - Erase the environment on storage
  202. *
  203. * This method is optional and required for 'eraseenv' to work.
  204. *
  205. * @return 0 if OK, -ve on error
  206. */
  207. int (*erase)(void);
  208. /**
  209. * init() - Set up the initial pre-relocation environment
  210. *
  211. * This method is optional.
  212. *
  213. * @return 0 if OK, -ENOENT if no initial environment could be found,
  214. * other -ve on error
  215. */
  216. int (*init)(void);
  217. };
  218. /* Declare a new environment location driver */
  219. #define U_BOOT_ENV_LOCATION(__name) \
  220. ll_entry_declare(struct env_driver, __name, env_driver)
  221. /* Declare the name of a location */
  222. #ifdef CONFIG_CMD_SAVEENV
  223. #define ENV_NAME(_name) .name = _name,
  224. #else
  225. #define ENV_NAME(_name)
  226. #endif
  227. #ifdef CONFIG_CMD_SAVEENV
  228. #define env_save_ptr(x) x
  229. #else
  230. #define env_save_ptr(x) NULL
  231. #endif
  232. extern struct hsearch_data env_htab;
  233. #endif /* DO_DEPS_ONLY */
  234. #endif /* _ENV_INTERNAL_H_ */