Skip to content

Commit

Permalink
Refactor dtbo_is_compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeChenL committed Jan 8, 2024
1 parent e45f586 commit 42f56c7
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 85 deletions.
31 changes: 1 addition & 30 deletions src/usr/lib/rsetup/cli/u-boot-menu.sh
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ disable_overlays() {
__rebuild_overlays_worker() {
local overlay="$1" new_overlays="$2"

if dtbo_is_compatible "$overlay"
if [[ -n "$(dtbo_is_compatible "$overlay")" ]]
then
cp "$overlay" "$new_overlays/$(basename "$overlay").disabled"
exec 100>>"$new_overlays/managed.list"
Expand Down Expand Up @@ -132,32 +132,3 @@ rebuild_overlays() {
mv "$old_overlays" "${old_overlays}_old"
mv "$new_overlays" "$old_overlays"
}

dtbo_is_compatible() {
if [[ ! -f /sys/firmware/devicetree/base/compatible ]]
then
# Assume we are running at image building stage
# Skip checking
return
fi

local overlay="$1" dtbo_compatible
mapfile -t dtbo_compatible < <(parse_dtbo "$overlay" "compatible")
if [[ "${dtbo_compatible[0]}" == "null" ]]
then
return
fi

for d in "${dtbo_compatible[@]}"
do
for p in $(xargs -0 < /sys/firmware/devicetree/base/compatible)
do
if [[ "$d" == "$p" ]]
then
return
fi
done
done

return 1
}
32 changes: 32 additions & 0 deletions src/usr/lib/rsetup/mod/dtbo_is_compatible
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-

from sys import argv, stdin, stderr
import yaml, re, os

def is_compatible(file, metadata, compatible):
try:
for dtbo_compatible in metadata["compatible"][0].split("\x00"):
if dtbo_compatible in compatible:
return True
# If the error type is KeyError it is assumed to be a third-party overlay installed by the user, which is compatible by default.
except KeyError:
return True
return False

files = argv[1:]

# If this file does not exist, it is assumed that the image is generated inside the chroot
try:
compatible= open("/sys/firmware/devicetree/base/compatible").read().split("\x00")[0:-1]
except FileNotFoundError as e:
for file in files:
print(file)
exit(0)

yaml.CLoader.add_constructor('!u8', yaml.constructor.FullConstructor.construct_yaml_seq)

for file in files:
metadata=yaml.load(os.popen("dtc -I dtb -O dts " + file + " 2>/dev/null | dtc -I dts -O yaml 2>/dev/null").read(), Loader=yaml.CLoader)[0]["metadata"]
if is_compatible(file, metadata, compatible):
print(file)
59 changes: 4 additions & 55 deletions src/usr/lib/rsetup/tui/overlay/overlay.sh
Original file line number Diff line number Diff line change
Expand Up @@ -69,70 +69,19 @@ Are you sure?"
fi
}

__overlay_filter_worker() {
local temp="$1" overlay="$2" state title overlay_name

if ! dtbo_is_compatible "$overlay"
then
return
fi

exec 100>> "$temp"
flock 100

if [[ "$overlay" == *.dtbo ]]
then
state="ON"
elif [[ "$overlay" == *.dtbo.disabled ]]
then
state="OFF"
else
return
fi

overlay_name="$(basename "$overlay" | sed -E "s/(.*\.dtbo).*/\1/")"
mapfile -t title < <(parse_dtbo --default-value "file" "title" "$overlay" )

echo -e "${title[0]}\0${state}\0${overlay_name}" >&100
}

__overlay_filter() {
local temp="$1" nproc index
local dtbos=( "$U_BOOT_FDT_OVERLAYS_DIR"/*.dtbo* )
mapfile -t index < <(eval "echo {0..$(( ${#dtbos[@]} - 1 ))}" | tr ' ' '\n')
nproc=$(nproc)

for i in "${index[@]}"
do
while (( $(jobs -r | wc -l) > nproc ))
do
sleep 0.1
done

__overlay_filter_worker "$temp" "${dtbos[$i]}" &
echo $(( i * 100 / (${index[-1]} + 1) ))
done

wait
}

__overlay_show() {
local validation="${1:-true}"
echo "Searching available overlays may take a while, please wait..." >&2
infobox "Searching available overlays may take a while, please wait..."
load_u-boot_setting

local temp
temp="$(mktemp)"
# shellcheck disable=SC2064
trap "rm -f $temp" RETURN EXIT

__overlay_filter "$temp" | gauge "Searching available overlays..." 0
local dtbos=( "$U_BOOT_FDT_OVERLAYS_DIR"/*.dtbo* )
mapfile -t dtbos < <(dtbo_is_compatible "${dtbos[@]}")

checklist_init
# Bash doesn support IFS=$'\0'
# Use array to emulate this
local items=()
mapfile -t items < <(sort "$temp" | tr $"\0" $"\n")
mapfile -t items < <(parse_dtbo --show-overlays "title" "${dtbos[@]}")
while (( ${#items[@]} >= 3 ))
do
checklist_add "${items[0]/$'\n'}" "${items[1]/$'\n'}" "${items[2]/$'\n'}"
Expand Down

0 comments on commit 42f56c7

Please sign in to comment.