This commit is contained in:
Evgeny Zinoviev 2022-10-13 21:41:00 +03:00
commit bb86f32181

162
extract-blobs-from-zip.sh Executable file
View File

@ -0,0 +1,162 @@
#!/bin/bash
# related documentation:
# https://wiki.lineageos.org/extracting_blobs_from_zips
set -e
#set -x
BOLD=$(tput bold)
RST=$(tput sgr0)
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
YELLOW=$(tput setaf 3)
CYAN=$(tput setaf 6)
PROGNAME="$0"
echoinfo() {
>&2 echo "${CYAN}$@${RST}"
}
echoerr() {
>&2 echo "${RED}${BOLD}error:${RST}${RED} $@${RST}"
}
echowarn() {
>&2 echo "${YELLOW}${BOLD}warning:${RST}${YELLOW} $@${RST}"
}
die() {
echoerr "$@"
exit 1
}
usage() {
cat <<-_EOF
usage: $PROGNAME OPTIONS
Options:
-i|--input FILE Path to installable zip
-o|--output DIR Directory to extract to
--skip-unzipping
--los-device-directory DIR
_EOF
exit 1
}
installed() {
command -v "$1" > /dev/null
return $?
}
block_ota() {
[ -d system ] && die "system: directory already exists!"
[ -f super.transfer.list ] && die "super.transfer.list: not supported"
local f= file=
for f in system vendor; do
if [ ! -f "${f}.new.dat.br" ]; then continue; fi
echoinfo "extracting $f..."
brotli --decompress --output="${f}.new.dat" "${f}.new.dat.br"
sdat2img.py "${f}.transfer.list" "${f}.new.dat" "${f}.img"
done
mkdir system
[ -f vendor.img ] && mkdir system/vendor
echoinfo "mounting system..."
sudo mount system.img system/
if [ -f vendor.img ]; then
echoinfo "mounting vendor..."
sudo mount vendor.img system/vendor/
fi
local workdir="$(pwd)"
pushd "$LOS_DEVICE_DIR"
echoinfo "launching extract-files.sh..."
./extract-files.sh "$workdir"
popd
if [ -f vendor.img ]; then
echoinfo "unmounting vendor..."
sudo umount system/vendor/
fi
echoinfo "unmounting system..."
sudo umount system/
}
payload_ota() {
die "payload_ota: not implemented"
}
filebased_ota() {
die "filebased_ota: not implemented"
}
[[ $# -lt 1 ]] && usage
for prog in brotli sdat2img.py; do
if ! installed $prog; then
die "$prog: command not found"
fi
done
INPUT=
OUTPUT=
SKIP_UNZIP=
LOS_DEVICE_DIR=
while [[ $# -gt 0 ]]; do
case $1 in
-i|--input)
INPUT="$2"
shift
;;
-o|--output)
OUTPUT="$2"
shift
;;
--los-device-directory)
LOS_DEVICE_DIR="$2"
shift
;;
--skip-unzipping)
SKIP_UNZIP=1
;;
*)
die "unrecognized option $1"
exit 1
;;
esac
shift
done
[ -z "$INPUT" ] && die "no input file specified"
[ -z "$OUTPUT" ] && die "no output directory specified"
[ -z "$LOS_DEVICE_DIR" ] && die "no lineageos device directory specified"
[ -d "$LOS_DEVICE_DIR" ] || die "$LOS_DEVICE_DIR: no such directory"
[ -f "$INPUT" ] || die "$INPUT: no such file"
[ -z "$SKIP_UNZIP" ] && [ -d "$OUTPUT" ] && die "$OUTPUT: directory already exists"
if [ -z "$SKIP_UNZIP" ]; then
unzip -d "$OUTPUT" "$INPUT"
elif [ ! -d "$OUTPUT" ]; then
die "$OUTPUT: no such directory"
fi
pushd "$OUTPUT"
if [ -f payload.bin ]; then
payload_ota
elif [ -f system.transfer.list ]; then
block_ota
else
filebased_ota
fi
popd