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