|
1
|
|
|
#!/usr/bin/env python3 |
|
2
|
|
|
# -*- coding: utf-8 -*- |
|
3
|
|
|
|
|
4
|
|
|
# pylint: disable=C0111,C0326,C0103 |
|
5
|
|
|
|
|
6
|
|
|
"""Download a given firmware file.""" |
|
7
|
|
|
|
|
8
|
|
|
import os |
|
9
|
|
|
import sys |
|
10
|
|
|
|
|
11
|
|
|
from tcllib import argparser |
|
12
|
|
|
from tcllib.devices import DesktopDevice |
|
13
|
|
|
from tcllib.dumpmgr import write_info_if_dumps_found |
|
14
|
|
|
from tcllib.requests import (DownloadRequest, EncryptHeaderRequest, |
|
15
|
|
|
RequestRunner, ServerSelector) |
|
16
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
dpdesc = """ |
|
19
|
|
|
Downloads the given firmware file. |
|
20
|
|
|
""" |
|
21
|
|
|
dp = argparser.DefaultParser(__file__, dpdesc) |
|
22
|
|
|
dp.add_argument("prd", nargs=1, help="CU Reference #, e.g. PRD-63117-011") |
|
23
|
|
|
dp.add_argument("targetversion", nargs=1, help="Firmware version to download, e.g. AAN990") |
|
24
|
|
|
dp.add_argument("fwid", nargs=1, help="Numeric firmware file id, e.g. 268932") |
|
25
|
|
|
dp.add_argument("-o", "--ota", metavar="FROMVERSION", help="download OTA from specified version (switches mode to OTA)", type=str) |
|
26
|
|
|
dp.add_argument("-i", "--imei", help="use specified IMEI instead of default", type=str) |
|
27
|
|
|
dp.add_argument("-t", "--type", help="download device (default to desktop)", default="desktop", type=str, choices=["desktop", "mobile"]) |
|
28
|
|
|
dp.add_argument("--rawmode", help="override --mode with raw value (2=OTA, 4=FULL)", metavar="MODE") |
|
29
|
|
|
dp.add_argument("--rawcltp", help="override --type with raw value (10=MOBILE, 2010=DESKTOP)", metavar="CLTP") |
|
30
|
|
|
args = dp.parse_args(sys.argv[1:]) |
|
31
|
|
|
|
|
32
|
|
|
dev = DesktopDevice() |
|
33
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
def sel_mode(defaultmode, rawval): |
|
36
|
|
|
"""Handle custom mode.""" |
|
37
|
|
|
if rawval: |
|
38
|
|
|
return rawval |
|
39
|
|
|
return defaultmode |
|
40
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
def sel_cltp(txtmode, rawval): |
|
43
|
|
|
"""Handle custom CLTP.""" |
|
44
|
|
|
if rawval: |
|
45
|
|
|
return rawval |
|
46
|
|
|
if txtmode == "mobile": |
|
47
|
|
|
return dev.CLTP_STATES["MOBILE"] |
|
48
|
|
|
return dev.CLTP_STATES["DESKTOP"] |
|
49
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
if args.imei: |
|
52
|
|
|
print("Use specified IMEI: {}".format(args.imei)) |
|
53
|
|
|
dev.imei = args.imei |
|
54
|
|
|
|
|
55
|
|
|
dev.curef = args.prd[0] |
|
56
|
|
|
if args.ota: |
|
57
|
|
|
dev.fwver = args.ota[0] |
|
58
|
|
|
dev.mode = sel_mode(dev.MODE_STATES["OTA"], args.rawmode) |
|
59
|
|
|
else: |
|
60
|
|
|
dev.fwver = args.targetversion[0] |
|
61
|
|
|
dev.mode = sel_mode(dev.MODE_STATES["FULL"], args.rawmode) |
|
62
|
|
|
dev.cltp = sel_cltp(args.type, args.rawcltp) |
|
63
|
|
|
|
|
64
|
|
|
print("Mode: {}".format(dev.mode)) |
|
65
|
|
|
print("CLTP: {}".format(dev.cltp)) |
|
66
|
|
|
|
|
67
|
|
|
runner = RequestRunner(ServerSelector()) |
|
68
|
|
|
runner.max_tries = 20 |
|
69
|
|
|
|
|
70
|
|
|
tv = args.targetversion[0] |
|
71
|
|
|
fw_id = args.fwid[0] |
|
72
|
|
|
dlr = DownloadRequest(dev, tv, fw_id) |
|
73
|
|
|
runner.run(dlr) |
|
74
|
|
|
if not dlr.success: |
|
75
|
|
|
print("ERROR: {}".format(dlr.error)) |
|
76
|
|
|
sys.exit(3) |
|
77
|
|
|
|
|
78
|
|
|
dlrres = dlr.get_result() |
|
79
|
|
|
print(dlrres.pretty_xml()) |
|
80
|
|
|
|
|
81
|
|
|
for s in dlrres.slaves: |
|
82
|
|
|
print("http://{}{}".format(s, dlrres.fileurl)) |
|
83
|
|
|
|
|
84
|
|
|
for s in dlrres.s3_slaves: |
|
85
|
|
|
print("http://{}{}".format(s, dlrres.s3_fileurl)) |
|
86
|
|
|
|
|
87
|
|
|
if dev.mode == dev.MODE_STATES["FULL"]: |
|
88
|
|
|
encrun = RequestRunner(ServerSelector(dlrres.encslaves), https=False) |
|
89
|
|
|
encrun.max_tries = 20 |
|
90
|
|
|
hdr = EncryptHeaderRequest(dlrres.fileurl) |
|
91
|
|
|
encrun.run(hdr) |
|
92
|
|
|
if hdr.success: |
|
93
|
|
|
hdrres = hdr.get_result() |
|
94
|
|
|
headname = "header_{}.bin".format(tv) |
|
95
|
|
|
headdir = "headers" |
|
96
|
|
|
if not os.path.exists(headdir): |
|
97
|
|
|
os.makedirs(headdir) |
|
98
|
|
|
if len(hdrres.rawdata) == 4194320: |
|
99
|
|
|
print("Header length check passed. Writing to {}.".format(headname)) |
|
100
|
|
|
with open(os.path.join(headdir, headname), "wb") as f: |
|
101
|
|
|
f.write(hdrres.rawdata) |
|
102
|
|
|
else: |
|
103
|
|
|
print("Header length invalid ({}).".format(len(hdrres.rawdata))) |
|
104
|
|
|
|
|
105
|
|
|
write_info_if_dumps_found() |
|
106
|
|
|
|