xref: /aosp_15_r20/build/make/tools/releasetools/merge/merge_builds.py (revision 9e94795a3d4ef5c1d47486f9a02bb378756cea8a)
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"); you may not
6# use this file except in compliance with the License. You may obtain a copy of
7# 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, WITHOUT
13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14# License for the specific language governing permissions and limitations under
15# the License.
16#
17"""Merges two non-dist partial builds together.
18
19Given two partial builds, a framework build and a vendor build, merge the builds
20together so that the images can be flashed using 'fastboot flashall'.
21
22To support both DAP and non-DAP vendor builds with a single framework partial
23build, the framework partial build should always be built with DAP enabled. The
24vendor partial build determines whether the merged result supports DAP.
25
26This script does not require builds to be built with 'make dist'.
27This script regenerates super_empty.img and vbmeta.img if necessary. Other
28images are assumed to not require regeneration.
29
30Usage: merge_builds.py [args]
31
32  --framework_images comma_separated_image_list
33      Comma-separated list of image names that should come from the framework
34      build.
35
36  --product_out_framework product_out_framework_path
37      Path to out/target/product/<framework build>.
38
39  --product_out_vendor product_out_vendor_path
40      Path to out/target/product/<vendor build>.
41
42  --build_vbmeta
43      If provided, vbmeta.img will be regenerated in out/target/product/<vendor
44      build>.
45
46  --framework_misc_info_keys
47      The optional path to a newline-separated config file containing keys to
48      obtain from the framework instance of misc_info.txt, used for creating
49      vbmeta.img. The remaining keys come from the vendor instance.
50
51  --avb_resolve_rollback_index_location_conflict
52      If provided, resolve the conflict AVB rollback index location when
53      necessary.
54"""
55from __future__ import print_function
56
57import logging
58import os
59import sys
60
61import build_super_image
62import common
63
64logger = logging.getLogger(__name__)
65
66OPTIONS = common.OPTIONS
67OPTIONS.framework_images = ("system",)
68OPTIONS.product_out_framework = None
69OPTIONS.product_out_vendor = None
70OPTIONS.build_vbmeta = False
71OPTIONS.framework_misc_info_keys = None
72OPTIONS.avb_resolve_rollback_index_location_conflict = False
73
74
75def CreateImageSymlinks():
76  for image in OPTIONS.framework_images:
77    image_path = os.path.join(OPTIONS.product_out_framework, "%s.img" % image)
78    symlink_path = os.path.join(OPTIONS.product_out_vendor, "%s.img" % image)
79    if os.path.exists(symlink_path):
80      if os.path.islink(symlink_path):
81        os.remove(symlink_path)
82      else:
83        raise ValueError("Attempting to overwrite built image: %s" %
84                         symlink_path)
85    os.symlink(image_path, symlink_path)
86
87
88def BuildSuperEmpty():
89  framework_dict = common.LoadDictionaryFromFile(
90      os.path.join(OPTIONS.product_out_framework, "misc_info.txt"))
91  vendor_dict = common.LoadDictionaryFromFile(
92      os.path.join(OPTIONS.product_out_vendor, "misc_info.txt"))
93  # Regenerate super_empty.img if both partial builds enable DAP. If only the
94  # the vendor build enables DAP, the vendor build's existing super_empty.img
95  # will be reused. If only the framework build should enable DAP, super_empty
96  # should be included in the --framework_images flag to copy the existing
97  # super_empty.img from the framework build.
98  if (framework_dict.get("use_dynamic_partitions") == "true") and (
99      vendor_dict.get("use_dynamic_partitions") == "true"):
100    logger.info("Building super_empty.img.")
101    merged_dict = dict(vendor_dict)
102    merged_dict.update(
103        common.MergeDynamicPartitionInfoDicts(
104            framework_dict=framework_dict, vendor_dict=vendor_dict))
105    output_super_empty_path = os.path.join(OPTIONS.product_out_vendor,
106                                           "super_empty.img")
107    build_super_image.BuildSuperImage(merged_dict, output_super_empty_path)
108
109
110def BuildVBMeta():
111  logger.info("Building vbmeta.img.")
112
113  framework_dict = common.LoadDictionaryFromFile(
114      os.path.join(OPTIONS.product_out_framework, "misc_info.txt"))
115  vendor_dict = common.LoadDictionaryFromFile(
116      os.path.join(OPTIONS.product_out_vendor, "misc_info.txt"))
117  merged_dict = dict(vendor_dict)
118  if OPTIONS.framework_misc_info_keys:
119    for key in common.LoadListFromFile(OPTIONS.framework_misc_info_keys):
120      merged_dict[key] = framework_dict[key]
121
122  # Build vbmeta.img using partitions in product_out_vendor.
123  partitions = {}
124  for partition in common.AVB_PARTITIONS:
125    partition_path = os.path.join(OPTIONS.product_out_vendor,
126                                  "%s.img" % partition)
127    if os.path.exists(partition_path):
128      partitions[partition] = partition_path
129
130  # vbmeta_partitions includes the partitions that should be included into
131  # top-level vbmeta.img, which are the ones that are not included in any
132  # chained VBMeta image plus the chained VBMeta images themselves.
133  vbmeta_partitions = common.AVB_PARTITIONS[:]
134  for partition in common.AVB_VBMETA_PARTITIONS:
135    chained_partitions = merged_dict.get("avb_%s" % partition, "").strip()
136    if chained_partitions:
137      partitions[partition] = os.path.join(OPTIONS.product_out_vendor,
138                                           "%s.img" % partition)
139      vbmeta_partitions = [
140          item for item in vbmeta_partitions
141          if item not in chained_partitions.split()
142      ]
143      vbmeta_partitions.append(partition)
144
145  output_vbmeta_path = os.path.join(OPTIONS.product_out_vendor, "vbmeta.img")
146  OPTIONS.info_dict = merged_dict
147  common.BuildVBMeta(output_vbmeta_path, partitions, "vbmeta",
148                     vbmeta_partitions,
149                     OPTIONS.avb_resolve_rollback_index_location_conflict)
150
151
152def MergeBuilds():
153  CreateImageSymlinks()
154  BuildSuperEmpty()
155  if OPTIONS.build_vbmeta:
156    BuildVBMeta()
157
158
159def main():
160  common.InitLogging()
161
162  def option_handler(o, a):
163    if o == "--framework_images":
164      OPTIONS.framework_images = [i.strip() for i in a.split(",")]
165    elif o == "--product_out_framework":
166      OPTIONS.product_out_framework = a
167    elif o == "--product_out_vendor":
168      OPTIONS.product_out_vendor = a
169    elif o == "--build_vbmeta":
170      OPTIONS.build_vbmeta = True
171    elif o == "--framework_misc_info_keys":
172      OPTIONS.framework_misc_info_keys = a
173    elif o == "--avb_resolve_rollback_index_location_conflict":
174      OPTIONS.avb_resolve_rollback_index_location_conflict = True
175    else:
176      return False
177    return True
178
179  args = common.ParseOptions(
180      sys.argv[1:],
181      __doc__,
182      extra_long_opts=[
183          "framework_images=",
184          "product_out_framework=",
185          "product_out_vendor=",
186          "build_vbmeta",
187          "framework_misc_info_keys=",
188          "avb_resolve_rollback_index_location_conflict"
189      ],
190      extra_option_handler=option_handler)
191
192  if (args or OPTIONS.product_out_framework is None or
193      OPTIONS.product_out_vendor is None):
194    common.Usage(__doc__)
195    sys.exit(1)
196
197  MergeBuilds()
198
199
200if __name__ == "__main__":
201  main()
202