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. |
| 16 | |
| 17 | """ |
| 18 | This script merges two partial target files packages (one of which contains |
| 19 | system files, and the other contains non-system files) together, producing a |
| 20 | complete target files package that can be used to generate an OTA package. |
| 21 | |
| 22 | Usage: merge_target_files.py [args] |
| 23 | |
| 24 | --system-target-files system-target-files-zip-archive |
| 25 | The input target files package containing system bits. This is a zip |
| 26 | archive. |
| 27 | |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 28 | --system-item-list system-item-list-file |
| 29 | The optional path to a newline-separated config file that replaces the |
| 30 | contents of default_system_item_list if provided. |
| 31 | |
| 32 | --system-misc-info-keys system-misc-info-keys-file |
| 33 | The optional path to a newline-separated config file that replaces the |
| 34 | contents of default_system_misc_info_keys if provided. |
| 35 | |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 36 | --other-target-files other-target-files-zip-archive |
| 37 | The input target files package containing other bits. This is a zip |
| 38 | archive. |
| 39 | |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 40 | --other-item-list other-item-list-file |
| 41 | The optional path to a newline-separated config file that replaces the |
| 42 | contents of default_other_item_list if provided. |
| 43 | |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 44 | --output-target-files output-target-files-package |
| 45 | The output merged target files package. Also a zip archive. |
Daniel Norman | a4911da | 2019-03-15 14:36:21 -0700 | [diff] [blame] | 46 | |
| 47 | --rebuild_recovery |
| 48 | Rebuild the recovery patch used by non-A/B devices and write it to the |
| 49 | system image. |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 50 | """ |
| 51 | |
| 52 | from __future__ import print_function |
| 53 | |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 54 | import fnmatch |
| 55 | import logging |
| 56 | import os |
| 57 | import sys |
| 58 | import zipfile |
| 59 | |
| 60 | import common |
| 61 | import add_img_to_target_files |
| 62 | |
| 63 | logger = logging.getLogger(__name__) |
| 64 | OPTIONS = common.OPTIONS |
| 65 | OPTIONS.verbose = True |
Bill Peckham | f753e15 | 2019-02-19 18:02:46 -0800 | [diff] [blame] | 66 | OPTIONS.system_target_files = None |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 67 | OPTIONS.system_item_list = None |
| 68 | OPTIONS.system_misc_info_keys = None |
Bill Peckham | f753e15 | 2019-02-19 18:02:46 -0800 | [diff] [blame] | 69 | OPTIONS.other_target_files = None |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 70 | OPTIONS.other_item_list = None |
Bill Peckham | f753e15 | 2019-02-19 18:02:46 -0800 | [diff] [blame] | 71 | OPTIONS.output_target_files = None |
Daniel Norman | a4911da | 2019-03-15 14:36:21 -0700 | [diff] [blame] | 72 | OPTIONS.rebuild_recovery = False |
Bill Peckham | f753e15 | 2019-02-19 18:02:46 -0800 | [diff] [blame] | 73 | OPTIONS.keep_tmp = False |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 74 | |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 75 | # default_system_item_list is a list of items to extract from the partial |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 76 | # system target files package as is, meaning these items will land in the |
| 77 | # output target files package exactly as they appear in the input partial |
| 78 | # system target files package. |
| 79 | |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 80 | default_system_item_list = [ |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 81 | 'META/apkcerts.txt', |
| 82 | 'META/filesystem_config.txt', |
| 83 | 'META/root_filesystem_config.txt', |
| 84 | 'META/system_manifest.xml', |
| 85 | 'META/system_matrix.xml', |
| 86 | 'META/update_engine_config.txt', |
| 87 | 'PRODUCT/*', |
| 88 | 'ROOT/*', |
| 89 | 'SYSTEM/*', |
| 90 | ] |
| 91 | |
| 92 | # system_extract_special_item_list is a list of items to extract from the |
| 93 | # partial system target files package that need some special processing, such |
| 94 | # as some sort of combination with items from the partial other target files |
| 95 | # package. |
| 96 | |
| 97 | system_extract_special_item_list = [ |
| 98 | 'META/*', |
| 99 | ] |
| 100 | |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 101 | # default_system_misc_info_keys is a list of keys to obtain from the system instance of |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 102 | # META/misc_info.txt. The remaining keys from the other instance. |
| 103 | |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 104 | default_system_misc_info_keys = [ |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 105 | 'avb_system_hashtree_enable', |
| 106 | 'avb_system_add_hashtree_footer_args', |
| 107 | 'avb_system_key_path', |
| 108 | 'avb_system_algorithm', |
| 109 | 'avb_system_rollback_index_location', |
| 110 | 'avb_product_hashtree_enable', |
| 111 | 'avb_product_add_hashtree_footer_args', |
| 112 | 'avb_product_services_hashtree_enable', |
| 113 | 'avb_product_services_add_hashtree_footer_args', |
| 114 | 'system_root_image', |
| 115 | 'root_dir', |
| 116 | 'ab_update', |
| 117 | 'default_system_dev_certificate', |
| 118 | 'system_size', |
| 119 | ] |
| 120 | |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 121 | # default_other_item_list is a list of items to extract from the partial |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 122 | # other target files package as is, meaning these items will land in the output |
| 123 | # target files package exactly as they appear in the input partial other target |
| 124 | # files package. |
| 125 | |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 126 | default_other_item_list = [ |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 127 | 'META/boot_filesystem_config.txt', |
| 128 | 'META/otakeys.txt', |
| 129 | 'META/releasetools.py', |
| 130 | 'META/vendor_filesystem_config.txt', |
| 131 | 'META/vendor_manifest.xml', |
| 132 | 'META/vendor_matrix.xml', |
| 133 | 'BOOT/*', |
| 134 | 'DATA/*', |
| 135 | 'ODM/*', |
| 136 | 'OTA/android-info.txt', |
| 137 | 'PREBUILT_IMAGES/*', |
| 138 | 'RADIO/*', |
| 139 | 'VENDOR/*', |
| 140 | ] |
| 141 | |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 142 | # other_extract_special_item_list is a list of items to extract from the |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 143 | # partial other target files package that need some special processing, such as |
| 144 | # some sort of combination with items from the partial system target files |
| 145 | # package. |
| 146 | |
| 147 | other_extract_special_item_list = [ |
| 148 | 'META/*', |
| 149 | ] |
| 150 | |
| 151 | |
| 152 | def extract_items(target_files, target_files_temp_dir, extract_item_list): |
| 153 | """Extract items from target files to temporary directory. |
| 154 | |
| 155 | This function extracts from the specified target files zip archive into the |
| 156 | specified temporary directory, the items specified in the extract item list. |
| 157 | |
| 158 | Args: |
| 159 | target_files: The target files zip archive from which to extract items. |
| 160 | |
| 161 | target_files_temp_dir: The temporary directory where the extracted items |
| 162 | will land. |
| 163 | |
| 164 | extract_item_list: A list of items to extract. |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 165 | """ |
| 166 | |
| 167 | logger.info('extracting from %s', target_files) |
| 168 | |
| 169 | # Filter the extract_item_list to remove any items that do not exist in the |
| 170 | # zip file. Otherwise, the extraction step will fail. |
| 171 | |
| 172 | with zipfile.ZipFile( |
| 173 | target_files, |
| 174 | 'r', |
| 175 | allowZip64=True) as target_files_zipfile: |
| 176 | target_files_namelist = target_files_zipfile.namelist() |
| 177 | |
| 178 | filtered_extract_item_list = [] |
| 179 | for pattern in extract_item_list: |
| 180 | matching_namelist = fnmatch.filter(target_files_namelist, pattern) |
| 181 | if not matching_namelist: |
| 182 | logger.warning('no match for %s', pattern) |
| 183 | else: |
| 184 | filtered_extract_item_list.append(pattern) |
| 185 | |
Bill Peckham | 8ff3fbd | 2019-02-22 10:57:43 -0800 | [diff] [blame] | 186 | # Extract from target_files into target_files_temp_dir the |
| 187 | # filtered_extract_item_list. |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 188 | |
Bill Peckham | 8ff3fbd | 2019-02-22 10:57:43 -0800 | [diff] [blame] | 189 | common.UnzipToDir( |
| 190 | target_files, |
| 191 | target_files_temp_dir, |
| 192 | filtered_extract_item_list) |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 193 | |
| 194 | |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 195 | def read_config_list(config_file_path): |
| 196 | """Reads a config file into a list of strings. |
| 197 | |
| 198 | Expects the file to be newline-separated. |
| 199 | |
| 200 | Args: |
| 201 | config_file_path: The path to the config file to open and read. |
| 202 | """ |
| 203 | with open(config_file_path) as config_file: |
| 204 | return config_file.read().splitlines() |
| 205 | |
| 206 | |
Daniel Norman | 19b9fe9 | 2019-03-19 14:48:02 -0700 | [diff] [blame^] | 207 | def validate_config_lists( |
| 208 | system_item_list, |
| 209 | system_misc_info_keys, |
| 210 | other_item_list): |
Daniel Norman | e596452 | 2019-03-19 10:32:03 -0700 | [diff] [blame] | 211 | """Performs validations on the merge config lists. |
| 212 | |
| 213 | Args: |
| 214 | system_item_list: The list of items to extract from the partial |
| 215 | system target files package as is. |
| 216 | |
Daniel Norman | 19b9fe9 | 2019-03-19 14:48:02 -0700 | [diff] [blame^] | 217 | system_misc_info_keys: A list of keys to obtain from the system instance |
| 218 | of META/misc_info.txt. The remaining keys from the other instance. |
| 219 | |
Daniel Norman | e596452 | 2019-03-19 10:32:03 -0700 | [diff] [blame] | 220 | other_item_list: The list of items to extract from the partial |
| 221 | other target files package as is. |
| 222 | |
| 223 | Returns: |
| 224 | False if a validation fails, otherwise true. |
| 225 | """ |
| 226 | default_combined_item_set = set(default_system_item_list) |
| 227 | default_combined_item_set.update(default_other_item_list) |
| 228 | |
| 229 | combined_item_set = set(system_item_list) |
| 230 | combined_item_set.update(other_item_list) |
| 231 | |
| 232 | # Check that the merge config lists are not missing any item specified |
| 233 | # by the default config lists. |
| 234 | difference = default_combined_item_set.difference(combined_item_set) |
| 235 | if difference: |
| 236 | logger.error('Missing merge config items: %s' % list(difference)) |
| 237 | logger.error('Please ensure missing items are in either the ' |
| 238 | 'system-item-list or other-item-list files provided to ' |
| 239 | 'this script.') |
| 240 | return False |
| 241 | |
Daniel Norman | 19b9fe9 | 2019-03-19 14:48:02 -0700 | [diff] [blame^] | 242 | if ('dynamic_partition_list' in system_misc_info_keys) or ( |
| 243 | 'super_partition_groups' in system_misc_info_keys): |
| 244 | logger.error('Dynamic partition misc info keys should come from ' |
| 245 | 'the other instance of META/misc_info.txt.') |
| 246 | return False |
| 247 | |
Daniel Norman | e596452 | 2019-03-19 10:32:03 -0700 | [diff] [blame] | 248 | return True |
| 249 | |
| 250 | |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 251 | def process_ab_partitions_txt( |
| 252 | system_target_files_temp_dir, |
| 253 | other_target_files_temp_dir, |
| 254 | output_target_files_temp_dir): |
| 255 | """Perform special processing for META/ab_partitions.txt |
| 256 | |
| 257 | This function merges the contents of the META/ab_partitions.txt files from |
| 258 | the system directory and the other directory, placing the merged result in |
| 259 | the output directory. The precondition in that the files are already |
| 260 | extracted. The post condition is that the output META/ab_partitions.txt |
| 261 | contains the merged content. The format for each ab_partitions.txt a one |
| 262 | partition name per line. The output file contains the union of the parition |
| 263 | names. |
| 264 | |
| 265 | Args: |
| 266 | system_target_files_temp_dir: The name of a directory containing the |
| 267 | special items extracted from the system target files package. |
| 268 | |
| 269 | other_target_files_temp_dir: The name of a directory containing the |
| 270 | special items extracted from the other target files package. |
| 271 | |
| 272 | output_target_files_temp_dir: The name of a directory that will be used |
| 273 | to create the output target files package after all the special cases |
| 274 | are processed. |
| 275 | """ |
| 276 | |
| 277 | system_ab_partitions_txt = os.path.join( |
| 278 | system_target_files_temp_dir, 'META', 'ab_partitions.txt') |
| 279 | |
| 280 | other_ab_partitions_txt = os.path.join( |
| 281 | other_target_files_temp_dir, 'META', 'ab_partitions.txt') |
| 282 | |
| 283 | with open(system_ab_partitions_txt) as f: |
| 284 | system_ab_partitions = f.read().splitlines() |
| 285 | |
| 286 | with open(other_ab_partitions_txt) as f: |
| 287 | other_ab_partitions = f.read().splitlines() |
| 288 | |
| 289 | output_ab_partitions = set(system_ab_partitions + other_ab_partitions) |
| 290 | |
| 291 | output_ab_partitions_txt = os.path.join( |
| 292 | output_target_files_temp_dir, 'META', 'ab_partitions.txt') |
| 293 | |
| 294 | with open(output_ab_partitions_txt, 'w') as output: |
| 295 | for partition in sorted(output_ab_partitions): |
| 296 | output.write('%s\n' % partition) |
| 297 | |
| 298 | |
| 299 | def process_misc_info_txt( |
| 300 | system_target_files_temp_dir, |
| 301 | other_target_files_temp_dir, |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 302 | output_target_files_temp_dir, |
| 303 | system_misc_info_keys): |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 304 | """Perform special processing for META/misc_info.txt |
| 305 | |
| 306 | This function merges the contents of the META/misc_info.txt files from the |
| 307 | system directory and the other directory, placing the merged result in the |
| 308 | output directory. The precondition in that the files are already extracted. |
| 309 | The post condition is that the output META/misc_info.txt contains the merged |
| 310 | content. |
| 311 | |
| 312 | Args: |
| 313 | system_target_files_temp_dir: The name of a directory containing the |
| 314 | special items extracted from the system target files package. |
| 315 | |
| 316 | other_target_files_temp_dir: The name of a directory containing the |
| 317 | special items extracted from the other target files package. |
| 318 | |
| 319 | output_target_files_temp_dir: The name of a directory that will be used |
| 320 | to create the output target files package after all the special cases |
| 321 | are processed. |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 322 | |
| 323 | system_misc_info_keys: A list of keys to obtain from the system instance |
| 324 | of META/misc_info.txt. The remaining keys from the other instance. |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 325 | """ |
| 326 | |
| 327 | def read_helper(d): |
| 328 | misc_info_txt = os.path.join(d, 'META', 'misc_info.txt') |
| 329 | with open(misc_info_txt) as f: |
| 330 | return list(f.read().splitlines()) |
| 331 | |
| 332 | system_info_dict = common.LoadDictionaryFromLines( |
| 333 | read_helper(system_target_files_temp_dir)) |
| 334 | |
| 335 | # We take most of the misc info from the other target files. |
| 336 | |
| 337 | merged_info_dict = common.LoadDictionaryFromLines( |
| 338 | read_helper(other_target_files_temp_dir)) |
| 339 | |
| 340 | # Replace certain values in merged_info_dict with values from |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 341 | # system_info_dict. |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 342 | |
| 343 | for key in system_misc_info_keys: |
| 344 | merged_info_dict[key] = system_info_dict[key] |
| 345 | |
Daniel Norman | 19b9fe9 | 2019-03-19 14:48:02 -0700 | [diff] [blame^] | 346 | # Merge misc info keys used for Dynamic Partitions. |
| 347 | if (merged_info_dict.get('use_dynamic_partitions') == 'true') and ( |
| 348 | system_info_dict.get('use_dynamic_partitions') == 'true'): |
| 349 | merged_info_dict['dynamic_partition_list'] = '%s %s' % ( |
| 350 | system_info_dict.get('dynamic_partition_list', ''), |
| 351 | merged_info_dict.get('dynamic_partition_list', '')) |
| 352 | # Partition groups and group sizes are defined by the other (non-system) |
| 353 | # misc info file because these values may vary for each board that uses |
| 354 | # a shared system image. |
| 355 | for partition_group in merged_info_dict['super_partition_groups'].split(' '): |
| 356 | if ('super_%s_group_size' % partition_group) not in merged_info_dict: |
| 357 | raise common.ExternalError( |
| 358 | 'Other META/misc_info.txt does not contain required key ' |
| 359 | 'super_%s_group_size.' % partition_group) |
| 360 | key = 'super_%s_partition_list' % partition_group |
| 361 | merged_info_dict[key] = '%s %s' % ( |
| 362 | system_info_dict.get(key, ''), |
| 363 | merged_info_dict.get(key, '')) |
| 364 | |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 365 | output_misc_info_txt = os.path.join( |
| 366 | output_target_files_temp_dir, |
| 367 | 'META', 'misc_info.txt') |
| 368 | |
| 369 | sorted_keys = sorted(merged_info_dict.keys()) |
| 370 | |
| 371 | with open(output_misc_info_txt, 'w') as output: |
| 372 | for key in sorted_keys: |
| 373 | output.write('{}={}\n'.format(key, merged_info_dict[key])) |
| 374 | |
| 375 | |
| 376 | def process_file_contexts_bin(temp_dir, output_target_files_temp_dir): |
| 377 | """Perform special processing for META/file_contexts.bin. |
| 378 | |
| 379 | This function combines plat_file_contexts and vendor_file_contexts, which are |
| 380 | expected to already be extracted in temp_dir, to produce a merged |
| 381 | file_contexts.bin that will land in temp_dir at META/file_contexts.bin. |
| 382 | |
| 383 | Args: |
| 384 | temp_dir: The name of a scratch directory that this function can use for |
| 385 | intermediate files generated during processing. |
| 386 | |
| 387 | output_target_files_temp_dir: The name of the working directory that must |
| 388 | already contain plat_file_contexts and vendor_file_contexts (in the |
| 389 | appropriate sub directories), and to which META/file_contexts.bin will be |
| 390 | written. |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 391 | """ |
| 392 | |
| 393 | # To create a merged file_contexts.bin file, we use the system and vendor |
| 394 | # file contexts files as input, the m4 tool to combine them, the sorting tool |
| 395 | # to sort, and finally the sefcontext_compile tool to generate the final |
| 396 | # output. We currently omit a checkfc step since the files had been checked |
| 397 | # as part of the build. |
| 398 | |
| 399 | # The m4 step concatenates the two input files contexts files. Since m4 |
| 400 | # writes to stdout, we receive that into an array of bytes, and then write it |
| 401 | # to a file. |
| 402 | |
| 403 | # Collect the file contexts that we're going to combine from SYSTEM, VENDOR, |
| 404 | # PRODUCT, and ODM. We require SYSTEM and VENDOR, but others are optional. |
| 405 | |
| 406 | file_contexts_list = [] |
| 407 | |
| 408 | for partition in ['SYSTEM', 'VENDOR', 'PRODUCT', 'ODM']: |
| 409 | prefix = 'plat' if partition == 'SYSTEM' else partition.lower() |
| 410 | |
| 411 | file_contexts = os.path.join( |
| 412 | output_target_files_temp_dir, |
| 413 | partition, 'etc', 'selinux', prefix + '_file_contexts') |
| 414 | |
| 415 | mandatory = partition in ['SYSTEM', 'VENDOR'] |
| 416 | |
| 417 | if mandatory or os.path.isfile(file_contexts): |
| 418 | file_contexts_list.append(file_contexts) |
| 419 | else: |
| 420 | logger.warning('file not found: %s', file_contexts) |
| 421 | |
| 422 | command = ['m4', '--fatal-warnings', '-s'] + file_contexts_list |
| 423 | |
| 424 | merged_content = common.RunAndCheckOutput(command, verbose=False) |
| 425 | |
| 426 | merged_file_contexts_txt = os.path.join(temp_dir, 'merged_file_contexts.txt') |
| 427 | |
| 428 | with open(merged_file_contexts_txt, 'wb') as f: |
| 429 | f.write(merged_content) |
| 430 | |
| 431 | # The sort step sorts the concatenated file. |
| 432 | |
| 433 | sorted_file_contexts_txt = os.path.join(temp_dir, 'sorted_file_contexts.txt') |
| 434 | command = ['fc_sort', merged_file_contexts_txt, sorted_file_contexts_txt] |
Bill Peckham | 889b0c6 | 2019-02-21 18:53:37 -0800 | [diff] [blame] | 435 | common.RunAndWait(command, verbose=True) |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 436 | |
| 437 | # Finally, the compile step creates the final META/file_contexts.bin. |
| 438 | |
| 439 | file_contexts_bin = os.path.join( |
| 440 | output_target_files_temp_dir, |
| 441 | 'META', 'file_contexts.bin') |
| 442 | |
| 443 | command = [ |
| 444 | 'sefcontext_compile', |
| 445 | '-o', file_contexts_bin, |
| 446 | sorted_file_contexts_txt, |
| 447 | ] |
| 448 | |
Bill Peckham | 889b0c6 | 2019-02-21 18:53:37 -0800 | [diff] [blame] | 449 | common.RunAndWait(command, verbose=True) |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 450 | |
| 451 | |
| 452 | def process_special_cases( |
| 453 | temp_dir, |
| 454 | system_target_files_temp_dir, |
| 455 | other_target_files_temp_dir, |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 456 | output_target_files_temp_dir, |
| 457 | system_misc_info_keys): |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 458 | """Perform special-case processing for certain target files items. |
| 459 | |
| 460 | Certain files in the output target files package require special-case |
| 461 | processing. This function performs all that special-case processing. |
| 462 | |
| 463 | Args: |
| 464 | temp_dir: The name of a scratch directory that this function can use for |
| 465 | intermediate files generated during processing. |
| 466 | |
| 467 | system_target_files_temp_dir: The name of a directory containing the |
| 468 | special items extracted from the system target files package. |
| 469 | |
| 470 | other_target_files_temp_dir: The name of a directory containing the |
| 471 | special items extracted from the other target files package. |
| 472 | |
| 473 | output_target_files_temp_dir: The name of a directory that will be used |
| 474 | to create the output target files package after all the special cases |
| 475 | are processed. |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 476 | |
| 477 | system_misc_info_keys: A list of keys to obtain from the system instance |
| 478 | of META/misc_info.txt. The remaining keys from the other instance. |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 479 | """ |
| 480 | |
| 481 | process_ab_partitions_txt( |
| 482 | system_target_files_temp_dir=system_target_files_temp_dir, |
| 483 | other_target_files_temp_dir=other_target_files_temp_dir, |
| 484 | output_target_files_temp_dir=output_target_files_temp_dir) |
| 485 | |
| 486 | process_misc_info_txt( |
| 487 | system_target_files_temp_dir=system_target_files_temp_dir, |
| 488 | other_target_files_temp_dir=other_target_files_temp_dir, |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 489 | output_target_files_temp_dir=output_target_files_temp_dir, |
| 490 | system_misc_info_keys=system_misc_info_keys) |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 491 | |
Bill Peckham | 889b0c6 | 2019-02-21 18:53:37 -0800 | [diff] [blame] | 492 | process_file_contexts_bin( |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 493 | temp_dir=temp_dir, |
| 494 | output_target_files_temp_dir=output_target_files_temp_dir) |
| 495 | |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 496 | |
| 497 | def merge_target_files( |
| 498 | temp_dir, |
| 499 | system_target_files, |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 500 | system_item_list, |
| 501 | system_misc_info_keys, |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 502 | other_target_files, |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 503 | other_item_list, |
Daniel Norman | a4911da | 2019-03-15 14:36:21 -0700 | [diff] [blame] | 504 | output_target_files, |
| 505 | rebuild_recovery): |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 506 | """Merge two target files packages together. |
| 507 | |
| 508 | This function takes system and other target files packages as input, performs |
| 509 | various file extractions, special case processing, and finally creates a |
| 510 | merged zip archive as output. |
| 511 | |
| 512 | Args: |
| 513 | temp_dir: The name of a directory we use when we extract items from the |
| 514 | input target files packages, and also a scratch directory that we use for |
| 515 | temporary files. |
| 516 | |
| 517 | system_target_files: The name of the zip archive containing the system |
| 518 | partial target files package. |
| 519 | |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 520 | system_item_list: The list of items to extract from the partial system |
| 521 | target files package as is, meaning these items will land in the output |
| 522 | target files package exactly as they appear in the input partial system |
| 523 | target files package. |
| 524 | |
| 525 | system_misc_info_keys: The list of keys to obtain from the system instance |
| 526 | of META/misc_info.txt. The remaining keys from the other instance. |
| 527 | |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 528 | other_target_files: The name of the zip archive containing the other |
| 529 | partial target files package. |
| 530 | |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 531 | other_item_list: The list of items to extract from the partial other |
| 532 | target files package as is, meaning these items will land in the output |
| 533 | target files package exactly as they appear in the input partial other |
| 534 | target files package. |
| 535 | |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 536 | output_target_files: The name of the output zip archive target files |
| 537 | package created by merging system and other. |
Daniel Norman | a4911da | 2019-03-15 14:36:21 -0700 | [diff] [blame] | 538 | |
| 539 | rebuild_recovery: If true, rebuild the recovery patch used by non-A/B |
| 540 | devices and write it to the system image. |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 541 | """ |
| 542 | |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 543 | logger.info( |
| 544 | 'starting: merge system %s and other %s into output %s', |
| 545 | system_target_files, |
| 546 | other_target_files, |
| 547 | output_target_files) |
| 548 | |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 549 | # Create directory names that we'll use when we extract files from system, |
| 550 | # and other, and for zipping the final output. |
| 551 | |
| 552 | system_target_files_temp_dir = os.path.join(temp_dir, 'system') |
| 553 | other_target_files_temp_dir = os.path.join(temp_dir, 'other') |
| 554 | output_target_files_temp_dir = os.path.join(temp_dir, 'output') |
| 555 | |
| 556 | # Extract "as is" items from the input system partial target files package. |
| 557 | # We extract them directly into the output temporary directory since the |
| 558 | # items do not need special case processing. |
| 559 | |
Bill Peckham | 889b0c6 | 2019-02-21 18:53:37 -0800 | [diff] [blame] | 560 | extract_items( |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 561 | target_files=system_target_files, |
| 562 | target_files_temp_dir=output_target_files_temp_dir, |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 563 | extract_item_list=system_item_list) |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 564 | |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 565 | # Extract "as is" items from the input other partial target files package. We |
| 566 | # extract them directly into the output temporary directory since the items |
| 567 | # do not need special case processing. |
| 568 | |
Bill Peckham | 889b0c6 | 2019-02-21 18:53:37 -0800 | [diff] [blame] | 569 | extract_items( |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 570 | target_files=other_target_files, |
| 571 | target_files_temp_dir=output_target_files_temp_dir, |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 572 | extract_item_list=other_item_list) |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 573 | |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 574 | # Extract "special" items from the input system partial target files package. |
| 575 | # We extract these items to different directory since they require special |
| 576 | # processing before they will end up in the output directory. |
| 577 | |
Bill Peckham | 889b0c6 | 2019-02-21 18:53:37 -0800 | [diff] [blame] | 578 | extract_items( |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 579 | target_files=system_target_files, |
| 580 | target_files_temp_dir=system_target_files_temp_dir, |
| 581 | extract_item_list=system_extract_special_item_list) |
| 582 | |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 583 | # Extract "special" items from the input other partial target files package. |
| 584 | # We extract these items to different directory since they require special |
| 585 | # processing before they will end up in the output directory. |
| 586 | |
Bill Peckham | 889b0c6 | 2019-02-21 18:53:37 -0800 | [diff] [blame] | 587 | extract_items( |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 588 | target_files=other_target_files, |
| 589 | target_files_temp_dir=other_target_files_temp_dir, |
| 590 | extract_item_list=other_extract_special_item_list) |
| 591 | |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 592 | # Now that the temporary directories contain all the extracted files, perform |
| 593 | # special case processing on any items that need it. After this function |
| 594 | # completes successfully, all the files we need to create the output target |
| 595 | # files package are in place. |
| 596 | |
Bill Peckham | 889b0c6 | 2019-02-21 18:53:37 -0800 | [diff] [blame] | 597 | process_special_cases( |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 598 | temp_dir=temp_dir, |
| 599 | system_target_files_temp_dir=system_target_files_temp_dir, |
| 600 | other_target_files_temp_dir=other_target_files_temp_dir, |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 601 | output_target_files_temp_dir=output_target_files_temp_dir, |
| 602 | system_misc_info_keys=system_misc_info_keys) |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 603 | |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 604 | # Regenerate IMAGES in the temporary directory. |
| 605 | |
Daniel Norman | a4911da | 2019-03-15 14:36:21 -0700 | [diff] [blame] | 606 | add_img_args = ['--verbose'] |
| 607 | if rebuild_recovery: |
| 608 | add_img_args.append('--rebuild_recovery') |
| 609 | add_img_args.append(output_target_files_temp_dir) |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 610 | |
| 611 | add_img_to_target_files.main(add_img_args) |
| 612 | |
| 613 | # Finally, create the output target files zip archive. |
| 614 | |
| 615 | output_zip = os.path.abspath(output_target_files) |
| 616 | output_target_files_list = os.path.join(temp_dir, 'output.list') |
| 617 | output_target_files_meta_dir = os.path.join( |
| 618 | output_target_files_temp_dir, 'META') |
| 619 | |
| 620 | command = [ |
| 621 | 'find', |
| 622 | output_target_files_meta_dir, |
| 623 | ] |
| 624 | # TODO(bpeckham): sort this to be more like build. |
| 625 | meta_content = common.RunAndCheckOutput(command, verbose=False) |
| 626 | command = [ |
| 627 | 'find', |
| 628 | output_target_files_temp_dir, |
| 629 | '-path', |
| 630 | output_target_files_meta_dir, |
| 631 | '-prune', |
| 632 | '-o', |
| 633 | '-print' |
| 634 | ] |
| 635 | # TODO(bpeckham): sort this to be more like build. |
| 636 | other_content = common.RunAndCheckOutput(command, verbose=False) |
| 637 | |
| 638 | with open(output_target_files_list, 'wb') as f: |
| 639 | f.write(meta_content) |
| 640 | f.write(other_content) |
| 641 | |
| 642 | command = [ |
Bill Peckham | f753e15 | 2019-02-19 18:02:46 -0800 | [diff] [blame] | 643 | 'soong_zip', |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 644 | '-d', |
| 645 | '-o', output_zip, |
| 646 | '-C', output_target_files_temp_dir, |
| 647 | '-l', output_target_files_list, |
| 648 | ] |
| 649 | logger.info('creating %s', output_target_files) |
Bill Peckham | 889b0c6 | 2019-02-21 18:53:37 -0800 | [diff] [blame] | 650 | common.RunAndWait(command, verbose=True) |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 651 | |
| 652 | |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 653 | def call_func_with_temp_dir(func, keep_tmp): |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 654 | """Manage the creation and cleanup of the temporary directory. |
| 655 | |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 656 | This function calls the given function after first creating a temporary |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 657 | directory. It also cleans up the temporary directory. |
| 658 | |
| 659 | Args: |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 660 | func: The function to call. Should accept one parameter, the path to |
| 661 | the temporary directory. |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 662 | |
| 663 | keep_tmp: Keep the temporary directory after processing is complete. |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 664 | """ |
| 665 | |
| 666 | # Create a temporary directory. This will serve as the parent of directories |
| 667 | # we use when we extract items from the input target files packages, and also |
| 668 | # a scratch directory that we use for temporary files. |
| 669 | |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 670 | temp_dir = common.MakeTempDir(prefix='merge_target_files_') |
| 671 | |
| 672 | try: |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 673 | func(temp_dir) |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 674 | except: |
| 675 | raise |
| 676 | finally: |
| 677 | if keep_tmp: |
| 678 | logger.info('keeping %s', temp_dir) |
| 679 | else: |
| 680 | common.Cleanup() |
| 681 | |
| 682 | |
| 683 | def main(): |
| 684 | """The main function. |
| 685 | |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 686 | Process command line arguments, then call merge_target_files to |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 687 | perform the heavy lifting. |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 688 | """ |
| 689 | |
| 690 | common.InitLogging() |
| 691 | |
Bill Peckham | f753e15 | 2019-02-19 18:02:46 -0800 | [diff] [blame] | 692 | def option_handler(o, a): |
| 693 | if o == '--system-target-files': |
| 694 | OPTIONS.system_target_files = a |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 695 | elif o == '--system-item-list': |
| 696 | OPTIONS.system_item_list = a |
| 697 | elif o == '--system-misc-info-keys': |
| 698 | OPTIONS.system_misc_info_keys = a |
Bill Peckham | f753e15 | 2019-02-19 18:02:46 -0800 | [diff] [blame] | 699 | elif o == '--other-target-files': |
| 700 | OPTIONS.other_target_files = a |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 701 | elif o == '--other-item-list': |
| 702 | OPTIONS.other_item_list = a |
Bill Peckham | f753e15 | 2019-02-19 18:02:46 -0800 | [diff] [blame] | 703 | elif o == '--output-target-files': |
| 704 | OPTIONS.output_target_files = a |
Daniel Norman | a4911da | 2019-03-15 14:36:21 -0700 | [diff] [blame] | 705 | elif o == '--rebuild_recovery': |
| 706 | OPTIONS.rebuild_recovery = True |
Bill Peckham | f753e15 | 2019-02-19 18:02:46 -0800 | [diff] [blame] | 707 | elif o == '--keep_tmp': |
| 708 | OPTIONS.keep_tmp = True |
| 709 | else: |
| 710 | return False |
| 711 | return True |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 712 | |
Bill Peckham | f753e15 | 2019-02-19 18:02:46 -0800 | [diff] [blame] | 713 | args = common.ParseOptions( |
| 714 | sys.argv[1:], __doc__, |
| 715 | extra_long_opts=[ |
| 716 | 'system-target-files=', |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 717 | 'system-item-list=', |
| 718 | 'system-misc-info-keys=', |
Bill Peckham | f753e15 | 2019-02-19 18:02:46 -0800 | [diff] [blame] | 719 | 'other-target-files=', |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 720 | 'other-item-list=', |
Bill Peckham | f753e15 | 2019-02-19 18:02:46 -0800 | [diff] [blame] | 721 | 'output-target-files=', |
Daniel Norman | a4911da | 2019-03-15 14:36:21 -0700 | [diff] [blame] | 722 | 'rebuild_recovery', |
Bill Peckham | f753e15 | 2019-02-19 18:02:46 -0800 | [diff] [blame] | 723 | "keep_tmp", |
| 724 | ], |
| 725 | extra_option_handler=option_handler) |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 726 | |
Bill Peckham | 889b0c6 | 2019-02-21 18:53:37 -0800 | [diff] [blame] | 727 | if (len(args) != 0 or |
Bill Peckham | f753e15 | 2019-02-19 18:02:46 -0800 | [diff] [blame] | 728 | OPTIONS.system_target_files is None or |
| 729 | OPTIONS.other_target_files is None or |
| 730 | OPTIONS.output_target_files is None): |
| 731 | common.Usage(__doc__) |
Bill Peckham | 889b0c6 | 2019-02-21 18:53:37 -0800 | [diff] [blame] | 732 | sys.exit(1) |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 733 | |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 734 | if OPTIONS.system_item_list: |
| 735 | system_item_list = read_config_list(OPTIONS.system_item_list) |
| 736 | else: |
| 737 | system_item_list = default_system_item_list |
| 738 | |
| 739 | if OPTIONS.system_misc_info_keys: |
| 740 | system_misc_info_keys = read_config_list(OPTIONS.system_misc_info_keys) |
| 741 | else: |
| 742 | system_misc_info_keys = default_system_misc_info_keys |
| 743 | |
| 744 | if OPTIONS.other_item_list: |
| 745 | other_item_list = read_config_list(OPTIONS.other_item_list) |
| 746 | else: |
| 747 | other_item_list = default_other_item_list |
| 748 | |
Daniel Norman | e596452 | 2019-03-19 10:32:03 -0700 | [diff] [blame] | 749 | if not validate_config_lists( |
| 750 | system_item_list=system_item_list, |
Daniel Norman | 19b9fe9 | 2019-03-19 14:48:02 -0700 | [diff] [blame^] | 751 | system_misc_info_keys=system_misc_info_keys, |
Daniel Norman | e596452 | 2019-03-19 10:32:03 -0700 | [diff] [blame] | 752 | other_item_list=other_item_list): |
| 753 | sys.exit(1) |
| 754 | |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 755 | call_func_with_temp_dir( |
| 756 | lambda temp_dir: merge_target_files( |
| 757 | temp_dir=temp_dir, |
| 758 | system_target_files=OPTIONS.system_target_files, |
| 759 | system_item_list=system_item_list, |
| 760 | system_misc_info_keys=system_misc_info_keys, |
| 761 | other_target_files=OPTIONS.other_target_files, |
| 762 | other_item_list=other_item_list, |
Daniel Norman | a4911da | 2019-03-15 14:36:21 -0700 | [diff] [blame] | 763 | output_target_files=OPTIONS.output_target_files, |
| 764 | rebuild_recovery=OPTIONS.rebuild_recovery), |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 765 | OPTIONS.keep_tmp) |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 766 | |
| 767 | |
| 768 | if __name__ == '__main__': |
Bill Peckham | 889b0c6 | 2019-02-21 18:53:37 -0800 | [diff] [blame] | 769 | main() |