Jooyung Han | 504105f | 2021-10-26 15:54:50 +0900 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright (C) 2021 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 | """sign_virt_apex is a command line tool for sign the Virt APEX file. |
| 17 | |
Jooyung Han | 98498f4 | 2022-02-07 15:23:08 +0900 | [diff] [blame] | 18 | Typical usage: |
Jooyung Han | 486609f | 2022-04-20 11:38:00 +0900 | [diff] [blame^] | 19 | sign_virt_apex payload_key payload_dir |
| 20 | -v, --verbose |
| 21 | --verify |
| 22 | --avbtool path_to_avbtool |
| 23 | --signing_args args |
Jooyung Han | 504105f | 2021-10-26 15:54:50 +0900 | [diff] [blame] | 24 | |
| 25 | sign_virt_apex uses external tools which are assumed to be available via PATH. |
| 26 | - avbtool (--avbtool can override the tool) |
| 27 | - lpmake, lpunpack, simg2img, img2simg |
| 28 | """ |
| 29 | import argparse |
Jooyung Han | 02dceed | 2021-11-08 17:50:22 +0900 | [diff] [blame] | 30 | import hashlib |
Jooyung Han | 504105f | 2021-10-26 15:54:50 +0900 | [diff] [blame] | 31 | import os |
| 32 | import re |
Jooyung Han | 98498f4 | 2022-02-07 15:23:08 +0900 | [diff] [blame] | 33 | import shlex |
Jooyung Han | 504105f | 2021-10-26 15:54:50 +0900 | [diff] [blame] | 34 | import subprocess |
| 35 | import sys |
| 36 | import tempfile |
Jooyung Han | 486609f | 2022-04-20 11:38:00 +0900 | [diff] [blame^] | 37 | import traceback |
| 38 | from concurrent import futures |
| 39 | |
| 40 | # pylint: disable=line-too-long,consider-using-with |
| 41 | |
| 42 | # Use executor to parallelize the invocation of external tools |
| 43 | # If a task depends on another, pass the future object of the previous task as wait list. |
| 44 | # Every future object created by a task should be consumed with AwaitAll() |
| 45 | # so that exceptions are propagated . |
| 46 | executor = futures.ThreadPoolExecutor() |
| 47 | |
| 48 | # Temporary directory for unpacked super.img. |
| 49 | # We could put its creation/deletion into the task graph as well, but |
| 50 | # having it as a global setup is much simpler. |
| 51 | unpack_dir = tempfile.TemporaryDirectory() |
| 52 | |
| 53 | # tasks created with Async() are kept in a list so that they are awaited |
| 54 | # before exit. |
| 55 | tasks = [] |
| 56 | |
| 57 | # create an async task and return a future value of it. |
| 58 | def Async(fn, *args, wait=None, **kwargs): |
| 59 | |
| 60 | # wrap a function with AwaitAll() |
| 61 | def wrapped(): |
| 62 | AwaitAll(wait) |
| 63 | fn(*args, **kwargs) |
| 64 | |
| 65 | task = executor.submit(wrapped) |
| 66 | tasks.append(task) |
| 67 | return task |
| 68 | |
| 69 | |
| 70 | # waits for task (captured in fs as future values) with future.result() |
| 71 | # so that any exception raised during task can be raised upward. |
| 72 | def AwaitAll(fs): |
| 73 | if fs: |
| 74 | for f in fs: |
| 75 | f.result() |
Jooyung Han | 504105f | 2021-10-26 15:54:50 +0900 | [diff] [blame] | 76 | |
| 77 | |
| 78 | def ParseArgs(argv): |
| 79 | parser = argparse.ArgumentParser(description='Sign the Virt APEX') |
Jooyung Han | 02dceed | 2021-11-08 17:50:22 +0900 | [diff] [blame] | 80 | parser.add_argument('--verify', action='store_true', |
| 81 | help='Verify the Virt APEX') |
Jooyung Han | 504105f | 2021-10-26 15:54:50 +0900 | [diff] [blame] | 82 | parser.add_argument( |
| 83 | '-v', '--verbose', |
| 84 | action='store_true', |
| 85 | help='verbose execution') |
| 86 | parser.add_argument( |
| 87 | '--avbtool', |
| 88 | default='avbtool', |
| 89 | help='Optional flag that specifies the AVB tool to use. Defaults to `avbtool`.') |
| 90 | parser.add_argument( |
Jooyung Han | 98498f4 | 2022-02-07 15:23:08 +0900 | [diff] [blame] | 91 | '--signing_args', |
| 92 | help='the extra signing arguments passed to avbtool.' |
| 93 | ) |
| 94 | parser.add_argument( |
Jooyung Han | 1c3d2fa | 2022-02-24 02:35:59 +0900 | [diff] [blame] | 95 | '--key_override', |
| 96 | metavar="filename=key", |
| 97 | action='append', |
| 98 | help='Overrides a signing key for a file e.g. microdroid_bootloader=mykey (for testing)') |
| 99 | parser.add_argument( |
Jooyung Han | 504105f | 2021-10-26 15:54:50 +0900 | [diff] [blame] | 100 | 'key', |
| 101 | help='path to the private key file.') |
| 102 | parser.add_argument( |
| 103 | 'input_dir', |
| 104 | help='the directory having files to be packaged') |
Jooyung Han | 1c3d2fa | 2022-02-24 02:35:59 +0900 | [diff] [blame] | 105 | args = parser.parse_args(argv) |
| 106 | # preprocess --key_override into a map |
| 107 | args.key_overrides = dict() |
| 108 | if args.key_override: |
| 109 | for pair in args.key_override: |
| 110 | name, key = pair.split('=') |
| 111 | args.key_overrides[name] = key |
| 112 | return args |
Jooyung Han | 504105f | 2021-10-26 15:54:50 +0900 | [diff] [blame] | 113 | |
| 114 | |
Jooyung Han | 486609f | 2022-04-20 11:38:00 +0900 | [diff] [blame^] | 115 | def RunCommand(args, cmd, env=None, expected_return_values=None): |
| 116 | expected_return_values = expected_return_values or {0} |
Jooyung Han | 504105f | 2021-10-26 15:54:50 +0900 | [diff] [blame] | 117 | env = env or {} |
| 118 | env.update(os.environ.copy()) |
| 119 | |
| 120 | # TODO(b/193504286): we need a way to find other tool (cmd[0]) in various contexts |
| 121 | # e.g. sign_apex.py, sign_target_files_apk.py |
| 122 | if cmd[0] == 'avbtool': |
| 123 | cmd[0] = args.avbtool |
| 124 | |
| 125 | if args.verbose: |
| 126 | print('Running: ' + ' '.join(cmd)) |
| 127 | p = subprocess.Popen( |
| 128 | cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=env, universal_newlines=True) |
| 129 | output, _ = p.communicate() |
| 130 | |
| 131 | if args.verbose or p.returncode not in expected_return_values: |
| 132 | print(output.rstrip()) |
| 133 | |
| 134 | assert p.returncode in expected_return_values, ( |
| 135 | '%d Failed to execute: ' + ' '.join(cmd)) % p.returncode |
| 136 | return (output, p.returncode) |
| 137 | |
| 138 | |
| 139 | def ReadBytesSize(value): |
| 140 | return int(value.removesuffix(' bytes')) |
| 141 | |
| 142 | |
Jooyung Han | 832d11d | 2021-11-08 12:51:47 +0900 | [diff] [blame] | 143 | def ExtractAvbPubkey(args, key, output): |
| 144 | RunCommand(args, ['avbtool', 'extract_public_key', |
| 145 | '--key', key, '--output', output]) |
| 146 | |
| 147 | |
| 148 | def AvbInfo(args, image_path): |
Jooyung Han | 504105f | 2021-10-26 15:54:50 +0900 | [diff] [blame] | 149 | """Parses avbtool --info image output |
| 150 | |
| 151 | Args: |
| 152 | args: program arguments. |
| 153 | image_path: The path to the image. |
| 154 | descriptor_name: Descriptor name of interest. |
| 155 | |
| 156 | Returns: |
| 157 | A pair of |
| 158 | - a dict that contains VBMeta info. None if there's no VBMeta info. |
Jooyung Han | 832d11d | 2021-11-08 12:51:47 +0900 | [diff] [blame] | 159 | - a list of descriptors. |
Jooyung Han | 504105f | 2021-10-26 15:54:50 +0900 | [diff] [blame] | 160 | """ |
| 161 | if not os.path.exists(image_path): |
| 162 | raise ValueError('Failed to find image: {}'.format(image_path)) |
| 163 | |
| 164 | output, ret_code = RunCommand( |
| 165 | args, ['avbtool', 'info_image', '--image', image_path], expected_return_values={0, 1}) |
| 166 | if ret_code == 1: |
| 167 | return None, None |
| 168 | |
Jooyung Han | 832d11d | 2021-11-08 12:51:47 +0900 | [diff] [blame] | 169 | info, descriptors = {}, [] |
Jooyung Han | 504105f | 2021-10-26 15:54:50 +0900 | [diff] [blame] | 170 | |
| 171 | # Read `avbtool info_image` output as "key:value" lines |
| 172 | matcher = re.compile(r'^(\s*)([^:]+):\s*(.*)$') |
| 173 | |
| 174 | def IterateLine(output): |
| 175 | for line in output.split('\n'): |
| 176 | line_info = matcher.match(line) |
| 177 | if not line_info: |
| 178 | continue |
| 179 | yield line_info.group(1), line_info.group(2), line_info.group(3) |
| 180 | |
| 181 | gen = IterateLine(output) |
Jooyung Han | 832d11d | 2021-11-08 12:51:47 +0900 | [diff] [blame] | 182 | |
| 183 | def ReadDescriptors(cur_indent, cur_name, cur_value): |
| 184 | descriptor = cur_value if cur_name == 'Prop' else {} |
| 185 | descriptors.append((cur_name, descriptor)) |
| 186 | for indent, key, value in gen: |
| 187 | if indent <= cur_indent: |
| 188 | # read descriptors recursively to pass the read key as descriptor name |
| 189 | ReadDescriptors(indent, key, value) |
| 190 | break |
| 191 | descriptor[key] = value |
| 192 | |
Jooyung Han | 504105f | 2021-10-26 15:54:50 +0900 | [diff] [blame] | 193 | # Read VBMeta info |
| 194 | for _, key, value in gen: |
| 195 | if key == 'Descriptors': |
Jooyung Han | 832d11d | 2021-11-08 12:51:47 +0900 | [diff] [blame] | 196 | ReadDescriptors(*next(gen)) |
Jooyung Han | 504105f | 2021-10-26 15:54:50 +0900 | [diff] [blame] | 197 | break |
| 198 | info[key] = value |
| 199 | |
Jooyung Han | 832d11d | 2021-11-08 12:51:47 +0900 | [diff] [blame] | 200 | return info, descriptors |
Jooyung Han | 504105f | 2021-10-26 15:54:50 +0900 | [diff] [blame] | 201 | |
Jooyung Han | 832d11d | 2021-11-08 12:51:47 +0900 | [diff] [blame] | 202 | |
| 203 | # Look up a list of (key, value) with a key. Returns the value of the first matching pair. |
| 204 | def LookUp(pairs, key): |
| 205 | for k, v in pairs: |
| 206 | if key == k: |
| 207 | return v |
| 208 | return None |
Jooyung Han | 504105f | 2021-10-26 15:54:50 +0900 | [diff] [blame] | 209 | |
| 210 | |
| 211 | def AddHashFooter(args, key, image_path): |
Jooyung Han | 1c3d2fa | 2022-02-24 02:35:59 +0900 | [diff] [blame] | 212 | if os.path.basename(image_path) in args.key_overrides: |
| 213 | key = args.key_overrides[os.path.basename(image_path)] |
Jooyung Han | 832d11d | 2021-11-08 12:51:47 +0900 | [diff] [blame] | 214 | info, descriptors = AvbInfo(args, image_path) |
Jooyung Han | 504105f | 2021-10-26 15:54:50 +0900 | [diff] [blame] | 215 | if info: |
Jooyung Han | 832d11d | 2021-11-08 12:51:47 +0900 | [diff] [blame] | 216 | descriptor = LookUp(descriptors, 'Hash descriptor') |
Jooyung Han | 504105f | 2021-10-26 15:54:50 +0900 | [diff] [blame] | 217 | image_size = ReadBytesSize(info['Image size']) |
| 218 | algorithm = info['Algorithm'] |
| 219 | partition_name = descriptor['Partition Name'] |
| 220 | partition_size = str(image_size) |
| 221 | |
| 222 | cmd = ['avbtool', 'add_hash_footer', |
| 223 | '--key', key, |
| 224 | '--algorithm', algorithm, |
| 225 | '--partition_name', partition_name, |
| 226 | '--partition_size', partition_size, |
| 227 | '--image', image_path] |
Jooyung Han | 98498f4 | 2022-02-07 15:23:08 +0900 | [diff] [blame] | 228 | if args.signing_args: |
| 229 | cmd.extend(shlex.split(args.signing_args)) |
Jooyung Han | 504105f | 2021-10-26 15:54:50 +0900 | [diff] [blame] | 230 | RunCommand(args, cmd) |
| 231 | |
| 232 | |
| 233 | def AddHashTreeFooter(args, key, image_path): |
Jooyung Han | 1c3d2fa | 2022-02-24 02:35:59 +0900 | [diff] [blame] | 234 | if os.path.basename(image_path) in args.key_overrides: |
| 235 | key = args.key_overrides[os.path.basename(image_path)] |
Jooyung Han | 832d11d | 2021-11-08 12:51:47 +0900 | [diff] [blame] | 236 | info, descriptors = AvbInfo(args, image_path) |
Jooyung Han | 504105f | 2021-10-26 15:54:50 +0900 | [diff] [blame] | 237 | if info: |
Jooyung Han | 832d11d | 2021-11-08 12:51:47 +0900 | [diff] [blame] | 238 | descriptor = LookUp(descriptors, 'Hashtree descriptor') |
Jooyung Han | 504105f | 2021-10-26 15:54:50 +0900 | [diff] [blame] | 239 | image_size = ReadBytesSize(info['Image size']) |
| 240 | algorithm = info['Algorithm'] |
| 241 | partition_name = descriptor['Partition Name'] |
| 242 | partition_size = str(image_size) |
| 243 | |
| 244 | cmd = ['avbtool', 'add_hashtree_footer', |
| 245 | '--key', key, |
| 246 | '--algorithm', algorithm, |
| 247 | '--partition_name', partition_name, |
| 248 | '--partition_size', partition_size, |
| 249 | '--do_not_generate_fec', |
| 250 | '--image', image_path] |
Jooyung Han | 98498f4 | 2022-02-07 15:23:08 +0900 | [diff] [blame] | 251 | if args.signing_args: |
| 252 | cmd.extend(shlex.split(args.signing_args)) |
Jooyung Han | 504105f | 2021-10-26 15:54:50 +0900 | [diff] [blame] | 253 | RunCommand(args, cmd) |
| 254 | |
| 255 | |
Jooyung Han | 832d11d | 2021-11-08 12:51:47 +0900 | [diff] [blame] | 256 | def MakeVbmetaImage(args, key, vbmeta_img, images=None, chained_partitions=None): |
Jooyung Han | 1c3d2fa | 2022-02-24 02:35:59 +0900 | [diff] [blame] | 257 | if os.path.basename(vbmeta_img) in args.key_overrides: |
| 258 | key = args.key_overrides[os.path.basename(vbmeta_img)] |
Jooyung Han | 832d11d | 2021-11-08 12:51:47 +0900 | [diff] [blame] | 259 | info, descriptors = AvbInfo(args, vbmeta_img) |
| 260 | if info is None: |
| 261 | return |
| 262 | |
Jooyung Han | 486609f | 2022-04-20 11:38:00 +0900 | [diff] [blame^] | 263 | with tempfile.TemporaryDirectory() as work_dir: |
Jooyung Han | 504105f | 2021-10-26 15:54:50 +0900 | [diff] [blame] | 264 | algorithm = info['Algorithm'] |
| 265 | rollback_index = info['Rollback Index'] |
| 266 | rollback_index_location = info['Rollback Index Location'] |
| 267 | |
| 268 | cmd = ['avbtool', 'make_vbmeta_image', |
| 269 | '--key', key, |
| 270 | '--algorithm', algorithm, |
| 271 | '--rollback_index', rollback_index, |
| 272 | '--rollback_index_location', rollback_index_location, |
| 273 | '--output', vbmeta_img] |
Jooyung Han | 832d11d | 2021-11-08 12:51:47 +0900 | [diff] [blame] | 274 | if images: |
| 275 | for img in images: |
| 276 | cmd.extend(['--include_descriptors_from_image', img]) |
| 277 | |
| 278 | # replace pubkeys of chained_partitions as well |
| 279 | for name, descriptor in descriptors: |
| 280 | if name == 'Chain Partition descriptor': |
| 281 | part_name = descriptor['Partition Name'] |
| 282 | ril = descriptor['Rollback Index Location'] |
| 283 | part_key = chained_partitions[part_name] |
| 284 | avbpubkey = os.path.join(work_dir, part_name + '.avbpubkey') |
| 285 | ExtractAvbPubkey(args, part_key, avbpubkey) |
| 286 | cmd.extend(['--chain_partition', '%s:%s:%s' % |
| 287 | (part_name, ril, avbpubkey)]) |
| 288 | |
Jooyung Han | 98498f4 | 2022-02-07 15:23:08 +0900 | [diff] [blame] | 289 | if args.signing_args: |
| 290 | cmd.extend(shlex.split(args.signing_args)) |
| 291 | |
Jooyung Han | 504105f | 2021-10-26 15:54:50 +0900 | [diff] [blame] | 292 | RunCommand(args, cmd) |
| 293 | # libavb expects to be able to read the maximum vbmeta size, so we must provide a partition |
| 294 | # which matches this or the read will fail. |
Jooyung Han | dcb0b49 | 2022-02-26 09:04:17 +0900 | [diff] [blame] | 295 | with open(vbmeta_img, 'a') as f: |
| 296 | f.truncate(65536) |
Jooyung Han | 504105f | 2021-10-26 15:54:50 +0900 | [diff] [blame] | 297 | |
| 298 | |
Jooyung Han | 486609f | 2022-04-20 11:38:00 +0900 | [diff] [blame^] | 299 | def UnpackSuperImg(args, super_img, work_dir): |
| 300 | tmp_super_img = os.path.join(work_dir, 'super.img') |
| 301 | RunCommand(args, ['simg2img', super_img, tmp_super_img]) |
| 302 | RunCommand(args, ['lpunpack', tmp_super_img, work_dir]) |
Jooyung Han | 504105f | 2021-10-26 15:54:50 +0900 | [diff] [blame] | 303 | |
| 304 | |
| 305 | def MakeSuperImage(args, partitions, output): |
Jooyung Han | 486609f | 2022-04-20 11:38:00 +0900 | [diff] [blame^] | 306 | with tempfile.TemporaryDirectory() as work_dir: |
Jooyung Han | 504105f | 2021-10-26 15:54:50 +0900 | [diff] [blame] | 307 | cmd = ['lpmake', '--device-size=auto', '--metadata-slots=2', # A/B |
| 308 | '--metadata-size=65536', '--sparse', '--output=' + output] |
| 309 | |
| 310 | for part, img in partitions.items(): |
| 311 | tmp_img = os.path.join(work_dir, part) |
| 312 | RunCommand(args, ['img2simg', img, tmp_img]) |
| 313 | |
| 314 | image_arg = '--image=%s=%s' % (part, img) |
| 315 | partition_arg = '--partition=%s:readonly:%d:default' % ( |
| 316 | part, os.path.getsize(img)) |
| 317 | cmd.extend([image_arg, partition_arg]) |
| 318 | |
| 319 | RunCommand(args, cmd) |
| 320 | |
| 321 | |
Jooyung Han | 486609f | 2022-04-20 11:38:00 +0900 | [diff] [blame^] | 322 | def SignSuperImg(args, key, super_img, work_dir): |
| 323 | # unpack super.img |
| 324 | UnpackSuperImg(args, super_img, work_dir) |
| 325 | |
| 326 | system_a_img = os.path.join(work_dir, 'system_a.img') |
| 327 | vendor_a_img = os.path.join(work_dir, 'vendor_a.img') |
| 328 | |
| 329 | # re-sign each partition |
| 330 | system_a_f = Async(AddHashTreeFooter, args, key, system_a_img) |
| 331 | vendor_a_f = Async(AddHashTreeFooter, args, key, vendor_a_img) |
| 332 | |
| 333 | # 3. re-pack super.img |
| 334 | partitions = {"system_a": system_a_img, "vendor_a": vendor_a_img} |
| 335 | Async(MakeSuperImage, args, partitions, super_img, wait=[system_a_f, vendor_a_f]) |
| 336 | |
| 337 | |
Jooyung Han | 31b1c2b | 2021-10-27 03:35:42 +0900 | [diff] [blame] | 338 | def ReplaceBootloaderPubkey(args, key, bootloader, bootloader_pubkey): |
Jooyung Han | 1c3d2fa | 2022-02-24 02:35:59 +0900 | [diff] [blame] | 339 | if os.path.basename(bootloader) in args.key_overrides: |
| 340 | key = args.key_overrides[os.path.basename(bootloader)] |
Jooyung Han | 31b1c2b | 2021-10-27 03:35:42 +0900 | [diff] [blame] | 341 | # read old pubkey before replacement |
| 342 | with open(bootloader_pubkey, 'rb') as f: |
| 343 | old_pubkey = f.read() |
| 344 | |
Jooyung Han | 832d11d | 2021-11-08 12:51:47 +0900 | [diff] [blame] | 345 | # replace bootloader pubkey (overwrite the old one with the new one) |
| 346 | ExtractAvbPubkey(args, key, bootloader_pubkey) |
Jooyung Han | 31b1c2b | 2021-10-27 03:35:42 +0900 | [diff] [blame] | 347 | |
| 348 | # read new pubkey |
| 349 | with open(bootloader_pubkey, 'rb') as f: |
| 350 | new_pubkey = f.read() |
| 351 | |
| 352 | assert len(old_pubkey) == len(new_pubkey) |
| 353 | |
| 354 | # replace pubkey embedded in bootloader |
| 355 | with open(bootloader, 'r+b') as bl_f: |
| 356 | pos = bl_f.read().find(old_pubkey) |
| 357 | assert pos != -1 |
| 358 | bl_f.seek(pos) |
| 359 | bl_f.write(new_pubkey) |
| 360 | |
| 361 | |
Jooyung Han | 486609f | 2022-04-20 11:38:00 +0900 | [diff] [blame^] | 362 | # dict of (key, file) for re-sign/verification. keys are un-versioned for readability. |
| 363 | virt_apex_files = { |
| 364 | 'bootloader.pubkey': 'etc/microdroid_bootloader.avbpubkey', |
| 365 | 'bootloader': 'etc/microdroid_bootloader', |
| 366 | 'boot.img': 'etc/fs/microdroid_boot-5.10.img', |
| 367 | 'vendor_boot.img': 'etc/fs/microdroid_vendor_boot-5.10.img', |
| 368 | 'init_boot.img': 'etc/fs/microdroid_init_boot.img', |
| 369 | 'super.img': 'etc/fs/microdroid_super.img', |
| 370 | 'vbmeta.img': 'etc/fs/microdroid_vbmeta.img', |
| 371 | 'vbmeta_bootconfig.img': 'etc/fs/microdroid_vbmeta_bootconfig.img', |
| 372 | 'bootconfig.normal': 'etc/microdroid_bootconfig.normal', |
| 373 | 'bootconfig.app_debuggable': 'etc/microdroid_bootconfig.app_debuggable', |
| 374 | 'bootconfig.full_debuggable': 'etc/microdroid_bootconfig.full_debuggable', |
| 375 | 'uboot_env.img': 'etc/uboot_env.img' |
| 376 | } |
| 377 | |
| 378 | |
| 379 | def TargetFiles(input_dir): |
| 380 | return {k: os.path.join(input_dir, v) for k, v in virt_apex_files.items()} |
| 381 | |
| 382 | |
Jooyung Han | 504105f | 2021-10-26 15:54:50 +0900 | [diff] [blame] | 383 | def SignVirtApex(args): |
| 384 | key = args.key |
| 385 | input_dir = args.input_dir |
Jooyung Han | 486609f | 2022-04-20 11:38:00 +0900 | [diff] [blame^] | 386 | files = TargetFiles(input_dir) |
Jooyung Han | 504105f | 2021-10-26 15:54:50 +0900 | [diff] [blame] | 387 | |
Jooyung Han | 486609f | 2022-04-20 11:38:00 +0900 | [diff] [blame^] | 388 | # unpacked files (will be unpacked from super.img below) |
| 389 | system_a_img = os.path.join(unpack_dir.name, 'system_a.img') |
| 390 | vendor_a_img = os.path.join(unpack_dir.name, 'vendor_a.img') |
Jooyung Han | 504105f | 2021-10-26 15:54:50 +0900 | [diff] [blame] | 391 | |
Jooyung Han | 486609f | 2022-04-20 11:38:00 +0900 | [diff] [blame^] | 392 | # Key(pubkey) embedded in bootloader should match with the one used to make VBmeta below |
Jooyung Han | 31b1c2b | 2021-10-27 03:35:42 +0900 | [diff] [blame] | 393 | # while it's okay to use different keys for other image files. |
Jooyung Han | 486609f | 2022-04-20 11:38:00 +0900 | [diff] [blame^] | 394 | replace_f = Async(ReplaceBootloaderPubkey, args, |
| 395 | key, files['bootloader'], files['bootloader.pubkey']) |
Jooyung Han | 31b1c2b | 2021-10-27 03:35:42 +0900 | [diff] [blame] | 396 | |
Devin Moore | dc9158e | 2022-01-10 18:51:12 +0000 | [diff] [blame] | 397 | # re-sign bootloader, boot.img, vendor_boot.img, and init_boot.img |
Jooyung Han | 486609f | 2022-04-20 11:38:00 +0900 | [diff] [blame^] | 398 | Async(AddHashFooter, args, key, files['bootloader'], wait=[replace_f]) |
| 399 | boot_img_f = Async(AddHashFooter, args, key, files['boot.img']) |
| 400 | vendor_boot_img_f = Async(AddHashFooter, args, key, files['vendor_boot.img']) |
| 401 | init_boot_img_f = Async(AddHashFooter, args, key, files['init_boot.img']) |
Jooyung Han | 504105f | 2021-10-26 15:54:50 +0900 | [diff] [blame] | 402 | |
| 403 | # re-sign super.img |
Jooyung Han | 486609f | 2022-04-20 11:38:00 +0900 | [diff] [blame^] | 404 | super_img_f = Async(SignSuperImg, args, key, files['super.img'], unpack_dir.name) |
Jooyung Han | 504105f | 2021-10-26 15:54:50 +0900 | [diff] [blame] | 405 | |
Jooyung Han | 486609f | 2022-04-20 11:38:00 +0900 | [diff] [blame^] | 406 | # re-generate vbmeta from re-signed {boot, vendor_boot, init_boot, system_a, vendor_a}.img |
| 407 | Async(MakeVbmetaImage, args, key, files['vbmeta.img'], |
| 408 | images=[files['boot.img'], files['vendor_boot.img'], |
| 409 | files['init_boot.img'], system_a_img, vendor_a_img], |
| 410 | wait=[boot_img_f, vendor_boot_img_f, init_boot_img_f, super_img_f]) |
Jooyung Han | 504105f | 2021-10-26 15:54:50 +0900 | [diff] [blame] | 411 | |
Jiyong Park | 34ad918 | 2022-01-28 21:29:48 +0900 | [diff] [blame] | 412 | # Re-sign bootconfigs and the uboot_env with the same key |
Jooyung Han | 832d11d | 2021-11-08 12:51:47 +0900 | [diff] [blame] | 413 | bootconfig_sign_key = key |
Jooyung Han | 486609f | 2022-04-20 11:38:00 +0900 | [diff] [blame^] | 414 | Async(AddHashFooter, args, bootconfig_sign_key, files['bootconfig.normal']) |
| 415 | Async(AddHashFooter, args, bootconfig_sign_key, files['bootconfig.app_debuggable']) |
| 416 | Async(AddHashFooter, args, bootconfig_sign_key, files['bootconfig.full_debuggable']) |
| 417 | Async(AddHashFooter, args, bootconfig_sign_key, files['uboot_env.img']) |
Jooyung Han | 832d11d | 2021-11-08 12:51:47 +0900 | [diff] [blame] | 418 | |
Jiyong Park | 34ad918 | 2022-01-28 21:29:48 +0900 | [diff] [blame] | 419 | # Re-sign vbmeta_bootconfig with chained_partitions to "bootconfig" and |
| 420 | # "uboot_env". Note that, for now, `key` and `bootconfig_sign_key` are the |
| 421 | # same, but technically they can be different. Vbmeta records pubkeys which |
| 422 | # signed chained partitions. |
Jooyung Han | 486609f | 2022-04-20 11:38:00 +0900 | [diff] [blame^] | 423 | Async(MakeVbmetaImage, args, key, files['vbmeta_bootconfig.img'], chained_partitions={ |
| 424 | 'bootconfig': bootconfig_sign_key, |
| 425 | 'uboot_env': bootconfig_sign_key, |
Jiyong Park | 34ad918 | 2022-01-28 21:29:48 +0900 | [diff] [blame] | 426 | }) |
Jooyung Han | 832d11d | 2021-11-08 12:51:47 +0900 | [diff] [blame] | 427 | |
Jooyung Han | 504105f | 2021-10-26 15:54:50 +0900 | [diff] [blame] | 428 | |
Jooyung Han | 02dceed | 2021-11-08 17:50:22 +0900 | [diff] [blame] | 429 | def VerifyVirtApex(args): |
Jooyung Han | 486609f | 2022-04-20 11:38:00 +0900 | [diff] [blame^] | 430 | key = args.key |
| 431 | input_dir = args.input_dir |
| 432 | files = TargetFiles(input_dir) |
Jooyung Han | 02dceed | 2021-11-08 17:50:22 +0900 | [diff] [blame] | 433 | |
Jooyung Han | 486609f | 2022-04-20 11:38:00 +0900 | [diff] [blame^] | 434 | # unpacked files |
| 435 | UnpackSuperImg(args, files['super.img'], unpack_dir.name) |
| 436 | system_a_img = os.path.join(unpack_dir.name, 'system_a.img') |
| 437 | vendor_a_img = os.path.join(unpack_dir.name, 'vendor_a.img') |
Jooyung Han | 02dceed | 2021-11-08 17:50:22 +0900 | [diff] [blame] | 438 | |
Jooyung Han | 486609f | 2022-04-20 11:38:00 +0900 | [diff] [blame^] | 439 | # Read pubkey digest from the input key |
| 440 | with tempfile.NamedTemporaryFile() as pubkey_file: |
| 441 | ExtractAvbPubkey(args, key, pubkey_file.name) |
| 442 | with open(pubkey_file.name, 'rb') as f: |
| 443 | pubkey = f.read() |
| 444 | pubkey_digest = hashlib.sha1(pubkey).hexdigest() |
Jooyung Han | 02dceed | 2021-11-08 17:50:22 +0900 | [diff] [blame] | 445 | |
Jooyung Han | 486609f | 2022-04-20 11:38:00 +0900 | [diff] [blame^] | 446 | def contents(file): |
| 447 | with open(file, 'rb') as f: |
| 448 | return f.read() |
Jooyung Han | 02dceed | 2021-11-08 17:50:22 +0900 | [diff] [blame] | 449 | |
Jooyung Han | 486609f | 2022-04-20 11:38:00 +0900 | [diff] [blame^] | 450 | def check_equals_pubkey(file): |
| 451 | assert contents(file) == pubkey, 'pubkey mismatch: %s' % file |
Jooyung Han | 02dceed | 2021-11-08 17:50:22 +0900 | [diff] [blame] | 452 | |
Jooyung Han | 486609f | 2022-04-20 11:38:00 +0900 | [diff] [blame^] | 453 | def check_contains_pubkey(file): |
| 454 | assert contents(file).find(pubkey) != -1, 'pubkey missing: %s' % file |
| 455 | |
| 456 | def check_avb_pubkey(file): |
| 457 | info, _ = AvbInfo(args, file) |
| 458 | assert info is not None, 'no avbinfo: %s' % file |
| 459 | assert info['Public key (sha1)'] == pubkey_digest, 'pubkey mismatch: %s' % file |
| 460 | |
| 461 | for f in files.values(): |
| 462 | if f == files['bootloader.pubkey']: |
| 463 | Async(check_equals_pubkey, f) |
| 464 | elif f == files['bootloader']: |
| 465 | Async(check_contains_pubkey, f) |
| 466 | elif f == files['super.img']: |
| 467 | Async(check_avb_pubkey, system_a_img) |
| 468 | Async(check_avb_pubkey, vendor_a_img) |
| 469 | else: |
| 470 | # Check pubkey for other files using avbtool |
| 471 | Async(check_avb_pubkey, f) |
Jooyung Han | 02dceed | 2021-11-08 17:50:22 +0900 | [diff] [blame] | 472 | |
| 473 | |
Jooyung Han | 504105f | 2021-10-26 15:54:50 +0900 | [diff] [blame] | 474 | def main(argv): |
| 475 | try: |
| 476 | args = ParseArgs(argv) |
Jooyung Han | 02dceed | 2021-11-08 17:50:22 +0900 | [diff] [blame] | 477 | if args.verify: |
| 478 | VerifyVirtApex(args) |
| 479 | else: |
| 480 | SignVirtApex(args) |
Jooyung Han | 486609f | 2022-04-20 11:38:00 +0900 | [diff] [blame^] | 481 | # ensure all tasks are completed without exceptions |
| 482 | AwaitAll(tasks) |
| 483 | except: # pylint: disable=bare-except |
| 484 | traceback.print_exc() |
Jooyung Han | 504105f | 2021-10-26 15:54:50 +0900 | [diff] [blame] | 485 | sys.exit(1) |
| 486 | |
| 487 | |
| 488 | if __name__ == '__main__': |
| 489 | main(sys.argv[1:]) |