Lines Matching +full:child +full:- +full:node
1 // SPDX-License-Identifier: GPL-2.0
19 * Extra Boot Config (XBC) is given as tree-structured ascii text of
20 * key-value pairs on memory.
21 * xbc_parse() parses the text to build a simple tree. Each tree node is
22 * simply a key word or a value. A key node may have a next key node or/and
23 * a child node (both key and value). A value node may have a next value
24 * node (for array).
40 xbc_err_pos = (int)(p - xbc_data); in xbc_parse_error()
42 return -EINVAL; in xbc_parse_error()
46 * xbc_root_node() - Get the root node of extended boot config
48 * Return the address of root node of extended boot config. If the
60 * xbc_node_index() - Get the index of XBC node
61 * @node: A target node of getting index.
63 * Return the index number of @node in XBC node list.
65 int __init xbc_node_index(struct xbc_node *node) in xbc_node_index() argument
67 return node - &xbc_nodes[0]; in xbc_node_index()
71 * xbc_node_get_parent() - Get the parent XBC node
72 * @node: An XBC node.
74 * Return the parent node of @node. If the node is top node of the tree,
77 struct xbc_node * __init xbc_node_get_parent(struct xbc_node *node) in xbc_node_get_parent() argument
79 return node->parent == XBC_NODE_MAX ? NULL : &xbc_nodes[node->parent]; in xbc_node_get_parent()
83 * xbc_node_get_child() - Get the child XBC node
84 * @node: An XBC node.
86 * Return the first child node of @node. If the node has no child, return
89 struct xbc_node * __init xbc_node_get_child(struct xbc_node *node) in xbc_node_get_child() argument
91 return node->child ? &xbc_nodes[node->child] : NULL; in xbc_node_get_child()
95 * xbc_node_get_next() - Get the next sibling XBC node
96 * @node: An XBC node.
98 * Return the NEXT sibling node of @node. If the node has no next sibling,
99 * return NULL. Note that even if this returns NULL, it doesn't mean @node
100 * has no siblings. (You also has to check whether the parent's child node
101 * is @node or not.)
103 struct xbc_node * __init xbc_node_get_next(struct xbc_node *node) in xbc_node_get_next() argument
105 return node->next ? &xbc_nodes[node->next] : NULL; in xbc_node_get_next()
109 * xbc_node_get_data() - Get the data of XBC node
110 * @node: An XBC node.
112 * Return the data (which is always a null terminated string) of @node.
113 * If the node has invalid data, warn and return NULL.
115 const char * __init xbc_node_get_data(struct xbc_node *node) in xbc_node_get_data() argument
117 int offset = node->data & ~XBC_VALUE; in xbc_node_get_data()
126 xbc_node_match_prefix(struct xbc_node *node, const char **prefix) in xbc_node_match_prefix() argument
128 const char *p = xbc_node_get_data(node); in xbc_node_match_prefix()
145 * xbc_node_find_child() - Find a child node which matches given key
146 * @parent: An XBC node.
149 * Search a node under @parent which matches @key. The @key can contain
151 * node from whole tree. Return NULL if no node is matched.
156 struct xbc_node *node; in xbc_node_find_child() local
159 node = xbc_node_get_child(parent); in xbc_node_find_child()
161 node = xbc_root_node(); in xbc_node_find_child()
163 while (node && xbc_node_is_key(node)) { in xbc_node_find_child()
164 if (!xbc_node_match_prefix(node, &key)) in xbc_node_find_child()
165 node = xbc_node_get_next(node); in xbc_node_find_child()
167 node = xbc_node_get_child(node); in xbc_node_find_child()
172 return node; in xbc_node_find_child()
176 * xbc_node_find_value() - Find a value node which matches given key
177 * @parent: An XBC node.
179 * @vnode: A container pointer of found XBC node.
181 * Search a value node under @parent whose (parent) key node matches @key,
184 * this searches the node from whole tree. Return the value string if a
185 * matched key found, return NULL if no node is matched.
186 * Note that this returns 0-length string and stores NULL in *@vnode if the
194 struct xbc_node *node = xbc_node_find_child(parent, key); in xbc_node_find_value() local
196 if (!node || !xbc_node_is_key(node)) in xbc_node_find_value()
199 node = xbc_node_get_child(node); in xbc_node_find_value()
200 if (node && !xbc_node_is_value(node)) in xbc_node_find_value()
204 *vnode = node; in xbc_node_find_value()
206 return node ? xbc_node_get_data(node) : ""; in xbc_node_find_value()
210 * xbc_node_compose_key_after() - Compose partial key string of the XBC node
211 * @root: Root XBC node
212 * @node: Target XBC node.
216 * Compose the partial key of the @node into @buf, which is starting right
218 * key words of @node.
219 * Returns the total length of the key stored in @buf. Returns -EINVAL
220 * if @node is NULL or @root is not the ancestor of @node or @root is @node,
221 * or returns -ERANGE if the key depth is deeper than max depth.
222 * This is expected to be used with xbc_find_node() to list up all (child)
226 struct xbc_node *node, in xbc_node_compose_key_after() argument
232 if (!node || node == root) in xbc_node_compose_key_after()
233 return -EINVAL; in xbc_node_compose_key_after()
235 if (xbc_node_is_value(node)) in xbc_node_compose_key_after()
236 node = xbc_node_get_parent(node); in xbc_node_compose_key_after()
238 while (node && node != root) { in xbc_node_compose_key_after()
239 keys[depth++] = xbc_node_index(node); in xbc_node_compose_key_after()
241 return -ERANGE; in xbc_node_compose_key_after()
242 node = xbc_node_get_parent(node); in xbc_node_compose_key_after()
244 if (!node && root) in xbc_node_compose_key_after()
245 return -EINVAL; in xbc_node_compose_key_after()
247 while (--depth >= 0) { in xbc_node_compose_key_after()
248 node = xbc_nodes + keys[depth]; in xbc_node_compose_key_after()
249 ret = snprintf(buf, size, "%s%s", xbc_node_get_data(node), in xbc_node_compose_key_after()
256 size -= ret; in xbc_node_compose_key_after()
266 * xbc_node_find_next_leaf() - Find the next leaf node under given node
267 * @root: An XBC root node
268 * @node: An XBC node which starts from.
270 * Search the next leaf node (which means the terminal key node) of @node
271 * under @root node (including @root node itself).
272 * Return the next node or NULL if next leaf node is not found.
275 struct xbc_node *node) in xbc_node_find_next_leaf() argument
280 if (!node) { /* First try */ in xbc_node_find_next_leaf()
281 node = root; in xbc_node_find_next_leaf()
282 if (!node) in xbc_node_find_next_leaf()
283 node = xbc_nodes; in xbc_node_find_next_leaf()
285 if (node == root) /* @root was a leaf, no child node. */ in xbc_node_find_next_leaf()
288 while (!node->next) { in xbc_node_find_next_leaf()
289 node = xbc_node_get_parent(node); in xbc_node_find_next_leaf()
290 if (node == root) in xbc_node_find_next_leaf()
292 /* User passed a node which is not uder parent */ in xbc_node_find_next_leaf()
293 if (WARN_ON(!node)) in xbc_node_find_next_leaf()
296 node = xbc_node_get_next(node); in xbc_node_find_next_leaf()
299 while (node && !xbc_node_is_leaf(node)) in xbc_node_find_next_leaf()
300 node = xbc_node_get_child(node); in xbc_node_find_next_leaf()
302 return node; in xbc_node_find_next_leaf()
306 * xbc_node_find_next_key_value() - Find the next key-value pair nodes
307 * @root: An XBC root node
308 * @leaf: A container pointer of XBC node which starts from.
310 * Search the next leaf node (which means the terminal key node) of *@leaf
311 * under @root node. Returns the value and update *@leaf if next leaf node
312 * is found, or NULL if no next leaf node is found.
313 * Note that this returns 0-length string if the key has no value, or
326 if ((*leaf)->child) in xbc_node_find_next_key_value()
334 static int __init xbc_init_node(struct xbc_node *node, char *data, u32 flag) in xbc_init_node() argument
336 unsigned long offset = data - xbc_data; in xbc_init_node()
339 return -EINVAL; in xbc_init_node()
341 node->data = (u16)offset | flag; in xbc_init_node()
342 node->child = 0; in xbc_init_node()
343 node->next = 0; in xbc_init_node()
350 struct xbc_node *node; in xbc_add_node() local
355 node = &xbc_nodes[xbc_node_num++]; in xbc_add_node()
356 if (xbc_init_node(node, data, flag) < 0) in xbc_add_node()
359 return node; in xbc_add_node()
362 static inline __init struct xbc_node *xbc_last_sibling(struct xbc_node *node) in xbc_last_sibling() argument
364 while (node->next) in xbc_last_sibling()
365 node = xbc_node_get_next(node); in xbc_last_sibling()
367 return node; in xbc_last_sibling()
372 struct xbc_node *sib, *node = xbc_add_node(data, flag); in xbc_add_sibling() local
374 if (node) { in xbc_add_sibling()
376 node->parent = XBC_NODE_MAX; in xbc_add_sibling()
378 sib->next = xbc_node_index(node); in xbc_add_sibling()
380 node->parent = xbc_node_index(last_parent); in xbc_add_sibling()
381 if (!last_parent->child) { in xbc_add_sibling()
382 last_parent->child = xbc_node_index(node); in xbc_add_sibling()
386 sib->next = xbc_node_index(node); in xbc_add_sibling()
392 return node; in xbc_add_sibling()
397 struct xbc_node *node = xbc_add_sibling(data, flag); in xbc_add_child() local
399 if (node) in xbc_add_child()
400 last_parent = node; in xbc_add_child()
402 return node; in xbc_add_child()
410 while (isalnum(*key) || *key == '-' || *key == '_') in xbc_valid_keyword()
448 brace_index--; in __xbc_close_brace()
456 last_parent = &xbc_nodes[open_brace[brace_index - 1]]; in __xbc_close_brace()
462 * Return delimiter or error, no node added. As same as lib/cmdline.c,
479 p = v - 1; in __xbc_parse_value()
516 struct xbc_node *node; in xbc_parse_array() local
525 node = xbc_add_sibling(*__v, XBC_VALUE); in xbc_parse_array()
526 if (!node) in xbc_parse_array()
527 return -ENOMEM; in xbc_parse_array()
530 node->next = 0; in xbc_parse_array()
536 struct xbc_node *find_match_node(struct xbc_node *node, char *k) in find_match_node() argument
538 while (node) { in find_match_node()
539 if (!strcmp(xbc_node_get_data(node), k)) in find_match_node()
541 node = xbc_node_get_next(node); in find_match_node()
543 return node; in find_match_node()
548 struct xbc_node *node, *child; in __xbc_add_key() local
557 node = find_match_node(xbc_nodes, k); in __xbc_add_key()
559 child = xbc_node_get_child(last_parent); in __xbc_add_key()
560 if (child && xbc_node_is_value(child)) in __xbc_add_key()
562 node = find_match_node(child, k); in __xbc_add_key()
565 if (node) in __xbc_add_key()
566 last_parent = node; in __xbc_add_key()
569 node = xbc_add_child(k, XBC_KEY); in __xbc_add_key()
570 if (!node) in __xbc_add_key()
571 return -ENOMEM; in __xbc_add_key()
596 struct xbc_node *child; in xbc_parse_kv() local
604 child = xbc_node_get_child(last_parent); in xbc_parse_kv()
605 if (child) { in xbc_parse_kv()
606 if (xbc_node_is_key(child)) in xbc_parse_kv()
616 if (op == ':' && child) { in xbc_parse_kv()
617 xbc_init_node(child, v, XBC_VALUE); in xbc_parse_kv()
619 return -ENOMEM; in xbc_parse_kv()
630 ret = __xbc_close_brace(next - 1); in xbc_parse_kv()
666 return __xbc_open_brace(n - 1); in xbc_open_brace()
678 return __xbc_close_brace(n - 1); in xbc_close_brace()
696 return -ENOENT; in xbc_verify_tree()
727 len -= wlen; in xbc_verify_tree()
733 len -= strlen(xbc_node_get_data(n)) + 1; in xbc_verify_tree()
734 depth--; in xbc_verify_tree()
744 * xbc_destroy_all() - Clean up all parsed bootconfig
761 * xbc_init() - Parse given XBC file and build XBC internal tree
768 * Return the number of stored nodes (>0) if succeeded, or -errno
772 * of @buf. If the error is not a parser error, @epos will be -1.
780 *epos = -1; in xbc_init()
785 return -EBUSY; in xbc_init()
789 if (ret > XBC_DATA_MAX - 1 || ret == 0) { in xbc_init()
793 return -ERANGE; in xbc_init()
801 return -ENOMEM; in xbc_init()
827 q - 2); in xbc_init()
866 * xbc_debug_dump() - Dump current XBC node list
868 * Dump the current XBC node list on printk buffer for debug.
875 pr_debug("[%d] %s (%s) .next=%d, .child=%d .parent=%d\n", i, in xbc_debug_dump()
878 xbc_nodes[i].next, xbc_nodes[i].child, in xbc_debug_dump()