Customizing u-boot build configuration

Created On:

u-boot uses the Kconfig build system like Linux. One issue I faced with Kconfig in u-boot is how to alter the configuration of a build via a script. The typical u-boot flow is to seed the build configuration by using a defconfig for a specific board.

$ make pine64_plus_defconfig

The .config file is then populated with reasonable default values. The next step is to interactively edit the configuration via a ncurses application.

$ make menuconfig

This approach is not feasible in a script because make menuconfig is interactive. Fortunately u-boot ships with a utility script ./scripts/kconfig/merge_config.sh which allows merging of two different config files.

This script allows merging two sets of config files together. For example to add on zstd compression to the pine64_plus_defconfig configuration just do the following:

$ make pine64_plus_defconfig
$ echo 'CONFIG_ZSTD=y' > ./.custom_config
$ ./scripts/kconfig/merge_config.sh '.config' '.custom_config'

After running the above the CONFIG_ZSTD option will be merge into the .config file and set.

The same approach can be taken to disable certain configuration values as well. For example to disable any networking related functionality, it’s possible to flip the value of the CONFIG_NET option.

$ make pine64_plus_defconfig
$ echo 'CONFIG_NET=n' > ./.custom_config
$ ./scripts/kconfig/merge_config.sh '.config' '.custom_config'

After the script runs, any networking related options will be disabled.

There are some limitations to this approach, with depends on such as the following:

config CMD_USB_MASS_STORAGE
    bool "UMS usb mass storage"
    depends on USB_GADGET_DOWNLOAD
    select USB_FUNCTION_MASS_STORAGE
    depends on BLK && USB_GADGET
    help
      Enables the command "ums" and the USB mass storage support to the
      export a block device: U-Boot, the USB device, acts as a simple
      external hard drive plugged on the host USB port.

The pine64_plus_defconfig does not set CMD_USB_MASS_STORAGE but it also doesn’t set USB_GADGET_DOWNLOAD. Trying to enable CMD_USB_MASS_STORAGE without the dependencies results in the following.

$ make pine64_plus_defconfig
$ echo 'CONFIG_CMD_USB_MASS_STORAGE=y' > ./.custom_config
$ ./scripts/kconfig/merge_config.sh '.config' '.custom_config'

Using .config as base
Merging .custom_config
scripts/kconfig/conf  --alldefconfig Kconfig
#
# configuration written to .config
#
Value requested for CONFIG_CMD_USB_MASS_STORAGE not in final .config
Requested value:  CONFIG_CMD_USB_MASS_STORAGE=y
Actual value:

In order to actually enable this feature, all of the dependencies need to be enabled as well in the final configuration.

$ make pine64_plus_defconfig
$ echo 'CONFIG_CMD_USB_MASS_STORAGE=y' > ./.custom_config
$ echo 'CONFIG_USB_GADGET_DOWNLOAD=y' >> ./.custom_config
$ ./scripts/kconfig/merge_config.sh '.config' '.custom_config'

Using .config as base
Merging .custom_config
Value of CONFIG_USB_GADGET_DOWNLOAD is redefined by fragment .custom_config:
Previous value: # CONFIG_USB_GADGET_DOWNLOAD is not set
New value: CONFIG_USB_GADGET_DOWNLOAD=y

scripts/kconfig/conf  --alldefconfig Kconfig
#
# configuration written to .config
#

Despite its limitations the merge_config.sh script is a powerful way to alter the build configuration of u-boot via a script.