1 /* SPDX-License-Identifier: GPL-2.0 */ 1 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.o 8 */ 9 10 #ifdef __KERNEL__ 11 #include <linux/kernel.h> 12 #include <linux/types.h> 13 bool __init cmdline_has_extra_options(void); 14 #else /* !__KERNEL__ */ 15 /* 16 * NOTE: This is only for tools/bootconfig, be 17 * run the parser sanity test. 18 * This does NOT mean linux/bootconfig.h is av 19 * However, if you change this file, please ma 20 * has no issue on building and running. 21 */ 22 #endif 23 24 #define BOOTCONFIG_MAGIC "#BOOTCONFIG\n 25 #define BOOTCONFIG_MAGIC_LEN 12 26 #define BOOTCONFIG_ALIGN_SHIFT 2 27 #define BOOTCONFIG_ALIGN (1 << BOOTCONF 28 #define BOOTCONFIG_ALIGN_MASK (BOOTCONFIG_AL 29 30 /** 31 * xbc_calc_checksum() - Calculate checksum of 32 * @data: Bootconfig data. 33 * @size: The size of the bootconfig data. 34 * 35 * Calculate the checksum value of the bootcon 36 * The checksum will be used with the BOOTCONF 37 * embedding the bootconfig in the initrd imag 38 */ 39 static inline __init uint32_t xbc_calc_checksu 40 { 41 unsigned char *p = data; 42 uint32_t ret = 0; 43 44 while (size--) 45 ret += *p++; 46 47 return ret; 48 } 49 50 /* XBC tree node */ 51 struct xbc_node { 52 uint16_t next; 53 uint16_t child; 54 uint16_t parent; 55 uint16_t data; 56 } __attribute__ ((__packed__)); 57 58 #define XBC_KEY 0 59 #define XBC_VALUE (1 << 15) 60 /* Maximum size of boot config is 32KB - 1 */ 61 #define XBC_DATA_MAX (XBC_VALUE - 1) 62 63 #define XBC_NODE_MAX 8192 64 #define XBC_KEYLEN_MAX 256 65 #define XBC_DEPTH_MAX 16 66 67 /* Node tree access raw APIs */ 68 struct xbc_node * __init xbc_root_node(void); 69 int __init xbc_node_index(struct xbc_node *nod 70 struct xbc_node * __init xbc_node_get_parent(s 71 struct xbc_node * __init xbc_node_get_child(st 72 struct xbc_node * __init xbc_node_get_next(str 73 const char * __init xbc_node_get_data(struct x 74 75 /** 76 * xbc_node_is_value() - Test the node is a va 77 * @node: An XBC node. 78 * 79 * Test the @node is a value node and return t 80 */ 81 static inline __init bool xbc_node_is_value(st 82 { 83 return node->data & XBC_VALUE; 84 } 85 86 /** 87 * xbc_node_is_key() - Test the node is a key 88 * @node: An XBC node. 89 * 90 * Test the @node is a key node and return tru 91 */ 92 static inline __init bool xbc_node_is_key(stru 93 { 94 return !xbc_node_is_value(node); 95 } 96 97 /** 98 * xbc_node_is_array() - Test the node is an a 99 * @node: An XBC node. 100 * 101 * Test the @node is an arraied value node. 102 */ 103 static inline __init bool xbc_node_is_array(st 104 { 105 return xbc_node_is_value(node) && node 106 } 107 108 /** 109 * xbc_node_is_leaf() - Test the node is a lea 110 * @node: An XBC node. 111 * 112 * Test the @node is a leaf key node which is 113 * or no child. Returns true if it is a leaf n 114 * Note that the leaf node can have subkey nod 115 * value node. 116 */ 117 static inline __init bool xbc_node_is_leaf(str 118 { 119 return xbc_node_is_key(node) && 120 (!node->child || xbc_node_is_v 121 } 122 123 /* Tree-based key-value access APIs */ 124 struct xbc_node * __init xbc_node_find_subkey( 125 c 126 127 const char * __init xbc_node_find_value(struct 128 const 129 struct 130 131 struct xbc_node * __init xbc_node_find_next_le 132 133 134 const char * __init xbc_node_find_next_key_val 135 136 137 /** 138 * xbc_find_value() - Find a value which match 139 * @key: Search key 140 * @vnode: A container pointer of XBC value no 141 * 142 * Search a value whose key matches @key from 143 * the value if found. Found value node is sto 144 * Note that this can return 0-length string a 145 * key-only (non-value) entry. 146 */ 147 static inline const char * __init 148 xbc_find_value(const char *key, struct xbc_nod 149 { 150 return xbc_node_find_value(NULL, key, 151 } 152 153 /** 154 * xbc_find_node() - Find a node which matches 155 * @key: Search key 156 * 157 * Search a (key) node whose key matches @key 158 * return the node if found. If not found, ret 159 */ 160 static inline struct xbc_node * __init xbc_fin 161 { 162 return xbc_node_find_subkey(NULL, key) 163 } 164 165 /** 166 * xbc_node_get_subkey() - Return the first su 167 * @node: Parent node 168 * 169 * Return the first subkey node of the @node. 170 * or only value node, this will return NULL. 171 */ 172 static inline struct xbc_node * __init xbc_nod 173 { 174 struct xbc_node *child = xbc_node_get_ 175 176 if (child && xbc_node_is_value(child)) 177 return xbc_node_get_next(child 178 else 179 return child; 180 } 181 182 /** 183 * xbc_array_for_each_value() - Iterate value 184 * @anode: An XBC arraied value node 185 * @value: A value 186 * 187 * Iterate array value nodes and values starts 188 * be used with xbc_find_value() and xbc_node_ 189 * process each array entry node. 190 */ 191 #define xbc_array_for_each_value(anode, value) 192 for (value = xbc_node_get_data(anode); 193 anode = xbc_node_get_child(anode) 194 value = anode ? xbc_node_get_data 195 196 /** 197 * xbc_node_for_each_child() - Iterate child n 198 * @parent: An XBC node. 199 * @child: Iterated XBC node. 200 * 201 * Iterate child nodes of @parent. Each child 202 * The @child can be mixture of a value node a 203 */ 204 #define xbc_node_for_each_child(parent, child) 205 for (child = xbc_node_get_child(parent 206 child = xbc_node_get_next(child)) 207 208 /** 209 * xbc_node_for_each_subkey() - Iterate child 210 * @parent: An XBC node. 211 * @child: Iterated XBC node. 212 * 213 * Iterate subkey nodes of @parent. Each child 214 * The @child is only the subkey node. 215 */ 216 #define xbc_node_for_each_subkey(parent, child 217 for (child = xbc_node_get_subkey(paren 218 child = xbc_node_get_next(child)) 219 220 /** 221 * xbc_node_for_each_array_value() - Iterate a 222 * @node: An XBC node. 223 * @key: A key string searched under @node 224 * @anode: Iterated XBC node of array entry. 225 * @value: Iterated value of array entry. 226 * 227 * Iterate array entries of given @key under @ 228 * is stored to @anode and @value. If the @nod 229 * it does nothing. 230 * Note that even if the found key node has on 231 * this executes block once. However, if the f 232 * (key-only node), this does nothing. So don' 233 * key-value pair existence. 234 */ 235 #define xbc_node_for_each_array_value(node, ke 236 for (value = xbc_node_find_value(node, 237 anode = xbc_node_get_child(anode) 238 value = anode ? xbc_node_get_data 239 240 /** 241 * xbc_node_for_each_key_value() - Iterate key 242 * @node: An XBC node. 243 * @knode: Iterated key node 244 * @value: Iterated value string 245 * 246 * Iterate key-value pairs under @node. Each k 247 * stored in @knode and @value respectively. 248 */ 249 #define xbc_node_for_each_key_value(node, knod 250 for (knode = NULL, value = xbc_node_fi 251 knode != NULL; value = xbc_node_f 252 253 /** 254 * xbc_for_each_key_value() - Iterate key-valu 255 * @knode: Iterated key node 256 * @value: Iterated value string 257 * 258 * Iterate key-value pairs in whole XBC tree. 259 * are stored in @knode and @value respectivel 260 */ 261 #define xbc_for_each_key_value(knode, value) 262 xbc_node_for_each_key_value(NULL, knod 263 264 /* Compose partial key */ 265 int __init xbc_node_compose_key_after(struct x 266 struct xbc_node *node, 267 268 /** 269 * xbc_node_compose_key() - Compose full key s 270 * @node: An XBC node. 271 * @buf: A buffer to store the key. 272 * @size: The size of the @buf. 273 * 274 * Compose the full-length key of the @node in 275 * length of the key stored in @buf. Or return 276 * and -ERANGE if the key depth is deeper than 277 */ 278 static inline int __init xbc_node_compose_key( 279 280 { 281 return xbc_node_compose_key_after(NULL 282 } 283 284 /* XBC node initializer */ 285 int __init xbc_init(const char *buf, size_t si 286 287 /* XBC node and size information */ 288 int __init xbc_get_info(int *node_size, size_t 289 290 /* XBC cleanup data structures */ 291 void __init _xbc_exit(bool early); 292 293 static inline void xbc_exit(void) 294 { 295 _xbc_exit(false); 296 } 297 298 /* XBC embedded bootconfig data in kernel */ 299 #ifdef CONFIG_BOOT_CONFIG_EMBED 300 const char * __init xbc_get_embedded_bootconfi 301 #else 302 static inline const char *xbc_get_embedded_boo 303 { 304 return NULL; 305 } 306 #endif 307 308 #endif 309
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.