How to select a devicetree overlay (*.dtbo) dynamically in runtime?

I can confirm that the device tree overlay mechanism essentially works as implemented in the SDK sources, I had previously made a typo.

To make it functional (as in: it selects different overlays depending on runtime detection of hardware), the following changes are needed:

  1. Add all the different overlay files in common/arch/arm64/boot/dts/amlogic/

  2. Edit device/khadas/common/kernelbuild/build.sh under "===build dtbo===" section to include all overlay files in the dtbo.img. Currently only the file defined by device/khadas/kvim4/build.config.meson.arm64.trunk:DTBO_DEVICETREE is included in the build.

  3. Create and execute a u-boot command/script during the CONFIG_PREBOOT stage that detects the hardware and selects the correct device tree overlay index by calling setenv androidboot.dtbo_idx <selected index>;

  4. Even if device/khadas/kvim4/BoardConfig.mk:BOARD_KERNEL_CMDLINE += androidboot.dtbo_idx=0 is defined at build time the runtime variable will take precedence based on the code in bootloader/uboot/common/image-android.c:save_dtbo_idx()