Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 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. |
Daniel Norman | 4cc9df6 | 2019-07-18 10:11:07 -0700 | [diff] [blame] | 16 | # |
| 17 | """This script merges two partial target files packages. |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 18 | |
Daniel Norman | dbbf5a3 | 2020-10-22 16:03:32 -0700 | [diff] [blame] | 19 | One input package contains framework files, and the other contains vendor files. |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 20 | |
Daniel Norman | dbbf5a3 | 2020-10-22 16:03:32 -0700 | [diff] [blame] | 21 | This script produces a complete, merged target files package: |
| 22 | - This package can be used to generate a flashable IMG package. |
| 23 | See --output-img. |
| 24 | - This package can be used to generate an OTA package. See --output-ota. |
| 25 | - The merged package is checked for compatibility between the two inputs. |
| 26 | |
| 27 | Usage: merge_target_files [args] |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 28 | |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 29 | --framework-target-files framework-target-files-zip-archive |
| 30 | The input target files package containing framework bits. This is a zip |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 31 | archive. |
| 32 | |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 33 | --framework-item-list framework-item-list-file |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 34 | The optional path to a newline-separated config file that replaces the |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 35 | contents of DEFAULT_FRAMEWORK_ITEM_LIST if provided. |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 36 | |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 37 | --framework-misc-info-keys framework-misc-info-keys-file |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 38 | The optional path to a newline-separated config file that replaces the |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 39 | contents of DEFAULT_FRAMEWORK_MISC_INFO_KEYS if provided. |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 40 | |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 41 | --vendor-target-files vendor-target-files-zip-archive |
| 42 | The input target files package containing vendor bits. This is a zip |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 43 | archive. |
| 44 | |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 45 | --vendor-item-list vendor-item-list-file |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 46 | The optional path to a newline-separated config file that replaces the |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 47 | contents of DEFAULT_VENDOR_ITEM_LIST if provided. |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 48 | |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 49 | --output-target-files output-target-files-package |
Daniel Norman | fdb3881 | 2019-04-15 09:47:24 -0700 | [diff] [blame] | 50 | If provided, the output merged target files package. Also a zip archive. |
| 51 | |
| 52 | --output-dir output-directory |
| 53 | If provided, the destination directory for saving merged files. Requires |
| 54 | the --output-item-list flag. |
| 55 | Can be provided alongside --output-target-files, or by itself. |
| 56 | |
| 57 | --output-item-list output-item-list-file. |
| 58 | The optional path to a newline-separated config file that specifies the |
| 59 | file patterns to copy into the --output-dir. Required if providing |
| 60 | the --output-dir flag. |
Daniel Norman | a4911da | 2019-03-15 14:36:21 -0700 | [diff] [blame] | 61 | |
Daniel Norman | 3b64ce1 | 2019-04-16 16:11:35 -0700 | [diff] [blame] | 62 | --output-ota output-ota-package |
| 63 | The output ota package. This is a zip archive. Use of this flag may |
| 64 | require passing the --path common flag; see common.py. |
| 65 | |
Daniel Norman | 1bd2a1d | 2019-04-18 12:32:18 -0700 | [diff] [blame] | 66 | --output-img output-img-package |
| 67 | The output img package, suitable for use with 'fastboot update'. Use of |
| 68 | this flag may require passing the --path common flag; see common.py. |
| 69 | |
Daniel Norman | f031825 | 2019-04-15 11:34:56 -0700 | [diff] [blame] | 70 | --output-super-empty output-super-empty-image |
| 71 | If provided, creates a super_empty.img file from the merged target |
| 72 | files package and saves it at this path. |
| 73 | |
Daniel Norman | a4911da | 2019-03-15 14:36:21 -0700 | [diff] [blame] | 74 | --rebuild_recovery |
Bill Peckham | e868aec | 2019-09-17 17:06:47 -0700 | [diff] [blame] | 75 | Deprecated; does nothing. |
Bill Peckham | 364c1cc | 2019-03-29 18:27:23 -0700 | [diff] [blame] | 76 | |
Daniel Norman | b0c7591 | 2020-09-24 14:30:21 -0700 | [diff] [blame] | 77 | --allow-duplicate-apkapex-keys |
| 78 | If provided, duplicate APK/APEX keys are ignored and the value from the |
| 79 | framework is used. |
| 80 | |
Daniel Norman | 571e182 | 2021-06-25 17:18:25 -0700 | [diff] [blame] | 81 | --rebuild-sepolicy |
| 82 | If provided, rebuilds odm.img or vendor.img to include merged sepolicy |
| 83 | files. If odm is present then odm is preferred. |
| 84 | |
| 85 | --vendor-otatools otatools.zip |
| 86 | If provided, use this otatools.zip when recompiling the odm or vendor |
| 87 | image to include sepolicy. |
| 88 | |
Bill Peckham | 364c1cc | 2019-03-29 18:27:23 -0700 | [diff] [blame] | 89 | --keep-tmp |
| 90 | Keep tempoary files for debugging purposes. |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 91 | """ |
| 92 | |
| 93 | from __future__ import print_function |
| 94 | |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 95 | import fnmatch |
Daniel Norman | d335156 | 2020-10-29 12:33:11 -0700 | [diff] [blame] | 96 | import json |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 97 | import logging |
| 98 | import os |
Bill Peckham | 19c3feb | 2020-03-20 18:31:43 -0700 | [diff] [blame] | 99 | import re |
Daniel Norman | fdb3881 | 2019-04-15 09:47:24 -0700 | [diff] [blame] | 100 | import shutil |
Bill Peckham | 540d91a | 2019-04-25 14:18:16 -0700 | [diff] [blame] | 101 | import subprocess |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 102 | import sys |
| 103 | import zipfile |
Daniel Norman | 48603ff | 2021-02-22 15:15:24 -0800 | [diff] [blame] | 104 | from xml.etree import ElementTree |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 105 | |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 106 | import add_img_to_target_files |
Daniel Norman | e9af70a | 2021-04-15 16:39:22 -0700 | [diff] [blame] | 107 | import apex_utils |
Daniel Norman | db8cacc | 2021-04-09 15:34:43 -0700 | [diff] [blame] | 108 | import build_image |
Daniel Norman | f031825 | 2019-04-15 11:34:56 -0700 | [diff] [blame] | 109 | import build_super_image |
Yifan Hong | ade0d3f | 2019-08-21 16:37:11 -0700 | [diff] [blame] | 110 | import check_target_files_vintf |
Daniel Norman | f031825 | 2019-04-15 11:34:56 -0700 | [diff] [blame] | 111 | import common |
Daniel Norman | 1bd2a1d | 2019-04-18 12:32:18 -0700 | [diff] [blame] | 112 | import img_from_target_files |
Daniel Norman | b8d52a2 | 2020-10-26 17:55:00 -0700 | [diff] [blame] | 113 | import find_shareduid_violation |
Daniel Norman | 3b64ce1 | 2019-04-16 16:11:35 -0700 | [diff] [blame] | 114 | import ota_from_target_files |
Daniel Norman | db8cacc | 2021-04-09 15:34:43 -0700 | [diff] [blame] | 115 | import sparse_img |
| 116 | import verity_utils |
| 117 | |
| 118 | from common import AddCareMapForAbOta, ExternalError, PARTITIONS_WITH_CARE_MAP |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 119 | |
| 120 | logger = logging.getLogger(__name__) |
Tao Bao | 2ad4b82 | 2019-06-27 16:52:12 -0700 | [diff] [blame] | 121 | |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 122 | OPTIONS = common.OPTIONS |
Bill Peckham | cb84817 | 2020-04-03 12:50:47 -0700 | [diff] [blame] | 123 | # Always turn on verbose logging. |
| 124 | OPTIONS.verbose = True |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 125 | OPTIONS.framework_target_files = None |
| 126 | OPTIONS.framework_item_list = None |
| 127 | OPTIONS.framework_misc_info_keys = None |
| 128 | OPTIONS.vendor_target_files = None |
| 129 | OPTIONS.vendor_item_list = None |
Bill Peckham | f753e15 | 2019-02-19 18:02:46 -0800 | [diff] [blame] | 130 | OPTIONS.output_target_files = None |
Daniel Norman | fdb3881 | 2019-04-15 09:47:24 -0700 | [diff] [blame] | 131 | OPTIONS.output_dir = None |
| 132 | OPTIONS.output_item_list = None |
Daniel Norman | 3b64ce1 | 2019-04-16 16:11:35 -0700 | [diff] [blame] | 133 | OPTIONS.output_ota = None |
Daniel Norman | 1bd2a1d | 2019-04-18 12:32:18 -0700 | [diff] [blame] | 134 | OPTIONS.output_img = None |
Daniel Norman | f031825 | 2019-04-15 11:34:56 -0700 | [diff] [blame] | 135 | OPTIONS.output_super_empty = None |
Bill Peckham | e868aec | 2019-09-17 17:06:47 -0700 | [diff] [blame] | 136 | # TODO(b/132730255): Remove this option. |
Daniel Norman | a4911da | 2019-03-15 14:36:21 -0700 | [diff] [blame] | 137 | OPTIONS.rebuild_recovery = False |
Daniel Norman | b0c7591 | 2020-09-24 14:30:21 -0700 | [diff] [blame] | 138 | # TODO(b/150582573): Remove this option. |
| 139 | OPTIONS.allow_duplicate_apkapex_keys = False |
Daniel Norman | 571e182 | 2021-06-25 17:18:25 -0700 | [diff] [blame] | 140 | OPTIONS.vendor_otatools = None |
| 141 | OPTIONS.rebuild_sepolicy = False |
Bill Peckham | f753e15 | 2019-02-19 18:02:46 -0800 | [diff] [blame] | 142 | OPTIONS.keep_tmp = False |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 143 | |
Bill Peckham | 19c3feb | 2020-03-20 18:31:43 -0700 | [diff] [blame] | 144 | # In an item list (framework or vendor), we may see entries that select whole |
| 145 | # partitions. Such an entry might look like this 'SYSTEM/*' (e.g., for the |
| 146 | # system partition). The following regex matches this and extracts the |
| 147 | # partition name. |
| 148 | |
| 149 | PARTITION_ITEM_PATTERN = re.compile(r'^([A-Z_]+)/\*$') |
| 150 | |
Bill Peckham | 5c7b034 | 2020-04-03 15:36:23 -0700 | [diff] [blame] | 151 | # In apexkeys.txt or apkcerts.txt, we will find partition tags on each entry in |
| 152 | # the file. We use these partition tags to filter the entries in those files |
| 153 | # from the two different target files packages to produce a merged apexkeys.txt |
| 154 | # or apkcerts.txt file. A partition tag (e.g., for the product partition) looks |
| 155 | # like this: 'partition="product"'. We use the group syntax grab the value of |
| 156 | # the tag. We use non-greedy matching in case there are other fields on the |
| 157 | # same line. |
Bill Peckham | 19c3feb | 2020-03-20 18:31:43 -0700 | [diff] [blame] | 158 | |
Bill Peckham | 5c7b034 | 2020-04-03 15:36:23 -0700 | [diff] [blame] | 159 | PARTITION_TAG_PATTERN = re.compile(r'partition="(.*?)"') |
Bill Peckham | 19c3feb | 2020-03-20 18:31:43 -0700 | [diff] [blame] | 160 | |
| 161 | # The sorting algorithm for apexkeys.txt and apkcerts.txt does not include the |
| 162 | # ".apex" or ".apk" suffix, so we use the following pattern to extract a key. |
| 163 | |
| 164 | MODULE_KEY_PATTERN = re.compile(r'name="(.+)\.(apex|apk)"') |
| 165 | |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 166 | # DEFAULT_FRAMEWORK_ITEM_LIST is a list of items to extract from the partial |
| 167 | # framework target files package as is, meaning these items will land in the |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 168 | # output target files package exactly as they appear in the input partial |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 169 | # framework target files package. |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 170 | |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 171 | DEFAULT_FRAMEWORK_ITEM_LIST = ( |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 172 | 'META/apkcerts.txt', |
| 173 | 'META/filesystem_config.txt', |
| 174 | 'META/root_filesystem_config.txt', |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 175 | 'META/update_engine_config.txt', |
| 176 | 'PRODUCT/*', |
| 177 | 'ROOT/*', |
| 178 | 'SYSTEM/*', |
Daniel Norman | edf1247 | 2019-05-22 10:47:08 -0700 | [diff] [blame] | 179 | ) |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 180 | |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 181 | # DEFAULT_FRAMEWORK_MISC_INFO_KEYS is a list of keys to obtain from the |
Daniel Norman | dbbf5a3 | 2020-10-22 16:03:32 -0700 | [diff] [blame] | 182 | # framework instance of META/misc_info.txt. The remaining keys should come |
| 183 | # from the vendor instance. |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 184 | |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 185 | DEFAULT_FRAMEWORK_MISC_INFO_KEYS = ( |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 186 | 'avb_system_hashtree_enable', |
| 187 | 'avb_system_add_hashtree_footer_args', |
| 188 | 'avb_system_key_path', |
| 189 | 'avb_system_algorithm', |
| 190 | 'avb_system_rollback_index_location', |
| 191 | 'avb_product_hashtree_enable', |
| 192 | 'avb_product_add_hashtree_footer_args', |
Justin Yun | 6151e3f | 2019-06-25 15:58:13 +0900 | [diff] [blame] | 193 | 'avb_system_ext_hashtree_enable', |
| 194 | 'avb_system_ext_add_hashtree_footer_args', |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 195 | 'system_root_image', |
| 196 | 'root_dir', |
| 197 | 'ab_update', |
| 198 | 'default_system_dev_certificate', |
| 199 | 'system_size', |
Chris Gross | 203191b | 2020-05-30 02:39:12 +0000 | [diff] [blame] | 200 | 'building_system_image', |
| 201 | 'building_system_ext_image', |
| 202 | 'building_product_image', |
Daniel Norman | edf1247 | 2019-05-22 10:47:08 -0700 | [diff] [blame] | 203 | ) |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 204 | |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 205 | # DEFAULT_VENDOR_ITEM_LIST is a list of items to extract from the partial |
| 206 | # vendor target files package as is, meaning these items will land in the output |
| 207 | # target files package exactly as they appear in the input partial vendor target |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 208 | # files package. |
| 209 | |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 210 | DEFAULT_VENDOR_ITEM_LIST = ( |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 211 | 'META/boot_filesystem_config.txt', |
| 212 | 'META/otakeys.txt', |
| 213 | 'META/releasetools.py', |
| 214 | 'META/vendor_filesystem_config.txt', |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 215 | 'BOOT/*', |
| 216 | 'DATA/*', |
| 217 | 'ODM/*', |
| 218 | 'OTA/android-info.txt', |
| 219 | 'PREBUILT_IMAGES/*', |
| 220 | 'RADIO/*', |
| 221 | 'VENDOR/*', |
Daniel Norman | edf1247 | 2019-05-22 10:47:08 -0700 | [diff] [blame] | 222 | ) |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 223 | |
Daniel Norman | edf1247 | 2019-05-22 10:47:08 -0700 | [diff] [blame] | 224 | # The merge config lists should not attempt to extract items from both |
| 225 | # builds for any of the following partitions. The partitions in |
| 226 | # SINGLE_BUILD_PARTITIONS should come entirely from a single build (either |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 227 | # framework or vendor, but not both). |
Daniel Norman | edf1247 | 2019-05-22 10:47:08 -0700 | [diff] [blame] | 228 | |
| 229 | SINGLE_BUILD_PARTITIONS = ( |
| 230 | 'BOOT/', |
| 231 | 'DATA/', |
| 232 | 'ODM/', |
| 233 | 'PRODUCT/', |
Justin Yun | 6151e3f | 2019-06-25 15:58:13 +0900 | [diff] [blame] | 234 | 'SYSTEM_EXT/', |
Daniel Norman | edf1247 | 2019-05-22 10:47:08 -0700 | [diff] [blame] | 235 | 'RADIO/', |
| 236 | 'RECOVERY/', |
| 237 | 'ROOT/', |
| 238 | 'SYSTEM/', |
| 239 | 'SYSTEM_OTHER/', |
| 240 | 'VENDOR/', |
Yifan Hong | cfb917a | 2020-05-07 14:58:20 -0700 | [diff] [blame] | 241 | 'VENDOR_DLKM/', |
Yifan Hong | f496f1b | 2020-07-15 16:52:59 -0700 | [diff] [blame] | 242 | 'ODM_DLKM/', |
Daniel Norman | edf1247 | 2019-05-22 10:47:08 -0700 | [diff] [blame] | 243 | ) |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 244 | |
| 245 | |
Chris Gross | fabf50a | 2019-05-02 12:42:09 -0700 | [diff] [blame] | 246 | def write_sorted_data(data, path): |
Tao Bao | 2ad4b82 | 2019-06-27 16:52:12 -0700 | [diff] [blame] | 247 | """Writes the sorted contents of either a list or dict to file. |
Chris Gross | fabf50a | 2019-05-02 12:42:09 -0700 | [diff] [blame] | 248 | |
Tao Bao | 2ad4b82 | 2019-06-27 16:52:12 -0700 | [diff] [blame] | 249 | This function sorts the contents of the list or dict and then writes the |
| 250 | resulting sorted contents to a file specified by path. |
Chris Gross | fabf50a | 2019-05-02 12:42:09 -0700 | [diff] [blame] | 251 | |
| 252 | Args: |
| 253 | data: The list or dict to sort and write. |
| 254 | path: Path to the file to write the sorted values to. The file at path will |
| 255 | be overridden if it exists. |
| 256 | """ |
| 257 | with open(path, 'w') as output: |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 258 | for entry in sorted(data): |
Chris Gross | fabf50a | 2019-05-02 12:42:09 -0700 | [diff] [blame] | 259 | out_str = '{}={}\n'.format(entry, data[entry]) if isinstance( |
| 260 | data, dict) else '{}\n'.format(entry) |
| 261 | output.write(out_str) |
| 262 | |
| 263 | |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 264 | def extract_items(target_files, target_files_temp_dir, extract_item_list): |
Tao Bao | 2ad4b82 | 2019-06-27 16:52:12 -0700 | [diff] [blame] | 265 | """Extracts items from target files to temporary directory. |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 266 | |
| 267 | This function extracts from the specified target files zip archive into the |
| 268 | specified temporary directory, the items specified in the extract item list. |
| 269 | |
| 270 | Args: |
| 271 | target_files: The target files zip archive from which to extract items. |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 272 | target_files_temp_dir: The temporary directory where the extracted items |
Daniel Norman | e5b134a | 2019-04-17 14:54:06 -0700 | [diff] [blame] | 273 | will land. |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 274 | extract_item_list: A list of items to extract. |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 275 | """ |
| 276 | |
| 277 | logger.info('extracting from %s', target_files) |
| 278 | |
| 279 | # Filter the extract_item_list to remove any items that do not exist in the |
| 280 | # zip file. Otherwise, the extraction step will fail. |
| 281 | |
Daniel Norman | 4cc9df6 | 2019-07-18 10:11:07 -0700 | [diff] [blame] | 282 | with zipfile.ZipFile(target_files, allowZip64=True) as target_files_zipfile: |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 283 | target_files_namelist = target_files_zipfile.namelist() |
| 284 | |
| 285 | filtered_extract_item_list = [] |
| 286 | for pattern in extract_item_list: |
| 287 | matching_namelist = fnmatch.filter(target_files_namelist, pattern) |
| 288 | if not matching_namelist: |
| 289 | logger.warning('no match for %s', pattern) |
| 290 | else: |
| 291 | filtered_extract_item_list.append(pattern) |
| 292 | |
Bill Peckham | 8ff3fbd | 2019-02-22 10:57:43 -0800 | [diff] [blame] | 293 | # Extract from target_files into target_files_temp_dir the |
| 294 | # filtered_extract_item_list. |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 295 | |
Daniel Norman | e5b134a | 2019-04-17 14:54:06 -0700 | [diff] [blame] | 296 | common.UnzipToDir(target_files, target_files_temp_dir, |
| 297 | filtered_extract_item_list) |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 298 | |
| 299 | |
Daniel Norman | fdb3881 | 2019-04-15 09:47:24 -0700 | [diff] [blame] | 300 | def copy_items(from_dir, to_dir, patterns): |
| 301 | """Similar to extract_items() except uses an input dir instead of zip.""" |
| 302 | file_paths = [] |
| 303 | for dirpath, _, filenames in os.walk(from_dir): |
Daniel Norman | e5b134a | 2019-04-17 14:54:06 -0700 | [diff] [blame] | 304 | file_paths.extend( |
| 305 | os.path.relpath(path=os.path.join(dirpath, filename), start=from_dir) |
| 306 | for filename in filenames) |
Daniel Norman | fdb3881 | 2019-04-15 09:47:24 -0700 | [diff] [blame] | 307 | |
| 308 | filtered_file_paths = set() |
| 309 | for pattern in patterns: |
| 310 | filtered_file_paths.update(fnmatch.filter(file_paths, pattern)) |
| 311 | |
| 312 | for file_path in filtered_file_paths: |
| 313 | original_file_path = os.path.join(from_dir, file_path) |
| 314 | copied_file_path = os.path.join(to_dir, file_path) |
| 315 | copied_file_dir = os.path.dirname(copied_file_path) |
| 316 | if not os.path.exists(copied_file_dir): |
| 317 | os.makedirs(copied_file_dir) |
| 318 | if os.path.islink(original_file_path): |
| 319 | os.symlink(os.readlink(original_file_path), copied_file_path) |
| 320 | else: |
| 321 | shutil.copyfile(original_file_path, copied_file_path) |
| 322 | |
| 323 | |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 324 | def validate_config_lists(framework_item_list, framework_misc_info_keys, |
| 325 | vendor_item_list): |
Daniel Norman | e596452 | 2019-03-19 10:32:03 -0700 | [diff] [blame] | 326 | """Performs validations on the merge config lists. |
| 327 | |
| 328 | Args: |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 329 | framework_item_list: The list of items to extract from the partial framework |
Daniel Norman | e5b134a | 2019-04-17 14:54:06 -0700 | [diff] [blame] | 330 | target files package as is. |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 331 | framework_misc_info_keys: A list of keys to obtain from the framework |
Daniel Norman | dbbf5a3 | 2020-10-22 16:03:32 -0700 | [diff] [blame] | 332 | instance of META/misc_info.txt. The remaining keys should come from the |
| 333 | vendor instance. |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 334 | vendor_item_list: The list of items to extract from the partial vendor |
| 335 | target files package as is. |
Daniel Norman | e596452 | 2019-03-19 10:32:03 -0700 | [diff] [blame] | 336 | |
| 337 | Returns: |
| 338 | False if a validation fails, otherwise true. |
| 339 | """ |
Daniel Norman | edf1247 | 2019-05-22 10:47:08 -0700 | [diff] [blame] | 340 | has_error = False |
| 341 | |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 342 | default_combined_item_set = set(DEFAULT_FRAMEWORK_ITEM_LIST) |
| 343 | default_combined_item_set.update(DEFAULT_VENDOR_ITEM_LIST) |
Daniel Norman | e596452 | 2019-03-19 10:32:03 -0700 | [diff] [blame] | 344 | |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 345 | combined_item_set = set(framework_item_list) |
| 346 | combined_item_set.update(vendor_item_list) |
Daniel Norman | e596452 | 2019-03-19 10:32:03 -0700 | [diff] [blame] | 347 | |
| 348 | # Check that the merge config lists are not missing any item specified |
| 349 | # by the default config lists. |
| 350 | difference = default_combined_item_set.difference(combined_item_set) |
| 351 | if difference: |
Daniel Norman | e5b134a | 2019-04-17 14:54:06 -0700 | [diff] [blame] | 352 | logger.error('Missing merge config items: %s', list(difference)) |
Daniel Norman | e596452 | 2019-03-19 10:32:03 -0700 | [diff] [blame] | 353 | logger.error('Please ensure missing items are in either the ' |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 354 | 'framework-item-list or vendor-item-list files provided to ' |
Daniel Norman | e596452 | 2019-03-19 10:32:03 -0700 | [diff] [blame] | 355 | 'this script.') |
Daniel Norman | edf1247 | 2019-05-22 10:47:08 -0700 | [diff] [blame] | 356 | has_error = True |
| 357 | |
Daniel Norman | dbbf5a3 | 2020-10-22 16:03:32 -0700 | [diff] [blame] | 358 | # Check that partitions only come from one input. |
Daniel Norman | edf1247 | 2019-05-22 10:47:08 -0700 | [diff] [blame] | 359 | for partition in SINGLE_BUILD_PARTITIONS: |
Daniel Norman | dbbf5a3 | 2020-10-22 16:03:32 -0700 | [diff] [blame] | 360 | image_path = 'IMAGES/{}.img'.format(partition.lower().replace('/', '')) |
| 361 | in_framework = ( |
| 362 | any(item.startswith(partition) for item in framework_item_list) or |
| 363 | image_path in framework_item_list) |
| 364 | in_vendor = ( |
| 365 | any(item.startswith(partition) for item in vendor_item_list) or |
| 366 | image_path in vendor_item_list) |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 367 | if in_framework and in_vendor: |
Daniel Norman | edf1247 | 2019-05-22 10:47:08 -0700 | [diff] [blame] | 368 | logger.error( |
Tao Bao | 2ad4b82 | 2019-06-27 16:52:12 -0700 | [diff] [blame] | 369 | 'Cannot extract items from %s for both the framework and vendor' |
Kiyoung Kim | ebe7c9c | 2019-06-25 17:09:55 +0900 | [diff] [blame] | 370 | ' builds. Please ensure only one merge config item list' |
Tao Bao | 2ad4b82 | 2019-06-27 16:52:12 -0700 | [diff] [blame] | 371 | ' includes %s.', partition, partition) |
Daniel Norman | edf1247 | 2019-05-22 10:47:08 -0700 | [diff] [blame] | 372 | has_error = True |
Daniel Norman | e596452 | 2019-03-19 10:32:03 -0700 | [diff] [blame] | 373 | |
Daniel Norman | db8cacc | 2021-04-09 15:34:43 -0700 | [diff] [blame] | 374 | if ('dynamic_partition_list' |
| 375 | in framework_misc_info_keys) or ('super_partition_groups' |
| 376 | in framework_misc_info_keys): |
Daniel Norman | 19b9fe9 | 2019-03-19 14:48:02 -0700 | [diff] [blame] | 377 | logger.error('Dynamic partition misc info keys should come from ' |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 378 | 'the vendor instance of META/misc_info.txt.') |
Daniel Norman | edf1247 | 2019-05-22 10:47:08 -0700 | [diff] [blame] | 379 | has_error = True |
Daniel Norman | 19b9fe9 | 2019-03-19 14:48:02 -0700 | [diff] [blame] | 380 | |
Daniel Norman | edf1247 | 2019-05-22 10:47:08 -0700 | [diff] [blame] | 381 | return not has_error |
Daniel Norman | e596452 | 2019-03-19 10:32:03 -0700 | [diff] [blame] | 382 | |
| 383 | |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 384 | def process_ab_partitions_txt(framework_target_files_temp_dir, |
| 385 | vendor_target_files_temp_dir, |
Daniel Norman | e5b134a | 2019-04-17 14:54:06 -0700 | [diff] [blame] | 386 | output_target_files_temp_dir): |
Tao Bao | 2ad4b82 | 2019-06-27 16:52:12 -0700 | [diff] [blame] | 387 | """Performs special processing for META/ab_partitions.txt. |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 388 | |
Tao Bao | 2ad4b82 | 2019-06-27 16:52:12 -0700 | [diff] [blame] | 389 | This function merges the contents of the META/ab_partitions.txt files from the |
| 390 | framework directory and the vendor directory, placing the merged result in the |
| 391 | output directory. The precondition in that the files are already extracted. |
| 392 | The post condition is that the output META/ab_partitions.txt contains the |
Daniel Norman | dbbf5a3 | 2020-10-22 16:03:32 -0700 | [diff] [blame] | 393 | merged content. The format for each ab_partitions.txt is one partition name |
| 394 | per line. The output file contains the union of the partition names. |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 395 | |
| 396 | Args: |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 397 | framework_target_files_temp_dir: The name of a directory containing the |
| 398 | special items extracted from the framework target files package. |
| 399 | vendor_target_files_temp_dir: The name of a directory containing the special |
| 400 | items extracted from the vendor target files package. |
Daniel Norman | e5b134a | 2019-04-17 14:54:06 -0700 | [diff] [blame] | 401 | output_target_files_temp_dir: The name of a directory that will be used to |
| 402 | create the output target files package after all the special cases are |
| 403 | processed. |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 404 | """ |
| 405 | |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 406 | framework_ab_partitions_txt = os.path.join(framework_target_files_temp_dir, |
| 407 | 'META', 'ab_partitions.txt') |
| 408 | |
| 409 | vendor_ab_partitions_txt = os.path.join(vendor_target_files_temp_dir, 'META', |
Daniel Norman | e5b134a | 2019-04-17 14:54:06 -0700 | [diff] [blame] | 410 | 'ab_partitions.txt') |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 411 | |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 412 | with open(framework_ab_partitions_txt) as f: |
| 413 | framework_ab_partitions = f.read().splitlines() |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 414 | |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 415 | with open(vendor_ab_partitions_txt) as f: |
| 416 | vendor_ab_partitions = f.read().splitlines() |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 417 | |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 418 | output_ab_partitions = set(framework_ab_partitions + vendor_ab_partitions) |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 419 | |
Daniel Norman | e5b134a | 2019-04-17 14:54:06 -0700 | [diff] [blame] | 420 | output_ab_partitions_txt = os.path.join(output_target_files_temp_dir, 'META', |
| 421 | 'ab_partitions.txt') |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 422 | |
Chris Gross | fabf50a | 2019-05-02 12:42:09 -0700 | [diff] [blame] | 423 | write_sorted_data(data=output_ab_partitions, path=output_ab_partitions_txt) |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 424 | |
| 425 | |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 426 | def process_misc_info_txt(framework_target_files_temp_dir, |
| 427 | vendor_target_files_temp_dir, |
| 428 | output_target_files_temp_dir, |
| 429 | framework_misc_info_keys): |
Tao Bao | 2ad4b82 | 2019-06-27 16:52:12 -0700 | [diff] [blame] | 430 | """Performs special processing for META/misc_info.txt. |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 431 | |
| 432 | This function merges the contents of the META/misc_info.txt files from the |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 433 | framework directory and the vendor directory, placing the merged result in the |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 434 | output directory. The precondition in that the files are already extracted. |
| 435 | The post condition is that the output META/misc_info.txt contains the merged |
| 436 | content. |
| 437 | |
| 438 | Args: |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 439 | framework_target_files_temp_dir: The name of a directory containing the |
| 440 | special items extracted from the framework target files package. |
| 441 | vendor_target_files_temp_dir: The name of a directory containing the special |
| 442 | items extracted from the vendor target files package. |
Daniel Norman | e5b134a | 2019-04-17 14:54:06 -0700 | [diff] [blame] | 443 | output_target_files_temp_dir: The name of a directory that will be used to |
| 444 | create the output target files package after all the special cases are |
| 445 | processed. |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 446 | framework_misc_info_keys: A list of keys to obtain from the framework |
Daniel Norman | dbbf5a3 | 2020-10-22 16:03:32 -0700 | [diff] [blame] | 447 | instance of META/misc_info.txt. The remaining keys should come from the |
| 448 | vendor instance. |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 449 | """ |
| 450 | |
Kiyoung Kim | ebe7c9c | 2019-06-25 17:09:55 +0900 | [diff] [blame] | 451 | misc_info_path = ['META', 'misc_info.txt'] |
| 452 | framework_dict = common.LoadDictionaryFromFile( |
| 453 | os.path.join(framework_target_files_temp_dir, *misc_info_path)) |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 454 | |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 455 | # We take most of the misc info from the vendor target files. |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 456 | |
Kiyoung Kim | ebe7c9c | 2019-06-25 17:09:55 +0900 | [diff] [blame] | 457 | merged_dict = common.LoadDictionaryFromFile( |
| 458 | os.path.join(vendor_target_files_temp_dir, *misc_info_path)) |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 459 | |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 460 | # Replace certain values in merged_dict with values from |
| 461 | # framework_dict. |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 462 | |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 463 | for key in framework_misc_info_keys: |
| 464 | merged_dict[key] = framework_dict[key] |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 465 | |
Daniel Norman | 19b9fe9 | 2019-03-19 14:48:02 -0700 | [diff] [blame] | 466 | # Merge misc info keys used for Dynamic Partitions. |
Daniel Norman | db8cacc | 2021-04-09 15:34:43 -0700 | [diff] [blame] | 467 | if (merged_dict.get('use_dynamic_partitions') |
| 468 | == 'true') and (framework_dict.get('use_dynamic_partitions') == 'true'): |
Daniel Norman | bfc51ef | 2019-07-24 14:34:54 -0700 | [diff] [blame] | 469 | merged_dynamic_partitions_dict = common.MergeDynamicPartitionInfoDicts( |
Daniel Norman | 5541714 | 2019-11-25 16:04:36 -0800 | [diff] [blame] | 470 | framework_dict=framework_dict, vendor_dict=merged_dict) |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 471 | merged_dict.update(merged_dynamic_partitions_dict) |
Tao Bao | 48a2feb | 2019-06-28 11:00:05 -0700 | [diff] [blame] | 472 | # Ensure that add_img_to_target_files rebuilds super split images for |
| 473 | # devices that retrofit dynamic partitions. This flag may have been set to |
| 474 | # false in the partial builds to prevent duplicate building of super.img. |
Daniel Norman | 0bf940c | 2019-06-10 12:50:19 -0700 | [diff] [blame] | 475 | merged_dict['build_super_partition'] = 'true' |
Daniel Norman | 19b9fe9 | 2019-03-19 14:48:02 -0700 | [diff] [blame] | 476 | |
Daniel Norman | 38888d3 | 2020-11-19 14:51:15 -0800 | [diff] [blame] | 477 | # If AVB is enabled then ensure that we build vbmeta.img. |
| 478 | # Partial builds with AVB enabled may set PRODUCT_BUILD_VBMETA_IMAGE=false to |
| 479 | # skip building an incomplete vbmeta.img. |
| 480 | if merged_dict.get('avb_enable') == 'true': |
| 481 | merged_dict['avb_building_vbmeta_image'] = 'true' |
| 482 | |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 483 | # Replace <image>_selinux_fc values with framework or vendor file_contexts.bin |
Daniel Norman | 72c626f | 2019-05-13 15:58:14 -0700 | [diff] [blame] | 484 | # depending on which dictionary the key came from. |
| 485 | # Only the file basename is required because all selinux_fc properties are |
| 486 | # replaced with the full path to the file under META/ when misc_info.txt is |
| 487 | # loaded from target files for repacking. See common.py LoadInfoDict(). |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 488 | for key in merged_dict: |
Daniel Norman | 72c626f | 2019-05-13 15:58:14 -0700 | [diff] [blame] | 489 | if key.endswith('_selinux_fc'): |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 490 | merged_dict[key] = 'vendor_file_contexts.bin' |
| 491 | for key in framework_dict: |
Daniel Norman | 72c626f | 2019-05-13 15:58:14 -0700 | [diff] [blame] | 492 | if key.endswith('_selinux_fc'): |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 493 | merged_dict[key] = 'framework_file_contexts.bin' |
Daniel Norman | 72c626f | 2019-05-13 15:58:14 -0700 | [diff] [blame] | 494 | |
Daniel Norman | e5b134a | 2019-04-17 14:54:06 -0700 | [diff] [blame] | 495 | output_misc_info_txt = os.path.join(output_target_files_temp_dir, 'META', |
| 496 | 'misc_info.txt') |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 497 | write_sorted_data(data=merged_dict, path=output_misc_info_txt) |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 498 | |
| 499 | |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 500 | def process_dynamic_partitions_info_txt(framework_target_files_dir, |
| 501 | vendor_target_files_dir, |
Daniel Norman | a61cde0 | 2019-05-03 14:19:13 -0700 | [diff] [blame] | 502 | output_target_files_dir): |
Tao Bao | 2ad4b82 | 2019-06-27 16:52:12 -0700 | [diff] [blame] | 503 | """Performs special processing for META/dynamic_partitions_info.txt. |
Daniel Norman | a61cde0 | 2019-05-03 14:19:13 -0700 | [diff] [blame] | 504 | |
| 505 | This function merges the contents of the META/dynamic_partitions_info.txt |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 506 | files from the framework directory and the vendor directory, placing the |
| 507 | merged result in the output directory. |
Daniel Norman | a61cde0 | 2019-05-03 14:19:13 -0700 | [diff] [blame] | 508 | |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 509 | This function does nothing if META/dynamic_partitions_info.txt from the vendor |
Daniel Norman | a61cde0 | 2019-05-03 14:19:13 -0700 | [diff] [blame] | 510 | directory does not exist. |
| 511 | |
| 512 | Args: |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 513 | framework_target_files_dir: The name of a directory containing the special |
| 514 | items extracted from the framework target files package. |
| 515 | vendor_target_files_dir: The name of a directory containing the special |
| 516 | items extracted from the vendor target files package. |
Daniel Norman | a61cde0 | 2019-05-03 14:19:13 -0700 | [diff] [blame] | 517 | output_target_files_dir: The name of a directory that will be used to create |
| 518 | the output target files package after all the special cases are processed. |
| 519 | """ |
| 520 | |
| 521 | if not os.path.exists( |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 522 | os.path.join(vendor_target_files_dir, 'META', |
Daniel Norman | a61cde0 | 2019-05-03 14:19:13 -0700 | [diff] [blame] | 523 | 'dynamic_partitions_info.txt')): |
| 524 | return |
| 525 | |
Kiyoung Kim | ebe7c9c | 2019-06-25 17:09:55 +0900 | [diff] [blame] | 526 | dynamic_partitions_info_path = ['META', 'dynamic_partitions_info.txt'] |
Daniel Norman | a61cde0 | 2019-05-03 14:19:13 -0700 | [diff] [blame] | 527 | |
Kiyoung Kim | ebe7c9c | 2019-06-25 17:09:55 +0900 | [diff] [blame] | 528 | framework_dynamic_partitions_dict = common.LoadDictionaryFromFile( |
| 529 | os.path.join(framework_target_files_dir, *dynamic_partitions_info_path)) |
| 530 | vendor_dynamic_partitions_dict = common.LoadDictionaryFromFile( |
| 531 | os.path.join(vendor_target_files_dir, *dynamic_partitions_info_path)) |
Daniel Norman | a61cde0 | 2019-05-03 14:19:13 -0700 | [diff] [blame] | 532 | |
Daniel Norman | bfc51ef | 2019-07-24 14:34:54 -0700 | [diff] [blame] | 533 | merged_dynamic_partitions_dict = common.MergeDynamicPartitionInfoDicts( |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 534 | framework_dict=framework_dynamic_partitions_dict, |
Daniel Norman | 5541714 | 2019-11-25 16:04:36 -0800 | [diff] [blame] | 535 | vendor_dict=vendor_dynamic_partitions_dict) |
Daniel Norman | a61cde0 | 2019-05-03 14:19:13 -0700 | [diff] [blame] | 536 | |
| 537 | output_dynamic_partitions_info_txt = os.path.join( |
| 538 | output_target_files_dir, 'META', 'dynamic_partitions_info.txt') |
Chris Gross | fabf50a | 2019-05-02 12:42:09 -0700 | [diff] [blame] | 539 | write_sorted_data( |
| 540 | data=merged_dynamic_partitions_dict, |
| 541 | path=output_dynamic_partitions_info_txt) |
| 542 | |
| 543 | |
Bill Peckham | 19c3feb | 2020-03-20 18:31:43 -0700 | [diff] [blame] | 544 | def item_list_to_partition_set(item_list): |
| 545 | """Converts a target files item list to a partition set. |
| 546 | |
| 547 | The item list contains items that might look like 'SYSTEM/*' or 'VENDOR/*' or |
| 548 | 'OTA/android-info.txt'. Items that end in '/*' are assumed to match entire |
| 549 | directories where 'SYSTEM' or 'VENDOR' is a directory name that identifies the |
| 550 | contents of a partition of the same name. Other items in the list, such as the |
| 551 | 'OTA' example contain metadata. This function iterates such a list, returning |
| 552 | a set that contains the partition entries. |
| 553 | |
| 554 | Args: |
| 555 | item_list: A list of items in a target files package. |
Daniel Norman | b0c7591 | 2020-09-24 14:30:21 -0700 | [diff] [blame] | 556 | |
Bill Peckham | 19c3feb | 2020-03-20 18:31:43 -0700 | [diff] [blame] | 557 | Returns: |
| 558 | A set of partitions extracted from the list of items. |
| 559 | """ |
| 560 | |
| 561 | partition_set = set() |
| 562 | |
| 563 | for item in item_list: |
| 564 | match = PARTITION_ITEM_PATTERN.search(item.strip()) |
| 565 | partition_tag = match.group(1).lower() if match else None |
| 566 | |
| 567 | if partition_tag: |
| 568 | partition_set.add(partition_tag) |
| 569 | |
| 570 | return partition_set |
| 571 | |
| 572 | |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 573 | def process_apex_keys_apk_certs_common(framework_target_files_dir, |
| 574 | vendor_target_files_dir, |
Bill Peckham | 19c3feb | 2020-03-20 18:31:43 -0700 | [diff] [blame] | 575 | output_target_files_dir, |
| 576 | framework_partition_set, |
| 577 | vendor_partition_set, file_name): |
Tao Bao | 2ad4b82 | 2019-06-27 16:52:12 -0700 | [diff] [blame] | 578 | """Performs special processing for META/apexkeys.txt or META/apkcerts.txt. |
Chris Gross | fabf50a | 2019-05-02 12:42:09 -0700 | [diff] [blame] | 579 | |
| 580 | This function merges the contents of the META/apexkeys.txt or |
Tao Bao | 2ad4b82 | 2019-06-27 16:52:12 -0700 | [diff] [blame] | 581 | META/apkcerts.txt files from the framework directory and the vendor directory, |
| 582 | placing the merged result in the output directory. The precondition in that |
| 583 | the files are already extracted. The post condition is that the output |
| 584 | META/apexkeys.txt or META/apkcerts.txt contains the merged content. |
Chris Gross | fabf50a | 2019-05-02 12:42:09 -0700 | [diff] [blame] | 585 | |
| 586 | Args: |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 587 | framework_target_files_dir: The name of a directory containing the special |
| 588 | items extracted from the framework target files package. |
| 589 | vendor_target_files_dir: The name of a directory containing the special |
| 590 | items extracted from the vendor target files package. |
Chris Gross | fabf50a | 2019-05-02 12:42:09 -0700 | [diff] [blame] | 591 | output_target_files_dir: The name of a directory that will be used to create |
| 592 | the output target files package after all the special cases are processed. |
Bill Peckham | 19c3feb | 2020-03-20 18:31:43 -0700 | [diff] [blame] | 593 | framework_partition_set: Partitions that are considered framework |
| 594 | partitions. Used to filter apexkeys.txt and apkcerts.txt. |
| 595 | vendor_partition_set: Partitions that are considered vendor partitions. Used |
| 596 | to filter apexkeys.txt and apkcerts.txt. |
Chris Gross | fabf50a | 2019-05-02 12:42:09 -0700 | [diff] [blame] | 597 | file_name: The name of the file to merge. One of apkcerts.txt or |
| 598 | apexkeys.txt. |
| 599 | """ |
| 600 | |
| 601 | def read_helper(d): |
| 602 | temp = {} |
| 603 | file_path = os.path.join(d, 'META', file_name) |
| 604 | with open(file_path) as f: |
| 605 | for line in f: |
| 606 | if line.strip(): |
Bill Peckham | 19c3feb | 2020-03-20 18:31:43 -0700 | [diff] [blame] | 607 | name = line.split()[0] |
| 608 | match = MODULE_KEY_PATTERN.search(name) |
| 609 | temp[match.group(1)] = line.strip() |
Chris Gross | fabf50a | 2019-05-02 12:42:09 -0700 | [diff] [blame] | 610 | return temp |
| 611 | |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 612 | framework_dict = read_helper(framework_target_files_dir) |
| 613 | vendor_dict = read_helper(vendor_target_files_dir) |
Bill Peckham | 19c3feb | 2020-03-20 18:31:43 -0700 | [diff] [blame] | 614 | merged_dict = {} |
Chris Gross | fabf50a | 2019-05-02 12:42:09 -0700 | [diff] [blame] | 615 | |
Bill Peckham | 19c3feb | 2020-03-20 18:31:43 -0700 | [diff] [blame] | 616 | def filter_into_merged_dict(item_dict, partition_set): |
| 617 | for key, value in item_dict.items(): |
| 618 | match = PARTITION_TAG_PATTERN.search(value) |
| 619 | |
| 620 | if match is None: |
| 621 | raise ValueError('Entry missing partition tag: %s' % value) |
| 622 | |
| 623 | partition_tag = match.group(1) |
| 624 | |
| 625 | if partition_tag in partition_set: |
| 626 | if key in merged_dict: |
Daniel Norman | b0c7591 | 2020-09-24 14:30:21 -0700 | [diff] [blame] | 627 | if OPTIONS.allow_duplicate_apkapex_keys: |
| 628 | # TODO(b/150582573) Always raise on duplicates. |
| 629 | logger.warning('Duplicate key %s' % key) |
| 630 | continue |
| 631 | else: |
| 632 | raise ValueError('Duplicate key %s' % key) |
Bill Peckham | 19c3feb | 2020-03-20 18:31:43 -0700 | [diff] [blame] | 633 | |
| 634 | merged_dict[key] = value |
| 635 | |
| 636 | filter_into_merged_dict(framework_dict, framework_partition_set) |
| 637 | filter_into_merged_dict(vendor_dict, vendor_partition_set) |
Chris Gross | fabf50a | 2019-05-02 12:42:09 -0700 | [diff] [blame] | 638 | |
| 639 | output_file = os.path.join(output_target_files_dir, 'META', file_name) |
| 640 | |
Bill Peckham | 19c3feb | 2020-03-20 18:31:43 -0700 | [diff] [blame] | 641 | # The following code is similar to write_sorted_data, but different enough |
| 642 | # that we couldn't use that function. We need the output to be sorted by the |
| 643 | # basename of the apex/apk (without the ".apex" or ".apk" suffix). This |
| 644 | # allows the sort to be consistent with the framework/vendor input data and |
| 645 | # eases comparison of input data with merged data. |
| 646 | with open(output_file, 'w') as output: |
| 647 | for key in sorted(merged_dict.keys()): |
| 648 | out_str = merged_dict[key] + '\n' |
| 649 | output.write(out_str) |
Daniel Norman | a61cde0 | 2019-05-03 14:19:13 -0700 | [diff] [blame] | 650 | |
| 651 | |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 652 | def copy_file_contexts(framework_target_files_dir, vendor_target_files_dir, |
Daniel Norman | 72c626f | 2019-05-13 15:58:14 -0700 | [diff] [blame] | 653 | output_target_files_dir): |
| 654 | """Creates named copies of each build's file_contexts.bin in output META/.""" |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 655 | framework_fc_path = os.path.join(framework_target_files_dir, 'META', |
| 656 | 'framework_file_contexts.bin') |
| 657 | if not os.path.exists(framework_fc_path): |
| 658 | framework_fc_path = os.path.join(framework_target_files_dir, 'META', |
| 659 | 'file_contexts.bin') |
| 660 | if not os.path.exists(framework_fc_path): |
| 661 | raise ValueError('Missing framework file_contexts.bin.') |
| 662 | shutil.copyfile( |
| 663 | framework_fc_path, |
| 664 | os.path.join(output_target_files_dir, 'META', |
| 665 | 'framework_file_contexts.bin')) |
| 666 | |
| 667 | vendor_fc_path = os.path.join(vendor_target_files_dir, 'META', |
| 668 | 'vendor_file_contexts.bin') |
| 669 | if not os.path.exists(vendor_fc_path): |
| 670 | vendor_fc_path = os.path.join(vendor_target_files_dir, 'META', |
Daniel Norman | edf1247 | 2019-05-22 10:47:08 -0700 | [diff] [blame] | 671 | 'file_contexts.bin') |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 672 | if not os.path.exists(vendor_fc_path): |
| 673 | raise ValueError('Missing vendor file_contexts.bin.') |
Daniel Norman | 72c626f | 2019-05-13 15:58:14 -0700 | [diff] [blame] | 674 | shutil.copyfile( |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 675 | vendor_fc_path, |
| 676 | os.path.join(output_target_files_dir, 'META', 'vendor_file_contexts.bin')) |
Daniel Norman | 72c626f | 2019-05-13 15:58:14 -0700 | [diff] [blame] | 677 | |
| 678 | |
Daniel Norman | 571e182 | 2021-06-25 17:18:25 -0700 | [diff] [blame] | 679 | def compile_split_sepolicy(product_out, partition_map): |
Daniel Norman | 48603ff | 2021-02-22 15:15:24 -0800 | [diff] [blame] | 680 | """Uses secilc to compile a split sepolicy file. |
| 681 | |
| 682 | Depends on various */etc/selinux/* and */etc/vintf/* files within partitions. |
| 683 | |
| 684 | Args: |
| 685 | product_out: PRODUCT_OUT directory, containing partition directories. |
| 686 | partition_map: A map of partition name -> relative path within product_out. |
Daniel Norman | 48603ff | 2021-02-22 15:15:24 -0800 | [diff] [blame] | 687 | |
| 688 | Returns: |
| 689 | A command list that can be executed to create the compiled sepolicy. |
| 690 | """ |
| 691 | |
| 692 | def get_file(partition, path): |
| 693 | if partition not in partition_map: |
| 694 | logger.warning('Cannot load SEPolicy files for missing partition %s', |
| 695 | partition) |
| 696 | return None |
| 697 | return os.path.join(product_out, partition_map[partition], path) |
| 698 | |
| 699 | # Load the kernel sepolicy version from the FCM. This is normally provided |
| 700 | # directly to selinux.cpp as a build flag, but is also available in this file. |
| 701 | fcm_file = get_file('system', 'etc/vintf/compatibility_matrix.device.xml') |
| 702 | if not fcm_file or not os.path.exists(fcm_file): |
| 703 | raise ExternalError('Missing required file for loading sepolicy: %s', fcm) |
| 704 | kernel_sepolicy_version = ElementTree.parse(fcm_file).getroot().find( |
| 705 | 'sepolicy/kernel-sepolicy-version').text |
| 706 | |
| 707 | # Load the vendor's plat sepolicy version. This is the version used for |
| 708 | # locating sepolicy mapping files. |
| 709 | vendor_plat_version_file = get_file('vendor', |
| 710 | 'etc/selinux/plat_sepolicy_vers.txt') |
| 711 | if not vendor_plat_version_file or not os.path.exists( |
Daniel Norman | 2d7989a | 2021-04-05 17:40:47 +0000 | [diff] [blame] | 712 | vendor_plat_version_file): |
Daniel Norman | 48603ff | 2021-02-22 15:15:24 -0800 | [diff] [blame] | 713 | raise ExternalError('Missing required sepolicy file %s', |
| 714 | vendor_plat_version_file) |
| 715 | with open(vendor_plat_version_file) as f: |
| 716 | vendor_plat_version = f.read().strip() |
| 717 | |
| 718 | # Use the same flags and arguments as selinux.cpp OpenSplitPolicy(). |
| 719 | cmd = ['secilc', '-m', '-M', 'true', '-G', '-N'] |
| 720 | cmd.extend(['-c', kernel_sepolicy_version]) |
Daniel Norman | 571e182 | 2021-06-25 17:18:25 -0700 | [diff] [blame] | 721 | cmd.extend(['-o', os.path.join(product_out, 'META/combined_sepolicy')]) |
Daniel Norman | 48603ff | 2021-02-22 15:15:24 -0800 | [diff] [blame] | 722 | cmd.extend(['-f', '/dev/null']) |
| 723 | |
| 724 | required_policy_files = ( |
| 725 | ('system', 'etc/selinux/plat_sepolicy.cil'), |
| 726 | ('system', 'etc/selinux/mapping/%s.cil' % vendor_plat_version), |
| 727 | ('vendor', 'etc/selinux/vendor_sepolicy.cil'), |
| 728 | ('vendor', 'etc/selinux/plat_pub_versioned.cil'), |
| 729 | ) |
| 730 | for policy in (map(lambda partition_and_path: get_file(*partition_and_path), |
| 731 | required_policy_files)): |
| 732 | if not policy or not os.path.exists(policy): |
| 733 | raise ExternalError('Missing required sepolicy file %s', policy) |
| 734 | cmd.append(policy) |
| 735 | |
| 736 | optional_policy_files = ( |
| 737 | ('system', 'etc/selinux/mapping/%s.compat.cil' % vendor_plat_version), |
| 738 | ('system_ext', 'etc/selinux/system_ext_sepolicy.cil'), |
| 739 | ('system_ext', 'etc/selinux/mapping/%s.cil' % vendor_plat_version), |
| 740 | ('product', 'etc/selinux/product_sepolicy.cil'), |
| 741 | ('product', 'etc/selinux/mapping/%s.cil' % vendor_plat_version), |
| 742 | ('odm', 'etc/selinux/odm_sepolicy.cil'), |
| 743 | ) |
| 744 | for policy in (map(lambda partition_and_path: get_file(*partition_and_path), |
| 745 | optional_policy_files)): |
| 746 | if policy and os.path.exists(policy): |
| 747 | cmd.append(policy) |
| 748 | |
| 749 | return cmd |
| 750 | |
| 751 | |
Daniel Norman | e9af70a | 2021-04-15 16:39:22 -0700 | [diff] [blame] | 752 | def validate_merged_apex_info(output_target_files_dir, partitions): |
| 753 | """Validates the APEX files in the merged target files directory. |
| 754 | |
| 755 | Checks the APEX files in all possible preinstalled APEX directories. |
| 756 | Depends on the <partition>/apex/* APEX files within partitions. |
| 757 | |
| 758 | Args: |
Daniel Norman | 571e182 | 2021-06-25 17:18:25 -0700 | [diff] [blame] | 759 | output_target_files_dir: Output directory containing merged partition |
| 760 | directories. |
Daniel Norman | e9af70a | 2021-04-15 16:39:22 -0700 | [diff] [blame] | 761 | partitions: A list of all the partitions in the output directory. |
| 762 | |
| 763 | Raises: |
| 764 | RuntimeError: if apex_utils fails to parse any APEX file. |
| 765 | ExternalError: if the same APEX package is provided by multiple partitions. |
| 766 | """ |
| 767 | apex_packages = set() |
| 768 | |
| 769 | apex_partitions = ('system', 'system_ext', 'product', 'vendor') |
| 770 | for partition in filter(lambda p: p in apex_partitions, partitions): |
| 771 | apex_info = apex_utils.GetApexInfoFromTargetFiles( |
| 772 | output_target_files_dir, partition, compressed_only=False) |
| 773 | partition_apex_packages = set([info.package_name for info in apex_info]) |
| 774 | duplicates = apex_packages.intersection(partition_apex_packages) |
| 775 | if duplicates: |
| 776 | raise ExternalError( |
| 777 | 'Duplicate APEX packages found in multiple partitions: %s' % |
| 778 | ' '.join(duplicates)) |
| 779 | apex_packages.update(partition_apex_packages) |
| 780 | |
| 781 | |
Daniel Norman | db8cacc | 2021-04-09 15:34:43 -0700 | [diff] [blame] | 782 | def generate_care_map(partitions, output_target_files_dir): |
| 783 | """Generates a merged META/care_map.pb file in the output target files dir. |
| 784 | |
| 785 | Depends on the info dict from META/misc_info.txt, as well as built images |
| 786 | within IMAGES/. |
| 787 | |
| 788 | Args: |
| 789 | partitions: A list of partitions to potentially include in the care map. |
| 790 | output_target_files_dir: The name of a directory that will be used to create |
| 791 | the output target files package after all the special cases are processed. |
| 792 | """ |
| 793 | OPTIONS.info_dict = common.LoadInfoDict(output_target_files_dir) |
| 794 | partition_image_map = {} |
| 795 | for partition in partitions: |
| 796 | image_path = os.path.join(output_target_files_dir, 'IMAGES', |
| 797 | '{}.img'.format(partition)) |
| 798 | if os.path.exists(image_path): |
| 799 | partition_image_map[partition] = image_path |
| 800 | # Regenerated images should have their image_size property already set. |
| 801 | image_size_prop = '{}_image_size'.format(partition) |
| 802 | if image_size_prop not in OPTIONS.info_dict: |
| 803 | # Images copied directly from input target files packages will need |
| 804 | # their image sizes calculated. |
| 805 | partition_size = sparse_img.GetImagePartitionSize(image_path) |
| 806 | image_props = build_image.ImagePropFromGlobalDict( |
| 807 | OPTIONS.info_dict, partition) |
| 808 | verity_image_builder = verity_utils.CreateVerityImageBuilder( |
| 809 | image_props) |
| 810 | image_size = verity_image_builder.CalculateMaxImageSize(partition_size) |
| 811 | OPTIONS.info_dict[image_size_prop] = image_size |
| 812 | |
| 813 | AddCareMapForAbOta( |
| 814 | os.path.join(output_target_files_dir, 'META', 'care_map.pb'), |
| 815 | PARTITIONS_WITH_CARE_MAP, partition_image_map) |
| 816 | |
| 817 | |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 818 | def process_special_cases(framework_target_files_temp_dir, |
| 819 | vendor_target_files_temp_dir, |
| 820 | output_target_files_temp_dir, |
Daniel Norman | b0c7591 | 2020-09-24 14:30:21 -0700 | [diff] [blame] | 821 | framework_misc_info_keys, framework_partition_set, |
Bill Peckham | 19c3feb | 2020-03-20 18:31:43 -0700 | [diff] [blame] | 822 | vendor_partition_set): |
Tao Bao | 2ad4b82 | 2019-06-27 16:52:12 -0700 | [diff] [blame] | 823 | """Performs special-case processing for certain target files items. |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 824 | |
| 825 | Certain files in the output target files package require special-case |
| 826 | processing. This function performs all that special-case processing. |
| 827 | |
| 828 | Args: |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 829 | framework_target_files_temp_dir: The name of a directory containing the |
| 830 | special items extracted from the framework target files package. |
| 831 | vendor_target_files_temp_dir: The name of a directory containing the special |
| 832 | items extracted from the vendor target files package. |
Daniel Norman | e5b134a | 2019-04-17 14:54:06 -0700 | [diff] [blame] | 833 | output_target_files_temp_dir: The name of a directory that will be used to |
| 834 | create the output target files package after all the special cases are |
| 835 | processed. |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 836 | framework_misc_info_keys: A list of keys to obtain from the framework |
Daniel Norman | dbbf5a3 | 2020-10-22 16:03:32 -0700 | [diff] [blame] | 837 | instance of META/misc_info.txt. The remaining keys should come from the |
| 838 | vendor instance. |
Bill Peckham | 19c3feb | 2020-03-20 18:31:43 -0700 | [diff] [blame] | 839 | framework_partition_set: Partitions that are considered framework |
| 840 | partitions. Used to filter apexkeys.txt and apkcerts.txt. |
| 841 | vendor_partition_set: Partitions that are considered vendor partitions. Used |
| 842 | to filter apexkeys.txt and apkcerts.txt. |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 843 | """ |
| 844 | |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 845 | if 'ab_update' in framework_misc_info_keys: |
Bill Peckham | 364c1cc | 2019-03-29 18:27:23 -0700 | [diff] [blame] | 846 | process_ab_partitions_txt( |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 847 | framework_target_files_temp_dir=framework_target_files_temp_dir, |
| 848 | vendor_target_files_temp_dir=vendor_target_files_temp_dir, |
Bill Peckham | 364c1cc | 2019-03-29 18:27:23 -0700 | [diff] [blame] | 849 | output_target_files_temp_dir=output_target_files_temp_dir) |
| 850 | |
Daniel Norman | 72c626f | 2019-05-13 15:58:14 -0700 | [diff] [blame] | 851 | copy_file_contexts( |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 852 | framework_target_files_dir=framework_target_files_temp_dir, |
| 853 | vendor_target_files_dir=vendor_target_files_temp_dir, |
Daniel Norman | 72c626f | 2019-05-13 15:58:14 -0700 | [diff] [blame] | 854 | output_target_files_dir=output_target_files_temp_dir) |
| 855 | |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 856 | process_misc_info_txt( |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 857 | framework_target_files_temp_dir=framework_target_files_temp_dir, |
| 858 | vendor_target_files_temp_dir=vendor_target_files_temp_dir, |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 859 | output_target_files_temp_dir=output_target_files_temp_dir, |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 860 | framework_misc_info_keys=framework_misc_info_keys) |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 861 | |
Daniel Norman | a61cde0 | 2019-05-03 14:19:13 -0700 | [diff] [blame] | 862 | process_dynamic_partitions_info_txt( |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 863 | framework_target_files_dir=framework_target_files_temp_dir, |
| 864 | vendor_target_files_dir=vendor_target_files_temp_dir, |
Daniel Norman | 714bd12 | 2019-05-08 16:20:02 -0700 | [diff] [blame] | 865 | output_target_files_dir=output_target_files_temp_dir) |
Daniel Norman | a61cde0 | 2019-05-03 14:19:13 -0700 | [diff] [blame] | 866 | |
Chris Gross | fabf50a | 2019-05-02 12:42:09 -0700 | [diff] [blame] | 867 | process_apex_keys_apk_certs_common( |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 868 | framework_target_files_dir=framework_target_files_temp_dir, |
| 869 | vendor_target_files_dir=vendor_target_files_temp_dir, |
Chris Gross | fabf50a | 2019-05-02 12:42:09 -0700 | [diff] [blame] | 870 | output_target_files_dir=output_target_files_temp_dir, |
Bill Peckham | 19c3feb | 2020-03-20 18:31:43 -0700 | [diff] [blame] | 871 | framework_partition_set=framework_partition_set, |
| 872 | vendor_partition_set=vendor_partition_set, |
Chris Gross | fabf50a | 2019-05-02 12:42:09 -0700 | [diff] [blame] | 873 | file_name='apkcerts.txt') |
| 874 | |
| 875 | process_apex_keys_apk_certs_common( |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 876 | framework_target_files_dir=framework_target_files_temp_dir, |
| 877 | vendor_target_files_dir=vendor_target_files_temp_dir, |
Chris Gross | fabf50a | 2019-05-02 12:42:09 -0700 | [diff] [blame] | 878 | output_target_files_dir=output_target_files_temp_dir, |
Bill Peckham | 19c3feb | 2020-03-20 18:31:43 -0700 | [diff] [blame] | 879 | framework_partition_set=framework_partition_set, |
| 880 | vendor_partition_set=vendor_partition_set, |
Chris Gross | fabf50a | 2019-05-02 12:42:09 -0700 | [diff] [blame] | 881 | file_name='apexkeys.txt') |
| 882 | |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 883 | |
Kiyoung Kim | 7cbeda7 | 2019-06-28 13:26:04 +0900 | [diff] [blame] | 884 | def create_merged_package(temp_dir, framework_target_files, framework_item_list, |
| 885 | vendor_target_files, vendor_item_list, |
Daniel Norman | 4cc9df6 | 2019-07-18 10:11:07 -0700 | [diff] [blame] | 886 | framework_misc_info_keys, rebuild_recovery): |
Tao Bao | 2ad4b82 | 2019-06-27 16:52:12 -0700 | [diff] [blame] | 887 | """Merges two target files packages into one target files structure. |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 888 | |
| 889 | Args: |
| 890 | temp_dir: The name of a directory we use when we extract items from the |
Daniel Norman | e5b134a | 2019-04-17 14:54:06 -0700 | [diff] [blame] | 891 | input target files packages, and also a scratch directory that we use for |
| 892 | temporary files. |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 893 | framework_target_files: The name of the zip archive containing the framework |
Daniel Norman | e5b134a | 2019-04-17 14:54:06 -0700 | [diff] [blame] | 894 | partial target files package. |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 895 | framework_item_list: The list of items to extract from the partial framework |
Daniel Norman | e5b134a | 2019-04-17 14:54:06 -0700 | [diff] [blame] | 896 | target files package as is, meaning these items will land in the output |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 897 | target files package exactly as they appear in the input partial framework |
Daniel Norman | e5b134a | 2019-04-17 14:54:06 -0700 | [diff] [blame] | 898 | target files package. |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 899 | vendor_target_files: The name of the zip archive containing the vendor |
| 900 | partial target files package. |
| 901 | vendor_item_list: The list of items to extract from the partial vendor |
| 902 | target files package as is, meaning these items will land in the output |
| 903 | target files package exactly as they appear in the input partial vendor |
Daniel Norman | e5b134a | 2019-04-17 14:54:06 -0700 | [diff] [blame] | 904 | target files package. |
Daniel Norman | dbbf5a3 | 2020-10-22 16:03:32 -0700 | [diff] [blame] | 905 | framework_misc_info_keys: A list of keys to obtain from the framework |
| 906 | instance of META/misc_info.txt. The remaining keys should come from the |
| 907 | vendor instance. |
Daniel Norman | a4911da | 2019-03-15 14:36:21 -0700 | [diff] [blame] | 908 | rebuild_recovery: If true, rebuild the recovery patch used by non-A/B |
Daniel Norman | e5b134a | 2019-04-17 14:54:06 -0700 | [diff] [blame] | 909 | devices and write it to the system image. |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 910 | |
Kiyoung Kim | 7cbeda7 | 2019-06-28 13:26:04 +0900 | [diff] [blame] | 911 | Returns: |
| 912 | Path to merged package under temp directory. |
| 913 | """ |
Daniel Norman | dbbf5a3 | 2020-10-22 16:03:32 -0700 | [diff] [blame] | 914 | # Extract "as is" items from the input framework and vendor partial target |
| 915 | # files packages directly into the output temporary directory, since these items |
| 916 | # do not need special case processing. |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 917 | |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 918 | output_target_files_temp_dir = os.path.join(temp_dir, 'output') |
Bill Peckham | 889b0c6 | 2019-02-21 18:53:37 -0800 | [diff] [blame] | 919 | extract_items( |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 920 | target_files=framework_target_files, |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 921 | target_files_temp_dir=output_target_files_temp_dir, |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 922 | extract_item_list=framework_item_list) |
Bill Peckham | 889b0c6 | 2019-02-21 18:53:37 -0800 | [diff] [blame] | 923 | extract_items( |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 924 | target_files=vendor_target_files, |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 925 | target_files_temp_dir=output_target_files_temp_dir, |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 926 | extract_item_list=vendor_item_list) |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 927 | |
Daniel Norman | dbbf5a3 | 2020-10-22 16:03:32 -0700 | [diff] [blame] | 928 | # Perform special case processing on META/* items. |
| 929 | # After this function completes successfully, all the files we need to create |
| 930 | # the output target files package are in place. |
| 931 | framework_target_files_temp_dir = os.path.join(temp_dir, 'framework') |
| 932 | vendor_target_files_temp_dir = os.path.join(temp_dir, 'vendor') |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 933 | extract_items( |
| 934 | target_files=framework_target_files, |
| 935 | target_files_temp_dir=framework_target_files_temp_dir, |
Daniel Norman | dbbf5a3 | 2020-10-22 16:03:32 -0700 | [diff] [blame] | 936 | extract_item_list=('META/*',)) |
Bill Peckham | 889b0c6 | 2019-02-21 18:53:37 -0800 | [diff] [blame] | 937 | extract_items( |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 938 | target_files=vendor_target_files, |
| 939 | target_files_temp_dir=vendor_target_files_temp_dir, |
Daniel Norman | dbbf5a3 | 2020-10-22 16:03:32 -0700 | [diff] [blame] | 940 | extract_item_list=('META/*',)) |
Bill Peckham | 889b0c6 | 2019-02-21 18:53:37 -0800 | [diff] [blame] | 941 | process_special_cases( |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 942 | framework_target_files_temp_dir=framework_target_files_temp_dir, |
| 943 | vendor_target_files_temp_dir=vendor_target_files_temp_dir, |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 944 | output_target_files_temp_dir=output_target_files_temp_dir, |
Bill Peckham | 19c3feb | 2020-03-20 18:31:43 -0700 | [diff] [blame] | 945 | framework_misc_info_keys=framework_misc_info_keys, |
| 946 | framework_partition_set=item_list_to_partition_set(framework_item_list), |
| 947 | vendor_partition_set=item_list_to_partition_set(vendor_item_list)) |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 948 | |
Kiyoung Kim | 7cbeda7 | 2019-06-28 13:26:04 +0900 | [diff] [blame] | 949 | return output_target_files_temp_dir |
| 950 | |
| 951 | |
| 952 | def generate_images(target_files_dir, rebuild_recovery): |
| 953 | """Generate images from target files. |
| 954 | |
| 955 | This function takes merged output temporary directory and create images |
| 956 | from it. |
| 957 | |
| 958 | Args: |
| 959 | target_files_dir: Path to merged temp directory. |
| 960 | rebuild_recovery: If true, rebuild the recovery patch used by non-A/B |
| 961 | devices and write it to the system image. |
| 962 | """ |
| 963 | |
| 964 | # Regenerate IMAGES in the target directory. |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 965 | |
Daniel Norman | dbbf5a3 | 2020-10-22 16:03:32 -0700 | [diff] [blame] | 966 | add_img_args = [ |
| 967 | '--verbose', |
| 968 | '--add_missing', |
| 969 | ] |
Bill Peckham | e868aec | 2019-09-17 17:06:47 -0700 | [diff] [blame] | 970 | # TODO(b/132730255): Remove this if statement. |
Daniel Norman | a4911da | 2019-03-15 14:36:21 -0700 | [diff] [blame] | 971 | if rebuild_recovery: |
| 972 | add_img_args.append('--rebuild_recovery') |
Kiyoung Kim | 7cbeda7 | 2019-06-28 13:26:04 +0900 | [diff] [blame] | 973 | add_img_args.append(target_files_dir) |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 974 | |
| 975 | add_img_to_target_files.main(add_img_args) |
| 976 | |
Kiyoung Kim | 7cbeda7 | 2019-06-28 13:26:04 +0900 | [diff] [blame] | 977 | |
Daniel Norman | 571e182 | 2021-06-25 17:18:25 -0700 | [diff] [blame] | 978 | def rebuild_image_with_sepolicy(target_files_dir, |
| 979 | vendor_otatools=None, |
| 980 | vendor_target_files=None): |
| 981 | """Rebuilds odm.img or vendor.img to include merged sepolicy files. |
| 982 | |
| 983 | If odm is present then odm is preferred -- otherwise vendor is used. |
| 984 | |
| 985 | Args: |
| 986 | target_files_dir: Path to the extracted merged target-files package. |
| 987 | vendor_otatools: If not None, path to an otatools.zip from the vendor build |
| 988 | that is used when recompiling the image. |
| 989 | vendor_target_files: Expected if vendor_otatools is not None. Path to the |
| 990 | vendor target-files zip. |
| 991 | """ |
| 992 | partition = 'vendor' |
| 993 | if os.path.exists(os.path.join(target_files_dir, 'ODM')) or os.path.exists( |
| 994 | os.path.join(target_files_dir, 'IMAGES/odm.img')): |
| 995 | partition = 'odm' |
| 996 | partition_img = '{}.img'.format(partition) |
| 997 | |
| 998 | logger.info('Recompiling %s using the merged sepolicy files.', partition_img) |
| 999 | |
| 1000 | # Copy the combined SEPolicy file and framework hashes to the image that is |
| 1001 | # being rebuilt. |
| 1002 | def copy_selinux_file(input_path, output_filename): |
| 1003 | shutil.copy( |
| 1004 | os.path.join(target_files_dir, input_path), |
| 1005 | os.path.join(target_files_dir, partition.upper(), 'etc/selinux', |
| 1006 | output_filename)) |
| 1007 | |
| 1008 | copy_selinux_file('META/combined_sepolicy', 'precompiled_sepolicy') |
| 1009 | copy_selinux_file('SYSTEM/etc/selinux/plat_sepolicy_and_mapping.sha256', |
| 1010 | 'precompiled_sepolicy.plat_sepolicy_and_mapping.sha256') |
| 1011 | copy_selinux_file( |
| 1012 | 'SYSTEM_EXT/etc/selinux/system_ext_sepolicy_and_mapping.sha256', |
| 1013 | 'precompiled_sepolicy.system_ext_sepolicy_and_mapping.sha256') |
| 1014 | copy_selinux_file('PRODUCT/etc/selinux/product_sepolicy_and_mapping.sha256', |
| 1015 | 'precompiled_sepolicy.product_sepolicy_and_mapping.sha256') |
| 1016 | |
| 1017 | if not vendor_otatools: |
| 1018 | # Remove the partition from the merged target-files archive. It will be |
| 1019 | # rebuilt later automatically by generate_images(). |
| 1020 | os.remove(os.path.join(target_files_dir, 'IMAGES', partition_img)) |
| 1021 | else: |
| 1022 | # TODO(b/192253131): Remove the need for vendor_otatools by fixing |
| 1023 | # backwards-compatibility issues when compiling images on R from S+. |
| 1024 | if not vendor_target_files: |
| 1025 | raise ValueError( |
| 1026 | 'Expected vendor_target_files if vendor_otatools is not None.') |
| 1027 | logger.info( |
| 1028 | '%s recompilation will be performed using the vendor otatools.zip', |
| 1029 | partition_img) |
| 1030 | |
| 1031 | # Unzip the vendor build's otatools.zip and target-files archive. |
| 1032 | vendor_otatools_dir = common.MakeTempDir( |
| 1033 | prefix='merge_target_files_vendor_otatools_') |
| 1034 | vendor_target_files_dir = common.MakeTempDir( |
| 1035 | prefix='merge_target_files_vendor_target_files_') |
| 1036 | common.UnzipToDir(vendor_otatools, vendor_otatools_dir) |
| 1037 | common.UnzipToDir(vendor_target_files, vendor_target_files_dir) |
| 1038 | |
| 1039 | # Copy the partition contents from the merged target-files archive to the |
| 1040 | # vendor target-files archive. |
| 1041 | shutil.rmtree(os.path.join(vendor_target_files_dir, partition.upper())) |
| 1042 | shutil.copytree( |
| 1043 | os.path.join(target_files_dir, partition.upper()), |
| 1044 | os.path.join(vendor_target_files_dir, partition.upper())) |
| 1045 | |
| 1046 | # Delete then rebuild the partition. |
| 1047 | os.remove(os.path.join(vendor_target_files_dir, 'IMAGES', partition_img)) |
| 1048 | rebuild_partition_command = [ |
| 1049 | os.path.join(vendor_otatools_dir, 'bin', 'add_img_to_target_files'), |
| 1050 | '--verbose', |
| 1051 | '--add_missing', |
| 1052 | vendor_target_files_dir, |
| 1053 | ] |
| 1054 | logger.info('Recompiling %s: %s', partition_img, |
| 1055 | ' '.join(rebuild_partition_command)) |
| 1056 | common.RunAndCheckOutput(rebuild_partition_command, verbose=True) |
| 1057 | |
| 1058 | # Move the newly-created image to the merged target files dir. |
| 1059 | shutil.move( |
| 1060 | os.path.join(vendor_target_files_dir, 'IMAGES', partition_img), |
| 1061 | os.path.join(target_files_dir, 'IMAGES', partition_img)) |
| 1062 | |
| 1063 | |
Kiyoung Kim | 7cbeda7 | 2019-06-28 13:26:04 +0900 | [diff] [blame] | 1064 | def generate_super_empty_image(target_dir, output_super_empty): |
Tao Bao | 2ad4b82 | 2019-06-27 16:52:12 -0700 | [diff] [blame] | 1065 | """Generates super_empty image from target package. |
Kiyoung Kim | 7cbeda7 | 2019-06-28 13:26:04 +0900 | [diff] [blame] | 1066 | |
| 1067 | Args: |
| 1068 | target_dir: Path to the target file package which contains misc_info.txt for |
| 1069 | detailed information for super image. |
| 1070 | output_super_empty: If provided, copies a super_empty.img file from the |
| 1071 | target files package to this path. |
| 1072 | """ |
Daniel Norman | 1bd2a1d | 2019-04-18 12:32:18 -0700 | [diff] [blame] | 1073 | # Create super_empty.img using the merged misc_info.txt. |
| 1074 | |
Daniel Norman | 4cc9df6 | 2019-07-18 10:11:07 -0700 | [diff] [blame] | 1075 | misc_info_txt = os.path.join(target_dir, 'META', 'misc_info.txt') |
Daniel Norman | 1bd2a1d | 2019-04-18 12:32:18 -0700 | [diff] [blame] | 1076 | |
Kiyoung Kim | ebe7c9c | 2019-06-25 17:09:55 +0900 | [diff] [blame] | 1077 | use_dynamic_partitions = common.LoadDictionaryFromFile(misc_info_txt).get( |
| 1078 | 'use_dynamic_partitions') |
Daniel Norman | 1bd2a1d | 2019-04-18 12:32:18 -0700 | [diff] [blame] | 1079 | |
| 1080 | if use_dynamic_partitions != 'true' and output_super_empty: |
| 1081 | raise ValueError( |
| 1082 | 'Building super_empty.img requires use_dynamic_partitions=true.') |
| 1083 | elif use_dynamic_partitions == 'true': |
Daniel Norman | 4cc9df6 | 2019-07-18 10:11:07 -0700 | [diff] [blame] | 1084 | super_empty_img = os.path.join(target_dir, 'IMAGES', 'super_empty.img') |
Daniel Norman | 1bd2a1d | 2019-04-18 12:32:18 -0700 | [diff] [blame] | 1085 | build_super_image_args = [ |
| 1086 | misc_info_txt, |
| 1087 | super_empty_img, |
| 1088 | ] |
| 1089 | build_super_image.main(build_super_image_args) |
| 1090 | |
| 1091 | # Copy super_empty.img to the user-provided output_super_empty location. |
| 1092 | if output_super_empty: |
| 1093 | shutil.copyfile(super_empty_img, output_super_empty) |
| 1094 | |
Daniel Norman | b8a2f9d | 2019-04-24 12:55:51 -0700 | [diff] [blame] | 1095 | |
Kiyoung Kim | 7cbeda7 | 2019-06-28 13:26:04 +0900 | [diff] [blame] | 1096 | def create_target_files_archive(output_file, source_dir, temp_dir): |
Tao Bao | 2ad4b82 | 2019-06-27 16:52:12 -0700 | [diff] [blame] | 1097 | """Creates archive from target package. |
Kiyoung Kim | 7cbeda7 | 2019-06-28 13:26:04 +0900 | [diff] [blame] | 1098 | |
| 1099 | Args: |
| 1100 | output_file: The name of the zip archive target files package. |
| 1101 | source_dir: The target directory contains package to be archived. |
| 1102 | temp_dir: Path to temporary directory for any intermediate files. |
| 1103 | """ |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 1104 | output_target_files_list = os.path.join(temp_dir, 'output.list') |
Kiyoung Kim | 7cbeda7 | 2019-06-28 13:26:04 +0900 | [diff] [blame] | 1105 | output_zip = os.path.abspath(output_file) |
Daniel Norman | 4cc9df6 | 2019-07-18 10:11:07 -0700 | [diff] [blame] | 1106 | output_target_files_meta_dir = os.path.join(source_dir, 'META') |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 1107 | |
Daniel Norman | dbbf5a3 | 2020-10-22 16:03:32 -0700 | [diff] [blame] | 1108 | def files_from_path(target_path, extra_args=None): |
| 1109 | """Gets files under the given path and return a sorted list.""" |
| 1110 | find_command = ['find', target_path] + (extra_args or []) |
| 1111 | find_process = common.Run( |
| 1112 | find_command, stdout=subprocess.PIPE, verbose=False) |
| 1113 | return common.RunAndCheckOutput(['sort'], |
| 1114 | stdin=find_process.stdout, |
| 1115 | verbose=False) |
| 1116 | |
Kiyoung Kim | 7cbeda7 | 2019-06-28 13:26:04 +0900 | [diff] [blame] | 1117 | meta_content = files_from_path(output_target_files_meta_dir) |
Daniel Norman | 4cc9df6 | 2019-07-18 10:11:07 -0700 | [diff] [blame] | 1118 | other_content = files_from_path( |
| 1119 | source_dir, |
| 1120 | ['-path', output_target_files_meta_dir, '-prune', '-o', '-print']) |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 1121 | |
Tao Bao | 2ad4b82 | 2019-06-27 16:52:12 -0700 | [diff] [blame] | 1122 | with open(output_target_files_list, 'w') as f: |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 1123 | f.write(meta_content) |
| 1124 | f.write(other_content) |
| 1125 | |
| 1126 | command = [ |
Bill Peckham | f753e15 | 2019-02-19 18:02:46 -0800 | [diff] [blame] | 1127 | 'soong_zip', |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 1128 | '-d', |
Daniel Norman | e5b134a | 2019-04-17 14:54:06 -0700 | [diff] [blame] | 1129 | '-o', |
| 1130 | output_zip, |
| 1131 | '-C', |
Kiyoung Kim | 7cbeda7 | 2019-06-28 13:26:04 +0900 | [diff] [blame] | 1132 | source_dir, |
Daniel Norman | eaf5c1d | 2021-02-09 11:01:42 -0800 | [diff] [blame] | 1133 | '-r', |
Daniel Norman | e5b134a | 2019-04-17 14:54:06 -0700 | [diff] [blame] | 1134 | output_target_files_list, |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 1135 | ] |
Kiyoung Kim | 7cbeda7 | 2019-06-28 13:26:04 +0900 | [diff] [blame] | 1136 | |
| 1137 | logger.info('creating %s', output_file) |
Daniel Norman | eaf5c1d | 2021-02-09 11:01:42 -0800 | [diff] [blame] | 1138 | common.RunAndCheckOutput(command, verbose=True) |
Kiyoung Kim | 7cbeda7 | 2019-06-28 13:26:04 +0900 | [diff] [blame] | 1139 | logger.info('finished creating %s', output_file) |
| 1140 | |
| 1141 | return output_zip |
| 1142 | |
| 1143 | |
Kiyoung Kim | 7cbeda7 | 2019-06-28 13:26:04 +0900 | [diff] [blame] | 1144 | def merge_target_files(temp_dir, framework_target_files, framework_item_list, |
| 1145 | framework_misc_info_keys, vendor_target_files, |
| 1146 | vendor_item_list, output_target_files, output_dir, |
| 1147 | output_item_list, output_ota, output_img, |
Daniel Norman | 571e182 | 2021-06-25 17:18:25 -0700 | [diff] [blame] | 1148 | output_super_empty, rebuild_recovery, vendor_otatools, |
| 1149 | rebuild_sepolicy): |
Tao Bao | 2ad4b82 | 2019-06-27 16:52:12 -0700 | [diff] [blame] | 1150 | """Merges two target files packages together. |
Kiyoung Kim | 7cbeda7 | 2019-06-28 13:26:04 +0900 | [diff] [blame] | 1151 | |
| 1152 | This function takes framework and vendor target files packages as input, |
| 1153 | performs various file extractions, special case processing, and finally |
| 1154 | creates a merged zip archive as output. |
| 1155 | |
| 1156 | Args: |
| 1157 | temp_dir: The name of a directory we use when we extract items from the |
| 1158 | input target files packages, and also a scratch directory that we use for |
| 1159 | temporary files. |
| 1160 | framework_target_files: The name of the zip archive containing the framework |
| 1161 | partial target files package. |
| 1162 | framework_item_list: The list of items to extract from the partial framework |
| 1163 | target files package as is, meaning these items will land in the output |
| 1164 | target files package exactly as they appear in the input partial framework |
| 1165 | target files package. |
Daniel Norman | dbbf5a3 | 2020-10-22 16:03:32 -0700 | [diff] [blame] | 1166 | framework_misc_info_keys: A list of keys to obtain from the framework |
| 1167 | instance of META/misc_info.txt. The remaining keys should come from the |
| 1168 | vendor instance. |
Kiyoung Kim | 7cbeda7 | 2019-06-28 13:26:04 +0900 | [diff] [blame] | 1169 | vendor_target_files: The name of the zip archive containing the vendor |
| 1170 | partial target files package. |
| 1171 | vendor_item_list: The list of items to extract from the partial vendor |
| 1172 | target files package as is, meaning these items will land in the output |
| 1173 | target files package exactly as they appear in the input partial vendor |
| 1174 | target files package. |
| 1175 | output_target_files: The name of the output zip archive target files package |
| 1176 | created by merging framework and vendor. |
| 1177 | output_dir: The destination directory for saving merged files. |
| 1178 | output_item_list: The list of items to copy into the output_dir. |
| 1179 | output_ota: The name of the output zip archive ota package. |
| 1180 | output_img: The name of the output zip archive img package. |
| 1181 | output_super_empty: If provided, creates a super_empty.img file from the |
| 1182 | merged target files package and saves it at this path. |
| 1183 | rebuild_recovery: If true, rebuild the recovery patch used by non-A/B |
| 1184 | devices and write it to the system image. |
Daniel Norman | 571e182 | 2021-06-25 17:18:25 -0700 | [diff] [blame] | 1185 | vendor_otatools: Path to an otatools zip used for recompiling vendor images. |
| 1186 | rebuild_sepolicy: If true, rebuild odm.img (if target uses ODM) or |
| 1187 | vendor.img using a merged precompiled_sepolicy file. |
Kiyoung Kim | 7cbeda7 | 2019-06-28 13:26:04 +0900 | [diff] [blame] | 1188 | """ |
| 1189 | |
| 1190 | logger.info('starting: merge framework %s and vendor %s into output %s', |
| 1191 | framework_target_files, vendor_target_files, output_target_files) |
| 1192 | |
| 1193 | output_target_files_temp_dir = create_merged_package( |
| 1194 | temp_dir, framework_target_files, framework_item_list, |
| 1195 | vendor_target_files, vendor_item_list, framework_misc_info_keys, |
| 1196 | rebuild_recovery) |
| 1197 | |
Yifan Hong | ade0d3f | 2019-08-21 16:37:11 -0700 | [diff] [blame] | 1198 | if not check_target_files_vintf.CheckVintf(output_target_files_temp_dir): |
Daniel Norman | b0c7591 | 2020-09-24 14:30:21 -0700 | [diff] [blame] | 1199 | raise RuntimeError('Incompatible VINTF metadata') |
Yifan Hong | ade0d3f | 2019-08-21 16:37:11 -0700 | [diff] [blame] | 1200 | |
Daniel Norman | 21c34f7 | 2020-11-11 17:25:50 -0800 | [diff] [blame] | 1201 | partition_map = common.PartitionMapFromTargetFiles( |
| 1202 | output_target_files_temp_dir) |
| 1203 | |
Daniel Norman | d335156 | 2020-10-29 12:33:11 -0700 | [diff] [blame] | 1204 | # Generate and check for cross-partition violations of sharedUserId |
| 1205 | # values in APKs. This requires the input target-files packages to contain |
| 1206 | # *.apk files. |
Daniel Norman | b8d52a2 | 2020-10-26 17:55:00 -0700 | [diff] [blame] | 1207 | shareduid_violation_modules = os.path.join( |
| 1208 | output_target_files_temp_dir, 'META', 'shareduid_violation_modules.json') |
| 1209 | with open(shareduid_violation_modules, 'w') as f: |
Daniel Norman | b8d52a2 | 2020-10-26 17:55:00 -0700 | [diff] [blame] | 1210 | violation = find_shareduid_violation.FindShareduidViolation( |
| 1211 | output_target_files_temp_dir, partition_map) |
Daniel Norman | d335156 | 2020-10-29 12:33:11 -0700 | [diff] [blame] | 1212 | |
| 1213 | # Write the output to a file to enable debugging. |
Daniel Norman | b8d52a2 | 2020-10-26 17:55:00 -0700 | [diff] [blame] | 1214 | f.write(violation) |
Daniel Norman | d335156 | 2020-10-29 12:33:11 -0700 | [diff] [blame] | 1215 | |
| 1216 | # Check for violations across the input builds' partition groups. |
Daniel Norman | 21c34f7 | 2020-11-11 17:25:50 -0800 | [diff] [blame] | 1217 | framework_partitions = item_list_to_partition_set(framework_item_list) |
| 1218 | vendor_partitions = item_list_to_partition_set(vendor_item_list) |
Daniel Norman | d335156 | 2020-10-29 12:33:11 -0700 | [diff] [blame] | 1219 | shareduid_errors = common.SharedUidPartitionViolations( |
| 1220 | json.loads(violation), [framework_partitions, vendor_partitions]) |
| 1221 | if shareduid_errors: |
| 1222 | for error in shareduid_errors: |
| 1223 | logger.error(error) |
| 1224 | raise ValueError('sharedUserId APK error. See %s' % |
| 1225 | shareduid_violation_modules) |
Daniel Norman | b8d52a2 | 2020-10-26 17:55:00 -0700 | [diff] [blame] | 1226 | |
Daniel Norman | 48603ff | 2021-02-22 15:15:24 -0800 | [diff] [blame] | 1227 | # host_init_verifier and secilc check only the following partitions: |
Daniel Norman | 21c34f7 | 2020-11-11 17:25:50 -0800 | [diff] [blame] | 1228 | filtered_partitions = { |
| 1229 | partition: path |
| 1230 | for partition, path in partition_map.items() |
Daniel Norman | 21c34f7 | 2020-11-11 17:25:50 -0800 | [diff] [blame] | 1231 | if partition in ['system', 'system_ext', 'product', 'vendor', 'odm'] |
| 1232 | } |
Daniel Norman | 48603ff | 2021-02-22 15:15:24 -0800 | [diff] [blame] | 1233 | |
| 1234 | # Run host_init_verifier on the combined init rc files. |
Daniel Norman | 21c34f7 | 2020-11-11 17:25:50 -0800 | [diff] [blame] | 1235 | common.RunHostInitVerifier( |
| 1236 | product_out=output_target_files_temp_dir, |
| 1237 | partition_map=filtered_partitions) |
| 1238 | |
Daniel Norman | 48603ff | 2021-02-22 15:15:24 -0800 | [diff] [blame] | 1239 | # Check that the split sepolicy from the multiple builds can compile. |
Daniel Norman | 571e182 | 2021-06-25 17:18:25 -0700 | [diff] [blame] | 1240 | split_sepolicy_cmd = compile_split_sepolicy(output_target_files_temp_dir, |
| 1241 | filtered_partitions) |
Daniel Norman | 48603ff | 2021-02-22 15:15:24 -0800 | [diff] [blame] | 1242 | logger.info('Compiling split sepolicy: %s', ' '.join(split_sepolicy_cmd)) |
| 1243 | common.RunAndCheckOutput(split_sepolicy_cmd) |
Daniel Norman | 571e182 | 2021-06-25 17:18:25 -0700 | [diff] [blame] | 1244 | # Include the compiled policy in an image if requested. |
| 1245 | if rebuild_sepolicy: |
| 1246 | rebuild_image_with_sepolicy(output_target_files_temp_dir, vendor_otatools, |
| 1247 | vendor_target_files) |
Daniel Norman | 48603ff | 2021-02-22 15:15:24 -0800 | [diff] [blame] | 1248 | |
Daniel Norman | e9af70a | 2021-04-15 16:39:22 -0700 | [diff] [blame] | 1249 | # Run validation checks on the pre-installed APEX files. |
| 1250 | validate_merged_apex_info(output_target_files_temp_dir, partition_map.keys()) |
| 1251 | |
Kiyoung Kim | 7cbeda7 | 2019-06-28 13:26:04 +0900 | [diff] [blame] | 1252 | generate_images(output_target_files_temp_dir, rebuild_recovery) |
| 1253 | |
| 1254 | generate_super_empty_image(output_target_files_temp_dir, output_super_empty) |
| 1255 | |
Kiyoung Kim | 7cbeda7 | 2019-06-28 13:26:04 +0900 | [diff] [blame] | 1256 | # Finally, create the output target files zip archive and/or copy the |
| 1257 | # output items to the output target files directory. |
| 1258 | |
| 1259 | if output_dir: |
| 1260 | copy_items(output_target_files_temp_dir, output_dir, output_item_list) |
| 1261 | |
| 1262 | if not output_target_files: |
| 1263 | return |
| 1264 | |
Daniel Norman | db8cacc | 2021-04-09 15:34:43 -0700 | [diff] [blame] | 1265 | # Create the merged META/care_map.bp |
| 1266 | generate_care_map(partition_map.keys(), output_target_files_temp_dir) |
| 1267 | |
Kiyoung Kim | 7cbeda7 | 2019-06-28 13:26:04 +0900 | [diff] [blame] | 1268 | output_zip = create_target_files_archive(output_target_files, |
| 1269 | output_target_files_temp_dir, |
| 1270 | temp_dir) |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 1271 | |
Daniel Norman | 74eb74b | 2019-09-18 14:01:48 -0700 | [diff] [blame] | 1272 | # Create the IMG package from the merged target files package. |
Daniel Norman | 74eb74b | 2019-09-18 14:01:48 -0700 | [diff] [blame] | 1273 | if output_img: |
| 1274 | img_from_target_files.main([output_zip, output_img]) |
| 1275 | |
Daniel Norman | 3b64ce1 | 2019-04-16 16:11:35 -0700 | [diff] [blame] | 1276 | # Create the OTA package from the merged target files package. |
| 1277 | |
| 1278 | if output_ota: |
Daniel Norman | 4cc9df6 | 2019-07-18 10:11:07 -0700 | [diff] [blame] | 1279 | ota_from_target_files.main([output_zip, output_ota]) |
Daniel Norman | 3b64ce1 | 2019-04-16 16:11:35 -0700 | [diff] [blame] | 1280 | |
Daniel Norman | 1bd2a1d | 2019-04-18 12:32:18 -0700 | [diff] [blame] | 1281 | |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 1282 | def call_func_with_temp_dir(func, keep_tmp): |
Tao Bao | 2ad4b82 | 2019-06-27 16:52:12 -0700 | [diff] [blame] | 1283 | """Manages the creation and cleanup of the temporary directory. |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 1284 | |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 1285 | This function calls the given function after first creating a temporary |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 1286 | directory. It also cleans up the temporary directory. |
| 1287 | |
| 1288 | Args: |
Daniel Norman | e5b134a | 2019-04-17 14:54:06 -0700 | [diff] [blame] | 1289 | func: The function to call. Should accept one parameter, the path to the |
| 1290 | temporary directory. |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 1291 | keep_tmp: Keep the temporary directory after processing is complete. |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 1292 | """ |
| 1293 | |
| 1294 | # Create a temporary directory. This will serve as the parent of directories |
| 1295 | # we use when we extract items from the input target files packages, and also |
| 1296 | # a scratch directory that we use for temporary files. |
| 1297 | |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 1298 | temp_dir = common.MakeTempDir(prefix='merge_target_files_') |
| 1299 | |
| 1300 | try: |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 1301 | func(temp_dir) |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 1302 | finally: |
| 1303 | if keep_tmp: |
| 1304 | logger.info('keeping %s', temp_dir) |
| 1305 | else: |
| 1306 | common.Cleanup() |
| 1307 | |
| 1308 | |
| 1309 | def main(): |
| 1310 | """The main function. |
| 1311 | |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 1312 | Process command line arguments, then call merge_target_files to |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 1313 | perform the heavy lifting. |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 1314 | """ |
| 1315 | |
| 1316 | common.InitLogging() |
| 1317 | |
Bill Peckham | f753e15 | 2019-02-19 18:02:46 -0800 | [diff] [blame] | 1318 | def option_handler(o, a): |
| 1319 | if o == '--system-target-files': |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 1320 | logger.warning( |
| 1321 | '--system-target-files has been renamed to --framework-target-files') |
| 1322 | OPTIONS.framework_target_files = a |
| 1323 | elif o == '--framework-target-files': |
| 1324 | OPTIONS.framework_target_files = a |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 1325 | elif o == '--system-item-list': |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 1326 | logger.warning( |
| 1327 | '--system-item-list has been renamed to --framework-item-list') |
| 1328 | OPTIONS.framework_item_list = a |
| 1329 | elif o == '--framework-item-list': |
| 1330 | OPTIONS.framework_item_list = a |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 1331 | elif o == '--system-misc-info-keys': |
Daniel Norman | 4cc9df6 | 2019-07-18 10:11:07 -0700 | [diff] [blame] | 1332 | logger.warning('--system-misc-info-keys has been renamed to ' |
| 1333 | '--framework-misc-info-keys') |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 1334 | OPTIONS.framework_misc_info_keys = a |
| 1335 | elif o == '--framework-misc-info-keys': |
| 1336 | OPTIONS.framework_misc_info_keys = a |
Bill Peckham | f753e15 | 2019-02-19 18:02:46 -0800 | [diff] [blame] | 1337 | elif o == '--other-target-files': |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 1338 | logger.warning( |
| 1339 | '--other-target-files has been renamed to --vendor-target-files') |
| 1340 | OPTIONS.vendor_target_files = a |
| 1341 | elif o == '--vendor-target-files': |
| 1342 | OPTIONS.vendor_target_files = a |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 1343 | elif o == '--other-item-list': |
Daniel Norman | 2d7989a | 2021-04-05 17:40:47 +0000 | [diff] [blame] | 1344 | logger.warning('--other-item-list has been renamed to --vendor-item-list') |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 1345 | OPTIONS.vendor_item_list = a |
| 1346 | elif o == '--vendor-item-list': |
| 1347 | OPTIONS.vendor_item_list = a |
Bill Peckham | f753e15 | 2019-02-19 18:02:46 -0800 | [diff] [blame] | 1348 | elif o == '--output-target-files': |
| 1349 | OPTIONS.output_target_files = a |
Daniel Norman | fdb3881 | 2019-04-15 09:47:24 -0700 | [diff] [blame] | 1350 | elif o == '--output-dir': |
| 1351 | OPTIONS.output_dir = a |
| 1352 | elif o == '--output-item-list': |
| 1353 | OPTIONS.output_item_list = a |
Daniel Norman | 3b64ce1 | 2019-04-16 16:11:35 -0700 | [diff] [blame] | 1354 | elif o == '--output-ota': |
| 1355 | OPTIONS.output_ota = a |
Daniel Norman | 1bd2a1d | 2019-04-18 12:32:18 -0700 | [diff] [blame] | 1356 | elif o == '--output-img': |
| 1357 | OPTIONS.output_img = a |
Daniel Norman | f031825 | 2019-04-15 11:34:56 -0700 | [diff] [blame] | 1358 | elif o == '--output-super-empty': |
| 1359 | OPTIONS.output_super_empty = a |
Daniel Norman | b0c7591 | 2020-09-24 14:30:21 -0700 | [diff] [blame] | 1360 | elif o == '--rebuild_recovery': # TODO(b/132730255): Warn |
Daniel Norman | a4911da | 2019-03-15 14:36:21 -0700 | [diff] [blame] | 1361 | OPTIONS.rebuild_recovery = True |
Daniel Norman | b0c7591 | 2020-09-24 14:30:21 -0700 | [diff] [blame] | 1362 | elif o == '--allow-duplicate-apkapex-keys': |
| 1363 | OPTIONS.allow_duplicate_apkapex_keys = True |
Daniel Norman | 571e182 | 2021-06-25 17:18:25 -0700 | [diff] [blame] | 1364 | elif o == '--vendor-otatools': |
| 1365 | OPTIONS.vendor_otatools = a |
| 1366 | elif o == '--rebuild-sepolicy': |
| 1367 | OPTIONS.rebuild_sepolicy = True |
Bill Peckham | 364c1cc | 2019-03-29 18:27:23 -0700 | [diff] [blame] | 1368 | elif o == '--keep-tmp': |
Bill Peckham | f753e15 | 2019-02-19 18:02:46 -0800 | [diff] [blame] | 1369 | OPTIONS.keep_tmp = True |
| 1370 | else: |
| 1371 | return False |
| 1372 | return True |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 1373 | |
Bill Peckham | f753e15 | 2019-02-19 18:02:46 -0800 | [diff] [blame] | 1374 | args = common.ParseOptions( |
Daniel Norman | e5b134a | 2019-04-17 14:54:06 -0700 | [diff] [blame] | 1375 | sys.argv[1:], |
| 1376 | __doc__, |
Bill Peckham | f753e15 | 2019-02-19 18:02:46 -0800 | [diff] [blame] | 1377 | extra_long_opts=[ |
| 1378 | 'system-target-files=', |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 1379 | 'framework-target-files=', |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 1380 | 'system-item-list=', |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 1381 | 'framework-item-list=', |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 1382 | 'system-misc-info-keys=', |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 1383 | 'framework-misc-info-keys=', |
Bill Peckham | f753e15 | 2019-02-19 18:02:46 -0800 | [diff] [blame] | 1384 | 'other-target-files=', |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 1385 | 'vendor-target-files=', |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 1386 | 'other-item-list=', |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 1387 | 'vendor-item-list=', |
Bill Peckham | f753e15 | 2019-02-19 18:02:46 -0800 | [diff] [blame] | 1388 | 'output-target-files=', |
Daniel Norman | fdb3881 | 2019-04-15 09:47:24 -0700 | [diff] [blame] | 1389 | 'output-dir=', |
| 1390 | 'output-item-list=', |
Daniel Norman | 3b64ce1 | 2019-04-16 16:11:35 -0700 | [diff] [blame] | 1391 | 'output-ota=', |
Daniel Norman | 1bd2a1d | 2019-04-18 12:32:18 -0700 | [diff] [blame] | 1392 | 'output-img=', |
Daniel Norman | f031825 | 2019-04-15 11:34:56 -0700 | [diff] [blame] | 1393 | 'output-super-empty=', |
Daniel Norman | a4911da | 2019-03-15 14:36:21 -0700 | [diff] [blame] | 1394 | 'rebuild_recovery', |
Daniel Norman | b0c7591 | 2020-09-24 14:30:21 -0700 | [diff] [blame] | 1395 | 'allow-duplicate-apkapex-keys', |
Daniel Norman | 571e182 | 2021-06-25 17:18:25 -0700 | [diff] [blame] | 1396 | 'vendor-otatools=', |
| 1397 | 'rebuild-sepolicy', |
Bill Peckham | 364c1cc | 2019-03-29 18:27:23 -0700 | [diff] [blame] | 1398 | 'keep-tmp', |
Bill Peckham | f753e15 | 2019-02-19 18:02:46 -0800 | [diff] [blame] | 1399 | ], |
| 1400 | extra_option_handler=option_handler) |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 1401 | |
Tao Bao | 2ad4b82 | 2019-06-27 16:52:12 -0700 | [diff] [blame] | 1402 | # pylint: disable=too-many-boolean-expressions |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 1403 | if (args or OPTIONS.framework_target_files is None or |
| 1404 | OPTIONS.vendor_target_files is None or |
Daniel Norman | e5b134a | 2019-04-17 14:54:06 -0700 | [diff] [blame] | 1405 | (OPTIONS.output_target_files is None and OPTIONS.output_dir is None) or |
Daniel Norman | 2d7989a | 2021-04-05 17:40:47 +0000 | [diff] [blame] | 1406 | (OPTIONS.output_dir is not None and OPTIONS.output_item_list is None)): |
Bill Peckham | f753e15 | 2019-02-19 18:02:46 -0800 | [diff] [blame] | 1407 | common.Usage(__doc__) |
Bill Peckham | 889b0c6 | 2019-02-21 18:53:37 -0800 | [diff] [blame] | 1408 | sys.exit(1) |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 1409 | |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 1410 | if OPTIONS.framework_item_list: |
Daniel Norman | 4cc9df6 | 2019-07-18 10:11:07 -0700 | [diff] [blame] | 1411 | framework_item_list = common.LoadListFromFile(OPTIONS.framework_item_list) |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 1412 | else: |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 1413 | framework_item_list = DEFAULT_FRAMEWORK_ITEM_LIST |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 1414 | |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 1415 | if OPTIONS.framework_misc_info_keys: |
Daniel Norman | 4cc9df6 | 2019-07-18 10:11:07 -0700 | [diff] [blame] | 1416 | framework_misc_info_keys = common.LoadListFromFile( |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 1417 | OPTIONS.framework_misc_info_keys) |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 1418 | else: |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 1419 | framework_misc_info_keys = DEFAULT_FRAMEWORK_MISC_INFO_KEYS |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 1420 | |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 1421 | if OPTIONS.vendor_item_list: |
Daniel Norman | 4cc9df6 | 2019-07-18 10:11:07 -0700 | [diff] [blame] | 1422 | vendor_item_list = common.LoadListFromFile(OPTIONS.vendor_item_list) |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 1423 | else: |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 1424 | vendor_item_list = DEFAULT_VENDOR_ITEM_LIST |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 1425 | |
Daniel Norman | fdb3881 | 2019-04-15 09:47:24 -0700 | [diff] [blame] | 1426 | if OPTIONS.output_item_list: |
Daniel Norman | 4cc9df6 | 2019-07-18 10:11:07 -0700 | [diff] [blame] | 1427 | output_item_list = common.LoadListFromFile(OPTIONS.output_item_list) |
Daniel Norman | fdb3881 | 2019-04-15 09:47:24 -0700 | [diff] [blame] | 1428 | else: |
| 1429 | output_item_list = None |
| 1430 | |
Daniel Norman | e596452 | 2019-03-19 10:32:03 -0700 | [diff] [blame] | 1431 | if not validate_config_lists( |
Daniel Norman | 2d7989a | 2021-04-05 17:40:47 +0000 | [diff] [blame] | 1432 | framework_item_list=framework_item_list, |
| 1433 | framework_misc_info_keys=framework_misc_info_keys, |
| 1434 | vendor_item_list=vendor_item_list): |
Daniel Norman | e596452 | 2019-03-19 10:32:03 -0700 | [diff] [blame] | 1435 | sys.exit(1) |
| 1436 | |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 1437 | call_func_with_temp_dir( |
| 1438 | lambda temp_dir: merge_target_files( |
| 1439 | temp_dir=temp_dir, |
Daniel Norman | d5d70ea | 2019-06-05 15:13:43 -0700 | [diff] [blame] | 1440 | framework_target_files=OPTIONS.framework_target_files, |
| 1441 | framework_item_list=framework_item_list, |
| 1442 | framework_misc_info_keys=framework_misc_info_keys, |
| 1443 | vendor_target_files=OPTIONS.vendor_target_files, |
| 1444 | vendor_item_list=vendor_item_list, |
Daniel Norman | a4911da | 2019-03-15 14:36:21 -0700 | [diff] [blame] | 1445 | output_target_files=OPTIONS.output_target_files, |
Daniel Norman | fdb3881 | 2019-04-15 09:47:24 -0700 | [diff] [blame] | 1446 | output_dir=OPTIONS.output_dir, |
| 1447 | output_item_list=output_item_list, |
Daniel Norman | 3b64ce1 | 2019-04-16 16:11:35 -0700 | [diff] [blame] | 1448 | output_ota=OPTIONS.output_ota, |
Daniel Norman | 1bd2a1d | 2019-04-18 12:32:18 -0700 | [diff] [blame] | 1449 | output_img=OPTIONS.output_img, |
Daniel Norman | f031825 | 2019-04-15 11:34:56 -0700 | [diff] [blame] | 1450 | output_super_empty=OPTIONS.output_super_empty, |
Daniel Norman | 571e182 | 2021-06-25 17:18:25 -0700 | [diff] [blame] | 1451 | rebuild_recovery=OPTIONS.rebuild_recovery, |
| 1452 | vendor_otatools=OPTIONS.vendor_otatools, |
| 1453 | rebuild_sepolicy=OPTIONS.rebuild_sepolicy), OPTIONS.keep_tmp) |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 1454 | |
| 1455 | |
| 1456 | if __name__ == '__main__': |
Bill Peckham | 889b0c6 | 2019-02-21 18:53:37 -0800 | [diff] [blame] | 1457 | main() |