-
Notifications
You must be signed in to change notification settings - Fork 4
/
rooted-ota.sh
executable file
·449 lines (353 loc) · 14.7 KB
/
rooted-ota.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
#!/usr/bin/env bash
# Requires git, jq, and curl
KEY_AVB=${KEY_AVB:-avb.key}
KEY_OTA=${KEY_OTA:-ota.key}
CERT_OTA=${CERT_OTA:-ota.crt}
# Or else, set these env vars
KEY_AVB_BASE64=${KEY_AVB_BASE64:-''}
KEY_OTA_BASE64=${KEY_OTA_BASE64:-''}
CERT_OTA_BASE64=${CERT_OTA_BASE64:-''}
# Set these env vars, or else these params will be queries interactively
# PASSPHRASE_AVB
# PASSPHRASE_OTA
# Enable debug output only after sensitive vars have been set, to reduce risk of leak
DEBUG=${DEBUG:-''}
if [[ -n "${DEBUG}" ]]; then set -x; fi
# Mandatory params
DEVICE_ID=${DEVICE_ID:-} # See here for device IDs https://grapheneos.org/releases
GITHUB_TOKEN=${GITHUB_TOKEN:-''}
GITHUB_REPO=${GITHUB_REPO:-''}
# Optional
# If you want an OTA patched with magisk, set the preinit for your device
MAGISK_PREINIT_DEVICE=${MAGISK_PREINIT_DEVICE:-}
# Skip creation of rootless OTA by setting to "true"
SKIP_ROOTLESS=${SKIP_ROOTLESS:-'false'}
# https://grapheneos.org/releases#stable-channel
OTA_VERSION=${OTA_VERSION:-'latest'}
# It's recommended to pin magisk version in combination with AVB_ROOT_VERSION.
# Breaking changes in magisk might need to be adapted in new avbroot version
# Find latest magisk version here: https://github.com/topjohnwu/Magisk/releases, or:
# curl --fail -sL -I -o /dev/null -w '%{url_effective}' https://github.com/topjohnwu/Magisk/releases/latest | sed 's/.*\/tag\///;'
MAGISK_VERSION=${MAGISK_VERSION:-'v28.0'}
SKIP_CLEANUP=${SKIP_CLEANUP:-''}
# Set asset released by this script to latest version, even when OTA_VERSION already exists for this device
FORCE_OTA_SERVER_UPLOAD=${FORCE_OTA_SERVER_UPLOAD:-'false'}
# Skip setting asset released by this script to latest version, even when OTA_VERSION is latest for this device
# Takes precedence over FORCE_OTA_SERVER_UPLOAD
SKIP_OTA_SERVER_UPLOAD=${SKIP_OTA_SERVER_UPLOAD:-'false'}
# Upload OTA to test folder on OTA server
UPLOAD_TEST_OTA=${UPLOAD_TEST_OTA:-false}
OTA_CHANNEL=${OTA_CHANNEL:-stable} # Alternative: 'alpha'
OTA_BASE_URL="https://releases.grapheneos.org"
AVB_ROOT_VERSION=3.9.0
CUSTOTA_VERSION=5.2
CHENXIAOLONG_PK='ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIDOe6/tBnO7xZhAWXRj3ApUYgn+XZ0wnQiXM8B7tPgv4'
set -o nounset -o pipefail -o errexit
declare -A POTENTIAL_ASSETS
function generateKeys() {
downloadAvBroot
# https://github.com/chenxiaolong/avbroot/tree/077a80f4ce7233b0e93d4a1477d09334af0da246#generating-keys
# Generate the AVB and OTA signing keys.
.tmp/avbroot key generate-key -o $KEY_AVB
.tmp/avbroot key generate-key -o $KEY_OTA
# Convert the public key portion of the AVB signing key to the AVB public key metadata format.
# This is the format that the bootloader requires when setting the custom root of trust.
.tmp/avbroot key extract-avb -k $KEY_AVB -o avb_pkmd.bin
# Generate a self-signed certificate for the OTA signing key. This is used by recovery to verify OTA updates when sideloading.
.tmp/avbroot key generate-cert -k $KEY_OTA -o $CERT_OTA
echo Upload these to your CI server, if necessary.
echo The script takes these values as env or file
key2base64
}
function key2base64() {
KEY_AVB_BASE64=$(base64 -w0 "$KEY_AVB") && echo "KEY_AVB_BASE64=$KEY_AVB_BASE64"
KEY_OTA_BASE64=$(base64 -w0 "$KEY_OTA") && echo "KEY_OTA_BASE64=$KEY_OTA_BASE64"
CERT_OTA_BASE64=$(base64 -w0 "$CERT_OTA") && echo "CERT_OTA_BASE64=$CERT_OTA_BASE64"
export KEY_AVB_BASE64 KEY_OTA_BASE64 CERT_OTA_BASE64
}
function createAndReleaseRootedOta() {
createRootedOta
releaseOta
createOtaServerData
uploadOtaServerData
}
function createRootedOta() {
[[ "$SKIP_CLEANUP" != 'true' ]] && trap cleanup EXIT ERR
findLatestVersion
checkBuildNecessary
downloadAndroidDependencies
patchOTAs
}
function cleanup() {
echo "Cleaning up..."
rm -rf .tmp
unset KEY_AVB_BASE64 KEY_OTA_BASE64 CERT_OTA_BASE64
echo "Cleanup complete."
}
function checkBuildNecessary() {
local currentCommit
currentCommit=$(git rev-parse --short HEAD)
if [[ -n "$MAGISK_PREINIT_DEVICE" ]]; then
# e.g. oriole-2023121200-magisk-v26.4-4647f74-dirty.zip
POTENTIAL_ASSETS['magisk']="${DEVICE_ID}-${OTA_VERSION}-${currentCommit}-magisk-${MAGISK_VERSION}$(createAssetSuffix).zip"
else
printGreen "MAGISK_PREINIT_DEVICE not set for device, not creating magisk OTA"
fi
if [[ "$SKIP_ROOTLESS" != 'true' ]]; then
POTENTIAL_ASSETS['rootless']="${DEVICE_ID}-${OTA_VERSION}-${currentCommit}-rootless$(createAssetSuffix).zip"
else
printGreen "SKIP_ROOTLESS set, not creating rootless OTA"
fi
RELEASE_ID=''
local response
if [[ -z "$GITHUB_REPO" ]]; then echo "Env Var GITHUB_REPO not set, skipping check for existing release" && return; fi
echo "Potential release: ${OTA_VERSION}"
local params=()
local url="https://api.github.com/repos/${GITHUB_REPO}/releases"
if [ -n "${GITHUB_TOKEN}" ]; then
params+=("-H" "Authorization: token ${GITHUB_TOKEN}")
fi
params+=("-H" "Accept: application/vnd.github.v3+json")
response=$(
curl --fail -sL "${params[@]}" "${url}" |
jq --arg release_tag "${OTA_VERSION}" '.[] | select(.tag_name == $release_tag) | {id, tag_name, name, published_at, assets}'
)
if [[ -n ${response} ]]; then
RELEASE_ID=$(echo "$response" | jq -r '.id')
echo "Release ${OTA_VERSION} exists. ID=$RELEASE_ID"
for flavor in "${!POTENTIAL_ASSETS[@]}"; do
local POTENTIAL_ASSET_NAME="${POTENTIAL_ASSETS[$flavor]}"
echo "Checking if asset exists ${POTENTIAL_ASSET_NAME}"
selected_asset=$(echo "$response" | jq -r --arg assetName "${POTENTIAL_ASSET_NAME}" '.assets[] | select(.name == $assetName)')
if [ -n "$selected_asset" ]; then
printGreen "Asset with name '$POTENTIAL_ASSET_NAME' already released. Not creating it."
unset "POTENTIAL_ASSETS[$flavor]"
else
echo "No asset found with name '$POTENTIAL_ASSET_NAME'."
fi
done
if [ "${#POTENTIAL_ASSETS[@]}" -eq 0 ]; then
printGreen "All potential assets already exist. Exiting"
exit 0
fi
else
echo "Release ${OTA_VERSION} does not exist."
fi
}
function checkMandatoryVariable() {
for var_name in "$@"; do
local var_value="${!var_name}"
if [[ -z "$var_value" ]]; then
printRed "Missing mandatory param $var_name"
exit 1
fi
done
}
function createAssetSuffix() {
if [[ -n "$(git status --porcelain --untracked-files=no)" ]]; then
echo '-dirty'
elif [[ "${UPLOAD_TEST_OTA}" == 'true' ]]; then
echo '-test'
else
echo ''
fi
}
function downloadAndroidDependencies() {
checkMandatoryVariable 'MAGISK_VERSION' 'OTA_TARGET'
mkdir -p .tmp
if ! ls ".tmp/magisk-$MAGISK_VERSION.apk" >/dev/null 2>&1 && [[ "${POTENTIAL_ASSETS['magisk']+isset}" ]]; then
curl --fail -sLo ".tmp/magisk-$MAGISK_VERSION.apk" "https://github.com/topjohnwu/Magisk/releases/download/$MAGISK_VERSION/Magisk-$MAGISK_VERSION.apk"
fi
if ! ls ".tmp/$OTA_TARGET.zip" >/dev/null 2>&1; then
curl --fail -sLo ".tmp/$OTA_TARGET.zip" "$OTA_URL"
fi
}
function findLatestVersion() {
checkMandatoryVariable DEVICE_ID
if [[ "$MAGISK_VERSION" == 'latest' ]]; then
MAGISK_VERSION=$(curl --fail -sL -I -o /dev/null -w '%{url_effective}' https://github.com/topjohnwu/Magisk/releases/latest | sed 's/.*\/tag\///;')
fi
echo "Magisk version: $MAGISK_VERSION"
# Search for a new version grapheneos.
# e.g. https://releases.grapheneos.org/shiba-stable
if [[ "$OTA_VERSION" == 'latest' ]]; then
OTA_VERSION=$(curl --fail -sL "$OTA_BASE_URL/$DEVICE_ID-$OTA_CHANNEL" | head -n1 | awk '{print $1;}')
fi
GRAPHENE_TYPE=${GRAPHENE_TYPE:-'ota_update'} # Other option: factory
OTA_TARGET="$DEVICE_ID-$GRAPHENE_TYPE-$OTA_VERSION"
OTA_URL="$OTA_BASE_URL/$OTA_TARGET.zip"
# e.g. shiba-ota_update-2023121200
echo "OTA target: $OTA_TARGET; OTA URL: $OTA_URL"
}
function downloadAvBroot() {
downloadAndVerifyFromChenxiaolong 'avbroot' "$AVB_ROOT_VERSION"
}
function downloadAndVerifyFromChenxiaolong() {
local repo="$1"
local version="$2"
local artifact="${3:-$1}" # optional: If not set, use repo name
local url="https://github.com/chenxiaolong/${repo}/releases/download/v${version}/${artifact}-${version}-x86_64-unknown-linux-gnu.zip"
local downloadedZipFile
downloadedZipFile="$(mktemp)"
mkdir -p .tmp
if ! ls ".tmp/${artifact}" >/dev/null 2>&1; then
curl --fail -sL "${url}" > "${downloadedZipFile}"
curl --fail -sL "${url}.sig" > "${downloadedZipFile}.sig"
# Validate against author's public key
ssh-keygen -Y verify -I chenxiaolong -f <(echo "chenxiaolong $CHENXIAOLONG_PK") -n file \
-s "${downloadedZipFile}.sig" < "${downloadedZipFile}"
echo N | unzip "${downloadedZipFile}" -d .tmp
rm "${downloadedZipFile}"*
chmod +x ".tmp/${artifact}" # e.g. .tmp/custota-tool
fi
}
function patchOTAs() {
downloadAvBroot
base642key
for flavor in "${!POTENTIAL_ASSETS[@]}"; do
local targetFile=".tmp/${POTENTIAL_ASSETS[$flavor]}"
if ls "$targetFile" >/dev/null 2>&1; then
printGreen "File $targetFile already exists locally, not patching."
else
local args=()
args+=("--output" "$targetFile")
args+=("--input" ".tmp/$OTA_TARGET.zip")
args+=("--key-avb" "$KEY_AVB")
args+=("--key-ota" "$KEY_OTA")
args+=("--cert-ota" "$CERT_OTA")
if [[ "$flavor" == 'magisk' ]]; then
args+=("--magisk" ".tmp/magisk-$MAGISK_VERSION.apk")
args+=("--magisk-preinit-device" "$MAGISK_PREINIT_DEVICE")
elif [[ "$flavor" == 'rootless' ]]; then
args+=("--rootless")
fi
# If env vars not set, passphrases will be queried interactively
if [ -v PASSPHRASE_AVB ]; then
args+=("--pass-avb-env-var" "PASSPHRASE_AVB")
fi
if [ -v PASSPHRASE_OTA ]; then
args+=("--pass-ota-env-var" "PASSPHRASE_OTA")
fi
.tmp/avbroot ota patch "${args[@]}"
fi
done
}
function base642key() {
set +x # Don't expose secrets to log
if [ -n "$KEY_AVB_BASE64" ]; then
echo "$KEY_AVB_BASE64" | base64 -d >.tmp/$KEY_AVB
KEY_AVB=.tmp/$KEY_AVB
fi
if [ -n "$KEY_OTA_BASE64" ]; then
echo "$KEY_OTA_BASE64" | base64 -d >.tmp/$KEY_OTA
KEY_OTA=.tmp/$KEY_OTA
fi
if [ -n "$CERT_OTA_BASE64" ]; then
echo "$CERT_OTA_BASE64" | base64 -d >.tmp/$CERT_OTA
CERT_OTA=.tmp/$CERT_OTA
fi
if [[ -n "${DEBUG}" ]]; then set -x; fi
}
function releaseOta() {
checkMandatoryVariable 'GITHUB_REPO' 'GITHUB_TOKEN'
local response
if [[ -z "$RELEASE_ID" ]]; then
response=$(curl --fail -X POST -H "Authorization: token $GITHUB_TOKEN" \
-d "{
\"tag_name\": \"$OTA_VERSION\",
\"target_commitish\": \"main\",
\"name\": \"$OTA_VERSION\",
\"body\": \"See [Changelog](https://grapheneos.org/releases#$OTA_VERSION).\"
}" \
"https://api.github.com/repos/$GITHUB_REPO/releases")
RELEASE_ID=$(echo "$response" | jq -r '.id')
fi
for flavor in "${!POTENTIAL_ASSETS[@]}"; do
local assetName="${POTENTIAL_ASSETS[$flavor]}"
uploadFile ".tmp/$assetName" "$assetName" "application/zip"
done
}
function uploadFile() {
local sourceFileName="$1"
local targetFileName="$2"
local contentType="$3"
# Note that --data-binary might lead to out of memory
curl --fail -X POST -H "Authorization: token $GITHUB_TOKEN" \
-H "Content-Type: $contentType" \
--upload-file "$sourceFileName" \
"https://uploads.github.com/repos/$GITHUB_REPO/releases/$RELEASE_ID/assets?name=$targetFileName"
}
function createOtaServerData() {
downloadCusotaTool
for flavor in "${!POTENTIAL_ASSETS[@]}"; do
local POTENTIAL_ASSET_NAME="${POTENTIAL_ASSETS[$flavor]}"
local targetFile=".tmp/${POTENTIAL_ASSET_NAME}"
local args=()
args+=("--input" "${targetFile}")
args+=("--output" "${targetFile}.csig")
args+=("--key" "$KEY_OTA")
args+=("--cert" "$CERT_OTA")
# Keep version 1 for a while in order to stay downward compatible
args+=("--csig-version" '1')
# If env vars not set, passphrases will be queried interactively
if [ -v PASSPHRASE_OTA ]; then
args+=("--passphrase-env-var" "PASSPHRASE_OTA")
fi
.tmp/custota-tool gen-csig "${args[@]}"
mkdir -p ".tmp/${flavor}"
local args=()
args+=("--file" ".tmp/${flavor}/${DEVICE_ID}.json")
# e.g. https://github.com/schnatterer/rooted-graphene/releases/download/2023121200-v26.4-e54c67f/oriole-ota_update-2023121200.zip
# Instead of constructing the location we could also parse it from the upload response
args+=("--location" "https://github.com/$GITHUB_REPO/releases/download/$OTA_VERSION/$POTENTIAL_ASSET_NAME")
.tmp/custota-tool gen-update-info "${args[@]}"
done
}
function downloadCusotaTool() {
downloadAndVerifyFromChenxiaolong 'Custota' "$CUSTOTA_VERSION" 'custota-tool'
}
function uploadOtaServerData() {
# Update OTA server (github pages)
local current_branch current_commit current_author
current_branch=$(git rev-parse --abbrev-ref HEAD)
current_commit=$(git rev-parse --short HEAD)
current_author=$(git log -1 --format="%an <%ae>")
folderPrefix=''
if [[ "${UPLOAD_TEST_OTA}" == 'true' ]]; then
folderPrefix='test/'
fi
git checkout gh-pages
for flavor in "${!POTENTIAL_ASSETS[@]}"; do
local POTENTIAL_ASSET_NAME="${POTENTIAL_ASSETS[$flavor]}"
local targetFile="${folderPrefix}${flavor}/${DEVICE_ID}.json"
uploadFile ".tmp/${POTENTIAL_ASSET_NAME}.csig" "$POTENTIAL_ASSET_NAME.csig" "application/octet-stream"
mkdir -p "${folderPrefix}${flavor}"
# update only, if current $DEVICE_ID.json does not contain $OTA_VERSION
# We don't want to trigger users to upgrade on new commits from this repo or new magisk versions
# They can manually upgrade by downloading the OTAs from the releases and "adb sideload" them
if ! grep -q "$OTA_VERSION" "${targetFile}" || [[ "$FORCE_OTA_SERVER_UPLOAD" == 'true' ]] && [[ "$SKIP_OTA_SERVER_UPLOAD" != 'true' ]]; then
cp ".tmp/${flavor}/$DEVICE_ID.json" "${targetFile}"
git add "${targetFile}"
elif grep -q "${OTA_VERSION}" "${targetFile}"; then
printGreen "Skipping update of OTA server, because ${OTA_VERSION} already in ${folderPrefix}${flavor}/${DEVICE_ID}.json and FORCE_OTA_SERVER_UPLOAD is false."
else
printGreen "Skipping update of OTA server, because SKIP_OTA_SERVER_UPLOAD is true."
fi
done
if ! git diff-index --quiet HEAD; then
# Commit and push only when there are changes
git config user.name "GitHub Actions" && git config user.email "[email protected]"
git commit \
--message "Update device $DEVICE_ID basing on commit $current_commit" \
--author="$current_author"
git push origin gh-pages
fi
# Switch back to the original branch
git checkout "$current_branch"
}
function printGreen() {
echo -e "\e[32m$1\e[0m"
}
function printRed() {
echo -e "\e[31m$1\e[0m"
}