env_internal.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. # if defined(CONFIG_ENV_ADDR_REDUND) && \
  30. ((CONFIG_ENV_ADDR >= CONFIG_SYS_MONITOR_BASE) && \
  31. (CONFIG_ENV_ADDR_REDUND + CONFIG_ENV_SIZE) <= \
  32. (CONFIG_SYS_MONITOR_BASE + CONFIG_SYS_MONITOR_LEN))
  33. # define ENV_IS_EMBEDDED
  34. # endif
  35. # if (CONFIG_ENV_ADDR >= CONFIG_SYS_MONITOR_BASE) && \
  36. (CONFIG_ENV_ADDR + CONFIG_ENV_SIZE) <= \
  37. (CONFIG_SYS_MONITOR_BASE + CONFIG_SYS_MONITOR_LEN)
  38. # define ENV_IS_EMBEDDED
  39. # endif
  40. #endif /* CONFIG_ENV_IS_IN_FLASH */
  41. #if defined(CONFIG_ENV_IS_IN_NAND)
  42. # if defined(CONFIG_ENV_OFFSET_OOB)
  43. # ifdef CONFIG_ENV_OFFSET_REDUND
  44. # error "CONFIG_ENV_OFFSET_REDUND is not supported when CONFIG_ENV_OFFSET_OOB"
  45. # error "is set"
  46. # endif
  47. extern unsigned long nand_env_oob_offset;
  48. # endif /* CONFIG_ENV_OFFSET_OOB */
  49. #endif /* CONFIG_ENV_IS_IN_NAND */
  50. #include "compiler.h"
  51. #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
  52. # define ENV_HEADER_SIZE (sizeof(uint32_t) + 1)
  53. #else
  54. # define ENV_HEADER_SIZE (sizeof(uint32_t))
  55. #endif
  56. #define ENV_SIZE (CONFIG_ENV_SIZE - ENV_HEADER_SIZE)
  57. /*
  58. * If the environment is in RAM, allocate extra space for it in the malloc
  59. * region.
  60. */
  61. #if defined(ENV_IS_EMBEDDED)
  62. #define TOTAL_MALLOC_LEN CONFIG_SYS_MALLOC_LEN
  63. #elif (CONFIG_ENV_ADDR + CONFIG_ENV_SIZE < CONFIG_SYS_MONITOR_BASE) || \
  64. (CONFIG_ENV_ADDR >= CONFIG_SYS_MONITOR_BASE + CONFIG_SYS_MONITOR_LEN) || \
  65. defined(CONFIG_ENV_IS_IN_NVRAM)
  66. #define TOTAL_MALLOC_LEN (CONFIG_SYS_MALLOC_LEN + CONFIG_ENV_SIZE)
  67. #else
  68. #define TOTAL_MALLOC_LEN CONFIG_SYS_MALLOC_LEN
  69. #endif
  70. typedef struct environment_s {
  71. uint32_t crc; /* CRC32 over data bytes */
  72. #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
  73. unsigned char flags; /* active/obsolete flags ENVF_REDUND_ */
  74. #endif
  75. unsigned char data[ENV_SIZE]; /* Environment data */
  76. } env_t;
  77. #ifdef ENV_IS_EMBEDDED
  78. extern env_t embedded_environment;
  79. #endif /* ENV_IS_EMBEDDED */
  80. #ifdef DEFAULT_ENV_IS_RW
  81. extern char default_environment[];
  82. #else
  83. extern const char default_environment[];
  84. #endif
  85. #ifndef DO_DEPS_ONLY
  86. #include <env_attr.h>
  87. #include <env_callback.h>
  88. #include <env_flags.h>
  89. #include <search.h>
  90. enum env_location {
  91. ENVL_UNKNOWN,
  92. ENVL_EEPROM,
  93. ENVL_EXT4,
  94. ENVL_FAT,
  95. ENVL_FLASH,
  96. ENVL_MMC,
  97. ENVL_NAND,
  98. ENVL_NVRAM,
  99. ENVL_ONENAND,
  100. ENVL_REMOTE,
  101. ENVL_SPI_FLASH,
  102. ENVL_UBI,
  103. ENVL_NOWHERE,
  104. ENVL_COUNT,
  105. };
  106. /* value for the various operations we want to perform on the env */
  107. enum env_operation {
  108. ENVOP_GET_CHAR, /* we want to call the get_char function */
  109. ENVOP_INIT, /* we want to call the init function */
  110. ENVOP_LOAD, /* we want to call the load function */
  111. ENVOP_SAVE, /* we want to call the save function */
  112. ENVOP_ERASE, /* we want to call the erase function */
  113. };
  114. struct env_driver {
  115. const char *name;
  116. enum env_location location;
  117. /**
  118. * load() - Load the environment from storage
  119. *
  120. * This method is required for loading environment
  121. *
  122. * @return 0 if OK, -ve on error
  123. */
  124. int (*load)(void);
  125. /**
  126. * save() - Save the environment to storage
  127. *
  128. * This method is required for 'saveenv' to work.
  129. *
  130. * @return 0 if OK, -ve on error
  131. */
  132. int (*save)(void);
  133. /**
  134. * erase() - Erase the environment on storage
  135. *
  136. * This method is optional and required for 'eraseenv' to work.
  137. *
  138. * @return 0 if OK, -ve on error
  139. */
  140. int (*erase)(void);
  141. /**
  142. * init() - Set up the initial pre-relocation environment
  143. *
  144. * This method is optional.
  145. *
  146. * @return 0 if OK, -ENOENT if no initial environment could be found,
  147. * other -ve on error
  148. */
  149. int (*init)(void);
  150. };
  151. /* Declare a new environment location driver */
  152. #define U_BOOT_ENV_LOCATION(__name) \
  153. ll_entry_declare(struct env_driver, __name, env_driver)
  154. /* Declare the name of a location */
  155. #ifdef CONFIG_CMD_SAVEENV
  156. #define ENV_NAME(_name) .name = _name,
  157. #else
  158. #define ENV_NAME(_name)
  159. #endif
  160. #ifdef CONFIG_CMD_SAVEENV
  161. #define env_save_ptr(x) x
  162. #else
  163. #define env_save_ptr(x) NULL
  164. #endif
  165. #define ENV_SAVE_PTR(x) (CONFIG_IS_ENABLED(SAVEENV) ? (x) : NULL)
  166. #define ENV_ERASE_PTR(x) (IS_ENABLED(CONFIG_CMD_ERASEENV) ? (x) : NULL)
  167. extern struct hsearch_data env_htab;
  168. /**
  169. * env_ext4_get_intf() - Provide the interface for env in EXT4
  170. *
  171. * It is a weak function allowing board to overidde the default interface for
  172. * U-Boot env in EXT4: CONFIG_ENV_EXT4_INTERFACE
  173. *
  174. * Return: string of interface, empty if not supported
  175. */
  176. const char *env_ext4_get_intf(void);
  177. /**
  178. * env_ext4_get_dev_part() - Provide the device and partition for env in EXT4
  179. *
  180. * It is a weak function allowing board to overidde the default device and
  181. * partition used for U-Boot env in EXT4: CONFIG_ENV_EXT4_DEVICE_AND_PART
  182. *
  183. * Return: string of device and partition
  184. */
  185. const char *env_ext4_get_dev_part(void);
  186. /**
  187. * arch_env_get_location()- Provide the best location for the U-Boot environment
  188. *
  189. * It is a weak function allowing board to overidde the environment location
  190. * on architecture level. This has lower priority than env_get_location(),
  191. * which can be defined on board level.
  192. *
  193. * @op: operations performed on the environment
  194. * @prio: priority between the multiple environments, 0 being the
  195. * highest priority
  196. * Return: an enum env_location value on success, or -ve error code.
  197. */
  198. enum env_location arch_env_get_location(enum env_operation op, int prio);
  199. /**
  200. * env_get_location()- Provide the best location for the U-Boot environment
  201. *
  202. * It is a weak function allowing board to overidde the environment location
  203. * on board level. This has higher priority than arch_env_get_location(),
  204. * which can be defined on architecture level.
  205. *
  206. * @op: operations performed on the environment
  207. * @prio: priority between the multiple environments, 0 being the
  208. * highest priority
  209. * Return: an enum env_location value on success, or -ve error code.
  210. */
  211. enum env_location env_get_location(enum env_operation op, int prio);
  212. /**
  213. * env_fat_get_intf() - Provide the interface for env in FAT
  214. *
  215. * It is a weak function allowing board to overidde the default interface for
  216. * U-Boot env in FAT: CONFIG_ENV_FAT_INTERFACE
  217. *
  218. * Return: string of interface, empty if not supported
  219. */
  220. const char *env_fat_get_intf(void);
  221. /**
  222. * env_fat_get_dev_part() - Provide the device and partition for env in FAT
  223. *
  224. * It is a weak function allowing board to overidde the default device and
  225. * partition used for U-Boot env in FAT: CONFIG_ENV_FAT_DEVICE_AND_PART
  226. *
  227. * Return: string of device and partition
  228. */
  229. char *env_fat_get_dev_part(void);
  230. #endif /* DO_DEPS_ONLY */
  231. #endif /* _ENV_INTERNAL_H_ */