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