environment.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * (C) Copyright 2002
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #ifndef _ENVIRONMENT_H_
  8. #define _ENVIRONMENT_H_
  9. /**************************************************************************
  10. *
  11. * The "environment" is stored as a list of '\0' terminated
  12. * "name=value" strings. The end of the list is marked by a double
  13. * '\0'. New entries are always added at the end. Deleting an entry
  14. * shifts the remaining entries to the front. Replacing an entry is a
  15. * combination of deleting the old value and adding the new one.
  16. *
  17. * The environment is preceded by a 32 bit CRC over the data part.
  18. *
  19. *************************************************************************/
  20. #if defined(CONFIG_ENV_IS_IN_FLASH)
  21. # ifndef CONFIG_ENV_ADDR
  22. # define CONFIG_ENV_ADDR (CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET)
  23. # endif
  24. # ifndef CONFIG_ENV_OFFSET
  25. # define CONFIG_ENV_OFFSET (CONFIG_ENV_ADDR - CONFIG_SYS_FLASH_BASE)
  26. # endif
  27. # if !defined(CONFIG_ENV_ADDR_REDUND) && defined(CONFIG_ENV_OFFSET_REDUND)
  28. # define CONFIG_ENV_ADDR_REDUND \
  29. (CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET_REDUND)
  30. # endif
  31. # if defined(CONFIG_ENV_SECT_SIZE) || defined(CONFIG_ENV_SIZE)
  32. # ifndef CONFIG_ENV_SECT_SIZE
  33. # define CONFIG_ENV_SECT_SIZE CONFIG_ENV_SIZE
  34. # endif
  35. # ifndef CONFIG_ENV_SIZE
  36. # define CONFIG_ENV_SIZE CONFIG_ENV_SECT_SIZE
  37. # endif
  38. # else
  39. # error "Both CONFIG_ENV_SECT_SIZE and CONFIG_ENV_SIZE undefined"
  40. # endif
  41. # if defined(CONFIG_ENV_ADDR_REDUND) && !defined(CONFIG_ENV_SIZE_REDUND)
  42. # define CONFIG_ENV_SIZE_REDUND CONFIG_ENV_SIZE
  43. # endif
  44. # if (CONFIG_ENV_ADDR >= CONFIG_SYS_MONITOR_BASE) && \
  45. (CONFIG_ENV_ADDR + CONFIG_ENV_SIZE) <= \
  46. (CONFIG_SYS_MONITOR_BASE + CONFIG_SYS_MONITOR_LEN)
  47. # define ENV_IS_EMBEDDED
  48. # endif
  49. # if defined(CONFIG_ENV_ADDR_REDUND) || defined(CONFIG_ENV_OFFSET_REDUND)
  50. # define CONFIG_SYS_REDUNDAND_ENVIRONMENT
  51. # endif
  52. # ifdef CONFIG_ENV_IS_EMBEDDED
  53. # error "do not define CONFIG_ENV_IS_EMBEDDED in your board config"
  54. # error "it is calculated automatically for you"
  55. # endif
  56. #endif /* CONFIG_ENV_IS_IN_FLASH */
  57. #if defined(CONFIG_ENV_IS_IN_MMC)
  58. # ifdef CONFIG_ENV_OFFSET_REDUND
  59. # define CONFIG_SYS_REDUNDAND_ENVIRONMENT
  60. # endif
  61. #endif
  62. #if defined(CONFIG_ENV_IS_IN_NAND)
  63. # if defined(CONFIG_ENV_OFFSET_OOB)
  64. # ifdef CONFIG_ENV_OFFSET_REDUND
  65. # error "CONFIG_ENV_OFFSET_REDUND is not supported when CONFIG_ENV_OFFSET_OOB"
  66. # error "is set"
  67. # endif
  68. extern unsigned long nand_env_oob_offset;
  69. # define CONFIG_ENV_OFFSET nand_env_oob_offset
  70. # else
  71. # ifndef CONFIG_ENV_OFFSET
  72. # error "Need to define CONFIG_ENV_OFFSET when using CONFIG_ENV_IS_IN_NAND"
  73. # endif
  74. # ifdef CONFIG_ENV_OFFSET_REDUND
  75. # define CONFIG_SYS_REDUNDAND_ENVIRONMENT
  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. # if defined(CONFIG_ENV_UBI_VOLUME_REDUND)
  90. # define CONFIG_SYS_REDUNDAND_ENVIRONMENT
  91. # endif
  92. # ifndef CONFIG_ENV_SIZE
  93. # error "Need to define CONFIG_ENV_SIZE when using CONFIG_ENV_IS_IN_UBI"
  94. # endif
  95. # ifndef CONFIG_CMD_UBI
  96. # error "Need to define CONFIG_CMD_UBI when using CONFIG_ENV_IS_IN_UBI"
  97. # endif
  98. #endif /* CONFIG_ENV_IS_IN_UBI */
  99. /* Embedded env is only supported for some flash types */
  100. #ifdef CONFIG_ENV_IS_EMBEDDED
  101. # if !defined(CONFIG_ENV_IS_IN_FLASH) && \
  102. !defined(CONFIG_ENV_IS_IN_NAND) && \
  103. !defined(CONFIG_ENV_IS_IN_ONENAND) && \
  104. !defined(CONFIG_ENV_IS_IN_SPI_FLASH)
  105. # error "CONFIG_ENV_IS_EMBEDDED not supported for your flash type"
  106. # endif
  107. #endif
  108. /*
  109. * For the flash types where embedded env is supported, but it cannot be
  110. * calculated automatically (i.e. NAND), take the board opt-in.
  111. */
  112. #if defined(CONFIG_ENV_IS_EMBEDDED) && !defined(ENV_IS_EMBEDDED)
  113. # define ENV_IS_EMBEDDED
  114. #endif
  115. /* The build system likes to know if the env is embedded */
  116. #ifdef DO_DEPS_ONLY
  117. # ifdef ENV_IS_EMBEDDED
  118. # ifndef CONFIG_ENV_IS_EMBEDDED
  119. # define CONFIG_ENV_IS_EMBEDDED
  120. # endif
  121. # endif
  122. #endif
  123. #include "compiler.h"
  124. #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
  125. # define ENV_HEADER_SIZE (sizeof(uint32_t) + 1)
  126. # define ACTIVE_FLAG 1
  127. # define OBSOLETE_FLAG 0
  128. #else
  129. # define ENV_HEADER_SIZE (sizeof(uint32_t))
  130. #endif
  131. #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
  132. extern char *env_name_spec;
  133. #endif
  134. #ifdef CONFIG_ENV_AES
  135. /* Make sure the payload is multiple of AES block size */
  136. #define ENV_SIZE ((CONFIG_ENV_SIZE - ENV_HEADER_SIZE) & ~(16 - 1))
  137. #else
  138. #define ENV_SIZE (CONFIG_ENV_SIZE - ENV_HEADER_SIZE)
  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 */
  144. #endif
  145. unsigned char data[ENV_SIZE]; /* Environment data */
  146. } env_t
  147. #ifdef CONFIG_ENV_AES
  148. /* Make sure the env is aligned to block size. */
  149. __attribute__((aligned(16)))
  150. #endif
  151. ;
  152. #ifdef ENV_IS_EMBEDDED
  153. extern env_t environment;
  154. #endif /* ENV_IS_EMBEDDED */
  155. extern const unsigned char default_environment[];
  156. extern env_t *env_ptr;
  157. extern void env_relocate_spec(void);
  158. extern unsigned char env_get_char_spec(int);
  159. #if defined(CONFIG_NEEDS_MANUAL_RELOC)
  160. extern void env_reloc(void);
  161. #endif
  162. #ifdef CONFIG_ENV_IS_IN_MMC
  163. #include <mmc.h>
  164. extern int mmc_get_env_addr(struct mmc *mmc, int copy, u32 *env_addr);
  165. # ifdef CONFIG_SYS_MMC_ENV_PART
  166. extern uint mmc_get_env_part(struct mmc *mmc);
  167. # endif
  168. #endif
  169. #ifndef DO_DEPS_ONLY
  170. #include <env_attr.h>
  171. #include <env_callback.h>
  172. #include <env_flags.h>
  173. #include <search.h>
  174. extern struct hsearch_data env_htab;
  175. /* Function that returns a character from the environment */
  176. unsigned char env_get_char(int);
  177. /* Function that returns a pointer to a value from the environment */
  178. const unsigned char *env_get_addr(int);
  179. unsigned char env_get_char_memory(int index);
  180. /* Function that updates CRC of the enironment */
  181. void env_crc_update(void);
  182. /* Look up the variable from the default environment */
  183. char *getenv_default(const char *name);
  184. /* [re]set to the default environment */
  185. void set_default_env(const char *s);
  186. /* [re]set individual variables to their value in the default environment */
  187. int set_default_vars(int nvars, char * const vars[]);
  188. /* Import from binary representation into hash table */
  189. int env_import(const char *buf, int check);
  190. /* Export from hash table into binary representation */
  191. int env_export(env_t *env_out);
  192. #endif /* DO_DEPS_ONLY */
  193. #endif /* _ENVIRONMENT_H_ */