forked from Project-Awaken/android_system_update_engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trim_ota_package.py
38 lines (31 loc) · 1.28 KB
/
trim_ota_package.py
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
#!/usr/bin/env python3
import zipfile
import struct
def readPayloadMetadata(zfp: zipfile.ZipFile, entry):
_MAGIC = b'CrAU'
# 8 bytes for version, 8 bytes for manifest length, 4 bytes for metadata signature length
HEADER_STRUCT = ">4sQQL"
HEADER_LEN = struct.calcsize(HEADER_STRUCT)
with zfp.open(entry) as fp:
header = fp.read(HEADER_LEN)
(magic, version, manifest_length,
metadata_signature_len) = struct.unpack(HEADER_STRUCT, header)
assert magic == _MAGIC
assert version == 2, "Unsupported major payload version " + str(version)
print(f"{manifest_length} {metadata_signature_len}")
return header + fp.read(manifest_length + metadata_signature_len)
def main(argv):
if len(argv) != 3:
print("Usage:", argv[0], "<input file> <output file>")
return 1
infile = argv[1]
outfile = argv[2]
with zipfile.ZipFile(infile, "r") as inzfp, zipfile.ZipFile(outfile, "w") as outzfp:
for entry in inzfp.infolist():
if entry.filename.startswith("META") or entry.filename.endswith(".map") or entry.filename.endswith(".prop"):
outzfp.writestr(entry, inzfp.read(entry))
elif entry.filename == "payload.bin":
outzfp.writestr(entry, readPayloadMetadata(inzfp, entry))
if __name__ == '__main__':
import sys
sys.exit(main(sys.argv))