forked from gmrt/update_payload_extractor
-
Notifications
You must be signed in to change notification settings - Fork 5
/
extract.py
executable file
·67 lines (56 loc) · 2.51 KB
/
extract.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
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
#!/usr/bin/env python3
import argparse
import errno
import os
import update_payload
from update_payload import applier
def list_content(payload_file_name):
with open(payload_file_name, 'rb') as payload_file:
payload = update_payload.Payload(payload_file)
payload.Init()
for part in payload.manifest.partitions:
print("{} ({} bytes)".format(part.partition_name,
part.new_partition_info.size))
def extract(payload_file_name, output_dir="output", old_dir="old", partition_names=None):
try:
os.makedirs(output_dir)
except OSError as e:
if e.errno != errno.EEXIST:
raise
with open(payload_file_name, 'rb') as payload_file:
payload = update_payload.Payload(payload_file)
payload.Init()
helper = applier.PayloadApplier(payload)
for part in payload.manifest.partitions:
if partition_names and part.partition_name not in partition_names:
continue
print("Extracting {}".format(part.partition_name))
output_file = os.path.join(output_dir, part.partition_name)
if payload.IsDelta():
old_file = os.path.join(old_dir, part.partition_name)
helper._ApplyToPartition(
part.operations, part.partition_name,
'install_operations', output_file,
part.new_partition_info, old_file, part.old_partition_info)
else:
helper._ApplyToPartition(
part.operations, part.partition_name,
'install_operations', output_file,
part.new_partition_info)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("payload", metavar="payload.bin",
help="Path to the payload.bin")
parser.add_argument("--output_dir", default="output",
help="Output directory")
parser.add_argument("--old_dir", default="old",
help="Old directory")
parser.add_argument("--partitions", type=str, nargs='+',
help="Name of the partitions to extract")
parser.add_argument("--list_partitions", action="store_true",
help="List the partitions included in the payload.bin")
args = parser.parse_args()
if args.list_partitions:
list_content(args.payload)
else:
extract(args.payload, args.output_dir, args.old_dir, args.partitions)