Skip to contentSkip to conversation

Fix AVB pytest suite for sandbox

PATCH In review Mattijs Korpershoek 2 participants 15 messages Oct 22 – Oct 26

lore
Share & tools
lore
#0 lore raw

v3 cover Fix AVB pytest suite for sandbox

The AVB tests have the following limitations on sandbox:

* The mmc1.img required by sandbox is not created when the avb test runs
* mmc1.img contains a Fedora image, so avb verification cannot be done
* The tests silently pass due to bad usage of string.find()

This series improve the test suite to have more useful results on sandbox.

---
Changes in v3:
- Fix typo in commit message for PATCH 10/10 (Simon)
- Drop U-Boot restart in sandbox tests (Simon)
- Add error handling on bind /mmc12 (Simon)
- Use unbind /mmc12 on teardown using yield (Simon)
- Add docstring for create_gpt_image (Simon)
- Link to v2: https://patch.msgid.link/[email protected]

Changes in v2:
- Add missing find() return code check in test_avb_mmc_uuid (Guillaume)
- Skip avb tests when qemu-img or cgpt is not installed (Igor)
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Mattijs Korpershoek (10):
avb: Use map_to_sysmem() for flush_cache() address test/py: android: Move android image creation to a dedicated file test/py: android: Drop verbose printing and unused return test/py: android: Use for loop to create the images / update disks test/py: android: Add create_gpt_image() for image creation test/py: android: Add setup_android_v{4,2}_image subfunctions test/py: avb: Use format specifiers instead of str() for mmc_dev test/py: avb: Move mmc device number to a pytest fixture test/py: avb: Add sandbox AVB testing with dedicated mmc12 device test/py: avb: Fix find() logic when matching response strings

 arch/sandbox/dts/test.dts                   |   8 ++
 common/avb_verify.c                         |   3 +-
 test/py/tests/test_android/android_image.py | 116 +++++++++++++++++++++++++
 test/py/tests/test_android/test_abootimg.py |  77 +++++++++++++++++
 test/py/tests/test_android/test_avb.py      |  81 +++++++++++------
 test/py/tests/test_ut.py                    | 129 +---------------------------
 6 files changed, 261 insertions(+), 153 deletions(-)

---
base-commit: fed92546960a92177d67c0b8d7b22385f50fed29
change-id: 20261015-test-avb-fixes-9a4682e8057d

Best regards,

Signature · 1 line
Mattijs Korpershoek <[email protected]>
#1 lore raw

PATCH v3 1/10 avb: Use map_to_sysmem() for flush_cache() address

flush_cache() expects a U-Boot physical address, but avb_verify's mmc_read_and_flush() passes a raw pointer cast to ulong. On sandbox, host heap pointers (e.g. 0x1a27ee70) can be outside the emulated SDRAM region (0x0 to gd->ram_size), causing map_sysmem() inside flush_cache() to crash:

phys_to_virt: Cannot map sandbox address 1a27ee70

    (SDRAM from 0 to 10000000)

Fix the crash by using map_to_sysmem() to convert the buffer pointer to a proper U-Boot address.

---
 common/avb_verify.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
common/avb_verify.c +2 -1
index 09f1534c3cc5..d157adc4af07 100644
@@ -7,6 +7,7 @@
 #include <blk.h>
 #include <cpu_func.h>
 #include <image.h>
+#include <mapmem.h>
 #include <malloc.h>
 #include <part.h>
 #include <tee.h>
@@ -346,7 +347,7 @@ static unsigned long mmc_read_and_flush(struct mmc_part *part,
 	blks = blk_dread(part->mmc_blk,
 			 start, sectors, tmp_buf);
 	/* flush cache after read */
-	flush_cache((ulong)tmp_buf, sectors * part->info.blksz);
+	flush_cache(map_to_sysmem(tmp_buf), sectors * part->info.blksz);
 
 	if (unaligned)
 		memcpy(buffer, tmp_buf, sectors * part->info.blksz);
Signature · 1 line
2.56.0
#2 lore raw

PATCH v3 2/10 test/py: android: Move android image creation to a dedicated file

In reply to Mattijs Korpershoek

The image creation code in test_ut is huge and difficult to follow. The android part contain code duplications when making mmc7.img/mmc8.img.

To prepare some code refactoring, move the Android specific bits into a dedicated file under test_android/.

No functional change.

---
 test/py/tests/test_android/android_image.py | 135 ++++++++++++++++++++++++++++
 test/py/tests/test_ut.py                    | 129 +-------------------------
 2 files changed, 136 insertions(+), 128 deletions(-)
test/py/tests/test_android/android_image.py new file +135 -0
new file mode 100644
index 000000000000..3bea311586bf
@@ -0,0 +1,135 @@
+# SPDX-License-Identifier: GPL-2.0
+"""Helpers to create Android disk images for sandbox testing."""
+
+import collections
+import os
+
+import utils
+from test_android import test_abootimg
+
+def setup_android_image(ubman):
+    """Create a 20MB disk image with Android partitions"""
+    Partition = collections.namedtuple('part', 'start,size,name')
+    parts = {}
+    disk_data = None
+
+    def set_part_data(partnum, data):
+        """Set the contents of a disk partition
+
+        This updates disk_data by putting data in the right place
+
+        Args:
+            partnum (int): Partition number to set
+            data (bytes): Data for that partition
+        """
+        nonlocal disk_data
+
+        start = parts[partnum].start * sect_size
+        disk_data = disk_data[:start] + data + disk_data[start + len(data):]
+
+    mmc_dev = 7
+    fname = os.path.join(ubman.config.source_dir, f'mmc{mmc_dev}.img')
+    utils.run_and_log(ubman, f'qemu-img create {fname} 20M')
+    utils.run_and_log(ubman, f'cgpt create {fname}')
+
+    ptr = 40
+
+    # Number of sectors in 1MB
+    sect_size = 512
+    sect_1mb = (1 << 20) // sect_size
+
+    required_parts = [
+        {'num': 1, 'label':'misc', 'size': '1M'},
+        {'num': 2, 'label':'boot_a', 'size': '4M'},
+        {'num': 3, 'label':'boot_b', 'size': '4M'},
+        {'num': 4, 'label':'vendor_boot_a', 'size': '4M'},
+        {'num': 5, 'label':'vendor_boot_b', 'size': '4M'},
+    ]
+
+    for part in required_parts:
+        size_str = part['size']
+        if 'M' in size_str:
+            size = int(size_str[:-1]) * sect_1mb
+        else:
+            size = int(size_str)
+        utils.run_and_log(
+            ubman,
+            f"cgpt add -i {part['num']} -b {ptr} -s {size} -l {part['label']} -t basicdata {fname}")
+        ptr += size
+
+    utils.run_and_log(ubman, f'cgpt boot -p {fname}')
+    out = utils.run_and_log(ubman, f'cgpt show -q {fname}')
+
+    # Create a dict (indexed by partition number) containing the above info
+    for line in out.splitlines():
+        start, size, num, name = line.split(maxsplit=3)
+        parts[int(num)] = Partition(int(start), int(size), name)
+
+    with open(fname, 'rb') as inf:
+        disk_data = inf.read()
+
+    test_abootimg.AbootimgTestDiskImage(ubman, 'bootv4.img', test_abootimg.boot_img_hex)
+    boot_img = os.path.join(ubman.config.result_dir, 'bootv4.img')
+    with open(boot_img, 'rb') as inf:
+        set_part_data(2, inf.read())
+
+    test_abootimg.AbootimgTestDiskImage(ubman, 'vendor_boot.img', test_abootimg.vboot_img_hex)
+    vendor_boot_img = os.path.join(ubman.config.result_dir, 'vendor_boot.img')
+    with open(vendor_boot_img, 'rb') as inf:
+        set_part_data(4, inf.read())
+
+    with open(fname, 'wb') as outf:
+        outf.write(disk_data)
+
+    print(f'wrote to {fname}')
+
+    mmc_dev = 8
+    fname = os.path.join(ubman.config.source_dir, f'mmc{mmc_dev}.img')
+    utils.run_and_log(ubman, f'qemu-img create {fname} 20M')
+    utils.run_and_log(ubman, f'cgpt create {fname}')
+
+    ptr = 40
+
+    # Number of sectors in 1MB
+    sect_size = 512
+    sect_1mb = (1 << 20) // sect_size
+
+    required_parts = [
+        {'num': 1, 'label':'misc', 'size': '1M'},
+        {'num': 2, 'label':'boot_a', 'size': '4M'},
+        {'num': 3, 'label':'boot_b', 'size': '4M'},
+    ]
+
+    for part in required_parts:
+        size_str = part['size']
+        if 'M' in size_str:
+            size = int(size_str[:-1]) * sect_1mb
+        else:
+            size = int(size_str)
+        utils.run_and_log(
+            ubman,
+            f"cgpt add -i {part['num']} -b {ptr} -s {size} -l {part['label']} -t basicdata {fname}")
+        ptr += size
+
+    utils.run_and_log(ubman, f'cgpt boot -p {fname}')
+    out = utils.run_and_log(ubman, f'cgpt show -q {fname}')
+
+    # Create a dict (indexed by partition number) containing the above info
+    for line in out.splitlines():
+        start, size, num, name = line.split(maxsplit=3)
+        parts[int(num)] = Partition(int(start), int(size), name)
+
+    with open(fname, 'rb') as inf:
+        disk_data = inf.read()
+
+    test_abootimg.AbootimgTestDiskImage(ubman, 'boot.img', test_abootimg.img_hex)
+    boot_img = os.path.join(ubman.config.result_dir, 'boot.img')
+    with open(boot_img, 'rb') as inf:
+        set_part_data(2, inf.read())
+
+    with open(fname, 'wb') as outf:
+        outf.write(disk_data)
+
+    print(f'wrote to {fname}')
+
+    return fname
test/py/tests/test_ut.py +1 -128

141 lines not on this page · raw message

Signature · 1 line
2.56.0
#3 lore raw

PATCH v3 3/10 test/py: android: Drop verbose printing and unused return

In reply to Mattijs Korpershoek

The setup_android_image() is only called once. The callers does not use the return value.

Drop the unused return. While at it, also remove the verbose printing.

---
 test/py/tests/test_android/android_image.py | 6 ------
 1 file changed, 6 deletions(-)
test/py/tests/test_android/android_image.py +0 -6
index 3bea311586bf..8b536754656e 100644
@@ -81,8 +81,6 @@ def setup_android_image(ubman):
     with open(fname, 'wb') as outf:
         outf.write(disk_data)
 
-    print(f'wrote to {fname}')
-
     mmc_dev = 8
     fname = os.path.join(ubman.config.source_dir, f'mmc{mmc_dev}.img')
     utils.run_and_log(ubman, f'qemu-img create {fname} 20M')
@@ -129,7 +127,3 @@ def setup_android_image(ubman):
 
     with open(fname, 'wb') as outf:
         outf.write(disk_data)
-
-    print(f'wrote to {fname}')
-
-    return fname
Signature · 1 line
2.56.0
#4 lore raw

PATCH v3 4/10 test/py: android: Use for loop to create the images / update disks

In reply to Mattijs Korpershoek

setup_android_image() creates 2 mmcX.img files:

* mmc7.img: For boot image v4 support
* mmc8.img: For boot image v2 support

There is quite some code that's common for both images. Currently, most code is duplicated.

To prepare a cleaner refactoring, move the image descriptions into an array and create each partition using a loop.

---
 test/py/tests/test_android/android_image.py | 48 +++++++++++++----------------
 1 file changed, 21 insertions(+), 27 deletions(-)
test/py/tests/test_android/android_image.py +21 -27
index 8b536754656e..14dfd26fffd8 100644
@@ -13,20 +13,6 @@ def setup_android_image(ubman):
     parts = {}
     disk_data = None
 
-    def set_part_data(partnum, data):
-        """Set the contents of a disk partition
-
-        This updates disk_data by putting data in the right place
-
-        Args:
-            partnum (int): Partition number to set
-            data (bytes): Data for that partition
-        """
-        nonlocal disk_data
-
-        start = parts[partnum].start * sect_size
-        disk_data = disk_data[:start] + data + disk_data[start + len(data):]
-
     mmc_dev = 7
     fname = os.path.join(ubman.config.source_dir, f'mmc{mmc_dev}.img')
     utils.run_and_log(ubman, f'qemu-img create {fname} 20M')
@@ -68,15 +54,17 @@ def setup_android_image(ubman):
     with open(fname, 'rb') as inf:
         disk_data = inf.read()
 
-    test_abootimg.AbootimgTestDiskImage(ubman, 'bootv4.img', test_abootimg.boot_img_hex)
-    boot_img = os.path.join(ubman.config.result_dir, 'bootv4.img')
-    with open(boot_img, 'rb') as inf:
-        set_part_data(2, inf.read())
-
-    test_abootimg.AbootimgTestDiskImage(ubman, 'vendor_boot.img', test_abootimg.vboot_img_hex)
-    vendor_boot_img = os.path.join(ubman.config.result_dir, 'vendor_boot.img')
-    with open(vendor_boot_img, 'rb') as inf:
-        set_part_data(4, inf.read())
+    images = [
+        (2, 'bootv4.img', test_abootimg.boot_img_hex),
+        (4, 'vendor_boot.img', test_abootimg.vboot_img_hex),
+    ]
+    for partnum, img_name, hex_data in images:
+        test_abootimg.AbootimgTestDiskImage(ubman, img_name, hex_data)
+        img_path = os.path.join(ubman.config.result_dir, img_name)
+        with open(img_path, 'rb') as inf:
+            data = inf.read()
+        start = parts[partnum].start * sect_size
+        disk_data = disk_data[:start] + data + disk_data[start + len(data):]
 
     with open(fname, 'wb') as outf:
         outf.write(disk_data)
@@ -120,10 +108,16 @@ def setup_android_image(ubman):
     with open(fname, 'rb') as inf:
         disk_data = inf.read()
 
-    test_abootimg.AbootimgTestDiskImage(ubman, 'boot.img', test_abootimg.img_hex)
-    boot_img = os.path.join(ubman.config.result_dir, 'boot.img')
-    with open(boot_img, 'rb') as inf:
-        set_part_data(2, inf.read())
+    images = [
+        (2, 'boot.img', test_abootimg.img_hex),
+    ]
+    for partnum, img_name, hex_data in images:
+        test_abootimg.AbootimgTestDiskImage(ubman, img_name, hex_data)
+        img_path = os.path.join(ubman.config.result_dir, img_name)
+        with open(img_path, 'rb') as inf:
+            data = inf.read()
+        start = parts[partnum].start * sect_size
+        disk_data = disk_data[:start] + data + disk_data[start + len(data):]
 
     with open(fname, 'wb') as outf:
         outf.write(disk_data)
Signature · 1 line
2.56.0
#5 lore raw

PATCH v3 5/10 test/py: android: Add create_gpt_image() for image creation

In reply to Mattijs Korpershoek

There is quite some code duplication between the image creation for mmc7.img and mmc8.img. If we want to add more images, this will be even more duplication.

Add a common function, create_gpt_image() which removes all the code duplication.

---
 test/py/tests/test_android/android_image.py | 93 ++++++++++-------------------
 1 file changed, 33 insertions(+), 60 deletions(-)
test/py/tests/test_android/android_image.py +33 -60
index 14dfd26fffd8..53b5ebc1de19 100644
@@ -7,13 +7,24 @@ import os
 import utils
 from test_android import test_abootimg
 
-def setup_android_image(ubman):
-    """Create a 20MB disk image with Android partitions"""
+def create_gpt_image(ubman, mmc_dev, required_parts, images):
+    """Create a 20MB GPT disk image and write it to mmc{mmc_dev}.img
+
+    Args:
+        ubman: U-Boot console fixture
+        mmc_dev (int): MMC device number, used to name the output image
+            file mmc{mmc_dev}.img in the source directory
+        required_parts (list of dict): Partitions to create, each with
+            'num' (int) partition number, 'label' (str) partition name,
+            and 'size' (str) either a sector count or 'NM' for N
+            megabytes (e.g. '4M')
+        images (list of tuple): Data to write into partitions, each a
+            (partnum, filename, hex_data) tuple where partnum (int) is
+            the target partition, filename (str) the image file name,
+            and hex_data (str) the image content as hex
+    """
     Partition = collections.namedtuple('part', 'start,size,name')
     parts = {}
-    disk_data = None
-
-    mmc_dev = 7
     fname = os.path.join(ubman.config.source_dir, f'mmc{mmc_dev}.img')
     utils.run_and_log(ubman, f'qemu-img create {fname} 20M')
     utils.run_and_log(ubman, f'cgpt create {fname}')
@@ -24,14 +35,6 @@ def setup_android_image(ubman):
     sect_size = 512
     sect_1mb = (1 << 20) // sect_size
 
-    required_parts = [
-        {'num': 1, 'label':'misc', 'size': '1M'},
-        {'num': 2, 'label':'boot_a', 'size': '4M'},
-        {'num': 3, 'label':'boot_b', 'size': '4M'},
-        {'num': 4, 'label':'vendor_boot_a', 'size': '4M'},
-        {'num': 5, 'label':'vendor_boot_b', 'size': '4M'},
-    ]
-
     for part in required_parts:
         size_str = part['size']
         if 'M' in size_str:
@@ -54,10 +57,6 @@ def setup_android_image(ubman):
     with open(fname, 'rb') as inf:
         disk_data = inf.read()
 
-    images = [
-        (2, 'bootv4.img', test_abootimg.boot_img_hex),
-        (4, 'vendor_boot.img', test_abootimg.vboot_img_hex),
-    ]
     for partnum, img_name, hex_data in images:
         test_abootimg.AbootimgTestDiskImage(ubman, img_name, hex_data)
         img_path = os.path.join(ubman.config.result_dir, img_name)
@@ -69,55 +68,29 @@ def setup_android_image(ubman):
     with open(fname, 'wb') as outf:
         outf.write(disk_data)
 
-    mmc_dev = 8
-    fname = os.path.join(ubman.config.source_dir, f'mmc{mmc_dev}.img')
-    utils.run_and_log(ubman, f'qemu-img create {fname} 20M')
-    utils.run_and_log(ubman, f'cgpt create {fname}')
-
-    ptr = 40
-
-    # Number of sectors in 1MB
-    sect_size = 512
-    sect_1mb = (1 << 20) // sect_size
-
+def setup_android_image(ubman):
+    """Create a 20MB disk image with Android partitions"""
+    mmc_dev = 7
     required_parts = [
         {'num': 1, 'label':'misc', 'size': '1M'},
         {'num': 2, 'label':'boot_a', 'size': '4M'},
         {'num': 3, 'label':'boot_b', 'size': '4M'},
+        {'num': 4, 'label':'vendor_boot_a', 'size': '4M'},
+        {'num': 5, 'label':'vendor_boot_b', 'size': '4M'},
     ]
+    images = [
+        (2, 'bootv4.img', test_abootimg.boot_img_hex),
+        (4, 'vendor_boot.img', test_abootimg.vboot_img_hex),
+    ]
+    create_gpt_image(ubman, mmc_dev, required_parts, images)
 
-    for part in required_parts:
-        size_str = part['size']
-        if 'M' in size_str:
-            size = int(size_str[:-1]) * sect_1mb
-        else:
-            size = int(size_str)
-        utils.run_and_log(
-            ubman,
-            f"cgpt add -i {part['num']} -b {ptr} -s {size} -l {part['label']} -t basicdata {fname}")
-        ptr += size
-
-    utils.run_and_log(ubman, f'cgpt boot -p {fname}')
-    out = utils.run_and_log(ubman, f'cgpt show -q {fname}')
-
-    # Create a dict (indexed by partition number) containing the above info
-    for line in out.splitlines():
-        start, size, num, name = line.split(maxsplit=3)
-        parts[int(num)] = Partition(int(start), int(size), name)
-
-    with open(fname, 'rb') as inf:
-        disk_data = inf.read()
-
+    mmc_dev = 8
+    required_parts = [
+        {'num': 1, 'label':'misc', 'size': '1M'},
+        {'num': 2, 'label':'boot_a', 'size': '4M'},
+        {'num': 3, 'label':'boot_b', 'size': '4M'},
+    ]
     images = [
         (2, 'boot.img', test_abootimg.img_hex),
     ]
-    for partnum, img_name, hex_data in images:
-        test_abootimg.AbootimgTestDiskImage(ubman, img_name, hex_data)
-        img_path = os.path.join(ubman.config.result_dir, img_name)
-        with open(img_path, 'rb') as inf:
-            data = inf.read()
-        start = parts[partnum].start * sect_size
-        disk_data = disk_data[:start] + data + disk_data[start + len(data):]
-
-    with open(fname, 'wb') as outf:
-        outf.write(disk_data)
+    create_gpt_image(ubman, mmc_dev, required_parts, images)
Signature · 1 line
2.56.0
Reviewed-by on v3 5/10
#6 lore raw

Re: [PATCH v3 05/10] test/py: android: Add create_gpt_image() for image creation

Quoted 9 lines · Mattijs Korpershoek

test/py: android: Add create_gpt_image() for image creation

There is quite some code duplication between the image creation for mmc7.img and mmc8.img. If we want to add more images, this will be even more duplication.

Add a common function, create_gpt_image() which removes all the code duplication.

Quoted 8 lines
 test/py/tests/test_android/android_image.py | 93 ++++++++++-------------------
 1 file changed, 33 insertions(+), 60 deletions(-)
#7 lore raw

PATCH v3 6/10 test/py: android: Add setup_android_v{4,2}_image subfunctions

In reply to Mattijs Korpershoek

To split out the logic even further, do each image creation in a separate function.

This will make it cleaner to add future images as well.

---
 test/py/tests/test_android/android_image.py | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)
test/py/tests/test_android/android_image.py +11 -6
index 53b5ebc1de19..79e2965f2309 100644
@@ -68,9 +68,8 @@ def create_gpt_image(ubman, mmc_dev, required_parts, images):
     with open(fname, 'wb') as outf:
         outf.write(disk_data)
 
-def setup_android_image(ubman):
-    """Create a 20MB disk image with Android partitions"""
-    mmc_dev = 7
+def setup_android_v4_image(ubman):
+    """Create mmc7.img: boot image v4 with vendor_boot partitions."""
     required_parts = [
         {'num': 1, 'label':'misc', 'size': '1M'},
         {'num': 2, 'label':'boot_a', 'size': '4M'},
@@ -82,9 +81,10 @@ def setup_android_image(ubman):
         (2, 'bootv4.img', test_abootimg.boot_img_hex),
         (4, 'vendor_boot.img', test_abootimg.vboot_img_hex),
     ]
-    create_gpt_image(ubman, mmc_dev, required_parts, images)
+    create_gpt_image(ubman, 7, required_parts, images)
 
-    mmc_dev = 8
+def setup_android_v2_image(ubman):
+    """Create mmc8.img: boot image v2 without vendor_boot."""
     required_parts = [
         {'num': 1, 'label':'misc', 'size': '1M'},
         {'num': 2, 'label':'boot_a', 'size': '4M'},
@@ -93,4 +93,9 @@ def setup_android_image(ubman):
     images = [
         (2, 'boot.img', test_abootimg.img_hex),
     ]
-    create_gpt_image(ubman, mmc_dev, required_parts, images)
+    create_gpt_image(ubman, 8, required_parts, images)
+
+def setup_android_image(ubman):
+    """Create disk images with Android partitions."""
+    setup_android_v4_image(ubman)
+    setup_android_v2_image(ubman)
Signature · 1 line
2.56.0
#8 lore raw

PATCH v3 7/10 test/py: avb: Use format specifiers instead of str() for mmc_dev

In reply to Mattijs Korpershoek

The tests format mmc_dev using str() conversion, which produces a decimal string. This works when mmc_dev is 1, since "1" is valid in both decimal and hex, but breaks for other device numbers like 12 (0xc): str(12) produces "12", which avb init would interpret as 0x12.

Use %x for commands that parse hex (avb init) and %d for commands that parse decimal (mmc dev) to format device numbers correctly regardless of their value.

---
 test/py/tests/test_android/test_avb.py | 18 ++++++++----------
 1 file changed, 8 insertions(+), 10 deletions(-)
test/py/tests/test_android/test_avb.py +8 -10
index 137d83e1deab..c66ef0cc52d5 100644
@@ -29,7 +29,7 @@ def test_avb_verify(ubman):
 
     success_str = "Verification passed successfully"
 
-    response = ubman.run_command('avb init %s' %str(mmc_dev))
+    response = ubman.run_command('avb init %x' % mmc_dev)
     assert response == ''
     response = ubman.run_command('avb verify')
     assert response.find(success_str)
@@ -43,11 +43,10 @@ def test_avb_mmc_uuid(ubman):
     'part list mmc 1' output
     """
 
-    response = ubman.run_command('avb init %s' % str(mmc_dev))
+    response = ubman.run_command('avb init %x' % mmc_dev)
     assert response == ''
 
-    response = ubman.run_command('mmc rescan; mmc dev %s' %
-                                          str(mmc_dev))
+    response = ubman.run_command('mmc rescan; mmc dev %d' % mmc_dev)
     assert response.find('is current device')
 
     part_lines = ubman.run_command('mmc part').splitlines()
@@ -75,7 +74,7 @@ def test_avb_read_rb(ubman):
     """Test reading rollback indexes
     """
 
-    response = ubman.run_command('avb init %s' % str(mmc_dev))
+    response = ubman.run_command('avb init %x' % mmc_dev)
     assert response == ''
 
     response = ubman.run_command('avb read_rb 1')
@@ -87,7 +86,7 @@ def test_avb_is_unlocked(ubman):
     """Test if device is in the unlocked state
     """
 
-    response = ubman.run_command('avb init %s' % str(mmc_dev))
+    response = ubman.run_command('avb init %x' % mmc_dev)
     assert response == ''
 
     response = ubman.run_command('avb is_unlocked')
@@ -101,14 +100,13 @@ def test_avb_mmc_read(ubman):
     """Test mmc read operation
     """
 
-    response = ubman.run_command('mmc rescan; mmc dev %s 0' %
-                                          str(mmc_dev))
+    response = ubman.run_command('mmc rescan; mmc dev %d 0' % mmc_dev)
     assert response.find('is current device')
 
     response = ubman.run_command('mmc read 0x%x 0x100 0x1' % temp_addr)
     assert response.find('read: OK')
 
-    response = ubman.run_command('avb init %s' % str(mmc_dev))
+    response = ubman.run_command('avb init %x' % mmc_dev)
     assert response == ''
 
     response = ubman.run_command('avb read_part xloader 0 100 0x%x' %
@@ -127,7 +125,7 @@ def test_avb_persistent_values(ubman):
     """Test reading/writing persistent storage to avb
     """
 
-    response = ubman.run_command('avb init %s' % str(mmc_dev))
+    response = ubman.run_command('avb init %x' % mmc_dev)
     assert response == ''
 
     response = ubman.run_command('avb write_pvalue test value_value')
Signature · 1 line
2.56.0
#9 lore raw

PATCH v3 8/10 test/py: avb: Move mmc device number to a pytest fixture

In reply to Mattijs Korpershoek

The AVB tests hardcode mmc device 1, which is usually correct on real hardware. On sandbox, this is not the case because mmc1.img contains a fedora image.

Replace the module-level mmc_dev constant with an avb_device_id fixture so that a subsequent commit can override the device number for sandbox.

---
 test/py/tests/test_android/test_avb.py | 38 ++++++++++++++++++++--------------
 1 file changed, 22 insertions(+), 16 deletions(-)
test/py/tests/test_android/test_avb.py +22 -16
index c66ef0cc52d5..34cddc485ebc 100644
@@ -16,20 +16,26 @@ For configuration verification:
 
 import pytest
 
-# defauld mmc id
-mmc_dev = 1
 temp_addr = 0x90000000
 temp_addr2 = 0x90002000
 
[email protected](scope='function')
+def avb_device_id(ubman):
+    """Returns the device id number for AVB
+    """
+
+    # default mmc id
+    return 1
+
 @pytest.mark.buildconfigspec('cmd_avb')
 @pytest.mark.buildconfigspec('cmd_mmc')
-def test_avb_verify(ubman):
+def test_avb_verify(ubman, avb_device_id):
     """Run AVB 2.0 boot verification chain with avb subset of commands
     """
 
     success_str = "Verification passed successfully"
 
-    response = ubman.run_command('avb init %x' % mmc_dev)
+    response = ubman.run_command('avb init %x' % avb_device_id)
     assert response == ''
     response = ubman.run_command('avb verify')
     assert response.find(success_str)
@@ -38,15 +44,15 @@ def test_avb_verify(ubman):
 @pytest.mark.buildconfigspec('cmd_avb')
 @pytest.mark.buildconfigspec('cmd_mmc')
 @pytest.mark.notbuildconfigspec('sandbox')
-def test_avb_mmc_uuid(ubman):
+def test_avb_mmc_uuid(ubman, avb_device_id):
     """Check if 'avb get_uuid' works, compare results with
     'part list mmc 1' output
     """
 
-    response = ubman.run_command('avb init %x' % mmc_dev)
+    response = ubman.run_command('avb init %x' % avb_device_id)
     assert response == ''
 
-    response = ubman.run_command('mmc rescan; mmc dev %d' % mmc_dev)
+    response = ubman.run_command('mmc rescan; mmc dev %d' % avb_device_id)
     assert response.find('is current device')
 
     part_lines = ubman.run_command('mmc part').splitlines()
@@ -70,11 +76,11 @@ def test_avb_mmc_uuid(ubman):
 
 
 @pytest.mark.buildconfigspec('cmd_avb')
-def test_avb_read_rb(ubman):
+def test_avb_read_rb(ubman, avb_device_id):
     """Test reading rollback indexes
     """
 
-    response = ubman.run_command('avb init %x' % mmc_dev)
+    response = ubman.run_command('avb init %x' % avb_device_id)
     assert response == ''
 
     response = ubman.run_command('avb read_rb 1')
@@ -82,11 +88,11 @@ def test_avb_read_rb(ubman):
 
 
 @pytest.mark.buildconfigspec('cmd_avb')
-def test_avb_is_unlocked(ubman):
+def test_avb_is_unlocked(ubman, avb_device_id):
     """Test if device is in the unlocked state
     """
 
-    response = ubman.run_command('avb init %x' % mmc_dev)
+    response = ubman.run_command('avb init %x' % avb_device_id)
     assert response == ''
 
     response = ubman.run_command('avb is_unlocked')
@@ -96,17 +102,17 @@ def test_avb_is_unlocked(ubman):
 @pytest.mark.buildconfigspec('cmd_avb')
 @pytest.mark.buildconfigspec('cmd_mmc')
 @pytest.mark.notbuildconfigspec('sandbox')
-def test_avb_mmc_read(ubman):
+def test_avb_mmc_read(ubman, avb_device_id):
     """Test mmc read operation
     """
 
-    response = ubman.run_command('mmc rescan; mmc dev %d 0' % mmc_dev)
+    response = ubman.run_command('mmc rescan; mmc dev %d 0' % avb_device_id)
     assert response.find('is current device')
 
     response = ubman.run_command('mmc read 0x%x 0x100 0x1' % temp_addr)
     assert response.find('read: OK')
 
-    response = ubman.run_command('avb init %x' % mmc_dev)
+    response = ubman.run_command('avb init %x' % avb_device_id)
     assert response == ''
 
     response = ubman.run_command('avb read_part xloader 0 100 0x%x' %
@@ -121,11 +127,11 @@ def test_avb_mmc_read(ubman):
 
 @pytest.mark.buildconfigspec('cmd_avb')
 @pytest.mark.buildconfigspec('optee_ta_avb')
-def test_avb_persistent_values(ubman):
+def test_avb_persistent_values(ubman, avb_device_id):
     """Test reading/writing persistent storage to avb
     """
 
-    response = ubman.run_command('avb init %x' % mmc_dev)
+    response = ubman.run_command('avb init %x' % avb_device_id)
     assert response == ''
 
     response = ubman.run_command('avb write_pvalue test value_value')
Signature · 1 line
2.56.0
#10 lore raw

PATCH v3 9/10 test/py: avb: Add sandbox AVB testing with dedicated mmc12 device

In reply to Mattijs Korpershoek

Right now, sandbox runs the AVB tests using mmc1.img, which contains a fedora image.
This does not really make sense. We should test on an mmc device which has a proper vbmeta partition instead.

Add a new mmc12 in sandbox which contains all required partitions for proper AVB testing.
Use the mmc12 device in the avb tests only when running in sandbox. The mmc12 DT node gets enabled by the avb_device_id fixture if CMD_BIND is available.

Note: the vbmeta.img has been generated using:

    $ ./avbtool.py add_hash_footer \
                   --image boot.img \
                   --partition_name boot \
                   --partition_size 4194304
    $ ./avbtool.py make_vbmeta_image \
                   --key test/data/testkey_rsa4096.pem \
                   --algorithm SHA256_RSA4096 \
                   --include_descriptors_from_image boot.img \
                   --padding_size 4096 --flags 1 \
                   --output vbmeta.img
     $ gzip -9 vbmeta.img
     $ xxd -p vbmeta.img.gz > vbmeta.img.gz.hex
---
 arch/sandbox/dts/test.dts                   |  8 +++
 test/py/tests/test_android/android_image.py | 15 ++++++
 test/py/tests/test_android/test_abootimg.py | 77 +++++++++++++++++++++++++++++
 test/py/tests/test_android/test_avb.py      | 31 +++++++++++-
 4 files changed, 130 insertions(+), 1 deletion(-)
arch/sandbox/dts/test.dts +8 -0
index 25c99ef81472..80f505083cd9 100644
@@ -50,6 +50,7 @@
 		mmc9 = "/mmc9";
 		mmc10 = "/mmc10";
 		mmc11 = "/mmc11";
+		mmc12 = "/mmc12";
 		pci0 = &pci0;
 		pci1 = &pci1;
 		pci2 = &pci2;
@@ -1398,6 +1399,13 @@
 		filename = "mmc11.img";
 	};
 
+	/* This is used for Android AVB tests */
+	mmc12 {
+		status = "disabled";
+		compatible = "sandbox,mmc";
+		filename = "mmc12.img";
+	};
+
 	pch {
 		compatible = "sandbox,pch";
 	};
test/py/tests/test_android/android_image.py +15 -0
index 79e2965f2309..dbfba8a403bb 100644
@@ -95,7 +95,22 @@ def setup_android_v2_image(ubman):
     ]
     create_gpt_image(ubman, 8, required_parts, images)
 
+def setup_android_avb_image(ubman):
+    """Create mmc12.img: boot image v2 with vbmeta.img."""
+
+    required_parts = [
+        {'num': 1, 'label':'boot', 'size': '4M'},
+        {'num': 2, 'label':'system', 'size': '4M'},
+        {'num': 3, 'label':'vbmeta', 'size': '4M'},
+    ]
+    images = [
+        (1, 'boot.img', test_abootimg.img_hex),
+        (3, 'vbmeta.img', test_abootimg.vbmeta_img_hex),
+    ]
+    create_gpt_image(ubman, 12, required_parts, images)
+
 def setup_android_image(ubman):
     """Create disk images with Android partitions."""
     setup_android_v4_image(ubman)
     setup_android_v2_image(ubman)
+    setup_android_avb_image(ubman)
test/py/tests/test_android/test_abootimg.py +77 -0

89 lines not on this page · raw message

test/py/tests/test_android/test_avb.py +30 -1
index 34cddc485ebc..9104cf6f37a8 100644
@@ -13,8 +13,11 @@ For configuration verification:
 - Corrupt boot partition and check for failure
 - Corrupt vbmeta partition and check for failure
 """
+import os
 
 import pytest
+import shutil
+from android_image import setup_android_avb_image
 
 temp_addr = 0x90000000
 temp_addr2 = 0x90002000
@@ -22,10 +25,36 @@ temp_addr2 = 0x90002000
 @pytest.fixture(scope='function')
 def avb_device_id(ubman):
     """Returns the device id number for AVB
+
+    On sandbox, creates mmc12.img and binds the disabled mmc12 DT node.
+    On other boards, defaults to device 1.
     """
+    if ubman.config.buildconfig.get('config_sandbox'):
+        if not ubman.config.buildconfig.get('config_cmd_bind'):
+            pytest.skip('CMD_BIND required for AVB on sandbox')
+
+        required_host_tools = ['qemu-img', 'cgpt']
+        for tool in required_host_tools:
+            if shutil.which(tool) is None:
+                pytest.skip(f'tool "{tool}" not in $PATH')
+
+        mmc_dev = 0xc
+        fname = os.path.join(ubman.config.source_dir, f'mmc{mmc_dev}.img')
+        if not os.path.exists(fname):
+            setup_android_avb_image(ubman)
+
+        response = ubman.run_command(f'bind /mmc{mmc_dev} mmc_sandbox')
+        assert response == ''
+        # Run mmc list to ensure that mmc_sandbox driver is probed
+        ubman.run_command('mmc list')
+
+        yield mmc_dev
+
+        ubman.run_command(f'unbind /mmc{mmc_dev}')
+        return
 
     # default mmc id
-    return 1
+    yield 1
 
 @pytest.mark.buildconfigspec('cmd_avb')
 @pytest.mark.buildconfigspec('cmd_mmc')
Signature · 1 line
2.56.0
Reviewed-by on v3 9/10
#11 lore raw

Re: [PATCH v3 09/10] test/py: avb: Add sandbox AVB testing with dedicated mmc12 device

Quoted 35 lines · Mattijs Korpershoek

test/py: avb: Add sandbox AVB testing with dedicated mmc12 device

Right now, sandbox runs the AVB tests using mmc1.img, which contains a fedora image.
This does not really make sense. We should test on an mmc device which has a proper vbmeta partition instead.

Add a new mmc12 in sandbox which contains all required partitions for proper AVB testing.
Use the mmc12 device in the avb tests only when running in sandbox. The mmc12 DT node gets enabled by the avb_device_id fixture if CMD_BIND is available.

Note: the vbmeta.img has been generated using:

    $ ./avbtool.py add_hash_footer \
                   --image boot.img \
                   --partition_name boot \
                   --partition_size 4194304
    $ ./avbtool.py make_vbmeta_image \
                   --key test/data/testkey_rsa4096.pem \
                   --algorithm SHA256_RSA4096 \
                   --include_descriptors_from_image boot.img \
                   --padding_size 4096 --flags 1 \
                   --output vbmeta.img
     $ gzip -9 vbmeta.img
     $ xxd -p vbmeta.img.gz > vbmeta.img.gz.hex
Quoted 5 lines
 arch/sandbox/dts/test.dts                   |  8 +++
 test/py/tests/test_android/android_image.py | 15 ++++++
 test/py/tests/test_android/test_abootimg.py | 77 +++++++++++++++++++++++++++++
 test/py/tests/test_android/test_avb.py      | 31 +++++++++++-
 4 files changed, 130 insertions(+), 1 deletion(-)
#12 lore raw

PATCH v3 10/10 test/py: avb: Fix find() logic when matching response strings

In reply to Mattijs Korpershoek

The AVB test suite runs some avb commands (such as "avb verify") and then parses the console output for success/failure.

It does so by using the string.find() function which returns:
* -1 when the substring does not exist
* 0/a positive number when the substring exists

For example, here is the output of:

$ ./test/py/test.py --bd sandbox --build -k test_avb_verify -s -v

Hit any key to stop autoboot: 0
=> => avb init 1
=> => avb verify
get_partition: no MMC device at slot 1
get_partition: no MMC device at slot 1
avb_slot_verify.c:644: ERROR: boot: Error loading footer. Verification failed, reason: I/O error occurred while trying to load data => PASSED

Here, the test is marked as PASSED because assert(-1) will return success.

Fix the logic to fail these tests when the expected string is not found.

---
 test/py/tests/test_android/test_avb.py | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
test/py/tests/test_android/test_avb.py +6 -6
index 9104cf6f37a8..70b2fec5c552 100644
@@ -67,7 +67,7 @@ def test_avb_verify(ubman, avb_device_id):
     response = ubman.run_command('avb init %x' % avb_device_id)
     assert response == ''
     response = ubman.run_command('avb verify')
-    assert response.find(success_str)
+    assert success_str in response
 
 
 @pytest.mark.buildconfigspec('cmd_avb')
@@ -82,7 +82,7 @@ def test_avb_mmc_uuid(ubman, avb_device_id):
     assert response == ''
 
     response = ubman.run_command('mmc rescan; mmc dev %d' % avb_device_id)
-    assert response.find('is current device')
+    assert 'is current device' in response
 
     part_lines = ubman.run_command('mmc part').splitlines()
     part_list = {}
@@ -136,22 +136,22 @@ def test_avb_mmc_read(ubman, avb_device_id):
     """
 
     response = ubman.run_command('mmc rescan; mmc dev %d 0' % avb_device_id)
-    assert response.find('is current device')
+    assert 'is current device' in response
 
     response = ubman.run_command('mmc read 0x%x 0x100 0x1' % temp_addr)
-    assert response.find('read: OK')
+    assert 'read: OK' in response
 
     response = ubman.run_command('avb init %x' % avb_device_id)
     assert response == ''
 
     response = ubman.run_command('avb read_part xloader 0 100 0x%x' %
                                            temp_addr2)
-    assert response.find('Read 512 bytes')
+    assert 'Read 512 bytes' in response
 
     # Now lets compare two buffers
     response = ubman.run_command('cmp 0x%x 0x%x 40' %
                                           (temp_addr, temp_addr2))
-    assert response.find('64 word')
+    assert '64 word' in response
 
 
 @pytest.mark.buildconfigspec('cmd_avb')
Signature · 1 line
2.56.0
Reviewed-by on v3 10/10
#13 lore raw

Re: [PATCH v3 10/10] test/py: avb: Fix find() logic when matching response strings

Quoted 26 lines · Mattijs Korpershoek

test/py: avb: Fix find() logic when matching response strings

The AVB test suite runs some avb commands (such as "avb verify") and then parses the console output for success/failure.

It does so by using the string.find() function which returns:
* -1 when the substring does not exist
* 0/a positive number when the substring exists

For example, here is the output of:

$ ./test/py/test.py --bd sandbox --build -k test_avb_verify -s -v

Hit any key to stop autoboot: 0
=> => avb init 1
=> => avb verify
get_partition: no MMC device at slot 1
get_partition: no MMC device at slot 1
avb_slot_verify.c:644: ERROR: boot: Error loading footer. Verification failed, reason: I/O error occurred while trying to load data => PASSED

Here, the test is marked as PASSED because assert(-1) will return success.

Fix the logic to fail these tests when the expected string is not found.

Quoted 7 lines
 test/py/tests/test_android/test_avb.py | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
#14 lore raw
In reply to Mattijs Korpershoek

Hi,

Quoted 10 lines · Mattijs Korpershoek

The AVB tests have the following limitations on sandbox:

* The mmc1.img required by sandbox is not created when the avb test runs
* mmc1.img contains a Fedora image, so avb verification cannot be done
* The tests silently pass due to bad usage of string.find()

This series improve the test suite to have more useful results on sandbox.

[...]

Thanks, Applied to https://git.u-boot-project.org/u-boot/custodians/u-boot-dfu (u-boot-dfu)

[01/10] avb: Use map_to_sysmem() for flush_cache() address

        https://git.u-boot-project.org/u-boot/custodians/u-boot-dfu/-/commit/b83adb74d99c585742e684c20d27742d5dd8c357
[02/10] test/py: android: Move android image creation to a dedicated file
        https://git.u-boot-project.org/u-boot/custodians/u-boot-dfu/-/commit/be4c8df84ec97e876c9f8a0b3b3499e03fa2893c
[03/10] test/py: android: Drop verbose printing and unused return
        https://git.u-boot-project.org/u-boot/custodians/u-boot-dfu/-/commit/6a62c505686a2c37d3b8b920ebba006d93574058
[04/10] test/py: android: Use for loop to create the images / update disks
        https://git.u-boot-project.org/u-boot/custodians/u-boot-dfu/-/commit/9d5adaff1cb0383a68b897593a4aeebc9ab69e3a
[05/10] test/py: android: Add create_gpt_image() for image creation
        https://git.u-boot-project.org/u-boot/custodians/u-boot-dfu/-/commit/f46f0d0ad9dedf4c9f738bcc05dec364b62ff414
[06/10] test/py: android: Add setup_android_v{4,2}_image subfunctions
        https://git.u-boot-project.org/u-boot/custodians/u-boot-dfu/-/commit/15d92cf855f6b7cf5ec238d7fb63a07030c97564
[07/10] test/py: avb: Use format specifiers instead of str() for mmc_dev
        https://git.u-boot-project.org/u-boot/custodians/u-boot-dfu/-/commit/8647765c5c6e2f6c623c2a44219b0dd205b9e13e
[08/10] test/py: avb: Move mmc device number to a pytest fixture
        https://git.u-boot-project.org/u-boot/custodians/u-boot-dfu/-/commit/6463b691087674824c938c331fe57368a07cd2bd
[09/10] test/py: avb: Add sandbox AVB testing with dedicated mmc12 device
        https://git.u-boot-project.org/u-boot/custodians/u-boot-dfu/-/commit/d3d0fff23a1f24e34b76d8b077556708499f2579
[10/10] test/py: avb: Fix find() logic when matching response strings
        https://git.u-boot-project.org/u-boot/custodians/u-boot-dfu/-/commit/974861925ede465ecd780e000a98d64c32181abf

--
Mattijs