xref: /rk3399_rockchip-uboot/include/environment.h (revision 994bc671cf8fd3f6d41bb680da738f814f4a9f38)
1c609719bSwdenk /*
2c609719bSwdenk  * (C) Copyright 2002
3c609719bSwdenk  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4c609719bSwdenk  *
5c609719bSwdenk  * See file CREDITS for list of people who contributed to this
6c609719bSwdenk  * project.
7c609719bSwdenk  *
8c609719bSwdenk  * This program is free software; you can redistribute it and/or
9c609719bSwdenk  * modify it under the terms of the GNU General Public License as
10c609719bSwdenk  * published by the Free Software Foundation; either version 2 of
11c609719bSwdenk  * the License, or (at your option) any later version.
12c609719bSwdenk  *
13c609719bSwdenk  * This program is distributed in the hope that it will be useful,
14c609719bSwdenk  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15c609719bSwdenk  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16c609719bSwdenk  * GNU General Public License for more details.
17c609719bSwdenk  *
18c609719bSwdenk  * You should have received a copy of the GNU General Public License
19c609719bSwdenk  * along with this program; if not, write to the Free Software
20c609719bSwdenk  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21c609719bSwdenk  * MA 02111-1307 USA
22c609719bSwdenk  */
23c609719bSwdenk 
24c609719bSwdenk #ifndef _ENVIRONMENT_H_
25c609719bSwdenk #define _ENVIRONMENT_H_	1
26c609719bSwdenk 
27c609719bSwdenk /**************************************************************************
28c609719bSwdenk  *
29c609719bSwdenk  * The "environment" is stored as a list of '\0' terminated
30c609719bSwdenk  * "name=value" strings. The end of the list is marked by a double
31c609719bSwdenk  * '\0'. New entries are always added at the end. Deleting an entry
32c609719bSwdenk  * shifts the remaining entries to the front. Replacing an entry is a
33c609719bSwdenk  * combination of deleting the old value and adding the new one.
34c609719bSwdenk  *
35c609719bSwdenk  * The environment is preceeded by a 32 bit CRC over the data part.
36c609719bSwdenk  *
37c609719bSwdenk  **************************************************************************
38c609719bSwdenk  */
39c609719bSwdenk 
405a1aceb0SJean-Christophe PLAGNIOL-VILLARD #if defined(CONFIG_ENV_IS_IN_FLASH)
410e8d1586SJean-Christophe PLAGNIOL-VILLARD # ifndef	CONFIG_ENV_ADDR
426d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #  define	CONFIG_ENV_ADDR	(CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET)
43c609719bSwdenk # endif
440e8d1586SJean-Christophe PLAGNIOL-VILLARD # ifndef	CONFIG_ENV_OFFSET
456d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #  define	CONFIG_ENV_OFFSET (CONFIG_ENV_ADDR - CONFIG_SYS_FLASH_BASE)
46c609719bSwdenk # endif
470e8d1586SJean-Christophe PLAGNIOL-VILLARD # if !defined(CONFIG_ENV_ADDR_REDUND) && defined(CONFIG_ENV_OFFSET_REDUND)
48507651d6SIgor Grinberg #  define	CONFIG_ENV_ADDR_REDUND	\
49507651d6SIgor Grinberg 		(CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET_REDUND)
50c609719bSwdenk # endif
510e8d1586SJean-Christophe PLAGNIOL-VILLARD # if defined(CONFIG_ENV_SECT_SIZE) || defined(CONFIG_ENV_SIZE)
520e8d1586SJean-Christophe PLAGNIOL-VILLARD #  ifndef	CONFIG_ENV_SECT_SIZE
530e8d1586SJean-Christophe PLAGNIOL-VILLARD #   define	CONFIG_ENV_SECT_SIZE	CONFIG_ENV_SIZE
54500545ccSwdenk #  endif
550e8d1586SJean-Christophe PLAGNIOL-VILLARD #  ifndef	CONFIG_ENV_SIZE
560e8d1586SJean-Christophe PLAGNIOL-VILLARD #   define	CONFIG_ENV_SIZE	CONFIG_ENV_SECT_SIZE
57c609719bSwdenk #  endif
58500545ccSwdenk # else
590e8d1586SJean-Christophe PLAGNIOL-VILLARD #  error "Both CONFIG_ENV_SECT_SIZE and CONFIG_ENV_SIZE undefined"
60500545ccSwdenk # endif
610e8d1586SJean-Christophe PLAGNIOL-VILLARD # if defined(CONFIG_ENV_ADDR_REDUND) && !defined(CONFIG_ENV_SIZE_REDUND)
620e8d1586SJean-Christophe PLAGNIOL-VILLARD #  define CONFIG_ENV_SIZE_REDUND	CONFIG_ENV_SIZE
63c609719bSwdenk # endif
646d0f6bcfSJean-Christophe PLAGNIOL-VILLARD # if	(CONFIG_ENV_ADDR >= CONFIG_SYS_MONITOR_BASE) &&		\
65507651d6SIgor Grinberg 	(CONFIG_ENV_ADDR + CONFIG_ENV_SIZE) <=			\
66507651d6SIgor Grinberg 	(CONFIG_SYS_MONITOR_BASE + CONFIG_SYS_MONITOR_LEN)
67c609719bSwdenk #  define ENV_IS_EMBEDDED	1
68c609719bSwdenk # endif
690e8d1586SJean-Christophe PLAGNIOL-VILLARD # if defined(CONFIG_ENV_ADDR_REDUND) || defined(CONFIG_ENV_OFFSET_REDUND)
706d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #  define CONFIG_SYS_REDUNDAND_ENVIRONMENT	1
71c609719bSwdenk # endif
720a9e4e77SMike Frysinger # ifdef CONFIG_ENV_IS_EMBEDDED
730a9e4e77SMike Frysinger #  error "do not define CONFIG_ENV_IS_EMBEDDED in your board config"
740a9e4e77SMike Frysinger #  error "it is calculated automatically for you"
750a9e4e77SMike Frysinger # endif
765a1aceb0SJean-Christophe PLAGNIOL-VILLARD #endif	/* CONFIG_ENV_IS_IN_FLASH */
77c609719bSwdenk 
7851bfee19SJean-Christophe PLAGNIOL-VILLARD #if defined(CONFIG_ENV_IS_IN_NAND)
79c9f7351bSBen Gardiner # if defined(CONFIG_ENV_OFFSET_OOB)
80c9f7351bSBen Gardiner #  ifdef CONFIG_ENV_OFFSET_REDUND
81c9f7351bSBen Gardiner #   error "CONFIG_ENV_OFFSET_REDUND is not supported when CONFIG_ENV_OFFSET_OOB"
82c9f7351bSBen Gardiner #   error "is set"
83c9f7351bSBen Gardiner #  endif
84c9f7351bSBen Gardiner extern unsigned long nand_env_oob_offset;
85c9f7351bSBen Gardiner #  define CONFIG_ENV_OFFSET nand_env_oob_offset
86c9f7351bSBen Gardiner # else
870e8d1586SJean-Christophe PLAGNIOL-VILLARD #  ifndef CONFIG_ENV_OFFSET
880e8d1586SJean-Christophe PLAGNIOL-VILLARD #   error "Need to define CONFIG_ENV_OFFSET when using CONFIG_ENV_IS_IN_NAND"
89e443c944SMarkus Klotzbuecher #  endif
900e8d1586SJean-Christophe PLAGNIOL-VILLARD #  ifdef CONFIG_ENV_OFFSET_REDUND
916d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #   define CONFIG_SYS_REDUNDAND_ENVIRONMENT
92e443c944SMarkus Klotzbuecher #  endif
93c9f7351bSBen Gardiner # endif /* CONFIG_ENV_OFFSET_OOB */
94c9f7351bSBen Gardiner # ifndef CONFIG_ENV_SIZE
95c9f7351bSBen Gardiner #  error "Need to define CONFIG_ENV_SIZE when using CONFIG_ENV_IS_IN_NAND"
96c9f7351bSBen Gardiner # endif
9751bfee19SJean-Christophe PLAGNIOL-VILLARD #endif /* CONFIG_ENV_IS_IN_NAND */
98e443c944SMarkus Klotzbuecher 
9975eb82ecSunsik Kim #if defined(CONFIG_ENV_IS_IN_MG_DISK)
10075eb82ecSunsik Kim # ifndef CONFIG_ENV_ADDR
10175eb82ecSunsik Kim #  error "Need to define CONFIG_ENV_ADDR when using CONFIG_ENV_IS_IN_MG_DISK"
10275eb82ecSunsik Kim # endif
10375eb82ecSunsik Kim # ifndef CONFIG_ENV_SIZE
10475eb82ecSunsik Kim #  error "Need to define CONFIG_ENV_SIZE when using CONFIG_ENV_IS_IN_MG_DISK"
10575eb82ecSunsik Kim # endif
10675eb82ecSunsik Kim #endif /* CONFIG_ENV_IS_IN_MG_DISK */
10775eb82ecSunsik Kim 
1080a9e4e77SMike Frysinger /* Embedded env is only supported for some flash types */
1090a9e4e77SMike Frysinger #ifdef CONFIG_ENV_IS_EMBEDDED
1100a9e4e77SMike Frysinger # if	!defined(CONFIG_ENV_IS_IN_FLASH)	&& \
1110a9e4e77SMike Frysinger 	!defined(CONFIG_ENV_IS_IN_NAND)		&& \
112efb06339SYoshihiro Shimoda 	!defined(CONFIG_ENV_IS_IN_ONENAND)	&& \
113efb06339SYoshihiro Shimoda 	!defined(CONFIG_ENV_IS_IN_SPI_FLASH)
1140a9e4e77SMike Frysinger #  error "CONFIG_ENV_IS_EMBEDDED not supported for your flash type"
1150a9e4e77SMike Frysinger # endif
1160a9e4e77SMike Frysinger #endif
1170a9e4e77SMike Frysinger 
1180a9e4e77SMike Frysinger /*
1190a9e4e77SMike Frysinger  * For the flash types where embedded env is supported, but it cannot be
1200a9e4e77SMike Frysinger  * calculated automatically (i.e. NAND), take the board opt-in.
1210a9e4e77SMike Frysinger  */
1220a9e4e77SMike Frysinger #if defined(CONFIG_ENV_IS_EMBEDDED) && !defined(ENV_IS_EMBEDDED)
1230a9e4e77SMike Frysinger # define ENV_IS_EMBEDDED 1
1240a9e4e77SMike Frysinger #endif
1250a9e4e77SMike Frysinger 
1260a9e4e77SMike Frysinger /* The build system likes to know if the env is embedded */
1270a9e4e77SMike Frysinger #ifdef DO_DEPS_ONLY
1280a9e4e77SMike Frysinger # ifdef ENV_IS_EMBEDDED
12933a6b9e9SWolfgang Denk #  ifndef CONFIG_ENV_IS_EMBEDDED
1300a9e4e77SMike Frysinger #   define CONFIG_ENV_IS_EMBEDDED
1310a9e4e77SMike Frysinger #  endif
1320a9e4e77SMike Frysinger # endif
13333a6b9e9SWolfgang Denk #endif
1340a9e4e77SMike Frysinger 
13537566090SMike Frysinger #include "compiler.h"
136c609719bSwdenk 
1376d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
13889cdab78SMike Frysinger # define ENV_HEADER_SIZE	(sizeof(uint32_t) + 1)
139b218ccb5SWolfgang Denk 
140b218ccb5SWolfgang Denk # define ACTIVE_FLAG   1
141b218ccb5SWolfgang Denk # define OBSOLETE_FLAG 0
142c609719bSwdenk #else
14389cdab78SMike Frysinger # define ENV_HEADER_SIZE	(sizeof(uint32_t))
144c609719bSwdenk #endif
145c609719bSwdenk 
146c609719bSwdenk 
1470e8d1586SJean-Christophe PLAGNIOL-VILLARD #define ENV_SIZE (CONFIG_ENV_SIZE - ENV_HEADER_SIZE)
148c609719bSwdenk 
149c609719bSwdenk typedef struct environment_s {
15089cdab78SMike Frysinger 	uint32_t	crc;		/* CRC32 over data bytes	*/
1516d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
152c609719bSwdenk 	unsigned char	flags;		/* active/obsolete flags	*/
153c609719bSwdenk #endif
154c609719bSwdenk 	unsigned char	data[ENV_SIZE]; /* Environment data		*/
155c609719bSwdenk } env_t;
156c609719bSwdenk 
157*994bc671SIgor Grinberg #ifdef ENV_IS_EMBEDDED
158*994bc671SIgor Grinberg extern env_t environment;
159*994bc671SIgor Grinberg #endif /* ENV_IS_EMBEDDED */
160*994bc671SIgor Grinberg 
161d1459f0fSIgor Grinberg extern const unsigned char default_environment[];
162d1459f0fSIgor Grinberg 
1632eb1573fSMike Frysinger #ifndef DO_DEPS_ONLY
1642eb1573fSMike Frysinger 
1652eb1573fSMike Frysinger #include <search.h>
1662eb1573fSMike Frysinger 
1672eb1573fSMike Frysinger extern struct hsearch_data env_htab;
1682eb1573fSMike Frysinger 
169b502611bSJoakim Tjernlund /* Function that returns a character from the environment */
170b502611bSJoakim Tjernlund unsigned char env_get_char(int);
171a8409f4fSWolfgang Denk 
172a8409f4fSWolfgang Denk /* Function that returns a pointer to a value from the environment */
173147c7169SMike Frysinger const unsigned char *env_get_addr(int);
174a8409f4fSWolfgang Denk unsigned char env_get_char_memory(int index);
175a8409f4fSWolfgang Denk 
176a8409f4fSWolfgang Denk /* Function that updates CRC of the enironment */
177a8409f4fSWolfgang Denk void env_crc_update(void);
178a8409f4fSWolfgang Denk 
1795bb12dbdSHarald Welte /* [re]set to the default environment */
180ea882bafSWolfgang Denk void set_default_env(const char *s);
181ea882bafSWolfgang Denk 
182ea882bafSWolfgang Denk /* Import from binary representation into hash table */
183ea882bafSWolfgang Denk int env_import(const char *buf, int check);
1845bb12dbdSHarald Welte 
185507651d6SIgor Grinberg #endif /* DO_DEPS_ONLY */
1862eb1573fSMike Frysinger 
187c609719bSwdenk #endif /* _ENVIRONMENT_H_ */
188