Skip to content

Commit 3fb2d02

Browse files
leitaogregkh
authored andcommitted
bootconfig: move xbc_snprint_cmdline() to lib/bootconfig.c
[ Upstream commit 5a643e4 ] 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: dec4d81 ("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>
1 parent 3a4627d commit 3fb2d02

3 files changed

Lines changed: 59 additions & 45 deletions

File tree

include/linux/bootconfig.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,9 @@ static inline struct xbc_node * __init xbc_node_get_subkey(struct xbc_node *node
264264
int __init xbc_node_compose_key_after(struct xbc_node *root,
265265
struct xbc_node *node, char *buf, size_t size);
266266

267+
/* Render key/value pairs under @root as a flat cmdline string */
268+
int __init xbc_snprint_cmdline(char *buf, size_t size, struct xbc_node *root);
269+
267270
/**
268271
* xbc_node_compose_key() - Compose full key string of the XBC node
269272
* @node: An XBC node.

init/main.c

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -316,51 +316,6 @@ static void * __init get_boot_config_from_initrd(size_t *_size)
316316

317317
#ifdef CONFIG_BOOT_CONFIG
318318

319-
static char xbc_namebuf[XBC_KEYLEN_MAX] __initdata;
320-
321-
#define rest(dst, end) ((end) > (dst) ? (end) - (dst) : 0)
322-
323-
static int __init xbc_snprint_cmdline(char *buf, size_t size,
324-
struct xbc_node *root)
325-
{
326-
struct xbc_node *knode, *vnode;
327-
char *end = buf + size;
328-
const char *val, *q;
329-
int ret;
330-
331-
xbc_node_for_each_key_value(root, knode, val) {
332-
ret = xbc_node_compose_key_after(root, knode,
333-
xbc_namebuf, XBC_KEYLEN_MAX);
334-
if (ret < 0)
335-
return ret;
336-
337-
vnode = xbc_node_get_child(knode);
338-
if (!vnode) {
339-
ret = snprintf(buf, rest(buf, end), "%s ", xbc_namebuf);
340-
if (ret < 0)
341-
return ret;
342-
buf += ret;
343-
continue;
344-
}
345-
xbc_array_for_each_value(vnode, val) {
346-
/*
347-
* For prettier and more readable /proc/cmdline, only
348-
* quote the value when necessary, i.e. when it contains
349-
* whitespace.
350-
*/
351-
q = strpbrk(val, " \t\r\n") ? "\"" : "";
352-
ret = snprintf(buf, rest(buf, end), "%s=%s%s%s ",
353-
xbc_namebuf, q, val, q);
354-
if (ret < 0)
355-
return ret;
356-
buf += ret;
357-
}
358-
}
359-
360-
return buf - (end - size);
361-
}
362-
#undef rest
363-
364319
/* Make an extra command line under given key word */
365320
static char * __init xbc_make_cmdline(const char *key)
366321
{

lib/bootconfig.c

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,62 @@ const char * __init xbc_node_find_next_key_value(struct xbc_node *root,
407407
return ""; /* No value key */
408408
}
409409

410+
static char xbc_namebuf[XBC_KEYLEN_MAX] __initdata;
411+
412+
#define rest(dst, end) ((end) > (dst) ? (end) - (dst) : 0)
413+
414+
/**
415+
* xbc_snprint_cmdline() - Render bootconfig keys under @root as a cmdline string
416+
* @buf: Destination buffer (may be NULL when @size is 0 to query the length)
417+
* @size: Size of @buf in bytes
418+
* @root: Subtree root whose key=value pairs should be rendered
419+
*
420+
* Walk all key/value pairs under @root and emit them as a space-separated
421+
* cmdline string into @buf. Values containing whitespace are quoted with
422+
* double quotes. Returns the number of bytes that would be written if @buf
423+
* were large enough (matching snprintf semantics), or a negative errno on
424+
* failure.
425+
*/
426+
int __init xbc_snprint_cmdline(char *buf, size_t size, struct xbc_node *root)
427+
{
428+
struct xbc_node *knode, *vnode;
429+
char *end = buf + size;
430+
const char *val, *q;
431+
int ret;
432+
433+
xbc_node_for_each_key_value(root, knode, val) {
434+
ret = xbc_node_compose_key_after(root, knode,
435+
xbc_namebuf, XBC_KEYLEN_MAX);
436+
if (ret < 0)
437+
return ret;
438+
439+
vnode = xbc_node_get_child(knode);
440+
if (!vnode) {
441+
ret = snprintf(buf, rest(buf, end), "%s ", xbc_namebuf);
442+
if (ret < 0)
443+
return ret;
444+
buf += ret;
445+
continue;
446+
}
447+
xbc_array_for_each_value(vnode, val) {
448+
/*
449+
* For prettier and more readable /proc/cmdline, only
450+
* quote the value when necessary, i.e. when it contains
451+
* whitespace.
452+
*/
453+
q = strpbrk(val, " \t\r\n") ? "\"" : "";
454+
ret = snprintf(buf, rest(buf, end), "%s=%s%s%s ",
455+
xbc_namebuf, q, val, q);
456+
if (ret < 0)
457+
return ret;
458+
buf += ret;
459+
}
460+
}
461+
462+
return buf - (end - size);
463+
}
464+
#undef rest
465+
410466
/* XBC parse and tree build */
411467

412468
static int __init xbc_init_node(struct xbc_node *node, char *data, uint32_t flag)

0 commit comments

Comments
 (0)