Skip to content

Instantly share code, notes, and snippets.

@bennofs
Created May 28, 2019 19:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bennofs/480078ad80d833350251c9a7a5838239 to your computer and use it in GitHub Desktop.
Save bennofs/480078ad80d833350251c9a7a5838239 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import argparse
import shutil
import sys
from hashlib import sha256
from zipfile import ZipFile
# download latest OTA from https://www.oneplus.com/support/softwareupgrade
# this has been extracted from OnePlus5TOxygen_43_OTA_038_all_1808082017_453d65d4235346a0.zip
# by deleting parts that are not relevant for firmware updates
# the hash is so that we notice if the updater script changes (and the changes might require different modifications)
UPDATER_SCRIPT_HASH = '51d2853b9e387966402b5715099d76ab627ea3e6c386970f1a6dfdde7b498e63'
firmware_script = r'''
ui_print("Firmware Updater Oneplus 5T");
getprop("ro.display.series") == "OnePlus 5T" || abort("E3004: This package is for \"OnePlus 5T\" devices; this is a \"" + getprop("ro.display.series") + "\".");
show_progress(0.200000, 10);
ui_print("Writing static_nvbk image...");
package_extract_file("RADIO/static_nvbk.bin", "/dev/block/bootdevice/by-name/oem_stanvbk");
# ---- radio update tasks ----
ui_print("Patching firmware images...");
ifelse(msm.boot_update("main"), (
package_extract_file("firmware-update/cmnlib64.mbn", "/dev/block/bootdevice/by-name/cmnlib64");
package_extract_file("firmware-update/cmnlib.mbn", "/dev/block/bootdevice/by-name/cmnlib");
package_extract_file("firmware-update/hyp.mbn", "/dev/block/bootdevice/by-name/hyp");
package_extract_file("firmware-update/pmic.elf", "/dev/block/bootdevice/by-name/pmic");
package_extract_file("firmware-update/tz.mbn", "/dev/block/bootdevice/by-name/tz");
package_extract_file("firmware-update/abl.elf", "/dev/block/bootdevice/by-name/abl");
package_extract_file("firmware-update/devcfg.mbn", "/dev/block/bootdevice/by-name/devcfg");
package_extract_file("firmware-update/keymaster.mbn", "/dev/block/bootdevice/by-name/keymaster");
package_extract_file("firmware-update/xbl.elf", "/dev/block/bootdevice/by-name/xbl");
package_extract_file("firmware-update/rpm.mbn", "/dev/block/bootdevice/by-name/rpm");
), "");
ifelse(msm.boot_update("backup"), (
package_extract_file("firmware-update/cmnlib64.mbn", "/dev/block/bootdevice/by-name/cmnlib64bak");
package_extract_file("firmware-update/cmnlib.mbn", "/dev/block/bootdevice/by-name/cmnlibbak");
package_extract_file("firmware-update/hyp.mbn", "/dev/block/bootdevice/by-name/hypbak");
package_extract_file("firmware-update/tz.mbn", "/dev/block/bootdevice/by-name/tzbak");
package_extract_file("firmware-update/abl.elf", "/dev/block/bootdevice/by-name/ablbak");
package_extract_file("firmware-update/keymaster.mbn", "/dev/block/bootdevice/by-name/keymasterbak");
package_extract_file("firmware-update/xbl.elf", "/dev/block/bootdevice/by-name/xblbak");
package_extract_file("firmware-update/rpm.mbn", "/dev/block/bootdevice/by-name/rpmbak");
), "");
msm.boot_update("finalize");
package_extract_file("firmware-update/logo.bin", "/dev/block/bootdevice/by-name/LOGO");
package_extract_file("firmware-update/NON-HLOS.bin", "/dev/block/bootdevice/by-name/modem");
package_extract_file("firmware-update/adspso.bin", "/dev/block/bootdevice/by-name/dsp");
package_extract_file("firmware-update/BTFM.bin", "/dev/block/bootdevice/by-name/bluetooth");
set_progress(1.000000);
'''.encode("utf-8")
parser = argparse.ArgumentParser(description="Make OnePlus 5T firmware OTA from OxygenOS OTA")
parser.add_argument('ota', type=argparse.FileType('rb'))
parser.add_argument('outfile', type=argparse.FileType('wb'))
def main(args):
ota = ZipFile(args.ota)
if sha256(ota.read("META-INF/com/google/android/updater-script")).hexdigest() != UPDATER_SCRIPT_HASH:
raise RuntimeError("updater-script has changed. please review the new script and then change UPDATE_SCRIPT_HASH and firmware_script")
out = ZipFile(args.outfile, 'x')
required_files = {
"META-INF/com/android/metadata",
"META-INF/com/android/otacert",
"META-INF/com/google/android/update-binary",
}
for name in ota.namelist():
if name.startswith("firmware-update/") or name.startswith("RADIO/") or name in required_files:
with ota.open(name, "r") as source:
with out.open(name, "w") as dest:
print(f"add {name}", file=sys.stderr)
shutil.copyfileobj(source, dest)
out.writestr("META-INF/com/google/android/updater-script", firmware_script)
print("done", file=sys.stderr)
if __name__ == '__main__':
args = parser.parse_args()
main(args)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment