xref: /rk3399_rockchip-uboot/include/environment.h (revision e55dfbd47140353ad2ac122e706d44b699c8162a)
1 /*
2  * (C) Copyright 2002
3  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4  *
5  * SPDX-License-Identifier:	GPL-2.0+
6  */
7 
8 #ifndef _ENVIRONMENT_H_
9 #define _ENVIRONMENT_H_
10 
11 #include <stdbool.h>
12 #include <linux/kconfig.h>
13 
14 /**************************************************************************
15  *
16  * The "environment" is stored as a list of '\0' terminated
17  * "name=value" strings. The end of the list is marked by a double
18  * '\0'. New entries are always added at the end. Deleting an entry
19  * shifts the remaining entries to the front. Replacing an entry is a
20  * combination of deleting the old value and adding the new one.
21  *
22  * The environment is preceded by a 32 bit CRC over the data part.
23  *
24  *************************************************************************/
25 
26 #if defined(CONFIG_ENV_IS_IN_FLASH)
27 # ifndef	CONFIG_ENV_ADDR
28 #  define	CONFIG_ENV_ADDR	(CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET)
29 # endif
30 # ifndef	CONFIG_ENV_OFFSET
31 #  define	CONFIG_ENV_OFFSET (CONFIG_ENV_ADDR - CONFIG_SYS_FLASH_BASE)
32 # endif
33 # if !defined(CONFIG_ENV_ADDR_REDUND) && defined(CONFIG_ENV_OFFSET_REDUND)
34 #  define	CONFIG_ENV_ADDR_REDUND	\
35 		(CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET_REDUND)
36 # endif
37 # if defined(CONFIG_ENV_SECT_SIZE) || defined(CONFIG_ENV_SIZE)
38 #  ifndef	CONFIG_ENV_SECT_SIZE
39 #   define	CONFIG_ENV_SECT_SIZE	CONFIG_ENV_SIZE
40 #  endif
41 #  ifndef	CONFIG_ENV_SIZE
42 #   define	CONFIG_ENV_SIZE	CONFIG_ENV_SECT_SIZE
43 #  endif
44 # else
45 #  error "Both CONFIG_ENV_SECT_SIZE and CONFIG_ENV_SIZE undefined"
46 # endif
47 # if defined(CONFIG_ENV_ADDR_REDUND) && !defined(CONFIG_ENV_SIZE_REDUND)
48 #  define CONFIG_ENV_SIZE_REDUND	CONFIG_ENV_SIZE
49 # endif
50 # if	(CONFIG_ENV_ADDR >= CONFIG_SYS_MONITOR_BASE) &&		\
51 	(CONFIG_ENV_ADDR + CONFIG_ENV_SIZE) <=			\
52 	(CONFIG_SYS_MONITOR_BASE + CONFIG_SYS_MONITOR_LEN)
53 #  define ENV_IS_EMBEDDED
54 # endif
55 # if defined(CONFIG_ENV_ADDR_REDUND) || defined(CONFIG_ENV_OFFSET_REDUND)
56 #  define CONFIG_SYS_REDUNDAND_ENVIRONMENT
57 # endif
58 # ifdef CONFIG_ENV_IS_EMBEDDED
59 #  error "do not define CONFIG_ENV_IS_EMBEDDED in your board config"
60 #  error "it is calculated automatically for you"
61 # endif
62 #endif	/* CONFIG_ENV_IS_IN_FLASH */
63 
64 #if defined(CONFIG_ENV_IS_IN_MMC)
65 # ifdef CONFIG_ENV_OFFSET_REDUND
66 #  define CONFIG_SYS_REDUNDAND_ENVIRONMENT
67 # endif
68 #endif
69 
70 #if defined(CONFIG_ENV_IS_IN_NAND)
71 # if defined(CONFIG_ENV_OFFSET_OOB)
72 #  ifdef CONFIG_ENV_OFFSET_REDUND
73 #   error "CONFIG_ENV_OFFSET_REDUND is not supported when CONFIG_ENV_OFFSET_OOB"
74 #   error "is set"
75 #  endif
76 extern unsigned long nand_env_oob_offset;
77 #  define CONFIG_ENV_OFFSET nand_env_oob_offset
78 # else
79 #  ifndef CONFIG_ENV_OFFSET
80 #   error "Need to define CONFIG_ENV_OFFSET when using CONFIG_ENV_IS_IN_NAND"
81 #  endif
82 #  ifdef CONFIG_ENV_OFFSET_REDUND
83 #   define CONFIG_SYS_REDUNDAND_ENVIRONMENT
84 #  endif
85 # endif /* CONFIG_ENV_OFFSET_OOB */
86 # ifndef CONFIG_ENV_SIZE
87 #  error "Need to define CONFIG_ENV_SIZE when using CONFIG_ENV_IS_IN_NAND"
88 # endif
89 #endif /* CONFIG_ENV_IS_IN_NAND */
90 
91 #if defined(CONFIG_ENV_IS_IN_UBI)
92 # ifndef CONFIG_ENV_UBI_PART
93 #  error "Need to define CONFIG_ENV_UBI_PART when using CONFIG_ENV_IS_IN_UBI"
94 # endif
95 # ifndef CONFIG_ENV_UBI_VOLUME
96 #  error "Need to define CONFIG_ENV_UBI_VOLUME when using CONFIG_ENV_IS_IN_UBI"
97 # endif
98 # if defined(CONFIG_ENV_UBI_VOLUME_REDUND)
99 #  define CONFIG_SYS_REDUNDAND_ENVIRONMENT
100 # endif
101 # ifndef CONFIG_ENV_SIZE
102 #  error "Need to define CONFIG_ENV_SIZE when using CONFIG_ENV_IS_IN_UBI"
103 # endif
104 # ifndef CONFIG_CMD_UBI
105 #  error "Need to define CONFIG_CMD_UBI when using CONFIG_ENV_IS_IN_UBI"
106 # endif
107 #endif /* CONFIG_ENV_IS_IN_UBI */
108 
109 /* Embedded env is only supported for some flash types */
110 #ifdef CONFIG_ENV_IS_EMBEDDED
111 # if	!defined(CONFIG_ENV_IS_IN_FLASH)	&& \
112 	!defined(CONFIG_ENV_IS_IN_NAND)		&& \
113 	!defined(CONFIG_ENV_IS_IN_ONENAND)	&& \
114 	!defined(CONFIG_ENV_IS_IN_SPI_FLASH)
115 #  error "CONFIG_ENV_IS_EMBEDDED not supported for your flash type"
116 # endif
117 #endif
118 
119 /*
120  * For the flash types where embedded env is supported, but it cannot be
121  * calculated automatically (i.e. NAND), take the board opt-in.
122  */
123 #if defined(CONFIG_ENV_IS_EMBEDDED) && !defined(ENV_IS_EMBEDDED)
124 # define ENV_IS_EMBEDDED
125 #endif
126 
127 /* The build system likes to know if the env is embedded */
128 #ifdef DO_DEPS_ONLY
129 # ifdef ENV_IS_EMBEDDED
130 #  ifndef CONFIG_ENV_IS_EMBEDDED
131 #   define CONFIG_ENV_IS_EMBEDDED
132 #  endif
133 # endif
134 #endif
135 
136 #include "compiler.h"
137 
138 #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
139 # define ENV_HEADER_SIZE	(sizeof(uint32_t) + 1)
140 
141 # define ACTIVE_FLAG   1
142 # define OBSOLETE_FLAG 0
143 #else
144 # define ENV_HEADER_SIZE	(sizeof(uint32_t))
145 #endif
146 
147 /* Select the large size value if required */
148 #if defined(CONFIG_ENV_NAND_SIZE) && (CONFIG_ENV_SIZE < CONFIG_ENV_NAND_SIZE)
149 #define ENV_SIZE_VAL	CONFIG_ENV_NAND_SIZE
150 #else
151 #define ENV_SIZE_VAL	CONFIG_ENV_SIZE
152 #endif
153 
154 #ifdef CONFIG_ENV_AES
155 /* Make sure the payload is multiple of AES block size */
156 #define ENV_SIZE ((ENV_SIZE_VAL - ENV_HEADER_SIZE) & ~(16 - 1))
157 #else
158 #define ENV_SIZE (ENV_SIZE_VAL - ENV_HEADER_SIZE)
159 #endif
160 
161 typedef struct environment_s {
162 	uint32_t	crc;		/* CRC32 over data bytes	*/
163 #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
164 	unsigned char	flags;		/* active/obsolete flags	*/
165 #endif
166 	unsigned char	data[ENV_SIZE]; /* Environment data		*/
167 } env_t
168 #ifdef CONFIG_ENV_AES
169 /* Make sure the env is aligned to block size. */
170 __attribute__((aligned(16)))
171 #endif
172 ;
173 
174 #ifdef ENV_IS_EMBEDDED
175 extern env_t environment;
176 #endif /* ENV_IS_EMBEDDED */
177 
178 extern const unsigned char default_environment[];
179 extern env_t *env_ptr;
180 
181 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
182 extern void env_reloc(void);
183 #endif
184 
185 #ifdef CONFIG_ENV_IS_IN_MMC
186 #include <mmc.h>
187 
188 extern int mmc_get_env_addr(struct mmc *mmc, int copy, u32 *env_addr);
189 # ifdef CONFIG_SYS_MMC_ENV_PART
190 extern uint mmc_get_env_part(struct mmc *mmc);
191 # endif
192 #endif
193 
194 #ifndef DO_DEPS_ONLY
195 
196 #include <env_attr.h>
197 #include <env_callback.h>
198 #include <env_flags.h>
199 #include <search.h>
200 
201 /* Value for environment validity */
202 enum env_valid {
203 	ENV_INVALID,	/* No valid environment */
204 	ENV_VALID,	/* First or only environment is valid */
205 	ENV_REDUND,	/* Redundant environment is valid */
206 };
207 
208 enum env_location {
209 	ENVL_EEPROM,
210 	ENVL_EXT4,
211 	ENVL_FAT,
212 	ENVL_FLASH,
213 	ENVL_MMC,
214 	ENVL_NAND,
215 	ENVL_NVRAM,
216 	ENVL_ONENAND,
217 	ENVL_REMOTE,
218 	ENVL_SPI_FLASH,
219 	ENVL_UBI,
220 	ENVL_NOWHERE,
221 	ENVL_BLK,
222 
223 	ENVL_COUNT,
224 	ENVL_UNKNOWN,
225 };
226 
227 struct env_driver {
228 	const char *name;
229 	enum env_location location;
230 
231 	/**
232 	 * get_char() - Read a character from the environment
233 	 *
234 	 * This method is optional. If not provided, a default implementation
235 	 * will read from gd->env_addr.
236 	 *
237 	 * @index: Index of character to read (0=first)
238 	 * @return character read, or -ve on error
239 	 */
240 	int (*get_char)(int index);
241 
242 	/**
243 	 * load() - Load the environment from storage
244 	 *
245 	 * This method is optional. If not provided, no environment will be
246 	 * loaded.
247 	 *
248 	 * @return 0 if OK, -ve on error
249 	 */
250 	int (*load)(void);
251 
252 	/**
253 	 * save() - Save the environment to storage
254 	 *
255 	 * This method is required for 'saveenv' to work.
256 	 *
257 	 * @return 0 if OK, -ve on error
258 	 */
259 	int (*save)(void);
260 
261 	/**
262 	 * init() - Set up the initial pre-relocation environment
263 	 *
264 	 * This method is optional.
265 	 *
266 	 * @return 0 if OK, -ENOENT if no initial environment could be found,
267 	 * other -ve on error
268 	 */
269 	int (*init)(void);
270 };
271 
272 /* Declare a new environment location driver */
273 #define U_BOOT_ENV_LOCATION(__name)					\
274 	ll_entry_declare(struct env_driver, __name, env_driver)
275 
276 /* Declare the name of a location */
277 #ifdef CONFIG_CMD_SAVEENV
278 #define ENV_NAME(_name) .name = _name,
279 #else
280 #define ENV_NAME(_name)
281 #endif
282 
283 #ifdef CONFIG_CMD_SAVEENV
284 #define env_save_ptr(x) x
285 #else
286 #define env_save_ptr(x) NULL
287 #endif
288 
289 extern struct hsearch_data env_htab;
290 
291 /* Function that updates CRC of the enironment */
292 void env_crc_update(void);
293 
294 /* Look up the variable from the default environment */
295 char *env_get_default(const char *name);
296 
297 /* [re]set to the default environment */
298 void set_default_env(const char *s);
299 
300 /* [re]set to the board environment */
301 int set_board_env(const char *vars, int size, int flags, bool ready);
302 
303 /* [re]set individual variables to their value in the default environment */
304 int set_default_vars(int nvars, char * const vars[]);
305 
306 /* Import from binary representation into hash table */
307 int env_import(const char *buf, int check);
308 
309 /* Export from hash table into binary representation */
310 int env_export(env_t *env_out);
311 
312 #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
313 /* Select and import one of two redundant environments */
314 int env_import_redund(const char *buf1, const char *buf2);
315 #endif
316 
317 /**
318  * env_driver_lookup_default() - Look up the default environment driver
319  *
320  * @return pointer to driver, or NULL if none (which should not happen)
321  */
322 struct env_driver *env_driver_lookup_default(void);
323 
324 /**
325  * env_get_char() - Get a character from the early environment
326  *
327  * This reads from the pre-relocation environemnt
328  *
329  * @index: Index of character to read (0 = first)
330  * @return character read, or -ve on error
331  */
332 int env_get_char(int index);
333 
334 /**
335  * env_load() - Load the environment from storage
336  *
337  * @return 0 if OK, -ve on error
338  */
339 int env_load(void);
340 
341 /**
342  * env_save() - Save the environment to storage
343  *
344  * @return 0 if OK, -ve on error
345  */
346 int env_save(void);
347 
348 #endif /* DO_DEPS_ONLY */
349 
350 #endif /* _ENVIRONMENT_H_ */
351