environment.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * (C) Copyright 2002
  4. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  5. */
  6. #ifndef _ENVIRONMENT_H_
  7. #define _ENVIRONMENT_H_
  8. #include <linux/kconfig.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. #define ENV_SIZE (CONFIG_ENV_SIZE - ENV_HEADER_SIZE)
  132. typedef struct environment_s {
  133. uint32_t crc; /* CRC32 over data bytes */
  134. #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
  135. unsigned char flags; /* active/obsolete flags */
  136. #endif
  137. unsigned char data[ENV_SIZE]; /* Environment data */
  138. } env_t;
  139. #ifdef ENV_IS_EMBEDDED
  140. extern env_t environment;
  141. #endif /* ENV_IS_EMBEDDED */
  142. extern const unsigned char default_environment[];
  143. extern env_t *env_ptr;
  144. #if defined(CONFIG_NEEDS_MANUAL_RELOC)
  145. extern void env_reloc(void);
  146. #endif
  147. #ifdef CONFIG_ENV_IS_IN_MMC
  148. #include <mmc.h>
  149. extern int mmc_get_env_addr(struct mmc *mmc, int copy, u32 *env_addr);
  150. # ifdef CONFIG_SYS_MMC_ENV_PART
  151. extern uint mmc_get_env_part(struct mmc *mmc);
  152. # endif
  153. #endif
  154. #ifndef DO_DEPS_ONLY
  155. #include <env_attr.h>
  156. #include <env_callback.h>
  157. #include <env_flags.h>
  158. #include <search.h>
  159. /* Value for environment validity */
  160. enum env_valid {
  161. ENV_INVALID, /* No valid environment */
  162. ENV_VALID, /* First or only environment is valid */
  163. ENV_REDUND, /* Redundant environment is valid */
  164. };
  165. enum env_location {
  166. ENVL_UNKNOWN,
  167. ENVL_EEPROM,
  168. ENVL_EXT4,
  169. ENVL_FAT,
  170. ENVL_FLASH,
  171. ENVL_MMC,
  172. ENVL_NAND,
  173. ENVL_NVRAM,
  174. ENVL_ONENAND,
  175. ENVL_REMOTE,
  176. ENVL_SPI_FLASH,
  177. ENVL_UBI,
  178. ENVL_NOWHERE,
  179. ENVL_COUNT,
  180. };
  181. /* value for the various operations we want to perform on the env */
  182. enum env_operation {
  183. ENVOP_GET_CHAR, /* we want to call the get_char function */
  184. ENVOP_INIT, /* we want to call the init function */
  185. ENVOP_LOAD, /* we want to call the load function */
  186. ENVOP_SAVE, /* we want to call the save function */
  187. };
  188. struct env_driver {
  189. const char *name;
  190. enum env_location location;
  191. /**
  192. * load() - Load the environment from storage
  193. *
  194. * This method is optional. If not provided, no environment will be
  195. * loaded.
  196. *
  197. * @return 0 if OK, -ve on error
  198. */
  199. int (*load)(void);
  200. /**
  201. * save() - Save the environment to storage
  202. *
  203. * This method is required for 'saveenv' to work.
  204. *
  205. * @return 0 if OK, -ve on error
  206. */
  207. int (*save)(void);
  208. /**
  209. * init() - Set up the initial pre-relocation environment
  210. *
  211. * This method is optional.
  212. *
  213. * @return 0 if OK, -ENOENT if no initial environment could be found,
  214. * other -ve on error
  215. */
  216. int (*init)(void);
  217. };
  218. /* Declare a new environment location driver */
  219. #define U_BOOT_ENV_LOCATION(__name) \
  220. ll_entry_declare(struct env_driver, __name, env_driver)
  221. /* Declare the name of a location */
  222. #ifdef CONFIG_CMD_SAVEENV
  223. #define ENV_NAME(_name) .name = _name,
  224. #else
  225. #define ENV_NAME(_name)
  226. #endif
  227. #ifdef CONFIG_CMD_SAVEENV
  228. #define env_save_ptr(x) x
  229. #else
  230. #define env_save_ptr(x) NULL
  231. #endif
  232. extern struct hsearch_data env_htab;
  233. /* Function that updates CRC of the enironment */
  234. void env_crc_update(void);
  235. /* Look up the variable from the default environment */
  236. char *env_get_default(const char *name);
  237. /* [re]set to the default environment */
  238. void set_default_env(const char *s);
  239. /* [re]set individual variables to their value in the default environment */
  240. int set_default_vars(int nvars, char * const vars[]);
  241. /* Import from binary representation into hash table */
  242. int env_import(const char *buf, int check);
  243. /* Export from hash table into binary representation */
  244. int env_export(env_t *env_out);
  245. #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
  246. /* Select and import one of two redundant environments */
  247. int env_import_redund(const char *buf1, int buf1_status,
  248. const char *buf2, int buf2_status);
  249. #endif
  250. /**
  251. * env_get_char() - Get a character from the early environment
  252. *
  253. * This reads from the pre-relocation environemnt
  254. *
  255. * @index: Index of character to read (0 = first)
  256. * @return character read, or -ve on error
  257. */
  258. int env_get_char(int index);
  259. /**
  260. * env_load() - Load the environment from storage
  261. *
  262. * @return 0 if OK, -ve on error
  263. */
  264. int env_load(void);
  265. /**
  266. * env_save() - Save the environment to storage
  267. *
  268. * @return 0 if OK, -ve on error
  269. */
  270. int env_save(void);
  271. /**
  272. * env_fix_drivers() - Updates envdriver as per relocation
  273. */
  274. void env_fix_drivers(void);
  275. void eth_parse_enetaddr(const char *addr, uint8_t *enetaddr);
  276. int eth_env_get_enetaddr(const char *name, uint8_t *enetaddr);
  277. int eth_env_set_enetaddr(const char *name, const uint8_t *enetaddr);
  278. #endif /* DO_DEPS_ONLY */
  279. #endif /* _ENVIRONMENT_H_ */