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  ota_metadata = ["ota-type=BRICK", "post-timestamp=9999999999",
49                  "pre-device=" + product_name]
50  if serialno is not None:
51      ota_metadata.append("serialno=" + serialno)
52  # recovery requiers product name to be a | separated list
53  product_name = product_name.replace(",", "|")
54  with zipfile.ZipFile(output_path, "w") as zfp:
55    zfp.writestr("recovery.wipe", "\n".join(partitions_to_wipe))
56    zfp.writestr("payload.bin", "")
57    zfp.writestr("META-INF/com/android/metadata", "\n".join(
58        ota_metadata))
59
60
61def main(argv):
62  parser = argparse.ArgumentParser(description='Android Brick OTA generator')
63  parser.add_argument('otafile', metavar='PAYLOAD', type=str,
64                      help='The output OTA package file.')
65  parser.add_argument('--product', type=str,
66                      help='The product name of the device, for example, bramble, redfin.', required=True)
67  parser.add_argument('--serialno', type=str,
68                      help='The serial number of devices that are allowed to install this OTA package. This can be a | separated list.')
69  parser.add_argument('--extra_wipe_partitions', type=str,
70                      help='Additional partitions on device which should be wiped.')
71  parser.add_argument('-v', action="store_true",
72                      help="Enable verbose logging", dest="verbose")
73  parser.add_argument('--package_key', type=str,
74                      help='Paths to private key for signing payload')
75  parser.add_argument('--search_path', type=str,
76                      help='Search path for framework/signapk.jar')
77  parser.add_argument('--private_key_suffix', type=str,
78                      help='Suffix to be appended to package_key path', default=".pk8")
79  args = parser.parse_args(argv[1:])
80  if args.search_path:
81    common.OPTIONS.search_path = args.search_path
82  if args.verbose:
83    common.OPTIONS.verbose = args.verbose
84  CreateBrickOta(args.product, args.otafile,
85                 args.extra_wipe_partitions, args.serialno)
86  if args.package_key:
87    common.OPTIONS.private_key_suffix = args.private_key_suffix
88    with tempfile.NamedTemporaryFile() as tmpfile:
89      common.SignFile(args.otafile, tmpfile.name,
90                      args.package_key, None, whole_file=True)
91      shutil.copy(tmpfile.name, args.otafile)
92
93
94if __name__ == "__main__":
95  import sys
96  main(sys.argv)
97