Merge changes from topic "common.GetBootImageTimestamp"
* changes: Load boot build props to info_dict properly. Add GetBootImageBuildProp. Split PARTITIONS_WITH_BUILD_PROP from *_WITH_CARE_MAP Move GetBootImageTimestamp to common.
This commit is contained in:
@@ -128,6 +128,12 @@ PARTITIONS_WITH_CARE_MAP = [
|
|||||||
'odm_dlkm',
|
'odm_dlkm',
|
||||||
]
|
]
|
||||||
|
|
||||||
|
# Partitions with a build.prop file
|
||||||
|
PARTITIONS_WITH_BUILD_PROP = PARTITIONS_WITH_CARE_MAP + ['boot']
|
||||||
|
|
||||||
|
# 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):
|
class ErrorCode(object):
|
||||||
"""Define error_codes for failures that happen during the actual
|
"""Define error_codes for failures that happen during the actual
|
||||||
@@ -417,7 +423,7 @@ class BuildInfo(object):
|
|||||||
"3.2.2. Build Parameters.".format(fingerprint))
|
"3.2.2. Build Parameters.".format(fingerprint))
|
||||||
|
|
||||||
self._partition_fingerprints = {}
|
self._partition_fingerprints = {}
|
||||||
for partition in PARTITIONS_WITH_CARE_MAP:
|
for partition in PARTITIONS_WITH_BUILD_PROP:
|
||||||
try:
|
try:
|
||||||
fingerprint = self.CalculatePartitionFingerprint(partition)
|
fingerprint = self.CalculatePartitionFingerprint(partition)
|
||||||
check_fingerprint(fingerprint)
|
check_fingerprint(fingerprint)
|
||||||
@@ -425,7 +431,7 @@ class BuildInfo(object):
|
|||||||
except ExternalError:
|
except ExternalError:
|
||||||
continue
|
continue
|
||||||
if "system" in self._partition_fingerprints:
|
if "system" in self._partition_fingerprints:
|
||||||
# system_other is not included in PARTITIONS_WITH_CARE_MAP, but does
|
# system_other is not included in PARTITIONS_WITH_BUILD_PROP, but does
|
||||||
# need a fingerprint when creating the image.
|
# need a fingerprint when creating the image.
|
||||||
self._partition_fingerprints[
|
self._partition_fingerprints[
|
||||||
"system_other"] = self._partition_fingerprints["system"]
|
"system_other"] = self._partition_fingerprints["system"]
|
||||||
@@ -473,12 +479,16 @@ class BuildInfo(object):
|
|||||||
|
|
||||||
def GetPartitionBuildProp(self, prop, partition):
|
def GetPartitionBuildProp(self, prop, partition):
|
||||||
"""Returns the inquired build property for the provided partition."""
|
"""Returns the inquired build property for the provided partition."""
|
||||||
|
|
||||||
|
# Boot image uses ro.[product.]bootimage instead of boot.
|
||||||
|
prop_partition = "bootimage" if partition == "boot" else partition
|
||||||
|
|
||||||
# If provided a partition for this property, only look within that
|
# If provided a partition for this property, only look within that
|
||||||
# partition's build.prop.
|
# partition's build.prop.
|
||||||
if prop in BuildInfo._RO_PRODUCT_RESOLVE_PROPS:
|
if prop in BuildInfo._RO_PRODUCT_RESOLVE_PROPS:
|
||||||
prop = prop.replace("ro.product", "ro.product.{}".format(partition))
|
prop = prop.replace("ro.product", "ro.product.{}".format(prop_partition))
|
||||||
else:
|
else:
|
||||||
prop = prop.replace("ro.", "ro.{}.".format(partition))
|
prop = prop.replace("ro.", "ro.{}.".format(prop_partition))
|
||||||
|
|
||||||
prop_val = self._GetRawBuildProp(prop, partition)
|
prop_val = self._GetRawBuildProp(prop, partition)
|
||||||
if prop_val is not None:
|
if prop_val is not None:
|
||||||
@@ -649,6 +659,20 @@ def ReadFromInputFile(input_file, fn):
|
|||||||
raise KeyError(fn)
|
raise KeyError(fn)
|
||||||
|
|
||||||
|
|
||||||
|
def ExtractFromInputFile(input_file, fn):
|
||||||
|
"""Extracts the contents of fn from input zipfile or directory into a file."""
|
||||||
|
if isinstance(input_file, zipfile.ZipFile):
|
||||||
|
tmp_file = MakeTempFile(os.path.basename(fn))
|
||||||
|
with open(tmp_file, 'w') as f:
|
||||||
|
f.write(input_file.read(fn))
|
||||||
|
return tmp_file
|
||||||
|
else:
|
||||||
|
file = os.path.join(input_file, *fn.split("/"))
|
||||||
|
if not os.path.exists(file):
|
||||||
|
raise KeyError(fn)
|
||||||
|
return file
|
||||||
|
|
||||||
|
|
||||||
def LoadInfoDict(input_file, repacking=False):
|
def LoadInfoDict(input_file, repacking=False):
|
||||||
"""Loads the key/value pairs from the given input target_files.
|
"""Loads the key/value pairs from the given input target_files.
|
||||||
|
|
||||||
@@ -753,7 +777,7 @@ def LoadInfoDict(input_file, repacking=False):
|
|||||||
|
|
||||||
# Tries to load the build props for all partitions with care_map, including
|
# Tries to load the build props for all partitions with care_map, including
|
||||||
# system and vendor.
|
# system and vendor.
|
||||||
for partition in PARTITIONS_WITH_CARE_MAP:
|
for partition in PARTITIONS_WITH_BUILD_PROP:
|
||||||
partition_prop = "{}.build.prop".format(partition)
|
partition_prop = "{}.build.prop".format(partition)
|
||||||
d[partition_prop] = PartitionBuildProps.FromInputFile(
|
d[partition_prop] = PartitionBuildProps.FromInputFile(
|
||||||
input_file, partition)
|
input_file, partition)
|
||||||
@@ -763,7 +787,7 @@ def LoadInfoDict(input_file, repacking=False):
|
|||||||
# hash / hashtree footers.
|
# hash / hashtree footers.
|
||||||
if d.get("avb_enable") == "true":
|
if d.get("avb_enable") == "true":
|
||||||
build_info = BuildInfo(d)
|
build_info = BuildInfo(d)
|
||||||
for partition in PARTITIONS_WITH_CARE_MAP:
|
for partition in PARTITIONS_WITH_BUILD_PROP:
|
||||||
fingerprint = build_info.GetPartitionFingerprint(partition)
|
fingerprint = build_info.GetPartitionFingerprint(partition)
|
||||||
if fingerprint:
|
if fingerprint:
|
||||||
d["avb_{}_salt".format(partition)] = sha256(fingerprint.encode()).hexdigest()
|
d["avb_{}_salt".format(partition)] = sha256(fingerprint.encode()).hexdigest()
|
||||||
@@ -839,6 +863,39 @@ class PartitionBuildProps(object):
|
|||||||
@staticmethod
|
@staticmethod
|
||||||
def FromInputFile(input_file, name, placeholder_values=None):
|
def FromInputFile(input_file, name, placeholder_values=None):
|
||||||
"""Loads the build.prop file and builds the attributes."""
|
"""Loads the build.prop file and builds the attributes."""
|
||||||
|
|
||||||
|
if name == "boot":
|
||||||
|
data = PartitionBuildProps._ReadBootPropFile(input_file)
|
||||||
|
else:
|
||||||
|
data = PartitionBuildProps._ReadPartitionPropFile(input_file, name)
|
||||||
|
|
||||||
|
props = PartitionBuildProps(input_file, name, placeholder_values)
|
||||||
|
props._LoadBuildProp(data)
|
||||||
|
return props
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _ReadBootPropFile(input_file):
|
||||||
|
"""
|
||||||
|
Read build.prop for boot image from input_file.
|
||||||
|
Return empty string if not found.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
boot_img = ExtractFromInputFile(input_file, 'IMAGES/boot.img')
|
||||||
|
except KeyError:
|
||||||
|
logger.warning('Failed to read IMAGES/boot.img')
|
||||||
|
return ''
|
||||||
|
prop_file = GetBootImageBuildProp(boot_img)
|
||||||
|
if prop_file is None:
|
||||||
|
return ''
|
||||||
|
with open(prop_file) as f:
|
||||||
|
return f.read().decode()
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _ReadPartitionPropFile(input_file, name):
|
||||||
|
"""
|
||||||
|
Read build.prop for name from input_file.
|
||||||
|
Return empty string if not found.
|
||||||
|
"""
|
||||||
data = ''
|
data = ''
|
||||||
for prop_file in ['{}/etc/build.prop'.format(name.upper()),
|
for prop_file in ['{}/etc/build.prop'.format(name.upper()),
|
||||||
'{}/build.prop'.format(name.upper())]:
|
'{}/build.prop'.format(name.upper())]:
|
||||||
@@ -847,10 +904,7 @@ class PartitionBuildProps(object):
|
|||||||
break
|
break
|
||||||
except KeyError:
|
except KeyError:
|
||||||
logger.warning('Failed to read %s', prop_file)
|
logger.warning('Failed to read %s', prop_file)
|
||||||
|
return data
|
||||||
props = PartitionBuildProps(input_file, name, placeholder_values)
|
|
||||||
props._LoadBuildProp(data)
|
|
||||||
return props
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def FromBuildPropFile(name, build_prop_file):
|
def FromBuildPropFile(name, build_prop_file):
|
||||||
@@ -3589,3 +3643,76 @@ class DynamicPartitionsDifference(object):
|
|||||||
comment('Move partition %s from default to %s' %
|
comment('Move partition %s from default to %s' %
|
||||||
(p, u.tgt_group))
|
(p, u.tgt_group))
|
||||||
append('move %s %s' % (p, u.tgt_group))
|
append('move %s %s' % (p, u.tgt_group))
|
||||||
|
|
||||||
|
|
||||||
|
def GetBootImageBuildProp(boot_img):
|
||||||
|
"""
|
||||||
|
Get build.prop from ramdisk within the boot image
|
||||||
|
|
||||||
|
Args:
|
||||||
|
boot_img: the boot image file. Ramdisk must be compressed with lz4 format.
|
||||||
|
|
||||||
|
Return:
|
||||||
|
An extracted file that stores properties in the boot image.
|
||||||
|
"""
|
||||||
|
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)
|
||||||
|
|
||||||
|
return prop_file
|
||||||
|
|
||||||
|
except ExternalError as e:
|
||||||
|
logger.warning('Unable to get boot image build props: %s', e)
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
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.
|
||||||
|
"""
|
||||||
|
prop_file = GetBootImageBuildProp(boot_img)
|
||||||
|
if not prop_file:
|
||||||
|
return None
|
||||||
|
|
||||||
|
props = PartitionBuildProps.FromBuildPropFile('boot', prop_file)
|
||||||
|
if props is None:
|
||||||
|
return None
|
||||||
|
|
||||||
|
try:
|
||||||
|
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
|
||||||
|
@@ -21,8 +21,7 @@ import zipfile
|
|||||||
import ota_metadata_pb2
|
import ota_metadata_pb2
|
||||||
from common import (ZipDelete, ZipClose, OPTIONS, MakeTempFile,
|
from common import (ZipDelete, ZipClose, OPTIONS, MakeTempFile,
|
||||||
ZipWriteStr, BuildInfo, LoadDictionaryFromFile,
|
ZipWriteStr, BuildInfo, LoadDictionaryFromFile,
|
||||||
SignFile, PARTITIONS_WITH_CARE_MAP, PartitionBuildProps,
|
SignFile, PARTITIONS_WITH_BUILD_PROP, PartitionBuildProps)
|
||||||
MakeTempDir, RunAndCheckOutput, ExternalError)
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
@@ -41,10 +40,6 @@ METADATA_NAME = 'META-INF/com/android/metadata'
|
|||||||
METADATA_PROTO_NAME = 'META-INF/com/android/metadata.pb'
|
METADATA_PROTO_NAME = 'META-INF/com/android/metadata.pb'
|
||||||
UNZIP_PATTERN = ['IMAGES/*', 'META/*', 'OTA/*', 'RADIO/*']
|
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):
|
def FinalizeMetadata(metadata, input_file, output_file, needed_property_files):
|
||||||
"""Finalizes the metadata and signs an A/B OTA package.
|
"""Finalizes the metadata and signs an A/B OTA package.
|
||||||
|
|
||||||
@@ -179,7 +174,7 @@ def UpdateDeviceState(device_state, build_info, boot_variable_values,
|
|||||||
# delta_generator will error out on unused timestamps,
|
# delta_generator will error out on unused timestamps,
|
||||||
# so only generate timestamps for dynamic partitions
|
# so only generate timestamps for dynamic partitions
|
||||||
# used in OTA update.
|
# used in OTA update.
|
||||||
for partition in sorted(set(PARTITIONS_WITH_CARE_MAP) & ab_partitions):
|
for partition in sorted(set(PARTITIONS_WITH_BUILD_PROP) & ab_partitions):
|
||||||
partition_prop = build_info.info_dict.get(
|
partition_prop = build_info.info_dict.get(
|
||||||
'{}.build.prop'.format(partition))
|
'{}.build.prop'.format(partition))
|
||||||
# Skip if the partition is missing, or it doesn't have a build.prop
|
# Skip if the partition is missing, or it doesn't have a build.prop
|
||||||
@@ -365,7 +360,7 @@ def ComputeRuntimeBuildInfos(default_build_info, boot_variable_values):
|
|||||||
# Reload the info_dict as some build properties may change their values
|
# Reload the info_dict as some build properties may change their values
|
||||||
# based on the value of ro.boot* properties.
|
# based on the value of ro.boot* properties.
|
||||||
info_dict = copy.deepcopy(default_build_info.info_dict)
|
info_dict = copy.deepcopy(default_build_info.info_dict)
|
||||||
for partition in PARTITIONS_WITH_CARE_MAP:
|
for partition in PARTITIONS_WITH_BUILD_PROP:
|
||||||
partition_prop_key = "{}.build.prop".format(partition)
|
partition_prop_key = "{}.build.prop".format(partition)
|
||||||
input_file = info_dict[partition_prop_key].input_file
|
input_file = info_dict[partition_prop_key].input_file
|
||||||
if isinstance(input_file, zipfile.ZipFile):
|
if isinstance(input_file, zipfile.ZipFile):
|
||||||
@@ -567,55 +562,3 @@ def SignOutput(temp_zip_name, output_zip_name):
|
|||||||
|
|
||||||
SignFile(temp_zip_name, output_zip_name, OPTIONS.package_key, pw,
|
SignFile(temp_zip_name, output_zip_name, OPTIONS.package_key, pw,
|
||||||
whole_file=True)
|
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
|
|
||||||
|
Reference in New Issue
Block a user