blob: 04bb64602b2561dfbb9982faa128eb2327296407 [file] [log] [blame]
wdenk34b613e2002-12-17 01:51:00 +00001/*
Grant Erickson73a4e5e2008-05-06 20:16:15 -07002 * (C) Copyright 2002-2008
wdenk34b613e2002-12-17 01:51:00 +00003 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
Wolfgang Denkbd8ec7e2013-10-07 13:07:26 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenk34b613e2002-12-17 01:51:00 +00006 */
7
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +01008#include <stdint.h>
Stefano Babic831c4d52017-04-05 18:08:01 +02009#include <uboot_aes.h>
Tom Rinicfd1e542012-12-20 07:30:27 -070010
Stefano Babic413cc6e2017-04-05 18:08:02 +020011/*
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 Fenkart24371902016-04-05 23:13:42 +020018struct env_opts {
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +010019#ifdef CONFIG_FILE
20 char *config_file;
21#endif
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +010022 int aes_flag; /* Is AES encryption used? */
Andreas Fenkart24371902016-04-05 23:13:42 +020023 uint8_t aes_key[AES_KEY_LENGTH];
B, Raviccb293f2016-09-26 18:24:08 +053024 char *lockname;
Andreas Fenkart33e91772015-12-09 13:13:23 +010025};
Andreas Fenkart33e91772015-12-09 13:13:23 +010026
Andreas Fenkartaa01f8d2015-12-09 13:13:24 +010027int parse_aes_key(char *key, uint8_t *bin_key);
28
Andreas Fenkartc4a722c2016-07-16 17:06:13 +020029/**
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 Fenkart24371902016-04-05 23:13:42 +020044int fw_printenv(int argc, char *argv[], int value_only, struct env_opts *opts);
Andreas Fenkartc4a722c2016-07-16 17:06:13 +020045
46/**
47 * fw_setenv() - adds or removes one variable to the environment
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 Babic4b0a2932017-04-05 18:08:03 +020056 * Uses fw_env_open, fw_env_write, fw_env_flush
Andreas Fenkartc4a722c2016-07-16 17:06:13 +020057 *
58 * Return:
59 * 0 on success, -1 on failure (modifies errno)
60 *
61 * ERRORS:
62 * EROFS - some variables ("ethaddr", "serial#") cannot be modified
63 */
Andreas Fenkart24371902016-04-05 23:13:42 +020064int fw_setenv(int argc, char *argv[], struct env_opts *opts);
Andreas Fenkartc4a722c2016-07-16 17:06:13 +020065
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 Babic4b0a2932017-04-05 18:08:03 +020073 * Uses fw_env_open, fw_env_write, fw_env_flush
Andreas Fenkartc4a722c2016-07-16 17:06:13 +020074 *
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 Fenkart24371902016-04-05 23:13:42 +0200101int fw_parse_script(char *fname, struct env_opts *opts);
Andreas Fenkartc4a722c2016-07-16 17:06:13 +0200102
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 Fenkart24371902016-04-05 23:13:42 +0200112int fw_env_open(struct env_opts *opts);
Andreas Fenkartc4a722c2016-07-16 17:06:13 +0200113
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 */
121char *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 Fenkart576d08e2016-04-19 22:43:40 +0200138int fw_env_write(char *name, char *value);
Andreas Fenkartc4a722c2016-07-16 17:06:13 +0200139
140/**
Stefano Babic4b0a2932017-04-05 18:08:03 +0200141 * fw_env_flush - write the environment from RAM cache back to flash
Andreas Fenkartc4a722c2016-07-16 17:06:13 +0200142 *
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 Babic4b0a2932017-04-05 18:08:03 +0200148int 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 Fenkart24371902016-04-05 23:13:42 +0200158int fw_env_close(struct env_opts *opts);
wdenk34b613e2002-12-17 01:51:00 +0000159
Stefano Babic4b0a2932017-04-05 18:08:03 +0200160
Stefano Babic413cc6e2017-04-05 18:08:02 +0200161/**
162 * fw_env_version - return the current version of the library
163 *
164 * Return:
165 * version string of the library
166 */
167char *fw_env_version(void);
168
Andreas Fenkart576d08e2016-04-19 22:43:40 +0200169unsigned long crc32(unsigned long, const unsigned char *, unsigned);