blob: 060eea542cc5d5f43b3a3094ac926fcb92ee5b24 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
wdenk34b613e2002-12-17 01:51:00 +00002/*
Grant Erickson73a4e5e2008-05-06 20:16:15 -07003 * (C) Copyright 2002-2008
wdenk34b613e2002-12-17 01:51:00 +00004 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
wdenk34b613e2002-12-17 01:51:00 +00005 */
6
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +01007#include <stdint.h>
Tom Rinicfd1e542012-12-20 07:30:27 -07008
Stefano Babic413cc6e2017-04-05 18:08:02 +02009/*
10 * Programs using the library must check which API is available,
11 * that varies depending on the U-Boot version.
12 * This can be changed in future
13 */
14#define FW_ENV_API_VERSION 1
15
Andreas Fenkart24371902016-04-05 23:13:42 +020016struct env_opts {
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +010017#ifdef CONFIG_FILE
18 char *config_file;
19#endif
B, Raviccb293f2016-09-26 18:24:08 +053020 char *lockname;
Andreas Fenkart33e91772015-12-09 13:13:23 +010021};
Andreas Fenkart33e91772015-12-09 13:13:23 +010022
Andreas Fenkartc4a722c2016-07-16 17:06:13 +020023/**
24 * fw_printenv() - print one or several environment variables
25 *
26 * @argc: number of variables names to be printed, prints all if 0
27 * @argv: array of variable names to be printed, if argc != 0
28 * @value_only: do not repeat the variable name, print the bare value,
29 * only one variable allowed with this option, argc must be 1
30 * @opts: encryption key, configuration file, defaults are used if NULL
31 *
32 * Description:
33 * Uses fw_env_open, fw_getenv
34 *
35 * Return:
36 * 0 on success, -1 on failure (modifies errno)
37 */
Andreas Fenkart24371902016-04-05 23:13:42 +020038int fw_printenv(int argc, char *argv[], int value_only, struct env_opts *opts);
Andreas Fenkartc4a722c2016-07-16 17:06:13 +020039
40/**
Simon Glass6a38e412017-08-03 12:22:09 -060041 * fw_env_set() - adds or removes one variable to the environment
Andreas Fenkartc4a722c2016-07-16 17:06:13 +020042 *
43 * @argc: number of strings in argv, argv[0] is variable name,
44 * argc==1 means erase variable, argc > 1 means add a variable
45 * @argv: argv[0] is variable name, argv[1..argc-1] are concatenated separated
46 * by single blank and set as the new value of the variable
47 * @opts: how to retrieve environment from flash, defaults are used if NULL
48 *
49 * Description:
Stefano Babic4b0a2932017-04-05 18:08:03 +020050 * Uses fw_env_open, fw_env_write, fw_env_flush
Andreas Fenkartc4a722c2016-07-16 17:06:13 +020051 *
52 * Return:
53 * 0 on success, -1 on failure (modifies errno)
54 *
55 * ERRORS:
56 * EROFS - some variables ("ethaddr", "serial#") cannot be modified
57 */
Simon Glass6a38e412017-08-03 12:22:09 -060058int fw_env_set(int argc, char *argv[], struct env_opts *opts);
Andreas Fenkartc4a722c2016-07-16 17:06:13 +020059
60/**
61 * fw_parse_script() - adds or removes multiple variables with a batch script
62 *
63 * @fname: batch script file name
64 * @opts: encryption key, configuration file, defaults are used if NULL
65 *
66 * Description:
Stefano Babic4b0a2932017-04-05 18:08:03 +020067 * Uses fw_env_open, fw_env_write, fw_env_flush
Andreas Fenkartc4a722c2016-07-16 17:06:13 +020068 *
69 * Return:
70 * 0 success, -1 on failure (modifies errno)
71 *
72 * Script Syntax:
73 *
74 * key [ [space]+ value]
75 *
76 * lines starting with '#' treated as comment
77 *
78 * A variable without value will be deleted. Any number of spaces are allowed
79 * between key and value. The value starts with the first non-space character
80 * and ends with newline. No comments allowed on these lines. Spaces inside
81 * the value are preserved verbatim.
82 *
83 * Script Example:
84 *
85 * netdev eth0
86 *
87 * kernel_addr 400000
88 *
89 * foo spaces are copied verbatim
90 *
91 * # delete variable bar
92 *
93 * bar
94 */
Andreas Fenkart24371902016-04-05 23:13:42 +020095int fw_parse_script(char *fname, struct env_opts *opts);
Andreas Fenkartc4a722c2016-07-16 17:06:13 +020096
Andreas Fenkartc4a722c2016-07-16 17:06:13 +020097/**
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 */
Andreas Fenkart24371902016-04-05 23:13:42 +0200105int fw_env_open(struct env_opts *opts);
Andreas Fenkartc4a722c2016-07-16 17:06:13 +0200106
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 */
114char *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 */
Andreas Fenkart576d08e2016-04-19 22:43:40 +0200131int fw_env_write(char *name, char *value);
Andreas Fenkartc4a722c2016-07-16 17:06:13 +0200132
133/**
Stefano Babic4b0a2932017-04-05 18:08:03 +0200134 * fw_env_flush - write the environment from RAM cache back to flash
Andreas Fenkartc4a722c2016-07-16 17:06:13 +0200135 *
136 * @opts: encryption key, configuration file, defaults are used if NULL
137 *
138 * Return:
139 * 0 on success, -1 on failure (modifies errno)
140 */
Stefano Babic4b0a2932017-04-05 18:08:03 +0200141int fw_env_flush(struct env_opts *opts);
142
143/**
144 * fw_env_close - free allocated structure and close env
145 *
146 * @opts: encryption key, configuration file, defaults are used if NULL
147 *
148 * Return:
149 * 0 on success, -1 on failure (modifies errno)
150 */
Andreas Fenkart24371902016-04-05 23:13:42 +0200151int fw_env_close(struct env_opts *opts);
wdenk34b613e2002-12-17 01:51:00 +0000152
Stefano Babic413cc6e2017-04-05 18:08:02 +0200153/**
154 * fw_env_version - return the current version of the library
155 *
156 * Return:
157 * version string of the library
158 */
159char *fw_env_version(void);