-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulate_ota.py
147 lines (124 loc) · 4.97 KB
/
simulate_ota.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#
# Copyright (C) 2020 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""Tools for running host side simulation of an OTA update."""
from __future__ import print_function
import argparse
import filecmp
import os
import shutil
import subprocess
import sys
import tempfile
import zipfile
import update_payload
def extract_file(zip_file_path, entry_name, target_file_path):
"""Extract a file from zip archive into |target_file_path|"""
with open(target_file_path, 'wb') as out_fp:
if isinstance(zip_file_path, zipfile.ZipFile):
with zip_file_path.open(entry_name) as fp:
shutil.copyfileobj(fp, out_fp)
elif os.path.isdir(zip_file_path):
with open(os.path.join(zip_file_path, entry_name), "rb") as fp:
shutil.copyfileobj(fp, out_fp)
def is_sparse_image(filepath):
with open(filepath, 'rb') as fp:
# Magic for android sparse image format
# https://source.android.com/devices/bootloader/images
return fp.read(4) == b'\x3A\xFF\x26\xED'
def extract_img(zip_archive, img_name, output_path):
entry_name = "IMAGES/" + img_name + ".img"
extract_file(zip_archive, entry_name, output_path)
if is_sparse_image(output_path):
raw_img_path = output_path + ".raw"
subprocess.check_output(["simg2img", output_path, raw_img_path])
os.rename(raw_img_path, output_path)
def run_ota(source, target, payload_path, tempdir):
"""Run an OTA on host side"""
payload = update_payload.Payload(payload_path)
payload.Init()
if zipfile.is_zipfile(source):
source = zipfile.ZipFile(source)
if zipfile.is_zipfile(target):
target = zipfile.ZipFile(target)
old_partitions = []
new_partitions = []
expected_new_partitions = []
for part in payload.manifest.partitions:
name = part.partition_name
old_image = os.path.join(tempdir, "source_" + name + ".img")
new_image = os.path.join(tempdir, "target_" + name + ".img")
print("Extracting source image for", name)
extract_img(source, name, old_image)
print("Extracting target image for", name)
extract_img(target, name, new_image)
old_partitions.append(old_image)
scratch_image_name = new_image + ".actual"
new_partitions.append(scratch_image_name)
with open(scratch_image_name, "wb") as fp:
fp.truncate(part.new_partition_info.size)
expected_new_partitions.append(new_image)
delta_generator_args = ["delta_generator", "--in_file=" + payload_path]
partition_names = [
part.partition_name for part in payload.manifest.partitions
]
delta_generator_args.append("--partition_names=" + ":".join(partition_names))
delta_generator_args.append("--old_partitions=" + ":".join(old_partitions))
delta_generator_args.append("--new_partitions=" + ":".join(new_partitions))
subprocess.check_output(delta_generator_args)
valid = True
for (expected_part, actual_part, part_name) in \
zip(expected_new_partitions, new_partitions, partition_names):
if filecmp.cmp(expected_part, actual_part):
print("Partition `{}` is valid".format(part_name))
else:
valid = False
print(
"Partition `{}` is INVALID expected image: {} actual image: {}"
.format(part_name, expected_part, actual_part))
if not valid and sys.stdout.isatty():
input("Paused to investigate invalid partitions, press any key to exit.")
def main():
parser = argparse.ArgumentParser(
description="Run host side simulation of OTA package")
parser.add_argument(
"--source",
help="Target file zip for the source build",
required=True, nargs=1)
parser.add_argument(
"--target",
help="Target file zip for the target build",
required=True, nargs=1)
parser.add_argument(
"payload",
help="payload.bin for the OTA package, or a zip of OTA package itself",
nargs=1)
args = parser.parse_args()
print(args)
assert os.path.exists(args.source[0]), \
"source target file must point to a valid zipfile or directory"
assert os.path.exists(args.target[0]), \
"target path must point to a valid zipfile or directory"
# pylint: disable=no-member
with tempfile.TemporaryDirectory() as tempdir:
payload_path = args.payload[0]
if zipfile.is_zipfile(payload_path):
with zipfile.ZipFile(payload_path, "r") as zfp:
payload_entry_name = 'payload.bin'
zfp.extract(payload_entry_name, tempdir)
payload_path = os.path.join(tempdir, payload_entry_name)
run_ota(args.source[0], args.target[0], payload_path, tempdir)
if __name__ == '__main__':
main()