Blame view

include/environment.h 8.34 KB
c609719b8   wdenk   Initial revision
1
2
3
4
  /*
   * (C) Copyright 2002
   * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
   *
1a4596601   Wolfgang Denk   Add GPL-2.0+ SPDX...
5
   * SPDX-License-Identifier:	GPL-2.0+
c609719b8   wdenk   Initial revision
6
7
8
   */
  
  #ifndef _ENVIRONMENT_H_
6f403bad8   Igor Grinberg   env: remove value...
9
  #define _ENVIRONMENT_H_
c609719b8   wdenk   Initial revision
10

fb1c43cc3   Maxime Ripard   common: Move envi...
11
  #include <linux/kconfig.h>
c609719b8   wdenk   Initial revision
12
13
14
15
16
17
18
19
  /**************************************************************************
   *
   * The "environment" is stored as a list of '\0' terminated
   * "name=value" strings. The end of the list is marked by a double
   * '\0'. New entries are always added at the end. Deleting an entry
   * shifts the remaining entries to the front. Replacing an entry is a
   * combination of deleting the old value and adding the new one.
   *
fc0b5948e   Robert P. J. Day   Various, accumula...
20
   * The environment is preceded by a 32 bit CRC over the data part.
c609719b8   wdenk   Initial revision
21
   *
fc0b5948e   Robert P. J. Day   Various, accumula...
22
   *************************************************************************/
c609719b8   wdenk   Initial revision
23

5a1aceb06   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ENV_IS...
24
  #if defined(CONFIG_ENV_IS_IN_FLASH)
507651d61   Igor Grinberg   env: clean enviro...
25
26
  # ifndef	CONFIG_ENV_ADDR
  #  define	CONFIG_ENV_ADDR	(CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET)
c609719b8   wdenk   Initial revision
27
  # endif
507651d61   Igor Grinberg   env: clean enviro...
28
29
  # ifndef	CONFIG_ENV_OFFSET
  #  define	CONFIG_ENV_OFFSET (CONFIG_ENV_ADDR - CONFIG_SYS_FLASH_BASE)
c609719b8   wdenk   Initial revision
30
  # endif
0e8d15866   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ENV ma...
31
  # if !defined(CONFIG_ENV_ADDR_REDUND) && defined(CONFIG_ENV_OFFSET_REDUND)
507651d61   Igor Grinberg   env: clean enviro...
32
33
  #  define	CONFIG_ENV_ADDR_REDUND	\
  		(CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET_REDUND)
c609719b8   wdenk   Initial revision
34
  # endif
0e8d15866   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ENV ma...
35
  # if defined(CONFIG_ENV_SECT_SIZE) || defined(CONFIG_ENV_SIZE)
507651d61   Igor Grinberg   env: clean enviro...
36
37
  #  ifndef	CONFIG_ENV_SECT_SIZE
  #   define	CONFIG_ENV_SECT_SIZE	CONFIG_ENV_SIZE
500545cc6   wdenk   Fix problem with ...
38
  #  endif
507651d61   Igor Grinberg   env: clean enviro...
39
40
  #  ifndef	CONFIG_ENV_SIZE
  #   define	CONFIG_ENV_SIZE	CONFIG_ENV_SECT_SIZE
500545cc6   wdenk   Fix problem with ...
41
42
  #  endif
  # else
0e8d15866   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ENV ma...
43
  #  error "Both CONFIG_ENV_SECT_SIZE and CONFIG_ENV_SIZE undefined"
c609719b8   wdenk   Initial revision
44
  # endif
0e8d15866   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ENV ma...
45
46
  # if defined(CONFIG_ENV_ADDR_REDUND) && !defined(CONFIG_ENV_SIZE_REDUND)
  #  define CONFIG_ENV_SIZE_REDUND	CONFIG_ENV_SIZE
c609719b8   wdenk   Initial revision
47
  # endif
507651d61   Igor Grinberg   env: clean enviro...
48
49
50
  # if	(CONFIG_ENV_ADDR >= CONFIG_SYS_MONITOR_BASE) &&		\
  	(CONFIG_ENV_ADDR + CONFIG_ENV_SIZE) <=			\
  	(CONFIG_SYS_MONITOR_BASE + CONFIG_SYS_MONITOR_LEN)
6f403bad8   Igor Grinberg   env: remove value...
51
  #  define ENV_IS_EMBEDDED
c609719b8   wdenk   Initial revision
52
  # endif
0e8d15866   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ENV ma...
53
  # if defined(CONFIG_ENV_ADDR_REDUND) || defined(CONFIG_ENV_OFFSET_REDUND)
6f403bad8   Igor Grinberg   env: remove value...
54
  #  define CONFIG_SYS_REDUNDAND_ENVIRONMENT
c609719b8   wdenk   Initial revision
55
  # endif
0a9e4e772   Mike Frysinger   unify {CONFIG_,}E...
56
57
58
59
  # ifdef CONFIG_ENV_IS_EMBEDDED
  #  error "do not define CONFIG_ENV_IS_EMBEDDED in your board config"
  #  error "it is calculated automatically for you"
  # endif
5a1aceb06   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ENV_IS...
60
  #endif	/* CONFIG_ENV_IS_IN_FLASH */
c609719b8   wdenk   Initial revision
61

5707df779   Bo Shen   mmc: fix env in m...
62
63
64
65
66
  #if defined(CONFIG_ENV_IS_IN_MMC)
  # ifdef CONFIG_ENV_OFFSET_REDUND
  #  define CONFIG_SYS_REDUNDAND_ENVIRONMENT
  # endif
  #endif
51bfee192   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ENV_IS...
67
  #if defined(CONFIG_ENV_IS_IN_NAND)
c9f7351b5   Ben Gardiner   NAND: environment...
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
  # if defined(CONFIG_ENV_OFFSET_OOB)
  #  ifdef CONFIG_ENV_OFFSET_REDUND
  #   error "CONFIG_ENV_OFFSET_REDUND is not supported when CONFIG_ENV_OFFSET_OOB"
  #   error "is set"
  #  endif
  extern unsigned long nand_env_oob_offset;
  #  define CONFIG_ENV_OFFSET nand_env_oob_offset
  # else
  #  ifndef CONFIG_ENV_OFFSET
  #   error "Need to define CONFIG_ENV_OFFSET when using CONFIG_ENV_IS_IN_NAND"
  #  endif
  #  ifdef CONFIG_ENV_OFFSET_REDUND
  #   define CONFIG_SYS_REDUNDAND_ENVIRONMENT
  #  endif
  # endif /* CONFIG_ENV_OFFSET_OOB */
0e8d15866   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ENV ma...
83
84
  # ifndef CONFIG_ENV_SIZE
  #  error "Need to define CONFIG_ENV_SIZE when using CONFIG_ENV_IS_IN_NAND"
e443c944c   Markus Klotzbuecher   Support for redun...
85
  # endif
51bfee192   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ENV_IS...
86
  #endif /* CONFIG_ENV_IS_IN_NAND */
e443c944c   Markus Klotzbuecher   Support for redun...
87

2b74433f3   Joe Hershberger   env: Add support ...
88
89
90
91
92
93
94
  #if defined(CONFIG_ENV_IS_IN_UBI)
  # ifndef CONFIG_ENV_UBI_PART
  #  error "Need to define CONFIG_ENV_UBI_PART when using CONFIG_ENV_IS_IN_UBI"
  # endif
  # ifndef CONFIG_ENV_UBI_VOLUME
  #  error "Need to define CONFIG_ENV_UBI_VOLUME when using CONFIG_ENV_IS_IN_UBI"
  # endif
785881f77   Joe Hershberger   env: Add redundan...
95
96
97
  # if defined(CONFIG_ENV_UBI_VOLUME_REDUND)
  #  define CONFIG_SYS_REDUNDAND_ENVIRONMENT
  # endif
2b74433f3   Joe Hershberger   env: Add support ...
98
99
100
101
102
103
104
  # ifndef CONFIG_ENV_SIZE
  #  error "Need to define CONFIG_ENV_SIZE when using CONFIG_ENV_IS_IN_UBI"
  # endif
  # ifndef CONFIG_CMD_UBI
  #  error "Need to define CONFIG_CMD_UBI when using CONFIG_ENV_IS_IN_UBI"
  # endif
  #endif /* CONFIG_ENV_IS_IN_UBI */
0a9e4e772   Mike Frysinger   unify {CONFIG_,}E...
105
106
  /* Embedded env is only supported for some flash types */
  #ifdef CONFIG_ENV_IS_EMBEDDED
507651d61   Igor Grinberg   env: clean enviro...
107
108
109
110
  # if	!defined(CONFIG_ENV_IS_IN_FLASH)	&& \
  	!defined(CONFIG_ENV_IS_IN_NAND)		&& \
  	!defined(CONFIG_ENV_IS_IN_ONENAND)	&& \
  	!defined(CONFIG_ENV_IS_IN_SPI_FLASH)
0a9e4e772   Mike Frysinger   unify {CONFIG_,}E...
111
112
113
114
115
116
117
118
119
  #  error "CONFIG_ENV_IS_EMBEDDED not supported for your flash type"
  # endif
  #endif
  
  /*
   * For the flash types where embedded env is supported, but it cannot be
   * calculated automatically (i.e. NAND), take the board opt-in.
   */
  #if defined(CONFIG_ENV_IS_EMBEDDED) && !defined(ENV_IS_EMBEDDED)
6f403bad8   Igor Grinberg   env: remove value...
120
  # define ENV_IS_EMBEDDED
0a9e4e772   Mike Frysinger   unify {CONFIG_,}E...
121
122
123
124
125
  #endif
  
  /* The build system likes to know if the env is embedded */
  #ifdef DO_DEPS_ONLY
  # ifdef ENV_IS_EMBEDDED
33a6b9e90   Wolfgang Denk   environment.h: fi...
126
127
128
  #  ifndef CONFIG_ENV_IS_EMBEDDED
  #   define CONFIG_ENV_IS_EMBEDDED
  #  endif
0a9e4e772   Mike Frysinger   unify {CONFIG_,}E...
129
130
  # endif
  #endif
375660907   Mike Frysinger   compiler.h: unify...
131
  #include "compiler.h"
c609719b8   wdenk   Initial revision
132

6d0f6bcf3   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ macro...
133
  #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
89cdab788   Mike Frysinger   crc32: use uint32...
134
  # define ENV_HEADER_SIZE	(sizeof(uint32_t) + 1)
b218ccb54   Wolfgang Denk   Redundant environ...
135
136
137
  
  # define ACTIVE_FLAG   1
  # define OBSOLETE_FLAG 0
c609719b8   wdenk   Initial revision
138
  #else
89cdab788   Mike Frysinger   crc32: use uint32...
139
  # define ENV_HEADER_SIZE	(sizeof(uint32_t))
c609719b8   wdenk   Initial revision
140
  #endif
0e8d15866   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ENV ma...
141
  #define ENV_SIZE (CONFIG_ENV_SIZE - ENV_HEADER_SIZE)
c609719b8   wdenk   Initial revision
142

507651d61   Igor Grinberg   env: clean enviro...
143
  typedef struct environment_s {
89cdab788   Mike Frysinger   crc32: use uint32...
144
  	uint32_t	crc;		/* CRC32 over data bytes	*/
6d0f6bcf3   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ macro...
145
  #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
c609719b8   wdenk   Initial revision
146
147
148
  	unsigned char	flags;		/* active/obsolete flags	*/
  #endif
  	unsigned char	data[ENV_SIZE]; /* Environment data		*/
c6831c74a   Tom Rini   env: Remove CONFI...
149
  } env_t;
c609719b8   wdenk   Initial revision
150

994bc671c   Igor Grinberg   env: move extern ...
151
152
153
  #ifdef ENV_IS_EMBEDDED
  extern env_t environment;
  #endif /* ENV_IS_EMBEDDED */
d1459f0fa   Igor Grinberg   env: move extern ...
154
  extern const unsigned char default_environment[];
27aafe988   Igor Grinberg   env: clean env_co...
155
  extern env_t *env_ptr;
27aafe988   Igor Grinberg   env: clean env_co...
156
157
158
  #if defined(CONFIG_NEEDS_MANUAL_RELOC)
  extern void env_reloc(void);
  #endif
d1459f0fa   Igor Grinberg   env: move extern ...
159

ac77f42d0   Dmitry Lifshitz   env_mmc: add mmc_...
160
161
162
163
  #ifdef CONFIG_ENV_IS_IN_MMC
  #include <mmc.h>
  
  extern int mmc_get_env_addr(struct mmc *mmc, int copy, u32 *env_addr);
6e7b7df4d   Dmitry Lifshitz   env_mmc: support ...
164
165
166
  # ifdef CONFIG_SYS_MMC_ENV_PART
  extern uint mmc_get_env_part(struct mmc *mmc);
  # endif
ac77f42d0   Dmitry Lifshitz   env_mmc: add mmc_...
167
  #endif
2eb1573f0   Mike Frysinger   hashtable: drop a...
168
  #ifndef DO_DEPS_ONLY
170ab1107   Joe Hershberger   env: Add support ...
169
170
  #include <env_attr.h>
  #include <env_callback.h>
2598090b7   Joe Hershberger   env: Add environm...
171
  #include <env_flags.h>
2eb1573f0   Mike Frysinger   hashtable: drop a...
172
  #include <search.h>
203e94f6c   Simon Glass   env: Add an enum ...
173
174
175
176
177
178
  /* Value for environment validity */
  enum env_valid {
  	ENV_INVALID,	/* No valid environment */
  	ENV_VALID,	/* First or only environment is valid */
  	ENV_REDUND,	/* Redundant environment is valid */
  };
4415f1d1f   Simon Glass   env: Create a loc...
179
  enum env_location {
e1caa5841   York Sun   env: Fix env_load...
180
  	ENVL_UNKNOWN,
4415f1d1f   Simon Glass   env: Create a loc...
181
  	ENVL_EEPROM,
aaef173cb   Ye Li   MLK-18141-1 env: ...
182
  	ENVL_ESATA,
4415f1d1f   Simon Glass   env: Create a loc...
183
184
185
186
187
188
189
190
191
192
193
194
195
  	ENVL_EXT4,
  	ENVL_FAT,
  	ENVL_FLASH,
  	ENVL_MMC,
  	ENVL_NAND,
  	ENVL_NVRAM,
  	ENVL_ONENAND,
  	ENVL_REMOTE,
  	ENVL_SPI_FLASH,
  	ENVL_UBI,
  	ENVL_NOWHERE,
  
  	ENVL_COUNT,
4415f1d1f   Simon Glass   env: Create a loc...
196
  };
8a3a7e227   Maxime Ripard   env: Pass additio...
197
198
199
200
201
202
203
  /* value for the various operations we want to perform on the env */
  enum env_operation {
  	ENVOP_GET_CHAR,	/* we want to call the get_char function */
  	ENVOP_INIT,	/* we want to call the init function */
  	ENVOP_LOAD,	/* we want to call the load function */
  	ENVOP_SAVE,	/* we want to call the save function */
  };
4415f1d1f   Simon Glass   env: Create a loc...
204
  struct env_driver {
ac358beb8   Simon Glass   env: Drop the env...
205
  	const char *name;
4415f1d1f   Simon Glass   env: Create a loc...
206
207
208
  	enum env_location location;
  
  	/**
4415f1d1f   Simon Glass   env: Create a loc...
209
210
211
212
  	 * load() - Load the environment from storage
  	 *
  	 * This method is optional. If not provided, no environment will be
  	 * loaded.
c59519919   Simon Glass   env: Adjust the l...
213
214
  	 *
  	 * @return 0 if OK, -ve on error
4415f1d1f   Simon Glass   env: Create a loc...
215
  	 */
c59519919   Simon Glass   env: Adjust the l...
216
  	int (*load)(void);
4415f1d1f   Simon Glass   env: Create a loc...
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
  
  	/**
  	 * save() - Save the environment to storage
  	 *
  	 * This method is required for 'saveenv' to work.
  	 *
  	 * @return 0 if OK, -ve on error
  	 */
  	int (*save)(void);
  
  	/**
  	 * init() - Set up the initial pre-relocation environment
  	 *
  	 * This method is optional.
  	 *
7938822a6   Simon Glass   env: Drop common ...
232
233
  	 * @return 0 if OK, -ENOENT if no initial environment could be found,
  	 * other -ve on error
4415f1d1f   Simon Glass   env: Create a loc...
234
235
236
237
238
239
240
  	 */
  	int (*init)(void);
  };
  
  /* Declare a new environment location driver */
  #define U_BOOT_ENV_LOCATION(__name)					\
  	ll_entry_declare(struct env_driver, __name, env_driver)
ac358beb8   Simon Glass   env: Drop the env...
241
242
243
244
245
246
  /* Declare the name of a location */
  #ifdef CONFIG_CMD_SAVEENV
  #define ENV_NAME(_name) .name = _name,
  #else
  #define ENV_NAME(_name)
  #endif
4415f1d1f   Simon Glass   env: Create a loc...
247
248
249
250
251
  #ifdef CONFIG_CMD_SAVEENV
  #define env_save_ptr(x) x
  #else
  #define env_save_ptr(x) NULL
  #endif
2eb1573f0   Mike Frysinger   hashtable: drop a...
252
  extern struct hsearch_data env_htab;
a8409f4f1   Wolfgang Denk   environment: clea...
253
  /* Function that updates CRC of the enironment */
507651d61   Igor Grinberg   env: clean enviro...
254
  void env_crc_update(void);
a8409f4f1   Wolfgang Denk   environment: clea...
255

267541f77   Joe Hershberger   env: Add support ...
256
  /* Look up the variable from the default environment */
723806cc5   Simon Glass   env: Rename some ...
257
  char *env_get_default(const char *name);
267541f77   Joe Hershberger   env: Add support ...
258

5bb12dbd7   Harald Welte   Remove code dupli...
259
  /* [re]set to the default environment */
ea882baf9   Wolfgang Denk   New implementatio...
260
  void set_default_env(const char *s);
b64b7c3df   Gerlando Falauto   env: make "env de...
261
262
  /* [re]set individual variables to their value in the default environment */
  int set_default_vars(int nvars, char * const vars[]);
ea882baf9   Wolfgang Denk   New implementatio...
263
264
  /* Import from binary representation into hash table */
  int env_import(const char *buf, int check);
5bb12dbd7   Harald Welte   Remove code dupli...
265

7ce1526ed   Marek Vasut   env: Add env_expo...
266
267
  /* Export from hash table into binary representation */
  int env_export(env_t *env_out);
76768f5f0   Fiach Antaw   env: Add generic ...
268
269
  #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
  /* Select and import one of two redundant environments */
31f044bd9   Simon Goldschmidt   env: move more co...
270
271
  int env_import_redund(const char *buf1, int buf1_status,
  		      const char *buf2, int buf2_status);
76768f5f0   Fiach Antaw   env: Add generic ...
272
  #endif
ac358beb8   Simon Glass   env: Drop the env...
273
  /**
a69d0f60e   Simon Glass   env: Drop env_get...
274
275
276
277
278
279
280
281
   * env_get_char() - Get a character from the early environment
   *
   * This reads from the pre-relocation environemnt
   *
   * @index: Index of character to read (0 = first)
   * @return character read, or -ve on error
   */
  int env_get_char(int index);
310fb14b2   Simon Glass   env: Drop env_rel...
282
283
284
285
286
287
  /**
   * env_load() - Load the environment from storage
   *
   * @return 0 if OK, -ve on error
   */
  int env_load(void);
01510091d   Simon Glass   env: Drop saveenv...
288
289
290
291
292
293
  /**
   * env_save() - Save the environment to storage
   *
   * @return 0 if OK, -ve on error
   */
  int env_save(void);
507651d61   Igor Grinberg   env: clean enviro...
294
  #endif /* DO_DEPS_ONLY */
2eb1573f0   Mike Frysinger   hashtable: drop a...
295

507651d61   Igor Grinberg   env: clean enviro...
296
  #endif /* _ENVIRONMENT_H_ */