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""" 51from __future__ import print_function 52 53import logging 54import os 55import sys 56 57import build_super_image 58import common 59 60logger = logging.getLogger(__name__) 61 62OPTIONS = common.OPTIONS 63OPTIONS.framework_images = ("system",) 64OPTIONS.product_out_framework = None 65OPTIONS.product_out_vendor = None 66OPTIONS.build_vbmeta = False 67OPTIONS.framework_misc_info_keys = None 68 69 70def CreateImageSymlinks(): 71 for image in OPTIONS.framework_images: 72 image_path = os.path.join(OPTIONS.product_out_framework, "%s.img" % image) 73 symlink_path = os.path.join(OPTIONS.product_out_vendor, "%s.img" % image) 74 if os.path.exists(symlink_path): 75 if os.path.islink(symlink_path): 76 os.remove(symlink_path) 77 else: 78 raise ValueError("Attempting to overwrite built image: %s" % 79 symlink_path) 80 os.symlink(image_path, symlink_path) 81 82 83def BuildSuperEmpty(): 84 framework_dict = common.LoadDictionaryFromFile( 85 os.path.join(OPTIONS.product_out_framework, "misc_info.txt")) 86 vendor_dict = common.LoadDictionaryFromFile( 87 os.path.join(OPTIONS.product_out_vendor, "misc_info.txt")) 88 # Regenerate super_empty.img if both partial builds enable DAP. If only the 89 # the vendor build enables DAP, the vendor build's existing super_empty.img 90 # will be reused. If only the framework build should enable DAP, super_empty 91 # should be included in the --framework_images flag to copy the existing 92 # super_empty.img from the framework build. 93 if (framework_dict.get("use_dynamic_partitions") == "true") and ( 94 vendor_dict.get("use_dynamic_partitions") == "true"): 95 logger.info("Building super_empty.img.") 96 merged_dict = dict(vendor_dict) 97 merged_dict.update( 98 common.MergeDynamicPartitionInfoDicts( 99 framework_dict=framework_dict, vendor_dict=vendor_dict)) 100 output_super_empty_path = os.path.join(OPTIONS.product_out_vendor, 101 "super_empty.img") 102 build_super_image.BuildSuperImage(merged_dict, output_super_empty_path) 103 104 105def BuildVBMeta(): 106 logger.info("Building vbmeta.img.") 107 108 framework_dict = common.LoadDictionaryFromFile( 109 os.path.join(OPTIONS.product_out_framework, "misc_info.txt")) 110 vendor_dict = common.LoadDictionaryFromFile( 111 os.path.join(OPTIONS.product_out_vendor, "misc_info.txt")) 112 merged_dict = dict(vendor_dict) 113 if OPTIONS.framework_misc_info_keys: 114 for key in common.LoadListFromFile(OPTIONS.framework_misc_info_keys): 115 merged_dict[key] = framework_dict[key] 116 117 # Build vbmeta.img using partitions in product_out_vendor. 118 partitions = {} 119 for partition in common.AVB_PARTITIONS: 120 partition_path = os.path.join(OPTIONS.product_out_vendor, 121 "%s.img" % partition) 122 if os.path.exists(partition_path): 123 partitions[partition] = partition_path 124 125 # vbmeta_partitions includes the partitions that should be included into 126 # top-level vbmeta.img, which are the ones that are not included in any 127 # chained VBMeta image plus the chained VBMeta images themselves. 128 vbmeta_partitions = common.AVB_PARTITIONS[:] 129 for partition in common.AVB_VBMETA_PARTITIONS: 130 chained_partitions = merged_dict.get("avb_%s" % partition, "").strip() 131 if chained_partitions: 132 partitions[partition] = os.path.join(OPTIONS.product_out_vendor, 133 "%s.img" % partition) 134 vbmeta_partitions = [ 135 item for item in vbmeta_partitions 136 if item not in chained_partitions.split() 137 ] 138 vbmeta_partitions.append(partition) 139 140 output_vbmeta_path = os.path.join(OPTIONS.product_out_vendor, "vbmeta.img") 141 OPTIONS.info_dict = merged_dict 142 common.BuildVBMeta(output_vbmeta_path, partitions, "vbmeta", 143 vbmeta_partitions) 144 145 146def MergeBuilds(): 147 CreateImageSymlinks() 148 BuildSuperEmpty() 149 if OPTIONS.build_vbmeta: 150 BuildVBMeta() 151 152 153def main(): 154 common.InitLogging() 155 156 def option_handler(o, a): 157 if o == "--framework_images": 158 OPTIONS.framework_images = [i.strip() for i in a.split(",")] 159 elif o == "--product_out_framework": 160 OPTIONS.product_out_framework = a 161 elif o == "--product_out_vendor": 162 OPTIONS.product_out_vendor = a 163 elif o == "--build_vbmeta": 164 OPTIONS.build_vbmeta = True 165 elif o == "--framework_misc_info_keys": 166 OPTIONS.framework_misc_info_keys = a 167 else: 168 return False 169 return True 170 171 args = common.ParseOptions( 172 sys.argv[1:], 173 __doc__, 174 extra_long_opts=[ 175 "framework_images=", 176 "product_out_framework=", 177 "product_out_vendor=", 178 "build_vbmeta", 179 "framework_misc_info_keys=", 180 ], 181 extra_option_handler=option_handler) 182 183 if (args or OPTIONS.product_out_framework is None or 184 OPTIONS.product_out_vendor is None): 185 common.Usage(__doc__) 186 sys.exit(1) 187 188 MergeBuilds() 189 190 191if __name__ == "__main__": 192 main() 193