Skip to content

Instantly share code, notes, and snippets.

@Linux4
Last active February 9, 2024 10:46
Show Gist options
  • Save Linux4/07d50dd8fe7437141747680fbd5dee4e to your computer and use it in GitHub Desktop.
Save Linux4/07d50dd8fe7437141747680fbd5dee4e to your computer and use it in GitHub Desktop.
Flash GSIs or even full ROMs as DSU (full ROM only works if it is compatible with currently installed ROM's kernel)
#!/bin/bash
set -e
function unsparse_if_needed() {
if [ "$(file -b $1 | cut -f1 -d ',')" = "Android sparse image" ]; then
echo "Unsparsing..."
simg2img "$1" "$1".raw
mv "$1".raw "$1"
fi
}
function unpack_img_from_zip() {
local ZIP="$1"
local IMG="$2"
local DST="$3"
local DAT_BR="$IMG".new.dat.br
local TRANSFER_LIST="$IMG".transfer.list
if [ ! -z "$(zipinfo -1 $ZIP | grep $DAT_BR)" ]; then
unzip -p "$ZIP" "$DAT_BR" > "$DST".dat.br
unzip -p "$ZIP" "$TRANSFER_LIST" > "$DST".transfer.list
rm -f "$DST".dat
brotli -d "$DST".dat.br
rm "$DST".dat.br
rm -rf sdat2img
git clone https://github.com/xpirt/sdat2img
./sdat2img/sdat2img.py "$DST".transfer.list "$DST".dat "$DST"
rm "$DST".dat "$DST".transfer.list
else
unzip -p "$ZIP" "$IMG".img > "$DST"
fi
unsparse_if_needed "$DST"
}
if [ -z "$1" ]; then
echo "Usage: $0 <GSI>"
exit 1
fi
REQUIREMENTS="adb brotli file git gunzip simg2img unzip unxz zipinfo"
for REQUIREMENT in $REQUIREMENTS; do
if [ -z "$(command -v $REQUIREMENT)" ]; then
echo "Command $REQUIREMENT is missing, please install it!"
exit 1
fi
done
IMG="$1"
EXT="$(echo $IMG | rev | cut -f1 -d '.' | rev)"
if [ "$EXT" = "xz" ]; then
echo "unxz $IMG"
unxz "$IMG"
IMG="$(echo $IMG | rev | cut -f2- -d '.' | rev)"
elif [ "$EXT" = "gz" ]; then
echo "gunzip $IMG"
gunzip -k "$IMG"
IMG="$(echo $IMG | rev | cut -f2- -d '.' | rev)"
elif [ "$EXT" = "zip" ]; then
echo "Extracting system.img"
NEWIMG="$(echo $IMG | rev | cut -f2- -d '.' | rev)"
unpack_img_from_zip "$IMG" system "$NEWIMG"
IMG="$NEWIMG"
else
unsparse_if_needed "$IMG"
fi
IMGNAME="$(basename $IMG)"
adb wait-for-device root
adb wait-for-device push "$IMG" /sdcard/
adb wait-for-device shell "gsi_tool install --userdata-size 8589934592 --install-dir /data/gsi/dsu --gsi-size $(du -b $IMG) --no-reboot < /sdcard/$IMGNAME"
if [ "$EXT" = "zip" ]; then # can contain multiple images
for PART in $(zipinfo -1 "$1"); do
EXT="$(echo $PART | cut -f2- -d '.')"
PART="$(echo $PART | cut -f1 -d '.')"
[ "$EXT" != "img" ] && [ "$EXT" != "new.dat.br" ] && continue;
if [ "$PART" = "system_ext" ] || [ "$PART" = "odm" ] || [ "$PART" = "product" ] || [ "$PART" = "vendor" ]; then
if [ "$PART" = "vendor" ] || [ "$PART" = "odm" ]; then
read -e -p "Really flash device-specific image $PART? (y/n): " -i "y" flash
[ ! -z "$flash" ] && [ "$flash" != "y" ] && continue;
fi
echo "Installing additional image: $PART"
unpack_img_from_zip "$1" "$PART" "$IMG"
adb wait-for-device push "$IMG" /sdcard
adb wait-for-device shell "gsi_tool create-partition --install-dir /data/gsi/dsu --partition-name $(echo $PART | cut -f1 -d '.') --size $(du -b $IMG) < /sdcard/$IMGNAME"
fi
done
fi
adb wait-for-device shell rm /sdcard/"$IMGNAME"
rm "$IMG"
rm -rf sdat2img
echo "You can now boot your new DSU installation, have fun!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment