From 42f56c7620003bc5ab6505be6c0af4c3664e7557 Mon Sep 17 00:00:00 2001 From: CodeChenL <2540735020@qq.com> Date: Mon, 8 Jan 2024 21:00:52 +0800 Subject: [PATCH] Refactor dtbo_is_compatible --- src/usr/lib/rsetup/cli/u-boot-menu.sh | 31 +----------- src/usr/lib/rsetup/mod/dtbo_is_compatible | 32 ++++++++++++ src/usr/lib/rsetup/tui/overlay/overlay.sh | 59 ++--------------------- 3 files changed, 37 insertions(+), 85 deletions(-) create mode 100755 src/usr/lib/rsetup/mod/dtbo_is_compatible diff --git a/src/usr/lib/rsetup/cli/u-boot-menu.sh b/src/usr/lib/rsetup/cli/u-boot-menu.sh index d6bd7d28..c9f02be5 100644 --- a/src/usr/lib/rsetup/cli/u-boot-menu.sh +++ b/src/usr/lib/rsetup/cli/u-boot-menu.sh @@ -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" @@ -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 -} diff --git a/src/usr/lib/rsetup/mod/dtbo_is_compatible b/src/usr/lib/rsetup/mod/dtbo_is_compatible new file mode 100755 index 00000000..b5687e8a --- /dev/null +++ b/src/usr/lib/rsetup/mod/dtbo_is_compatible @@ -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) diff --git a/src/usr/lib/rsetup/tui/overlay/overlay.sh b/src/usr/lib/rsetup/tui/overlay/overlay.sh index 1592ed0a..0fb3141e 100644 --- a/src/usr/lib/rsetup/tui/overlay/overlay.sh +++ b/src/usr/lib/rsetup/tui/overlay/overlay.sh @@ -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'}"