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 struct env_opts { 12 #ifdef CONFIG_FILE 13 char *config_file; 14 #endif 15 int aes_flag; /* Is AES encryption used? */ 16 uint8_t aes_key[AES_KEY_LENGTH]; 17 char *lockname; 18 }; 19 20 int parse_aes_key(char *key, uint8_t *bin_key); 21 22 /** 23 * fw_printenv() - print one or several environment variables 24 * 25 * @argc: number of variables names to be printed, prints all if 0 26 * @argv: array of variable names to be printed, if argc != 0 27 * @value_only: do not repeat the variable name, print the bare value, 28 * only one variable allowed with this option, argc must be 1 29 * @opts: encryption key, configuration file, defaults are used if NULL 30 * 31 * Description: 32 * Uses fw_env_open, fw_getenv 33 * 34 * Return: 35 * 0 on success, -1 on failure (modifies errno) 36 */ 37 int fw_printenv(int argc, char *argv[], int value_only, struct env_opts *opts); 38 39 /** 40 * fw_setenv() - adds or removes one variable to the environment 41 * 42 * @argc: number of strings in argv, argv[0] is variable name, 43 * argc==1 means erase variable, argc > 1 means add a variable 44 * @argv: argv[0] is variable name, argv[1..argc-1] are concatenated separated 45 * by single blank and set as the new value of the variable 46 * @opts: how to retrieve environment from flash, defaults are used if NULL 47 * 48 * Description: 49 * Uses fw_env_open, fw_env_write, fw_env_close 50 * 51 * Return: 52 * 0 on success, -1 on failure (modifies errno) 53 * 54 * ERRORS: 55 * EROFS - some variables ("ethaddr", "serial#") cannot be modified 56 */ 57 int fw_setenv(int argc, char *argv[], struct env_opts *opts); 58 59 /** 60 * fw_parse_script() - adds or removes multiple variables with a batch script 61 * 62 * @fname: batch script file name 63 * @opts: encryption key, configuration file, defaults are used if NULL 64 * 65 * Description: 66 * Uses fw_env_open, fw_env_write, fw_env_close 67 * 68 * Return: 69 * 0 success, -1 on failure (modifies errno) 70 * 71 * Script Syntax: 72 * 73 * key [ [space]+ value] 74 * 75 * lines starting with '#' treated as comment 76 * 77 * A variable without value will be deleted. Any number of spaces are allowed 78 * between key and value. The value starts with the first non-space character 79 * and ends with newline. No comments allowed on these lines. Spaces inside 80 * the value are preserved verbatim. 81 * 82 * Script Example: 83 * 84 * netdev eth0 85 * 86 * kernel_addr 400000 87 * 88 * foo spaces are copied verbatim 89 * 90 * # delete variable bar 91 * 92 * bar 93 */ 94 int fw_parse_script(char *fname, struct env_opts *opts); 95 96 97 /** 98 * fw_env_open() - read enviroment from flash into RAM cache 99 * 100 * @opts: encryption key, configuration file, defaults are used if NULL 101 * 102 * Return: 103 * 0 on success, -1 on failure (modifies errno) 104 */ 105 int fw_env_open(struct env_opts *opts); 106 107 /** 108 * fw_getenv() - lookup variable in the RAM cache 109 * 110 * @name: variable to be searched 111 * Return: 112 * pointer to start of value, NULL if not found 113 */ 114 char *fw_getenv(char *name); 115 116 /** 117 * fw_env_write() - modify a variable held in the RAM cache 118 * 119 * @name: variable 120 * @value: delete variable if NULL, otherwise create or overwrite the variable 121 * 122 * This is called in sequence to update the environment in RAM without updating 123 * the copy in flash after each set 124 * 125 * Return: 126 * 0 on success, -1 on failure (modifies errno) 127 * 128 * ERRORS: 129 * EROFS - some variables ("ethaddr", "serial#") cannot be modified 130 */ 131 int fw_env_write(char *name, char *value); 132 133 /** 134 * fw_env_close - write the environment from RAM cache back to flash 135 * 136 * @opts: encryption key, configuration file, defaults are used if NULL 137 * 138 * Return: 139 * 0 on success, -1 on failure (modifies errno) 140 */ 141 int fw_env_close(struct env_opts *opts); 142 143 unsigned long crc32(unsigned long, const unsigned char *, unsigned); 144