environment.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. #ifdef CONFIG_ENV_AES
  133. /* Make sure the payload is multiple of AES block size */
  134. #define ENV_SIZE ((CONFIG_ENV_SIZE - ENV_HEADER_SIZE) & ~(16 - 1))
  135. #else
  136. #define ENV_SIZE (CONFIG_ENV_SIZE - ENV_HEADER_SIZE)
  137. #endif
  138. typedef struct environment_s {
  139. uint32_t crc; /* CRC32 over data bytes */
  140. #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
  141. unsigned char flags; /* active/obsolete flags */
  142. #endif
  143. unsigned char data[ENV_SIZE]; /* Environment data */
  144. } env_t
  145. #ifdef CONFIG_ENV_AES
  146. /* Make sure the env is aligned to block size. */
  147. __attribute__((aligned(16)))
  148. #endif
  149. ;
  150. #ifdef ENV_IS_EMBEDDED
  151. extern env_t environment;
  152. #endif /* ENV_IS_EMBEDDED */
  153. extern const unsigned char default_environment[];
  154. extern env_t *env_ptr;
  155. extern void env_relocate_spec(void);
  156. extern unsigned char env_get_char_spec(int);
  157. #if defined(CONFIG_NEEDS_MANUAL_RELOC)
  158. extern void env_reloc(void);
  159. #endif
  160. #ifdef CONFIG_ENV_IS_IN_MMC
  161. #include <mmc.h>
  162. extern int mmc_get_env_addr(struct mmc *mmc, int copy, u32 *env_addr);
  163. # ifdef CONFIG_SYS_MMC_ENV_PART
  164. extern uint mmc_get_env_part(struct mmc *mmc);
  165. # endif
  166. #endif
  167. #ifndef DO_DEPS_ONLY
  168. #include <env_attr.h>
  169. #include <env_callback.h>
  170. #include <env_flags.h>
  171. #include <search.h>
  172. /* Value for environment validity */
  173. enum env_valid {
  174. ENV_INVALID, /* No valid environment */
  175. ENV_VALID, /* First or only environment is valid */
  176. ENV_REDUND, /* Redundant environment is valid */
  177. };
  178. enum env_location {
  179. ENVL_DATAFLASH,
  180. ENVL_EEPROM,
  181. ENVL_EXT4,
  182. ENVL_FAT,
  183. ENVL_FLASH,
  184. ENVL_MMC,
  185. ENVL_NAND,
  186. ENVL_NVRAM,
  187. ENVL_ONENAND,
  188. ENVL_REMOTE,
  189. ENVL_SPI_FLASH,
  190. ENVL_UBI,
  191. ENVL_NOWHERE,
  192. ENVL_COUNT,
  193. ENVL_UNKNOWN,
  194. };
  195. struct env_driver {
  196. const char *name;
  197. enum env_location location;
  198. /**
  199. * get_char() - Read a character from the environment
  200. *
  201. * This method is optional. If not provided, a default implementation
  202. * will read from gd->env_addr.
  203. *
  204. * @index: Index of character to read (0=first)
  205. * @return character read
  206. */
  207. unsigned char (*get_char)(int index);
  208. /**
  209. * load() - Load the environment from storage
  210. *
  211. * This method is optional. If not provided, no environment will be
  212. * loaded.
  213. */
  214. void (*load)(void);
  215. /**
  216. * save() - Save the environment to storage
  217. *
  218. * This method is required for 'saveenv' to work.
  219. *
  220. * @return 0 if OK, -ve on error
  221. */
  222. int (*save)(void);
  223. /**
  224. * init() - Set up the initial pre-relocation environment
  225. *
  226. * This method is optional.
  227. *
  228. * @return 0 if OK, -ENOENT if no initial environment could be found,
  229. * other -ve on error
  230. */
  231. int (*init)(void);
  232. };
  233. /* Declare a new environment location driver */
  234. #define U_BOOT_ENV_LOCATION(__name) \
  235. ll_entry_declare(struct env_driver, __name, env_driver)
  236. /* Declare the name of a location */
  237. #ifdef CONFIG_CMD_SAVEENV
  238. #define ENV_NAME(_name) .name = _name,
  239. #else
  240. #define ENV_NAME(_name)
  241. #endif
  242. #ifdef CONFIG_CMD_SAVEENV
  243. #define env_save_ptr(x) x
  244. #else
  245. #define env_save_ptr(x) NULL
  246. #endif
  247. extern struct hsearch_data env_htab;
  248. /* Function that returns a character from the environment */
  249. unsigned char env_get_char(int);
  250. /* Function that updates CRC of the enironment */
  251. void env_crc_update(void);
  252. /* Look up the variable from the default environment */
  253. char *getenv_default(const char *name);
  254. /* [re]set to the default environment */
  255. void set_default_env(const char *s);
  256. /* [re]set individual variables to their value in the default environment */
  257. int set_default_vars(int nvars, char * const vars[]);
  258. /* Import from binary representation into hash table */
  259. int env_import(const char *buf, int check);
  260. /* Export from hash table into binary representation */
  261. int env_export(env_t *env_out);
  262. #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
  263. /* Select and import one of two redundant environments */
  264. int env_import_redund(const char *buf1, const char *buf2);
  265. #endif
  266. /**
  267. * env_driver_lookup_default() - Look up the default environment driver
  268. *
  269. * @return pointer to driver, or NULL if none (which should not happen)
  270. */
  271. struct env_driver *env_driver_lookup_default(void);
  272. #endif /* DO_DEPS_ONLY */
  273. #endif /* _ENVIRONMENT_H_ */