1#!/usr/bin/env python3 2# 3# Copyright (C) 2023 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 18import argparse 19from pathlib import Path 20import zipfile 21from typing import List 22import common 23import tempfile 24import shutil 25 26PARTITIONS_TO_WIPE = ["/dev/block/by-name/vbmeta", 27 "/dev/block/by-name/vbmeta_a", 28 "/dev/block/by-name/vbmeta_b", 29 "/dev/block/by-name/vbmeta_system_a", 30 "/dev/block/by-name/vbmeta_system_b", 31 "/dev/block/by-name/boot", 32 "/dev/block/by-name/boot_a", 33 "/dev/block/by-name/boot_b", 34 "/dev/block/by-name/vendor_boot", 35 "/dev/block/by-name/vendor_boot_a", 36 "/dev/block/by-name/vendor_boot_b", 37 "/dev/block/by-name/init_boot_a", 38 "/dev/block/by-name/init_boot_b", 39 "/dev/block/by-name/metadata", 40 "/dev/block/by-name/super", 41 "/dev/block/by-name/userdata"] 42 43 44def CreateBrickOta(product_name: str, output_path: Path, extra_wipe_partitions: str, serialno: str): 45 partitions_to_wipe = PARTITIONS_TO_WIPE 46 if extra_wipe_partitions is not None: 47 partitions_to_wipe = PARTITIONS_TO_WIPE + extra_wipe_partitions.split(",") 48 # recovery requiers product name to be a | separated list 49 product_name = product_name.replace(",", "|") 50 with zipfile.ZipFile(output_path, "w") as zfp: 51 zfp.writestr("recovery.wipe", "\n".join(partitions_to_wipe)) 52 zfp.writestr("payload.bin", "") 53 zfp.writestr("META-INF/com/android/metadata", "\n".join( 54 ["ota-type=BRICK", "post-timestamp=9999999999", "pre-device=" + product_name, "serialno=" + serialno])) 55 56 57def main(argv): 58 parser = argparse.ArgumentParser(description='Android Brick OTA generator') 59 parser.add_argument('otafile', metavar='PAYLOAD', type=str, 60 help='The output OTA package file.') 61 parser.add_argument('--product', type=str, 62 help='The product name of the device, for example, bramble, redfin. This can be a comma separated list.', required=True) 63 parser.add_argument('--serialno', type=str, 64 help='The serial number of devices that are allowed to install this OTA package. This can be a comma separated list.') 65 parser.add_argument('--extra_wipe_partitions', type=str, 66 help='Additional partitions on device which should be wiped.') 67 parser.add_argument('-v', action="store_true", 68 help="Enable verbose logging", dest="verbose") 69 parser.add_argument('--package_key', type=str, 70 help='Paths to private key for signing payload') 71 parser.add_argument('--search_path', type=str, 72 help='Search path for framework/signapk.jar') 73 parser.add_argument('--private_key_suffix', type=str, 74 help='Suffix to be appended to package_key path', default=".pk8") 75 args = parser.parse_args(argv[1:]) 76 if args.search_path: 77 common.OPTIONS.search_path = args.search_path 78 if args.verbose: 79 common.OPTIONS.verbose = args.verbose 80 CreateBrickOta(args.product, args.otafile, 81 args.extra_wipe_partitions, args.serialno) 82 if args.package_key: 83 common.OPTIONS.private_key_suffix = args.private_key_suffix 84 with tempfile.NamedTemporaryFile() as tmpfile: 85 common.SignFile(args.otafile, tmpfile.name, 86 args.package_key, None, whole_file=True) 87 shutil.copy(tmpfile.name, args.otafile) 88 89 90if __name__ == "__main__": 91 import sys 92 main(sys.argv) 93