aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Pursell <dpursell@google.com>2024-02-12 11:54:49 -0800
committerDavid Pursell <dpursell@google.com>2024-02-12 13:36:21 -0800
commitf759fc4c18b53485f2dccd0057ed1593be753eca (patch)
treedd04663d716d4adaa91ed752188a49343d4d8ae5
parentb798e6083f6468128d51e38b015829eb63943e35 (diff)
downloadavb-f759fc4c18b53485f2dccd0057ed1593be753eca.tar.gz
avbtool: open hash image read-only when possible
The `add_hash_footer` command normally adds the hash descriptor into the image directly, except when the `--do_not_append_vbmeta_image` flag is given. In this case we don't touch the input image so can open it read-only. An example use case is: 1. Android source tree is checked out read-only (e.g. infra bots) 2. Some test image is checked into the tree as a binary 3. We want to call `add_hash_footer` on test image as a build rule In this case, `avbtool` will fail during build if it attempts to open the read-only test image as writable, so must open it read-only. Bug: b/324451620 Test: atest libavb_host_unittest Change-Id: I438030fed51244769100f305fcec7116fc679f4f
-rwxr-xr-xavbtool.py10
-rw-r--r--test/avbtool_unittest.cc2
2 files changed, 8 insertions, 4 deletions
diff --git a/avbtool.py b/avbtool.py
index 5be377f..e46d042 100755
--- a/avbtool.py
+++ b/avbtool.py
@@ -3441,7 +3441,10 @@ class Avb(object):
print('{}'.format(partition_size - max_metadata_size))
return
- image = ImageHandler(image_filename)
+ # If we aren't appending the vbmeta footer to the input image we can
+ # open it in read-only mode.
+ image = ImageHandler(image_filename,
+ read_only=do_not_append_vbmeta_image)
# If there's already a footer, truncate the image to its original
# size. This way 'avbtool add_hash_footer' is idempotent (modulo
@@ -4382,8 +4385,7 @@ class AvbTool(object):
sub_parser = subparsers.add_parser('add_hash_footer',
help='Add hashes and footer to image.')
sub_parser.add_argument('--image',
- help='Image to add hashes to',
- type=argparse.FileType('rb+'))
+ help='Image to add hashes to')
sub_parser.add_argument('--partition_size',
help='Partition size',
type=parse_number)
@@ -4818,7 +4820,7 @@ class AvbTool(object):
def add_hash_footer(self, args):
"""Implements the 'add_hash_footer' sub-command."""
args = self._fixup_common_args(args)
- self.avb.add_hash_footer(args.image.name if args.image else None,
+ self.avb.add_hash_footer(args.image,
args.partition_size, args.dynamic_partition_size,
args.partition_name, args.hash_algorithm,
args.salt, args.chain_partition,
diff --git a/test/avbtool_unittest.cc b/test/avbtool_unittest.cc
index 93cae41..753aaca 100644
--- a/test/avbtool_unittest.cc
+++ b/test/avbtool_unittest.cc
@@ -656,6 +656,8 @@ void AvbToolTest::AddHashFooterTest(bool sparse_image) {
EXPECT_EQ(static_cast<size_t>(erased_footer_file_size), rootfs_size);
// Check that --do_not_append_vbmeta_image works as intended.
+ // In this case we don't modify the input image so it should work read-only.
+ EXPECT_COMMAND(0, "chmod a-w %s", rootfs_path.value().c_str());
EXPECT_COMMAND(0,
"./avbtool.py add_hash_footer --salt d00df00d "
"--hash_algorithm sha256 --image %s "