aboutsummaryrefslogtreecommitdiff
path: root/tools/releasetools/create_brick_ota.py
blob: 9e040a53d4f72b490d5230042351f4e63bb1108c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env python3
#
# Copyright (C) 2023 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import argparse
from pathlib import Path
import zipfile
from typing import List
import common
import tempfile
import shutil

PARTITIONS_TO_WIPE = ["/dev/block/by-name/vbmeta",
                      "/dev/block/by-name/vbmeta_a",
                      "/dev/block/by-name/vbmeta_b",
                      "/dev/block/by-name/vbmeta_system_a",
                      "/dev/block/by-name/vbmeta_system_b",
                      "/dev/block/by-name/boot",
                      "/dev/block/by-name/boot_a",
                      "/dev/block/by-name/boot_b",
                      "/dev/block/by-name/vendor_boot",
                      "/dev/block/by-name/vendor_boot_a",
                      "/dev/block/by-name/vendor_boot_b",
                      "/dev/block/by-name/init_boot_a",
                      "/dev/block/by-name/init_boot_b",
                      "/dev/block/by-name/metadata",
                      "/dev/block/by-name/super",
                      "/dev/block/by-name/userdata"]


def CreateBrickOta(product_name: str, output_path: Path, extra_wipe_partitions: str, serialno: str):
  partitions_to_wipe = PARTITIONS_TO_WIPE
  if extra_wipe_partitions is not None:
    partitions_to_wipe = PARTITIONS_TO_WIPE + extra_wipe_partitions.split(",")
    ota_metadata = ["ota-type=BRICK", "post-timestamp=9999999999",
                    "pre-device=" + product_name]
    if serialno is not None:
        ota_metadata.append("serialno=" + serialno)
  # recovery requiers product name to be a | separated list
  product_name = product_name.replace(",", "|")
  with zipfile.ZipFile(output_path, "w") as zfp:
    zfp.writestr("recovery.wipe", "\n".join(partitions_to_wipe))
    zfp.writestr("payload.bin", "")
    zfp.writestr("META-INF/com/android/metadata", "\n".join(
        ota_metadata))


def main(argv):
  parser = argparse.ArgumentParser(description='Android Brick OTA generator')
  parser.add_argument('otafile', metavar='PAYLOAD', type=str,
                      help='The output OTA package file.')
  parser.add_argument('--product', type=str,
                      help='The product name of the device, for example, bramble, redfin.', required=True)
  parser.add_argument('--serialno', type=str,
                      help='The serial number of devices that are allowed to install this OTA package. This can be a | separated list.')
  parser.add_argument('--extra_wipe_partitions', type=str,
                      help='Additional partitions on device which should be wiped.')
  parser.add_argument('-v', action="store_true",
                      help="Enable verbose logging", dest="verbose")
  parser.add_argument('--package_key', type=str,
                      help='Paths to private key for signing payload')
  parser.add_argument('--search_path', type=str,
                      help='Search path for framework/signapk.jar')
  parser.add_argument('--private_key_suffix', type=str,
                      help='Suffix to be appended to package_key path', default=".pk8")
  args = parser.parse_args(argv[1:])
  if args.search_path:
    common.OPTIONS.search_path = args.search_path
  if args.verbose:
    common.OPTIONS.verbose = args.verbose
  CreateBrickOta(args.product, args.otafile,
                 args.extra_wipe_partitions, args.serialno)
  if args.package_key:
    common.OPTIONS.private_key_suffix = args.private_key_suffix
    with tempfile.NamedTemporaryFile() as tmpfile:
      common.SignFile(args.otafile, tmpfile.name,
                      args.package_key, None, whole_file=True)
      shutil.copy(tmpfile.name, args.otafile)


if __name__ == "__main__":
  import sys
  main(sys.argv)