Lines Matching +full:root +full:- +full:node

1 // SPDX-License-Identifier: GPL-2.0
17 /* embedded_bootconfig_data is defined in bootconfig-data.S */
23 *size = embedded_bootconfig_data_end - embedded_bootconfig_data; in xbc_get_embedded_bootconfig()
40 * Extra Boot Config (XBC) is given as tree-structured ascii text of
41 * key-value pairs on memory.
42 * xbc_parse() parses the text to build a simple tree. Each tree node is
43 * simply a key word or a value. A key node may have a next key node or/and
44 * a child node (both key and value). A value node may have a next value
45 * node (for array).
85 * xbc_get_info() - Get the information of loaded boot config
91 * Return 0 if the boot config is initialized, or return -ENODEV.
96 return -ENODEV; in xbc_get_info()
108 xbc_err_pos = (int)(p - xbc_data); in xbc_parse_error()
110 return -EINVAL; in xbc_parse_error()
114 * xbc_root_node() - Get the root node of extended boot config
116 * Return the address of root node of extended boot config. If the
128 * xbc_node_index() - Get the index of XBC node
129 * @node: A target node of getting index.
131 * Return the index number of @node in XBC node list.
133 int __init xbc_node_index(struct xbc_node *node) in xbc_node_index() argument
135 return node - &xbc_nodes[0]; in xbc_node_index()
139 * xbc_node_get_parent() - Get the parent XBC node
140 * @node: An XBC node.
142 * Return the parent node of @node. If the node is top node of the tree,
145 struct xbc_node * __init xbc_node_get_parent(struct xbc_node *node) in xbc_node_get_parent() argument
147 return node->parent == XBC_NODE_MAX ? NULL : &xbc_nodes[node->parent]; in xbc_node_get_parent()
151 * xbc_node_get_child() - Get the child XBC node
152 * @node: An XBC node.
154 * Return the first child node of @node. If the node has no child, return
157 struct xbc_node * __init xbc_node_get_child(struct xbc_node *node) in xbc_node_get_child() argument
159 return node->child ? &xbc_nodes[node->child] : NULL; in xbc_node_get_child()
163 * xbc_node_get_next() - Get the next sibling XBC node
164 * @node: An XBC node.
166 * Return the NEXT sibling node of @node. If the node has no next sibling,
167 * return NULL. Note that even if this returns NULL, it doesn't mean @node
168 * has no siblings. (You also has to check whether the parent's child node
169 * is @node or not.)
171 struct xbc_node * __init xbc_node_get_next(struct xbc_node *node) in xbc_node_get_next() argument
173 return node->next ? &xbc_nodes[node->next] : NULL; in xbc_node_get_next()
177 * xbc_node_get_data() - Get the data of XBC node
178 * @node: An XBC node.
180 * Return the data (which is always a null terminated string) of @node.
181 * If the node has invalid data, warn and return NULL.
183 const char * __init xbc_node_get_data(struct xbc_node *node) in xbc_node_get_data() argument
185 int offset = node->data & ~XBC_VALUE; in xbc_node_get_data()
194 xbc_node_match_prefix(struct xbc_node *node, const char **prefix) in xbc_node_match_prefix() argument
196 const char *p = xbc_node_get_data(node); in xbc_node_match_prefix()
213 * xbc_node_find_subkey() - Find a subkey node which matches given key
214 * @parent: An XBC node.
217 * Search a key node under @parent which matches @key. The @key can contain
219 * node from whole tree. Return NULL if no node is matched.
224 struct xbc_node *node; in xbc_node_find_subkey() local
227 node = xbc_node_get_subkey(parent); in xbc_node_find_subkey()
229 node = xbc_root_node(); in xbc_node_find_subkey()
231 while (node && xbc_node_is_key(node)) { in xbc_node_find_subkey()
232 if (!xbc_node_match_prefix(node, &key)) in xbc_node_find_subkey()
233 node = xbc_node_get_next(node); in xbc_node_find_subkey()
235 node = xbc_node_get_subkey(node); in xbc_node_find_subkey()
240 return node; in xbc_node_find_subkey()
244 * xbc_node_find_value() - Find a value node which matches given key
245 * @parent: An XBC node.
247 * @vnode: A container pointer of found XBC node.
249 * Search a value node under @parent whose (parent) key node matches @key,
252 * this searches the node from whole tree. Return the value string if a
253 * matched key found, return NULL if no node is matched.
254 * Note that this returns 0-length string and stores NULL in *@vnode if the
262 struct xbc_node *node = xbc_node_find_subkey(parent, key); in xbc_node_find_value() local
264 if (!node || !xbc_node_is_key(node)) in xbc_node_find_value()
267 node = xbc_node_get_child(node); in xbc_node_find_value()
268 if (node && !xbc_node_is_value(node)) in xbc_node_find_value()
272 *vnode = node; in xbc_node_find_value()
274 return node ? xbc_node_get_data(node) : ""; in xbc_node_find_value()
278 * xbc_node_compose_key_after() - Compose partial key string of the XBC node
279 * @root: Root XBC node
280 * @node: Target XBC node.
284 * Compose the partial key of the @node into @buf, which is starting right
285 * after @root (@root is not included.) If @root is NULL, this returns full
286 * key words of @node.
287 * Returns the total length of the key stored in @buf. Returns -EINVAL
288 * if @node is NULL or @root is not the ancestor of @node or @root is @node,
289 * or returns -ERANGE if the key depth is deeper than max depth.
293 int __init xbc_node_compose_key_after(struct xbc_node *root, in xbc_node_compose_key_after() argument
294 struct xbc_node *node, in xbc_node_compose_key_after() argument
300 if (!node || node == root) in xbc_node_compose_key_after()
301 return -EINVAL; in xbc_node_compose_key_after()
303 if (xbc_node_is_value(node)) in xbc_node_compose_key_after()
304 node = xbc_node_get_parent(node); in xbc_node_compose_key_after()
306 while (node && node != root) { in xbc_node_compose_key_after()
307 keys[depth++] = xbc_node_index(node); in xbc_node_compose_key_after()
309 return -ERANGE; in xbc_node_compose_key_after()
310 node = xbc_node_get_parent(node); in xbc_node_compose_key_after()
312 if (!node && root) in xbc_node_compose_key_after()
313 return -EINVAL; in xbc_node_compose_key_after()
315 while (--depth >= 0) { in xbc_node_compose_key_after()
316 node = xbc_nodes + keys[depth]; in xbc_node_compose_key_after()
317 ret = snprintf(buf, size, "%s%s", xbc_node_get_data(node), in xbc_node_compose_key_after()
324 size -= ret; in xbc_node_compose_key_after()
334 * xbc_node_find_next_leaf() - Find the next leaf node under given node
335 * @root: An XBC root node
336 * @node: An XBC node which starts from.
338 * Search the next leaf node (which means the terminal key node) of @node
339 * under @root node (including @root node itself).
340 * Return the next node or NULL if next leaf node is not found.
342 struct xbc_node * __init xbc_node_find_next_leaf(struct xbc_node *root, in xbc_node_find_next_leaf() argument
343 struct xbc_node *node) in xbc_node_find_next_leaf() argument
350 if (!node) { /* First try */ in xbc_node_find_next_leaf()
351 node = root; in xbc_node_find_next_leaf()
352 if (!node) in xbc_node_find_next_leaf()
353 node = xbc_nodes; in xbc_node_find_next_leaf()
355 /* Leaf node may have a subkey */ in xbc_node_find_next_leaf()
356 next = xbc_node_get_subkey(node); in xbc_node_find_next_leaf()
358 node = next; in xbc_node_find_next_leaf()
362 if (node == root) /* @root was a leaf, no child node. */ in xbc_node_find_next_leaf()
365 while (!node->next) { in xbc_node_find_next_leaf()
366 node = xbc_node_get_parent(node); in xbc_node_find_next_leaf()
367 if (node == root) in xbc_node_find_next_leaf()
369 /* User passed a node which is not uder parent */ in xbc_node_find_next_leaf()
370 if (WARN_ON(!node)) in xbc_node_find_next_leaf()
373 node = xbc_node_get_next(node); in xbc_node_find_next_leaf()
377 while (node && !xbc_node_is_leaf(node)) in xbc_node_find_next_leaf()
378 node = xbc_node_get_child(node); in xbc_node_find_next_leaf()
380 return node; in xbc_node_find_next_leaf()
384 * xbc_node_find_next_key_value() - Find the next key-value pair nodes
385 * @root: An XBC root node
386 * @leaf: A container pointer of XBC node which starts from.
388 * Search the next leaf node (which means the terminal key node) of *@leaf
389 * under @root node. Returns the value and update *@leaf if next leaf node
390 * is found, or NULL if no next leaf node is found.
391 * Note that this returns 0-length string if the key has no value, or
394 const char * __init xbc_node_find_next_key_value(struct xbc_node *root, in xbc_node_find_next_key_value() argument
401 *leaf = xbc_node_find_next_leaf(root, *leaf); in xbc_node_find_next_key_value()
404 if ((*leaf)->child) in xbc_node_find_next_key_value()
412 static int __init xbc_init_node(struct xbc_node *node, char *data, uint32_t flag) in xbc_init_node() argument
414 unsigned long offset = data - xbc_data; in xbc_init_node()
417 return -EINVAL; in xbc_init_node()
419 node->data = (uint16_t)offset | flag; in xbc_init_node()
420 node->child = 0; in xbc_init_node()
421 node->next = 0; in xbc_init_node()
428 struct xbc_node *node; in xbc_add_node() local
433 node = &xbc_nodes[xbc_node_num++]; in xbc_add_node()
434 if (xbc_init_node(node, data, flag) < 0) in xbc_add_node()
437 return node; in xbc_add_node()
440 static inline __init struct xbc_node *xbc_last_sibling(struct xbc_node *node) in xbc_last_sibling() argument
442 while (node->next) in xbc_last_sibling()
443 node = xbc_node_get_next(node); in xbc_last_sibling()
445 return node; in xbc_last_sibling()
448 static inline __init struct xbc_node *xbc_last_child(struct xbc_node *node) in xbc_last_child() argument
450 while (node->child) in xbc_last_child()
451 node = xbc_node_get_child(node); in xbc_last_child()
453 return node; in xbc_last_child()
458 struct xbc_node *sib, *node = xbc_add_node(data, flag); in __xbc_add_sibling() local
460 if (node) { in __xbc_add_sibling()
463 node->parent = XBC_NODE_MAX; in __xbc_add_sibling()
465 sib->next = xbc_node_index(node); in __xbc_add_sibling()
467 node->parent = xbc_node_index(last_parent); in __xbc_add_sibling()
468 if (!last_parent->child || head) { in __xbc_add_sibling()
469 node->next = last_parent->child; in __xbc_add_sibling()
470 last_parent->child = xbc_node_index(node); in __xbc_add_sibling()
474 sib->next = xbc_node_index(node); in __xbc_add_sibling()
480 return node; in __xbc_add_sibling()
495 struct xbc_node *node = xbc_add_sibling(data, flag); in xbc_add_child() local
497 if (node) in xbc_add_child()
498 last_parent = node; in xbc_add_child()
500 return node; in xbc_add_child()
508 while (isalnum(*key) || *key == '-' || *key == '_') in xbc_valid_keyword()
546 brace_index--; in __xbc_close_brace()
554 last_parent = &xbc_nodes[open_brace[brace_index - 1]]; in __xbc_close_brace()
560 * Return delimiter or error, no node added. As same as lib/cmdline.c,
577 p = v - 1; in __xbc_parse_value()
614 struct xbc_node *node; in xbc_parse_array() local
618 if (last_parent->child) in xbc_parse_array()
626 node = xbc_add_child(*__v, XBC_VALUE); in xbc_parse_array()
627 if (!node) in xbc_parse_array()
628 return -ENOMEM; in xbc_parse_array()
631 node->child = 0; in xbc_parse_array()
637 struct xbc_node *find_match_node(struct xbc_node *node, char *k) in find_match_node() argument
639 while (node) { in find_match_node()
640 if (!strcmp(xbc_node_get_data(node), k)) in find_match_node()
642 node = xbc_node_get_next(node); in find_match_node()
644 return node; in find_match_node()
649 struct xbc_node *node, *child; in __xbc_add_key() local
658 node = find_match_node(xbc_nodes, k); in __xbc_add_key()
661 /* Since the value node is the first child, skip it. */ in __xbc_add_key()
664 node = find_match_node(child, k); in __xbc_add_key()
667 if (node) in __xbc_add_key()
668 last_parent = node; in __xbc_add_key()
671 node = xbc_add_child(k, XBC_KEY); in __xbc_add_key()
672 if (!node) in __xbc_add_key()
673 return -ENOMEM; in __xbc_add_key()
715 unsigned short nidx = child->next; in xbc_parse_kv()
718 child->next = nidx; /* keep subkeys */ in xbc_parse_kv()
724 /* The value node should always be the first child */ in xbc_parse_kv()
726 return -ENOMEM; in xbc_parse_kv()
738 ret = __xbc_close_brace(next - 1); in xbc_parse_kv()
774 return __xbc_open_brace(n - 1); in xbc_open_brace()
786 return __xbc_close_brace(n - 1); in xbc_close_brace()
804 return -ENOENT; in xbc_verify_tree()
835 len -= wlen; in xbc_verify_tree()
841 len -= strlen(xbc_node_get_data(n)) + 1; in xbc_verify_tree()
842 depth--; in xbc_verify_tree()
877 q - 2); in xbc_parse_tree()
904 * _xbc_exit() - Clean up all parsed bootconfig
923 * xbc_init() - Parse given XBC file and build XBC internal tree
931 * Return the number of stored nodes (>0) if succeeded, or -errno
935 * of @buf. If the error is not a parser error, @epos will be -1.
942 *epos = -1; in xbc_init()
947 return -EBUSY; in xbc_init()
953 return -ERANGE; in xbc_init()
960 return -ENOMEM; in xbc_init()
971 return -ENOMEM; in xbc_init()