0001 .. SPDX-License-Identifier: GPL-2.0
0002
0003 .. _bootconfig:
0004
0005 ==================
0006 Boot Configuration
0007 ==================
0008
0009 :Author: Masami Hiramatsu <mhiramat@kernel.org>
0010
0011 Overview
0012 ========
0013
0014 The boot configuration expands the current kernel command line to support
0015 additional key-value data when booting the kernel in an efficient way.
0016 This allows administrators to pass a structured-Key config file.
0017
0018 Config File Syntax
0019 ==================
0020
0021 The boot config syntax is a simple structured key-value. Each key consists
0022 of dot-connected-words, and key and value are connected by ``=``. The value
0023 has to be terminated by semi-colon (``;``) or newline (``\n``).
0024 For array value, array entries are separated by comma (``,``). ::
0025
0026 KEY[.WORD[...]] = VALUE[, VALUE2[...]][;]
0027
0028 Unlike the kernel command line syntax, spaces are OK around the comma and ``=``.
0029
0030 Each key word must contain only alphabets, numbers, dash (``-``) or underscore
0031 (``_``). And each value only contains printable characters or spaces except
0032 for delimiters such as semi-colon (``;``), new-line (``\n``), comma (``,``),
0033 hash (``#``) and closing brace (``}``).
0034
0035 If you want to use those delimiters in a value, you can use either double-
0036 quotes (``"VALUE"``) or single-quotes (``'VALUE'``) to quote it. Note that
0037 you can not escape these quotes.
0038
0039 There can be a key which doesn't have value or has an empty value. Those keys
0040 are used for checking if the key exists or not (like a boolean).
0041
0042 Key-Value Syntax
0043 ----------------
0044
0045 The boot config file syntax allows user to merge partially same word keys
0046 by brace. For example::
0047
0048 foo.bar.baz = value1
0049 foo.bar.qux.quux = value2
0050
0051 These can be written also in::
0052
0053 foo.bar {
0054 baz = value1
0055 qux.quux = value2
0056 }
0057
0058 Or more shorter, written as following::
0059
0060 foo.bar { baz = value1; qux.quux = value2 }
0061
0062 In both styles, same key words are automatically merged when parsing it
0063 at boot time. So you can append similar trees or key-values.
0064
0065 Same-key Values
0066 ---------------
0067
0068 It is prohibited that two or more values or arrays share a same-key.
0069 For example,::
0070
0071 foo = bar, baz
0072 foo = qux # !ERROR! we can not re-define same key
0073
0074 If you want to update the value, you must use the override operator
0075 ``:=`` explicitly. For example::
0076
0077 foo = bar, baz
0078 foo := qux
0079
0080 then, the ``qux`` is assigned to ``foo`` key. This is useful for
0081 overriding the default value by adding (partial) custom bootconfigs
0082 without parsing the default bootconfig.
0083
0084 If you want to append the value to existing key as an array member,
0085 you can use ``+=`` operator. For example::
0086
0087 foo = bar, baz
0088 foo += qux
0089
0090 In this case, the key ``foo`` has ``bar``, ``baz`` and ``qux``.
0091
0092 Moreover, sub-keys and a value can coexist under a parent key.
0093 For example, following config is allowed.::
0094
0095 foo = value1
0096 foo.bar = value2
0097 foo := value3 # This will update foo's value.
0098
0099 Note, since there is no syntax to put a raw value directly under a
0100 structured key, you have to define it outside of the brace. For example::
0101
0102 foo {
0103 bar = value1
0104 bar {
0105 baz = value2
0106 qux = value3
0107 }
0108 }
0109
0110 Also, the order of the value node under a key is fixed. If there
0111 are a value and subkeys, the value is always the first child node
0112 of the key. Thus if user specifies subkeys first, e.g.::
0113
0114 foo.bar = value1
0115 foo = value2
0116
0117 In the program (and /proc/bootconfig), it will be shown as below::
0118
0119 foo = value2
0120 foo.bar = value1
0121
0122 Comments
0123 --------
0124
0125 The config syntax accepts shell-script style comments. The comments starting
0126 with hash ("#") until newline ("\n") will be ignored.
0127
0128 ::
0129
0130 # comment line
0131 foo = value # value is set to foo.
0132 bar = 1, # 1st element
0133 2, # 2nd element
0134 3 # 3rd element
0135
0136 This is parsed as below::
0137
0138 foo = value
0139 bar = 1, 2, 3
0140
0141 Note that you can not put a comment between value and delimiter(``,`` or
0142 ``;``). This means following config has a syntax error ::
0143
0144 key = 1 # comment
0145 ,2
0146
0147
0148 /proc/bootconfig
0149 ================
0150
0151 /proc/bootconfig is a user-space interface of the boot config.
0152 Unlike /proc/cmdline, this file shows the key-value style list.
0153 Each key-value pair is shown in each line with following style::
0154
0155 KEY[.WORDS...] = "[VALUE]"[,"VALUE2"...]
0156
0157
0158 Boot Kernel With a Boot Config
0159 ==============================
0160
0161 There are two options to boot the kernel with bootconfig: attaching the
0162 bootconfig to the initrd image or embedding it in the kernel itself.
0163
0164 Attaching a Boot Config to Initrd
0165 ---------------------------------
0166
0167 Since the boot configuration file is loaded with initrd by default,
0168 it will be added to the end of the initrd (initramfs) image file with
0169 padding, size, checksum and 12-byte magic word as below.
0170
0171 [initrd][bootconfig][padding][size(le32)][checksum(le32)][#BOOTCONFIG\n]
0172
0173 The size and checksum fields are unsigned 32bit little endian value.
0174
0175 When the boot configuration is added to the initrd image, the total
0176 file size is aligned to 4 bytes. To fill the gap, null characters
0177 (``\0``) will be added. Thus the ``size`` is the length of the bootconfig
0178 file + padding bytes.
0179
0180 The Linux kernel decodes the last part of the initrd image in memory to
0181 get the boot configuration data.
0182 Because of this "piggyback" method, there is no need to change or
0183 update the boot loader and the kernel image itself as long as the boot
0184 loader passes the correct initrd file size. If by any chance, the boot
0185 loader passes a longer size, the kernel fails to find the bootconfig data.
0186
0187 To do this operation, Linux kernel provides ``bootconfig`` command under
0188 tools/bootconfig, which allows admin to apply or delete the config file
0189 to/from initrd image. You can build it by the following command::
0190
0191 # make -C tools/bootconfig
0192
0193 To add your boot config file to initrd image, run bootconfig as below
0194 (Old data is removed automatically if exists)::
0195
0196 # tools/bootconfig/bootconfig -a your-config /boot/initrd.img-X.Y.Z
0197
0198 To remove the config from the image, you can use -d option as below::
0199
0200 # tools/bootconfig/bootconfig -d /boot/initrd.img-X.Y.Z
0201
0202 Then add "bootconfig" on the normal kernel command line to tell the
0203 kernel to look for the bootconfig at the end of the initrd file.
0204
0205 Embedding a Boot Config into Kernel
0206 -----------------------------------
0207
0208 If you can not use initrd, you can also embed the bootconfig file in the
0209 kernel by Kconfig options. In this case, you need to recompile the kernel
0210 with the following configs::
0211
0212 CONFIG_BOOT_CONFIG_EMBED=y
0213 CONFIG_BOOT_CONFIG_EMBED_FILE="/PATH/TO/BOOTCONFIG/FILE"
0214
0215 ``CONFIG_BOOT_CONFIG_EMBED_FILE`` requires an absolute path or a relative
0216 path to the bootconfig file from source tree or object tree.
0217 The kernel will embed it as the default bootconfig.
0218
0219 Just as when attaching the bootconfig to the initrd, you need ``bootconfig``
0220 option on the kernel command line to enable the embedded bootconfig.
0221
0222 Note that even if you set this option, you can override the embedded
0223 bootconfig by another bootconfig which attached to the initrd.
0224
0225 Kernel parameters via Boot Config
0226 =================================
0227
0228 In addition to the kernel command line, the boot config can be used for
0229 passing the kernel parameters. All the key-value pairs under ``kernel``
0230 key will be passed to kernel cmdline directly. Moreover, the key-value
0231 pairs under ``init`` will be passed to init process via the cmdline.
0232 The parameters are concatinated with user-given kernel cmdline string
0233 as the following order, so that the command line parameter can override
0234 bootconfig parameters (this depends on how the subsystem handles parameters
0235 but in general, earlier parameter will be overwritten by later one.)::
0236
0237 [bootconfig params][cmdline params] -- [bootconfig init params][cmdline init params]
0238
0239 Here is an example of the bootconfig file for kernel/init parameters.::
0240
0241 kernel {
0242 root = 01234567-89ab-cdef-0123-456789abcd
0243 }
0244 init {
0245 splash
0246 }
0247
0248 This will be copied into the kernel cmdline string as the following::
0249
0250 root="01234567-89ab-cdef-0123-456789abcd" -- splash
0251
0252 If user gives some other command line like,::
0253
0254 ro bootconfig -- quiet
0255
0256 The final kernel cmdline will be the following::
0257
0258 root="01234567-89ab-cdef-0123-456789abcd" ro bootconfig -- splash quiet
0259
0260
0261 Config File Limitation
0262 ======================
0263
0264 Currently the maximum config size size is 32KB and the total key-words (not
0265 key-value entries) must be under 1024 nodes.
0266 Note: this is not the number of entries but nodes, an entry must consume
0267 more than 2 nodes (a key-word and a value). So theoretically, it will be
0268 up to 512 key-value pairs. If keys contains 3 words in average, it can
0269 contain 256 key-value pairs. In most cases, the number of config items
0270 will be under 100 entries and smaller than 8KB, so it would be enough.
0271 If the node number exceeds 1024, parser returns an error even if the file
0272 size is smaller than 32KB. (Note that this maximum size is not including
0273 the padding null characters.)
0274 Anyway, since bootconfig command verifies it when appending a boot config
0275 to initrd image, user can notice it before boot.
0276
0277
0278 Bootconfig APIs
0279 ===============
0280
0281 User can query or loop on key-value pairs, also it is possible to find
0282 a root (prefix) key node and find key-values under that node.
0283
0284 If you have a key string, you can query the value directly with the key
0285 using xbc_find_value(). If you want to know what keys exist in the boot
0286 config, you can use xbc_for_each_key_value() to iterate key-value pairs.
0287 Note that you need to use xbc_array_for_each_value() for accessing
0288 each array's value, e.g.::
0289
0290 vnode = NULL;
0291 xbc_find_value("key.word", &vnode);
0292 if (vnode && xbc_node_is_array(vnode))
0293 xbc_array_for_each_value(vnode, value) {
0294 printk("%s ", value);
0295 }
0296
0297 If you want to focus on keys which have a prefix string, you can use
0298 xbc_find_node() to find a node by the prefix string, and iterate
0299 keys under the prefix node with xbc_node_for_each_key_value().
0300
0301 But the most typical usage is to get the named value under prefix
0302 or get the named array under prefix as below::
0303
0304 root = xbc_find_node("key.prefix");
0305 value = xbc_node_find_value(root, "option", &vnode);
0306 ...
0307 xbc_node_for_each_array_value(root, "array-option", value, anode) {
0308 ...
0309 }
0310
0311 This accesses a value of "key.prefix.option" and an array of
0312 "key.prefix.array-option".
0313
0314 Locking is not needed, since after initialization, the config becomes
0315 read-only. All data and keys must be copied if you need to modify it.
0316
0317
0318 Functions and structures
0319 ========================
0320
0321 .. kernel-doc:: include/linux/bootconfig.h
0322 .. kernel-doc:: lib/bootconfig.c
0323