blob: 04bb64602b2561dfbb9982faa128eb2327296407 [file] [log] [blame]
wdenk6aff3112002-12-17 01:51:00 +00001/*
Grant Ericksonbc117562008-05-06 20:16:15 -07002 * (C) Copyright 2002-2008
wdenk6aff3112002-12-17 01:51:00 +00003 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
Wolfgang Denk3765b3e2013-10-07 13:07:26 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenk6aff3112002-12-17 01:51:00 +00006 */
7
Andreas Fenkart371ee132015-12-09 13:13:24 +01008#include <stdint.h>
Stefano Babic9d80b492017-04-05 18:08:01 +02009#include <uboot_aes.h>
Tom Rinie3c52f22012-12-20 07:30:27 -070010
Stefano Babic00c234f2017-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 Fenkart81974f42016-04-05 23:13:42 +020018struct env_opts {
Andreas Fenkart371ee132015-12-09 13:13:24 +010019#ifdef CONFIG_FILE
20 char *config_file;
21#endif
Andreas Fenkart371ee132015-12-09 13:13:24 +010022 int aes_flag; /* Is AES encryption used? */
Andreas Fenkart81974f42016-04-05 23:13:42 +020023 uint8_t aes_key[AES_KEY_LENGTH];
B, Ravid40dbfb2016-09-26 18:24:08 +053024 char *lockname;
Andreas Fenkart371ee132015-12-09 13:13:24 +010025};
Andreas Fenkart07ce9442015-12-09 13:13:23 +010026
Andreas Fenkart371ee132015-12-09 13:13:24 +010027int parse_aes_key(char *key, uint8_t *bin_key);
28
Andreas Fenkartfd4e3282016-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 Fenkart81974f42016-04-05 23:13:42 +020044int fw_printenv(int argc, char *argv[], int value_only, struct env_opts *opts);
Andreas Fenkartfd4e3282016-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 Babic33f00862017-04-05 18:08:03 +020056 * Uses fw_env_open, fw_env_write, fw_env_flush
Andreas Fenkartfd4e3282016-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 Fenkart81974f42016-04-05 23:13:42 +020064int fw_setenv(int argc, char *argv[], struct env_opts *opts);
Andreas Fenkartfd4e3282016-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 Babic33f00862017-04-05 18:08:03 +020073 * Uses fw_env_open, fw_env_write, fw_env_flush
Andreas Fenkartfd4e3282016-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 Fenkart81974f42016-04-05 23:13:42 +0200101int fw_parse_script(char *fname, struct env_opts *opts);
Andreas Fenkartfd4e3282016-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 Fenkart81974f42016-04-05 23:13:42 +0200112int fw_env_open(struct env_opts *opts);
Andreas Fenkartfd4e3282016-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 Fenkartc3a23e82016-04-19 22:43:40 +0200138int fw_env_write(char *name, char *value);
Andreas Fenkartfd4e3282016-07-16 17:06:13 +0200139
140/**
Stefano Babic33f00862017-04-05 18:08:03 +0200141 * fw_env_flush - write the environment from RAM cache back to flash
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 */
148int fw_env_flush(struct env_opts *opts);
149
150/**
151 * fw_env_close - free allocated structure and close env
Andreas Fenkartfd4e3282016-07-16 17:06:13 +0200152 *
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 Fenkart81974f42016-04-05 23:13:42 +0200158int fw_env_close(struct env_opts *opts);
wdenk6aff3112002-12-17 01:51:00 +0000159
Stefano Babic33f00862017-04-05 18:08:03 +0200160
Stefano Babic00c234f2017-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 Fenkartc3a23e82016-04-19 22:43:40 +0200169unsigned long crc32(unsigned long, const unsigned char *, unsigned);