env_internal.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 optional. If not provided, no environment will be
  137. * loaded.
  138. *
  139. * @return 0 if OK, -ve on error
  140. */
  141. int (*load)(void);
  142. /**
  143. * save() - Save the environment to storage
  144. *
  145. * This method is required for 'saveenv' to work.
  146. *
  147. * @return 0 if OK, -ve on error
  148. */
  149. int (*save)(void);
  150. /**
  151. * erase() - Erase the environment on storage
  152. *
  153. * This method is optional and required for 'eraseenv' to work.
  154. *
  155. * @return 0 if OK, -ve on error
  156. */
  157. int (*erase)(void);
  158. /**
  159. * init() - Set up the initial pre-relocation environment
  160. *
  161. * This method is optional.
  162. *
  163. * @return 0 if OK, -ENOENT if no initial environment could be found,
  164. * other -ve on error
  165. */
  166. int (*init)(void);
  167. };
  168. /* Declare a new environment location driver */
  169. #define U_BOOT_ENV_LOCATION(__name) \
  170. ll_entry_declare(struct env_driver, __name, env_driver)
  171. /* Declare the name of a location */
  172. #ifdef CONFIG_CMD_SAVEENV
  173. #define ENV_NAME(_name) .name = _name,
  174. #else
  175. #define ENV_NAME(_name)
  176. #endif
  177. #ifdef CONFIG_CMD_SAVEENV
  178. #define env_save_ptr(x) x
  179. #else
  180. #define env_save_ptr(x) NULL
  181. #endif
  182. extern struct hsearch_data env_htab;
  183. #endif /* DO_DEPS_ONLY */
  184. #endif /* _ENV_INTERNAL_H_ */