1
0
mirror of https://github.com/mbirth/tcl_ota_check.git synced 2024-11-10 06:16:46 +00:00
tcl_ota_check/tcldown.py

106 lines
3.3 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# pylint: disable=C0111,C0326,C0103
2018-02-03 21:25:26 +00:00
"""Download a given firmware file."""
import os
import sys
2018-02-03 20:24:36 +00:00
2018-02-11 00:40:52 +00:00
from tcllib import argparser
from tcllib.devices import DesktopDevice
2018-02-11 00:48:47 +00:00
from tcllib.dumpmgr import write_info_if_dumps_found
2018-02-11 01:38:38 +00:00
from tcllib.requests import (DownloadRequest, EncryptHeaderRequest,
RequestRunner, ServerSelector)
2018-02-03 20:24:36 +00:00
dpdesc = """
Downloads the given firmware file.
"""
2018-02-11 00:40:52 +00:00
dp = argparser.DefaultParser(__file__, dpdesc)
dp.add_argument("prd", nargs=1, help="CU Reference #, e.g. PRD-63117-011")
dp.add_argument("targetversion", nargs=1, help="Firmware version to download, e.g. AAN990")
dp.add_argument("fwid", nargs=1, help="Numeric firmware file id, e.g. 268932")
dp.add_argument("-o", "--ota", metavar="FROMVERSION", help="download OTA from specified version (switches mode to OTA)", type=str)
dp.add_argument("-i", "--imei", help="use specified IMEI instead of default", type=str)
dp.add_argument("-t", "--type", help="download device (default to desktop)", default="desktop", type=str, choices=["desktop", "mobile"])
dp.add_argument("--rawmode", help="override --mode with raw value (2=OTA, 4=FULL)", metavar="MODE")
dp.add_argument("--rawcltp", help="override --type with raw value (10=MOBILE, 2010=DESKTOP)", metavar="CLTP")
args = dp.parse_args(sys.argv[1:])
2018-02-11 00:40:52 +00:00
dev = DesktopDevice()
2018-02-03 20:24:36 +00:00
2018-02-11 01:38:38 +00:00
def sel_mode(defaultmode, rawval):
2018-02-03 21:25:26 +00:00
"""Handle custom mode."""
if rawval:
return rawval
return defaultmode
2018-02-03 20:24:36 +00:00
def sel_cltp(txtmode, rawval):
2018-02-03 21:25:26 +00:00
"""Handle custom CLTP."""
if rawval:
return rawval
if txtmode == "mobile":
return dev.CLTP_STATES["MOBILE"]
return dev.CLTP_STATES["DESKTOP"]
2018-02-03 20:24:36 +00:00
if args.imei:
print("Use specified IMEI: {}".format(args.imei))
dev.imei = args.imei
dev.curef = args.prd[0]
if args.ota:
dev.fwver = args.ota[0]
dev.mode = sel_mode(dev.MODE_STATES["OTA"], args.rawmode)
else:
dev.fwver = args.targetversion[0]
dev.mode = sel_mode(dev.MODE_STATES["FULL"], args.rawmode)
dev.cltp = sel_cltp(args.type, args.rawcltp)
print("Mode: {}".format(dev.mode))
print("CLTP: {}".format(dev.cltp))
2018-02-11 00:40:52 +00:00
runner = RequestRunner(ServerSelector())
runner.max_tries = 20
tv = args.targetversion[0]
fw_id = args.fwid[0]
2018-02-11 00:40:52 +00:00
dlr = DownloadRequest(dev, tv, fw_id)
runner.run(dlr)
if not dlr.success:
print("ERROR: {}".format(dlr.error))
sys.exit(3)
dlrres = dlr.get_result()
print(dlrres.pretty_xml())
2018-02-11 00:40:52 +00:00
for s in dlrres.slaves:
print("http://{}{}".format(s, dlrres.fileurl))
2018-02-11 00:40:52 +00:00
for s in dlrres.s3_slaves:
print("http://{}{}".format(s, dlrres.s3_fileurl))
if dev.mode == dev.MODE_STATES["FULL"]:
2018-02-11 00:40:52 +00:00
encrun = RequestRunner(ServerSelector(dlrres.encslaves), https=False)
encrun.max_tries = 20
hdr = EncryptHeaderRequest(dlrres.fileurl)
encrun.run(hdr)
if hdr.success:
hdrres = hdr.get_result()
headname = "header_{}.bin".format(tv)
headdir = "headers"
if not os.path.exists(headdir):
os.makedirs(headdir)
if len(hdrres.rawdata) == 4194320:
print("Header length check passed. Writing to {}.".format(headname))
with open(os.path.join(headdir, headname), "wb") as f:
f.write(hdrres.rawdata)
else:
print("Header length invalid ({}).".format(len(hdrres.rawdata)))
write_info_if_dumps_found()