1 /* 2 * (C) Copyright 2002-2008 3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de. 4 * 5 * SPDX-License-Identifier: GPL-2.0+ 6 */ 7 8 #include <stdint.h> 9 #include <uboot_aes.h> 10 11 /* 12 * Programs using the library must check which API is available, 13 * that varies depending on the U-Boot version. 14 * This can be changed in future 15 */ 16 #define FW_ENV_API_VERSION 1 17 18 struct env_opts { 19 #ifdef CONFIG_FILE 20 char *config_file; 21 #endif 22 int aes_flag; /* Is AES encryption used? */ 23 uint8_t aes_key[AES_KEY_LENGTH]; 24 char *lockname; 25 }; 26 27 int parse_aes_key(char *key, uint8_t *bin_key); 28 29 /** 30 * fw_printenv() - print one or several environment variables 31 * 32 * @argc: number of variables names to be printed, prints all if 0 33 * @argv: array of variable names to be printed, if argc != 0 34 * @value_only: do not repeat the variable name, print the bare value, 35 * only one variable allowed with this option, argc must be 1 36 * @opts: encryption key, configuration file, defaults are used if NULL 37 * 38 * Description: 39 * Uses fw_env_open, fw_getenv 40 * 41 * Return: 42 * 0 on success, -1 on failure (modifies errno) 43 */ 44 int fw_printenv(int argc, char *argv[], int value_only, struct env_opts *opts); 45 46 /** 47 * fw_setenv() - adds or removes one variable to the environment 48 * 49 * @argc: number of strings in argv, argv[0] is variable name, 50 * argc==1 means erase variable, argc > 1 means add a variable 51 * @argv: argv[0] is variable name, argv[1..argc-1] are concatenated separated 52 * by single blank and set as the new value of the variable 53 * @opts: how to retrieve environment from flash, defaults are used if NULL 54 * 55 * Description: 56 * Uses fw_env_open, fw_env_write, fw_env_flush 57 * 58 * Return: 59 * 0 on success, -1 on failure (modifies errno) 60 * 61 * ERRORS: 62 * EROFS - some variables ("ethaddr", "serial#") cannot be modified 63 */ 64 int fw_setenv(int argc, char *argv[], struct env_opts *opts); 65 66 /** 67 * fw_parse_script() - adds or removes multiple variables with a batch script 68 * 69 * @fname: batch script file name 70 * @opts: encryption key, configuration file, defaults are used if NULL 71 * 72 * Description: 73 * Uses fw_env_open, fw_env_write, fw_env_flush 74 * 75 * Return: 76 * 0 success, -1 on failure (modifies errno) 77 * 78 * Script Syntax: 79 * 80 * key [ [space]+ value] 81 * 82 * lines starting with '#' treated as comment 83 * 84 * A variable without value will be deleted. Any number of spaces are allowed 85 * between key and value. The value starts with the first non-space character 86 * and ends with newline. No comments allowed on these lines. Spaces inside 87 * the value are preserved verbatim. 88 * 89 * Script Example: 90 * 91 * netdev eth0 92 * 93 * kernel_addr 400000 94 * 95 * foo spaces are copied verbatim 96 * 97 * # delete variable bar 98 * 99 * bar 100 */ 101 int fw_parse_script(char *fname, struct env_opts *opts); 102 103 104 /** 105 * fw_env_open() - read enviroment from flash into RAM cache 106 * 107 * @opts: encryption key, configuration file, defaults are used if NULL 108 * 109 * Return: 110 * 0 on success, -1 on failure (modifies errno) 111 */ 112 int fw_env_open(struct env_opts *opts); 113 114 /** 115 * fw_getenv() - lookup variable in the RAM cache 116 * 117 * @name: variable to be searched 118 * Return: 119 * pointer to start of value, NULL if not found 120 */ 121 char *fw_getenv(char *name); 122 123 /** 124 * fw_env_write() - modify a variable held in the RAM cache 125 * 126 * @name: variable 127 * @value: delete variable if NULL, otherwise create or overwrite the variable 128 * 129 * This is called in sequence to update the environment in RAM without updating 130 * the copy in flash after each set 131 * 132 * Return: 133 * 0 on success, -1 on failure (modifies errno) 134 * 135 * ERRORS: 136 * EROFS - some variables ("ethaddr", "serial#") cannot be modified 137 */ 138 int fw_env_write(char *name, char *value); 139 140 /** 141 * fw_env_flush - write the environment from RAM cache back to flash 142 * 143 * @opts: encryption key, configuration file, defaults are used if NULL 144 * 145 * Return: 146 * 0 on success, -1 on failure (modifies errno) 147 */ 148 int fw_env_flush(struct env_opts *opts); 149 150 /** 151 * fw_env_close - free allocated structure and close env 152 * 153 * @opts: encryption key, configuration file, defaults are used if NULL 154 * 155 * Return: 156 * 0 on success, -1 on failure (modifies errno) 157 */ 158 int fw_env_close(struct env_opts *opts); 159 160 161 /** 162 * fw_env_version - return the current version of the library 163 * 164 * Return: 165 * version string of the library 166 */ 167 char *fw_env_version(void); 168 169 unsigned long crc32(unsigned long, const unsigned char *, unsigned); 170