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