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