xref: /freebsd/stand/libsa/nvstore.c (revision 61ba55bcf70f2340f9c943c9571113b3fd8eda69)
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  * nvstore_init(): attach new backend and create the environment mapping.
38  * nvstore_fini: detach backend and unmap the related environment.
39  *
40  * The disk based storage, such as UFS file or ZFS bootenv label area, is
41  * only accessible after root file system is set. Root file system change
42  * will switch the back end storage.
43  */
44 
45 #include <sys/cdefs.h>
46 #include <stdbool.h>
47 #include <sys/queue.h>
48 #include "stand.h"
49 #include "nvstore.h"
50 
51 nvstore_list_t stores = STAILQ_HEAD_INITIALIZER(stores);
52 
53 void *
54 nvstore_get_store(const char *name)
55 {
56 	nvstore_t *st;
57 
58 	st = NULL;
59 
60 	STAILQ_FOREACH(st, &stores, nvs_next) {
61 		if (strcmp(name, st->nvs_name) == 0)
62 			break;
63 	}
64 
65 	return (st);
66 }
67 
68 int
69 nvstore_init(const char *name, nvs_callbacks_t *cb, void *data)
70 {
71 	nvstore_t *st;
72 
73 	st = nvstore_get_store(name);
74 	if (st != NULL)
75 		return (EEXIST);
76 
77 	if ((st = malloc(sizeof (*st))) == NULL)
78 		return (ENOMEM);
79 
80 	if ((st->nvs_name = strdup(name)) == NULL) {
81 		free(st);
82 		return (ENOMEM);
83 	}
84 
85 	st->nvs_data = data;
86 	st->nvs_cb = cb;
87 
88 	STAILQ_INSERT_TAIL(&stores, st, nvs_next);
89 	return (0);
90 }
91 
92 int
93 nvstore_fini(const char *name)
94 {
95 	nvstore_t *st;
96 
97 	st = nvstore_get_store(name);
98 	if (st == NULL)
99 		return (ENOENT);
100 
101 	STAILQ_REMOVE(&stores, st, nvstore, nvs_next);
102 
103 	free(st->nvs_name);
104 	free(st->nvs_data);
105 	free(st);
106 	return (0);
107 }
108 
109 int
110 nvstore_print(void *ptr)
111 {
112 	nvstore_t *st = ptr;
113 
114 	return (st->nvs_cb->nvs_iterate(st->nvs_data, st->nvs_cb->nvs_print));
115 }
116 
117 int
118 nvstore_get_var(void *ptr, const char *name, void **data)
119 {
120 	nvstore_t *st = ptr;
121 
122 	return (st->nvs_cb->nvs_getter(st->nvs_data, name, data));
123 }
124 
125 int
126 nvstore_set_var(void *ptr, int type, const char *name,
127     void *data, size_t size)
128 {
129 	nvstore_t *st = ptr;
130 
131 	return (st->nvs_cb->nvs_setter(st->nvs_data, type, name, data, size));
132 }
133 
134 int
135 nvstore_set_var_from_string(void *ptr, const char *type, const char *name,
136     const char *data)
137 {
138 	nvstore_t *st = ptr;
139 
140 	return (st->nvs_cb->nvs_setter_str(st->nvs_data, type, name, data));
141 }
142 
143 int
144 nvstore_unset_var(void *ptr, const char *name)
145 {
146 	nvstore_t *st = ptr;
147 
148 	return (st->nvs_cb->nvs_unset(st->nvs_data, name));
149 }
150