wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 1 | /* |
Grant Erickson | 73a4e5e | 2008-05-06 20:16:15 -0700 | [diff] [blame] | 2 | * (C) Copyright 2002-2008 |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
Wolfgang Denk | bd8ec7e | 2013-10-07 13:07:26 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
Andreas Fenkart | aa01f8d | 2015-12-09 13:13:24 +0100 | [diff] [blame] | 8 | #include <stdint.h> |
Stefano Babic | 831c4d5 | 2017-04-05 18:08:01 +0200 | [diff] [blame] | 9 | #include <uboot_aes.h> |
Tom Rini | cfd1e54 | 2012-12-20 07:30:27 -0700 | [diff] [blame] | 10 | |
Stefano Babic | 413cc6e | 2017-04-05 18:08:02 +0200 | [diff] [blame] | 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 | |
Andreas Fenkart | 2437190 | 2016-04-05 23:13:42 +0200 | [diff] [blame] | 18 | struct env_opts { |
Andreas Fenkart | aa01f8d | 2015-12-09 13:13:24 +0100 | [diff] [blame] | 19 | #ifdef CONFIG_FILE |
| 20 | char *config_file; |
| 21 | #endif |
Andreas Fenkart | aa01f8d | 2015-12-09 13:13:24 +0100 | [diff] [blame] | 22 | int aes_flag; /* Is AES encryption used? */ |
Andreas Fenkart | 2437190 | 2016-04-05 23:13:42 +0200 | [diff] [blame] | 23 | uint8_t aes_key[AES_KEY_LENGTH]; |
B, Ravi | ccb293f | 2016-09-26 18:24:08 +0530 | [diff] [blame] | 24 | char *lockname; |
Andreas Fenkart | 33e9177 | 2015-12-09 13:13:23 +0100 | [diff] [blame] | 25 | }; |
Andreas Fenkart | 33e9177 | 2015-12-09 13:13:23 +0100 | [diff] [blame] | 26 | |
Andreas Fenkart | aa01f8d | 2015-12-09 13:13:24 +0100 | [diff] [blame] | 27 | int parse_aes_key(char *key, uint8_t *bin_key); |
| 28 | |
Andreas Fenkart | c4a722c | 2016-07-16 17:06:13 +0200 | [diff] [blame] | 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 | */ |
Andreas Fenkart | 2437190 | 2016-04-05 23:13:42 +0200 | [diff] [blame] | 44 | int fw_printenv(int argc, char *argv[], int value_only, struct env_opts *opts); |
Andreas Fenkart | c4a722c | 2016-07-16 17:06:13 +0200 | [diff] [blame] | 45 | |
| 46 | /** |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 47 | * fw_env_set() - adds or removes one variable to the environment |
Andreas Fenkart | c4a722c | 2016-07-16 17:06:13 +0200 | [diff] [blame] | 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: |
Stefano Babic | 4b0a293 | 2017-04-05 18:08:03 +0200 | [diff] [blame] | 56 | * Uses fw_env_open, fw_env_write, fw_env_flush |
Andreas Fenkart | c4a722c | 2016-07-16 17:06:13 +0200 | [diff] [blame] | 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 | */ |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 64 | int fw_env_set(int argc, char *argv[], struct env_opts *opts); |
Andreas Fenkart | c4a722c | 2016-07-16 17:06:13 +0200 | [diff] [blame] | 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: |
Stefano Babic | 4b0a293 | 2017-04-05 18:08:03 +0200 | [diff] [blame] | 73 | * Uses fw_env_open, fw_env_write, fw_env_flush |
Andreas Fenkart | c4a722c | 2016-07-16 17:06:13 +0200 | [diff] [blame] | 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 | */ |
Andreas Fenkart | 2437190 | 2016-04-05 23:13:42 +0200 | [diff] [blame] | 101 | int fw_parse_script(char *fname, struct env_opts *opts); |
Andreas Fenkart | c4a722c | 2016-07-16 17:06:13 +0200 | [diff] [blame] | 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 | */ |
Andreas Fenkart | 2437190 | 2016-04-05 23:13:42 +0200 | [diff] [blame] | 112 | int fw_env_open(struct env_opts *opts); |
Andreas Fenkart | c4a722c | 2016-07-16 17:06:13 +0200 | [diff] [blame] | 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 | */ |
Andreas Fenkart | 576d08e | 2016-04-19 22:43:40 +0200 | [diff] [blame] | 138 | int fw_env_write(char *name, char *value); |
Andreas Fenkart | c4a722c | 2016-07-16 17:06:13 +0200 | [diff] [blame] | 139 | |
| 140 | /** |
Stefano Babic | 4b0a293 | 2017-04-05 18:08:03 +0200 | [diff] [blame] | 141 | * fw_env_flush - write the environment from RAM cache back to flash |
Andreas Fenkart | c4a722c | 2016-07-16 17:06:13 +0200 | [diff] [blame] | 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 | */ |
Stefano Babic | 4b0a293 | 2017-04-05 18:08:03 +0200 | [diff] [blame] | 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 | */ |
Andreas Fenkart | 2437190 | 2016-04-05 23:13:42 +0200 | [diff] [blame] | 158 | int fw_env_close(struct env_opts *opts); |
wdenk | 34b613e | 2002-12-17 01:51:00 +0000 | [diff] [blame] | 159 | |
Stefano Babic | 4b0a293 | 2017-04-05 18:08:03 +0200 | [diff] [blame] | 160 | |
Stefano Babic | 413cc6e | 2017-04-05 18:08:02 +0200 | [diff] [blame] | 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 | |
Andreas Fenkart | 576d08e | 2016-04-19 22:43:40 +0200 | [diff] [blame] | 169 | unsigned long crc32(unsigned long, const unsigned char *, unsigned); |