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