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