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