Skip to contentSkip to conversation

bootconfig: Add mixed subkeys and value under the same key

PATCH Superseded Masami Hiramatsu 1 participant 8 messages Jun 2, 2021

Subsystems (3)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Masami Hiramatsu

author maintainer
#0 lore raw

v3 cover bootconfig: Add mixed subkeys and value under the same key

Hi,

Here is the 3rd version of the series which updates bootconfig to support mixed subkeys and a value under the same key.

Since the kernel cmdline accepts options like "aaa.bbb=val1 aaa.bbb.ccc=val2", it is better that the bootconfig also support it.

Here is the previous series:
https://lore.kernel.org/lkml/162157886618.78209.11141970623539574861.stgit@devnote2/

In this version, I rebased on top of the latest linus tree and add the build error fix [1/6](*) and a cleanup patch [6/6].

(*) https://lore.kernel.org/lkml/162087519356.442660.11385099982318160180.stgit@devnote2/

Changes in v3
[1/6]:
- Added from separated thread. This is a fundamental fix.
[6/6]:
- New cleanup patch.

With this series, sub-keys and a value can co-exist under a parent key. For example, following config is allowed.

foo = value1
foo.bar = value2

Note, since there is no syntax to put a raw value directly under a structured key, you have to define it outside of the brace. For example,

foo {
bar = value1
bar {
baz = value2
qux = value3
}
}

Also, the order of the value node under a key is fixed. If there are a value and subkeys, the value is always the first child node of the key. Thus if user specifies subkeys first, e.g.

foo.bar = value1
foo = value2

In the program (and /proc/bootconfig), it will be shown as below

foo = value2
foo.bar = value1

Thank you,

---

Masami Hiramatsu (6):
tools/bootconfig: Fix a build error accroding to undefined fallthrough
bootconfig: Change array value to use child node
bootconfig: Support mixing a value and subkeys under a key tools/bootconfig: Support mixed value and subkey test cases
docs: bootconfig: Update for mixing value and subkeys
bootconfig: Share the checksum function with tools

 tools/bootconfig/include/linux/bootconfig.h        |    4 +
 tools/bootconfig/main.c                            |   62 +++++++++++++-------
 tools/bootconfig/samples/bad-mixed-kv1.bconf       |    3 -
 tools/bootconfig/samples/bad-mixed-kv2.bconf       |    3 -
 tools/bootconfig/samples/bad-override.bconf        |    3 -
 tools/bootconfig/samples/bad-override2.bconf       |    3 -
 tools/bootconfig/samples/good-mixed-append.bconf   |    4 +
 tools/bootconfig/samples/good-mixed-kv1.bconf      |    3 +
 tools/bootconfig/samples/good-mixed-kv2.bconf      |    3 +
 tools/bootconfig/samples/good-mixed-kv3.bconf      |    6 ++
 tools/bootconfig/samples/good-mixed-override.bconf |    4 +
 11 files changed, 64 insertions(+), 34 deletions(-)
 delete mode 100644 tools/bootconfig/samples/bad-mixed-kv1.bconf
 delete mode 100644 tools/bootconfig/samples/bad-mixed-kv2.bconf
 delete mode 100644 tools/bootconfig/samples/bad-override.bconf
 delete mode 100644 tools/bootconfig/samples/bad-override2.bconf
 create mode 100644 tools/bootconfig/samples/good-mixed-append.bconf
 create mode 100644 tools/bootconfig/samples/good-mixed-kv1.bconf
 create mode 100644 tools/bootconfig/samples/good-mixed-kv2.bconf
 create mode 100644 tools/bootconfig/samples/good-mixed-kv3.bconf
 create mode 100644 tools/bootconfig/samples/good-mixed-override.bconf

--
Masami Hiramatsu (Linaro) <[email protected]>

author maintainer
#1 lore raw

PATCH v3 1/6 tools/bootconfig: Fix a build error accroding to undefined fallthrough

Since the "fallthrough" is defined only in the kernel, building lib/bootconfig.c as a part of user-space tools causes a build error.

Add a dummy fallthrough to avoid the build error.

Fixes 4c1ca831adb1 ("Revert "lib: Revert use of fallthrough pseudo-keyword in lib/"")
---
 tools/bootconfig/include/linux/bootconfig.h |    4 ++++
 1 file changed, 4 insertions(+)
tools/bootconfig/include/linux/bootconfig.h +4 -0
index 078cbd2ba651..de7f30f99af3 100644
@@ -4,4 +4,8 @@
 
 #include "../../../../include/linux/bootconfig.h"
 
+#ifndef fallthrough
+# define fallthrough
+#endif
+
 #endif
author maintainer
#2 lore raw

PATCH v3 2/6 bootconfig: Change array value to use child node

In reply to Masami Hiramatsu

Change array value to use child node of the xbc_node tree.

---
 tools/bootconfig/main.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/bootconfig.h +3 -3
index 2696eb0fc149..3178a31fdabc 100644
@@ -71,7 +71,7 @@ static inline __init bool xbc_node_is_key(struct xbc_node *node)
  */
 static inline __init bool xbc_node_is_array(struct xbc_node *node)
 {
-	return xbc_node_is_value(node) && node->next != 0;
+	return xbc_node_is_value(node) && node->child != 0;
 }
 
 /**
@@ -140,7 +140,7 @@ static inline struct xbc_node * __init xbc_find_node(const char *key)
  */
 #define xbc_array_for_each_value(anode, value)				\
 	for (value = xbc_node_get_data(anode); anode != NULL ;		\
-	     anode = xbc_node_get_next(anode),				\
+	     anode = xbc_node_get_child(anode),				\
 	     value = anode ? xbc_node_get_data(anode) : NULL)
 
 /**
@@ -171,7 +171,7 @@ static inline struct xbc_node * __init xbc_find_node(const char *key)
  */
 #define xbc_node_for_each_array_value(node, key, anode, value)		\
 	for (value = xbc_node_find_value(node, key, &anode); value != NULL; \
-	     anode = xbc_node_get_next(anode),				\
+	     anode = xbc_node_get_child(anode),				\
 	     value = anode ? xbc_node_get_data(anode) : NULL)
 
 /**
lib/bootconfig.c +18 -5
index 9f8c70a98fcf..87d21fe25010 100644
@@ -367,6 +367,14 @@ static inline __init struct xbc_node *xbc_last_sibling(struct xbc_node *node)
 	return node;
 }
 
+static inline __init struct xbc_node *xbc_last_child(struct xbc_node *node)
+{
+	while (node->child)
+		node = xbc_node_get_child(node);
+
+	return node;
+}
+
 static struct xbc_node * __init xbc_add_sibling(char *data, u32 flag)
 {
 	struct xbc_node *sib, *node = xbc_add_node(data, flag);
@@ -513,21 +521,22 @@ static int __init __xbc_parse_value(char **__v, char **__n)
 
 static int __init xbc_parse_array(char **__v)
 {
-	struct xbc_node *node;
+	struct xbc_node *node, *knode = last_parent;
 	char *next;
 	int c = 0;
 
+	last_parent = xbc_node_get_child(knode);
 	do {
 		c = __xbc_parse_value(__v, &next);
 		if (c < 0)
 			return c;
 
-		node = xbc_add_sibling(*__v, XBC_VALUE);
+		node = xbc_add_child(*__v, XBC_VALUE);
 		if (!node)
 			return -ENOMEM;
 		*__v = next;
 	} while (c == ',');
-	node->next = 0;
+	node->child = 0;
 
 	return c;
 }
@@ -615,8 +624,12 @@ static int __init xbc_parse_kv(char **k, char *v, int op)
 
 	if (op == ':' && child) {
 		xbc_init_node(child, v, XBC_VALUE);
-	} else if (!xbc_add_sibling(v, XBC_VALUE))
-		return -ENOMEM;
+	} else {
+		if (op == '+' && child)
+			last_parent = xbc_last_child(child);
+		if (!xbc_add_sibling(v, XBC_VALUE))
+			return -ENOMEM;
+	}
 
 	if (c == ',') {	/* Array */
 		c = xbc_parse_array(&next);
tools/bootconfig/main.c +1 -1
index 7362bef1a368..aaa4fec1c431 100644
@@ -27,7 +27,7 @@ static int xbc_show_value(struct xbc_node *node, bool semicolon)
 			q = '\'';
 		else
 			q = '"';
-		printf("%c%s%c%s", q, val, q, node->next ? ", " : eol);
+		printf("%c%s%c%s", q, val, q, node->child ? ", " : eol);
 		i++;
 	}
 	return i;
author maintainer
#3 lore raw

PATCH v3 3/6 bootconfig: Support mixing a value and subkeys under a key

In reply to Masami Hiramatsu

Support mixing a value and subkeys under a key. Since kernel cmdline options will support "aaa.bbb=value1 aaa.bbb.ccc=value2", it is better that the bootconfig supports such configuration too.

Note that this does not change syntax itself but just accepts mixed value and subkeys e.g.

key = value1
key.subkey = value2

But this is not accepted;

key {
value1
subkey = value2
}

That will make value1 as a subkey.

Also, the order of the value node under a key is fixed. If there are a value and subkeys, the value is always the first child node of the key. Thus if user specifies subkeys first, e.g.

key.subkey = value1
key = value2

In the program (and /proc/bootconfig), it will be shown as below

key = value2
key.subkey = value1

---
 tools/bootconfig/main.c |   45 +++++++++++++++++++++++++++++++++++++--------
 1 file changed, 37 insertions(+), 8 deletions(-)
include/linux/bootconfig.h +32 -0
index 3178a31fdabc..e49043ac77c9 100644
@@ -80,6 +80,8 @@ static inline __init bool xbc_node_is_array(struct xbc_node *node)
  *
  * Test the @node is a leaf key node which is a key node and has a value node
  * or no child. Returns true if it is a leaf node, or false if not.
+ * Note that the leaf node can have subkey nodes in addition to the
+ * value node.
  */
 static inline __init bool xbc_node_is_leaf(struct xbc_node *node)
 {
@@ -129,6 +131,23 @@ static inline struct xbc_node * __init xbc_find_node(const char *key)
 	return xbc_node_find_child(NULL, key);
 }
 
+/**
+ * xbc_node_get_subkey() - Return the first subkey node if exists
+ * @node: Parent node
+ *
+ * Return the first subkey node of the @node. If the @node has no child
+ * or only value node, this will return NULL.
+ */
+static inline struct xbc_node * __init xbc_node_get_subkey(struct xbc_node *node)
+{
+	struct xbc_node *child = xbc_node_get_child(node);
+
+	if (child && xbc_node_is_value(child))
+		return xbc_node_get_next(child);
+	else
+		return child;
+}
+
 /**
  * xbc_array_for_each_value() - Iterate value nodes on an array
  * @anode: An XBC arraied value node
@@ -149,11 +168,24 @@ static inline struct xbc_node * __init xbc_find_node(const char *key)
  * @child: Iterated XBC node.
  *
  * Iterate child nodes of @parent. Each child nodes are stored to @child.
+ * The @child can be mixture of a value node and subkey nodes.
  */
 #define xbc_node_for_each_child(parent, child)				\
 	for (child = xbc_node_get_child(parent); child != NULL ;	\
 	     child = xbc_node_get_next(child))
 
+/**
+ * xbc_node_for_each_subkey() - Iterate child subkey nodes
+ * @parent: An XBC node.
+ * @child: Iterated XBC node.
+ *
+ * Iterate subkey nodes of @parent. Each child nodes are stored to @child.
+ * The @child is only the subkey node.
+ */
+#define xbc_node_for_each_subkey(parent, child)				\
+	for (child = xbc_node_get_subkey(parent); child != NULL ;	\
+	     child = xbc_node_get_next(child))
+
 /**
  * xbc_node_for_each_array_value() - Iterate array entries of geven key
  * @node: An XBC node.
lib/bootconfig.c +45 -20

136 lines not on this page · raw message

tools/bootconfig/main.c +37 -8

85 lines not on this page · raw message

author maintainer
#4 lore raw

PATCH v3 4/6 tools/bootconfig: Support mixed value and subkey test cases

In reply to Masami Hiramatsu
---
 tools/bootconfig/samples/bad-mixed-kv1.bconf       |    3 ---
 tools/bootconfig/samples/bad-mixed-kv2.bconf       |    3 ---
 tools/bootconfig/samples/bad-override.bconf        |    3 ---
 tools/bootconfig/samples/bad-override2.bconf       |    3 ---
 tools/bootconfig/samples/good-mixed-append.bconf   |    4 ++++
 tools/bootconfig/samples/good-mixed-kv1.bconf      |    3 +++
 tools/bootconfig/samples/good-mixed-kv2.bconf      |    3 +++
 tools/bootconfig/samples/good-mixed-kv3.bconf      |    6 ++++++
 tools/bootconfig/samples/good-mixed-override.bconf |    4 ++++
 9 files changed, 20 insertions(+), 12 deletions(-)
 delete mode 100644 tools/bootconfig/samples/bad-mixed-kv1.bconf
 delete mode 100644 tools/bootconfig/samples/bad-mixed-kv2.bconf
 delete mode 100644 tools/bootconfig/samples/bad-override.bconf
 delete mode 100644 tools/bootconfig/samples/bad-override2.bconf
 create mode 100644 tools/bootconfig/samples/good-mixed-append.bconf
 create mode 100644 tools/bootconfig/samples/good-mixed-kv1.bconf
 create mode 100644 tools/bootconfig/samples/good-mixed-kv2.bconf
 create mode 100644 tools/bootconfig/samples/good-mixed-kv3.bconf
 create mode 100644 tools/bootconfig/samples/good-mixed-override.bconf
tools/bootconfig/samples/bad-mixed-kv1.bconf deleted +0 -3
deleted file mode 100644
index 1761547dd05c..000000000000
@@ -1,3 +0,0 @@
-# value -> subkey pattern
-key = value
-key.subkey = another-value
tools/bootconfig/samples/bad-mixed-kv2.bconf deleted +0 -3
deleted file mode 100644
index 6b32e0c3878c..000000000000
@@ -1,3 +0,0 @@
-# subkey -> value pattern
-key.subkey = value
-key = another-value
tools/bootconfig/samples/bad-override.bconf deleted +0 -3
deleted file mode 100644
index fde6c561512e..000000000000
@@ -1,3 +0,0 @@
-key.subkey = value
-# We can not override pre-defined subkeys with value
-key := value
tools/bootconfig/samples/bad-override2.bconf deleted +0 -3
deleted file mode 100644
index 688587cb023c..000000000000
@@ -1,3 +0,0 @@
-key = value
-# We can not override pre-defined value with subkey
-key.subkey := value
tools/bootconfig/samples/good-mixed-append.bconf new file +4 -0
new file mode 100644
index 000000000000..b99a089a05f5
@@ -0,0 +1,4 @@
+key = foo
+keyx.subkey = value
+key += bar
+
tools/bootconfig/samples/good-mixed-kv1.bconf new file +3 -0
new file mode 100644
index 000000000000..1761547dd05c
@@ -0,0 +1,3 @@
+# value -> subkey pattern
+key = value
+key.subkey = another-value
tools/bootconfig/samples/good-mixed-kv2.bconf new file +3 -0
new file mode 100644
index 000000000000..6b32e0c3878c
@@ -0,0 +1,3 @@
+# subkey -> value pattern
+key.subkey = value
+key = another-value
tools/bootconfig/samples/good-mixed-kv3.bconf new file +6 -0
new file mode 100644
index 000000000000..2ce2b02224b8
@@ -0,0 +1,6 @@
+# mixed key and subkeys with braces
+key = value
+key {
+	subkey1
+	subkey2 = foo
+}
tools/bootconfig/samples/good-mixed-override.bconf new file +4 -0
new file mode 100644
index 000000000000..18195b2873b6
@@ -0,0 +1,4 @@
+key.foo = bar
+key = value
+# mixed key value can be overridden
+key := value2
author maintainer
#5 lore raw

PATCH v3 5/6 docs: bootconfig: Update for mixing value and subkeys

In reply to Masami Hiramatsu

Update document for the mixing value and subkeys on a key.

---

 0 files changed
Documentation/admin-guide/bootconfig.rst +24 -4
index 452b7dcd7f6b..37bdae273b18 100644
@@ -89,13 +89,33 @@ you can use ``+=`` operator. For example::
 
 In this case, the key ``foo`` has ``bar``, ``baz`` and ``qux``.
 
-However, a sub-key and a value can not co-exist under a parent key.
-For example, following config is NOT allowed.::
+Moreover, sub-keys and a value can co-exist under a parent key.
+For example, following config is allowed.::
 
  foo = value1
- foo.bar = value2 # !ERROR! subkey "bar" and value "value1" can NOT co-exist
- foo.bar := value2 # !ERROR! even with the override operator, this is NOT allowed.
+ foo.bar = value2
+ foo := value3 # This will update foo's value.
 
+Note, since there is no syntax to add raw value in the structured
+key, you have to define it outside of the brace. For example::
+
+ foo.bar = value1
+ foo.bar {
+         baz = value2
+         qux = value3
+ }
+
+Also, the order of the value node under a key is fixed. If there
+are a value and subkeys, the value is always the first child node
+of the key. Thus if user specifies subkeys first, e.g.::
+
+ foo.bar = value1
+ foo = value2
+
+In the program (and /proc/bootconfig), it will be shown as below::
+
+ foo = value2
+ foo.bar = value1
 
 Comments
 --------
author maintainer
#6 lore raw

PATCH v3 6/6 bootconfig: Share the checksum function with tools

In reply to Masami Hiramatsu

Move the checksum calculation function into the header for sharing it with tools/bootconfig.

---
 tools/bootconfig/main.c |   15 ++-------------
 1 file changed, 2 insertions(+), 13 deletions(-)
include/linux/bootconfig.h +20 -0
index e49043ac77c9..6bdd94cff4e2 100644
@@ -16,6 +16,26 @@
 #define BOOTCONFIG_ALIGN	(1 << BOOTCONFIG_ALIGN_SHIFT)
 #define BOOTCONFIG_ALIGN_MASK	(BOOTCONFIG_ALIGN - 1)
 
+/**
+ * xbc_calc_checksum() - Calculate checksum of bootconfig
+ * @data: Bootconfig data.
+ * @size: The size of the bootconfig data.
+ *
+ * Calculate the checksum value of the bootconfig data.
+ * The checksum will be used with the BOOTCONFIG_MAGIC and the size for
+ * embedding the bootconfig in the initrd image.
+ */
+static inline __init u32 xbc_calc_checksum(void *data, u32 size)
+{
+	unsigned char *p = data;
+	u32 ret = 0;
+
+	while (size--)
+		ret += *p++;
+
+	return ret;
+}
+
 /* XBC tree node */
 struct xbc_node {
 	u16 next;
init/main.c +1 -11
index eb01e121d2f1..43914e675421 100644
@@ -386,16 +386,6 @@ static char * __init xbc_make_cmdline(const char *key)
 	return new_cmdline;
 }
 
-static u32 boot_config_checksum(unsigned char *p, u32 size)
-{
-	u32 ret = 0;
-
-	while (size--)
-		ret += *p++;
-
-	return ret;
-}
-
 static int __init bootconfig_params(char *param, char *val,
 				    const char *unused, void *arg)
 {
@@ -439,7 +429,7 @@ static void __init setup_boot_config(void)
 		return;
 	}
 
-	if (boot_config_checksum((unsigned char *)data, size) != csum) {
+	if (xbc_calc_checksum(data, size) != csum) {
 		pr_err("bootconfig checksum failed\n");
 		return;
 	}
tools/bootconfig/main.c +2 -13
index 5441b254eb7c..9e2c6eedacb1 100644
@@ -128,17 +128,6 @@ static void xbc_show_list(void)
 	}
 }
 
-/* Simple real checksum */
-static int checksum(unsigned char *buf, int len)
-{
-	int i, sum = 0;
-
-	for (i = 0; i < len; i++)
-		sum += buf[i];
-
-	return sum;
-}
-
 #define PAGE_SIZE	4096
 
 static int load_xbc_fd(int fd, char **buf, int size)
@@ -234,7 +223,7 @@ static int load_xbc_from_initrd(int fd, char **buf)
 		return ret;
 
 	/* Wrong Checksum */
-	rcsum = checksum((unsigned char *)*buf, size);
+	rcsum = xbc_calc_checksum(*buf, size);
 	if (csum != rcsum) {
 		pr_err("checksum error: %d != %d\n", csum, rcsum);
 		return -EINVAL;
@@ -383,7 +372,7 @@ static int apply_xbc(const char *path, const char *xbc_path)
 		return ret;
 	}
 	size = strlen(buf) + 1;
-	csum = checksum((unsigned char *)buf, size);
+	csum = xbc_calc_checksum(buf, size);
 
 	/* Backup the bootconfig data */
 	data = calloc(size + BOOTCONFIG_ALIGN +
author maintainer
#7 lore raw
In reply to Masami Hiramatsu

Hi,

Sorry, I missed to use the old series. Let me update it.

Quoted 91 lines · On Wed, 2 Jun 2021 15:24:22 +0900 Masami Hiramatsu <[email protected]> wrote:

Hi,

Here is the 3rd version of the series which updates bootconfig to support mixed subkeys and a value under the same key.

Since the kernel cmdline accepts options like "aaa.bbb=val1 aaa.bbb.ccc=val2", it is better that the bootconfig also support it.

Here is the previous series:
https://lore.kernel.org/lkml/162157886618.78209.11141970623539574861.stgit@devnote2/

In this version, I rebased on top of the latest linus tree and add the build error fix [1/6](*) and a cleanup patch [6/6].

(*) https://lore.kernel.org/lkml/162087519356.442660.11385099982318160180.stgit@devnote2/

Changes in v3
[1/6]:
- Added from separated thread. This is a fundamental fix.
[6/6]:
- New cleanup patch.

With this series, sub-keys and a value can co-exist under a parent key. For example, following config is allowed.

foo = value1
foo.bar = value2

Note, since there is no syntax to put a raw value directly under a structured key, you have to define it outside of the brace. For example,

foo {
bar = value1
bar {
baz = value2
qux = value3
}
}

Also, the order of the value node under a key is fixed. If there are a value and subkeys, the value is always the first child node of the key. Thus if user specifies subkeys first, e.g.

foo.bar = value1
foo = value2

In the program (and /proc/bootconfig), it will be shown as below

foo = value2
foo.bar = value1

Thank you,

---

Masami Hiramatsu (6):
tools/bootconfig: Fix a build error accroding to undefined fallthrough
bootconfig: Change array value to use child node
bootconfig: Support mixing a value and subkeys under a key tools/bootconfig: Support mixed value and subkey test cases
docs: bootconfig: Update for mixing value and subkeys
bootconfig: Share the checksum function with tools

 tools/bootconfig/include/linux/bootconfig.h        |    4 +
 tools/bootconfig/main.c                            |   62 +++++++++++++-------
 tools/bootconfig/samples/bad-mixed-kv1.bconf       |    3 -
 tools/bootconfig/samples/bad-mixed-kv2.bconf       |    3 -
 tools/bootconfig/samples/bad-override.bconf        |    3 -
 tools/bootconfig/samples/bad-override2.bconf       |    3 -
 tools/bootconfig/samples/good-mixed-append.bconf   |    4 +
 tools/bootconfig/samples/good-mixed-kv1.bconf      |    3 +
 tools/bootconfig/samples/good-mixed-kv2.bconf      |    3 +
 tools/bootconfig/samples/good-mixed-kv3.bconf      |    6 ++
 tools/bootconfig/samples/good-mixed-override.bconf |    4 +
 11 files changed, 64 insertions(+), 34 deletions(-)
 delete mode 100644 tools/bootconfig/samples/bad-mixed-kv1.bconf
 delete mode 100644 tools/bootconfig/samples/bad-mixed-kv2.bconf
 delete mode 100644 tools/bootconfig/samples/bad-override.bconf
 delete mode 100644 tools/bootconfig/samples/bad-override2.bconf
 create mode 100644 tools/bootconfig/samples/good-mixed-append.bconf
 create mode 100644 tools/bootconfig/samples/good-mixed-kv1.bconf
 create mode 100644 tools/bootconfig/samples/good-mixed-kv2.bconf
 create mode 100644 tools/bootconfig/samples/good-mixed-kv3.bconf
 create mode 100644 tools/bootconfig/samples/good-mixed-override.bconf

--
Masami Hiramatsu (Linaro) <[email protected]>

Signature · 1 line
Masami Hiramatsu <[email protected]>