Skip to content
74 changes: 57 additions & 17 deletions tools/modules/system/manage_dtoverlays.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

module_options+=(
["manage_dtoverlays,author"]="@viraniac"
["manage_dtoverlays,maintainer"]="@igorpecovnik,@The-Going"
["manage_dtoverlays,maintainer"]="@igorpecovnik,@The-going"
["manage_dtoverlays,ref_link"]=""
["manage_dtoverlays,feature"]="manage_dtoverlays"
["manage_dtoverlays,desc"]="Enable/disable device tree overlays"
Expand All @@ -20,6 +20,7 @@ function manage_dtoverlays () {
local overlayconf="/boot/armbianEnv.txt"
if [[ "${LINUXFAMILY}" == "bcm2711" ]]; then
# Raspberry Pi has different name
overlayconf="/boot/firmware/config.txt"
local overlaydir=$(find /boot/dtb/ -maxdepth 1 -type d \( -name "overlay" -o -name "overlays" \) | head -n1)
local overlay_prefix=$(awk -F= '/^overlay_prefix=/ {print $2}' "$overlayconf")
else
Expand All @@ -32,42 +33,81 @@ function manage_dtoverlays () {

[[ ! -f "${overlayconf}" || ! -d "${overlaydir}" ]] && echo -e "Incompatible OS configuration\nArmbian device tree configuration files not found" | show_message && return 1

# check /boot/boot.scr scenario overlay(s)/${overlay_prefix}-${overlay_name}.dtbo
# or overlay(s)/${overlay_name}.dtbo.
# scenario:
# 00 - The /boot/boot.scr script cannot load the overlays provided by Armbian.
# 01 - It is possible to load only if the full name of the overlay is written.
# 10 - Loading is possible only if the overlay name is written without a prefix.
# 11 - Both spellings will be loaded.
scenario=$(
awk 'BEGIN{p=0;s=0}
/load.*overlays?\/\${overlay_prefix}-\${overlay_file}.dtbo/{p=1}
/load.*overlays?\/\${overlay_file}.dtbo/{s=1}
END{print p s}
' /boot/boot.scr
)

while true; do
local options=()
j=0

# read overlays
if [[ "${LINUXFAMILY}" == "bcm2711" ]]; then
available_overlays=$(ls -1 ${overlaydir}/*.dtbo | sed 's/.dtbo//g' | awk -F'/' '{print $NF}')
overlayconf="/boot/firmware/config.txt"
#elif [[ -n "${BOOT_SOC}" ]]; then
# available_overlays=$(ls -1 ${overlaydir}/${overlay_prefix}*.dtbo | sed 's/^.*\('${overlay_prefix}'.*\)/\1/g' | grep -E "$BOOT_SOC|$BOARD" | sed 's/'${overlay_prefix}'-//g' | sed 's/.dtbo//g')
else
# available_overlays=$(ls -1 ${overlaydir}/${overlay_prefix}*.dtbo | sed 's/^.*\('${overlay_prefix}'.*\)/\1/g' | sed 's/'${overlay_prefix}'-//g' | sed 's/.dtbo//g')
#
# We don't have consistent naming in overlays, so we have to display them all
#
available_overlays=$(ls -1 ${overlaydir}/*.dtbo | sed 's/.dtbo//g' | awk -F'/' '{print $NF}')
if [[ "${scenario}" == "10" ]] || [[ "${scenario}" == "11" ]]; then
# read overlays
available_overlays=$(
# Find the files that match the overlay prefix pattern.
# Remove the overlay prefix, file extension, and path
# in one pass. Sort it out.
find "${overlaydir}"/ -name "$overlay_prefix"'*.dtbo' 2>/dev/null | \
awk -F'/' -v p="${overlay_prefix}-" '{
gsub(p, "", $NF)
gsub(".dtbo", "", $NF)
print $NF
}' | sort
)
fi

# Check the branch in case it is not available in /etc/armbian-release
update_kernel_env

# Add support for rk3588 vendor kernel overlays which don't have overlay prefix mostly
builtin_overlays=""
if [[ $BOARDFAMILY == "rockchip-rk3588" ]] && [[ $BRANCH == "vendor" ]]; then
builtin_overlays=$(ls -1 ${overlaydir}/*.dtbo | grep -v ${overlay_prefix} | sed 's#^'${overlaydir}'/##' | sed 's/.dtbo//g')
if [[ "${scenario}" == "01" ]] || [[ "${scenario}" == "11" ]]; then

if [[ $BOARDFAMILY == "rockchip-rk3588" ]] && [[ $BRANCH == "vendor" ]]; then
builtin_overlays=$(
find "${overlaydir}"/ -name '*.dtbo' ! -name "$overlay_prefix"'*.dtbo' 2>/dev/null | \
awk -F'/' -v p="${overlay_prefix}" '{
if ($0 !~ p) {
gsub(".dtbo", "", $NF)
print $NF
}
}' | sort
)
fi
fi

if [[ "${scenario}" == "00" ]]; then
$DIALOG --title " Manage devicetree overlays " \
--no-button "Cancel" \
--yes-button "Exit" \
--yesno " The overlays provided by Armbian cannot be loaded\n by /boot/boot.scr script.\n" 11 44
exit_status=$?
if [ $exit_status == 0 ]; then
exit 0
fi
break
fi

for overlay in ${available_overlays}; do
for overlay in ${available_overlays} ${builtin_overlays}; do
local status="OFF"
grep '^overlays' ${overlayconf} | grep -qw ${overlay} && status=ON
# Raspberry Pi
grep '^dtoverlay' ${overlayconf} | grep -qw ${overlay} && status=ON
options+=( "$overlay" "" "$status")
done
selection=$($DIALOG --title "Manage devicetree overlays" --cancel-button "Back" \
--ok-button "Save" --checklist "\nUse <space> to toggle functions and save them.\nExit when you are done.\n " \
--ok-button "Save" --checklist "\nUse <space> to toggle functions and save them.\nExit when you are done.\n\n overlay_prefix=$overlay_prefix\n " \
0 0 0 "${options[@]}" 3>&1 1>&2 2>&3)
exit_status=$?
case $exit_status in
Expand Down