Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Extra Boot Config
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Masami Hiramatsu <mhiramat@kernel.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #define pr_fmt(fmt)    "bootconfig: " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/bootconfig.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/bug.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/ctype.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/memblock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/printk.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  * Extra Boot Config (XBC) is given as tree-structured ascii text of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  * key-value pairs on memory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  * xbc_parse() parses the text to build a simple tree. Each tree node is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  * simply a key word or a value. A key node may have a next key node or/and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  * a child node (both key and value). A value node may have a next value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  * node (for array).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) static struct xbc_node *xbc_nodes __initdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) static int xbc_node_num __initdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) static char *xbc_data __initdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) static size_t xbc_data_size __initdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) static struct xbc_node *last_parent __initdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) static const char *xbc_err_msg __initdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) static int xbc_err_pos __initdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) static int open_brace[XBC_DEPTH_MAX] __initdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) static int brace_index __initdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) static int __init xbc_parse_error(const char *msg, const char *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	xbc_err_msg = msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	xbc_err_pos = (int)(p - xbc_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46)  * xbc_root_node() - Get the root node of extended boot config
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48)  * Return the address of root node of extended boot config. If the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49)  * extended boot config is not initiized, return NULL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) struct xbc_node * __init xbc_root_node(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	if (unlikely(!xbc_data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	return xbc_nodes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60)  * xbc_node_index() - Get the index of XBC node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61)  * @node: A target node of getting index.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63)  * Return the index number of @node in XBC node list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) int __init xbc_node_index(struct xbc_node *node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	return node - &xbc_nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71)  * xbc_node_get_parent() - Get the parent XBC node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72)  * @node: An XBC node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74)  * Return the parent node of @node. If the node is top node of the tree,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75)  * return NULL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) struct xbc_node * __init xbc_node_get_parent(struct xbc_node *node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	return node->parent == XBC_NODE_MAX ? NULL : &xbc_nodes[node->parent];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83)  * xbc_node_get_child() - Get the child XBC node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84)  * @node: An XBC node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86)  * Return the first child node of @node. If the node has no child, return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87)  * NULL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) struct xbc_node * __init xbc_node_get_child(struct xbc_node *node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	return node->child ? &xbc_nodes[node->child] : NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95)  * xbc_node_get_next() - Get the next sibling XBC node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96)  * @node: An XBC node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98)  * Return the NEXT sibling node of @node. If the node has no next sibling,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99)  * return NULL. Note that even if this returns NULL, it doesn't mean @node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)  * has no siblings. (You also has to check whether the parent's child node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)  * is @node or not.)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) struct xbc_node * __init xbc_node_get_next(struct xbc_node *node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	return node->next ? &xbc_nodes[node->next] : NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)  * xbc_node_get_data() - Get the data of XBC node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)  * @node: An XBC node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)  * Return the data (which is always a null terminated string) of @node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)  * If the node has invalid data, warn and return NULL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) const char * __init xbc_node_get_data(struct xbc_node *node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	int offset = node->data & ~XBC_VALUE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	if (WARN_ON(offset >= xbc_data_size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	return xbc_data + offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) static bool __init
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) xbc_node_match_prefix(struct xbc_node *node, const char **prefix)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	const char *p = xbc_node_get_data(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	int len = strlen(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	if (strncmp(*prefix, p, len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	p = *prefix + len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	if (*p == '.')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		p++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	else if (*p != '\0')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	*prefix = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)  * xbc_node_find_child() - Find a child node which matches given key
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)  * @parent: An XBC node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)  * @key: A key string.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)  * Search a node under @parent which matches @key. The @key can contain
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)  * several words jointed with '.'. If @parent is NULL, this searches the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)  * node from whole tree. Return NULL if no node is matched.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) struct xbc_node * __init
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) xbc_node_find_child(struct xbc_node *parent, const char *key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	struct xbc_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	if (parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		node = xbc_node_get_subkey(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 		node = xbc_root_node();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	while (node && xbc_node_is_key(node)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 		if (!xbc_node_match_prefix(node, &key))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 			node = xbc_node_get_next(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 		else if (*key != '\0')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 			node = xbc_node_get_subkey(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	return node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)  * xbc_node_find_value() - Find a value node which matches given key
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)  * @parent: An XBC node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)  * @key: A key string.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)  * @vnode: A container pointer of found XBC node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)  * Search a value node under @parent whose (parent) key node matches @key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)  * store it in *@vnode, and returns the value string.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)  * The @key can contain several words jointed with '.'. If @parent is NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)  * this searches the node from whole tree. Return the value string if a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)  * matched key found, return NULL if no node is matched.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)  * Note that this returns 0-length string and stores NULL in *@vnode if the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)  * key has no value. And also it will return the value of the first entry if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)  * the value is an array.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) const char * __init
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) xbc_node_find_value(struct xbc_node *parent, const char *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 		    struct xbc_node **vnode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	struct xbc_node *node = xbc_node_find_child(parent, key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	if (!node || !xbc_node_is_key(node))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	node = xbc_node_get_child(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	if (node && !xbc_node_is_value(node))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	if (vnode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		*vnode = node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	return node ? xbc_node_get_data(node) : "";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)  * xbc_node_compose_key_after() - Compose partial key string of the XBC node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)  * @root: Root XBC node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)  * @node: Target XBC node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)  * @buf: A buffer to store the key.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)  * @size: The size of the @buf.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)  * Compose the partial key of the @node into @buf, which is starting right
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)  * after @root (@root is not included.) If @root is NULL, this returns full
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)  * key words of @node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)  * Returns the total length of the key stored in @buf. Returns -EINVAL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)  * if @node is NULL or @root is not the ancestor of @node or @root is @node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)  * or returns -ERANGE if the key depth is deeper than max depth.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)  * This is expected to be used with xbc_find_node() to list up all (child)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)  * keys under given key.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) int __init xbc_node_compose_key_after(struct xbc_node *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 				      struct xbc_node *node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 				      char *buf, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	u16 keys[XBC_DEPTH_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	int depth = 0, ret = 0, total = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	if (!node || node == root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	if (xbc_node_is_value(node))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 		node = xbc_node_get_parent(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	while (node && node != root) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 		keys[depth++] = xbc_node_index(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 		if (depth == XBC_DEPTH_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 			return -ERANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 		node = xbc_node_get_parent(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	if (!node && root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	while (--depth >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 		node = xbc_nodes + keys[depth];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 		ret = snprintf(buf, size, "%s%s", xbc_node_get_data(node),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 			       depth ? "." : "");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 		if (ret > size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 			size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 			size -= ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 			buf += ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 		total += ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	return total;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)  * xbc_node_find_next_leaf() - Find the next leaf node under given node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)  * @root: An XBC root node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)  * @node: An XBC node which starts from.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)  * Search the next leaf node (which means the terminal key node) of @node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)  * under @root node (including @root node itself).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)  * Return the next node or NULL if next leaf node is not found.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) struct xbc_node * __init xbc_node_find_next_leaf(struct xbc_node *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 						 struct xbc_node *node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	struct xbc_node *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	if (unlikely(!xbc_data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	if (!node) {	/* First try */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 		node = root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 		if (!node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 			node = xbc_nodes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 		/* Leaf node may have a subkey */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 		next = xbc_node_get_subkey(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 		if (next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 			node = next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 			goto found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 		if (node == root)	/* @root was a leaf, no child node. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 			return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 		while (!node->next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 			node = xbc_node_get_parent(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 			if (node == root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 				return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 			/* User passed a node which is not uder parent */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 			if (WARN_ON(!node))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 				return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 		node = xbc_node_get_next(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) found:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	while (node && !xbc_node_is_leaf(node))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 		node = xbc_node_get_child(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	return node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)  * xbc_node_find_next_key_value() - Find the next key-value pair nodes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)  * @root: An XBC root node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)  * @leaf: A container pointer of XBC node which starts from.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320)  * Search the next leaf node (which means the terminal key node) of *@leaf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)  * under @root node. Returns the value and update *@leaf if next leaf node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322)  * is found, or NULL if no next leaf node is found.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323)  * Note that this returns 0-length string if the key has no value, or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)  * the value of the first entry if the value is an array.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) const char * __init xbc_node_find_next_key_value(struct xbc_node *root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 						 struct xbc_node **leaf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	/* tip must be passed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	if (WARN_ON(!leaf))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	*leaf = xbc_node_find_next_leaf(root, *leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	if (!*leaf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	if ((*leaf)->child)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 		return xbc_node_get_data(xbc_node_get_child(*leaf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 		return "";	/* No value key */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) /* XBC parse and tree build */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) static int __init xbc_init_node(struct xbc_node *node, char *data, u32 flag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	unsigned long offset = data - xbc_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	if (WARN_ON(offset >= XBC_DATA_MAX))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	node->data = (u16)offset | flag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	node->child = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	node->next = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) static struct xbc_node * __init xbc_add_node(char *data, u32 flag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	struct xbc_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	if (xbc_node_num == XBC_NODE_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	node = &xbc_nodes[xbc_node_num++];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	if (xbc_init_node(node, data, flag) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	return node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) static inline __init struct xbc_node *xbc_last_sibling(struct xbc_node *node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	while (node->next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 		node = xbc_node_get_next(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	return node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) static inline __init struct xbc_node *xbc_last_child(struct xbc_node *node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	while (node->child)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 		node = xbc_node_get_child(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	return node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) static struct xbc_node * __init __xbc_add_sibling(char *data, u32 flag, bool head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	struct xbc_node *sib, *node = xbc_add_node(data, flag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	if (node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 		if (!last_parent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 			/* Ignore @head in this case */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 			node->parent = XBC_NODE_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 			sib = xbc_last_sibling(xbc_nodes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 			sib->next = xbc_node_index(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 			node->parent = xbc_node_index(last_parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 			if (!last_parent->child || head) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 				node->next = last_parent->child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 				last_parent->child = xbc_node_index(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 				sib = xbc_node_get_child(last_parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 				sib = xbc_last_sibling(sib);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 				sib->next = xbc_node_index(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 		xbc_parse_error("Too many nodes", data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	return node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) static inline struct xbc_node * __init xbc_add_sibling(char *data, u32 flag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	return __xbc_add_sibling(data, flag, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) static inline struct xbc_node * __init xbc_add_head_sibling(char *data, u32 flag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	return __xbc_add_sibling(data, flag, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) static inline __init struct xbc_node *xbc_add_child(char *data, u32 flag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	struct xbc_node *node = xbc_add_sibling(data, flag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	if (node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 		last_parent = node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	return node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) static inline __init bool xbc_valid_keyword(char *key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	if (key[0] == '\0')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	while (isalnum(*key) || *key == '-' || *key == '_')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 		key++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	return *key == '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) static char *skip_comment(char *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	char *ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 	ret = strchr(p, '\n');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 	if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 		ret = p + strlen(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 		ret++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) static char *skip_spaces_until_newline(char *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 	while (isspace(*p) && *p != '\n')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 		p++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	return p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) static int __init __xbc_open_brace(char *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 	/* Push the last key as open brace */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	open_brace[brace_index++] = xbc_node_index(last_parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 	if (brace_index >= XBC_DEPTH_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 		return xbc_parse_error("Exceed max depth of braces", p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) static int __init __xbc_close_brace(char *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 	brace_index--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 	if (!last_parent || brace_index < 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 	    (open_brace[brace_index] != xbc_node_index(last_parent)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 		return xbc_parse_error("Unexpected closing brace", p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 	if (brace_index == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 		last_parent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 		last_parent = &xbc_nodes[open_brace[brace_index - 1]];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492)  * Return delimiter or error, no node added. As same as lib/cmdline.c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493)  * you can use " around spaces, but can't escape " for value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) static int __init __xbc_parse_value(char **__v, char **__n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 	char *p, *v = *__v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 	int c, quotes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 	v = skip_spaces(v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 	while (*v == '#') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 		v = skip_comment(v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 		v = skip_spaces(v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 	if (*v == '"' || *v == '\'') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 		quotes = *v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 		v++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 	p = v - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 	while ((c = *++p)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 		if (!isprint(c) && !isspace(c))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 			return xbc_parse_error("Non printable value", p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 		if (quotes) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 			if (c != quotes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 			quotes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 			*p++ = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 			p = skip_spaces_until_newline(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 			c = *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 			if (c && !strchr(",;\n#}", c))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 				return xbc_parse_error("No value delimiter", p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 			if (*p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 				p++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 		if (strchr(",;\n#}", c)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 			*p++ = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 			v = strim(v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 	if (quotes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 		return xbc_parse_error("No closing quotes", p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 	if (c == '#') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 		p = skip_comment(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 		c = '\n';	/* A comment must be treated as a newline */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 	*__n = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 	*__v = v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 	return c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) static int __init xbc_parse_array(char **__v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 	struct xbc_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 	char *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 	int c = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 	if (last_parent->child)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 		last_parent = xbc_node_get_child(last_parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 		c = __xbc_parse_value(__v, &next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 		if (c < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 			return c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 		node = xbc_add_child(*__v, XBC_VALUE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 		if (!node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 		*__v = next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 	} while (c == ',');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 	node->child = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 	return c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) static inline __init
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) struct xbc_node *find_match_node(struct xbc_node *node, char *k)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 	while (node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 		if (!strcmp(xbc_node_get_data(node), k))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 		node = xbc_node_get_next(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 	return node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) static int __init __xbc_add_key(char *k)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 	struct xbc_node *node, *child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 	if (!xbc_valid_keyword(k))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 		return xbc_parse_error("Invalid keyword", k);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 	if (unlikely(xbc_node_num == 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 		goto add_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 	if (!last_parent)	/* the first level */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 		node = find_match_node(xbc_nodes, k);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 		child = xbc_node_get_child(last_parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 		/* Since the value node is the first child, skip it. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 		if (child && xbc_node_is_value(child))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 			child = xbc_node_get_next(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 		node = find_match_node(child, k);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 	if (node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 		last_parent = node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) add_node:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 		node = xbc_add_child(k, XBC_KEY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 		if (!node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) static int __init __xbc_parse_keys(char *k)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 	char *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 	k = strim(k);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 	while ((p = strchr(k, '.'))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 		*p++ = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 		ret = __xbc_add_key(k);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 		k = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) 	return __xbc_add_key(k);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) static int __init xbc_parse_kv(char **k, char *v, int op)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) 	struct xbc_node *prev_parent = last_parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 	struct xbc_node *child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) 	char *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) 	int c, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) 	ret = __xbc_parse_keys(*k);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) 	c = __xbc_parse_value(&v, &next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) 	if (c < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) 		return c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) 	child = xbc_node_get_child(last_parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) 	if (child && xbc_node_is_value(child)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) 		if (op == '=')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) 			return xbc_parse_error("Value is redefined", v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) 		if (op == ':') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) 			unsigned short nidx = child->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) 			xbc_init_node(child, v, XBC_VALUE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) 			child->next = nidx;	/* keep subkeys */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) 			goto array;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) 		/* op must be '+' */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) 		last_parent = xbc_last_child(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) 	/* The value node should always be the first child */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) 	if (!xbc_add_head_sibling(v, XBC_VALUE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) array:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) 	if (c == ',') {	/* Array */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) 		c = xbc_parse_array(&next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) 		if (c < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) 			return c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) 	last_parent = prev_parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) 	if (c == '}') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) 		ret = __xbc_close_brace(next - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) 	*k = next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) static int __init xbc_parse_key(char **k, char *n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) 	struct xbc_node *prev_parent = last_parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) 	*k = strim(*k);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) 	if (**k != '\0') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) 		ret = __xbc_parse_keys(*k);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) 		last_parent = prev_parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) 	*k = n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) static int __init xbc_open_brace(char **k, char *n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) 	ret = __xbc_parse_keys(*k);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) 	*k = n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) 	return __xbc_open_brace(n - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) static int __init xbc_close_brace(char **k, char *n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) 	ret = xbc_parse_key(k, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) 	/* k is updated in xbc_parse_key() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) 	return __xbc_close_brace(n - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) static int __init xbc_verify_tree(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) 	int i, depth, len, wlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) 	struct xbc_node *n, *m;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) 	/* Brace closing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) 	if (brace_index) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) 		n = &xbc_nodes[open_brace[brace_index]];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) 		return xbc_parse_error("Brace is not closed",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) 					xbc_node_get_data(n));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) 	/* Empty tree */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) 	if (xbc_node_num == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) 		xbc_parse_error("Empty config", xbc_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) 		return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) 	for (i = 0; i < xbc_node_num; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) 		if (xbc_nodes[i].next > xbc_node_num) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) 			return xbc_parse_error("No closing brace",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) 				xbc_node_get_data(xbc_nodes + i));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) 	/* Key tree limitation check */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) 	n = &xbc_nodes[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) 	depth = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) 	len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) 	while (n) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) 		wlen = strlen(xbc_node_get_data(n)) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) 		len += wlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) 		if (len > XBC_KEYLEN_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) 			return xbc_parse_error("Too long key length",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) 				xbc_node_get_data(n));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) 		m = xbc_node_get_child(n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) 		if (m && xbc_node_is_key(m)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) 			n = m;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) 			depth++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) 			if (depth > XBC_DEPTH_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) 				return xbc_parse_error("Too many key words",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) 						xbc_node_get_data(n));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) 		len -= wlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) 		m = xbc_node_get_next(n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) 		while (!m) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) 			n = xbc_node_get_parent(n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) 			if (!n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) 			len -= strlen(xbc_node_get_data(n)) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) 			depth--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) 			m = xbc_node_get_next(n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) 		n = m;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784)  * xbc_destroy_all() - Clean up all parsed bootconfig
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786)  * This clears all data structures of parsed bootconfig on memory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787)  * If you need to reuse xbc_init() with new boot config, you can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788)  * use this.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) void __init xbc_destroy_all(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) 	xbc_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) 	xbc_data_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) 	xbc_node_num = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) 	memblock_free(__pa(xbc_nodes), sizeof(struct xbc_node) * XBC_NODE_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) 	xbc_nodes = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) 	brace_index = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801)  * xbc_init() - Parse given XBC file and build XBC internal tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802)  * @buf: boot config text
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803)  * @emsg: A pointer of const char * to store the error message
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804)  * @epos: A pointer of int to store the error position
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806)  * This parses the boot config text in @buf. @buf must be a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807)  * null terminated string and smaller than XBC_DATA_MAX.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808)  * Return the number of stored nodes (>0) if succeeded, or -errno
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809)  * if there is any error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810)  * In error cases, @emsg will be updated with an error message and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811)  * @epos will be updated with the error position which is the byte offset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812)  * of @buf. If the error is not a parser error, @epos will be -1.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) int __init xbc_init(char *buf, const char **emsg, int *epos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) 	char *p, *q;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) 	int ret, c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) 	if (epos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) 		*epos = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) 	if (xbc_data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) 		if (emsg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) 			*emsg = "Bootconfig is already initialized";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) 		return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) 	ret = strlen(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) 	if (ret > XBC_DATA_MAX - 1 || ret == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) 		if (emsg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) 			*emsg = ret ? "Config data is too big" :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) 				"Config data is empty";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) 		return -ERANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) 	xbc_nodes = memblock_alloc(sizeof(struct xbc_node) * XBC_NODE_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) 				   SMP_CACHE_BYTES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) 	if (!xbc_nodes) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) 		if (emsg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) 			*emsg = "Failed to allocate bootconfig nodes";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) 	memset(xbc_nodes, 0, sizeof(struct xbc_node) * XBC_NODE_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) 	xbc_data = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) 	xbc_data_size = ret + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) 	last_parent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) 	p = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) 		q = strpbrk(p, "{}=+;:\n#");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) 		if (!q) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) 			p = skip_spaces(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) 			if (*p != '\0')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) 				ret = xbc_parse_error("No delimiter", p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) 		c = *q;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) 		*q++ = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) 		switch (c) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) 		case ':':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) 		case '+':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) 			if (*q++ != '=') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) 				ret = xbc_parse_error(c == '+' ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) 						"Wrong '+' operator" :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) 						"Wrong ':' operator",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) 							q - 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) 			/* fall through */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) 		case '=':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) 			ret = xbc_parse_kv(&p, q, c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) 		case '{':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) 			ret = xbc_open_brace(&p, q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) 		case '#':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) 			q = skip_comment(q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) 			/* fall through */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) 		case ';':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) 		case '\n':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) 			ret = xbc_parse_key(&p, q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) 		case '}':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) 			ret = xbc_close_brace(&p, q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) 	} while (!ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) 	if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) 		ret = xbc_verify_tree();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) 		if (epos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) 			*epos = xbc_err_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) 		if (emsg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) 			*emsg = xbc_err_msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) 		xbc_destroy_all();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) 		ret = xbc_node_num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906)  * xbc_debug_dump() - Dump current XBC node list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908)  * Dump the current XBC node list on printk buffer for debug.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) void __init xbc_debug_dump(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) 	for (i = 0; i < xbc_node_num; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) 		pr_debug("[%d] %s (%s) .next=%d, .child=%d .parent=%d\n", i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) 			xbc_node_get_data(xbc_nodes + i),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) 			xbc_node_is_value(xbc_nodes + i) ? "value" : "key",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) 			xbc_nodes[i].next, xbc_nodes[i].child,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) 			xbc_nodes[i].parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) }