-
Notifications
You must be signed in to change notification settings - Fork 9
/
generate-vendor.sh
executable file
·194 lines (160 loc) · 4.98 KB
/
generate-vendor.sh
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/bin/bash
#
# Copyright 2017 Preetam J. D'Souza
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
set -e
set -u
source bytecode.sh
source mk.sh
source log.sh
readonly SCRIPT_NAME="$(basename "$0")"
readonly SCRIPT_DIR="$(readlink -f $(dirname "$0"))"
# included dependencies
readonly SIMG2IMG="${SCRIPT_DIR}/deps/bin/simg2img"
# some useful paths -- all relative to $WORKDIR
readonly TMP_DIR="tmp"
readonly IMAGE_DIR="${TMP_DIR}/factory"
readonly SMALI_TMP_DIR="${TMP_DIR}/smali"
readonly VENDOR_MK="device-vendor.mk"
readonly BLOBS_MK="device-partial.mk"
readonly MODULES_MK="Android.mk"
readonly CHECKSUMS="sha1sums.txt"
# globals
VENDOR=""
BLOBS=""
VENDOR_DIR=""
DEVICE_DIR=""
WORKDIR=""
# user options
OPT_DEVICE=""
OPT_IMAGE=""
OPT_OUT="."
OPT_INSPECT=false
help () {
cat <<EOF
Extract vendor files from a factory image and generate vendor makefiles for AOSP.
usage: $SCRIPT_NAME [OPTIONS] -d [DEVICE] -i [IMAGE ZIP]
-d, --device device codename
-i, --image factory image zip to extract blobs from
-o, --out output base dir, defaults to '.'
--inspect leave intermediate files in $TMP_DIR for inspection
EOF
}
cleanup () {
iecho "cleaning up..."
sudo umount "${IMAGE_DIR}/system" 2>/dev/null
[ "$OPT_INSPECT" = true ] || rm -rf "$TMP_DIR"
}
extract_file () {
local readonly file="$1"
local readonly dest="$2"
mkdir -p "$(dirname "$dest")"
cp "$file" "$dest"
}
extract_bytecode () {
local readonly bytecode="$1"
local readonly dest="$2"
if bytecode_is_optimized "$bytecode" ; then
iecho " de-optimizing $dest..."
PATH="${SCRIPT_DIR}/deps/jar:${PATH}" bytecode_deodex \
"$bytecode" "$dest" "${IMAGE_DIR}/system/framework/arm/boot.oat" "${TMP_DIR}/smali"
else
# just mirror the un-optimized apk over exactly
extract_file "$bytecode" "$dest"
fi
}
extract_blob () {
local readonly blob="$1"
local readonly dest="$2"
local readonly blob_extension="${blob##*.}"
case "$blob_extension" in
apk)
extract_bytecode "$blob" "$dest"
mk_add_apk "$dest" "$VENDOR" "$OPT_DEVICE"
;;
jar)
extract_bytecode "$blob" "$dest"
mk_add_jar "$dest" "$VENDOR" "$OPT_DEVICE"
;;
*)
extract_file "$blob" "$dest"
mk_mirror_file "$dest" "$VENDOR"
;;
esac
}
while [ $# -gt 0 ]; do
case "$1" in
-d|--device) OPT_DEVICE="$2"; shift 2 ;;
-i|--image) OPT_IMAGE="$2"; shift 2 ;;
-o|--out) OPT_OUT="$2"; shift 2 ;;
--inspect) OPT_INSPECT=true; shift ;;
-h|--help) help; exit 2 ;;
--) shift; break ;;
-*) fecho "unrecognized option $1"; exit 2 ;;
*) break;
esac
done
VENDOR="$(find "$SCRIPT_DIR" -type d -name "$OPT_DEVICE" | xargs -r dirname | xargs -r basename)"
if [ -z "$VENDOR" ] ; then
fecho "invalid device: '$OPT_DEVICE'"
exit 2
fi
if [ ! -f "$OPT_IMAGE" ] ; then
fecho "invalid image file: '$OPT_IMAGE'"
exit 2
fi
VENDOR_DIR="vendor/${VENDOR}/${OPT_DEVICE}"
DEVICE_DIR="${SCRIPT_DIR}/${VENDOR}/${OPT_DEVICE}"
BLOBS="${DEVICE_DIR}/proprietary-blobs.txt"
WORKDIR="${OPT_OUT}/${VENDOR_DIR}"
iecho "setting up output dir '$WORKDIR'..."
mkdir -p "$WORKDIR"
pushd "$WORKDIR" &>/dev/null
trap cleanup EXIT
iecho "preparing factory image..."
mkdir -p "$IMAGE_DIR"
unzip -j "$OPT_IMAGE" -d "$IMAGE_DIR" >/dev/null
pushd "$IMAGE_DIR" &>/dev/null
unzip image-*.zip system.img >/dev/null
"$SIMG2IMG" system.img system-raw.img
mkdir system
wecho " requesting sudo for loop mount..."
sudo mount -o loop system-raw.img system
popd &>/dev/null
iecho "generating vendor makefiles..."
mk_init "$VENDOR_DIR"
iecho "extracting vendor files from image..."
for blob in $(grep -v "#" < "$BLOBS") ; do # skips empty lines
image_blob="${IMAGE_DIR}/${blob}"
[ -f "$image_blob" ] || {
wecho " missing file from image: $blob"
continue
}
extract_blob "$image_blob" "$blob"
done
custom_modules=$(find "$DEVICE_DIR" -type f -name "*.mk")
for module in $custom_modules ; do
blob="$(grep LOCAL_SRC_FILES "$module" | cut -d ' ' -f 3)"
image_blob="${IMAGE_DIR}/${blob}"
iecho " extracting custom module $blob..."
extract_file "$image_blob" "$blob"
mk_add_custom_module "$module"
done
iecho "calculating checksums..."
{
echo "# $(basename "$OPT_IMAGE")"
find -xdev ! -path "./$TMP_DIR*" ! -path "./$CHECKSUMS" -type f | sort | xargs sha1sum
} > "$CHECKSUMS"
iecho "all tasks completed successfully"