environment.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. #include <linux/kconfig.h>
  10. /**************************************************************************
  11. *
  12. * The "environment" is stored as a list of '\0' terminated
  13. * "name=value" strings. The end of the list is marked by a double
  14. * '\0'. New entries are always added at the end. Deleting an entry
  15. * shifts the remaining entries to the front. Replacing an entry is a
  16. * combination of deleting the old value and adding the new one.
  17. *
  18. * The environment is preceded by a 32 bit CRC over the data part.
  19. *
  20. *************************************************************************/
  21. #if defined(CONFIG_ENV_IS_IN_FLASH)
  22. # ifndef CONFIG_ENV_ADDR
  23. # define CONFIG_ENV_ADDR (CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET)
  24. # endif
  25. # ifndef CONFIG_ENV_OFFSET
  26. # define CONFIG_ENV_OFFSET (CONFIG_ENV_ADDR - CONFIG_SYS_FLASH_BASE)
  27. # endif
  28. # if !defined(CONFIG_ENV_ADDR_REDUND) && defined(CONFIG_ENV_OFFSET_REDUND)
  29. # define CONFIG_ENV_ADDR_REDUND \
  30. (CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET_REDUND)
  31. # endif
  32. # if defined(CONFIG_ENV_SECT_SIZE) || defined(CONFIG_ENV_SIZE)
  33. # ifndef CONFIG_ENV_SECT_SIZE
  34. # define CONFIG_ENV_SECT_SIZE CONFIG_ENV_SIZE
  35. # endif
  36. # ifndef CONFIG_ENV_SIZE
  37. # define CONFIG_ENV_SIZE CONFIG_ENV_SECT_SIZE
  38. # endif
  39. # else
  40. # error "Both CONFIG_ENV_SECT_SIZE and CONFIG_ENV_SIZE undefined"
  41. # endif
  42. # if defined(CONFIG_ENV_ADDR_REDUND) && !defined(CONFIG_ENV_SIZE_REDUND)
  43. # define CONFIG_ENV_SIZE_REDUND CONFIG_ENV_SIZE
  44. # endif
  45. # if (CONFIG_ENV_ADDR >= CONFIG_SYS_MONITOR_BASE) && \
  46. (CONFIG_ENV_ADDR + CONFIG_ENV_SIZE) <= \
  47. (CONFIG_SYS_MONITOR_BASE + CONFIG_SYS_MONITOR_LEN)
  48. # define ENV_IS_EMBEDDED
  49. # endif
  50. # if defined(CONFIG_ENV_ADDR_REDUND) || defined(CONFIG_ENV_OFFSET_REDUND)
  51. # define CONFIG_SYS_REDUNDAND_ENVIRONMENT
  52. # endif
  53. # ifdef CONFIG_ENV_IS_EMBEDDED
  54. # error "do not define CONFIG_ENV_IS_EMBEDDED in your board config"
  55. # error "it is calculated automatically for you"
  56. # endif
  57. #endif /* CONFIG_ENV_IS_IN_FLASH */
  58. #if defined(CONFIG_ENV_IS_IN_MMC)
  59. # ifdef CONFIG_ENV_OFFSET_REDUND
  60. # define CONFIG_SYS_REDUNDAND_ENVIRONMENT
  61. # endif
  62. #endif
  63. #if defined(CONFIG_ENV_IS_IN_NAND)
  64. # if defined(CONFIG_ENV_OFFSET_OOB)
  65. # ifdef CONFIG_ENV_OFFSET_REDUND
  66. # error "CONFIG_ENV_OFFSET_REDUND is not supported when CONFIG_ENV_OFFSET_OOB"
  67. # error "is set"
  68. # endif
  69. extern unsigned long nand_env_oob_offset;
  70. # define CONFIG_ENV_OFFSET nand_env_oob_offset
  71. # else
  72. # ifndef CONFIG_ENV_OFFSET
  73. # error "Need to define CONFIG_ENV_OFFSET when using CONFIG_ENV_IS_IN_NAND"
  74. # endif
  75. # ifdef CONFIG_ENV_OFFSET_REDUND
  76. # define CONFIG_SYS_REDUNDAND_ENVIRONMENT
  77. # endif
  78. # endif /* CONFIG_ENV_OFFSET_OOB */
  79. # ifndef CONFIG_ENV_SIZE
  80. # error "Need to define CONFIG_ENV_SIZE when using CONFIG_ENV_IS_IN_NAND"
  81. # endif
  82. #endif /* CONFIG_ENV_IS_IN_NAND */
  83. #if defined(CONFIG_ENV_IS_IN_UBI)
  84. # ifndef CONFIG_ENV_UBI_PART
  85. # error "Need to define CONFIG_ENV_UBI_PART when using CONFIG_ENV_IS_IN_UBI"
  86. # endif
  87. # ifndef CONFIG_ENV_UBI_VOLUME
  88. # error "Need to define CONFIG_ENV_UBI_VOLUME when using CONFIG_ENV_IS_IN_UBI"
  89. # endif
  90. # if defined(CONFIG_ENV_UBI_VOLUME_REDUND)
  91. # define CONFIG_SYS_REDUNDAND_ENVIRONMENT
  92. # endif
  93. # ifndef CONFIG_ENV_SIZE
  94. # error "Need to define CONFIG_ENV_SIZE when using CONFIG_ENV_IS_IN_UBI"
  95. # endif
  96. # ifndef CONFIG_CMD_UBI
  97. # error "Need to define CONFIG_CMD_UBI when using CONFIG_ENV_IS_IN_UBI"
  98. # endif
  99. #endif /* CONFIG_ENV_IS_IN_UBI */
  100. /* Embedded env is only supported for some flash types */
  101. #ifdef CONFIG_ENV_IS_EMBEDDED
  102. # if !defined(CONFIG_ENV_IS_IN_FLASH) && \
  103. !defined(CONFIG_ENV_IS_IN_NAND) && \
  104. !defined(CONFIG_ENV_IS_IN_ONENAND) && \
  105. !defined(CONFIG_ENV_IS_IN_SPI_FLASH)
  106. # error "CONFIG_ENV_IS_EMBEDDED not supported for your flash type"
  107. # endif
  108. #endif
  109. /*
  110. * For the flash types where embedded env is supported, but it cannot be
  111. * calculated automatically (i.e. NAND), take the board opt-in.
  112. */
  113. #if defined(CONFIG_ENV_IS_EMBEDDED) && !defined(ENV_IS_EMBEDDED)
  114. # define ENV_IS_EMBEDDED
  115. #endif
  116. /* The build system likes to know if the env is embedded */
  117. #ifdef DO_DEPS_ONLY
  118. # ifdef ENV_IS_EMBEDDED
  119. # ifndef CONFIG_ENV_IS_EMBEDDED
  120. # define CONFIG_ENV_IS_EMBEDDED
  121. # endif
  122. # endif
  123. #endif
  124. #include "compiler.h"
  125. #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
  126. # define ENV_HEADER_SIZE (sizeof(uint32_t) + 1)
  127. # define ACTIVE_FLAG 1
  128. # define OBSOLETE_FLAG 0
  129. #else
  130. # define ENV_HEADER_SIZE (sizeof(uint32_t))
  131. #endif
  132. #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
  133. extern char *env_name_spec;
  134. #endif
  135. #ifdef CONFIG_ENV_AES
  136. /* Make sure the payload is multiple of AES block size */
  137. #define ENV_SIZE ((CONFIG_ENV_SIZE - ENV_HEADER_SIZE) & ~(16 - 1))
  138. #else
  139. #define ENV_SIZE (CONFIG_ENV_SIZE - ENV_HEADER_SIZE)
  140. #endif
  141. typedef struct environment_s {
  142. uint32_t crc; /* CRC32 over data bytes */
  143. #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
  144. unsigned char flags; /* active/obsolete flags */
  145. #endif
  146. unsigned char data[ENV_SIZE]; /* Environment data */
  147. } env_t
  148. #ifdef CONFIG_ENV_AES
  149. /* Make sure the env is aligned to block size. */
  150. __attribute__((aligned(16)))
  151. #endif
  152. ;
  153. #ifdef ENV_IS_EMBEDDED
  154. extern env_t environment;
  155. #endif /* ENV_IS_EMBEDDED */
  156. extern const unsigned char default_environment[];
  157. extern env_t *env_ptr;
  158. extern void env_relocate_spec(void);
  159. extern unsigned char env_get_char_spec(int);
  160. #if defined(CONFIG_NEEDS_MANUAL_RELOC)
  161. extern void env_reloc(void);
  162. #endif
  163. #ifdef CONFIG_ENV_IS_IN_MMC
  164. #include <mmc.h>
  165. extern int mmc_get_env_addr(struct mmc *mmc, int copy, u32 *env_addr);
  166. # ifdef CONFIG_SYS_MMC_ENV_PART
  167. extern uint mmc_get_env_part(struct mmc *mmc);
  168. # endif
  169. #endif
  170. #ifndef DO_DEPS_ONLY
  171. #include <env_attr.h>
  172. #include <env_callback.h>
  173. #include <env_flags.h>
  174. #include <search.h>
  175. /* Value for environment validity */
  176. enum env_valid {
  177. ENV_INVALID, /* No valid environment */
  178. ENV_VALID, /* First or only environment is valid */
  179. ENV_REDUND, /* Redundant environment is valid */
  180. };
  181. enum env_location {
  182. ENVL_DATAFLASH,
  183. ENVL_EEPROM,
  184. ENVL_EXT4,
  185. ENVL_FAT,
  186. ENVL_FLASH,
  187. ENVL_MMC,
  188. ENVL_NAND,
  189. ENVL_NVRAM,
  190. ENVL_ONENAND,
  191. ENVL_REMOTE,
  192. ENVL_SPI_FLASH,
  193. ENVL_UBI,
  194. ENVL_NOWHERE,
  195. ENVL_COUNT,
  196. ENVL_UNKNOWN,
  197. };
  198. struct env_driver {
  199. enum env_location location;
  200. /**
  201. * get_char() - Read a character from the environment
  202. *
  203. * This method is optional. If not provided, a default implementation
  204. * will read from gd->env_addr.
  205. *
  206. * @index: Index of character to read (0=first)
  207. * @return character read
  208. */
  209. unsigned char (*get_char)(int index);
  210. /**
  211. * load() - Load the environment from storage
  212. *
  213. * This method is optional. If not provided, no environment will be
  214. * loaded.
  215. */
  216. void (*load)(void);
  217. /**
  218. * save() - Save the environment to storage
  219. *
  220. * This method is required for 'saveenv' to work.
  221. *
  222. * @return 0 if OK, -ve on error
  223. */
  224. int (*save)(void);
  225. /**
  226. * init() - Set up the initial pre-relocation environment
  227. *
  228. * This method is optional.
  229. *
  230. * @return 0 if OK, -ve on error
  231. */
  232. int (*init)(void);
  233. };
  234. /* Declare a new environment location driver */
  235. #define U_BOOT_ENV_LOCATION(__name) \
  236. ll_entry_declare(struct env_driver, __name, env_driver)
  237. #ifdef CONFIG_CMD_SAVEENV
  238. #define env_save_ptr(x) x
  239. #else
  240. #define env_save_ptr(x) NULL
  241. #endif
  242. extern struct hsearch_data env_htab;
  243. /* Function that returns a character from the environment */
  244. unsigned char env_get_char(int);
  245. /* Function that updates CRC of the enironment */
  246. void env_crc_update(void);
  247. /* Look up the variable from the default environment */
  248. char *getenv_default(const char *name);
  249. /* [re]set to the default environment */
  250. void set_default_env(const char *s);
  251. /* [re]set individual variables to their value in the default environment */
  252. int set_default_vars(int nvars, char * const vars[]);
  253. /* Import from binary representation into hash table */
  254. int env_import(const char *buf, int check);
  255. /* Export from hash table into binary representation */
  256. int env_export(env_t *env_out);
  257. #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
  258. /* Select and import one of two redundant environments */
  259. int env_import_redund(const char *buf1, const char *buf2);
  260. #endif
  261. #endif /* DO_DEPS_ONLY */
  262. #endif /* _ENVIRONMENT_H_ */