Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef _LINUX_XBC_H
0003 #define _LINUX_XBC_H
0004 /*
0005  * Extra Boot Config
0006  * Copyright (C) 2019 Linaro Ltd.
0007  * Author: Masami Hiramatsu <mhiramat@kernel.org>
0008  */
0009 
0010 #ifdef __KERNEL__
0011 #include <linux/kernel.h>
0012 #include <linux/types.h>
0013 #else /* !__KERNEL__ */
0014 /*
0015  * NOTE: This is only for tools/bootconfig, because tools/bootconfig will
0016  * run the parser sanity test.
0017  * This does NOT mean linux/bootconfig.h is available in the user space.
0018  * However, if you change this file, please make sure the tools/bootconfig
0019  * has no issue on building and running.
0020  */
0021 #endif
0022 
0023 #define BOOTCONFIG_MAGIC    "#BOOTCONFIG\n"
0024 #define BOOTCONFIG_MAGIC_LEN    12
0025 #define BOOTCONFIG_ALIGN_SHIFT  2
0026 #define BOOTCONFIG_ALIGN    (1 << BOOTCONFIG_ALIGN_SHIFT)
0027 #define BOOTCONFIG_ALIGN_MASK   (BOOTCONFIG_ALIGN - 1)
0028 
0029 /**
0030  * xbc_calc_checksum() - Calculate checksum of bootconfig
0031  * @data: Bootconfig data.
0032  * @size: The size of the bootconfig data.
0033  *
0034  * Calculate the checksum value of the bootconfig data.
0035  * The checksum will be used with the BOOTCONFIG_MAGIC and the size for
0036  * embedding the bootconfig in the initrd image.
0037  */
0038 static inline __init uint32_t xbc_calc_checksum(void *data, uint32_t size)
0039 {
0040     unsigned char *p = data;
0041     uint32_t ret = 0;
0042 
0043     while (size--)
0044         ret += *p++;
0045 
0046     return ret;
0047 }
0048 
0049 /* XBC tree node */
0050 struct xbc_node {
0051     uint16_t next;
0052     uint16_t child;
0053     uint16_t parent;
0054     uint16_t data;
0055 } __attribute__ ((__packed__));
0056 
0057 #define XBC_KEY     0
0058 #define XBC_VALUE   (1 << 15)
0059 /* Maximum size of boot config is 32KB - 1 */
0060 #define XBC_DATA_MAX    (XBC_VALUE - 1)
0061 
0062 #define XBC_NODE_MAX    1024
0063 #define XBC_KEYLEN_MAX  256
0064 #define XBC_DEPTH_MAX   16
0065 
0066 /* Node tree access raw APIs */
0067 struct xbc_node * __init xbc_root_node(void);
0068 int __init xbc_node_index(struct xbc_node *node);
0069 struct xbc_node * __init xbc_node_get_parent(struct xbc_node *node);
0070 struct xbc_node * __init xbc_node_get_child(struct xbc_node *node);
0071 struct xbc_node * __init xbc_node_get_next(struct xbc_node *node);
0072 const char * __init xbc_node_get_data(struct xbc_node *node);
0073 
0074 /**
0075  * xbc_node_is_value() - Test the node is a value node
0076  * @node: An XBC node.
0077  *
0078  * Test the @node is a value node and return true if a value node, false if not.
0079  */
0080 static inline __init bool xbc_node_is_value(struct xbc_node *node)
0081 {
0082     return node->data & XBC_VALUE;
0083 }
0084 
0085 /**
0086  * xbc_node_is_key() - Test the node is a key node
0087  * @node: An XBC node.
0088  *
0089  * Test the @node is a key node and return true if a key node, false if not.
0090  */
0091 static inline __init bool xbc_node_is_key(struct xbc_node *node)
0092 {
0093     return !xbc_node_is_value(node);
0094 }
0095 
0096 /**
0097  * xbc_node_is_array() - Test the node is an arraied value node
0098  * @node: An XBC node.
0099  *
0100  * Test the @node is an arraied value node.
0101  */
0102 static inline __init bool xbc_node_is_array(struct xbc_node *node)
0103 {
0104     return xbc_node_is_value(node) && node->child != 0;
0105 }
0106 
0107 /**
0108  * xbc_node_is_leaf() - Test the node is a leaf key node
0109  * @node: An XBC node.
0110  *
0111  * Test the @node is a leaf key node which is a key node and has a value node
0112  * or no child. Returns true if it is a leaf node, or false if not.
0113  * Note that the leaf node can have subkey nodes in addition to the
0114  * value node.
0115  */
0116 static inline __init bool xbc_node_is_leaf(struct xbc_node *node)
0117 {
0118     return xbc_node_is_key(node) &&
0119         (!node->child || xbc_node_is_value(xbc_node_get_child(node)));
0120 }
0121 
0122 /* Tree-based key-value access APIs */
0123 struct xbc_node * __init xbc_node_find_subkey(struct xbc_node *parent,
0124                          const char *key);
0125 
0126 const char * __init xbc_node_find_value(struct xbc_node *parent,
0127                     const char *key,
0128                     struct xbc_node **vnode);
0129 
0130 struct xbc_node * __init xbc_node_find_next_leaf(struct xbc_node *root,
0131                          struct xbc_node *leaf);
0132 
0133 const char * __init xbc_node_find_next_key_value(struct xbc_node *root,
0134                          struct xbc_node **leaf);
0135 
0136 /**
0137  * xbc_find_value() - Find a value which matches the key
0138  * @key: Search key
0139  * @vnode: A container pointer of XBC value node.
0140  *
0141  * Search a value whose key matches @key from whole of XBC tree and return
0142  * the value if found. Found value node is stored in *@vnode.
0143  * Note that this can return 0-length string and store NULL in *@vnode for
0144  * key-only (non-value) entry.
0145  */
0146 static inline const char * __init
0147 xbc_find_value(const char *key, struct xbc_node **vnode)
0148 {
0149     return xbc_node_find_value(NULL, key, vnode);
0150 }
0151 
0152 /**
0153  * xbc_find_node() - Find a node which matches the key
0154  * @key: Search key
0155  *
0156  * Search a (key) node whose key matches @key from whole of XBC tree and
0157  * return the node if found. If not found, returns NULL.
0158  */
0159 static inline struct xbc_node * __init xbc_find_node(const char *key)
0160 {
0161     return xbc_node_find_subkey(NULL, key);
0162 }
0163 
0164 /**
0165  * xbc_node_get_subkey() - Return the first subkey node if exists
0166  * @node: Parent node
0167  *
0168  * Return the first subkey node of the @node. If the @node has no child
0169  * or only value node, this will return NULL.
0170  */
0171 static inline struct xbc_node * __init xbc_node_get_subkey(struct xbc_node *node)
0172 {
0173     struct xbc_node *child = xbc_node_get_child(node);
0174 
0175     if (child && xbc_node_is_value(child))
0176         return xbc_node_get_next(child);
0177     else
0178         return child;
0179 }
0180 
0181 /**
0182  * xbc_array_for_each_value() - Iterate value nodes on an array
0183  * @anode: An XBC arraied value node
0184  * @value: A value
0185  *
0186  * Iterate array value nodes and values starts from @anode. This is expected to
0187  * be used with xbc_find_value() and xbc_node_find_value(), so that user can
0188  * process each array entry node.
0189  */
0190 #define xbc_array_for_each_value(anode, value)              \
0191     for (value = xbc_node_get_data(anode); anode != NULL ;      \
0192          anode = xbc_node_get_child(anode),             \
0193          value = anode ? xbc_node_get_data(anode) : NULL)
0194 
0195 /**
0196  * xbc_node_for_each_child() - Iterate child nodes
0197  * @parent: An XBC node.
0198  * @child: Iterated XBC node.
0199  *
0200  * Iterate child nodes of @parent. Each child nodes are stored to @child.
0201  * The @child can be mixture of a value node and subkey nodes.
0202  */
0203 #define xbc_node_for_each_child(parent, child)              \
0204     for (child = xbc_node_get_child(parent); child != NULL ;    \
0205          child = xbc_node_get_next(child))
0206 
0207 /**
0208  * xbc_node_for_each_subkey() - Iterate child subkey nodes
0209  * @parent: An XBC node.
0210  * @child: Iterated XBC node.
0211  *
0212  * Iterate subkey nodes of @parent. Each child nodes are stored to @child.
0213  * The @child is only the subkey node.
0214  */
0215 #define xbc_node_for_each_subkey(parent, child)             \
0216     for (child = xbc_node_get_subkey(parent); child != NULL ;   \
0217          child = xbc_node_get_next(child))
0218 
0219 /**
0220  * xbc_node_for_each_array_value() - Iterate array entries of geven key
0221  * @node: An XBC node.
0222  * @key: A key string searched under @node
0223  * @anode: Iterated XBC node of array entry.
0224  * @value: Iterated value of array entry.
0225  *
0226  * Iterate array entries of given @key under @node. Each array entry node
0227  * is stored to @anode and @value. If the @node doesn't have @key node,
0228  * it does nothing.
0229  * Note that even if the found key node has only one value (not array)
0230  * this executes block once. However, if the found key node has no value
0231  * (key-only node), this does nothing. So don't use this for testing the
0232  * key-value pair existence.
0233  */
0234 #define xbc_node_for_each_array_value(node, key, anode, value)      \
0235     for (value = xbc_node_find_value(node, key, &anode); value != NULL; \
0236          anode = xbc_node_get_child(anode),             \
0237          value = anode ? xbc_node_get_data(anode) : NULL)
0238 
0239 /**
0240  * xbc_node_for_each_key_value() - Iterate key-value pairs under a node
0241  * @node: An XBC node.
0242  * @knode: Iterated key node
0243  * @value: Iterated value string
0244  *
0245  * Iterate key-value pairs under @node. Each key node and value string are
0246  * stored in @knode and @value respectively.
0247  */
0248 #define xbc_node_for_each_key_value(node, knode, value)         \
0249     for (knode = NULL, value = xbc_node_find_next_key_value(node, &knode);\
0250          knode != NULL; value = xbc_node_find_next_key_value(node, &knode))
0251 
0252 /**
0253  * xbc_for_each_key_value() - Iterate key-value pairs
0254  * @knode: Iterated key node
0255  * @value: Iterated value string
0256  *
0257  * Iterate key-value pairs in whole XBC tree. Each key node and value string
0258  * are stored in @knode and @value respectively.
0259  */
0260 #define xbc_for_each_key_value(knode, value)                \
0261     xbc_node_for_each_key_value(NULL, knode, value)
0262 
0263 /* Compose partial key */
0264 int __init xbc_node_compose_key_after(struct xbc_node *root,
0265             struct xbc_node *node, char *buf, size_t size);
0266 
0267 /**
0268  * xbc_node_compose_key() - Compose full key string of the XBC node
0269  * @node: An XBC node.
0270  * @buf: A buffer to store the key.
0271  * @size: The size of the @buf.
0272  *
0273  * Compose the full-length key of the @node into @buf. Returns the total
0274  * length of the key stored in @buf. Or returns -EINVAL if @node is NULL,
0275  * and -ERANGE if the key depth is deeper than max depth.
0276  */
0277 static inline int __init xbc_node_compose_key(struct xbc_node *node,
0278                           char *buf, size_t size)
0279 {
0280     return xbc_node_compose_key_after(NULL, node, buf, size);
0281 }
0282 
0283 /* XBC node initializer */
0284 int __init xbc_init(const char *buf, size_t size, const char **emsg, int *epos);
0285 
0286 /* XBC node and size information */
0287 int __init xbc_get_info(int *node_size, size_t *data_size);
0288 
0289 /* XBC cleanup data structures */
0290 void __init xbc_exit(void);
0291 
0292 /* XBC embedded bootconfig data in kernel */
0293 #ifdef CONFIG_BOOT_CONFIG_EMBED
0294 const char * __init xbc_get_embedded_bootconfig(size_t *size);
0295 #else
0296 static inline const char *xbc_get_embedded_bootconfig(size_t *size)
0297 {
0298     return NULL;
0299 }
0300 #endif
0301 
0302 #endif