From c65a0545dde5b94bf5c622fbc292bcd2e8cb393d Mon Sep 17 00:00:00 2001 From: Yifan Hong Date: Thu, 7 Jan 2021 14:21:01 -0800 Subject: [PATCH] Move GetBootImageTimestamp to common. Test: TH Bug: 162623577 Change-Id: Icbd8b07dce91006a1abca1f0dc40726de8a10219 --- tools/releasetools/common.py | 55 ++++++++++++++++++++++++++++++ tools/releasetools/ota_utils.py | 59 +-------------------------------- 2 files changed, 56 insertions(+), 58 deletions(-) diff --git a/tools/releasetools/common.py b/tools/releasetools/common.py index bae0b20f21..c4240c485b 100644 --- a/tools/releasetools/common.py +++ b/tools/releasetools/common.py @@ -128,6 +128,9 @@ PARTITIONS_WITH_CARE_MAP = [ 'odm_dlkm', ] +# See sysprop.mk. If file is moved, add new search paths here; don't remove +# existing search paths. +RAMDISK_BUILD_PROP_REL_PATHS = ['system/etc/ramdisk/build.prop'] class ErrorCode(object): """Define error_codes for failures that happen during the actual @@ -3569,3 +3572,55 @@ class DynamicPartitionsDifference(object): comment('Move partition %s from default to %s' % (p, u.tgt_group)) append('move %s %s' % (p, u.tgt_group)) + + +def GetBootImageTimestamp(boot_img): + """ + Get timestamp from ramdisk within the boot image + + Args: + boot_img: the boot image file. Ramdisk must be compressed with lz4 format. + + Return: + An integer that corresponds to the timestamp of the boot image, or None + if file has unknown format. Raise exception if an unexpected error has + occurred. + """ + + tmp_dir = MakeTempDir('boot_', suffix='.img') + try: + RunAndCheckOutput(['unpack_bootimg', '--boot_img', boot_img, '--out', tmp_dir]) + ramdisk = os.path.join(tmp_dir, 'ramdisk') + if not os.path.isfile(ramdisk): + logger.warning('Unable to get boot image timestamp: no ramdisk in boot') + return None + uncompressed_ramdisk = os.path.join(tmp_dir, 'uncompressed_ramdisk') + RunAndCheckOutput(['lz4', '-d', ramdisk, uncompressed_ramdisk]) + + abs_uncompressed_ramdisk = os.path.abspath(uncompressed_ramdisk) + extracted_ramdisk = MakeTempDir('extracted_ramdisk') + # Use "toybox cpio" instead of "cpio" because the latter invokes cpio from + # the host environment. + RunAndCheckOutput(['toybox', 'cpio', '-F', abs_uncompressed_ramdisk, '-i'], + cwd=extracted_ramdisk) + + prop_file = None + for search_path in RAMDISK_BUILD_PROP_REL_PATHS: + prop_file = os.path.join(extracted_ramdisk, search_path) + if os.path.isfile(prop_file): + break + logger.warning('Unable to get boot image timestamp: no %s in ramdisk', search_path) + + if not prop_file: + return None + + props = PartitionBuildProps.FromBuildPropFile('boot', prop_file) + timestamp = props.GetProp('ro.bootimage.build.date.utc') + if timestamp: + return int(timestamp) + logger.warning('Unable to get boot image timestamp: ro.bootimage.build.date.utc is undefined') + return None + + except ExternalError as e: + logger.warning('Unable to get boot image timestamp: %s', e) + return None diff --git a/tools/releasetools/ota_utils.py b/tools/releasetools/ota_utils.py index cb0f6e6ba5..be6a13495b 100644 --- a/tools/releasetools/ota_utils.py +++ b/tools/releasetools/ota_utils.py @@ -21,8 +21,7 @@ import zipfile import ota_metadata_pb2 from common import (ZipDelete, ZipClose, OPTIONS, MakeTempFile, ZipWriteStr, BuildInfo, LoadDictionaryFromFile, - SignFile, PARTITIONS_WITH_CARE_MAP, PartitionBuildProps, - MakeTempDir, RunAndCheckOutput, ExternalError) + SignFile, PARTITIONS_WITH_CARE_MAP, PartitionBuildProps) logger = logging.getLogger(__name__) @@ -41,10 +40,6 @@ METADATA_NAME = 'META-INF/com/android/metadata' METADATA_PROTO_NAME = 'META-INF/com/android/metadata.pb' UNZIP_PATTERN = ['IMAGES/*', 'META/*', 'OTA/*', 'RADIO/*'] -# See sysprop.mk. If file is moved, add new search paths here; don't remove -# existing search paths. -RAMDISK_BUILD_PROP_REL_PATHS = ['system/etc/ramdisk/build.prop'] - def FinalizeMetadata(metadata, input_file, output_file, needed_property_files): """Finalizes the metadata and signs an A/B OTA package. @@ -567,55 +562,3 @@ def SignOutput(temp_zip_name, output_zip_name): SignFile(temp_zip_name, output_zip_name, OPTIONS.package_key, pw, whole_file=True) - - -def GetBootImageTimestamp(boot_img): - """ - Get timestamp from ramdisk within the boot image - - Args: - boot_img: the boot image file. Ramdisk must be compressed with lz4 format. - - Return: - An integer that corresponds to the timestamp of the boot image, or None - if file has unknown format. Raise exception if an unexpected error has - occurred. - """ - - tmp_dir = MakeTempDir('boot_', suffix='.img') - try: - RunAndCheckOutput(['unpack_bootimg', '--boot_img', boot_img, '--out', tmp_dir]) - ramdisk = os.path.join(tmp_dir, 'ramdisk') - if not os.path.isfile(ramdisk): - logger.warning('Unable to get boot image timestamp: no ramdisk in boot') - return None - uncompressed_ramdisk = os.path.join(tmp_dir, 'uncompressed_ramdisk') - RunAndCheckOutput(['lz4', '-d', ramdisk, uncompressed_ramdisk]) - - abs_uncompressed_ramdisk = os.path.abspath(uncompressed_ramdisk) - extracted_ramdisk = MakeTempDir('extracted_ramdisk') - # Use "toybox cpio" instead of "cpio" because the latter invokes cpio from - # the host environment. - RunAndCheckOutput(['toybox', 'cpio', '-F', abs_uncompressed_ramdisk, '-i'], - cwd=extracted_ramdisk) - - prop_file = None - for search_path in RAMDISK_BUILD_PROP_REL_PATHS: - prop_file = os.path.join(extracted_ramdisk, search_path) - if os.path.isfile(prop_file): - break - logger.warning('Unable to get boot image timestamp: no %s in ramdisk', search_path) - - if not prop_file: - return None - - props = PartitionBuildProps.FromBuildPropFile('boot', prop_file) - timestamp = props.GetProp('ro.bootimage.build.date.utc') - if timestamp: - return int(timestamp) - logger.warning('Unable to get boot image timestamp: ro.bootimage.build.date.utc is undefined') - return None - - except ExternalError as e: - logger.warning('Unable to get boot image timestamp: %s', e) - return None