1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_XBC_H
3 #define _LINUX_XBC_H
4 /*
5  * Extra Boot Config
6  * Copyright (C) 2019 Linaro Ltd.
7  * Author: Masami Hiramatsu <mhiramat@kernel.org>
8  */
9 
10 #include <linux/kernel.h>
11 #include <linux/types.h>
12 
13 #define BOOTCONFIG_MAGIC	"#BOOTCONFIG\n"
14 #define BOOTCONFIG_MAGIC_LEN	12
15 #define BOOTCONFIG_ALIGN_SHIFT	2
16 #define BOOTCONFIG_ALIGN	(1 << BOOTCONFIG_ALIGN_SHIFT)
17 #define BOOTCONFIG_ALIGN_MASK	(BOOTCONFIG_ALIGN - 1)
18 
19 /* XBC tree node */
20 struct xbc_node {
21 	u16 next;
22 	u16 child;
23 	u16 parent;
24 	u16 data;
25 } __attribute__ ((__packed__));
26 
27 #define XBC_KEY		0
28 #define XBC_VALUE	(1 << 15)
29 /* Maximum size of boot config is 32KB - 1 */
30 #define XBC_DATA_MAX	(XBC_VALUE - 1)
31 
32 #define XBC_NODE_MAX	1024
33 #define XBC_KEYLEN_MAX	256
34 #define XBC_DEPTH_MAX	16
35 
36 /* Node tree access raw APIs */
37 struct xbc_node * __init xbc_root_node(void);
38 int __init xbc_node_index(struct xbc_node *node);
39 struct xbc_node * __init xbc_node_get_parent(struct xbc_node *node);
40 struct xbc_node * __init xbc_node_get_child(struct xbc_node *node);
41 struct xbc_node * __init xbc_node_get_next(struct xbc_node *node);
42 const char * __init xbc_node_get_data(struct xbc_node *node);
43 
44 /**
45  * xbc_node_is_value() - Test the node is a value node
46  * @node: An XBC node.
47  *
48  * Test the @node is a value node and return true if a value node, false if not.
49  */
xbc_node_is_value(struct xbc_node * node)50 static inline __init bool xbc_node_is_value(struct xbc_node *node)
51 {
52 	return node->data & XBC_VALUE;
53 }
54 
55 /**
56  * xbc_node_is_key() - Test the node is a key node
57  * @node: An XBC node.
58  *
59  * Test the @node is a key node and return true if a key node, false if not.
60  */
xbc_node_is_key(struct xbc_node * node)61 static inline __init bool xbc_node_is_key(struct xbc_node *node)
62 {
63 	return !xbc_node_is_value(node);
64 }
65 
66 /**
67  * xbc_node_is_array() - Test the node is an arraied value node
68  * @node: An XBC node.
69  *
70  * Test the @node is an arraied value node.
71  */
xbc_node_is_array(struct xbc_node * node)72 static inline __init bool xbc_node_is_array(struct xbc_node *node)
73 {
74 	return xbc_node_is_value(node) && node->next != 0;
75 }
76 
77 /**
78  * xbc_node_is_leaf() - Test the node is a leaf key node
79  * @node: An XBC node.
80  *
81  * Test the @node is a leaf key node which is a key node and has a value node
82  * or no child. Returns true if it is a leaf node, or false if not.
83  */
xbc_node_is_leaf(struct xbc_node * node)84 static inline __init bool xbc_node_is_leaf(struct xbc_node *node)
85 {
86 	return xbc_node_is_key(node) &&
87 		(!node->child || xbc_node_is_value(xbc_node_get_child(node)));
88 }
89 
90 /* Tree-based key-value access APIs */
91 struct xbc_node * __init xbc_node_find_child(struct xbc_node *parent,
92 					     const char *key);
93 
94 const char * __init xbc_node_find_value(struct xbc_node *parent,
95 					const char *key,
96 					struct xbc_node **vnode);
97 
98 struct xbc_node * __init xbc_node_find_next_leaf(struct xbc_node *root,
99 						 struct xbc_node *leaf);
100 
101 const char * __init xbc_node_find_next_key_value(struct xbc_node *root,
102 						 struct xbc_node **leaf);
103 
104 /**
105  * xbc_find_value() - Find a value which matches the key
106  * @key: Search key
107  * @vnode: A container pointer of XBC value node.
108  *
109  * Search a value whose key matches @key from whole of XBC tree and return
110  * the value if found. Found value node is stored in *@vnode.
111  * Note that this can return 0-length string and store NULL in *@vnode for
112  * key-only (non-value) entry.
113  */
114 static inline const char * __init
xbc_find_value(const char * key,struct xbc_node ** vnode)115 xbc_find_value(const char *key, struct xbc_node **vnode)
116 {
117 	return xbc_node_find_value(NULL, key, vnode);
118 }
119 
120 /**
121  * xbc_find_node() - Find a node which matches the key
122  * @key: Search key
123  *
124  * Search a (key) node whose key matches @key from whole of XBC tree and
125  * return the node if found. If not found, returns NULL.
126  */
xbc_find_node(const char * key)127 static inline struct xbc_node * __init xbc_find_node(const char *key)
128 {
129 	return xbc_node_find_child(NULL, key);
130 }
131 
132 /**
133  * xbc_array_for_each_value() - Iterate value nodes on an array
134  * @anode: An XBC arraied value node
135  * @value: A value
136  *
137  * Iterate array value nodes and values starts from @anode. This is expected to
138  * be used with xbc_find_value() and xbc_node_find_value(), so that user can
139  * process each array entry node.
140  */
141 #define xbc_array_for_each_value(anode, value)				\
142 	for (value = xbc_node_get_data(anode); anode != NULL ;		\
143 	     anode = xbc_node_get_next(anode),				\
144 	     value = anode ? xbc_node_get_data(anode) : NULL)
145 
146 /**
147  * xbc_node_for_each_child() - Iterate child nodes
148  * @parent: An XBC node.
149  * @child: Iterated XBC node.
150  *
151  * Iterate child nodes of @parent. Each child nodes are stored to @child.
152  */
153 #define xbc_node_for_each_child(parent, child)				\
154 	for (child = xbc_node_get_child(parent); child != NULL ;	\
155 	     child = xbc_node_get_next(child))
156 
157 /**
158  * xbc_node_for_each_array_value() - Iterate array entries of geven key
159  * @node: An XBC node.
160  * @key: A key string searched under @node
161  * @anode: Iterated XBC node of array entry.
162  * @value: Iterated value of array entry.
163  *
164  * Iterate array entries of given @key under @node. Each array entry node
165  * is stroed to @anode and @value. If the @node doesn't have @key node,
166  * it does nothing.
167  * Note that even if the found key node has only one value (not array)
168  * this executes block once. Hoever, if the found key node has no value
169  * (key-only node), this does nothing. So don't use this for testing the
170  * key-value pair existence.
171  */
172 #define xbc_node_for_each_array_value(node, key, anode, value)		\
173 	for (value = xbc_node_find_value(node, key, &anode); value != NULL; \
174 	     anode = xbc_node_get_next(anode),				\
175 	     value = anode ? xbc_node_get_data(anode) : NULL)
176 
177 /**
178  * xbc_node_for_each_key_value() - Iterate key-value pairs under a node
179  * @node: An XBC node.
180  * @knode: Iterated key node
181  * @value: Iterated value string
182  *
183  * Iterate key-value pairs under @node. Each key node and value string are
184  * stored in @knode and @value respectively.
185  */
186 #define xbc_node_for_each_key_value(node, knode, value)			\
187 	for (knode = NULL, value = xbc_node_find_next_key_value(node, &knode);\
188 	     knode != NULL; value = xbc_node_find_next_key_value(node, &knode))
189 
190 /**
191  * xbc_for_each_key_value() - Iterate key-value pairs
192  * @knode: Iterated key node
193  * @value: Iterated value string
194  *
195  * Iterate key-value pairs in whole XBC tree. Each key node and value string
196  * are stored in @knode and @value respectively.
197  */
198 #define xbc_for_each_key_value(knode, value)				\
199 	xbc_node_for_each_key_value(NULL, knode, value)
200 
201 /* Compose partial key */
202 int __init xbc_node_compose_key_after(struct xbc_node *root,
203 			struct xbc_node *node, char *buf, size_t size);
204 
205 /**
206  * xbc_node_compose_key() - Compose full key string of the XBC node
207  * @node: An XBC node.
208  * @buf: A buffer to store the key.
209  * @size: The size of the @buf.
210  *
211  * Compose the full-length key of the @node into @buf. Returns the total
212  * length of the key stored in @buf. Or returns -EINVAL if @node is NULL,
213  * and -ERANGE if the key depth is deeper than max depth.
214  */
xbc_node_compose_key(struct xbc_node * node,char * buf,size_t size)215 static inline int __init xbc_node_compose_key(struct xbc_node *node,
216 					      char *buf, size_t size)
217 {
218 	return xbc_node_compose_key_after(NULL, node, buf, size);
219 }
220 
221 /* XBC node initializer */
222 int __init xbc_init(char *buf, const char **emsg, int *epos);
223 
224 
225 /* XBC cleanup data structures */
226 void __init xbc_destroy_all(void);
227 
228 /* Debug dump functions */
229 void __init xbc_debug_dump(void);
230 
231 #endif
232