env_internal.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. # ifdef CONFIG_ENV_IS_EMBEDDED
  41. # error "do not define CONFIG_ENV_IS_EMBEDDED in your board config"
  42. # error "it is calculated automatically for you"
  43. # endif
  44. #endif /* CONFIG_ENV_IS_IN_FLASH */
  45. #if defined(CONFIG_ENV_IS_IN_NAND)
  46. # if defined(CONFIG_ENV_OFFSET_OOB)
  47. # ifdef CONFIG_ENV_OFFSET_REDUND
  48. # error "CONFIG_ENV_OFFSET_REDUND is not supported when CONFIG_ENV_OFFSET_OOB"
  49. # error "is set"
  50. # endif
  51. extern unsigned long nand_env_oob_offset;
  52. # define CONFIG_ENV_OFFSET nand_env_oob_offset
  53. # endif /* CONFIG_ENV_OFFSET_OOB */
  54. #endif /* CONFIG_ENV_IS_IN_NAND */
  55. /*
  56. * For the flash types where embedded env is supported, but it cannot be
  57. * calculated automatically (i.e. NAND), take the board opt-in.
  58. */
  59. #if defined(CONFIG_ENV_IS_EMBEDDED) && !defined(ENV_IS_EMBEDDED)
  60. # define ENV_IS_EMBEDDED
  61. #endif
  62. /* The build system likes to know if the env is embedded */
  63. #ifdef DO_DEPS_ONLY
  64. # ifdef ENV_IS_EMBEDDED
  65. # ifndef CONFIG_ENV_IS_EMBEDDED
  66. # define CONFIG_ENV_IS_EMBEDDED
  67. # endif
  68. # endif
  69. #endif
  70. #include "compiler.h"
  71. #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
  72. # define ENV_HEADER_SIZE (sizeof(uint32_t) + 1)
  73. #else
  74. # define ENV_HEADER_SIZE (sizeof(uint32_t))
  75. #endif
  76. #define ENV_SIZE (CONFIG_ENV_SIZE - ENV_HEADER_SIZE)
  77. /*
  78. * If the environment is in RAM, allocate extra space for it in the malloc
  79. * region.
  80. */
  81. #if defined(CONFIG_ENV_IS_EMBEDDED)
  82. #define TOTAL_MALLOC_LEN CONFIG_SYS_MALLOC_LEN
  83. #elif (CONFIG_ENV_ADDR + CONFIG_ENV_SIZE < CONFIG_SYS_MONITOR_BASE) || \
  84. (CONFIG_ENV_ADDR >= CONFIG_SYS_MONITOR_BASE + CONFIG_SYS_MONITOR_LEN) || \
  85. defined(CONFIG_ENV_IS_IN_NVRAM)
  86. #define TOTAL_MALLOC_LEN (CONFIG_SYS_MALLOC_LEN + CONFIG_ENV_SIZE)
  87. #else
  88. #define TOTAL_MALLOC_LEN CONFIG_SYS_MALLOC_LEN
  89. #endif
  90. typedef struct environment_s {
  91. uint32_t crc; /* CRC32 over data bytes */
  92. #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
  93. unsigned char flags; /* active/obsolete flags ENVF_REDUND_ */
  94. #endif
  95. unsigned char data[ENV_SIZE]; /* Environment data */
  96. } env_t;
  97. #ifdef ENV_IS_EMBEDDED
  98. extern env_t embedded_environment;
  99. #endif /* ENV_IS_EMBEDDED */
  100. extern const unsigned char default_environment[];
  101. #ifndef DO_DEPS_ONLY
  102. #include <env_attr.h>
  103. #include <env_callback.h>
  104. #include <env_flags.h>
  105. #include <search.h>
  106. enum env_location {
  107. ENVL_UNKNOWN,
  108. ENVL_EEPROM,
  109. ENVL_EXT4,
  110. ENVL_FAT,
  111. ENVL_FLASH,
  112. ENVL_MMC,
  113. ENVL_NAND,
  114. ENVL_NVRAM,
  115. ENVL_ONENAND,
  116. ENVL_REMOTE,
  117. ENVL_SPI_FLASH,
  118. ENVL_UBI,
  119. ENVL_NOWHERE,
  120. ENVL_COUNT,
  121. };
  122. /* value for the various operations we want to perform on the env */
  123. enum env_operation {
  124. ENVOP_GET_CHAR, /* we want to call the get_char function */
  125. ENVOP_INIT, /* we want to call the init function */
  126. ENVOP_LOAD, /* we want to call the load function */
  127. ENVOP_SAVE, /* we want to call the save function */
  128. ENVOP_ERASE, /* we want to call the erase function */
  129. };
  130. struct env_driver {
  131. const char *name;
  132. enum env_location location;
  133. /**
  134. * load() - Load the environment from storage
  135. *
  136. * This method is required for loading environment
  137. *
  138. * @return 0 if OK, -ve on error
  139. */
  140. int (*load)(void);
  141. /**
  142. * save() - Save the environment to storage
  143. *
  144. * This method is required for 'saveenv' to work.
  145. *
  146. * @return 0 if OK, -ve on error
  147. */
  148. int (*save)(void);
  149. /**
  150. * erase() - Erase the environment on storage
  151. *
  152. * This method is optional and required for 'eraseenv' to work.
  153. *
  154. * @return 0 if OK, -ve on error
  155. */
  156. int (*erase)(void);
  157. /**
  158. * init() - Set up the initial pre-relocation environment
  159. *
  160. * This method is optional.
  161. *
  162. * @return 0 if OK, -ENOENT if no initial environment could be found,
  163. * other -ve on error
  164. */
  165. int (*init)(void);
  166. };
  167. /* Declare a new environment location driver */
  168. #define U_BOOT_ENV_LOCATION(__name) \
  169. ll_entry_declare(struct env_driver, __name, env_driver)
  170. /* Declare the name of a location */
  171. #ifdef CONFIG_CMD_SAVEENV
  172. #define ENV_NAME(_name) .name = _name,
  173. #else
  174. #define ENV_NAME(_name)
  175. #endif
  176. #ifdef CONFIG_CMD_SAVEENV
  177. #define env_save_ptr(x) x
  178. #else
  179. #define env_save_ptr(x) NULL
  180. #endif
  181. #define ENV_SAVE_PTR(x) (CONFIG_IS_ENABLED(SAVEENV) ? (x) : NULL)
  182. extern struct hsearch_data env_htab;
  183. /**
  184. * env_ext4_get_intf() - Provide the interface for env in EXT4
  185. *
  186. * It is a weak function allowing board to overidde the default interface for
  187. * U-Boot env in EXT4: CONFIG_ENV_EXT4_INTERFACE
  188. *
  189. * @return string of interface, empty if not supported
  190. */
  191. const char *env_ext4_get_intf(void);
  192. /**
  193. * env_ext4_get_dev_part() - Provide the device and partition for env in EXT4
  194. *
  195. * It is a weak function allowing board to overidde the default device and
  196. * partition used for U-Boot env in EXT4: CONFIG_ENV_EXT4_DEVICE_AND_PART
  197. *
  198. * @return string of device and partition
  199. */
  200. const char *env_ext4_get_dev_part(void);
  201. /**
  202. * env_get_location()- Provide the best location for the U-Boot environment
  203. *
  204. * It is a weak function allowing board to overidde the environment location
  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. #endif /* DO_DEPS_ONLY */
  213. #endif /* _ENV_INTERNAL_H_ */