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