blob: e508f50e6f541c1b9800e219999925391ff1848a [file] [log] [blame]
Kelvin Zhang576efc52020-12-01 12:06:40 -05001#
2# Copyright (C) 2020 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16
17"""Tools for running host side simulation of an OTA update."""
18
19
20from __future__ import print_function
21
22import argparse
23import filecmp
24import os
25import shutil
26import subprocess
27import sys
28import tempfile
29import zipfile
30
31import update_payload
32
33
34def extract_file(zip_file_path, entry_name, target_file_path):
35 """Extract a file from zip archive into |target_file_path|"""
36 with open(target_file_path, 'wb') as out_fp:
37 if isinstance(zip_file_path, zipfile.ZipFile):
38 with zip_file_path.open(entry_name) as fp:
39 shutil.copyfileobj(fp, out_fp)
40 elif os.path.isdir(zip_file_path):
41 with open(os.path.join(zip_file_path, entry_name), "rb") as fp:
42 shutil.copyfileobj(fp, out_fp)
43
Kelvin Zhang8c856552021-09-07 21:15:49 -070044
Kelvin Zhang576efc52020-12-01 12:06:40 -050045def is_sparse_image(filepath):
46 with open(filepath, 'rb') as fp:
47 # Magic for android sparse image format
48 # https://source.android.com/devices/bootloader/images
49 return fp.read(4) == b'\x3A\xFF\x26\xED'
50
Kelvin Zhang8c856552021-09-07 21:15:49 -070051
Kelvin Zhang576efc52020-12-01 12:06:40 -050052def extract_img(zip_archive, img_name, output_path):
53 entry_name = "IMAGES/" + img_name + ".img"
54 extract_file(zip_archive, entry_name, output_path)
55 if is_sparse_image(output_path):
56 raw_img_path = output_path + ".raw"
57 subprocess.check_output(["simg2img", output_path, raw_img_path])
58 os.rename(raw_img_path, output_path)
59
Kelvin Zhang8c856552021-09-07 21:15:49 -070060
61def run_ota(source, target, payload_path, tempdir, output_dir):
Kelvin Zhang576efc52020-12-01 12:06:40 -050062 """Run an OTA on host side"""
63 payload = update_payload.Payload(payload_path)
64 payload.Init()
Kelvin Zhang8c856552021-09-07 21:15:49 -070065 if source and zipfile.is_zipfile(source):
Kelvin Zhang576efc52020-12-01 12:06:40 -050066 source = zipfile.ZipFile(source)
Kelvin Zhang8c856552021-09-07 21:15:49 -070067 if target and zipfile.is_zipfile(target):
Kelvin Zhang576efc52020-12-01 12:06:40 -050068 target = zipfile.ZipFile(target)
Kelvin Zhang8c856552021-09-07 21:15:49 -070069 source_exist = source and (isinstance(
70 source, zipfile.ZipFile) or os.path.exists(source))
71 target_exist = target and (isinstance(
72 target, zipfile.ZipFile) or os.path.exists(target))
Kelvin Zhang576efc52020-12-01 12:06:40 -050073
74 old_partitions = []
75 new_partitions = []
76 expected_new_partitions = []
77 for part in payload.manifest.partitions:
78 name = part.partition_name
79 old_image = os.path.join(tempdir, "source_" + name + ".img")
80 new_image = os.path.join(tempdir, "target_" + name + ".img")
Kelvin Zhang8c856552021-09-07 21:15:49 -070081 if part.HasField("old_partition_info"):
82 assert source_exist, \
83 "source target file must point to a valid zipfile or directory " + \
84 source
85 print("Extracting source image for", name)
86 extract_img(source, name, old_image)
87 if target_exist:
88 print("Extracting target image for", name)
89 extract_img(target, name, new_image)
Kelvin Zhang576efc52020-12-01 12:06:40 -050090
91 old_partitions.append(old_image)
92 scratch_image_name = new_image + ".actual"
93 new_partitions.append(scratch_image_name)
94 with open(scratch_image_name, "wb") as fp:
95 fp.truncate(part.new_partition_info.size)
96 expected_new_partitions.append(new_image)
97
98 delta_generator_args = ["delta_generator", "--in_file=" + payload_path]
99 partition_names = [
100 part.partition_name for part in payload.manifest.partitions
101 ]
Kelvin Zhang8c856552021-09-07 21:15:49 -0700102 if (payload.manifest.partial_update):
103 delta_generator_args.append("--is_partial_update")
104 if payload.is_incremental:
105 delta_generator_args.append("--old_partitions=" + ":".join(old_partitions))
Kelvin Zhang576efc52020-12-01 12:06:40 -0500106 delta_generator_args.append("--partition_names=" + ":".join(partition_names))
Kelvin Zhang576efc52020-12-01 12:06:40 -0500107 delta_generator_args.append("--new_partitions=" + ":".join(new_partitions))
108
109 subprocess.check_output(delta_generator_args)
110
111 valid = True
Kelvin Zhang8c856552021-09-07 21:15:49 -0700112 if not target_exist:
113 for part in new_partitions:
114 print("Output written to", part)
115 shutil.copy(part, output_dir)
116 return
Kelvin Zhang576efc52020-12-01 12:06:40 -0500117 for (expected_part, actual_part, part_name) in \
Kelvin Zhang8c856552021-09-07 21:15:49 -0700118 zip(expected_new_partitions, new_partitions, partition_names):
Kelvin Zhang576efc52020-12-01 12:06:40 -0500119 if filecmp.cmp(expected_part, actual_part):
120 print("Partition `{}` is valid".format(part_name))
121 else:
122 valid = False
123 print(
124 "Partition `{}` is INVALID expected image: {} actual image: {}"
125 .format(part_name, expected_part, actual_part))
126
127 if not valid and sys.stdout.isatty():
128 input("Paused to investigate invalid partitions, press any key to exit.")
129
130
131def main():
132 parser = argparse.ArgumentParser(
133 description="Run host side simulation of OTA package")
134 parser.add_argument(
135 "--source",
136 help="Target file zip for the source build",
Kelvin Zhang8c856552021-09-07 21:15:49 -0700137 required=False)
Kelvin Zhang576efc52020-12-01 12:06:40 -0500138 parser.add_argument(
139 "--target",
140 help="Target file zip for the target build",
Kelvin Zhang8c856552021-09-07 21:15:49 -0700141 required=False)
142 parser.add_argument(
143 "-o",
144 dest="output_dir",
145 help="Output directory to put all images, current directory by default"
146 )
Kelvin Zhang576efc52020-12-01 12:06:40 -0500147 parser.add_argument(
148 "payload",
149 help="payload.bin for the OTA package, or a zip of OTA package itself",
150 nargs=1)
151 args = parser.parse_args()
152 print(args)
153
Kelvin Zhang576efc52020-12-01 12:06:40 -0500154 # pylint: disable=no-member
155 with tempfile.TemporaryDirectory() as tempdir:
156 payload_path = args.payload[0]
157 if zipfile.is_zipfile(payload_path):
158 with zipfile.ZipFile(payload_path, "r") as zfp:
159 payload_entry_name = 'payload.bin'
160 zfp.extract(payload_entry_name, tempdir)
161 payload_path = os.path.join(tempdir, payload_entry_name)
Kelvin Zhang8c856552021-09-07 21:15:49 -0700162 if args.output_dir is None:
163 args.output_dir = "."
164 if not os.path.exists(args.output_dir):
165 os.makedirs(args.output_dir, exist_ok=True)
166 assert os.path.isdir(args.output_dir)
167 run_ota(args.source, args.target, payload_path, tempdir, args.output_dir)
Kelvin Zhang576efc52020-12-01 12:06:40 -0500168
169
170if __name__ == '__main__':
171 main()