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 237Rendering Embedded kernel.* Keys at Build Time 238---------------------------------------------- 239 240By default, the embedded bootconfig (``CONFIG_BOOT_CONFIG_EMBED=y``) is 241parsed at runtime, after ``parse_early_param()`` has already run. Early 242parameter handlers (``mem=``, ``earlycon=``, ``loglevel=``, ...) therefore 243cannot see values supplied via the embedded ``kernel`` subtree. 244 245``CONFIG_CMDLINE_FROM_BOOTCONFIG`` resolves this by rendering the 246``kernel`` subtree of ``CONFIG_BOOT_CONFIG_EMBED_FILE`` into a flat cmdline 247string at kernel build time (via ``tools/bootconfig -C``) and prepending 248it to ``boot_command_line`` during early architecture setup, so the keys 249are visible to ``parse_early_param()``. 250 251The option requires ``CONFIG_BOOT_CONFIG_EMBED=y``, a non-empty 252``CONFIG_BOOT_CONFIG_EMBED_FILE``, ``CONFIG_CMDLINE`` to be empty, and 253an architecture that selects ``CONFIG_ARCH_SUPPORTS_CMDLINE_FROM_BOOTCONFIG``. 254Currently only x86 selects it; on other architectures the embedded 255bootconfig still works, but only through the late runtime parser. 256 257The same ``bootconfig`` opt-in applies as elsewhere: the rendered keys 258are prepended only when ``bootconfig`` (in any form) appears on the 259kernel command line, or when ``CONFIG_BOOT_CONFIG_FORCE`` is set, which 260defaults to ``y`` when ``CONFIG_BOOT_CONFIG_EMBED`` is set. 261 262For example, given:: 263 264 kernel { 265 loglevel = 7 266 mem = 4G 267 } 268 269the kernel boots as if ``loglevel=7 mem=4G`` had been prepended to the 270bootloader command line, with the values visible to early-parsed 271handlers. Comma-separated values are still expanded into multiple 272cmdline entries per the bootconfig array convention -- the embedded 273``kernel.earlycon = "uart8250,io,0x3f8"`` must be quoted to land as a 274single ``earlycon=`` entry, exactly as for the runtime parser. 275 276If the rendered string would not fit in ``COMMAND_LINE_SIZE`` together 277with the existing command line, the prepend is skipped and an error is 278logged, so an oversized embedded bootconfig cannot brick a boot. 279 280Interaction with other command line and bootconfig sources 281~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 282 283With ``CONFIG_CMDLINE_FROM_BOOTCONFIG=y`` the rendered ``kernel`` 284subtree behaves like a build-time command line (similar to 285``CONFIG_CMDLINE``), not like a bootconfig source. It is prepended to 286``boot_command_line`` in ``setup_arch()``, before ``parse_early_param()`` 287and long before the runtime parser looks at an initrd. Options can reach 288the kernel from up to four places: 289 290- Bootloader command line: the arguments the boot loader passes. The 291 embedded cmdline is prepended in front of them, so for last-one-wins 292 parameters a bootloader option still overrides the embedded value. 293 Visible in /proc/cmdline. 294- Embedded cmdline (this option): the rendered ``kernel`` subtree, 295 prepended early so it is seen by ``parse_early_param()``. Visible in 296 /proc/cmdline. 297- Initrd bootconfig: parsed late in ``setup_boot_config()``; its 298 ``kernel`` keys are placed ahead of ``boot_command_line``, i.e. before 299 the embedded cmdline, so last-wins favors the embedded values. As a 300 bootconfig source, an initrd bootconfig still replaces the embedded 301 bootconfig. Visible in /proc/cmdline and /proc/bootconfig. 302- Embedded bootconfig (runtime): parsed late, only when no initrd 303 bootconfig is present. Visible in /proc/cmdline and /proc/bootconfig. 304 305So with this option the embedded ``kernel.*`` values take precedence 306over an initrd bootconfig's ``kernel.*`` values: for early parameters 307the initrd is not parsed yet, and for ordinary parameters the embedded 308keys land later in the command line. If you need an initrd bootconfig to 309override the embedded ``kernel.*`` keys, leave this option off and rely 310on the runtime parser. 311 312The rendered string is part of the command line, so it appears in 313/proc/cmdline. It is deliberately not shown in /proc/bootconfig: that 314file keeps reporting the parsed bootconfig tree -- the initrd bootconfig 315if present, otherwise the embedded bootconfig -- independent of whether 316build-time cmdline rendering is enabled. 317 318Kernel parameters via Boot Config 319================================= 320 321In addition to the kernel command line, the boot config can be used for 322passing the kernel parameters. All the key-value pairs under ``kernel`` 323key will be passed to kernel cmdline directly. Moreover, the key-value 324pairs under ``init`` will be passed to init process via the cmdline. 325The parameters are concatenated with user-given kernel cmdline string 326as the following order, so that the command line parameter can override 327bootconfig parameters (this depends on how the subsystem handles parameters 328but in general, earlier parameter will be overwritten by later one.):: 329 330 [bootconfig params][cmdline params] -- [bootconfig init params][cmdline init params] 331 332Here is an example of the bootconfig file for kernel/init parameters.:: 333 334 kernel { 335 root = 01234567-89ab-cdef-0123-456789abcd 336 } 337 init { 338 splash 339 } 340 341This will be copied into the kernel cmdline string as the following:: 342 343 root="01234567-89ab-cdef-0123-456789abcd" -- splash 344 345If user gives some other command line like,:: 346 347 ro bootconfig -- quiet 348 349The final kernel cmdline will be the following:: 350 351 root="01234567-89ab-cdef-0123-456789abcd" ro bootconfig -- splash quiet 352 353 354Config File Limitation 355====================== 356 357Currently the maximum config size is 32KB and the total key-words (not 358key-value entries) must be under 1024 nodes. 359Note: this is not the number of entries but nodes, an entry must consume 360more than 2 nodes (a key-word and a value). So theoretically, it will be 361up to 512 key-value pairs. If keys contains 3 words in average, it can 362contain 256 key-value pairs. In most cases, the number of config items 363will be under 100 entries and smaller than 8KB, so it would be enough. 364If the node number exceeds 1024, parser returns an error even if the file 365size is smaller than 32KB. (Note that this maximum size is not including 366the padding null characters.) 367Anyway, since bootconfig command verifies it when appending a boot config 368to initrd image, user can notice it before boot. 369 370 371Bootconfig APIs 372=============== 373 374User can query or loop on key-value pairs, also it is possible to find 375a root (prefix) key node and find key-values under that node. 376 377If you have a key string, you can query the value directly with the key 378using xbc_find_value(). If you want to know what keys exist in the boot 379config, you can use xbc_for_each_key_value() to iterate key-value pairs. 380Note that you need to use xbc_array_for_each_value() for accessing 381each array's value, e.g.:: 382 383 vnode = NULL; 384 xbc_find_value("key.word", &vnode); 385 if (vnode && xbc_node_is_array(vnode)) 386 xbc_array_for_each_value(vnode, value) { 387 printk("%s ", value); 388 } 389 390If you want to focus on keys which have a prefix string, you can use 391xbc_find_node() to find a node by the prefix string, and iterate 392keys under the prefix node with xbc_node_for_each_key_value(). 393 394But the most typical usage is to get the named value under prefix 395or get the named array under prefix as below:: 396 397 root = xbc_find_node("key.prefix"); 398 value = xbc_node_find_value(root, "option", &vnode); 399 ... 400 xbc_node_for_each_array_value(root, "array-option", value, anode) { 401 ... 402 } 403 404This accesses a value of "key.prefix.option" and an array of 405"key.prefix.array-option". 406 407Locking is not needed, since after initialization, the config becomes 408read-only. All data and keys must be copied if you need to modify it. 409 410 411Functions and structures 412======================== 413 414.. kernel-doc:: include/linux/bootconfig.h 415.. kernel-doc:: lib/bootconfig.c 416 417