Skip to content

Commit 5a643e4

Browse files
leitaomhiramat
authored andcommitted
bootconfig: move xbc_snprint_cmdline() to lib/bootconfig.c
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>
1 parent 5d69190 commit 5a643e4

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
@@ -265,6 +265,9 @@ static inline struct xbc_node * __init xbc_node_get_subkey(struct xbc_node *node
265265
int __init xbc_node_compose_key_after(struct xbc_node *root,
266266
struct xbc_node *node, char *buf, size_t size);
267267

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

init/main.c

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

325325
#ifdef CONFIG_BOOT_CONFIG
326326

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

lib/bootconfig.c

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

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

413469
static int __init xbc_init_node(struct xbc_node *node, char *data, uint16_t flag)

0 commit comments

Comments
 (0)