Skip to content

Commit

Permalink
Refactor parse_dtbo
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeChenL committed Jan 8, 2024
1 parent f06452b commit e45f586
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 39 deletions.
21 changes: 2 additions & 19 deletions src/usr/lib/rsetup/cli/u-boot-menu.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
# shellcheck source=src/usr/lib/rsetup/mod/overlay.sh
source "/usr/lib/rsetup/mod/overlay.sh"

PARSE_DTBO_METADATA="/usr/lib/rsetup/mod/parse_dtbo_metadata"

ALLOWED_RCONFIG_FUNC+=("load_u-boot_setting")

check_overlay_conflict_init() {
Expand All @@ -14,8 +12,8 @@ check_overlay_conflict_init() {

check_overlay_conflict() {
local name resources res i
mapfile -t resources < <(parse_dtbo "$1" "exclusive")
mapfile -t name < <(parse_dtbo "$1" "title" "$(basename "$1")")
mapfile -t resources < <(parse_dtbo "exclusive" "$1")
mapfile -t name < <(parse_dtbo --default-value "file" "title" "$1")

for res in "${resources[@]}"
do
Expand Down Expand Up @@ -135,21 +133,6 @@ rebuild_overlays() {
mv "$new_overlays" "$old_overlays"
}

parse_dtbo() {
local output
output="$(dtc -I dtb -O dts "$1" 2>/dev/null | dtc -I dts -O yaml 2>/dev/null | "$PARSE_DTBO_METADATA" "$2")"

if (( $# >= 3 ))
then
if [[ "${output}" == "null" ]]
then
echo "$3"
return
fi
fi
echo "${output}"
}

dtbo_is_compatible() {
if [[ ! -f /sys/firmware/devicetree/base/compatible ]]
then
Expand Down
35 changes: 35 additions & 0 deletions src/usr/lib/rsetup/mod/parse_dtbo
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-

import yaml, re, os, argparse

def status(file):
if ".disabled" == os.path.splitext(file)[-1]:
print("OFF")
else:
print("ON")

def basename(file):
print(re.match(r'(.+\.dtbo)',os.path.basename(file)).group(1))

parser = argparse.ArgumentParser(description='Parse dtbo metadata data')
parser.add_argument('--show-overlays', action='store_true', default=False, help='Control print DTBO title,enable status and DTBO filename')
parser.add_argument('--default-value', metavar='string', default="null", help='error default string')
parser.add_argument('field', metavar='string', help='Metadata field')
parser.add_argument('filename', nargs='+', metavar='filename', help='DTBO filename list')
args = parser.parse_args()

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

for file in args.filename:
try:
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"]
print(metadata[args.field][0].replace("\0", "\n"))
except Exception:
if args.default_value == "file":
basename(file)
else:
print(args.default_value)
if args.show_overlays:
status(file)
basename(file)
12 changes: 0 additions & 12 deletions src/usr/lib/rsetup/mod/parse_dtbo_metadata

This file was deleted.

2 changes: 2 additions & 0 deletions src/usr/lib/rsetup/tui/main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ source "/usr/lib/rsetup/tui/system/system.sh"
source "/usr/lib/rsetup/tui/task/task.sh"
source "/usr/lib/rsetup/tui/user/user.sh"

export PATH="$PATH:/usr/lib/rsetup/mod"

if $DEBUG
then
source "/usr/lib/rsetup/tui/test/test.sh"
Expand Down
16 changes: 8 additions & 8 deletions src/usr/lib/rsetup/tui/overlay/overlay.sh
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ __overlay_filter_worker() {
fi

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

echo -e "${title[0]}\0${state}\0${overlay_name}" >&100
}
Expand Down Expand Up @@ -167,8 +167,8 @@ __overlay_validate() {
fi

local title package
mapfile -t title < <(parse_dtbo "$U_BOOT_FDT_OVERLAYS_DIR/$item"* "title" "$(basename "$item")")
mapfile -t package < <(parse_dtbo "$U_BOOT_FDT_OVERLAYS_DIR/$item"* "package")
mapfile -t title < <(parse_dtbo --default-value "file" "title" "$U_BOOT_FDT_OVERLAYS_DIR/$item"*)
mapfile -t package < <(parse_dtbo "package" "$U_BOOT_FDT_OVERLAYS_DIR/$item"*)
if [[ "${package[0]}" != "null" ]]
then
if ! __depends_package "${title[0]}" "${package[@]}"
Expand Down Expand Up @@ -213,11 +213,11 @@ __overlay_info() {
for i in "${RSETUP_CHECKLIST_STATE_NEW[@]}"
do
item="$(checklist_getitem "$i")"
mapfile -t title < <(parse_dtbo "$U_BOOT_FDT_OVERLAYS_DIR/$item"* "title")
mapfile -t category < <(parse_dtbo "$U_BOOT_FDT_OVERLAYS_DIR/$item"* "category")
mapfile -t exclusive < <(parse_dtbo "$U_BOOT_FDT_OVERLAYS_DIR/$item"* "exclusive")
mapfile -t package < <(parse_dtbo "$U_BOOT_FDT_OVERLAYS_DIR/$item"* "package")
description="$(parse_dtbo "$U_BOOT_FDT_OVERLAYS_DIR/$item"* "description")"
mapfile -t title < <(parse_dtbo "title" "$U_BOOT_FDT_OVERLAYS_DIR/$item"*)
mapfile -t category < <(parse_dtbo "category" "$U_BOOT_FDT_OVERLAYS_DIR/$item"*)
mapfile -t exclusive < <(parse_dtbo "exclusive" "$U_BOOT_FDT_OVERLAYS_DIR/$item"*)
mapfile -t package < <(parse_dtbo "package" "$U_BOOT_FDT_OVERLAYS_DIR/$item"*)
description="$(parse_dtbo "description" "$U_BOOT_FDT_OVERLAYS_DIR/$item"*)"
if (( ${#title[@]} == 1 )) && [[ "${title[0]}" == "null" ]]
then
title=( "$item" )
Expand Down

0 comments on commit e45f586

Please sign in to comment.