bootconfig.rst 8.0 KB

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