blob: 282dc9905a3a4df10c1bc3b537c841ce58e05dc9 [file] [log] [blame]
Tao Baoafaa0a62017-02-27 15:08:36 -08001#!/usr/bin/env python
2
3# Copyright (C) 2017 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17"""
18Validate a given (signed) target_files.zip.
19
Tao Baoba557702018-03-10 20:41:16 -080020It performs the following checks to assert the integrity of the input zip.
21
Tao Baoafaa0a62017-02-27 15:08:36 -080022 - It verifies the file consistency between the ones in IMAGES/system.img (read
23 via IMAGES/system.map) and the ones under unpacked folder of SYSTEM/. The
24 same check also applies to the vendor image if present.
Tao Baoba557702018-03-10 20:41:16 -080025
26 - It verifies the install-recovery script consistency, by comparing the
27 checksums in the script against the ones of IMAGES/{boot,recovery}.img.
28
29 - It verifies the signed Verified Boot related images, for both of Verified
30 Boot 1.0 and 2.0 (aka AVB).
Tao Baoafaa0a62017-02-27 15:08:36 -080031"""
32
Tao Baoba557702018-03-10 20:41:16 -080033import argparse
34import filecmp
Tao Baoafaa0a62017-02-27 15:08:36 -080035import logging
36import os.path
Tianjie Xu9c384d22017-06-20 17:00:55 -070037import re
Tao Baoc63626b2018-03-07 21:40:24 -080038import zipfile
Kelvin Zhang26390482021-11-02 14:31:10 -070039
Tao Bao22632cc2019-10-03 23:12:55 -070040from hashlib import sha1
Kelvin Zhang26390482021-11-02 14:31:10 -070041from common import IsSparseImage
Tao Baoafaa0a62017-02-27 15:08:36 -080042
Tao Baobb20e8c2018-02-01 12:00:19 -080043import common
Tao Bao22632cc2019-10-03 23:12:55 -070044import rangelib
Tao Baoafaa0a62017-02-27 15:08:36 -080045
46
Tao Baob418c302017-08-30 15:54:59 -070047def _ReadFile(file_name, unpacked_name, round_up=False):
48 """Constructs and returns a File object. Rounds up its size if needed."""
Tianjie Xu9c384d22017-06-20 17:00:55 -070049 assert os.path.exists(unpacked_name)
Tao Baoda30cfa2017-12-01 16:19:46 -080050 with open(unpacked_name, 'rb') as f:
Tianjie Xu9c384d22017-06-20 17:00:55 -070051 file_data = f.read()
52 file_size = len(file_data)
53 if round_up:
Tao Baoc765cca2018-01-31 17:32:40 -080054 file_size_rounded_up = common.RoundUpTo4K(file_size)
Tao Bao22632cc2019-10-03 23:12:55 -070055 file_data += b'\0' * (file_size_rounded_up - file_size)
Tao Baob418c302017-08-30 15:54:59 -070056 return common.File(file_name, file_data)
Tianjie Xu9c384d22017-06-20 17:00:55 -070057
58
59def ValidateFileAgainstSha1(input_tmp, file_name, file_path, expected_sha1):
60 """Check if the file has the expected SHA-1."""
61
Tao Baobb20e8c2018-02-01 12:00:19 -080062 logging.info('Validating the SHA-1 of %s', file_name)
Tianjie Xu9c384d22017-06-20 17:00:55 -070063 unpacked_name = os.path.join(input_tmp, file_path)
64 assert os.path.exists(unpacked_name)
Tao Baob418c302017-08-30 15:54:59 -070065 actual_sha1 = _ReadFile(file_name, unpacked_name, False).sha1
Tianjie Xu9c384d22017-06-20 17:00:55 -070066 assert actual_sha1 == expected_sha1, \
67 'SHA-1 mismatches for {}. actual {}, expected {}'.format(
Tao Baobb20e8c2018-02-01 12:00:19 -080068 file_name, actual_sha1, expected_sha1)
Tianjie Xu9c384d22017-06-20 17:00:55 -070069
70
Tao Bao63e2f492018-05-11 23:38:46 -070071def ValidateFileConsistency(input_zip, input_tmp, info_dict):
Tianjie Xu9c384d22017-06-20 17:00:55 -070072 """Compare the files from image files and unpacked folders."""
73
Tao Baoafaa0a62017-02-27 15:08:36 -080074 def CheckAllFiles(which):
75 logging.info('Checking %s image.', which)
Kelvin Zhang26390482021-11-02 14:31:10 -070076 path = os.path.join(input_tmp, "IMAGES", which + ".img")
77 if not IsSparseImage(path):
78 logging.info("%s is non-sparse image", which)
79 image = common.GetNonSparseImage(which, input_tmp)
80 else:
81 logging.info("%s is sparse image", which)
82 # Allow having shared blocks when loading the sparse image, because allowing
83 # that doesn't affect the checks below (we will have all the blocks on file,
84 # unless it's skipped due to the holes).
85 image = common.GetSparseImage(which, input_tmp, input_zip, True)
Tao Baoafaa0a62017-02-27 15:08:36 -080086 prefix = '/' + which
87 for entry in image.file_map:
Tao Baoc765cca2018-01-31 17:32:40 -080088 # Skip entries like '__NONZERO-0'.
Tao Baoafaa0a62017-02-27 15:08:36 -080089 if not entry.startswith(prefix):
90 continue
91
92 # Read the blocks that the file resides. Note that it will contain the
93 # bytes past the file length, which is expected to be padded with '\0's.
94 ranges = image.file_map[entry]
Tao Baoc765cca2018-01-31 17:32:40 -080095
Tao Bao2a20f342018-12-03 15:08:23 -080096 # Use the original RangeSet if applicable, which includes the shared
97 # blocks. And this needs to happen before checking the monotonicity flag.
98 if ranges.extra.get('uses_shared_blocks'):
99 file_ranges = ranges.extra['uses_shared_blocks']
100 else:
101 file_ranges = ranges
102
xunchangc0f77ee2019-02-20 15:03:43 -0800103 incomplete = file_ranges.extra.get('incomplete', False)
104 if incomplete:
105 logging.warning('Skipping %s that has incomplete block list', entry)
106 continue
107
Tao Bao22632cc2019-10-03 23:12:55 -0700108 # If the file has non-monotonic ranges, read each range in order.
Tao Bao2a20f342018-12-03 15:08:23 -0800109 if not file_ranges.monotonic:
Tao Bao22632cc2019-10-03 23:12:55 -0700110 h = sha1()
111 for file_range in file_ranges.extra['text_str'].split(' '):
112 for data in image.ReadRangeSet(rangelib.RangeSet(file_range)):
113 h.update(data)
114 blocks_sha1 = h.hexdigest()
115 else:
116 blocks_sha1 = image.RangeSha1(file_ranges)
Tao Baoafaa0a62017-02-27 15:08:36 -0800117
118 # The filename under unpacked directory, such as SYSTEM/bin/sh.
119 unpacked_name = os.path.join(
120 input_tmp, which.upper(), entry[(len(prefix) + 1):])
Tao Baob418c302017-08-30 15:54:59 -0700121 unpacked_file = _ReadFile(entry, unpacked_name, True)
Tao Baob418c302017-08-30 15:54:59 -0700122 file_sha1 = unpacked_file.sha1
Tao Baoafaa0a62017-02-27 15:08:36 -0800123 assert blocks_sha1 == file_sha1, \
124 'file: %s, range: %s, blocks_sha1: %s, file_sha1: %s' % (
Tao Bao2a20f342018-12-03 15:08:23 -0800125 entry, file_ranges, blocks_sha1, file_sha1)
Tao Baoafaa0a62017-02-27 15:08:36 -0800126
127 logging.info('Validating file consistency.')
128
Tao Bao63e2f492018-05-11 23:38:46 -0700129 # TODO(b/79617342): Validate non-sparse images.
130 if info_dict.get('extfs_sparse_flag') != '-s':
131 logging.warning('Skipped due to target using non-sparse images')
132 return
133
Tao Baoafaa0a62017-02-27 15:08:36 -0800134 # Verify IMAGES/system.img.
135 CheckAllFiles('system')
136
137 # Verify IMAGES/vendor.img if applicable.
138 if 'VENDOR/' in input_zip.namelist():
139 CheckAllFiles('vendor')
140
141 # Not checking IMAGES/system_other.img since it doesn't have the map file.
142
143
Tianjie Xu9c384d22017-06-20 17:00:55 -0700144def ValidateInstallRecoveryScript(input_tmp, info_dict):
145 """Validate the SHA-1 embedded in install-recovery.sh.
146
147 install-recovery.sh is written in common.py and has the following format:
148
149 1. full recovery:
150 ...
Tao Bao4948aed2018-07-13 16:11:16 -0700151 if ! applypatch --check type:device:size:sha1; then
Bill Peckhame868aec2019-09-17 17:06:47 -0700152 applypatch --flash /vendor/etc/recovery.img \\
Tao Bao4948aed2018-07-13 16:11:16 -0700153 type:device:size:sha1 && \\
Tianjie Xu9c384d22017-06-20 17:00:55 -0700154 ...
155
156 2. recovery from boot:
157 ...
Tao Bao4948aed2018-07-13 16:11:16 -0700158 if ! applypatch --check type:recovery_device:recovery_size:recovery_sha1; then
159 applypatch [--bonus bonus_args] \\
Bill Peckhame868aec2019-09-17 17:06:47 -0700160 --patch /vendor/recovery-from-boot.p \\
Tao Bao4948aed2018-07-13 16:11:16 -0700161 --source type:boot_device:boot_size:boot_sha1 \\
162 --target type:recovery_device:recovery_size:recovery_sha1 && \\
Tianjie Xu9c384d22017-06-20 17:00:55 -0700163 ...
164
Bill Peckhame868aec2019-09-17 17:06:47 -0700165 For full recovery, we want to calculate the SHA-1 of /vendor/etc/recovery.img
Tianjie Xu9c384d22017-06-20 17:00:55 -0700166 and compare it against the one embedded in the script. While for recovery
167 from boot, we want to check the SHA-1 for both recovery.img and boot.img
168 under IMAGES/.
169 """
170
Bill Peckhame868aec2019-09-17 17:06:47 -0700171 board_uses_vendorimage = info_dict.get("board_uses_vendorimage") == "true"
172
173 if board_uses_vendorimage:
174 script_path = 'VENDOR/bin/install-recovery.sh'
175 recovery_img = 'VENDOR/etc/recovery.img'
176 else:
177 script_path = 'SYSTEM/vendor/bin/install-recovery.sh'
178 recovery_img = 'SYSTEM/vendor/etc/recovery.img'
179
Tianjie Xu9c384d22017-06-20 17:00:55 -0700180 if not os.path.exists(os.path.join(input_tmp, script_path)):
Tao Baobb20e8c2018-02-01 12:00:19 -0800181 logging.info('%s does not exist in input_tmp', script_path)
Tianjie Xu9c384d22017-06-20 17:00:55 -0700182 return
183
Tao Baobb20e8c2018-02-01 12:00:19 -0800184 logging.info('Checking %s', script_path)
Tianjie Xu9c384d22017-06-20 17:00:55 -0700185 with open(os.path.join(input_tmp, script_path), 'r') as script:
186 lines = script.read().strip().split('\n')
Tao Bao4948aed2018-07-13 16:11:16 -0700187 assert len(lines) >= 10
188 check_cmd = re.search(r'if ! applypatch --check (\w+:.+:\w+:\w+);',
Tianjie Xu9c384d22017-06-20 17:00:55 -0700189 lines[1].strip())
Tao Bao4948aed2018-07-13 16:11:16 -0700190 check_partition = check_cmd.group(1)
191 assert len(check_partition.split(':')) == 4
Tianjie Xu9c384d22017-06-20 17:00:55 -0700192
193 full_recovery_image = info_dict.get("full_recovery_image") == "true"
194 if full_recovery_image:
Tao Bao4948aed2018-07-13 16:11:16 -0700195 assert len(lines) == 10, "Invalid line count: {}".format(lines)
Tianjie Xu9c384d22017-06-20 17:00:55 -0700196
Tao Bao4948aed2018-07-13 16:11:16 -0700197 # Expect something like "EMMC:/dev/block/recovery:28:5f9c..62e3".
198 target = re.search(r'--target (.+) &&', lines[4].strip())
199 assert target is not None, \
200 "Failed to parse target line \"{}\"".format(lines[4])
201 flash_partition = target.group(1)
202
203 # Check we have the same recovery target in the check and flash commands.
204 assert check_partition == flash_partition, \
Kelvin Zhang4093d602021-05-25 09:17:38 -0400205 "Mismatching targets: {} vs {}".format(
206 check_partition, flash_partition)
Tao Bao4948aed2018-07-13 16:11:16 -0700207
208 # Validate the SHA-1 of the recovery image.
209 recovery_sha1 = flash_partition.split(':')[3]
210 ValidateFileAgainstSha1(
Bill Peckhame868aec2019-09-17 17:06:47 -0700211 input_tmp, 'recovery.img', recovery_img, recovery_sha1)
Tao Bao4948aed2018-07-13 16:11:16 -0700212 else:
213 assert len(lines) == 11, "Invalid line count: {}".format(lines)
214
215 # --source boot_type:boot_device:boot_size:boot_sha1
216 source = re.search(r'--source (\w+:.+:\w+:\w+) \\', lines[4].strip())
217 assert source is not None, \
218 "Failed to parse source line \"{}\"".format(lines[4])
219
220 source_partition = source.group(1)
221 source_info = source_partition.split(':')
222 assert len(source_info) == 4, \
223 "Invalid source partition: {}".format(source_partition)
Tianjie Xu9c384d22017-06-20 17:00:55 -0700224 ValidateFileAgainstSha1(input_tmp, file_name='boot.img',
Tao Baobb20e8c2018-02-01 12:00:19 -0800225 file_path='IMAGES/boot.img',
Tao Bao4948aed2018-07-13 16:11:16 -0700226 expected_sha1=source_info[3])
Tianjie Xu9c384d22017-06-20 17:00:55 -0700227
Tao Bao4948aed2018-07-13 16:11:16 -0700228 # --target recovery_type:recovery_device:recovery_size:recovery_sha1
229 target = re.search(r'--target (\w+:.+:\w+:\w+) && \\', lines[5].strip())
230 assert target is not None, \
231 "Failed to parse target line \"{}\"".format(lines[5])
232 target_partition = target.group(1)
233
234 # Check we have the same recovery target in the check and patch commands.
235 assert check_partition == target_partition, \
236 "Mismatching targets: {} vs {}".format(
237 check_partition, target_partition)
238
239 recovery_info = target_partition.split(':')
240 assert len(recovery_info) == 4, \
241 "Invalid target partition: {}".format(target_partition)
Tianjie Xu9c384d22017-06-20 17:00:55 -0700242 ValidateFileAgainstSha1(input_tmp, file_name='recovery.img',
Tao Baobb20e8c2018-02-01 12:00:19 -0800243 file_path='IMAGES/recovery.img',
Tao Bao4948aed2018-07-13 16:11:16 -0700244 expected_sha1=recovery_info[3])
Tianjie Xu9c384d22017-06-20 17:00:55 -0700245
Tao Baobb20e8c2018-02-01 12:00:19 -0800246 logging.info('Done checking %s', script_path)
Tianjie Xu9c384d22017-06-20 17:00:55 -0700247
Tianjie2e0b8352021-01-12 14:04:58 -0800248
Kelvin Zhang5d2b56b2020-06-26 11:37:28 -0400249# Symlink files in `src` to `dst`, if the files do not
250# already exists in `dst` directory.
251def symlinkIfNotExists(src, dst):
252 if not os.path.isdir(src):
253 return
254 for filename in os.listdir(src):
255 if os.path.exists(os.path.join(dst, filename)):
256 continue
257 os.symlink(os.path.join(src, filename), os.path.join(dst, filename))
Tianjie Xu9c384d22017-06-20 17:00:55 -0700258
Tianjie2e0b8352021-01-12 14:04:58 -0800259
Kelvin Zhang4093d602021-05-25 09:17:38 -0400260def ValidatePartitionFingerprints(input_tmp, info_dict):
261 build_info = common.BuildInfo(info_dict)
Kelvin Zhanga19fb312021-07-26 14:05:02 -0400262 if not build_info.avb_enabled:
263 logging.info("AVB not enabled, skipping partition fingerprint checks")
264 return
Kelvin Zhang4093d602021-05-25 09:17:38 -0400265 # Expected format:
266 # Prop: com.android.build.vendor.fingerprint -> 'generic/aosp_cf_x86_64_phone/vsoc_x86_64:S/AOSP.MASTER/7335886:userdebug/test-keys'
267 # Prop: com.android.build.vendor_boot.fingerprint -> 'generic/aosp_cf_x86_64_phone/vsoc_x86_64:S/AOSP.MASTER/7335886:userdebug/test-keys'
268 p = re.compile(
269 r"Prop: com.android.build.(?P<partition>\w+).fingerprint -> '(?P<fingerprint>[\w\/:\.-]+)'")
270 for vbmeta_partition in ["vbmeta", "vbmeta_system"]:
271 image = os.path.join(input_tmp, "IMAGES", vbmeta_partition + ".img")
Kelvin Zhanga19fb312021-07-26 14:05:02 -0400272 if not os.path.exists(image):
273 assert vbmeta_partition != "vbmeta",\
274 "{} is a required partition for AVB.".format(
275 vbmeta_partition)
276 logging.info("vb partition %s not present, skipping", vbmeta_partition)
277 continue
278
Kelvin Zhang4093d602021-05-25 09:17:38 -0400279 output = common.RunAndCheckOutput(
280 [info_dict["avb_avbtool"], "info_image", "--image", image])
281 matches = p.findall(output)
282 for (partition, fingerprint) in matches:
283 actual_fingerprint = build_info.GetPartitionFingerprint(
284 partition)
285 if actual_fingerprint is None:
286 logging.warning(
287 "Failed to get fingerprint for partition %s", partition)
288 continue
289 assert fingerprint == actual_fingerprint, "Fingerprint mismatch for partition {}, expected: {} actual: {}".format(
290 partition, fingerprint, actual_fingerprint)
291
292
Tao Baoba557702018-03-10 20:41:16 -0800293def ValidateVerifiedBootImages(input_tmp, info_dict, options):
294 """Validates the Verified Boot related images.
Tao Baoafaa0a62017-02-27 15:08:36 -0800295
Tao Baoba557702018-03-10 20:41:16 -0800296 For Verified Boot 1.0, it verifies the signatures of the bootable images
297 (boot/recovery etc), as well as the dm-verity metadata in system images
298 (system/vendor/product). For Verified Boot 2.0, it calls avbtool to verify
299 vbmeta.img, which in turn verifies all the descriptors listed in vbmeta.
Tao Baoafaa0a62017-02-27 15:08:36 -0800300
Tao Baoba557702018-03-10 20:41:16 -0800301 Args:
302 input_tmp: The top-level directory of unpacked target-files.zip.
303 info_dict: The loaded info dict.
304 options: A dict that contains the user-supplied public keys to be used for
305 image verification. In particular, 'verity_key' is used to verify the
306 bootable images in VB 1.0, and the vbmeta image in VB 2.0, where
307 applicable. 'verity_key_mincrypt' will be used to verify the system
308 images in VB 1.0.
309
310 Raises:
311 AssertionError: On any verification failure.
312 """
Kelvin Zhang5d2b56b2020-06-26 11:37:28 -0400313 # See bug 159299583
314 # After commit 5277d1015, some images (e.g. acpio.img and tos.img) are no
315 # longer copied from RADIO to the IMAGES folder. But avbtool assumes that
316 # images are in IMAGES folder. So we symlink them.
317 symlinkIfNotExists(os.path.join(input_tmp, "RADIO"),
Kelvin Zhang4093d602021-05-25 09:17:38 -0400318 os.path.join(input_tmp, "IMAGES"))
Tao Baoba557702018-03-10 20:41:16 -0800319 # Verified boot 1.0 (images signed with boot_signer and verity_signer).
320 if info_dict.get('boot_signer') == 'true':
321 logging.info('Verifying Verified Boot images...')
322
323 # Verify the boot/recovery images (signed with boot_signer), against the
324 # given X.509 encoded pubkey (or falling back to the one in the info_dict if
325 # none given).
326 verity_key = options['verity_key']
327 if verity_key is None:
328 verity_key = info_dict['verity_key'] + '.x509.pem'
329 for image in ('boot.img', 'recovery.img', 'recovery-two-step.img'):
Tao Bao04808502019-07-25 23:11:41 -0700330 if image == 'recovery-two-step.img':
331 image_path = os.path.join(input_tmp, 'OTA', image)
332 else:
333 image_path = os.path.join(input_tmp, 'IMAGES', image)
Tao Baoba557702018-03-10 20:41:16 -0800334 if not os.path.exists(image_path):
335 continue
336
337 cmd = ['boot_signer', '-verify', image_path, '-certificate', verity_key]
Tao Bao73dd4f42018-10-04 16:25:33 -0700338 proc = common.Run(cmd)
Tao Baoba557702018-03-10 20:41:16 -0800339 stdoutdata, _ = proc.communicate()
340 assert proc.returncode == 0, \
341 'Failed to verify {} with boot_signer:\n{}'.format(image, stdoutdata)
342 logging.info(
343 'Verified %s with boot_signer (key: %s):\n%s', image, verity_key,
344 stdoutdata.rstrip())
345
346 # Verify verity signed system images in Verified Boot 1.0. Note that not using
347 # 'elif' here, since 'boot_signer' and 'verity' are not bundled in VB 1.0.
348 if info_dict.get('verity') == 'true':
Tao Baoc9981932019-09-16 12:10:43 -0700349 # First verify that the verity key is built into the root image (regardless
350 # of system-as-root).
351 verity_key_mincrypt = os.path.join(input_tmp, 'ROOT', 'verity_key')
Tao Baoba557702018-03-10 20:41:16 -0800352 assert os.path.exists(verity_key_mincrypt), 'Missing verity_key'
353
Tao Baoc9981932019-09-16 12:10:43 -0700354 # Verify /verity_key matches the one given via command line, if any.
Tao Baoba557702018-03-10 20:41:16 -0800355 if options['verity_key_mincrypt'] is None:
356 logging.warn(
357 'Skipped checking the content of /verity_key, as the key file not '
358 'provided. Use --verity_key_mincrypt to specify.')
359 else:
360 expected_key = options['verity_key_mincrypt']
361 assert filecmp.cmp(expected_key, verity_key_mincrypt, shallow=False), \
362 "Mismatching mincrypt verity key files"
363 logging.info('Verified the content of /verity_key')
364
Tao Baoc9981932019-09-16 12:10:43 -0700365 # For devices with a separate ramdisk (i.e. non-system-as-root), there must
366 # be a copy in ramdisk.
367 if info_dict.get("system_root_image") != "true":
368 verity_key_ramdisk = os.path.join(
369 input_tmp, 'BOOT', 'RAMDISK', 'verity_key')
Kelvin Zhang4093d602021-05-25 09:17:38 -0400370 assert os.path.exists(
371 verity_key_ramdisk), 'Missing verity_key in ramdisk'
Tao Baoc9981932019-09-16 12:10:43 -0700372
373 assert filecmp.cmp(
374 verity_key_mincrypt, verity_key_ramdisk, shallow=False), \
Kelvin Zhang4093d602021-05-25 09:17:38 -0400375 'Mismatching verity_key files in root and ramdisk'
Tao Baoc9981932019-09-16 12:10:43 -0700376 logging.info('Verified the content of /verity_key in ramdisk')
377
Tao Baoba557702018-03-10 20:41:16 -0800378 # Then verify the verity signed system/vendor/product images, against the
379 # verity pubkey in mincrypt format.
380 for image in ('system.img', 'vendor.img', 'product.img'):
381 image_path = os.path.join(input_tmp, 'IMAGES', image)
382
383 # We are not checking if the image is actually enabled via info_dict (e.g.
384 # 'system_verity_block_device=...'). Because it's most likely a bug that
385 # skips signing some of the images in signed target-files.zip, while
386 # having the top-level verity flag enabled.
387 if not os.path.exists(image_path):
388 continue
389
390 cmd = ['verity_verifier', image_path, '-mincrypt', verity_key_mincrypt]
Tao Bao73dd4f42018-10-04 16:25:33 -0700391 proc = common.Run(cmd)
Tao Baoba557702018-03-10 20:41:16 -0800392 stdoutdata, _ = proc.communicate()
393 assert proc.returncode == 0, \
394 'Failed to verify {} with verity_verifier (key: {}):\n{}'.format(
395 image, verity_key_mincrypt, stdoutdata)
396 logging.info(
397 'Verified %s with verity_verifier (key: %s):\n%s', image,
398 verity_key_mincrypt, stdoutdata.rstrip())
399
400 # Handle the case of Verified Boot 2.0 (AVB).
401 if info_dict.get("avb_enable") == "true":
402 logging.info('Verifying Verified Boot 2.0 (AVB) images...')
403
Tao Baoa81d4292019-03-26 12:13:04 -0700404 key = options['verity_key']
405 if key is None:
406 key = info_dict['avb_vbmeta_key_path']
407
Kelvin Zhang4093d602021-05-25 09:17:38 -0400408 ValidatePartitionFingerprints(input_tmp, info_dict)
409
Tao Baoa81d4292019-03-26 12:13:04 -0700410 # avbtool verifies all the images that have descriptors listed in vbmeta.
cfig1aeef722019-09-20 22:45:06 +0800411 # Using `--follow_chain_partitions` so it would additionally verify chained
412 # vbmeta partitions (e.g. vbmeta_system).
Tao Baoa81d4292019-03-26 12:13:04 -0700413 image = os.path.join(input_tmp, 'IMAGES', 'vbmeta.img')
Tao Bao1ac886e2019-06-26 11:58:22 -0700414 cmd = [info_dict['avb_avbtool'], 'verify_image', '--image', image,
Tianjie Xu9bd832a2020-02-06 13:12:56 -0800415 '--follow_chain_partitions']
Tao Baoa81d4292019-03-26 12:13:04 -0700416
Hongguang Chenf23364d2020-04-27 18:36:36 -0700417 # Custom images.
418 custom_partitions = info_dict.get(
419 "avb_custom_images_partition_list", "").strip().split()
420
Tao Baoa81d4292019-03-26 12:13:04 -0700421 # Append the args for chained partitions if any.
Hongguang Chenf23364d2020-04-27 18:36:36 -0700422 for partition in (common.AVB_PARTITIONS + common.AVB_VBMETA_PARTITIONS +
423 tuple(custom_partitions)):
Tao Baoa81d4292019-03-26 12:13:04 -0700424 key_name = 'avb_' + partition + '_key_path'
425 if info_dict.get(key_name) is not None:
cfig1aeef722019-09-20 22:45:06 +0800426 if info_dict.get('ab_update') != 'true' and partition == 'recovery':
427 continue
428
Tao Bao08c190f2019-06-03 23:07:58 -0700429 # Use the key file from command line if specified; otherwise fall back
430 # to the one in info dict.
431 key_file = options.get(key_name, info_dict[key_name])
Tao Baoa81d4292019-03-26 12:13:04 -0700432 chained_partition_arg = common.GetAvbChainedPartitionArg(
Tao Bao08c190f2019-06-03 23:07:58 -0700433 partition, info_dict, key_file)
cfig1aeef722019-09-20 22:45:06 +0800434 cmd.extend(['--expected_chain_partition', chained_partition_arg])
Tao Baoa81d4292019-03-26 12:13:04 -0700435
Tianjie5ec1a7a2020-06-25 22:59:54 -0700436 # Handle the boot image with a non-default name, e.g. boot-5.4.img
437 boot_images = info_dict.get("boot_images")
438 if boot_images:
439 # we used the 1st boot image to generate the vbmeta. Rename the filename
440 # to boot.img so that avbtool can find it correctly.
441 first_image_name = boot_images.split()[0]
442 first_image_path = os.path.join(input_tmp, 'IMAGES', first_image_name)
443 assert os.path.isfile(first_image_path)
444 renamed_boot_image_path = os.path.join(input_tmp, 'IMAGES', 'boot.img')
445 os.rename(first_image_path, renamed_boot_image_path)
446
Tao Baoa81d4292019-03-26 12:13:04 -0700447 proc = common.Run(cmd)
448 stdoutdata, _ = proc.communicate()
449 assert proc.returncode == 0, \
450 'Failed to verify {} with avbtool (key: {}):\n{}'.format(
451 image, key, stdoutdata)
452
453 logging.info(
454 'Verified %s with avbtool (key: %s):\n%s', image, key,
455 stdoutdata.rstrip())
Tao Baoba557702018-03-10 20:41:16 -0800456
cfig1aeef722019-09-20 22:45:06 +0800457 # avbtool verifies recovery image for non-A/B devices.
458 if (info_dict.get('ab_update') != 'true' and
Kelvin Zhang4093d602021-05-25 09:17:38 -0400459 info_dict.get('no_recovery') != 'true'):
cfig1aeef722019-09-20 22:45:06 +0800460 image = os.path.join(input_tmp, 'IMAGES', 'recovery.img')
461 key = info_dict['avb_recovery_key_path']
462 cmd = [info_dict['avb_avbtool'], 'verify_image', '--image', image,
463 '--key', key]
464 proc = common.Run(cmd)
465 stdoutdata, _ = proc.communicate()
466 assert proc.returncode == 0, \
467 'Failed to verify {} with avbtool (key: {}):\n{}'.format(
468 image, key, stdoutdata)
469 logging.info(
470 'Verified %s with avbtool (key: %s):\n%s', image, key,
471 stdoutdata.rstrip())
472
Tianjie2e0b8352021-01-12 14:04:58 -0800473
474def CheckDataInconsistency(lines):
Kelvin Zhang4093d602021-05-25 09:17:38 -0400475 build_prop = {}
476 for line in lines:
477 if line.startswith("import") or line.startswith("#"):
478 continue
479 if "=" not in line:
480 continue
Tianjie2e0b8352021-01-12 14:04:58 -0800481
Kelvin Zhang4093d602021-05-25 09:17:38 -0400482 key, value = line.rstrip().split("=", 1)
483 if key in build_prop:
484 logging.info("Duplicated key found for {}".format(key))
485 if value != build_prop[key]:
486 logging.error("Key {} is defined twice with different values {} vs {}"
487 .format(key, value, build_prop[key]))
488 return key
489 build_prop[key] = value
Kelvin Zhangf2e846f2020-06-29 16:04:51 -0400490
Tianjie2e0b8352021-01-12 14:04:58 -0800491
Kelvin Zhangf2e846f2020-06-29 16:04:51 -0400492def CheckBuildPropDuplicity(input_tmp):
493 """Check all buld.prop files inside directory input_tmp, raise error
494 if they contain duplicates"""
495
496 if not os.path.isdir(input_tmp):
497 raise ValueError("Expect {} to be a directory".format(input_tmp))
498 for name in os.listdir(input_tmp):
499 if not name.isupper():
500 continue
501 for prop_file in ['build.prop', 'etc/build.prop']:
502 path = os.path.join(input_tmp, name, prop_file)
503 if not os.path.exists(path):
504 continue
505 logging.info("Checking {}".format(path))
506 with open(path, 'r') as fp:
Tianjie2e0b8352021-01-12 14:04:58 -0800507 dupKey = CheckDataInconsistency(fp.readlines())
Kelvin Zhangf2e846f2020-06-29 16:04:51 -0400508 if dupKey:
Tianjie2e0b8352021-01-12 14:04:58 -0800509 raise ValueError("{} contains duplicate keys for {}".format(
510 path, dupKey))
511
Tao Baoba557702018-03-10 20:41:16 -0800512
513def main():
514 parser = argparse.ArgumentParser(
515 description=__doc__,
516 formatter_class=argparse.RawDescriptionHelpFormatter)
517 parser.add_argument(
518 'target_files',
519 help='the input target_files.zip to be validated')
520 parser.add_argument(
521 '--verity_key',
522 help='the verity public key to verify the bootable images (Verified '
Tao Bao02a08592018-07-22 12:40:45 -0700523 'Boot 1.0), or the vbmeta image (Verified Boot 2.0, aka AVB), where '
Tao Baoba557702018-03-10 20:41:16 -0800524 'applicable')
Tao Bao08c190f2019-06-03 23:07:58 -0700525 for partition in common.AVB_PARTITIONS + common.AVB_VBMETA_PARTITIONS:
Tao Bao02a08592018-07-22 12:40:45 -0700526 parser.add_argument(
527 '--avb_' + partition + '_key_path',
528 help='the public or private key in PEM format to verify AVB chained '
529 'partition of {}'.format(partition))
Tao Baoba557702018-03-10 20:41:16 -0800530 parser.add_argument(
531 '--verity_key_mincrypt',
532 help='the verity public key in mincrypt format to verify the system '
533 'images, if target using Verified Boot 1.0')
534 args = parser.parse_args()
535
536 # Unprovided args will have 'None' as the value.
537 options = vars(args)
Tao Baoafaa0a62017-02-27 15:08:36 -0800538
539 logging_format = '%(asctime)s - %(filename)s - %(levelname)-8s: %(message)s'
540 date_format = '%Y/%m/%d %H:%M:%S'
541 logging.basicConfig(level=logging.INFO, format=logging_format,
542 datefmt=date_format)
543
Tao Baoba557702018-03-10 20:41:16 -0800544 logging.info("Unzipping the input target_files.zip: %s", args.target_files)
545 input_tmp = common.UnzipTemp(args.target_files)
Tao Baoafaa0a62017-02-27 15:08:36 -0800546
Tianjie Xu9c384d22017-06-20 17:00:55 -0700547 info_dict = common.LoadInfoDict(input_tmp)
Kelvin Zhang928c2342020-09-22 16:15:57 -0400548 with zipfile.ZipFile(args.target_files, 'r', allowZip64=True) as input_zip:
Tao Bao63e2f492018-05-11 23:38:46 -0700549 ValidateFileConsistency(input_zip, input_tmp, info_dict)
550
Kelvin Zhangf2e846f2020-06-29 16:04:51 -0400551 CheckBuildPropDuplicity(input_tmp)
552
Tianjie Xu9c384d22017-06-20 17:00:55 -0700553 ValidateInstallRecoveryScript(input_tmp, info_dict)
554
Tao Baoba557702018-03-10 20:41:16 -0800555 ValidateVerifiedBootImages(input_tmp, info_dict, options)
556
Tao Baoafaa0a62017-02-27 15:08:36 -0800557 # TODO: Check if the OTA keys have been properly updated (the ones on /system,
558 # in recovery image).
559
Tao Baoafaa0a62017-02-27 15:08:36 -0800560 logging.info("Done.")
561
562
563if __name__ == '__main__':
564 try:
Tao Baoba557702018-03-10 20:41:16 -0800565 main()
Tao Baoafaa0a62017-02-27 15:08:36 -0800566 finally:
567 common.Cleanup()