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 #ifdef CONFIG_ENV_AES 148 /* Make sure the payload is multiple of AES block size */ 149 #define ENV_SIZE ((CONFIG_ENV_SIZE - ENV_HEADER_SIZE) & ~(16 - 1)) 150 #else 151 #define ENV_SIZE (CONFIG_ENV_SIZE - ENV_HEADER_SIZE) 152 #endif 153 154 typedef struct environment_s { 155 uint32_t crc; /* CRC32 over data bytes */ 156 #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT 157 unsigned char flags; /* active/obsolete flags */ 158 #endif 159 unsigned char data[ENV_SIZE]; /* Environment data */ 160 } env_t 161 #ifdef CONFIG_ENV_AES 162 /* Make sure the env is aligned to block size. */ 163 __attribute__((aligned(16))) 164 #endif 165 ; 166 167 #ifdef ENV_IS_EMBEDDED 168 extern env_t environment; 169 #endif /* ENV_IS_EMBEDDED */ 170 171 extern const unsigned char default_environment[]; 172 extern env_t *env_ptr; 173 174 #if defined(CONFIG_NEEDS_MANUAL_RELOC) 175 extern void env_reloc(void); 176 #endif 177 178 #ifdef CONFIG_ENV_IS_IN_MMC 179 #include <mmc.h> 180 181 extern int mmc_get_env_addr(struct mmc *mmc, int copy, u32 *env_addr); 182 # ifdef CONFIG_SYS_MMC_ENV_PART 183 extern uint mmc_get_env_part(struct mmc *mmc); 184 # endif 185 #endif 186 187 #ifndef DO_DEPS_ONLY 188 189 #include <env_attr.h> 190 #include <env_callback.h> 191 #include <env_flags.h> 192 #include <search.h> 193 194 /* Value for environment validity */ 195 enum env_valid { 196 ENV_INVALID, /* No valid environment */ 197 ENV_VALID, /* First or only environment is valid */ 198 ENV_REDUND, /* Redundant environment is valid */ 199 }; 200 201 enum env_location { 202 ENVL_EEPROM, 203 ENVL_EXT4, 204 ENVL_FAT, 205 ENVL_FLASH, 206 ENVL_MMC, 207 ENVL_NAND, 208 ENVL_NVRAM, 209 ENVL_ONENAND, 210 ENVL_REMOTE, 211 ENVL_SPI_FLASH, 212 ENVL_UBI, 213 ENVL_NOWHERE, 214 ENVL_BLK, 215 216 ENVL_COUNT, 217 ENVL_UNKNOWN, 218 }; 219 220 struct env_driver { 221 const char *name; 222 enum env_location location; 223 224 /** 225 * get_char() - Read a character from the environment 226 * 227 * This method is optional. If not provided, a default implementation 228 * will read from gd->env_addr. 229 * 230 * @index: Index of character to read (0=first) 231 * @return character read, or -ve on error 232 */ 233 int (*get_char)(int index); 234 235 /** 236 * load() - Load the environment from storage 237 * 238 * This method is optional. If not provided, no environment will be 239 * loaded. 240 * 241 * @return 0 if OK, -ve on error 242 */ 243 int (*load)(void); 244 245 /** 246 * save() - Save the environment to storage 247 * 248 * This method is required for 'saveenv' to work. 249 * 250 * @return 0 if OK, -ve on error 251 */ 252 int (*save)(void); 253 254 /** 255 * init() - Set up the initial pre-relocation environment 256 * 257 * This method is optional. 258 * 259 * @return 0 if OK, -ENOENT if no initial environment could be found, 260 * other -ve on error 261 */ 262 int (*init)(void); 263 }; 264 265 /* Declare a new environment location driver */ 266 #define U_BOOT_ENV_LOCATION(__name) \ 267 ll_entry_declare(struct env_driver, __name, env_driver) 268 269 /* Declare the name of a location */ 270 #ifdef CONFIG_CMD_SAVEENV 271 #define ENV_NAME(_name) .name = _name, 272 #else 273 #define ENV_NAME(_name) 274 #endif 275 276 #ifdef CONFIG_CMD_SAVEENV 277 #define env_save_ptr(x) x 278 #else 279 #define env_save_ptr(x) NULL 280 #endif 281 282 extern struct hsearch_data env_htab; 283 284 /* Function that updates CRC of the enironment */ 285 void env_crc_update(void); 286 287 /* Look up the variable from the default environment */ 288 char *env_get_default(const char *name); 289 290 /* [re]set to the default environment */ 291 void set_default_env(const char *s); 292 293 /* [re]set to the board environment */ 294 int set_board_env(const char *vars, int size, int flags, bool ready); 295 296 /* [re]set individual variables to their value in the default environment */ 297 int set_default_vars(int nvars, char * const vars[]); 298 299 /* Import from binary representation into hash table */ 300 int env_import(const char *buf, int check); 301 302 /* Export from hash table into binary representation */ 303 int env_export(env_t *env_out); 304 305 #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT 306 /* Select and import one of two redundant environments */ 307 int env_import_redund(const char *buf1, const char *buf2); 308 #endif 309 310 /** 311 * env_driver_lookup_default() - Look up the default environment driver 312 * 313 * @return pointer to driver, or NULL if none (which should not happen) 314 */ 315 struct env_driver *env_driver_lookup_default(void); 316 317 /** 318 * env_get_char() - Get a character from the early environment 319 * 320 * This reads from the pre-relocation environemnt 321 * 322 * @index: Index of character to read (0 = first) 323 * @return character read, or -ve on error 324 */ 325 int env_get_char(int index); 326 327 /** 328 * env_load() - Load the environment from storage 329 * 330 * @return 0 if OK, -ve on error 331 */ 332 int env_load(void); 333 334 /** 335 * env_save() - Save the environment to storage 336 * 337 * @return 0 if OK, -ve on error 338 */ 339 int env_save(void); 340 341 #endif /* DO_DEPS_ONLY */ 342 343 #endif /* _ENVIRONMENT_H_ */ 344