Skip to content

Instantly share code, notes, and snippets.

@sandorex
Last active March 29, 2024 05:58
Show Gist options
  • Save sandorex/031c006cc9f705c3640bad8d5b9d66d2 to your computer and use it in GitHub Desktop.
Save sandorex/031c006cc9f705c3640bad8d5b9d66d2 to your computer and use it in GitHub Desktop.
Script to build GSI AP file for odin
#!/bin/bash
# build-gsi.sh - https://gist.github.com/sandorex/031c006cc9f705c3640bad8d5b9d66d2
# other files needed:
# otatools-mini - https://mega.nz/file/TLgHDD6L#Kwxb04VoJDtXa6XLHxsxbHAIusXc0VHM12dqAbvRWlQ
# vbmeta.img - https://mega.nz/file/HGoGzC5J#VZUJ6n_GVCaGRzgaO2up0FhFx52swhcO0T6ewlUW5Mk
#
# Copyright 2023 Aleksandar Radivojevic (https://github.com/sandorex)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
# associated documentation files (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge, publish, distribute,
# sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or substantial
# portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
# NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
# OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
# Version 1.0.5
set -e
SDIR=$(dirname "$(realpath "$0")")
FIRMWARE_AP=$1
GSI_IMAGE=$2
OTATOOLS="$SDIR"/otatools-mini
BUILD_DIR="$PWD"/build
BUILD_GSI_DIR="$BUILD_DIR"/gsi
CLEAN_VBMETA="$SDIR"/vbmeta.img
OUTPUT_FILE=AP_CUSTOM
extra_info() {
echo "The script saves almost all files so you can re run it with different GSI and it will be a lot faster"
echo
echo "To flash the GSI put the resulting file in odin AP and use original firmware's BL and CSC the do factory reset in recovery"
}
usage() {
echo "Usage: $0 <AP.tar.md5> <GSI.img> [<super image size in bytes>]"
echo "[WARNING]: DO NOT CHANGE THE FIRMWARE AP FILE WITHOUT DELETEDING THE BUILD FOLDER"
echo
echo "This script requires following dependencies: lz4 tar md5sum"
echo
extra_info
echo
}
[[ -z "$GSI_IMAGE" ]] && usage && exit 1
[[ -z "$FIRMWARE_AP" ]] && usage && exit 1
# get absolute paths
FIRMWARE_AP=$(realpath "$FIRMWARE_AP")
GSI_IMAGE=$(realpath "$GSI_IMAGE")
# check if all utilities are installed
( hash lz4 && hash tar && hash md5sum ) || (echo "Please install the dependenices" && exit 1)
[ -d "$BUILD_DIR" ] && echo ":: [WARNING]: Build directory already exists, if you changed the firmware delete build directory and restart!"
mkdir -p "$BUILD_DIR"
mkdir -p "$BUILD_GSI_DIR"
cd "$BUILD_DIR"
echo ":: Extracting firmware AP"
tar --skip-old-files -xvf "$FIRMWARE_AP"
echo ":: Uncompressing super image"
[[ ! -f super.img ]] && lz4 super.img.lz4 super.img
echo ":: Running simg2img"
[[ ! -f super.ext4.img ]] && "$OTATOOLS"/simg2img super.img super.ext4.img
echo ":: Unpacking super image"
[[ ! -f odm.img ]] && "$OTATOOLS"/lpunpack super.ext4.img
echo ":: Getting size of partitions"
ORIG_SUPER_SIZE=$(stat -c '%s' super.ext4.img)
ODM_SIZE=$(stat -c '%s' odm.img)
PRODUCT_SIZE=$(stat -c '%s' product.img)
SYSTEM_SIZE=$(stat -c '%s' system.img)
GSI_SIZE=$(stat -c '%s' "$GSI_IMAGE")
VENDOR_SIZE=$(stat -c '%s' vendor.img)
SUM_SIZE=`expr $ODM_SIZE + $PRODUCT_SIZE + $GSI_SIZE + $VENDOR_SIZE`
SUPER_OUT_SIZE=${3:-$ORIG_SUPER_SIZE}
echo "SUPER: $ORIG_SUPER_SIZE"
echo "ODM: $ODM_SIZE"
echo "PRODUCT: $PRODUCT_SIZE"
echo "SYSTEM: $SYSTEM_SIZE"
echo "GSI: $GSI_SIZE"
echo "VENDOR: $VENDOR_SIZE"
echo
echo "SUM: $SUM_SIZE"
echo "OUT: $SUPER_OUT_SIZE"
if [[ "$SUPER_OUT_SIZE" -le "$SUM_SIZE" ]]; then
echo ":: ERROR: Output image is bigger than original super image, rerun the script with correct super image size"
exit 1
fi
# build in seperate directory so there is kinda a caching system
cd "$BUILD_GSI_DIR"
echo ":: Repacking super image"
"$OTATOOLS"/lpmake --metadata-size 65536 \
--super-name super \
--metadata-slots 2 \
--device super:$SUPER_OUT_SIZE \
--group main:$SUM_SIZE \
--partition odm:readonly:$ODM_SIZE:main \
--image odm=../odm.img \
--partition product:readonly:$PRODUCT_SIZE:main \
--image product=../product.img \
--partition system:readonly:$GSI_SIZE:main \
--image system="$GSI_IMAGE" \
--partition vendor:readonly:$VENDOR_SIZE:main \
--image vendor=../vendor.img \
--sparse \
--output ./super.img
echo ":: Compressing super image"
lz4 -f -B6 --content-size ./super.img super.img.lz4
# get back one level
cd "$BUILD_DIR"
echo ":: Copying clean vbmeta image"
[[ ! -f vbmeta.img.lz4 ]] && cp "$CLEAN_VBMETA" ./
echo ":: Compresing clean vbmeta image"
[[ ! -f vbmeta.img.lz4 ]] && lz4 -B6 --content-size ./vbmeta.img vbmeta.img.lz4
echo ":: Compresing the tar file"
# NOTE: filtering out the original super.img.lz4 so i can speed up the rebuild by keeping it
tar --create \
--format=gnu \
-b20 \
--quoting-style=escape \
--owner=0 \
--group=0 \
--totals \
--mode=644 \
-f "$OUTPUT_FILE".tar \
-C "$BUILD_GSI_DIR" super.img.lz4 \
--exclude super.img.lz4 \
-C "$BUILD_DIR" *.lz4
echo ":: Adding checksum to the tar file"
md5sum -t "$OUTPUT_FILE".tar >> "$OUTPUT_FILE".tar
echo ":: Renaming the tar file file"
mv "$OUTPUT_FILE".tar "$SDIR"/"$OUTPUT_FILE".tar.md5
echo ":: Successfully created '${OUTPUT_FILE}.tar.md5'"
echo
extra_info
echo
echo "You can delete the build directory now"
echo
@verysillycat
Copy link

verysillycat commented Mar 29, 2024

thank you so much.

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