cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

bootconfig.h (9383B)


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