Tao Bao | afaa0a6 | 2017-02-27 15:08:36 -0800 | [diff] [blame] | 1 | #!/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 | """ |
| 18 | Validate a given (signed) target_files.zip. |
| 19 | |
Tao Bao | ba55770 | 2018-03-10 20:41:16 -0800 | [diff] [blame] | 20 | It performs the following checks to assert the integrity of the input zip. |
| 21 | |
Tao Bao | afaa0a6 | 2017-02-27 15:08:36 -0800 | [diff] [blame] | 22 | - 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 Bao | ba55770 | 2018-03-10 20:41:16 -0800 | [diff] [blame] | 25 | |
| 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 Bao | afaa0a6 | 2017-02-27 15:08:36 -0800 | [diff] [blame] | 31 | """ |
| 32 | |
Tao Bao | ba55770 | 2018-03-10 20:41:16 -0800 | [diff] [blame] | 33 | import argparse |
| 34 | import filecmp |
Tao Bao | afaa0a6 | 2017-02-27 15:08:36 -0800 | [diff] [blame] | 35 | import logging |
| 36 | import os.path |
Tianjie Xu | 9c384d2 | 2017-06-20 17:00:55 -0700 | [diff] [blame] | 37 | import re |
Tao Bao | c63626b | 2018-03-07 21:40:24 -0800 | [diff] [blame] | 38 | import zipfile |
Tao Bao | afaa0a6 | 2017-02-27 15:08:36 -0800 | [diff] [blame] | 39 | |
Tao Bao | bb20e8c | 2018-02-01 12:00:19 -0800 | [diff] [blame] | 40 | import common |
Tao Bao | afaa0a6 | 2017-02-27 15:08:36 -0800 | [diff] [blame] | 41 | |
| 42 | |
Tao Bao | b418c30 | 2017-08-30 15:54:59 -0700 | [diff] [blame] | 43 | def _ReadFile(file_name, unpacked_name, round_up=False): |
| 44 | """Constructs and returns a File object. Rounds up its size if needed.""" |
Tao Bao | afaa0a6 | 2017-02-27 15:08:36 -0800 | [diff] [blame] | 45 | |
Tianjie Xu | 9c384d2 | 2017-06-20 17:00:55 -0700 | [diff] [blame] | 46 | assert os.path.exists(unpacked_name) |
| 47 | with open(unpacked_name, 'r') as f: |
| 48 | file_data = f.read() |
| 49 | file_size = len(file_data) |
| 50 | if round_up: |
Tao Bao | c765cca | 2018-01-31 17:32:40 -0800 | [diff] [blame] | 51 | file_size_rounded_up = common.RoundUpTo4K(file_size) |
Tianjie Xu | 9c384d2 | 2017-06-20 17:00:55 -0700 | [diff] [blame] | 52 | file_data += '\0' * (file_size_rounded_up - file_size) |
Tao Bao | b418c30 | 2017-08-30 15:54:59 -0700 | [diff] [blame] | 53 | return common.File(file_name, file_data) |
Tianjie Xu | 9c384d2 | 2017-06-20 17:00:55 -0700 | [diff] [blame] | 54 | |
| 55 | |
| 56 | def ValidateFileAgainstSha1(input_tmp, file_name, file_path, expected_sha1): |
| 57 | """Check if the file has the expected SHA-1.""" |
| 58 | |
Tao Bao | bb20e8c | 2018-02-01 12:00:19 -0800 | [diff] [blame] | 59 | logging.info('Validating the SHA-1 of %s', file_name) |
Tianjie Xu | 9c384d2 | 2017-06-20 17:00:55 -0700 | [diff] [blame] | 60 | unpacked_name = os.path.join(input_tmp, file_path) |
| 61 | assert os.path.exists(unpacked_name) |
Tao Bao | b418c30 | 2017-08-30 15:54:59 -0700 | [diff] [blame] | 62 | actual_sha1 = _ReadFile(file_name, unpacked_name, False).sha1 |
Tianjie Xu | 9c384d2 | 2017-06-20 17:00:55 -0700 | [diff] [blame] | 63 | assert actual_sha1 == expected_sha1, \ |
| 64 | 'SHA-1 mismatches for {}. actual {}, expected {}'.format( |
Tao Bao | bb20e8c | 2018-02-01 12:00:19 -0800 | [diff] [blame] | 65 | file_name, actual_sha1, expected_sha1) |
Tianjie Xu | 9c384d2 | 2017-06-20 17:00:55 -0700 | [diff] [blame] | 66 | |
| 67 | |
Tao Bao | 63e2f49 | 2018-05-11 23:38:46 -0700 | [diff] [blame] | 68 | def ValidateFileConsistency(input_zip, input_tmp, info_dict): |
Tianjie Xu | 9c384d2 | 2017-06-20 17:00:55 -0700 | [diff] [blame] | 69 | """Compare the files from image files and unpacked folders.""" |
| 70 | |
Tao Bao | afaa0a6 | 2017-02-27 15:08:36 -0800 | [diff] [blame] | 71 | def CheckAllFiles(which): |
| 72 | logging.info('Checking %s image.', which) |
Tao Bao | c63626b | 2018-03-07 21:40:24 -0800 | [diff] [blame] | 73 | # Allow having shared blocks when loading the sparse image, because allowing |
| 74 | # that doesn't affect the checks below (we will have all the blocks on file, |
| 75 | # unless it's skipped due to the holes). |
| 76 | image = common.GetSparseImage(which, input_tmp, input_zip, True) |
Tao Bao | afaa0a6 | 2017-02-27 15:08:36 -0800 | [diff] [blame] | 77 | prefix = '/' + which |
| 78 | for entry in image.file_map: |
Tao Bao | c765cca | 2018-01-31 17:32:40 -0800 | [diff] [blame] | 79 | # Skip entries like '__NONZERO-0'. |
Tao Bao | afaa0a6 | 2017-02-27 15:08:36 -0800 | [diff] [blame] | 80 | if not entry.startswith(prefix): |
| 81 | continue |
| 82 | |
| 83 | # Read the blocks that the file resides. Note that it will contain the |
| 84 | # bytes past the file length, which is expected to be padded with '\0's. |
| 85 | ranges = image.file_map[entry] |
Tao Bao | c765cca | 2018-01-31 17:32:40 -0800 | [diff] [blame] | 86 | |
| 87 | incomplete = ranges.extra.get('incomplete', False) |
| 88 | if incomplete: |
| 89 | logging.warning('Skipping %s that has incomplete block list', entry) |
| 90 | continue |
| 91 | |
Tao Bao | d32936d | 2018-05-17 19:42:41 -0700 | [diff] [blame] | 92 | # TODO(b/79951650): Handle files with non-monotonic ranges. |
| 93 | if not ranges.monotonic: |
| 94 | logging.warning( |
| 95 | 'Skipping %s that has non-monotonic ranges: %s', entry, ranges) |
| 96 | continue |
| 97 | |
Tao Bao | afaa0a6 | 2017-02-27 15:08:36 -0800 | [diff] [blame] | 98 | blocks_sha1 = image.RangeSha1(ranges) |
| 99 | |
| 100 | # The filename under unpacked directory, such as SYSTEM/bin/sh. |
| 101 | unpacked_name = os.path.join( |
| 102 | input_tmp, which.upper(), entry[(len(prefix) + 1):]) |
Tao Bao | b418c30 | 2017-08-30 15:54:59 -0700 | [diff] [blame] | 103 | unpacked_file = _ReadFile(entry, unpacked_name, True) |
Tao Bao | b418c30 | 2017-08-30 15:54:59 -0700 | [diff] [blame] | 104 | file_sha1 = unpacked_file.sha1 |
Tao Bao | afaa0a6 | 2017-02-27 15:08:36 -0800 | [diff] [blame] | 105 | assert blocks_sha1 == file_sha1, \ |
| 106 | 'file: %s, range: %s, blocks_sha1: %s, file_sha1: %s' % ( |
| 107 | entry, ranges, blocks_sha1, file_sha1) |
| 108 | |
| 109 | logging.info('Validating file consistency.') |
| 110 | |
Tao Bao | 63e2f49 | 2018-05-11 23:38:46 -0700 | [diff] [blame] | 111 | # TODO(b/79617342): Validate non-sparse images. |
| 112 | if info_dict.get('extfs_sparse_flag') != '-s': |
| 113 | logging.warning('Skipped due to target using non-sparse images') |
| 114 | return |
| 115 | |
Tao Bao | afaa0a6 | 2017-02-27 15:08:36 -0800 | [diff] [blame] | 116 | # Verify IMAGES/system.img. |
| 117 | CheckAllFiles('system') |
| 118 | |
| 119 | # Verify IMAGES/vendor.img if applicable. |
| 120 | if 'VENDOR/' in input_zip.namelist(): |
| 121 | CheckAllFiles('vendor') |
| 122 | |
| 123 | # Not checking IMAGES/system_other.img since it doesn't have the map file. |
| 124 | |
| 125 | |
Tianjie Xu | 9c384d2 | 2017-06-20 17:00:55 -0700 | [diff] [blame] | 126 | def ValidateInstallRecoveryScript(input_tmp, info_dict): |
| 127 | """Validate the SHA-1 embedded in install-recovery.sh. |
| 128 | |
| 129 | install-recovery.sh is written in common.py and has the following format: |
| 130 | |
| 131 | 1. full recovery: |
| 132 | ... |
Tao Bao | 4948aed | 2018-07-13 16:11:16 -0700 | [diff] [blame] | 133 | if ! applypatch --check type:device:size:sha1; then |
| 134 | applypatch --flash /system/etc/recovery.img \\ |
| 135 | type:device:size:sha1 && \\ |
Tianjie Xu | 9c384d2 | 2017-06-20 17:00:55 -0700 | [diff] [blame] | 136 | ... |
| 137 | |
| 138 | 2. recovery from boot: |
| 139 | ... |
Tao Bao | 4948aed | 2018-07-13 16:11:16 -0700 | [diff] [blame] | 140 | if ! applypatch --check type:recovery_device:recovery_size:recovery_sha1; then |
| 141 | applypatch [--bonus bonus_args] \\ |
| 142 | --patch /system/recovery-from-boot.p \\ |
| 143 | --source type:boot_device:boot_size:boot_sha1 \\ |
| 144 | --target type:recovery_device:recovery_size:recovery_sha1 && \\ |
Tianjie Xu | 9c384d2 | 2017-06-20 17:00:55 -0700 | [diff] [blame] | 145 | ... |
| 146 | |
| 147 | For full recovery, we want to calculate the SHA-1 of /system/etc/recovery.img |
| 148 | and compare it against the one embedded in the script. While for recovery |
| 149 | from boot, we want to check the SHA-1 for both recovery.img and boot.img |
| 150 | under IMAGES/. |
| 151 | """ |
| 152 | |
| 153 | script_path = 'SYSTEM/bin/install-recovery.sh' |
| 154 | if not os.path.exists(os.path.join(input_tmp, script_path)): |
Tao Bao | bb20e8c | 2018-02-01 12:00:19 -0800 | [diff] [blame] | 155 | logging.info('%s does not exist in input_tmp', script_path) |
Tianjie Xu | 9c384d2 | 2017-06-20 17:00:55 -0700 | [diff] [blame] | 156 | return |
| 157 | |
Tao Bao | bb20e8c | 2018-02-01 12:00:19 -0800 | [diff] [blame] | 158 | logging.info('Checking %s', script_path) |
Tianjie Xu | 9c384d2 | 2017-06-20 17:00:55 -0700 | [diff] [blame] | 159 | with open(os.path.join(input_tmp, script_path), 'r') as script: |
| 160 | lines = script.read().strip().split('\n') |
Tao Bao | 4948aed | 2018-07-13 16:11:16 -0700 | [diff] [blame] | 161 | assert len(lines) >= 10 |
| 162 | check_cmd = re.search(r'if ! applypatch --check (\w+:.+:\w+:\w+);', |
Tianjie Xu | 9c384d2 | 2017-06-20 17:00:55 -0700 | [diff] [blame] | 163 | lines[1].strip()) |
Tao Bao | 4948aed | 2018-07-13 16:11:16 -0700 | [diff] [blame] | 164 | check_partition = check_cmd.group(1) |
| 165 | assert len(check_partition.split(':')) == 4 |
Tianjie Xu | 9c384d2 | 2017-06-20 17:00:55 -0700 | [diff] [blame] | 166 | |
| 167 | full_recovery_image = info_dict.get("full_recovery_image") == "true" |
| 168 | if full_recovery_image: |
Tao Bao | 4948aed | 2018-07-13 16:11:16 -0700 | [diff] [blame] | 169 | assert len(lines) == 10, "Invalid line count: {}".format(lines) |
Tianjie Xu | 9c384d2 | 2017-06-20 17:00:55 -0700 | [diff] [blame] | 170 | |
Tao Bao | 4948aed | 2018-07-13 16:11:16 -0700 | [diff] [blame] | 171 | # Expect something like "EMMC:/dev/block/recovery:28:5f9c..62e3". |
| 172 | target = re.search(r'--target (.+) &&', lines[4].strip()) |
| 173 | assert target is not None, \ |
| 174 | "Failed to parse target line \"{}\"".format(lines[4]) |
| 175 | flash_partition = target.group(1) |
| 176 | |
| 177 | # Check we have the same recovery target in the check and flash commands. |
| 178 | assert check_partition == flash_partition, \ |
| 179 | "Mismatching targets: {} vs {}".format(check_partition, flash_partition) |
| 180 | |
| 181 | # Validate the SHA-1 of the recovery image. |
| 182 | recovery_sha1 = flash_partition.split(':')[3] |
| 183 | ValidateFileAgainstSha1( |
| 184 | input_tmp, 'recovery.img', 'SYSTEM/etc/recovery.img', recovery_sha1) |
| 185 | else: |
| 186 | assert len(lines) == 11, "Invalid line count: {}".format(lines) |
| 187 | |
| 188 | # --source boot_type:boot_device:boot_size:boot_sha1 |
| 189 | source = re.search(r'--source (\w+:.+:\w+:\w+) \\', lines[4].strip()) |
| 190 | assert source is not None, \ |
| 191 | "Failed to parse source line \"{}\"".format(lines[4]) |
| 192 | |
| 193 | source_partition = source.group(1) |
| 194 | source_info = source_partition.split(':') |
| 195 | assert len(source_info) == 4, \ |
| 196 | "Invalid source partition: {}".format(source_partition) |
Tianjie Xu | 9c384d2 | 2017-06-20 17:00:55 -0700 | [diff] [blame] | 197 | ValidateFileAgainstSha1(input_tmp, file_name='boot.img', |
Tao Bao | bb20e8c | 2018-02-01 12:00:19 -0800 | [diff] [blame] | 198 | file_path='IMAGES/boot.img', |
Tao Bao | 4948aed | 2018-07-13 16:11:16 -0700 | [diff] [blame] | 199 | expected_sha1=source_info[3]) |
Tianjie Xu | 9c384d2 | 2017-06-20 17:00:55 -0700 | [diff] [blame] | 200 | |
Tao Bao | 4948aed | 2018-07-13 16:11:16 -0700 | [diff] [blame] | 201 | # --target recovery_type:recovery_device:recovery_size:recovery_sha1 |
| 202 | target = re.search(r'--target (\w+:.+:\w+:\w+) && \\', lines[5].strip()) |
| 203 | assert target is not None, \ |
| 204 | "Failed to parse target line \"{}\"".format(lines[5]) |
| 205 | target_partition = target.group(1) |
| 206 | |
| 207 | # Check we have the same recovery target in the check and patch commands. |
| 208 | assert check_partition == target_partition, \ |
| 209 | "Mismatching targets: {} vs {}".format( |
| 210 | check_partition, target_partition) |
| 211 | |
| 212 | recovery_info = target_partition.split(':') |
| 213 | assert len(recovery_info) == 4, \ |
| 214 | "Invalid target partition: {}".format(target_partition) |
Tianjie Xu | 9c384d2 | 2017-06-20 17:00:55 -0700 | [diff] [blame] | 215 | ValidateFileAgainstSha1(input_tmp, file_name='recovery.img', |
Tao Bao | bb20e8c | 2018-02-01 12:00:19 -0800 | [diff] [blame] | 216 | file_path='IMAGES/recovery.img', |
Tao Bao | 4948aed | 2018-07-13 16:11:16 -0700 | [diff] [blame] | 217 | expected_sha1=recovery_info[3]) |
Tianjie Xu | 9c384d2 | 2017-06-20 17:00:55 -0700 | [diff] [blame] | 218 | |
Tao Bao | bb20e8c | 2018-02-01 12:00:19 -0800 | [diff] [blame] | 219 | logging.info('Done checking %s', script_path) |
Tianjie Xu | 9c384d2 | 2017-06-20 17:00:55 -0700 | [diff] [blame] | 220 | |
| 221 | |
Tao Bao | ba55770 | 2018-03-10 20:41:16 -0800 | [diff] [blame] | 222 | def ValidateVerifiedBootImages(input_tmp, info_dict, options): |
| 223 | """Validates the Verified Boot related images. |
Tao Bao | afaa0a6 | 2017-02-27 15:08:36 -0800 | [diff] [blame] | 224 | |
Tao Bao | ba55770 | 2018-03-10 20:41:16 -0800 | [diff] [blame] | 225 | For Verified Boot 1.0, it verifies the signatures of the bootable images |
| 226 | (boot/recovery etc), as well as the dm-verity metadata in system images |
| 227 | (system/vendor/product). For Verified Boot 2.0, it calls avbtool to verify |
| 228 | vbmeta.img, which in turn verifies all the descriptors listed in vbmeta. |
Tao Bao | afaa0a6 | 2017-02-27 15:08:36 -0800 | [diff] [blame] | 229 | |
Tao Bao | ba55770 | 2018-03-10 20:41:16 -0800 | [diff] [blame] | 230 | Args: |
| 231 | input_tmp: The top-level directory of unpacked target-files.zip. |
| 232 | info_dict: The loaded info dict. |
| 233 | options: A dict that contains the user-supplied public keys to be used for |
| 234 | image verification. In particular, 'verity_key' is used to verify the |
| 235 | bootable images in VB 1.0, and the vbmeta image in VB 2.0, where |
| 236 | applicable. 'verity_key_mincrypt' will be used to verify the system |
| 237 | images in VB 1.0. |
| 238 | |
| 239 | Raises: |
| 240 | AssertionError: On any verification failure. |
| 241 | """ |
| 242 | # Verified boot 1.0 (images signed with boot_signer and verity_signer). |
| 243 | if info_dict.get('boot_signer') == 'true': |
| 244 | logging.info('Verifying Verified Boot images...') |
| 245 | |
| 246 | # Verify the boot/recovery images (signed with boot_signer), against the |
| 247 | # given X.509 encoded pubkey (or falling back to the one in the info_dict if |
| 248 | # none given). |
| 249 | verity_key = options['verity_key'] |
| 250 | if verity_key is None: |
| 251 | verity_key = info_dict['verity_key'] + '.x509.pem' |
| 252 | for image in ('boot.img', 'recovery.img', 'recovery-two-step.img'): |
| 253 | image_path = os.path.join(input_tmp, 'IMAGES', image) |
| 254 | if not os.path.exists(image_path): |
| 255 | continue |
| 256 | |
| 257 | cmd = ['boot_signer', '-verify', image_path, '-certificate', verity_key] |
Tao Bao | 73dd4f4 | 2018-10-04 16:25:33 -0700 | [diff] [blame^] | 258 | proc = common.Run(cmd) |
Tao Bao | ba55770 | 2018-03-10 20:41:16 -0800 | [diff] [blame] | 259 | stdoutdata, _ = proc.communicate() |
| 260 | assert proc.returncode == 0, \ |
| 261 | 'Failed to verify {} with boot_signer:\n{}'.format(image, stdoutdata) |
| 262 | logging.info( |
| 263 | 'Verified %s with boot_signer (key: %s):\n%s', image, verity_key, |
| 264 | stdoutdata.rstrip()) |
| 265 | |
| 266 | # Verify verity signed system images in Verified Boot 1.0. Note that not using |
| 267 | # 'elif' here, since 'boot_signer' and 'verity' are not bundled in VB 1.0. |
| 268 | if info_dict.get('verity') == 'true': |
| 269 | # First verify that the verity key that's built into the root image (as |
| 270 | # /verity_key) matches the one given via command line, if any. |
| 271 | if info_dict.get("system_root_image") == "true": |
| 272 | verity_key_mincrypt = os.path.join(input_tmp, 'ROOT', 'verity_key') |
| 273 | else: |
| 274 | verity_key_mincrypt = os.path.join( |
| 275 | input_tmp, 'BOOT', 'RAMDISK', 'verity_key') |
| 276 | assert os.path.exists(verity_key_mincrypt), 'Missing verity_key' |
| 277 | |
| 278 | if options['verity_key_mincrypt'] is None: |
| 279 | logging.warn( |
| 280 | 'Skipped checking the content of /verity_key, as the key file not ' |
| 281 | 'provided. Use --verity_key_mincrypt to specify.') |
| 282 | else: |
| 283 | expected_key = options['verity_key_mincrypt'] |
| 284 | assert filecmp.cmp(expected_key, verity_key_mincrypt, shallow=False), \ |
| 285 | "Mismatching mincrypt verity key files" |
| 286 | logging.info('Verified the content of /verity_key') |
| 287 | |
| 288 | # Then verify the verity signed system/vendor/product images, against the |
| 289 | # verity pubkey in mincrypt format. |
| 290 | for image in ('system.img', 'vendor.img', 'product.img'): |
| 291 | image_path = os.path.join(input_tmp, 'IMAGES', image) |
| 292 | |
| 293 | # We are not checking if the image is actually enabled via info_dict (e.g. |
| 294 | # 'system_verity_block_device=...'). Because it's most likely a bug that |
| 295 | # skips signing some of the images in signed target-files.zip, while |
| 296 | # having the top-level verity flag enabled. |
| 297 | if not os.path.exists(image_path): |
| 298 | continue |
| 299 | |
| 300 | cmd = ['verity_verifier', image_path, '-mincrypt', verity_key_mincrypt] |
Tao Bao | 73dd4f4 | 2018-10-04 16:25:33 -0700 | [diff] [blame^] | 301 | proc = common.Run(cmd) |
Tao Bao | ba55770 | 2018-03-10 20:41:16 -0800 | [diff] [blame] | 302 | stdoutdata, _ = proc.communicate() |
| 303 | assert proc.returncode == 0, \ |
| 304 | 'Failed to verify {} with verity_verifier (key: {}):\n{}'.format( |
| 305 | image, verity_key_mincrypt, stdoutdata) |
| 306 | logging.info( |
| 307 | 'Verified %s with verity_verifier (key: %s):\n%s', image, |
| 308 | verity_key_mincrypt, stdoutdata.rstrip()) |
| 309 | |
| 310 | # Handle the case of Verified Boot 2.0 (AVB). |
| 311 | if info_dict.get("avb_enable") == "true": |
| 312 | logging.info('Verifying Verified Boot 2.0 (AVB) images...') |
| 313 | |
| 314 | key = options['verity_key'] |
| 315 | if key is None: |
| 316 | key = info_dict['avb_vbmeta_key_path'] |
Tao Bao | 02a0859 | 2018-07-22 12:40:45 -0700 | [diff] [blame] | 317 | |
Tao Bao | ba55770 | 2018-03-10 20:41:16 -0800 | [diff] [blame] | 318 | # avbtool verifies all the images that have descriptors listed in vbmeta. |
| 319 | image = os.path.join(input_tmp, 'IMAGES', 'vbmeta.img') |
| 320 | cmd = ['avbtool', 'verify_image', '--image', image, '--key', key] |
Tao Bao | 02a0859 | 2018-07-22 12:40:45 -0700 | [diff] [blame] | 321 | |
| 322 | # Append the args for chained partitions if any. |
| 323 | for partition in common.AVB_PARTITIONS: |
| 324 | key_name = 'avb_' + partition + '_key_path' |
| 325 | if info_dict.get(key_name) is not None: |
| 326 | chained_partition_arg = common.GetAvbChainedPartitionArg( |
| 327 | partition, info_dict, options[key_name]) |
| 328 | cmd.extend(["--expected_chain_partition", chained_partition_arg]) |
| 329 | |
Tao Bao | 73dd4f4 | 2018-10-04 16:25:33 -0700 | [diff] [blame^] | 330 | proc = common.Run(cmd) |
Tao Bao | ba55770 | 2018-03-10 20:41:16 -0800 | [diff] [blame] | 331 | stdoutdata, _ = proc.communicate() |
| 332 | assert proc.returncode == 0, \ |
| 333 | 'Failed to verify {} with verity_verifier (key: {}):\n{}'.format( |
| 334 | image, key, stdoutdata) |
| 335 | |
| 336 | logging.info( |
| 337 | 'Verified %s with avbtool (key: %s):\n%s', image, key, |
| 338 | stdoutdata.rstrip()) |
| 339 | |
| 340 | |
| 341 | def main(): |
| 342 | parser = argparse.ArgumentParser( |
| 343 | description=__doc__, |
| 344 | formatter_class=argparse.RawDescriptionHelpFormatter) |
| 345 | parser.add_argument( |
| 346 | 'target_files', |
| 347 | help='the input target_files.zip to be validated') |
| 348 | parser.add_argument( |
| 349 | '--verity_key', |
| 350 | help='the verity public key to verify the bootable images (Verified ' |
Tao Bao | 02a0859 | 2018-07-22 12:40:45 -0700 | [diff] [blame] | 351 | 'Boot 1.0), or the vbmeta image (Verified Boot 2.0, aka AVB), where ' |
Tao Bao | ba55770 | 2018-03-10 20:41:16 -0800 | [diff] [blame] | 352 | 'applicable') |
Tao Bao | 02a0859 | 2018-07-22 12:40:45 -0700 | [diff] [blame] | 353 | for partition in common.AVB_PARTITIONS: |
| 354 | parser.add_argument( |
| 355 | '--avb_' + partition + '_key_path', |
| 356 | help='the public or private key in PEM format to verify AVB chained ' |
| 357 | 'partition of {}'.format(partition)) |
Tao Bao | ba55770 | 2018-03-10 20:41:16 -0800 | [diff] [blame] | 358 | parser.add_argument( |
| 359 | '--verity_key_mincrypt', |
| 360 | help='the verity public key in mincrypt format to verify the system ' |
| 361 | 'images, if target using Verified Boot 1.0') |
| 362 | args = parser.parse_args() |
| 363 | |
| 364 | # Unprovided args will have 'None' as the value. |
| 365 | options = vars(args) |
Tao Bao | afaa0a6 | 2017-02-27 15:08:36 -0800 | [diff] [blame] | 366 | |
| 367 | logging_format = '%(asctime)s - %(filename)s - %(levelname)-8s: %(message)s' |
| 368 | date_format = '%Y/%m/%d %H:%M:%S' |
| 369 | logging.basicConfig(level=logging.INFO, format=logging_format, |
| 370 | datefmt=date_format) |
| 371 | |
Tao Bao | ba55770 | 2018-03-10 20:41:16 -0800 | [diff] [blame] | 372 | logging.info("Unzipping the input target_files.zip: %s", args.target_files) |
| 373 | input_tmp = common.UnzipTemp(args.target_files) |
Tao Bao | afaa0a6 | 2017-02-27 15:08:36 -0800 | [diff] [blame] | 374 | |
Tianjie Xu | 9c384d2 | 2017-06-20 17:00:55 -0700 | [diff] [blame] | 375 | info_dict = common.LoadInfoDict(input_tmp) |
Tao Bao | 63e2f49 | 2018-05-11 23:38:46 -0700 | [diff] [blame] | 376 | with zipfile.ZipFile(args.target_files, 'r') as input_zip: |
| 377 | ValidateFileConsistency(input_zip, input_tmp, info_dict) |
| 378 | |
Tianjie Xu | 9c384d2 | 2017-06-20 17:00:55 -0700 | [diff] [blame] | 379 | ValidateInstallRecoveryScript(input_tmp, info_dict) |
| 380 | |
Tao Bao | ba55770 | 2018-03-10 20:41:16 -0800 | [diff] [blame] | 381 | ValidateVerifiedBootImages(input_tmp, info_dict, options) |
| 382 | |
Tao Bao | afaa0a6 | 2017-02-27 15:08:36 -0800 | [diff] [blame] | 383 | # TODO: Check if the OTA keys have been properly updated (the ones on /system, |
| 384 | # in recovery image). |
| 385 | |
Tao Bao | afaa0a6 | 2017-02-27 15:08:36 -0800 | [diff] [blame] | 386 | logging.info("Done.") |
| 387 | |
| 388 | |
| 389 | if __name__ == '__main__': |
| 390 | try: |
Tao Bao | ba55770 | 2018-03-10 20:41:16 -0800 | [diff] [blame] | 391 | main() |
Tao Bao | afaa0a6 | 2017-02-27 15:08:36 -0800 | [diff] [blame] | 392 | finally: |
| 393 | common.Cleanup() |