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