Skip to content

Instantly share code, notes, and snippets.

@eramax
Last active October 27, 2023 15:52
Show Gist options
  • Save eramax/fd2f10e0a9b4868b47c70faeb956ea4a to your computer and use it in GitHub Desktop.
Save eramax/fd2f10e0a9b4868b47c70faeb956ea4a to your computer and use it in GitHub Desktop.
Extract_dtb_dts_firmware.sh
#!/bin/bash
inp="$1" # Get the input file from the command line argument
# Check if the input file exists
if [ ! -f "$inp" ]; then
echo "File not found: $inp"
exit 1
fi
binwalk $inp > entries
lines=() # Create an array to store the lines from the input file
# Read the file into an array
mapfile -t lines < "entries"
count=${#lines[@]} # Get the number of lines in the array
d=0
for ((i = 0; i < count; i++)); do
line="${lines[i]}"
d=0
if [[ $line == *'device tree'* ]]; then
index=$(echo "$line" | awk '{print $1}')
if ((i+1 < count)); then
next_index=$(echo "${lines[i+1]}" | awk '{print $1}')
# Check if the next line is not empty or contains only whitespace
if [[ -n "$next_index" ]] && [[ "$next_index" =~ ^[0-9]+$ ]]; then
# Calculate the difference
difference=$((next_index - index))
dd if=$inp of=$index.dtb bs=1 skip=$index count=$difference status=none
d=1
fi
fi
if [ $d -eq 0 ]; then
dd if=$inp of=$index.dtb bs=1 skip=$index status=none
fi
dtc -q -s -I dtb $index.dtb -O dts -o $index.dts
echo "generated $index.dts"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment