Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright (C) 2019 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 | import logging |
| 18 | import os.path |
| 19 | import re |
| 20 | import shlex |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 21 | import shutil |
Tao Bao | e7354ba | 2019-05-09 16:54:15 -0700 | [diff] [blame] | 22 | import zipfile |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 23 | |
Mohammad Samiul Islam | 9fd5886 | 2021-01-06 13:33:25 +0000 | [diff] [blame] | 24 | import apex_manifest |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 25 | import common |
Mohammad Samiul Islam | 9fd5886 | 2021-01-06 13:33:25 +0000 | [diff] [blame] | 26 | from common import UnzipTemp, RunAndCheckOutput, MakeTempFile, OPTIONS |
| 27 | |
| 28 | import ota_metadata_pb2 |
| 29 | |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 30 | |
| 31 | logger = logging.getLogger(__name__) |
| 32 | |
Tao Bao | e7354ba | 2019-05-09 16:54:15 -0700 | [diff] [blame] | 33 | OPTIONS = common.OPTIONS |
| 34 | |
Tianjie | c180a5d | 2020-03-23 18:14:09 -0700 | [diff] [blame] | 35 | APEX_PAYLOAD_IMAGE = 'apex_payload.img' |
| 36 | |
Nikita Ioffe | 3608148 | 2021-01-20 01:32:28 +0000 | [diff] [blame] | 37 | APEX_PUBKEY = 'apex_pubkey' |
| 38 | |
Jooyung Han | 1e45baf | 2024-08-15 07:38:17 +0000 | [diff] [blame] | 39 | # Partitions supporting APEXes |
| 40 | PARTITIONS = ['system', 'system_ext', 'product', 'vendor', 'odm'] |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 41 | |
| 42 | class ApexInfoError(Exception): |
| 43 | """An Exception raised during Apex Information command.""" |
| 44 | |
| 45 | def __init__(self, message): |
| 46 | Exception.__init__(self, message) |
| 47 | |
| 48 | |
| 49 | class ApexSigningError(Exception): |
| 50 | """An Exception raised during Apex Payload signing.""" |
| 51 | |
| 52 | def __init__(self, message): |
| 53 | Exception.__init__(self, message) |
| 54 | |
| 55 | |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 56 | class ApexApkSigner(object): |
Jooyung Han | 0f5a41d | 2021-10-27 03:53:21 +0900 | [diff] [blame] | 57 | """Class to sign the apk files and other files in an apex payload image and repack the apex""" |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 58 | |
Melisa Carranza Zúñiga | 8e3198a | 2022-04-13 16:23:45 +0000 | [diff] [blame] | 59 | def __init__(self, apex_path, key_passwords, codename_to_api_level_map, avbtool=None, sign_tool=None): |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 60 | self.apex_path = apex_path |
Oleh Cherpak | e555ab1 | 2020-10-05 17:04:59 +0300 | [diff] [blame] | 61 | if not key_passwords: |
| 62 | self.key_passwords = dict() |
| 63 | else: |
| 64 | self.key_passwords = key_passwords |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 65 | self.codename_to_api_level_map = codename_to_api_level_map |
Kelvin Zhang | dd833dc | 2020-08-21 14:13:13 -0400 | [diff] [blame] | 66 | self.debugfs_path = os.path.join( |
| 67 | OPTIONS.search_path, "bin", "debugfs_static") |
Dennis Shen | f58e548 | 2022-10-10 21:19:46 +0000 | [diff] [blame] | 68 | self.fsckerofs_path = os.path.join( |
| 69 | OPTIONS.search_path, "bin", "fsck.erofs") |
Jooyung Han | 0f5a41d | 2021-10-27 03:53:21 +0900 | [diff] [blame] | 70 | self.avbtool = avbtool if avbtool else "avbtool" |
| 71 | self.sign_tool = sign_tool |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 72 | |
Thiébaud Weksteen | 62865ca | 2023-10-18 11:08:47 +1100 | [diff] [blame] | 73 | def ProcessApexFile(self, apk_keys, payload_key, signing_args=None): |
Jooyung Han | 0f5a41d | 2021-10-27 03:53:21 +0900 | [diff] [blame] | 74 | """Scans and signs the payload files and repack the apex |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 75 | |
| 76 | Args: |
| 77 | apk_keys: A dict that holds the signing keys for apk files. |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 78 | |
| 79 | Returns: |
| 80 | The repacked apex file containing the signed apk files. |
| 81 | """ |
Jooyung Han | 03d62d5 | 2024-12-02 16:54:40 +0900 | [diff] [blame] | 82 | payload_dir = self.ExtractApexPayload(self.apex_path) |
| 83 | apk_entries = [] |
| 84 | for base_dir, _, files in os.walk(payload_dir): |
| 85 | apk_entries.extend(os.path.join(base_dir, file) for file in files if file.endswith('.apk')) |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 86 | |
| 87 | # No need to sign and repack, return the original apex path. |
Thiébaud Weksteen | 62865ca | 2023-10-18 11:08:47 +1100 | [diff] [blame] | 88 | if not apk_entries and self.sign_tool is None: |
Melisa Carranza Zúñiga | 8e3198a | 2022-04-13 16:23:45 +0000 | [diff] [blame] | 89 | logger.info('No apk file to sign in %s', self.apex_path) |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 90 | return self.apex_path |
| 91 | |
| 92 | for entry in apk_entries: |
| 93 | apk_name = os.path.basename(entry) |
| 94 | if apk_name not in apk_keys: |
| 95 | raise ApexSigningError('Failed to find signing keys for apk file {} in' |
| 96 | ' apex {}. Use "-e <apkname>=" to specify a key' |
| 97 | .format(entry, self.apex_path)) |
| 98 | if not any(dirname in entry for dirname in ['app/', 'priv-app/', |
| 99 | 'overlay/']): |
| 100 | logger.warning('Apk path does not contain the intended directory name:' |
| 101 | ' %s', entry) |
| 102 | |
Jooyung Han | 03d62d5 | 2024-12-02 16:54:40 +0900 | [diff] [blame] | 103 | has_signed_content = self.SignContentsInPayload( |
| 104 | payload_dir, apk_entries, apk_keys, payload_key, signing_args) |
Jooyung Han | 0f5a41d | 2021-10-27 03:53:21 +0900 | [diff] [blame] | 105 | if not has_signed_content: |
Thiébaud Weksteen | 62865ca | 2023-10-18 11:08:47 +1100 | [diff] [blame] | 106 | logger.info('No contents has been signed in %s', self.apex_path) |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 107 | return self.apex_path |
| 108 | |
Baligh Uddin | 639b3b7 | 2020-03-25 20:50:23 -0700 | [diff] [blame] | 109 | return self.RepackApexPayload(payload_dir, payload_key, signing_args) |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 110 | |
Jooyung Han | 03d62d5 | 2024-12-02 16:54:40 +0900 | [diff] [blame] | 111 | def ExtractApexPayload(self, apex_path): |
| 112 | """Extracts the contents of an APEX and returns the directory of the contents""" |
Kelvin Zhang | dd833dc | 2020-08-21 14:13:13 -0400 | [diff] [blame] | 113 | if not os.path.exists(self.debugfs_path): |
| 114 | raise ApexSigningError( |
| 115 | "Couldn't find location of debugfs_static: " + |
Mohammad Samiul Islam | 9fd5886 | 2021-01-06 13:33:25 +0000 | [diff] [blame] | 116 | "Path {} does not exist. ".format(self.debugfs_path) + |
Kelvin Zhang | dd833dc | 2020-08-21 14:13:13 -0400 | [diff] [blame] | 117 | "Make sure bin/debugfs_static can be found in -p <path>") |
Dennis Shen | f58e548 | 2022-10-10 21:19:46 +0000 | [diff] [blame] | 118 | if not os.path.exists(self.fsckerofs_path): |
| 119 | raise ApexSigningError( |
| 120 | "Couldn't find location of fsck.erofs: " + |
| 121 | "Path {} does not exist. ".format(self.fsckerofs_path) + |
| 122 | "Make sure bin/fsck.erofs can be found in -p <path>") |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 123 | payload_dir = common.MakeTempDir() |
Dennis Shen | f58e548 | 2022-10-10 21:19:46 +0000 | [diff] [blame] | 124 | extract_cmd = ['deapexer', '--debugfs_path', self.debugfs_path, |
Dennis Shen | a8d1143 | 2022-11-07 21:37:06 +0000 | [diff] [blame] | 125 | '--fsckerofs_path', self.fsckerofs_path, |
Jooyung Han | 6294902 | 2023-06-14 15:16:34 +0900 | [diff] [blame] | 126 | 'extract', |
Jooyung Han | 03d62d5 | 2024-12-02 16:54:40 +0900 | [diff] [blame] | 127 | apex_path, payload_dir] |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 128 | common.RunAndCheckOutput(extract_cmd) |
Jooyung Han | 03d62d5 | 2024-12-02 16:54:40 +0900 | [diff] [blame] | 129 | return payload_dir |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 130 | |
Jooyung Han | 03d62d5 | 2024-12-02 16:54:40 +0900 | [diff] [blame] | 131 | def SignContentsInPayload(self, payload_dir, apk_entries, apk_keys, payload_key, signing_args): |
| 132 | """Signs the contents in payload.""" |
Jooyung Han | 0f5a41d | 2021-10-27 03:53:21 +0900 | [diff] [blame] | 133 | has_signed_content = False |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 134 | for entry in apk_entries: |
| 135 | apk_path = os.path.join(payload_dir, entry) |
Thiébaud Weksteen | 62865ca | 2023-10-18 11:08:47 +1100 | [diff] [blame] | 136 | assert os.path.exists(self.apex_path) |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 137 | |
| 138 | key_name = apk_keys.get(os.path.basename(entry)) |
| 139 | if key_name in common.SPECIAL_CERT_STRINGS: |
| 140 | logger.info('Not signing: %s due to special cert string', apk_path) |
| 141 | continue |
| 142 | |
| 143 | logger.info('Signing apk file %s in apex %s', apk_path, self.apex_path) |
| 144 | # Rename the unsigned apk and overwrite the original apk path with the |
| 145 | # signed apk file. |
| 146 | unsigned_apk = common.MakeTempFile() |
| 147 | os.rename(apk_path, unsigned_apk) |
Mohammad Samiul Islam | 9fd5886 | 2021-01-06 13:33:25 +0000 | [diff] [blame] | 148 | common.SignFile( |
| 149 | unsigned_apk, apk_path, key_name, self.key_passwords.get(key_name), |
| 150 | codename_to_api_level_map=self.codename_to_api_level_map) |
Jooyung Han | 0f5a41d | 2021-10-27 03:53:21 +0900 | [diff] [blame] | 151 | has_signed_content = True |
| 152 | |
| 153 | if self.sign_tool: |
Jooyung Han | 8caba5e | 2021-10-27 03:58:09 +0900 | [diff] [blame] | 154 | logger.info('Signing payload contents in apex %s with %s', self.apex_path, self.sign_tool) |
Jooyung Han | 39259ec | 2022-02-07 15:56:53 +0900 | [diff] [blame] | 155 | # Pass avbtool to the custom signing tool |
| 156 | cmd = [self.sign_tool, '--avbtool', self.avbtool] |
| 157 | # Pass signing_args verbatim which will be forwarded to avbtool (e.g. --signing_helper=...) |
| 158 | if signing_args: |
| 159 | cmd.extend(['--signing_args', '"{}"'.format(signing_args)]) |
| 160 | cmd.extend([payload_key, payload_dir]) |
Jooyung Han | 0f5a41d | 2021-10-27 03:53:21 +0900 | [diff] [blame] | 161 | common.RunAndCheckOutput(cmd) |
| 162 | has_signed_content = True |
| 163 | |
Jooyung Han | 03d62d5 | 2024-12-02 16:54:40 +0900 | [diff] [blame] | 164 | return has_signed_content |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 165 | |
Baligh Uddin | 639b3b7 | 2020-03-25 20:50:23 -0700 | [diff] [blame] | 166 | def RepackApexPayload(self, payload_dir, payload_key, signing_args=None): |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 167 | """Rebuilds the apex file with the updated payload directory.""" |
| 168 | apex_dir = common.MakeTempDir() |
| 169 | # Extract the apex file and reuse its meta files as repack parameters. |
| 170 | common.UnzipToDir(self.apex_path, apex_dir) |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 171 | arguments_dict = { |
| 172 | 'manifest': os.path.join(apex_dir, 'apex_manifest.pb'), |
| 173 | 'build_info': os.path.join(apex_dir, 'apex_build_info.pb'), |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 174 | 'key': payload_key, |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 175 | } |
| 176 | for filename in arguments_dict.values(): |
| 177 | assert os.path.exists(filename), 'file {} not found'.format(filename) |
| 178 | |
| 179 | # The repack process will add back these files later in the payload image. |
| 180 | for name in ['apex_manifest.pb', 'apex_manifest.json', 'lost+found']: |
| 181 | path = os.path.join(payload_dir, name) |
| 182 | if os.path.isfile(path): |
| 183 | os.remove(path) |
| 184 | elif os.path.isdir(path): |
Baligh Uddin | be2d7d0 | 2022-02-26 02:34:06 +0000 | [diff] [blame] | 185 | shutil.rmtree(path, ignore_errors=True) |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 186 | |
Tianjie | c180a5d | 2020-03-23 18:14:09 -0700 | [diff] [blame] | 187 | # TODO(xunchang) the signing process can be improved by using |
| 188 | # '--unsigned_payload_only'. But we need to parse the vbmeta earlier for |
| 189 | # the signing arguments, e.g. algorithm, salt, etc. |
| 190 | payload_img = os.path.join(apex_dir, APEX_PAYLOAD_IMAGE) |
| 191 | generate_image_cmd = ['apexer', '--force', '--payload_only', |
| 192 | '--do_not_check_keyname', '--apexer_tool_path', |
| 193 | os.getenv('PATH')] |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 194 | for key, val in arguments_dict.items(): |
Tianjie | c180a5d | 2020-03-23 18:14:09 -0700 | [diff] [blame] | 195 | generate_image_cmd.extend(['--' + key, val]) |
Baligh Uddin | 639b3b7 | 2020-03-25 20:50:23 -0700 | [diff] [blame] | 196 | |
| 197 | # Add quote to the signing_args as we will pass |
| 198 | # --signing_args "--signing_helper_with_files=%path" to apexer |
| 199 | if signing_args: |
Kelvin Zhang | d6b799a | 2020-08-19 14:54:42 -0400 | [diff] [blame] | 200 | generate_image_cmd.extend( |
Aaron Homer | 58992a6 | 2024-08-26 15:15:55 +0000 | [diff] [blame] | 201 | ['--signing_args', '"{}"'.format(signing_args)]) |
Baligh Uddin | 639b3b7 | 2020-03-25 20:50:23 -0700 | [diff] [blame] | 202 | |
Tianjie Xu | 83bd55c | 2020-01-29 11:37:43 -0800 | [diff] [blame] | 203 | # optional arguments for apex repacking |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 204 | manifest_json = os.path.join(apex_dir, 'apex_manifest.json') |
| 205 | if os.path.exists(manifest_json): |
Tianjie | c180a5d | 2020-03-23 18:14:09 -0700 | [diff] [blame] | 206 | generate_image_cmd.extend(['--manifest_json', manifest_json]) |
| 207 | generate_image_cmd.extend([payload_dir, payload_img]) |
Tianjie Xu | cea6ad1 | 2020-01-30 17:12:05 -0800 | [diff] [blame] | 208 | if OPTIONS.verbose: |
Tianjie | c180a5d | 2020-03-23 18:14:09 -0700 | [diff] [blame] | 209 | generate_image_cmd.append('-v') |
| 210 | common.RunAndCheckOutput(generate_image_cmd) |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 211 | |
Tianjie | c180a5d | 2020-03-23 18:14:09 -0700 | [diff] [blame] | 212 | # Add the payload image back to the apex file. |
| 213 | common.ZipDelete(self.apex_path, APEX_PAYLOAD_IMAGE) |
Kelvin Zhang | 928c234 | 2020-09-22 16:15:57 -0400 | [diff] [blame] | 214 | with zipfile.ZipFile(self.apex_path, 'a', allowZip64=True) as output_apex: |
Tianjie | c180a5d | 2020-03-23 18:14:09 -0700 | [diff] [blame] | 215 | common.ZipWrite(output_apex, payload_img, APEX_PAYLOAD_IMAGE, |
| 216 | compress_type=zipfile.ZIP_STORED) |
| 217 | return self.apex_path |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 218 | |
| 219 | |
Tao Bao | 1ac886e | 2019-06-26 11:58:22 -0700 | [diff] [blame] | 220 | def SignApexPayload(avbtool, payload_file, payload_key_path, payload_key_name, |
Jiyong Park | a1887f3 | 2020-05-19 23:18:03 +0900 | [diff] [blame] | 221 | algorithm, salt, hash_algorithm, no_hashtree, signing_args=None): |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 222 | """Signs a given payload_file with the payload key.""" |
| 223 | # Add the new footer. Old footer, if any, will be replaced by avbtool. |
Tao Bao | 1ac886e | 2019-06-26 11:58:22 -0700 | [diff] [blame] | 224 | cmd = [avbtool, 'add_hashtree_footer', |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 225 | '--do_not_generate_fec', |
| 226 | '--algorithm', algorithm, |
| 227 | '--key', payload_key_path, |
| 228 | '--prop', 'apex.key:{}'.format(payload_key_name), |
| 229 | '--image', payload_file, |
Jiyong Park | a1887f3 | 2020-05-19 23:18:03 +0900 | [diff] [blame] | 230 | '--salt', salt, |
| 231 | '--hash_algorithm', hash_algorithm] |
Tao Bao | 448004a | 2019-09-19 07:55:02 -0700 | [diff] [blame] | 232 | if no_hashtree: |
| 233 | cmd.append('--no_hashtree') |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 234 | if signing_args: |
| 235 | cmd.extend(shlex.split(signing_args)) |
| 236 | |
| 237 | try: |
| 238 | common.RunAndCheckOutput(cmd) |
| 239 | except common.ExternalError as e: |
Tao Bao | 86b529a | 2019-06-19 17:03:37 -0700 | [diff] [blame] | 240 | raise ApexSigningError( |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 241 | 'Failed to sign APEX payload {} with {}:\n{}'.format( |
Tao Bao | 86b529a | 2019-06-19 17:03:37 -0700 | [diff] [blame] | 242 | payload_file, payload_key_path, e)) |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 243 | |
| 244 | # Verify the signed payload image with specified public key. |
| 245 | logger.info('Verifying %s', payload_file) |
Tao Bao | 448004a | 2019-09-19 07:55:02 -0700 | [diff] [blame] | 246 | VerifyApexPayload(avbtool, payload_file, payload_key_path, no_hashtree) |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 247 | |
| 248 | |
Tao Bao | 448004a | 2019-09-19 07:55:02 -0700 | [diff] [blame] | 249 | def VerifyApexPayload(avbtool, payload_file, payload_key, no_hashtree=False): |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 250 | """Verifies the APEX payload signature with the given key.""" |
Tao Bao | 1ac886e | 2019-06-26 11:58:22 -0700 | [diff] [blame] | 251 | cmd = [avbtool, 'verify_image', '--image', payload_file, |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 252 | '--key', payload_key] |
Tao Bao | 448004a | 2019-09-19 07:55:02 -0700 | [diff] [blame] | 253 | if no_hashtree: |
| 254 | cmd.append('--accept_zeroed_hashtree') |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 255 | try: |
| 256 | common.RunAndCheckOutput(cmd) |
| 257 | except common.ExternalError as e: |
Tao Bao | 86b529a | 2019-06-19 17:03:37 -0700 | [diff] [blame] | 258 | raise ApexSigningError( |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 259 | 'Failed to validate payload signing for {} with {}:\n{}'.format( |
Tao Bao | 86b529a | 2019-06-19 17:03:37 -0700 | [diff] [blame] | 260 | payload_file, payload_key, e)) |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 261 | |
| 262 | |
Tao Bao | 1ac886e | 2019-06-26 11:58:22 -0700 | [diff] [blame] | 263 | def ParseApexPayloadInfo(avbtool, payload_path): |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 264 | """Parses the APEX payload info. |
| 265 | |
| 266 | Args: |
Tao Bao | 1ac886e | 2019-06-26 11:58:22 -0700 | [diff] [blame] | 267 | avbtool: The AVB tool to use. |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 268 | payload_path: The path to the payload image. |
| 269 | |
| 270 | Raises: |
| 271 | ApexInfoError on parsing errors. |
| 272 | |
| 273 | Returns: |
| 274 | A dict that contains payload property-value pairs. The dict should at least |
Tao Bao | 448004a | 2019-09-19 07:55:02 -0700 | [diff] [blame] | 275 | contain Algorithm, Salt, Tree Size and apex.key. |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 276 | """ |
| 277 | if not os.path.exists(payload_path): |
| 278 | raise ApexInfoError('Failed to find image: {}'.format(payload_path)) |
| 279 | |
Tao Bao | 1ac886e | 2019-06-26 11:58:22 -0700 | [diff] [blame] | 280 | cmd = [avbtool, 'info_image', '--image', payload_path] |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 281 | try: |
| 282 | output = common.RunAndCheckOutput(cmd) |
| 283 | except common.ExternalError as e: |
Tao Bao | 86b529a | 2019-06-19 17:03:37 -0700 | [diff] [blame] | 284 | raise ApexInfoError( |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 285 | 'Failed to get APEX payload info for {}:\n{}'.format( |
Tao Bao | 86b529a | 2019-06-19 17:03:37 -0700 | [diff] [blame] | 286 | payload_path, e)) |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 287 | |
Jiyong Park | a1887f3 | 2020-05-19 23:18:03 +0900 | [diff] [blame] | 288 | # Extract the Algorithm / Hash Algorithm / Salt / Prop info / Tree size from |
| 289 | # payload (i.e. an image signed with avbtool). For example, |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 290 | # Algorithm: SHA256_RSA4096 |
| 291 | PAYLOAD_INFO_PATTERN = ( |
Jiyong Park | a1887f3 | 2020-05-19 23:18:03 +0900 | [diff] [blame] | 292 | r'^\s*(?P<key>Algorithm|Hash Algorithm|Salt|Prop|Tree Size)\:\s*(?P<value>.*?)$') |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 293 | payload_info_matcher = re.compile(PAYLOAD_INFO_PATTERN) |
| 294 | |
| 295 | payload_info = {} |
| 296 | for line in output.split('\n'): |
| 297 | line_info = payload_info_matcher.match(line) |
| 298 | if not line_info: |
| 299 | continue |
| 300 | |
| 301 | key, value = line_info.group('key'), line_info.group('value') |
| 302 | |
| 303 | if key == 'Prop': |
| 304 | # Further extract the property key-value pair, from a 'Prop:' line. For |
| 305 | # example, |
| 306 | # Prop: apex.key -> 'com.android.runtime' |
| 307 | # Note that avbtool writes single or double quotes around values. |
| 308 | PROPERTY_DESCRIPTOR_PATTERN = r'^\s*(?P<key>.*?)\s->\s*(?P<value>.*?)$' |
| 309 | |
| 310 | prop_matcher = re.compile(PROPERTY_DESCRIPTOR_PATTERN) |
| 311 | prop = prop_matcher.match(value) |
| 312 | if not prop: |
| 313 | raise ApexInfoError( |
| 314 | 'Failed to parse prop string {}'.format(value)) |
| 315 | |
| 316 | prop_key, prop_value = prop.group('key'), prop.group('value') |
| 317 | if prop_key == 'apex.key': |
| 318 | # avbtool dumps the prop value with repr(), which contains single / |
| 319 | # double quotes that we don't want. |
| 320 | payload_info[prop_key] = prop_value.strip('\"\'') |
| 321 | |
| 322 | else: |
| 323 | payload_info[key] = value |
| 324 | |
Ivan Lozano | b021b2a | 2020-07-28 09:31:06 -0400 | [diff] [blame] | 325 | # Validation check. |
Jiyong Park | a1887f3 | 2020-05-19 23:18:03 +0900 | [diff] [blame] | 326 | for key in ('Algorithm', 'Salt', 'apex.key', 'Hash Algorithm'): |
Tao Bao | 1cd59f2 | 2019-03-15 15:13:01 -0700 | [diff] [blame] | 327 | if key not in payload_info: |
| 328 | raise ApexInfoError( |
| 329 | 'Failed to find {} prop in {}'.format(key, payload_path)) |
| 330 | |
| 331 | return payload_info |
Tao Bao | e7354ba | 2019-05-09 16:54:15 -0700 | [diff] [blame] | 332 | |
| 333 | |
Nikita Ioffe | 3608148 | 2021-01-20 01:32:28 +0000 | [diff] [blame] | 334 | def SignUncompressedApex(avbtool, apex_file, payload_key, container_key, |
Nikita Ioffe | 6068e8d | 2021-01-12 00:03:02 +0000 | [diff] [blame] | 335 | container_pw, apk_keys, codename_to_api_level_map, |
Thiébaud Weksteen | 62865ca | 2023-10-18 11:08:47 +1100 | [diff] [blame] | 336 | no_hashtree, signing_args=None, sign_tool=None): |
Nikita Ioffe | 6068e8d | 2021-01-12 00:03:02 +0000 | [diff] [blame] | 337 | """Signs the current uncompressed APEX with the given payload/container keys. |
Tao Bao | e7354ba | 2019-05-09 16:54:15 -0700 | [diff] [blame] | 338 | |
| 339 | Args: |
Nikita Ioffe | 3608148 | 2021-01-20 01:32:28 +0000 | [diff] [blame] | 340 | apex_file: Uncompressed APEX file. |
Tao Bao | e7354ba | 2019-05-09 16:54:15 -0700 | [diff] [blame] | 341 | payload_key: The path to payload signing key (w/ extension). |
| 342 | container_key: The path to container signing key (w/o extension). |
| 343 | container_pw: The matching password of the container_key, or None. |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 344 | apk_keys: A dict that holds the signing keys for apk files. |
Tao Bao | e7354ba | 2019-05-09 16:54:15 -0700 | [diff] [blame] | 345 | codename_to_api_level_map: A dict that maps from codename to API level. |
Tao Bao | 448004a | 2019-09-19 07:55:02 -0700 | [diff] [blame] | 346 | no_hashtree: Don't include hashtree in the signed APEX. |
Tao Bao | e7354ba | 2019-05-09 16:54:15 -0700 | [diff] [blame] | 347 | signing_args: Additional args to be passed to the payload signer. |
Jooyung Han | 0f5a41d | 2021-10-27 03:53:21 +0900 | [diff] [blame] | 348 | sign_tool: A tool to sign the contents of the APEX. |
Tao Bao | e7354ba | 2019-05-09 16:54:15 -0700 | [diff] [blame] | 349 | |
| 350 | Returns: |
| 351 | The path to the signed APEX file. |
| 352 | """ |
Jooyung Han | 0f5a41d | 2021-10-27 03:53:21 +0900 | [diff] [blame] | 353 | # 1. Extract the apex payload image and sign the files (e.g. APKs). Repack |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 354 | # the apex file after signing. |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 355 | apk_signer = ApexApkSigner(apex_file, container_pw, |
Jooyung Han | 0f5a41d | 2021-10-27 03:53:21 +0900 | [diff] [blame] | 356 | codename_to_api_level_map, |
Melisa Carranza Zúñiga | 8e3198a | 2022-04-13 16:23:45 +0000 | [diff] [blame] | 357 | avbtool, sign_tool) |
Thiébaud Weksteen | 62865ca | 2023-10-18 11:08:47 +1100 | [diff] [blame] | 358 | apex_file = apk_signer.ProcessApexFile(apk_keys, payload_key, signing_args) |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 359 | |
| 360 | # 2a. Extract and sign the APEX_PAYLOAD_IMAGE entry with the given |
Tao Bao | e7354ba | 2019-05-09 16:54:15 -0700 | [diff] [blame] | 361 | # payload_key. |
| 362 | payload_dir = common.MakeTempDir(prefix='apex-payload-') |
| 363 | with zipfile.ZipFile(apex_file) as apex_fd: |
| 364 | payload_file = apex_fd.extract(APEX_PAYLOAD_IMAGE, payload_dir) |
Baligh Uddin | 1588128 | 2019-08-25 12:01:44 -0700 | [diff] [blame] | 365 | zip_items = apex_fd.namelist() |
Tao Bao | e7354ba | 2019-05-09 16:54:15 -0700 | [diff] [blame] | 366 | |
Tao Bao | 1ac886e | 2019-06-26 11:58:22 -0700 | [diff] [blame] | 367 | payload_info = ParseApexPayloadInfo(avbtool, payload_file) |
Kelvin Zhang | 7cab750 | 2021-08-02 19:58:14 -0400 | [diff] [blame] | 368 | if no_hashtree is None: |
| 369 | no_hashtree = payload_info.get("Tree Size", 0) == 0 |
Tao Bao | e7354ba | 2019-05-09 16:54:15 -0700 | [diff] [blame] | 370 | SignApexPayload( |
Tao Bao | 1ac886e | 2019-06-26 11:58:22 -0700 | [diff] [blame] | 371 | avbtool, |
Tao Bao | e7354ba | 2019-05-09 16:54:15 -0700 | [diff] [blame] | 372 | payload_file, |
| 373 | payload_key, |
| 374 | payload_info['apex.key'], |
| 375 | payload_info['Algorithm'], |
| 376 | payload_info['Salt'], |
Jiyong Park | a1887f3 | 2020-05-19 23:18:03 +0900 | [diff] [blame] | 377 | payload_info['Hash Algorithm'], |
Tao Bao | 448004a | 2019-09-19 07:55:02 -0700 | [diff] [blame] | 378 | no_hashtree, |
Tao Bao | e7354ba | 2019-05-09 16:54:15 -0700 | [diff] [blame] | 379 | signing_args) |
| 380 | |
Tianjie Xu | 88a759d | 2020-01-23 10:47:54 -0800 | [diff] [blame] | 381 | # 2b. Update the embedded payload public key. |
Tianjie | c180a5d | 2020-03-23 18:14:09 -0700 | [diff] [blame] | 382 | payload_public_key = common.ExtractAvbPublicKey(avbtool, payload_key) |
Tao Bao | e7354ba | 2019-05-09 16:54:15 -0700 | [diff] [blame] | 383 | common.ZipDelete(apex_file, APEX_PAYLOAD_IMAGE) |
Baligh Uddin | 1588128 | 2019-08-25 12:01:44 -0700 | [diff] [blame] | 384 | if APEX_PUBKEY in zip_items: |
| 385 | common.ZipDelete(apex_file, APEX_PUBKEY) |
Kelvin Zhang | 928c234 | 2020-09-22 16:15:57 -0400 | [diff] [blame] | 386 | apex_zip = zipfile.ZipFile(apex_file, 'a', allowZip64=True) |
Tao Bao | e7354ba | 2019-05-09 16:54:15 -0700 | [diff] [blame] | 387 | common.ZipWrite(apex_zip, payload_file, arcname=APEX_PAYLOAD_IMAGE) |
| 388 | common.ZipWrite(apex_zip, payload_public_key, arcname=APEX_PUBKEY) |
Kelvin Zhang | f92f7f0 | 2023-04-14 21:32:54 +0000 | [diff] [blame] | 389 | common.ZipClose(apex_zip) |
Tao Bao | e7354ba | 2019-05-09 16:54:15 -0700 | [diff] [blame] | 390 | |
Jooyung Han | f9be5ee | 2021-07-22 18:21:25 +0900 | [diff] [blame] | 391 | # 3. Sign the APEX container with container_key. |
Tao Bao | e7354ba | 2019-05-09 16:54:15 -0700 | [diff] [blame] | 392 | signed_apex = common.MakeTempFile(prefix='apex-container-', suffix='.apex') |
| 393 | |
| 394 | # Specify the 4K alignment when calling SignApk. |
| 395 | extra_signapk_args = OPTIONS.extra_signapk_args[:] |
Jooyung Han | ebe9afe | 2021-07-12 13:23:52 +0900 | [diff] [blame] | 396 | extra_signapk_args.extend(['-a', '4096', '--align-file-size']) |
Tao Bao | e7354ba | 2019-05-09 16:54:15 -0700 | [diff] [blame] | 397 | |
Nikita Ioffe | c3fdfed | 2021-01-11 23:50:31 +0000 | [diff] [blame] | 398 | password = container_pw.get(container_key) if container_pw else None |
Tao Bao | e7354ba | 2019-05-09 16:54:15 -0700 | [diff] [blame] | 399 | common.SignFile( |
Jooyung Han | f9be5ee | 2021-07-22 18:21:25 +0900 | [diff] [blame] | 400 | apex_file, |
Tao Bao | e7354ba | 2019-05-09 16:54:15 -0700 | [diff] [blame] | 401 | signed_apex, |
| 402 | container_key, |
Nikita Ioffe | c3fdfed | 2021-01-11 23:50:31 +0000 | [diff] [blame] | 403 | password, |
Tao Bao | e7354ba | 2019-05-09 16:54:15 -0700 | [diff] [blame] | 404 | codename_to_api_level_map=codename_to_api_level_map, |
| 405 | extra_signapk_args=extra_signapk_args) |
| 406 | |
| 407 | return signed_apex |
Nikita Ioffe | 6068e8d | 2021-01-12 00:03:02 +0000 | [diff] [blame] | 408 | |
| 409 | |
Nikita Ioffe | 3608148 | 2021-01-20 01:32:28 +0000 | [diff] [blame] | 410 | def SignCompressedApex(avbtool, apex_file, payload_key, container_key, |
Kelvin Zhang | 7cab750 | 2021-08-02 19:58:14 -0400 | [diff] [blame] | 411 | container_pw, apk_keys, codename_to_api_level_map, |
Thiébaud Weksteen | 62865ca | 2023-10-18 11:08:47 +1100 | [diff] [blame] | 412 | no_hashtree, signing_args=None, sign_tool=None): |
Nikita Ioffe | 3608148 | 2021-01-20 01:32:28 +0000 | [diff] [blame] | 413 | """Signs the current compressed APEX with the given payload/container keys. |
| 414 | |
| 415 | Args: |
| 416 | apex_file: Raw uncompressed APEX data. |
| 417 | payload_key: The path to payload signing key (w/ extension). |
| 418 | container_key: The path to container signing key (w/o extension). |
| 419 | container_pw: The matching password of the container_key, or None. |
| 420 | apk_keys: A dict that holds the signing keys for apk files. |
| 421 | codename_to_api_level_map: A dict that maps from codename to API level. |
| 422 | no_hashtree: Don't include hashtree in the signed APEX. |
| 423 | signing_args: Additional args to be passed to the payload signer. |
| 424 | |
| 425 | Returns: |
| 426 | The path to the signed APEX file. |
| 427 | """ |
| 428 | debugfs_path = os.path.join(OPTIONS.search_path, 'bin', 'debugfs_static') |
| 429 | |
| 430 | # 1. Decompress original_apex inside compressed apex. |
| 431 | original_apex_file = common.MakeTempFile(prefix='original-apex-', |
| 432 | suffix='.apex') |
| 433 | # Decompression target path should not exist |
| 434 | os.remove(original_apex_file) |
| 435 | common.RunAndCheckOutput(['deapexer', '--debugfs_path', debugfs_path, |
| 436 | 'decompress', '--input', apex_file, |
| 437 | '--output', original_apex_file]) |
| 438 | |
| 439 | # 2. Sign original_apex |
| 440 | signed_original_apex_file = SignUncompressedApex( |
| 441 | avbtool, |
| 442 | original_apex_file, |
| 443 | payload_key, |
| 444 | container_key, |
| 445 | container_pw, |
| 446 | apk_keys, |
| 447 | codename_to_api_level_map, |
| 448 | no_hashtree, |
Jooyung Han | 0f5a41d | 2021-10-27 03:53:21 +0900 | [diff] [blame] | 449 | signing_args, |
Thiébaud Weksteen | 62865ca | 2023-10-18 11:08:47 +1100 | [diff] [blame] | 450 | sign_tool) |
Nikita Ioffe | 3608148 | 2021-01-20 01:32:28 +0000 | [diff] [blame] | 451 | |
| 452 | # 3. Compress signed original apex. |
| 453 | compressed_apex_file = common.MakeTempFile(prefix='apex-container-', |
| 454 | suffix='.capex') |
| 455 | common.RunAndCheckOutput(['apex_compression_tool', |
| 456 | 'compress', |
| 457 | '--apex_compression_tool_path', os.getenv('PATH'), |
| 458 | '--input', signed_original_apex_file, |
| 459 | '--output', compressed_apex_file]) |
| 460 | |
Jooyung Han | f9be5ee | 2021-07-22 18:21:25 +0900 | [diff] [blame] | 461 | # 4. Sign the APEX container with container_key. |
Nikita Ioffe | 3608148 | 2021-01-20 01:32:28 +0000 | [diff] [blame] | 462 | signed_apex = common.MakeTempFile(prefix='apex-container-', suffix='.capex') |
| 463 | |
Nikita Ioffe | 3608148 | 2021-01-20 01:32:28 +0000 | [diff] [blame] | 464 | password = container_pw.get(container_key) if container_pw else None |
| 465 | common.SignFile( |
Jooyung Han | f9be5ee | 2021-07-22 18:21:25 +0900 | [diff] [blame] | 466 | compressed_apex_file, |
Nikita Ioffe | 3608148 | 2021-01-20 01:32:28 +0000 | [diff] [blame] | 467 | signed_apex, |
| 468 | container_key, |
| 469 | password, |
| 470 | codename_to_api_level_map=codename_to_api_level_map, |
Jooyung Han | f9be5ee | 2021-07-22 18:21:25 +0900 | [diff] [blame] | 471 | extra_signapk_args=OPTIONS.extra_signapk_args) |
Nikita Ioffe | 3608148 | 2021-01-20 01:32:28 +0000 | [diff] [blame] | 472 | |
| 473 | return signed_apex |
| 474 | |
| 475 | |
Nikita Ioffe | 6068e8d | 2021-01-12 00:03:02 +0000 | [diff] [blame] | 476 | def SignApex(avbtool, apex_data, payload_key, container_key, container_pw, |
Thiébaud Weksteen | 62865ca | 2023-10-18 11:08:47 +1100 | [diff] [blame] | 477 | apk_keys, codename_to_api_level_map, |
| 478 | no_hashtree, signing_args=None, sign_tool=None): |
Nikita Ioffe | 6068e8d | 2021-01-12 00:03:02 +0000 | [diff] [blame] | 479 | """Signs the current APEX with the given payload/container keys. |
| 480 | |
| 481 | Args: |
| 482 | apex_file: Path to apex file path. |
| 483 | payload_key: The path to payload signing key (w/ extension). |
| 484 | container_key: The path to container signing key (w/o extension). |
| 485 | container_pw: The matching password of the container_key, or None. |
| 486 | apk_keys: A dict that holds the signing keys for apk files. |
| 487 | codename_to_api_level_map: A dict that maps from codename to API level. |
| 488 | no_hashtree: Don't include hashtree in the signed APEX. |
| 489 | signing_args: Additional args to be passed to the payload signer. |
| 490 | |
| 491 | Returns: |
| 492 | The path to the signed APEX file. |
| 493 | """ |
| 494 | apex_file = common.MakeTempFile(prefix='apex-container-', suffix='.apex') |
| 495 | with open(apex_file, 'wb') as output_fp: |
| 496 | output_fp.write(apex_data) |
| 497 | |
Nikita Ioffe | 3608148 | 2021-01-20 01:32:28 +0000 | [diff] [blame] | 498 | debugfs_path = os.path.join(OPTIONS.search_path, 'bin', 'debugfs_static') |
Nikita Ioffe | 6068e8d | 2021-01-12 00:03:02 +0000 | [diff] [blame] | 499 | cmd = ['deapexer', '--debugfs_path', debugfs_path, |
| 500 | 'info', '--print-type', apex_file] |
| 501 | |
| 502 | try: |
| 503 | apex_type = common.RunAndCheckOutput(cmd).strip() |
| 504 | if apex_type == 'UNCOMPRESSED': |
| 505 | return SignUncompressedApex( |
| 506 | avbtool, |
Nikita Ioffe | 3608148 | 2021-01-20 01:32:28 +0000 | [diff] [blame] | 507 | apex_file, |
| 508 | payload_key=payload_key, |
| 509 | container_key=container_key, |
Kelvin Zhang | 137807d | 2022-09-14 15:02:46 -0700 | [diff] [blame] | 510 | container_pw=container_pw, |
Nikita Ioffe | 3608148 | 2021-01-20 01:32:28 +0000 | [diff] [blame] | 511 | codename_to_api_level_map=codename_to_api_level_map, |
| 512 | no_hashtree=no_hashtree, |
| 513 | apk_keys=apk_keys, |
Jooyung Han | 0f5a41d | 2021-10-27 03:53:21 +0900 | [diff] [blame] | 514 | signing_args=signing_args, |
Thiébaud Weksteen | 62865ca | 2023-10-18 11:08:47 +1100 | [diff] [blame] | 515 | sign_tool=sign_tool) |
Nikita Ioffe | 3608148 | 2021-01-20 01:32:28 +0000 | [diff] [blame] | 516 | elif apex_type == 'COMPRESSED': |
| 517 | return SignCompressedApex( |
| 518 | avbtool, |
| 519 | apex_file, |
Nikita Ioffe | 6068e8d | 2021-01-12 00:03:02 +0000 | [diff] [blame] | 520 | payload_key=payload_key, |
| 521 | container_key=container_key, |
Kelvin Zhang | 137807d | 2022-09-14 15:02:46 -0700 | [diff] [blame] | 522 | container_pw=container_pw, |
Nikita Ioffe | 6068e8d | 2021-01-12 00:03:02 +0000 | [diff] [blame] | 523 | codename_to_api_level_map=codename_to_api_level_map, |
| 524 | no_hashtree=no_hashtree, |
| 525 | apk_keys=apk_keys, |
Jooyung Han | 0f5a41d | 2021-10-27 03:53:21 +0900 | [diff] [blame] | 526 | signing_args=signing_args, |
Thiébaud Weksteen | 62865ca | 2023-10-18 11:08:47 +1100 | [diff] [blame] | 527 | sign_tool=sign_tool) |
Nikita Ioffe | 6068e8d | 2021-01-12 00:03:02 +0000 | [diff] [blame] | 528 | else: |
| 529 | # TODO(b/172912232): support signing compressed apex |
| 530 | raise ApexInfoError('Unsupported apex type {}'.format(apex_type)) |
| 531 | |
| 532 | except common.ExternalError as e: |
| 533 | raise ApexInfoError( |
Mohammad Samiul Islam | 9fd5886 | 2021-01-06 13:33:25 +0000 | [diff] [blame] | 534 | 'Failed to get type for {}:\n{}'.format(apex_file, e)) |
| 535 | |
Kelvin Zhang | 7cab750 | 2021-08-02 19:58:14 -0400 | [diff] [blame] | 536 | |
Jooyung Han | 750aad5 | 2024-01-19 08:35:21 +0900 | [diff] [blame] | 537 | def GetApexInfoFromTargetFiles(input_file): |
Mohammad Samiul Islam | 9fd5886 | 2021-01-06 13:33:25 +0000 | [diff] [blame] | 538 | """ |
Jooyung Han | 750aad5 | 2024-01-19 08:35:21 +0900 | [diff] [blame] | 539 | Get information about APEXes stored in the input_file zip |
Mohammad Samiul Islam | 9fd5886 | 2021-01-06 13:33:25 +0000 | [diff] [blame] | 540 | |
| 541 | Args: |
| 542 | input_file: The filename of the target build target-files zip or directory. |
| 543 | |
| 544 | Return: |
| 545 | A list of ota_metadata_pb2.ApexInfo() populated using the APEX stored in |
Jooyung Han | 750aad5 | 2024-01-19 08:35:21 +0900 | [diff] [blame] | 546 | each partition of the input_file |
Mohammad Samiul Islam | 9fd5886 | 2021-01-06 13:33:25 +0000 | [diff] [blame] | 547 | """ |
| 548 | |
| 549 | # Extract the apex files so that we can run checks on them |
| 550 | if not isinstance(input_file, str): |
| 551 | raise RuntimeError("must pass filepath to target-files zip or directory") |
Jooyung Han | 750aad5 | 2024-01-19 08:35:21 +0900 | [diff] [blame] | 552 | apex_infos = [] |
Jooyung Han | 1e45baf | 2024-08-15 07:38:17 +0000 | [diff] [blame] | 553 | for partition in PARTITIONS: |
Jooyung Han | 750aad5 | 2024-01-19 08:35:21 +0900 | [diff] [blame] | 554 | apex_infos.extend(GetApexInfoForPartition(input_file, partition)) |
| 555 | return apex_infos |
Mohammad Samiul Islam | 9fd5886 | 2021-01-06 13:33:25 +0000 | [diff] [blame] | 556 | |
Jooyung Han | 750aad5 | 2024-01-19 08:35:21 +0900 | [diff] [blame] | 557 | |
| 558 | def GetApexInfoForPartition(input_file, partition): |
Daniel Norman | e9af70a | 2021-04-15 16:39:22 -0700 | [diff] [blame] | 559 | apex_subdir = os.path.join(partition.upper(), 'apex') |
Mohammad Samiul Islam | 9fd5886 | 2021-01-06 13:33:25 +0000 | [diff] [blame] | 560 | if os.path.isdir(input_file): |
| 561 | tmp_dir = input_file |
| 562 | else: |
Daniel Norman | e9af70a | 2021-04-15 16:39:22 -0700 | [diff] [blame] | 563 | tmp_dir = UnzipTemp(input_file, [os.path.join(apex_subdir, '*')]) |
| 564 | target_dir = os.path.join(tmp_dir, apex_subdir) |
Mohammad Samiul Islam | 9fd5886 | 2021-01-06 13:33:25 +0000 | [diff] [blame] | 565 | |
Daniel Norman | b4b07ab | 2021-02-17 13:22:21 -0800 | [diff] [blame] | 566 | # Partial target-files packages for vendor-only builds may not contain |
| 567 | # a system apex directory. |
| 568 | if not os.path.exists(target_dir): |
Daniel Norman | e9af70a | 2021-04-15 16:39:22 -0700 | [diff] [blame] | 569 | logger.info('No APEX directory at path: %s', target_dir) |
Daniel Norman | b4b07ab | 2021-02-17 13:22:21 -0800 | [diff] [blame] | 570 | return [] |
| 571 | |
Mohammad Samiul Islam | 9fd5886 | 2021-01-06 13:33:25 +0000 | [diff] [blame] | 572 | apex_infos = [] |
Kelvin Zhang | c72718c | 2021-01-27 14:17:14 -0500 | [diff] [blame] | 573 | |
| 574 | debugfs_path = "debugfs" |
| 575 | if OPTIONS.search_path: |
| 576 | debugfs_path = os.path.join(OPTIONS.search_path, "bin", "debugfs_static") |
Dennis Shen | f58e548 | 2022-10-10 21:19:46 +0000 | [diff] [blame] | 577 | |
Kelvin Zhang | c72718c | 2021-01-27 14:17:14 -0500 | [diff] [blame] | 578 | deapexer = 'deapexer' |
| 579 | if OPTIONS.search_path: |
Kelvin Zhang | 05a3f68 | 2021-01-29 14:38:24 -0500 | [diff] [blame] | 580 | deapexer_path = os.path.join(OPTIONS.search_path, "bin", "deapexer") |
Kelvin Zhang | c72718c | 2021-01-27 14:17:14 -0500 | [diff] [blame] | 581 | if os.path.isfile(deapexer_path): |
| 582 | deapexer = deapexer_path |
Dennis Shen | f58e548 | 2022-10-10 21:19:46 +0000 | [diff] [blame] | 583 | |
Håkan Kvist | 01e3819 | 2023-04-13 21:49:19 +0200 | [diff] [blame] | 584 | for apex_filename in sorted(os.listdir(target_dir)): |
Mohammad Samiul Islam | 9fd5886 | 2021-01-06 13:33:25 +0000 | [diff] [blame] | 585 | apex_filepath = os.path.join(target_dir, apex_filename) |
| 586 | if not os.path.isfile(apex_filepath) or \ |
Kelvin Zhang | 7cab750 | 2021-08-02 19:58:14 -0400 | [diff] [blame] | 587 | not zipfile.is_zipfile(apex_filepath): |
Mohammad Samiul Islam | 9fd5886 | 2021-01-06 13:33:25 +0000 | [diff] [blame] | 588 | logger.info("Skipping %s because it's not a zipfile", apex_filepath) |
| 589 | continue |
| 590 | apex_info = ota_metadata_pb2.ApexInfo() |
| 591 | # Open the apex file to retrieve information |
| 592 | manifest = apex_manifest.fromApex(apex_filepath) |
| 593 | apex_info.package_name = manifest.name |
| 594 | apex_info.version = manifest.version |
| 595 | # Check if the file is compressed or not |
Mohammad Samiul Islam | 9fd5886 | 2021-01-06 13:33:25 +0000 | [diff] [blame] | 596 | apex_type = RunAndCheckOutput([ |
| 597 | deapexer, "--debugfs_path", debugfs_path, |
| 598 | 'info', '--print-type', apex_filepath]).rstrip() |
| 599 | if apex_type == 'COMPRESSED': |
| 600 | apex_info.is_compressed = True |
| 601 | elif apex_type == 'UNCOMPRESSED': |
| 602 | apex_info.is_compressed = False |
| 603 | else: |
| 604 | raise RuntimeError('Not an APEX file: ' + apex_type) |
| 605 | |
| 606 | # Decompress compressed APEX to determine its size |
| 607 | if apex_info.is_compressed: |
| 608 | decompressed_file_path = MakeTempFile(prefix="decompressed-", |
| 609 | suffix=".apex") |
| 610 | # Decompression target path should not exist |
| 611 | os.remove(decompressed_file_path) |
| 612 | RunAndCheckOutput([deapexer, 'decompress', '--input', apex_filepath, |
| 613 | '--output', decompressed_file_path]) |
| 614 | apex_info.decompressed_size = os.path.getsize(decompressed_file_path) |
| 615 | |
Jooyung Han | 750aad5 | 2024-01-19 08:35:21 +0900 | [diff] [blame] | 616 | apex_infos.append(apex_info) |
Mohammad Samiul Islam | 9fd5886 | 2021-01-06 13:33:25 +0000 | [diff] [blame] | 617 | |
| 618 | return apex_infos |