1.. SPDX-License-Identifier: GPL-2.0
2
3==================
4Boot Configuration
5==================
6
7:Author: Masami Hiramatsu <mhiramat@kernel.org>
8
9Overview
10========
11
12The boot configuration is expanding current kernel cmdline to support
13additional key-value data when boot the kernel in an efficient way.
14This allows adoministrators to pass a structured-Key config file.
15
16Config File Syntax
17==================
18
19The boot config syntax is a simple structured key-value. Each key consists
20of dot-connected-words, and key and value are connected by "=". The value
21has to be terminated by semi-colon (``;``) or newline (``\n``).
22For array value, array entries are separated by comma (``,``). ::
23
24KEY[.WORD[...]] = VALUE[, VALUE2[...]][;]
25
26Each key word must contain only alphabets, numbers, dash (``-``) or underscore
27(``_``). And each value only contains printable characters or spaces except
28for delimiters such as semi-colon (``;``), new-line (``\n``), comma (``,``),
29hash (``#``) and closing brace (``}``).
30
31If you want to use those delimiters in a value, you can use either double-
32quotes (``"VALUE"``) or single-quotes (``'VALUE'``) to quote it. Note that
33you can not escape these quotes.
34
35There can be a key which doesn't have value or has an empty value. Those keys
36are used for checking the key exists or not (like a boolean).
37
38Key-Value Syntax
39----------------
40
41The boot config file syntax allows user to merge partially same word keys
42by brace. For example::
43
44 foo.bar.baz = value1
45 foo.bar.qux.quux = value2
46
47These can be written also in::
48
49 foo.bar {
50    baz = value1
51    qux.quux = value2
52 }
53
54Or more shorter, written as following::
55
56 foo.bar { baz = value1; qux.quux = value2 }
57
58In both styles, same key words are automatically merged when parsing it
59at boot time. So you can append similar trees or key-values.
60
61Comments
62--------
63
64The config syntax accepts shell-script style comments. The comments start
65with hash ("#") until newline ("\n") will be ignored.
66
67::
68
69 # comment line
70 foo = value # value is set to foo.
71 bar = 1, # 1st element
72       2, # 2nd element
73       3  # 3rd element
74
75This is parsed as below::
76
77 foo = value
78 bar = 1, 2, 3
79
80Note that you can not put a comment between value and delimiter(``,`` or
81``;``). This means following config has a syntax error ::
82
83 key = 1 # comment
84       ,2
85
86
87/proc/bootconfig
88================
89
90/proc/bootconfig is a user-space interface of the boot config.
91Unlike /proc/cmdline, this file shows the key-value style list.
92Each key-value pair is shown in each line with following style::
93
94 KEY[.WORDS...] = "[VALUE]"[,"VALUE2"...]
95
96
97Boot Kernel With a Boot Config
98==============================
99
100Since the boot configuration file is loaded with initrd, it will be added
101to the end of the initrd (initramfs) image file. The Linux kernel decodes
102the last part of the initrd image in memory to get the boot configuration
103data.
104Because of this "piggyback" method, there is no need to change or
105update the boot loader and the kernel image itself.
106
107To do this operation, Linux kernel provides "bootconfig" command under
108tools/bootconfig, which allows admin to apply or delete the config file
109to/from initrd image. You can build it by follwoing command::
110
111 # make -C tools/bootconfig
112
113To add your boot config file to initrd image, run bootconfig as below
114(Old data is removed automatically if exists)::
115
116 # tools/bootconfig/bootconfig -a your-config /boot/initrd.img-X.Y.Z
117
118To remove the config from the image, you can use -d option as below::
119
120 # tools/bootconfig/bootconfig -d /boot/initrd.img-X.Y.Z
121
122
123C onfig File Limitation
124======================
125
126Currently the maximum config size size is 32KB and the total key-words (not
127key-value entries) must be under 1024 nodes.
128Note: this is not the number of entries but nodes, an entry must consume
129more than 2 nodes (a key-word and a value). So theoretically, it will be
130up to 512 key-value pairs. If keys contains 3 words in average, it can
131contain 256 key-value pairs. In most cases, the number of config items
132will be under 100 entries and smaller than 8KB, so it would be enough.
133If the node number exceeds 1024, parser returns an error even if the file
134size is smaller than 32KB.
135Anyway, since bootconfig command verifies it when appending a boot config
136to initrd image, user can notice it before boot.
137
138
139Bootconfig APIs
140===============
141
142User can query or loop on key-value pairs, also it is possible to find
143a root (prefix) key node and find key-values under that node.
144
145If you have a key string, you can query the value directly with the key
146using xbc_find_value(). If you want to know what keys exist in the SKC
147tree, you can use xbc_for_each_key_value() to iterate key-value pairs.
148Note that you need to use xbc_array_for_each_value() for accessing
149each arraies value, e.g.::
150
151 vnode = NULL;
152 xbc_find_value("key.word", &vnode);
153 if (vnode && xbc_node_is_array(vnode))
154    xbc_array_for_each_value(vnode, value) {
155      printk("%s ", value);
156    }
157
158If you want to focus on keys which has a prefix string, you can use
159xbc_find_node() to find a node which prefix key words, and iterate
160keys under the prefix node with xbc_node_for_each_key_value().
161
162But the most typical usage is to get the named value under prefix
163or get the named array under prefix as below::
164
165 root = xbc_find_node("key.prefix");
166 value = xbc_node_find_value(root, "option", &vnode);
167 ...
168 xbc_node_for_each_array_value(root, "array-option", value, anode) {
169    ...
170 }
171
172This accesses a value of "key.prefix.option" and an array of
173"key.prefix.array-option".
174
175Locking is not needed, since after initialized, the config becomes readonly.
176All data and keys must be copied if you need to modify it.
177
178
179Functions and structures
180========================
181
182.. kernel-doc:: include/linux/bootconfig.h
183.. kernel-doc:: lib/bootconfig.c
184
185