xref: /freebsd/stand/common/nvstore.c (revision d1ea5017141ffecdb7ce97a3d97b27a6aabf8c4a)
1e307eb94SToomas Soome /*-
2e307eb94SToomas Soome  * Copyright 2020 Toomas Soome <tsoome@me.com>
3e307eb94SToomas Soome  *
4e307eb94SToomas Soome  * Redistribution and use in source and binary forms, with or without
5e307eb94SToomas Soome  * modification, are permitted provided that the following conditions
6e307eb94SToomas Soome  * are met:
7e307eb94SToomas Soome  * 1. Redistributions of source code must retain the above copyright
8e307eb94SToomas Soome  *    notice, this list of conditions and the following disclaimer.
9e307eb94SToomas Soome  * 2. Redistributions in binary form must reproduce the above copyright
10e307eb94SToomas Soome  *    notice, this list of conditions and the following disclaimer in the
11e307eb94SToomas Soome  *    documentation and/or other materials provided with the distribution.
12e307eb94SToomas Soome  *
13e307eb94SToomas Soome  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14e307eb94SToomas Soome  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15e307eb94SToomas Soome  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16e307eb94SToomas Soome  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17e307eb94SToomas Soome  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18e307eb94SToomas Soome  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19e307eb94SToomas Soome  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20e307eb94SToomas Soome  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21e307eb94SToomas Soome  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22e307eb94SToomas Soome  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23e307eb94SToomas Soome  * SUCH DAMAGE.
24e307eb94SToomas Soome  */
25e307eb94SToomas Soome 
26e307eb94SToomas Soome /*
27e307eb94SToomas Soome  * Big Theory Statement.
28e307eb94SToomas Soome  *
29e307eb94SToomas Soome  * nvstore is abstraction layer to implement data read/write to different
30e307eb94SToomas Soome  * types of non-volatile storage.
31e307eb94SToomas Soome  *
32*d1ea5017SWarner Losh  * Provides cli command 'nvostre'
33e307eb94SToomas Soome  */
34e307eb94SToomas Soome 
35e307eb94SToomas Soome #include "stand.h"
36*d1ea5017SWarner Losh #include "nvstore.h"
37*d1ea5017SWarner Losh #include "bootstrap.h"
38e307eb94SToomas Soome 
39e307eb94SToomas Soome COMMAND_SET(nvstore, "nvstore", "manage non-volatile data", command_nvstore);
40e307eb94SToomas Soome 
41e307eb94SToomas Soome static void
nvstore_usage(const char * me)42e307eb94SToomas Soome nvstore_usage(const char *me)
43e307eb94SToomas Soome {
44e307eb94SToomas Soome 	printf("Usage:\t%s -l\n", me);
45e307eb94SToomas Soome 	printf("\t%s store -l\n", me);
46e307eb94SToomas Soome 	printf("\t%s store [-t type] key value\n", me);
47e307eb94SToomas Soome 	printf("\t%s store -g key\n", me);
48e307eb94SToomas Soome 	printf("\t%s store -d key\n", me);
49e307eb94SToomas Soome }
50e307eb94SToomas Soome 
51e307eb94SToomas Soome /*
52e307eb94SToomas Soome  * Usage: nvstore -l		# list stores
53e307eb94SToomas Soome  *	nvstore store -l	# list data in store
54e307eb94SToomas Soome  *	nvstore store [-t type] key value
55e307eb94SToomas Soome  *	nvstore store -g key	# get value
56e307eb94SToomas Soome  *	nvstore store -d key	# delete key
57e307eb94SToomas Soome  */
58e307eb94SToomas Soome static int
command_nvstore(int argc,char * argv[])59e307eb94SToomas Soome command_nvstore(int argc, char *argv[])
60e307eb94SToomas Soome {
61e307eb94SToomas Soome 	int c;
62e307eb94SToomas Soome 	bool list, get, delete;
63e307eb94SToomas Soome 	nvstore_t *st;
64e307eb94SToomas Soome 	char *me, *name, *type;
65e307eb94SToomas Soome 
66e307eb94SToomas Soome 	me = argv[0];
67e307eb94SToomas Soome 	optind = 1;
68e307eb94SToomas Soome 	optreset = 1;
69e307eb94SToomas Soome 
70e307eb94SToomas Soome 	list = false;
71e307eb94SToomas Soome 	while ((c = getopt(argc, argv, "l")) != -1) {
72e307eb94SToomas Soome 		switch (c) {
73e307eb94SToomas Soome 		case 'l':
74e307eb94SToomas Soome 			list = true;
75e307eb94SToomas Soome 			break;
76e307eb94SToomas Soome 		case '?':
77e307eb94SToomas Soome 		default:
78e307eb94SToomas Soome 			return (CMD_ERROR);
79e307eb94SToomas Soome 		}
80e307eb94SToomas Soome 	}
81e307eb94SToomas Soome 
82e307eb94SToomas Soome 	argc -= optind;
83e307eb94SToomas Soome 	argv += optind;
84e307eb94SToomas Soome 
85e307eb94SToomas Soome 	if (argc == 0) {
86e307eb94SToomas Soome 		if (list) {
87e307eb94SToomas Soome 			if (STAILQ_EMPTY(&stores)) {
88e307eb94SToomas Soome 				printf("No configured nvstores\n");
89e307eb94SToomas Soome 				return (CMD_OK);
90e307eb94SToomas Soome 			}
91e307eb94SToomas Soome 			printf("List of configured nvstores:\n");
92e307eb94SToomas Soome 			STAILQ_FOREACH(st, &stores, nvs_next) {
93e307eb94SToomas Soome 				printf("\t%s\n", st->nvs_name);
94e307eb94SToomas Soome 			}
95e307eb94SToomas Soome 			return (CMD_OK);
96e307eb94SToomas Soome 		}
97e307eb94SToomas Soome 		nvstore_usage(me);
98e307eb94SToomas Soome 		return (CMD_ERROR);
99e307eb94SToomas Soome 	}
100e307eb94SToomas Soome 
101e307eb94SToomas Soome 	if (argc == 0 || (argc != 0 && list)) {
102e307eb94SToomas Soome 		nvstore_usage(me);
103e307eb94SToomas Soome 		return (CMD_ERROR);
104e307eb94SToomas Soome 	}
105e307eb94SToomas Soome 
106e307eb94SToomas Soome 	st = nvstore_get_store(argv[0]);
107e307eb94SToomas Soome 	if (st == NULL) {
108e307eb94SToomas Soome 		nvstore_usage(me);
109e307eb94SToomas Soome 		return (CMD_ERROR);
110e307eb94SToomas Soome 	}
111e307eb94SToomas Soome 
112e307eb94SToomas Soome 	optind = 1;
113e307eb94SToomas Soome 	optreset = 1;
114e307eb94SToomas Soome 	name = NULL;
115e307eb94SToomas Soome 	type = NULL;
116e307eb94SToomas Soome 	get = delete = false;
117e307eb94SToomas Soome 
118e307eb94SToomas Soome 	while ((c = getopt(argc, argv, "d:g:lt:")) != -1) {
119e307eb94SToomas Soome 		switch (c) {
120e307eb94SToomas Soome 		case 'd':
121e307eb94SToomas Soome 			if (list || get) {
122e307eb94SToomas Soome 				nvstore_usage(me);
123e307eb94SToomas Soome 				return (CMD_ERROR);
124e307eb94SToomas Soome 			}
125e307eb94SToomas Soome 			name = optarg;
126e307eb94SToomas Soome 			delete = true;
127e307eb94SToomas Soome 			break;
128e307eb94SToomas Soome 		case 'g':
129e307eb94SToomas Soome 			if (delete || list) {
130e307eb94SToomas Soome 				nvstore_usage(me);
131e307eb94SToomas Soome 				return (CMD_ERROR);
132e307eb94SToomas Soome 			}
133e307eb94SToomas Soome 			name = optarg;
134e307eb94SToomas Soome 			get = true;
135e307eb94SToomas Soome 			break;
136e307eb94SToomas Soome 		case 'l':
137e307eb94SToomas Soome 			if (delete || get) {
138e307eb94SToomas Soome 				nvstore_usage(me);
139e307eb94SToomas Soome 				return (CMD_ERROR);
140e307eb94SToomas Soome 			}
141e307eb94SToomas Soome 			list = true;
142e307eb94SToomas Soome 			break;
143e307eb94SToomas Soome 		case 't':
144e307eb94SToomas Soome 			type = optarg;
145e307eb94SToomas Soome 			break;
146e307eb94SToomas Soome 		case '?':
147e307eb94SToomas Soome 		default:
148e307eb94SToomas Soome 			return (CMD_ERROR);
149e307eb94SToomas Soome 		}
150e307eb94SToomas Soome 	}
151e307eb94SToomas Soome 
152e307eb94SToomas Soome 	argc -= optind;
153e307eb94SToomas Soome 	argv += optind;
154e307eb94SToomas Soome 
155e307eb94SToomas Soome 	if (list) {
156e307eb94SToomas Soome 		(void) nvstore_print(st);
157e307eb94SToomas Soome 		return (CMD_OK);
158e307eb94SToomas Soome 	}
159e307eb94SToomas Soome 
160e307eb94SToomas Soome 	if (delete && name != NULL) {
161e307eb94SToomas Soome 		(void) nvstore_unset_var(st, name);
162e307eb94SToomas Soome 		return (CMD_OK);
163e307eb94SToomas Soome 	}
164e307eb94SToomas Soome 
165e307eb94SToomas Soome 	if (get && name != NULL) {
166e307eb94SToomas Soome 		char *ptr = NULL;
167e307eb94SToomas Soome 
168e307eb94SToomas Soome 		if (nvstore_get_var(st, name, (void **)&ptr) == 0)
169e307eb94SToomas Soome 			printf("%s = %s\n", name, ptr);
170e307eb94SToomas Soome 		return (CMD_OK);
171e307eb94SToomas Soome 	}
172e307eb94SToomas Soome 
173e307eb94SToomas Soome 	if (argc == 2) {
174e307eb94SToomas Soome 		c = nvstore_set_var_from_string(st, type, argv[0], argv[1]);
175e307eb94SToomas Soome 		if (c != 0) {
176e307eb94SToomas Soome 			printf("error: %s\n", strerror(c));
177e307eb94SToomas Soome 			return (CMD_ERROR);
178e307eb94SToomas Soome 		}
179e307eb94SToomas Soome 		return (CMD_OK);
180e307eb94SToomas Soome 	}
181e307eb94SToomas Soome 
182e307eb94SToomas Soome 	nvstore_usage(me);
183e307eb94SToomas Soome 	return (CMD_OK);
184e307eb94SToomas Soome }
185