xref: /freebsd/stand/libsa/zfs/nvlist.h (revision e097436c)
1e097436cSMark Johnston /*-
2e097436cSMark Johnston  * Copyright 2020 Toomas Soome <tsoome@me.com>
3e097436cSMark Johnston  *
4e097436cSMark Johnston  * Redistribution and use in source and binary forms, with or without
5e097436cSMark Johnston  * modification, are permitted provided that the following conditions
6e097436cSMark Johnston  * are met:
7e097436cSMark Johnston  * 1. Redistributions of source code must retain the above copyright
8e097436cSMark Johnston  *    notice, this list of conditions and the following disclaimer.
9e097436cSMark Johnston  * 2. Redistributions in binary form must reproduce the above copyright
10e097436cSMark Johnston  *    notice, this list of conditions and the following disclaimer in the
11e097436cSMark Johnston  *    documentation and/or other materials provided with the distribution.
12e097436cSMark Johnston  *
13e097436cSMark Johnston  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14e097436cSMark Johnston  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15e097436cSMark Johnston  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16e097436cSMark Johnston  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17e097436cSMark Johnston  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18e097436cSMark Johnston  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19e097436cSMark Johnston  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20e097436cSMark Johnston  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21e097436cSMark Johnston  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22e097436cSMark Johnston  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23e097436cSMark Johnston  * SUCH DAMAGE.
24e097436cSMark Johnston  */
25e097436cSMark Johnston 
26e097436cSMark Johnston #ifndef _BOOT_NVLIST_H_
27e097436cSMark Johnston #define	_BOOT_NVLIST_H_
28e097436cSMark Johnston 
29e097436cSMark Johnston typedef enum {
30e097436cSMark Johnston 	DATA_TYPE_UNKNOWN = 0,
31e097436cSMark Johnston 	DATA_TYPE_BOOLEAN,
32e097436cSMark Johnston 	DATA_TYPE_BYTE,
33e097436cSMark Johnston 	DATA_TYPE_INT16,
34e097436cSMark Johnston 	DATA_TYPE_UINT16,
35e097436cSMark Johnston 	DATA_TYPE_INT32,
36e097436cSMark Johnston 	DATA_TYPE_UINT32,
37e097436cSMark Johnston 	DATA_TYPE_INT64,
38e097436cSMark Johnston 	DATA_TYPE_UINT64,
39e097436cSMark Johnston 	DATA_TYPE_STRING,
40e097436cSMark Johnston 	DATA_TYPE_BYTE_ARRAY,
41e097436cSMark Johnston 	DATA_TYPE_INT16_ARRAY,
42e097436cSMark Johnston 	DATA_TYPE_UINT16_ARRAY,
43e097436cSMark Johnston 	DATA_TYPE_INT32_ARRAY,
44e097436cSMark Johnston 	DATA_TYPE_UINT32_ARRAY,
45e097436cSMark Johnston 	DATA_TYPE_INT64_ARRAY,
46e097436cSMark Johnston 	DATA_TYPE_UINT64_ARRAY,
47e097436cSMark Johnston 	DATA_TYPE_STRING_ARRAY,
48e097436cSMark Johnston 	DATA_TYPE_HRTIME,
49e097436cSMark Johnston 	DATA_TYPE_NVLIST,
50e097436cSMark Johnston 	DATA_TYPE_NVLIST_ARRAY,
51e097436cSMark Johnston 	DATA_TYPE_BOOLEAN_VALUE,
52e097436cSMark Johnston 	DATA_TYPE_INT8,
53e097436cSMark Johnston 	DATA_TYPE_UINT8,
54e097436cSMark Johnston 	DATA_TYPE_BOOLEAN_ARRAY,
55e097436cSMark Johnston 	DATA_TYPE_INT8_ARRAY,
56e097436cSMark Johnston 	DATA_TYPE_UINT8_ARRAY
57e097436cSMark Johnston } data_type_t;
58e097436cSMark Johnston 
59e097436cSMark Johnston /* nvp implementation version */
60e097436cSMark Johnston #define	NV_VERSION		0
61e097436cSMark Johnston 
62e097436cSMark Johnston /* nvlist pack encoding */
63e097436cSMark Johnston #define	NV_ENCODE_NATIVE	0
64e097436cSMark Johnston #define	NV_ENCODE_XDR		1
65e097436cSMark Johnston 
66e097436cSMark Johnston /* nvlist persistent unique name flags, stored in nvl_nvflags */
67e097436cSMark Johnston #define	NV_UNIQUE_NAME		0x1
68e097436cSMark Johnston #define	NV_UNIQUE_NAME_TYPE	0x2
69e097436cSMark Johnston 
70e097436cSMark Johnston #define	NV_ALIGN4(x)		(((x) + 3) & ~3)
71e097436cSMark Johnston #define	NV_ALIGN(x)		(((x) + 7) & ~7)
72e097436cSMark Johnston 
73e097436cSMark Johnston /*
74e097436cSMark Johnston  * nvlist header.
75e097436cSMark Johnston  * nvlist has 4 bytes header followed by version and flags, then nvpairs
76e097436cSMark Johnston  * and the list is terminated by double zero.
77e097436cSMark Johnston  */
78e097436cSMark Johnston typedef struct {
79e097436cSMark Johnston 	char nvh_encoding;
80e097436cSMark Johnston 	char nvh_endian;
81e097436cSMark Johnston 	char nvh_reserved1;
82e097436cSMark Johnston 	char nvh_reserved2;
83e097436cSMark Johnston } nvs_header_t;
84e097436cSMark Johnston 
85e097436cSMark Johnston typedef struct {
86e097436cSMark Johnston 	nvs_header_t nv_header;
87e097436cSMark Johnston 	size_t nv_asize;
88e097436cSMark Johnston 	size_t nv_size;
89e097436cSMark Johnston 	uint8_t *nv_data;
90e097436cSMark Johnston 	uint8_t *nv_idx;
91e097436cSMark Johnston } nvlist_t;
92e097436cSMark Johnston 
93e097436cSMark Johnston /*
94e097436cSMark Johnston  * nvpair header.
95e097436cSMark Johnston  * nvpair has encoded and decoded size
96e097436cSMark Johnston  * name string (size and data)
97e097436cSMark Johnston  * data type and number of elements
98e097436cSMark Johnston  * data
99e097436cSMark Johnston  */
100e097436cSMark Johnston typedef struct {
101e097436cSMark Johnston 	unsigned encoded_size;
102e097436cSMark Johnston 	unsigned decoded_size;
103e097436cSMark Johnston } nvp_header_t;
104e097436cSMark Johnston 
105e097436cSMark Johnston /*
106e097436cSMark Johnston  * nvlist stream head.
107e097436cSMark Johnston  */
108e097436cSMark Johnston typedef struct {
109e097436cSMark Johnston 	unsigned nvl_version;
110e097436cSMark Johnston 	unsigned nvl_nvflag;
111e097436cSMark Johnston 	nvp_header_t nvl_pair;
112e097436cSMark Johnston } nvs_data_t;
113e097436cSMark Johnston 
114e097436cSMark Johnston typedef struct {
115e097436cSMark Johnston 	unsigned nv_size;
116e097436cSMark Johnston 	uint8_t nv_data[];	/* NV_ALIGN4(string) */
117e097436cSMark Johnston } nv_string_t;
118e097436cSMark Johnston 
119e097436cSMark Johnston typedef struct {
120e097436cSMark Johnston 	unsigned nv_type;	/* data_type_t */
121e097436cSMark Johnston 	unsigned nv_nelem;	/* number of elements */
122e097436cSMark Johnston 	uint8_t nv_data[];	/* data stream */
123e097436cSMark Johnston } nv_pair_data_t;
124e097436cSMark Johnston 
125e097436cSMark Johnston nvlist_t *nvlist_create(int);
126e097436cSMark Johnston void nvlist_destroy(nvlist_t *);
127e097436cSMark Johnston nvlist_t *nvlist_import(const char *, size_t);
128e097436cSMark Johnston int nvlist_export(nvlist_t *);
129e097436cSMark Johnston int nvlist_remove(nvlist_t *, const char *, data_type_t);
130e097436cSMark Johnston int nvpair_type_from_name(const char *);
131e097436cSMark Johnston nvp_header_t *nvpair_find(nvlist_t *, const char *);
132e097436cSMark Johnston void nvpair_print(nvp_header_t *, unsigned int);
133e097436cSMark Johnston void nvlist_print(const nvlist_t *, unsigned int);
134e097436cSMark Johnston char *nvstring_get(nv_string_t *);
135e097436cSMark Johnston int nvlist_find(const nvlist_t *, const char *, data_type_t,
136e097436cSMark Johnston     int *, void *, int *);
137e097436cSMark Johnston nvp_header_t *nvlist_next_nvpair(nvlist_t *, nvp_header_t *);
138e097436cSMark Johnston 
139e097436cSMark Johnston int nvlist_add_boolean_value(nvlist_t *, const char *, int);
140e097436cSMark Johnston int nvlist_add_byte(nvlist_t *, const char *, uint8_t);
141e097436cSMark Johnston int nvlist_add_int8(nvlist_t *, const char *, int8_t);
142e097436cSMark Johnston int nvlist_add_uint8(nvlist_t *, const char *, uint8_t);
143e097436cSMark Johnston int nvlist_add_int16(nvlist_t *, const char *, int16_t);
144e097436cSMark Johnston int nvlist_add_uint16(nvlist_t *, const char *, uint16_t);
145e097436cSMark Johnston int nvlist_add_int32(nvlist_t *, const char *, int32_t);
146e097436cSMark Johnston int nvlist_add_uint32(nvlist_t *, const char *, uint32_t);
147e097436cSMark Johnston int nvlist_add_int64(nvlist_t *, const char *, int64_t);
148e097436cSMark Johnston int nvlist_add_uint64(nvlist_t *, const char *, uint64_t);
149e097436cSMark Johnston int nvlist_add_string(nvlist_t *, const char *, const char *);
150e097436cSMark Johnston int nvlist_add_boolean_array(nvlist_t *, const char *, int *, uint32_t);
151e097436cSMark Johnston int nvlist_add_byte_array(nvlist_t *, const char *, uint8_t *, uint32_t);
152e097436cSMark Johnston int nvlist_add_int8_array(nvlist_t *, const char *, int8_t *, uint32_t);
153e097436cSMark Johnston int nvlist_add_uint8_array(nvlist_t *, const char *, uint8_t *, uint32_t);
154e097436cSMark Johnston int nvlist_add_int16_array(nvlist_t *, const char *, int16_t *, uint32_t);
155e097436cSMark Johnston int nvlist_add_uint16_array(nvlist_t *, const char *, uint16_t *, uint32_t);
156e097436cSMark Johnston int nvlist_add_int32_array(nvlist_t *, const char *, int32_t *, uint32_t);
157e097436cSMark Johnston int nvlist_add_uint32_array(nvlist_t *, const char *, uint32_t *, uint32_t);
158e097436cSMark Johnston int nvlist_add_int64_array(nvlist_t *, const char *, int64_t *, uint32_t);
159e097436cSMark Johnston int nvlist_add_uint64_array(nvlist_t *, const char *, uint64_t *, uint32_t);
160e097436cSMark Johnston int nvlist_add_string_array(nvlist_t *, const char *, char * const *, uint32_t);
161e097436cSMark Johnston int nvlist_add_nvlist(nvlist_t *, const char *, nvlist_t *);
162e097436cSMark Johnston int nvlist_add_nvlist_array(nvlist_t *, const char *, nvlist_t **, uint32_t);
163e097436cSMark Johnston 
164e097436cSMark Johnston #endif /* !_BOOT_NVLIST_H_ */
165