summaryrefslogtreecommitdiff
path: root/extract-blobs-from-zip.sh
blob: 5d0f8728589349ab95a80906ca3bf10d14ba3af4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
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