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