Section 6.40

Kconfig

Definition

Kconfig is a configuration language, with a tool that resolves it, for choosing before compilation which software features and settings a build contains. Kconfig files declare configuration symbols; each symbol has a type, such as on/off or a number, a default, and dependencies on other symbols. Configuration files request values, the tool combines the requests with the declared rules, and the result reaches the C code as constants named CONFIG_.... The language began in the Linux kernel, whose configuration files are named Kconfig; Zephyr §6.33 uses it too.

The idea

Firmware for a microcontroller §6.1 has to fit in a small flash memory §6.6, often 1 MiB or less, so it cannot carry every feature that its operating system offers. Choosing at build time removes the unused code completely, which also removes it from what an attacker can reach. Features depend on one another, though. A feature that needs a timer driver cannot work in a build without that driver. Kconfig lets each such rule be written once, next to the symbol it governs, and checks every build against all the rules.

Because of the rules, a request is not a result. The configuration line CONFIG_FEATURE_A=y asks for a feature; whether the build contains the feature depends on the rules that govern FEATURE_A. A car order form is a fair picture: ticking “sunroof” on a model whose sunroof needs the premium trim produces no sunroof unless the premium trim is ordered too. The picture stops at the reaction. A dealer would phone the buyer; Kconfig, in Zephyr’s default setup, prints a warning and builds the firmware without the feature.

A Kconfig value is fixed when the firmware is compiled. The C code sees it as a constant, and the compiler removes the code of every branch that the constant rules out. A deployed device therefore cannot change a Kconfig decision: no variable in its memory holds the decision, and the code for the other choice may not exist in its flash memory. Changing the decision means building a new image and installing it as a firmware update. A run-time setting, such as a value stored in flash memory and read at start-up, is the alternative for a decision that must stay changeable on deployed devices.

Zephyr’s Bluetooth stack takes several security decisions this way. The letters SMP in the symbol names stand for Security Manager Protocol §7.19, the part of Bluetooth Low Energy (BLE) §7.1 that runs pairing, and Bluetooth’s own documents shorten Low Energy to LE. BT_SMP_SC_PAIR_ONLY disables LE legacy pairing §7.20. BT_SMP_SC_ONLY turns on Secure Connections Only mode, in which the device accepts only authenticated LE Secure Connections §7.21 pairing with encryption, except for services that need no security. BT_SMP_ENFORCE_MITM makes the device ask for protection against a man-in-the-middle attack §7.26 whenever its input and output capabilities allow it. BT_SMP_MIN_ENC_KEY_SIZE sets the smallest encryption key size §7.27, in bytes, that the device accepts. A device built to accept LE legacy pairing keeps accepting it until new firmware is installed.

How it works

A symbol is declared like this:

config FEATURE_A
bool "Feature A"
depends on TIMER
default n

The parts:

In Zephyr, the starting configuration merges three sources: the board’s file of defaults, whose name ends in _defconfig; settings of the form CONFIG_... given to the build on the command line; and the application’s configuration, normally prj.conf, together with any extra fragments named in the build variable EXTRA_CONF_FILE. When the board’s file and the application both assign a symbol, the application’s value takes precedence, and when two fragments assign the same symbol, the later one wins. The tool then resolves every symbol under the rules and writes two outputs into the build folder: zephyr/.config, which lists every resolved value, and the header autoconf.h, which holds a line such as #define CONFIG_FEATURE_A 1 for each bool symbol that is y. A symbol that is n is not defined at all. Code tests a symbol with #ifdef CONFIG_FEATURE_A or with Zephyr’s macro IS_ENABLED(CONFIG_FEATURE_A).

When an assignment cannot take effect, the tool prints a warning that names the symbol, the requested value, the value it got and the unmet dependency. Zephyr’s documentation states that some Kconfig warnings stop the build by default, such as an assignment to a symbol that does not exist, while others are only printed, such as an assignment ignored because the symbol’s dependencies are not met. Setting the build variable KCONFIG_WARNING_AS_ERROR to y makes every Kconfig warning stop the build.

Worked example

Take two symbols, TIMER, declared as bool "Timer driver" with no dependencies and no default, and FEATURE_A, declared as in the listing above with depends on TIMER. Three configurations give three results.

  1. prj.conf holds only CONFIG_FEATURE_A=y. TIMER is visible, unassigned and without a default, so it is n. The dependency of FEATURE_A is therefore false, so FEATURE_A is n, the request is ignored, and a warning is printed. autoconf.h does not define CONFIG_FEATURE_A, so every block of code inside #ifdef CONFIG_FEATURE_A is left out of the firmware. Requested: y. Resolved: n.

  2. prj.conf also holds CONFIG_TIMER=y. Now the dependency is true, FEATURE_A is visible, and the request gives y.

  3. A third symbol, LOGGER, is declared as bool "Logger" with select FEATURE_A, and prj.conf holds only CONFIG_LOGGER=y. The selection forces FEATURE_A to y although TIMER is n. The tool warns about unmet direct dependencies, and the feature’s code is compiled without the timer driver it needs. Depending on the feature’s code, the build then fails when the linker §12.4 finds no timer functions, or it produces firmware whose feature does not work.

A real case from Zephyr’s Bluetooth source, as it stood in 2026, shows a request overridden in the safe direction. BT_SMP_MIN_ENC_KEY_SIZE is an int symbol with range 7 to 16, and its prompt carries the condition if !BT_SMP_SC_ONLY. With CONFIG_BT_SMP_SC_ONLY=y, the prompt’s condition is false and the symbol is invisible. A line CONFIG_BT_SMP_MIN_ENC_KEY_SIZE=7 in prj.conf is therefore ignored, and the symbol keeps its default, 16. Requested: 7 bytes. Resolved: 16 bytes.

Stated precisely

For a bool symbol SS, write yy as 1 and nn as 0, ∧\land for AND, which gives 1 only when both sides are 1, and ∨\lor for OR, which gives 1 when either side is 1. Let DD be the value of the symbol’s depends on condition (1 if it has none); PP the value of its prompt’s condition (1 for a prompt without a condition, 0 if there is no prompt); FF the value of the first default whose condition is 1 (0 if there is none); and RR the OR of the values of all symbols that select SS (0 if none does). The symbol is visible when V=D∧PV = D \land P is 1. Then, leaving imply aside, S=R∨{Uif V=1 and an input assigns the value U,D∧Fotherwise.S = R \lor \begin{cases} U & \text{if } V = 1 \text{ and an input assigns the value } U, \\ D \land F & \text{otherwise.} \end{cases}

In words: an assignment counts only while the symbol is visible; otherwise the default applies, and a default never turns on a symbol whose dependencies are false; a selection overrides everything, including false dependencies and an assignment of n. For int, hex and string symbols the same visibility rule applies: an assignment to an invisible symbol is ignored and the default is used.

The resolved configuration of a build is the file zephyr/.config in its build folder. A statement that a build contains a feature needs three facts: the symbol is y in that file; the code it guards was compiled and linked into the image; and that code runs, which needs the hardware it uses and a successful initialization at start-up.

Limits and common mistakes

A configuration file records requests, not results. Only the resolved zephyr/.config records results.

select ignores the selected symbol’s dependencies, so it can produce a combination that the rules for that symbol forbid. Kconfig warns but does not refuse.

Symbols and defaults change between releases. An assignment to a symbol that a new release renamed or removed refers to a symbol that no longer exists, which Zephyr treats as an error; a changed default silently changes every build that relied on the old one.

Kconfig chooses what is compiled. It does not show that the compiled code is correct or that it runs: a driver set to y does nothing without its hardware described in the devicetree §6.39, the separate text description of the board’s hardware.

Kconfig holds software choices. Addresses, pins and other hardware facts belong in the devicetree.

A decision made in Kconfig is fixed for the life of the image. If a security rule, such as refusing LE legacy pairing, must be changeable on deployed devices, it has to be a run-time setting; otherwise every change needs a firmware update. Run-time functions can still ask for more security on one connection, but they cannot bring back what the build left out.

Questions answered

Can a requested setting be overridden or made ineffective by dependencies?

Yes. In Kconfig, the build-time configuration language of Linux and Zephyr, a value written in a configuration file is only a request; the tool that resolves the configuration gives each symbol the value its rules allow, which can differ from the request. The resolved symbols become constants named CONFIG_... in the C code. A request is overridden or made ineffective in five ways:

A setting can also be accepted and still have no effect: the symbol is y, but the code it enables needs hardware that the devicetree does not describe, or the code fails at start-up. To know what a build contains, read the resolved zephyr/.config and the build’s warnings, not the configuration files. Where an ignored request must stop the build, set KCONFIG_WARNING_AS_ERROR=y.

Related entries: Devicetree (§6.39), Firmware build profile (§6.44), uBPF (§2.16), Security Manager Protocol (SMP) (§7.19). Further reading: [16].