mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git
synced 2026-09-22 09:34:56 +02:00
bootconfig: move xbc_snprint_cmdline() to lib/bootconfig.c
[ Upstream commit5a643e4623] Move xbc_snprint_cmdline() from init/main.c to lib/bootconfig.c so the function (and its xbc_namebuf scratch buffer) becomes part of the shared parser library. tools/bootconfig already compiles lib/bootconfig.c directly, which lets a follow-up patch reuse the same renderer in the userspace tool to convert a bootconfig file into a flat cmdline string at build time. No functional change. Link: https://lore.kernel.org/all/20260508-bootconfig_using_tools-v1-1-1132219aa773@debian.org/ Signed-off-by: Breno Leitao <leitao@debian.org> Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org> Stable-dep-of:dec4d8118c("bootconfig: fix NULL-pointer arithmetic in xbc_snprint_cmdline()") Signed-off-by: Sasha Levin <sashal@kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
This commit is contained in:
committed by
Greg Kroah-Hartman
parent
3a4627d4cd
commit
3fb2d026fb
@@ -264,6 +264,9 @@ static inline struct xbc_node * __init xbc_node_get_subkey(struct xbc_node *node
|
||||
int __init xbc_node_compose_key_after(struct xbc_node *root,
|
||||
struct xbc_node *node, char *buf, size_t size);
|
||||
|
||||
/* Render key/value pairs under @root as a flat cmdline string */
|
||||
int __init xbc_snprint_cmdline(char *buf, size_t size, struct xbc_node *root);
|
||||
|
||||
/**
|
||||
* xbc_node_compose_key() - Compose full key string of the XBC node
|
||||
* @node: An XBC node.
|
||||
|
||||
-45
@@ -316,51 +316,6 @@ static void * __init get_boot_config_from_initrd(size_t *_size)
|
||||
|
||||
#ifdef CONFIG_BOOT_CONFIG
|
||||
|
||||
static char xbc_namebuf[XBC_KEYLEN_MAX] __initdata;
|
||||
|
||||
#define rest(dst, end) ((end) > (dst) ? (end) - (dst) : 0)
|
||||
|
||||
static int __init xbc_snprint_cmdline(char *buf, size_t size,
|
||||
struct xbc_node *root)
|
||||
{
|
||||
struct xbc_node *knode, *vnode;
|
||||
char *end = buf + size;
|
||||
const char *val, *q;
|
||||
int ret;
|
||||
|
||||
xbc_node_for_each_key_value(root, knode, val) {
|
||||
ret = xbc_node_compose_key_after(root, knode,
|
||||
xbc_namebuf, XBC_KEYLEN_MAX);
|
||||
if (ret < 0)
|
||||
return ret;
|
||||
|
||||
vnode = xbc_node_get_child(knode);
|
||||
if (!vnode) {
|
||||
ret = snprintf(buf, rest(buf, end), "%s ", xbc_namebuf);
|
||||
if (ret < 0)
|
||||
return ret;
|
||||
buf += ret;
|
||||
continue;
|
||||
}
|
||||
xbc_array_for_each_value(vnode, val) {
|
||||
/*
|
||||
* For prettier and more readable /proc/cmdline, only
|
||||
* quote the value when necessary, i.e. when it contains
|
||||
* whitespace.
|
||||
*/
|
||||
q = strpbrk(val, " \t\r\n") ? "\"" : "";
|
||||
ret = snprintf(buf, rest(buf, end), "%s=%s%s%s ",
|
||||
xbc_namebuf, q, val, q);
|
||||
if (ret < 0)
|
||||
return ret;
|
||||
buf += ret;
|
||||
}
|
||||
}
|
||||
|
||||
return buf - (end - size);
|
||||
}
|
||||
#undef rest
|
||||
|
||||
/* Make an extra command line under given key word */
|
||||
static char * __init xbc_make_cmdline(const char *key)
|
||||
{
|
||||
|
||||
@@ -407,6 +407,62 @@ const char * __init xbc_node_find_next_key_value(struct xbc_node *root,
|
||||
return ""; /* No value key */
|
||||
}
|
||||
|
||||
static char xbc_namebuf[XBC_KEYLEN_MAX] __initdata;
|
||||
|
||||
#define rest(dst, end) ((end) > (dst) ? (end) - (dst) : 0)
|
||||
|
||||
/**
|
||||
* xbc_snprint_cmdline() - Render bootconfig keys under @root as a cmdline string
|
||||
* @buf: Destination buffer (may be NULL when @size is 0 to query the length)
|
||||
* @size: Size of @buf in bytes
|
||||
* @root: Subtree root whose key=value pairs should be rendered
|
||||
*
|
||||
* Walk all key/value pairs under @root and emit them as a space-separated
|
||||
* cmdline string into @buf. Values containing whitespace are quoted with
|
||||
* double quotes. Returns the number of bytes that would be written if @buf
|
||||
* were large enough (matching snprintf semantics), or a negative errno on
|
||||
* failure.
|
||||
*/
|
||||
int __init xbc_snprint_cmdline(char *buf, size_t size, struct xbc_node *root)
|
||||
{
|
||||
struct xbc_node *knode, *vnode;
|
||||
char *end = buf + size;
|
||||
const char *val, *q;
|
||||
int ret;
|
||||
|
||||
xbc_node_for_each_key_value(root, knode, val) {
|
||||
ret = xbc_node_compose_key_after(root, knode,
|
||||
xbc_namebuf, XBC_KEYLEN_MAX);
|
||||
if (ret < 0)
|
||||
return ret;
|
||||
|
||||
vnode = xbc_node_get_child(knode);
|
||||
if (!vnode) {
|
||||
ret = snprintf(buf, rest(buf, end), "%s ", xbc_namebuf);
|
||||
if (ret < 0)
|
||||
return ret;
|
||||
buf += ret;
|
||||
continue;
|
||||
}
|
||||
xbc_array_for_each_value(vnode, val) {
|
||||
/*
|
||||
* For prettier and more readable /proc/cmdline, only
|
||||
* quote the value when necessary, i.e. when it contains
|
||||
* whitespace.
|
||||
*/
|
||||
q = strpbrk(val, " \t\r\n") ? "\"" : "";
|
||||
ret = snprintf(buf, rest(buf, end), "%s=%s%s%s ",
|
||||
xbc_namebuf, q, val, q);
|
||||
if (ret < 0)
|
||||
return ret;
|
||||
buf += ret;
|
||||
}
|
||||
}
|
||||
|
||||
return buf - (end - size);
|
||||
}
|
||||
#undef rest
|
||||
|
||||
/* XBC parse and tree build */
|
||||
|
||||
static int __init xbc_init_node(struct xbc_node *node, char *data, uint32_t flag)
|
||||
|
||||
Reference in New Issue
Block a user