xref: /illumos-gate/usr/src/boot/common/nvstore.c (revision 22028508fd28d36ff74dc02c5774a8ba1f0db045)
1 /*
2  * Copyright 2020 Toomas Soome <tsoome@me.com>
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  */
25 
26 /*
27  * Big Theory Statement.
28  *
29  * nvstore is abstraction layer to implement data read/write to different
30  * types of non-volatile storage.
31  *
32  * User interfaces:
33  * Provide mapping via environment: setenv/unsetenv/putenv. Access via
34  * environment functions/commands is available once nvstore has
35  * attached the backend and stored textual data is mapped to environment.
36  *
37  * Provide command "nvstore" to create new data instances.
38  *
39  * API: TBD.
40  * nvstore_init(): attach new backend and create the environment mapping.
41  * nvstore_fini: detach backend and unmap the related environment.
42  *
43  * The disk based storage, such as UFS file or ZFS bootenv label area, is
44  * only accessible after root file system is set. Root file system change
45  * will switch the back end storage.
46  */
47 
48 #include <sys/cdefs.h>
49 
50 #include <stdbool.h>
51 #include <sys/queue.h>
52 #include <bootstrap.h>
53 #include "stand.h"
54 
55 typedef struct nvstore {
56 	char		*nvs_name;
57 	void		*nvs_data;
58 	nvs_callbacks_t	*nvs_cb;
59 	STAILQ_ENTRY(nvstore) nvs_next;
60 } nvstore_t;
61 
62 typedef STAILQ_HEAD(store_list, nvstore) nvstore_list_t;
63 
64 nvstore_list_t stores = STAILQ_HEAD_INITIALIZER(stores);
65 
66 void *
nvstore_get_store(const char * name)67 nvstore_get_store(const char *name)
68 {
69 	nvstore_t *st;
70 
71 	st = NULL;
72 
73 	STAILQ_FOREACH(st, &stores, nvs_next) {
74 		if (strcmp(name, st->nvs_name) == 0)
75 			break;
76 	}
77 
78 	return (st);
79 }
80 
81 int
nvstore_init(const char * name,nvs_callbacks_t * cb,void * data)82 nvstore_init(const char *name, nvs_callbacks_t *cb, void *data)
83 {
84 	nvstore_t *st;
85 
86 	st = nvstore_get_store(name);
87 	if (st != NULL)
88 		return (EEXIST);
89 
90 	if ((st = malloc(sizeof (*st))) == NULL)
91 		return (ENOMEM);
92 
93 	if ((st->nvs_name = strdup(name)) == NULL) {
94 		free(st);
95 		return (ENOMEM);
96 	}
97 
98 	st->nvs_data = data;
99 	st->nvs_cb = cb;
100 
101 	STAILQ_INSERT_TAIL(&stores, st, nvs_next);
102 	return (0);
103 }
104 
105 int
nvstore_fini(const char * name)106 nvstore_fini(const char *name)
107 {
108 	nvstore_t *st;
109 
110 	st = nvstore_get_store(name);
111 	if (st == NULL)
112 		return (ENOENT);
113 
114 	STAILQ_REMOVE(&stores, st, nvstore, nvs_next);
115 
116 	free(st->nvs_name);
117 	free(st->nvs_data);
118 	free(st);
119 	return (0);
120 }
121 
122 int
nvstore_print(void * ptr)123 nvstore_print(void *ptr)
124 {
125 	nvstore_t *st = ptr;
126 
127 	return (st->nvs_cb->nvs_iterate(st->nvs_data, st->nvs_cb->nvs_print));
128 }
129 
130 int
nvstore_get_var(void * ptr,const char * name,void ** data)131 nvstore_get_var(void *ptr, const char *name, void **data)
132 {
133 	nvstore_t *st = ptr;
134 
135 	return (st->nvs_cb->nvs_getter(st->nvs_data, name, data));
136 }
137 
138 int
nvstore_set_var(void * ptr,int type,const char * name,void * data,size_t size)139 nvstore_set_var(void *ptr, int type, const char *name,
140     void *data, size_t size)
141 {
142 	nvstore_t *st = ptr;
143 
144 	return (st->nvs_cb->nvs_setter(st->nvs_data, type, name, data, size));
145 }
146 
147 int
nvstore_set_var_from_string(void * ptr,const char * type,const char * name,const char * data)148 nvstore_set_var_from_string(void *ptr, const char *type, const char *name,
149     const char *data)
150 {
151 	nvstore_t *st = ptr;
152 
153 	return (st->nvs_cb->nvs_setter_str(st->nvs_data, type, name, data));
154 }
155 
156 int
nvstore_unset_var(void * ptr,const char * name)157 nvstore_unset_var(void *ptr, const char *name)
158 {
159 	nvstore_t *st = ptr;
160 
161 	return (st->nvs_cb->nvs_unset(st->nvs_data, name));
162 }
163 
164 COMMAND_SET(nvstore, "nvstore", "manage non-volatile data", command_nvstore);
165 
166 static void
nvstore_usage(const char * me)167 nvstore_usage(const char *me)
168 {
169 	printf("Usage:\t%s -l\n", me);
170 	printf("\t%s store -l\n", me);
171 	printf("\t%s store [-t type] key value\n", me);
172 	printf("\t%s store -g key\n", me);
173 	printf("\t%s store -d key\n", me);
174 }
175 
176 /*
177  * Usage: nvstore -l		# list stores
178  *	nvstore store -l	# list data in store
179  *	nvstore store [-t type] key value
180  *	nvstore store -g key	# get value
181  *	nvstore store -d key	# delete key
182  */
183 static int
command_nvstore(int argc,char * argv[])184 command_nvstore(int argc, char *argv[])
185 {
186 	int c;
187 	bool list, get, delete;
188 	nvstore_t *st;
189 	char *me, *name, *type;
190 
191 	me = argv[0];
192 	optind = 1;
193 	optreset = 1;
194 
195 	list = false;
196 	while ((c = getopt(argc, argv, "l")) != -1) {
197 		switch (c) {
198 		case 'l':
199 			list = true;
200 			break;
201 		case '?':
202 		default:
203 			return (CMD_ERROR);
204 		}
205 	}
206 
207 	argc -= optind;
208 	argv += optind;
209 
210 	if (argc == 0) {
211 		if (list) {
212 			if (STAILQ_EMPTY(&stores)) {
213 				printf("No configured nvstores\n");
214 				return (CMD_OK);
215 			}
216 			printf("List of configured nvstores:\n");
217 			STAILQ_FOREACH(st, &stores, nvs_next) {
218 				printf("\t%s\n", st->nvs_name);
219 			}
220 			return (CMD_OK);
221 		}
222 		nvstore_usage(me);
223 		return (CMD_ERROR);
224 	}
225 
226 	if (argc == 0 || (argc != 0 && list)) {
227 		nvstore_usage(me);
228 		return (CMD_ERROR);
229 	}
230 
231 	st = nvstore_get_store(argv[0]);
232 	if (st == NULL) {
233 		nvstore_usage(me);
234 		return (CMD_ERROR);
235 	}
236 
237 	optind = 1;
238 	optreset = 1;
239 	name = NULL;
240 	type = NULL;
241 	get = delete = false;
242 
243 	while ((c = getopt(argc, argv, "d:g:lt:")) != -1) {
244 		switch (c) {
245 		case 'd':
246 			if (list || get) {
247 				nvstore_usage(me);
248 				return (CMD_ERROR);
249 			}
250 			name = optarg;
251 			delete = true;
252 			break;
253 		case 'g':
254 			if (delete || list) {
255 				nvstore_usage(me);
256 				return (CMD_ERROR);
257 			}
258 			name = optarg;
259 			get = true;
260 			break;
261 		case 'l':
262 			if (delete || get) {
263 				nvstore_usage(me);
264 				return (CMD_ERROR);
265 			}
266 			list = true;
267 			break;
268 		case 't':
269 			type = optarg;
270 			break;
271 		case '?':
272 		default:
273 			return (CMD_ERROR);
274 		}
275 	}
276 
277 	argc -= optind;
278 	argv += optind;
279 
280 	if (list) {
281 		(void) nvstore_print(st);
282 		return (CMD_OK);
283 	}
284 
285 	if (delete && name != NULL) {
286 		(void) nvstore_unset_var(st, name);
287 		return (CMD_OK);
288 	}
289 
290 	if (get && name != NULL) {
291 		char *ptr = NULL;
292 
293 		if (nvstore_get_var(st, name, (void **)&ptr) == 0)
294 			printf("%s = %s\n", name, ptr);
295 		return (CMD_OK);
296 	}
297 
298 	if (argc == 2) {
299 		c = nvstore_set_var_from_string(st, type, argv[0], argv[1]);
300 		if (c != 0) {
301 			printf("error: %s\n", strerror(c));
302 			return (CMD_ERROR);
303 		}
304 		return (CMD_OK);
305 	}
306 
307 	nvstore_usage(me);
308 	return (CMD_OK);
309 }
310