Skip to content

Instantly share code, notes, and snippets.

@A2L5E0X1
Last active March 28, 2024 13:39
Show Gist options
  • Save A2L5E0X1/bbda57a9169b264aeef3234fa1c15037 to your computer and use it in GitHub Desktop.
Save A2L5E0X1/bbda57a9169b264aeef3234fa1c15037 to your computer and use it in GitHub Desktop.
Unpack Huawei DTS.img
#!/bin/bash
#
# Copyright (C) 2022 A2L5E0X1
#
#
# Simple script for unpacking DTS.img from HUAWEI kirin devices
#
#
# Changelog:
#
# rev 7: detection support for old-style emui 3 dt images
# rev 6: fix dtbs.txt generation
# rev 5: remove debugging leftovers
# rev 4: use case for DTB dialog (Thanks to @Razuuu)
# rev 3: full path support, fix gz leftover removement
# rev 2: add option to remove or keep DTBs
# rev 1: initial script, only file names are supported!
#
# CONFIG - DO NOT TOUCH
DTSIMG="$1"
DTSIMG_SHORT=$(basename "$DTSIMG")
LOG1="binwalk_log.txt"
LOG2="dtbs.txt"
# CONFIG - DO NOT TOUCH
if [ -z "$DTSIMG" ] || [ ! -f "$DTSIMG" ]; then
echo "Usage: ./unpack_dts.sh <huawei dts.img>"
exit 1
fi
# Intro
echo "/ "
echo "\ Huawei DTS.img unpacker "
echo "/ by A2L5E0X1 "
echo "\ "
echo "/ Credits: Linux4 for his research"
echo "\ "
sleep 1
# Requirements check
for requirements in dtc binwalk; do
if [ ! -f "$(which $requirements)" ]; then
echo "Please install the following missing requirement: $requirements"
exit 1
fi
done
# Check provided DTS.img
if [ -z "$(strings "$DTSIMG" | grep HSDT)" ]; then
echo "Provided image IS NOT a hisi dtimage!"
exit 1
fi
# Create or clean directories
for dir in srcs dtbs logs; do
if [ -d $dir ]; then
rm $dir/*
else
mkdir $dir
fi
done
# Removing old log files
for log in $LOG1 $LOG2; do
if [ -f $log ]; then
rm $log
fi
done
# Unpack DTS image
echo "Unpacking DTS images, please wait..."
echo "This could take some minutes, depends on your storage type."
binwalk -e "$DTSIMG" > $LOG1
if [ ! -d _$DTSIMG_SHORT.extracted ]; then
if [ ! -z "$(cat $LOG1 | grep -o -m 1 'Flattened device tree')" ]; then
echo "Old-style EMUI 3 dt image detected! Use 'extract-dtbs' from pip to unpack DTBs from your image!"
echo "This script only supports DTS.img by devices launched with EMUI 4 and newer."
exit 1
else
echo "Something went wrong! Binwalk wasn't able to unpack anything from your provided image, please check your image!"
exit 1
fi
fi
# Remove .gz.dtb leftovers
rm _$DTSIMG_SHORT.extracted/*gz*
echo "Successfully unpacked DTBs!"
echo ""
# Move dtbs to correct path
for FILE in $(find _$DTSIMG_SHORT.extracted -type f); do
mv $FILE dtbs/$(basename $FILE).dtb
done
# Remove binwalk path
rm -rf _$DTSIMG_SHORT.extracted
# Decompile dtbs
echo "Decompiling DTBs, please wait..."
echo "This could take some minutes, depends on your storage type."
for DTB in $(find dtbs -type f); do
dtc -Idtb -Odts $DTB -o srcs/$(basename $DTB .dtb).dts 2> logs/$(basename $DTB .dtb).txt
# DO NOT TOUCH
NEW_DTS_NAME=$(cat srcs/$(basename $DTB .dtb).dts | grep -e hisi,boardname | cut -f2 -d '"').dts
# DO NOT TOUCH
mv srcs/$(basename $DTB .dtb).dts srcs/$NEW_DTS_NAME
echo "$(basename $DTB): $NEW_DTS_NAME" >> $LOG2
done
echo "Successfully decompiled DTBS!"
echo ""
echo "Do you want to delete the DTBs? They are not needed in most cases and take alot of space. (Y/N)"
read -p " » " deldtb
echo ""
case $deldtb in
[Yy] ) echo "Removing DTBs..."; rm -rf dtbs; echo -e "Done!\n";;
[Nn] ) echo -e "Keeping DTBs!\n";;
* ) echo "Invalid option! Only Y/y/N/n is available! Keeping DTBs...\n";;
esac
# Done
echo "Paths:"
if [ -d dtbs ]; then
echo "Original device tree blobs: $(pwd)/dtbs"
fi
echo "Decompiled device tree sources: $(pwd)/srcs"
echo "File-List: $(pwd)/$LOG2"
@bm16ton
Copy link

bm16ton commented Mar 28, 2024

line 107 dtc -Idtb -Odts $DTB -o srcs/$(basename $DTB .dtb).dts 2&gt; logs/$(basename $DTB .dtb).txt probly should be
dtc -@ -Idtb -Odts $DTB -o srcs/$(basename $DTB .dtb).dts 2&gt; logs/$(basename $DTB .dtb).txt
I realize they are not dtbo's but (my very old android 7 bootloader anyway) doesnt seem to track without it. Probly an alignment switch wouldnt hurt either.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment