Merge "releasetools: GetCareMap supports non-sparse image."

This commit is contained in:
Tao Bao 2019-04-26 22:59:30 +00:00 committed by Gerrit Code Review
commit 0376d7dcbb
2 changed files with 50 additions and 15 deletions

View File

@ -113,22 +113,33 @@ def GetCareMap(which, imgname):
Returns: Returns:
(which, care_map_ranges): care_map_ranges is the raw string of the care_map (which, care_map_ranges): care_map_ranges is the raw string of the care_map
RangeSet. RangeSet; or an empty list.
""" """
assert which in common.PARTITIONS_WITH_CARE_MAP assert which in common.PARTITIONS_WITH_CARE_MAP
simg = sparse_img.SparseImage(imgname) # which + "_image_size" contains the size that the actual filesystem image
care_map_ranges = simg.care_map # resides in, which is all that needs to be verified. The additional blocks in
size_key = which + "_image_size" # the image file contain verity metadata, by reading which would trigger
image_size = OPTIONS.info_dict.get(size_key) # invalid reads.
if image_size: image_size = OPTIONS.info_dict.get(which + "_image_size")
# excludes the verity metadata blocks of the given image. When AVB is enabled, if not image_size:
# this size is the max image size returned by the AVB tool return []
image_blocks = int(image_size) / 4096 - 1
assert image_blocks > 0, "blocks for {} must be positive".format(which) image_blocks = int(image_size) / 4096 - 1
care_map_ranges = care_map_ranges.intersect( assert image_blocks > 0, "blocks for {} must be positive".format(which)
# For sparse images, we will only check the blocks that are listed in the care
# map, i.e. the ones with meaningful data.
if "extfs_sparse_flag" in OPTIONS.info_dict:
simg = sparse_img.SparseImage(imgname)
care_map_ranges = simg.care_map.intersect(
rangelib.RangeSet("0-{}".format(image_blocks))) rangelib.RangeSet("0-{}".format(image_blocks)))
# Otherwise for non-sparse images, we read all the blocks in the filesystem
# image.
else:
care_map_ranges = rangelib.RangeSet("0-{}".format(image_blocks))
return [which, care_map_ranges.to_string_raw()] return [which, care_map_ranges.to_string_raw()]

View File

@ -123,6 +123,9 @@ class AddImagesToTargetFilesTest(test_utils.ReleaseToolsTestCase):
def _test_AddCareMapForAbOta(): def _test_AddCareMapForAbOta():
"""Helper function to set up the test for test_AddCareMapForAbOta().""" """Helper function to set up the test for test_AddCareMapForAbOta()."""
OPTIONS.info_dict = { OPTIONS.info_dict = {
'extfs_sparse_flag' : '-s',
'system_image_size' : 65536,
'vendor_image_size' : 40960,
'system_verity_block_device': '/dev/block/system', 'system_verity_block_device': '/dev/block/system',
'vendor_verity_block_device': '/dev/block/vendor', 'vendor_verity_block_device': '/dev/block/vendor',
'system.build.prop': { 'system.build.prop': {
@ -131,7 +134,7 @@ class AddImagesToTargetFilesTest(test_utils.ReleaseToolsTestCase):
}, },
'vendor.build.prop': { 'vendor.build.prop': {
'ro.vendor.build.fingerprint': 'google/sailfish/678:user/dev-keys', 'ro.vendor.build.fingerprint': 'google/sailfish/678:user/dev-keys',
} },
} }
# Prepare the META/ folder. # Prepare the META/ folder.
@ -142,9 +145,9 @@ class AddImagesToTargetFilesTest(test_utils.ReleaseToolsTestCase):
system_image = test_utils.construct_sparse_image([ system_image = test_utils.construct_sparse_image([
(0xCAC1, 6), (0xCAC1, 6),
(0xCAC3, 4), (0xCAC3, 4),
(0xCAC1, 6)]) (0xCAC1, 8)])
vendor_image = test_utils.construct_sparse_image([ vendor_image = test_utils.construct_sparse_image([
(0xCAC2, 10)]) (0xCAC2, 12)])
image_paths = { image_paths = {
'system' : system_image, 'system' : system_image,
@ -203,6 +206,9 @@ class AddImagesToTargetFilesTest(test_utils.ReleaseToolsTestCase):
"""Tests the case for device using AVB.""" """Tests the case for device using AVB."""
image_paths = self._test_AddCareMapForAbOta() image_paths = self._test_AddCareMapForAbOta()
OPTIONS.info_dict = { OPTIONS.info_dict = {
'extfs_sparse_flag' : '-s',
'system_image_size' : 65536,
'vendor_image_size' : 40960,
'avb_system_hashtree_enable' : 'true', 'avb_system_hashtree_enable' : 'true',
'avb_vendor_hashtree_enable' : 'true', 'avb_vendor_hashtree_enable' : 'true',
'system.build.prop': { 'system.build.prop': {
@ -231,6 +237,9 @@ class AddImagesToTargetFilesTest(test_utils.ReleaseToolsTestCase):
"""Tests the case for partitions without fingerprint.""" """Tests the case for partitions without fingerprint."""
image_paths = self._test_AddCareMapForAbOta() image_paths = self._test_AddCareMapForAbOta()
OPTIONS.info_dict = { OPTIONS.info_dict = {
'extfs_sparse_flag' : '-s',
'system_image_size' : 65536,
'vendor_image_size' : 40960,
'system_verity_block_device': '/dev/block/system', 'system_verity_block_device': '/dev/block/system',
'vendor_verity_block_device': '/dev/block/vendor', 'vendor_verity_block_device': '/dev/block/vendor',
} }
@ -249,6 +258,9 @@ class AddImagesToTargetFilesTest(test_utils.ReleaseToolsTestCase):
"""Tests the case for partitions with thumbprint.""" """Tests the case for partitions with thumbprint."""
image_paths = self._test_AddCareMapForAbOta() image_paths = self._test_AddCareMapForAbOta()
OPTIONS.info_dict = { OPTIONS.info_dict = {
'extfs_sparse_flag' : '-s',
'system_image_size' : 65536,
'vendor_image_size' : 40960,
'system_verity_block_device': '/dev/block/system', 'system_verity_block_device': '/dev/block/system',
'vendor_verity_block_device': '/dev/block/vendor', 'vendor_verity_block_device': '/dev/block/vendor',
'system.build.prop': { 'system.build.prop': {
@ -256,7 +268,7 @@ class AddImagesToTargetFilesTest(test_utils.ReleaseToolsTestCase):
}, },
'vendor.build.prop' : { 'vendor.build.prop' : {
'ro.vendor.build.thumbprint': 'google/sailfish/456:user/dev-keys', 'ro.vendor.build.thumbprint': 'google/sailfish/456:user/dev-keys',
} },
} }
AddCareMapForAbOta(None, ['system', 'vendor'], image_paths) AddCareMapForAbOta(None, ['system', 'vendor'], image_paths)
@ -370,6 +382,7 @@ class AddImagesToTargetFilesTest(test_utils.ReleaseToolsTestCase):
(0xCAC3, 4), (0xCAC3, 4),
(0xCAC1, 6)]) (0xCAC1, 6)])
OPTIONS.info_dict = { OPTIONS.info_dict = {
'extfs_sparse_flag' : '-s',
'system_image_size' : 53248, 'system_image_size' : 53248,
} }
name, care_map = GetCareMap('system', sparse_image) name, care_map = GetCareMap('system', sparse_image)
@ -385,6 +398,17 @@ class AddImagesToTargetFilesTest(test_utils.ReleaseToolsTestCase):
(0xCAC3, 4), (0xCAC3, 4),
(0xCAC1, 6)]) (0xCAC1, 6)])
OPTIONS.info_dict = { OPTIONS.info_dict = {
'extfs_sparse_flag' : '-s',
'system_image_size' : -45056, 'system_image_size' : -45056,
} }
self.assertRaises(AssertionError, GetCareMap, 'system', sparse_image) self.assertRaises(AssertionError, GetCareMap, 'system', sparse_image)
def test_GetCareMap_nonSparseImage(self):
OPTIONS.info_dict = {
'system_image_size' : 53248,
}
# 'foo' is the image filename, which is expected to be not used by
# GetCareMap().
name, care_map = GetCareMap('system', 'foo')
self.assertEqual('system', name)
self.assertEqual(RangeSet("0-12").to_string_raw(), care_map)