environment.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. #if defined(CONFIG_NEEDS_MANUAL_RELOC)
  156. extern void env_reloc(void);
  157. #endif
  158. #ifdef CONFIG_ENV_IS_IN_MMC
  159. #include <mmc.h>
  160. extern int mmc_get_env_addr(struct mmc *mmc, int copy, u32 *env_addr);
  161. # ifdef CONFIG_SYS_MMC_ENV_PART
  162. extern uint mmc_get_env_part(struct mmc *mmc);
  163. # endif
  164. #endif
  165. #ifndef DO_DEPS_ONLY
  166. #include <env_attr.h>
  167. #include <env_callback.h>
  168. #include <env_flags.h>
  169. #include <search.h>
  170. /* Value for environment validity */
  171. enum env_valid {
  172. ENV_INVALID, /* No valid environment */
  173. ENV_VALID, /* First or only environment is valid */
  174. ENV_REDUND, /* Redundant environment is valid */
  175. };
  176. enum env_location {
  177. ENVL_DATAFLASH,
  178. ENVL_EEPROM,
  179. ENVL_EXT4,
  180. ENVL_FAT,
  181. ENVL_FLASH,
  182. ENVL_MMC,
  183. ENVL_NAND,
  184. ENVL_NVRAM,
  185. ENVL_ONENAND,
  186. ENVL_REMOTE,
  187. ENVL_SPI_FLASH,
  188. ENVL_UBI,
  189. ENVL_NOWHERE,
  190. ENVL_COUNT,
  191. ENVL_UNKNOWN,
  192. };
  193. struct env_driver {
  194. const char *name;
  195. enum env_location location;
  196. /**
  197. * get_char() - Read a character from the environment
  198. *
  199. * This method is optional. If not provided, a default implementation
  200. * will read from gd->env_addr.
  201. *
  202. * @index: Index of character to read (0=first)
  203. * @return character read, or -ve on error
  204. */
  205. int (*get_char)(int index);
  206. /**
  207. * load() - Load the environment from storage
  208. *
  209. * This method is optional. If not provided, no environment will be
  210. * loaded.
  211. */
  212. void (*load)(void);
  213. /**
  214. * save() - Save the environment to storage
  215. *
  216. * This method is required for 'saveenv' to work.
  217. *
  218. * @return 0 if OK, -ve on error
  219. */
  220. int (*save)(void);
  221. /**
  222. * init() - Set up the initial pre-relocation environment
  223. *
  224. * This method is optional.
  225. *
  226. * @return 0 if OK, -ENOENT if no initial environment could be found,
  227. * other -ve on error
  228. */
  229. int (*init)(void);
  230. };
  231. /* Declare a new environment location driver */
  232. #define U_BOOT_ENV_LOCATION(__name) \
  233. ll_entry_declare(struct env_driver, __name, env_driver)
  234. /* Declare the name of a location */
  235. #ifdef CONFIG_CMD_SAVEENV
  236. #define ENV_NAME(_name) .name = _name,
  237. #else
  238. #define ENV_NAME(_name)
  239. #endif
  240. #ifdef CONFIG_CMD_SAVEENV
  241. #define env_save_ptr(x) x
  242. #else
  243. #define env_save_ptr(x) NULL
  244. #endif
  245. extern struct hsearch_data env_htab;
  246. /* Function that updates CRC of the enironment */
  247. void env_crc_update(void);
  248. /* Look up the variable from the default environment */
  249. char *env_get_default(const char *name);
  250. /* [re]set to the default environment */
  251. void set_default_env(const char *s);
  252. /* [re]set individual variables to their value in the default environment */
  253. int set_default_vars(int nvars, char * const vars[]);
  254. /* Import from binary representation into hash table */
  255. int env_import(const char *buf, int check);
  256. /* Export from hash table into binary representation */
  257. int env_export(env_t *env_out);
  258. #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
  259. /* Select and import one of two redundant environments */
  260. int env_import_redund(const char *buf1, const char *buf2);
  261. #endif
  262. /**
  263. * env_driver_lookup_default() - Look up the default environment driver
  264. *
  265. * @return pointer to driver, or NULL if none (which should not happen)
  266. */
  267. struct env_driver *env_driver_lookup_default(void);
  268. /**
  269. * env_get_char() - Get a character from the early environment
  270. *
  271. * This reads from the pre-relocation environemnt
  272. *
  273. * @index: Index of character to read (0 = first)
  274. * @return character read, or -ve on error
  275. */
  276. int env_get_char(int index);
  277. /**
  278. * env_load() - Load the environment from storage
  279. *
  280. * @return 0 if OK, -ve on error
  281. */
  282. int env_load(void);
  283. /**
  284. * env_save() - Save the environment to storage
  285. *
  286. * @return 0 if OK, -ve on error
  287. */
  288. int env_save(void);
  289. #endif /* DO_DEPS_ONLY */
  290. #endif /* _ENVIRONMENT_H_ */