-
Notifications
You must be signed in to change notification settings - Fork 1
/
sign-update-files.sh
executable file
·164 lines (130 loc) · 4.74 KB
/
sign-update-files.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
#!/bin/bash
#
# Sign the LineageOS update files
#
# Author: Vasyl Gello <[email protected]>
# Date: 08.04.2019
#
SCRIPTDIR=$(readlink -f "$0")
SCRIPTDIR=$(dirname "$SCRIPTDIR")
# Check if build config file is present
if [ -e "$SCRIPTDIR/build.config" ]
then
. "$SCRIPTDIR/build.config"
else
echo "No build config found!"
echo "Exiting..."
exit 1
fi
# Check if certificates are present
if [ ! -d "$SCRIPTDIR/certs" ] || [ ! -e "$SCRIPTDIR/certs/releasekey.x509.pem" ]
then
echo "No certificates generated yet!"
echo "Please run generate-signing-keys.sh"
echo "Exiting..."
exit 1
fi
# Ask for signing keys unlock password
echo -n "Enter password: "
read -s password
echo
# Start signing the target files and forming the update files
# to be served by LineageOS updater app
for DEVICE_DIR in $(find "$SCRIPTDIR/device/" -mindepth 2 -maxdepth 2 -type d)
do
DEVICE_CANDIDATE=$(basename "$DEVICE_DIR")
FW_COUNT=$(find "$SCRIPTDIR/out/dist/" -name "*$DEVICE_CANDIDATE-target_files-*.zip" | wc -l)
[ $FW_COUNT -eq 0 ] && continue
echo "Signing $DEVICE_CANDIDATE..."
for TARGET_FILES_ZIP in $SCRIPTDIR/out/dist/*$DEVICE_CANDIDATE-target_files-*.zip
do
# Extract the required props and generate resulting ZIP name
BUILD_PROP=$(unzip -p "$TARGET_FILES_ZIP" SYSTEM/build.prop)
BUILD_FLAVOR=$(echo "$BUILD_PROP" | \
grep "ro.build.flavor" | \
grep -ioe "=.*_$DEVICE_CANDIDATE" | \
sed 's#=##' | \
sed "s|_$DEVICE_CANDIDATE.*$||")
BUILD_VERSION=$(echo "$BUILD_PROP" | \
grep "ro.cm.build.version=" | \
grep -ioe "=.*$" | \
sed 's#=##')
BUILD_RELTYPE=$(echo "$BUILD_PROP" | \
grep "ro.cm.releasetype" | \
grep -ioe "=.*$" | \
sed 's#=##')
BUILD_DATE_TS=$(echo "$BUILD_PROP" | \
grep "ro.build.date.utc" | \
grep -ioe "=[0-9]*$" | \
sed 's#=##')
BUILD_DATE=$(date +%Y%m%d -d @$BUILD_DATE_TS)
OTAFILENAME="$BUILD_FLAVOR-$BUILD_VERSION-$BUILD_DATE-$BUILD_RELTYPE-$DEVICE_CANDIDATE-signed.zip"
# Rewrite the build.prop with custom Lineage updater URI and
# inject the pinned CA certificate if required
if [ ! -z "$CUSTOM_UPDATER_URL" ]
then
unzip "$TARGET_FILES_ZIP" SYSTEM/build.prop \
META/filesystem_config.txt \
-d "$SCRIPTDIR/out/dist/"
if [ $? -ne 0 ]
then
exit 1
fi
pushd "$SCRIPTDIR/out/dist"
sed -i '/lineage.updater.uri/d' "SYSTEM/build.prop"
# Place proper updater URL property depending on build version
BUILD_VERSION_NUMBER=$(( $(echo "$BUILD_VERSION" | sed 's/\..*$//') ))
if [ $BUILD_VERSION_NUMBER -lt 15 ]
then
echo "cm.updater.uri=$CUSTOM_UPDATER_URL" >> "SYSTEM/build.prop"
else
echo "lineage.updater.uri=$CUSTOM_UPDATER_URL" >> "SYSTEM/build.prop"
fi
if [ -e "$SCRIPTDIR/certs/rootCA.pem" ]
then
mkdir "SYSTEM/etc"
cp "$SCRIPTDIR/certs/rootCA.pem" "SYSTEM/etc/updater-ca.pem"
echo "system/etc/updater-ca.pem 0 0 644 \
selabel=u:object_r:system_file:s0 capabilities=0x0" >> \
META/filesystem_config.txt
fi
zip -r "$TARGET_FILES_ZIP" SYSTEM META
if [ $? -ne 0 ]
then
popd
rm -rf SYSTEM
rm -rf META
exit 1
fi
rm -rf SYSTEM
rm -rf META
popd
fi
# sign target APKs
export ANDROID_SECURE_STORAGE_CMD="echo '$password'"
"$SCRIPTDIR/build/tools/releasetools/sign_target_files_apks" \
-o -d "$SCRIPTDIR/certs" \
"$TARGET_FILES_ZIP" \
"$TARGET_FILES_ZIP.signed"
if [ $? -ne 0 ]
then
rm -f "$TARGET_FILES_ZIP.signed"
exit 1
fi
# Sign the final OTA
"$SCRIPTDIR/build/tools/releasetools/ota_from_target_files" \
-k "$SCRIPTDIR/certs/releasekey" \
--block --backup=true \
"$TARGET_FILES_ZIP.signed" \
"$SCRIPTDIR/out/dist/$OTAFILENAME"
if [ $? -ne 0 ]
then
rm -f "$TARGET_FILES_ZIP.signed"
rm -f "$SCRIPTDIR/out/dist/$OTAFILENAME"
exit 1
fi
# Cleanup intermediate files
rm -f "$TARGET_FILES_ZIP.signed"
rm -f "$TARGET_FILES_ZIP"
done
done