xref: /linux/Documentation/admin-guide/bootconfig.rst (revision 23b0f90ba871f096474e1c27c3d14f455189d2d9)
1.. SPDX-License-Identifier: GPL-2.0
2
3.. _bootconfig:
4
5==================
6Boot Configuration
7==================
8
9:Author: Masami Hiramatsu <mhiramat@kernel.org>
10
11Overview
12========
13
14The boot configuration expands the current kernel command line to support
15additional key-value data when booting the kernel in an efficient way.
16This allows administrators to pass a structured-Key config file.
17
18Config File Syntax
19==================
20
21The boot config syntax is a simple structured key-value. Each key consists
22of dot-connected-words, and key and value are connected by ``=``. The value
23string has to be terminated by the following delimiters described below.
24
25Each key word must contain only alphabets, numbers, dash (``-``) or underscore
26(``_``). And each value only contains printable characters or spaces except
27for delimiters such as semi-colon (``;``), new-line (``\n``), comma (``,``),
28hash (``#``) and closing brace (``}``).
29
30If the ``=`` is followed by whitespace up to one of these delimiters, the
31key is assigned an empty value.
32
33For arrays, the array values are comma (``,``) separated, and comments and
34line breaks with newline (``\n``) are allowed between array values for
35readability. Thus the first entry of the array must be on the same line as
36the key.::
37
38  KEY[.WORD[...]] = VALUE[, VALUE2[...]][;]
39
40Unlike the kernel command line syntax, white spaces (including tabs) are
41ignored around the comma and ``=``.
42
43If you want to use those delimiters in a value, you can use either double-
44quotes (``"VALUE"``) or single-quotes (``'VALUE'``) to quote it. Note that
45you can not escape these quotes.
46
47There can be a key which doesn't have value or has an empty value. Those keys
48are used for checking if the key exists or not (like a boolean).
49
50Key-Value Syntax
51----------------
52
53The boot config file syntax allows user to merge partially same word keys
54by brace. For example::
55
56 foo.bar.baz = value1
57 foo.bar.qux.quux = value2
58
59These can be written also in::
60
61 foo.bar {
62    baz = value1
63    qux.quux = value2
64 }
65
66Or more shorter, written as following::
67
68 foo.bar { baz = value1; qux.quux = value2 }
69
70In both styles, same key words are automatically merged when parsing it
71at boot time. So you can append similar trees or key-values.
72
73Same-key Values
74---------------
75
76It is prohibited that two or more values or arrays share a same-key.
77For example,::
78
79 foo = bar, baz
80 foo = qux  # !ERROR! we can not re-define same key
81
82If you want to update the value, you must use the override operator
83``:=`` explicitly. For example::
84
85 foo = bar, baz
86 foo := qux
87
88then, the ``qux`` is assigned to ``foo`` key. This is useful for
89overriding the default value by adding (partial) custom bootconfigs
90without parsing the default bootconfig.
91
92If you want to append the value to existing key as an array member,
93you can use ``+=`` operator. For example::
94
95 foo = bar, baz
96 foo += qux
97
98In this case, the key ``foo`` has ``bar``, ``baz`` and ``qux``.
99
100Moreover, sub-keys and a value can coexist under a parent key.
101For example, following config is allowed.::
102
103 foo = value1
104 foo.bar = value2
105 foo := value3 # This will update foo's value.
106
107Note, since there is no syntax to put a raw value directly under a
108structured key, you have to define it outside of the brace. For example::
109
110 foo {
111     bar = value1
112     bar {
113         baz = value2
114         qux = value3
115     }
116 }
117
118Also, the order of the value node under a key is fixed. If there
119are a value and subkeys, the value is always the first child node
120of the key. Thus if user specifies subkeys first, e.g.::
121
122 foo.bar = value1
123 foo = value2
124
125In the program (and /proc/bootconfig), it will be shown as below::
126
127 foo = value2
128 foo.bar = value1
129
130Comments
131--------
132
133The config syntax accepts shell-script style comments. The comments starting
134with hash ("#") until newline ("\n") will be ignored.
135
136::
137
138 # comment line
139 foo = value # value is set to foo.
140 bar = 1, # 1st element
141       2, # 2nd element
142       3  # 3rd element
143
144This is parsed as below::
145
146 foo = value
147 bar = 1, 2, 3
148
149Note that you can NOT put a comment or a newline between value and delimiter
150(``,`` or ``;``). This means following config has a syntax error ::
151
152 key = 1 # comment
153       ,2
154
155
156/proc/bootconfig
157================
158
159/proc/bootconfig is a user-space interface of the boot config.
160Unlike /proc/cmdline, this file shows the key-value style list.
161Each key-value pair is shown in each line with following style::
162
163 KEY[.WORDS...] = "[VALUE]"[,"VALUE2"...]
164
165
166Boot Kernel With a Boot Config
167==============================
168
169There are two options to boot the kernel with bootconfig: attaching the
170bootconfig to the initrd image or embedding it in the kernel itself.
171
172Attaching a Boot Config to Initrd
173---------------------------------
174
175Since the boot configuration file is loaded with initrd by default,
176it will be added to the end of the initrd (initramfs) image file with
177padding, size, checksum and 12-byte magic word as below.
178
179[initrd][bootconfig][padding][size(le32)][checksum(le32)][#BOOTCONFIG\n]
180
181The size and checksum fields are unsigned 32bit little endian value.
182
183When the boot configuration is added to the initrd image, the total
184file size is aligned to 4 bytes. To fill the gap, null characters
185(``\0``) will be added. Thus the ``size`` is the length of the bootconfig
186file + padding bytes.
187
188The Linux kernel decodes the last part of the initrd image in memory to
189get the boot configuration data.
190Because of this "piggyback" method, there is no need to change or
191update the boot loader and the kernel image itself as long as the boot
192loader passes the correct initrd file size. If by any chance, the boot
193loader passes a longer size, the kernel fails to find the bootconfig data.
194
195To do this operation, Linux kernel provides ``bootconfig`` command under
196tools/bootconfig, which allows admin to apply or delete the config file
197to/from initrd image. You can build it by the following command::
198
199 # make -C tools/bootconfig
200
201To add your boot config file to initrd image, run bootconfig as below
202(Old data is removed automatically if exists)::
203
204 # tools/bootconfig/bootconfig -a your-config /boot/initrd.img-X.Y.Z
205
206To remove the config from the image, you can use -d option as below::
207
208 # tools/bootconfig/bootconfig -d /boot/initrd.img-X.Y.Z
209
210Then add "bootconfig" on the normal kernel command line to tell the
211kernel to look for the bootconfig at the end of the initrd file.
212Alternatively, build your kernel with the ``CONFIG_BOOT_CONFIG_FORCE``
213Kconfig option selected.
214
215Embedding a Boot Config into Kernel
216-----------------------------------
217
218If you can not use initrd, you can also embed the bootconfig file in the
219kernel by Kconfig options. In this case, you need to recompile the kernel
220with the following configs::
221
222 CONFIG_BOOT_CONFIG_EMBED=y
223 CONFIG_BOOT_CONFIG_EMBED_FILE="/PATH/TO/BOOTCONFIG/FILE"
224
225``CONFIG_BOOT_CONFIG_EMBED_FILE`` requires an absolute path or a relative
226path to the bootconfig file from source tree or object tree.
227The kernel will embed it as the default bootconfig.
228
229Just as when attaching the bootconfig to the initrd, you need ``bootconfig``
230option on the kernel command line to enable the embedded bootconfig, or,
231alternatively, build your kernel with the ``CONFIG_BOOT_CONFIG_FORCE``
232Kconfig option selected.
233
234Note that even if you set this option, you can override the embedded
235bootconfig by another bootconfig which attached to the initrd.
236
237Kernel parameters via Boot Config
238=================================
239
240In addition to the kernel command line, the boot config can be used for
241passing the kernel parameters. All the key-value pairs under ``kernel``
242key will be passed to kernel cmdline directly. Moreover, the key-value
243pairs under ``init`` will be passed to init process via the cmdline.
244The parameters are concatenated with user-given kernel cmdline string
245as the following order, so that the command line parameter can override
246bootconfig parameters (this depends on how the subsystem handles parameters
247but in general, earlier parameter will be overwritten by later one.)::
248
249 [bootconfig params][cmdline params] -- [bootconfig init params][cmdline init params]
250
251Here is an example of the bootconfig file for kernel/init parameters.::
252
253 kernel {
254   root = 01234567-89ab-cdef-0123-456789abcd
255 }
256 init {
257  splash
258 }
259
260This will be copied into the kernel cmdline string as the following::
261
262 root="01234567-89ab-cdef-0123-456789abcd" -- splash
263
264If user gives some other command line like,::
265
266 ro bootconfig -- quiet
267
268The final kernel cmdline will be the following::
269
270 root="01234567-89ab-cdef-0123-456789abcd" ro bootconfig -- splash quiet
271
272
273Config File Limitation
274======================
275
276Currently the maximum config size is 32KB and the total key-words (not
277key-value entries) must be under 1024 nodes.
278Note: this is not the number of entries but nodes, an entry must consume
279more than 2 nodes (a key-word and a value). So theoretically, it will be
280up to 512 key-value pairs. If keys contains 3 words in average, it can
281contain 256 key-value pairs. In most cases, the number of config items
282will be under 100 entries and smaller than 8KB, so it would be enough.
283If the node number exceeds 1024, parser returns an error even if the file
284size is smaller than 32KB. (Note that this maximum size is not including
285the padding null characters.)
286Anyway, since bootconfig command verifies it when appending a boot config
287to initrd image, user can notice it before boot.
288
289
290Bootconfig APIs
291===============
292
293User can query or loop on key-value pairs, also it is possible to find
294a root (prefix) key node and find key-values under that node.
295
296If you have a key string, you can query the value directly with the key
297using xbc_find_value(). If you want to know what keys exist in the boot
298config, you can use xbc_for_each_key_value() to iterate key-value pairs.
299Note that you need to use xbc_array_for_each_value() for accessing
300each array's value, e.g.::
301
302 vnode = NULL;
303 xbc_find_value("key.word", &vnode);
304 if (vnode && xbc_node_is_array(vnode))
305    xbc_array_for_each_value(vnode, value) {
306      printk("%s ", value);
307    }
308
309If you want to focus on keys which have a prefix string, you can use
310xbc_find_node() to find a node by the prefix string, and iterate
311keys under the prefix node with xbc_node_for_each_key_value().
312
313But the most typical usage is to get the named value under prefix
314or get the named array under prefix as below::
315
316 root = xbc_find_node("key.prefix");
317 value = xbc_node_find_value(root, "option", &vnode);
318 ...
319 xbc_node_for_each_array_value(root, "array-option", value, anode) {
320    ...
321 }
322
323This accesses a value of "key.prefix.option" and an array of
324"key.prefix.array-option".
325
326Locking is not needed, since after initialization, the config becomes
327read-only. All data and keys must be copied if you need to modify it.
328
329
330Functions and structures
331========================
332
333.. kernel-doc:: include/linux/bootconfig.h
334.. kernel-doc:: lib/bootconfig.c
335
336