diff mbox series

[v9,09/10] test: efi_capsule: refactor efi_capsule test

Message ID 20230607054201.42702-10-masahisa.kojima@linaro.org
State Accepted
Commit 482ef90aeb4c74700ea9f8bdf368235da7fc5d09
Headers show
Series FMP versioning support | expand

Commit Message

Masahisa Kojima June 7, 2023, 5:41 a.m. UTC
Current efi capsule python tests have much code duplication.
This commit creates the common function
in test/py/tests/test_efi_capsule/capsule_common.py,
aim to reduce the code size and improve maintainability.

Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Masahisa Kojima <masahisa.kojima@linaro.org>
---
Changes in v9:
- fix pylint warnings

Changes in v8:
- add function comments

 .../tests/test_efi_capsule/capsule_common.py  | 142 +++++++++++
 .../test_capsule_firmware_fit.py              | 152 +++---------
 .../test_capsule_firmware_raw.py              | 225 +++---------------
 .../test_capsule_firmware_signed_fit.py       | 199 +++-------------
 .../test_capsule_firmware_signed_raw.py       | 211 +++-------------
 5 files changed, 287 insertions(+), 642 deletions(-)
 create mode 100644 test/py/tests/test_efi_capsule/capsule_common.py

Comments

Ilias Apalodimas June 7, 2023, 9:52 a.m. UTC | #1
On Wed, 7 Jun 2023 at 08:43, Masahisa Kojima <masahisa.kojima@linaro.org> wrote:
>
> Current efi capsule python tests have much code duplication.
> This commit creates the common function
> in test/py/tests/test_efi_capsule/capsule_common.py,
> aim to reduce the code size and improve maintainability.
>
> Reviewed-by: Simon Glass <sjg@chromium.org>
> Signed-off-by: Masahisa Kojima <masahisa.kojima@linaro.org>
> ---
> Changes in v9:
> - fix pylint warnings
>
> Changes in v8:
> - add function comments
>
>  .../tests/test_efi_capsule/capsule_common.py  | 142 +++++++++++
>  .../test_capsule_firmware_fit.py              | 152 +++---------
>  .../test_capsule_firmware_raw.py              | 225 +++---------------
>  .../test_capsule_firmware_signed_fit.py       | 199 +++-------------
>  .../test_capsule_firmware_signed_raw.py       | 211 +++-------------
>  5 files changed, 287 insertions(+), 642 deletions(-)
>  create mode 100644 test/py/tests/test_efi_capsule/capsule_common.py
>
> diff --git a/test/py/tests/test_efi_capsule/capsule_common.py b/test/py/tests/test_efi_capsule/capsule_common.py
> new file mode 100644
> index 0000000000..9eef6767a6
> --- /dev/null
> +++ b/test/py/tests/test_efi_capsule/capsule_common.py
> @@ -0,0 +1,142 @@
> +# SPDX-License-Identifier:      GPL-2.0+
> +# Copyright (c) 2023, Linaro Limited
> +
> +
> +"""Common function for UEFI capsule test."""
> +
> +from capsule_defs import CAPSULE_DATA_DIR, CAPSULE_INSTALL_DIR
> +
> +def setup(u_boot_console, disk_img, osindications):
> +    """setup the test
> +
> +    Args:
> +        u_boot_console -- A console connection to U-Boot.
> +        disk_img -- A path to disk image to be used for testing.
> +        osindications -- String of osindications value.
> +    """
> +    u_boot_console.run_command_list([
> +        f'host bind 0 {disk_img}',
> +        'printenv -e PlatformLangCodes', # workaround for terminal size determination
> +        'efidebug boot add -b 1 TEST host 0:1 /helloworld.efi',
> +        'efidebug boot order 1',
> +        'env set dfu_alt_info "sf 0:0=u-boot-bin raw 0x100000 0x50000;'
> +        'u-boot-env raw 0x150000 0x200000"'])
> +
> +    if osindications is None:
> +        u_boot_console.run_command('env set -e OsIndications')
> +    else:
> +        u_boot_console.run_command(f'env set -e -nv -bs -rt OsIndications ={osindications}')
> +
> +    u_boot_console.run_command('env save')
> +
> +def init_content(u_boot_console, target, filename, expected):
> +    """initialize test content
> +
> +    Args:
> +        u_boot_console -- A console connection to U-Boot.
> +        target -- Target address to place the content.
> +        filename -- File name of the content.
> +        expected -- Expected string of the content.
> +    """
> +    output = u_boot_console.run_command_list([
> +        'sf probe 0:0',
> +        f'fatload host 0:1 4000000 {CAPSULE_DATA_DIR}/{filename}',
> +        f'sf write 4000000 {target} 10',
> +        'sf read 5000000 100000 10',
> +        'md.b 5000000 10'])
> +    assert expected in ''.join(output)
> +
> +def place_capsule_file(u_boot_console, filenames):
> +    """place the capsule file
> +
> +    Args:
> +        u_boot_console -- A console connection to U-Boot.
> +        filenames -- File name array of the target capsule files.
> +    """
> +    for name in filenames:
> +        u_boot_console.run_command_list([
> +            f'fatload host 0:1 4000000 {CAPSULE_DATA_DIR}/{name}',
> +            f'fatwrite host 0:1 4000000 {CAPSULE_INSTALL_DIR}/{name} $filesize'])
> +
> +    output = u_boot_console.run_command(f'fatls host 0:1 {CAPSULE_INSTALL_DIR}')
> +    for name in filenames:
> +        assert name in ''.join(output)
> +
> +def exec_manual_update(u_boot_console, disk_img, filenames, need_reboot = True):
> +    """execute capsule update manually
> +
> +    Args:
> +        u_boot_console -- A console connection to U-Boot.
> +        disk_img -- A path to disk image to be used for testing.
> +        filenames -- File name array of the target capsule files.
> +        need_reboot -- Flag indicates whether system reboot is required.
> +    """
> +    # make sure that dfu_alt_info exists even persistent variables
> +    # are not available.
> +    output = u_boot_console.run_command_list([
> +        'env set dfu_alt_info '
> +                '"sf 0:0=u-boot-bin raw 0x100000 0x50000;'
> +                'u-boot-env raw 0x150000 0x200000"',
> +        f'host bind 0 {disk_img}',
> +        f'fatls host 0:1 {CAPSULE_INSTALL_DIR}'])
> +    for name in filenames:
> +        assert name in ''.join(output)
> +
> +    # need to run uefi command to initiate capsule handling
> +    u_boot_console.run_command(
> +        'env print -e Capsule0000', wait_for_reboot = need_reboot)
> +
> +def check_file_removed(u_boot_console, disk_img, filenames):
> +    """check files are removed
> +
> +    Args:
> +        u_boot_console -- A console connection to U-Boot.
> +        disk_img -- A path to disk image to be used for testing.
> +        filenames -- File name array of the target capsule files.
> +    """
> +    output = u_boot_console.run_command_list([
> +        f'host bind 0 {disk_img}',
> +        f'fatls host 0:1 {CAPSULE_INSTALL_DIR}'])
> +    for name in filenames:
> +        assert name not in ''.join(output)
> +
> +def check_file_exist(u_boot_console, disk_img, filenames):
> +    """check files exist
> +
> +    Args:
> +        u_boot_console -- A console connection to U-Boot.
> +        disk_img -- A path to disk image to be used for testing.
> +        filenames -- File name array of the target capsule files.
> +    """
> +    output = u_boot_console.run_command_list([
> +        f'host bind 0 {disk_img}',
> +        f'fatls host 0:1 {CAPSULE_INSTALL_DIR}'])
> +    for name in filenames:
> +        assert name in ''.join(output)
> +
> +def verify_content(u_boot_console, target, expected):
> +    """verify the content
> +
> +    Args:
> +        u_boot_console -- A console connection to U-Boot.
> +        target -- Target address to verify.
> +        expected -- Expected string of the content.
> +    """
> +    output = u_boot_console.run_command_list([
> +        'sf probe 0:0',
> +        f'sf read 4000000 {target} 10',
> +        'md.b 4000000 10'])
> +    assert expected in ''.join(output)
> +
> +def do_reboot_dtb_specified(u_boot_config, u_boot_console, dtb_filename):
> +    """do reboot with specified DTB
> +
> +    Args:
> +        u_boot_config -- U-boot configuration.
> +        u_boot_console -- A console connection to U-Boot.
> +        dtb_filename -- DTB file name.
> +    """
> +    mnt_point = u_boot_config.persistent_data_dir + '/test_efi_capsule'
> +    u_boot_console.config.dtb = mnt_point + CAPSULE_DATA_DIR \
> +                                + f'/{dtb_filename}'
> +    u_boot_console.restart_uboot()
> diff --git a/test/py/tests/test_efi_capsule/test_capsule_firmware_fit.py b/test/py/tests/test_efi_capsule/test_capsule_firmware_fit.py
> index 9ee152818d..dd3dfdc047 100644
> --- a/test/py/tests/test_efi_capsule/test_capsule_firmware_fit.py
> +++ b/test/py/tests/test_efi_capsule/test_capsule_firmware_fit.py
> @@ -7,8 +7,14 @@ This test verifies capsule-on-disk firmware update for FIT images
>  """
>
>  import pytest
> -from capsule_defs import CAPSULE_DATA_DIR, CAPSULE_INSTALL_DIR
> -
> +from capsule_common import (
> +    setup,
> +    init_content,
> +    place_capsule_file,
> +    exec_manual_update,
> +    check_file_removed,
> +    verify_content
> +)
>
>  @pytest.mark.boardspec('sandbox_flattree')
>  @pytest.mark.buildconfigspec('efi_capsule_firmware_fit')
> @@ -40,37 +46,12 @@ class TestEfiCapsuleFirmwareFit():
>          u_boot_console.restart_uboot()
>
>          disk_img = efi_capsule_data
> +        capsule_files = ['Test05']
>          with u_boot_console.log.section('Test Case 1-a, before reboot'):
> -            output = u_boot_console.run_command_list([
> -                'host bind 0 %s' % disk_img,
> -                'efidebug boot add -b 1 TEST host 0:1 /helloworld.efi -s ""',
> -                'efidebug boot order 1',
> -                'env set -e -nv -bs -rt OsIndications =0x0000000000000004',
> -                'env set dfu_alt_info "sf 0:0=u-boot-bin raw 0x100000 0x50000;u-boot-env raw 0x150000 0x200000"',
> -                'env save'])
> -
> -            # initialize contents
> -            output = u_boot_console.run_command_list([
> -                'sf probe 0:0',
> -                'fatload host 0:1 4000000 %s/u-boot.bin.old' % CAPSULE_DATA_DIR,
> -                'sf write 4000000 100000 10',
> -                'sf read 5000000 100000 10',
> -                'md.b 5000000 10'])
> -            assert 'Old' in ''.join(output)
> -            output = u_boot_console.run_command_list([
> -                'sf probe 0:0',
> -                'fatload host 0:1 4000000 %s/u-boot.env.old' % CAPSULE_DATA_DIR,
> -                'sf write 4000000 150000 10',
> -                'sf read 5000000 150000 10',
> -                'md.b 5000000 10'])
> -            assert 'Old' in ''.join(output)
> -
> -            # place a capsule file
> -            output = u_boot_console.run_command_list([
> -                'fatload host 0:1 4000000 %s/Test05' % CAPSULE_DATA_DIR,
> -                'fatwrite host 0:1 4000000 %s/Test05 $filesize' % CAPSULE_INSTALL_DIR,
> -                'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
> -            assert 'Test05' in ''.join(output)
> +            setup(u_boot_console, disk_img, '0x0000000000000004')
> +            init_content(u_boot_console, '100000', 'u-boot.bin.old', 'Old')
> +            init_content(u_boot_console, '150000', 'u-boot.env.old', 'Old')
> +            place_capsule_file(u_boot_console, capsule_files)
>
>          capsule_early = u_boot_config.buildconfig.get(
>              'config_efi_capsule_on_disk_early')
> @@ -80,28 +61,13 @@ class TestEfiCapsuleFirmwareFit():
>
>          with u_boot_console.log.section('Test Case 1-b, after reboot'):
>              if not capsule_early:
> -                # make sure that dfu_alt_info exists even persistent variables
> -                # are not available.
> -                output = u_boot_console.run_command_list([
> -                    'env set dfu_alt_info "sf 0:0=u-boot-bin raw 0x100000 0x50000;u-boot-env raw 0x150000 0x200000"',
> -                    'host bind 0 %s' % disk_img,
> -                    'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
> -                assert 'Test05' in ''.join(output)
> -
> -                # need to run uefi command to initiate capsule handling
> -                output = u_boot_console.run_command(
> -                    'env print -e Capsule0000', wait_for_reboot = True)
> -
> -            output = u_boot_console.run_command_list([
> -                'sf probe 0:0',
> -                'sf read 4000000 100000 10',
> -                'md.b 4000000 10'])
> -            assert 'u-boot:Old' in ''.join(output)
> -
> -            output = u_boot_console.run_command_list([
> -                'sf read 4000000 150000 10',
> -                'md.b 4000000 10'])
> -            assert 'u-boot-env:Old' in ''.join(output)
> +                exec_manual_update(u_boot_console, disk_img, capsule_files)
> +
> +            # deleted anyway
> +            check_file_removed(u_boot_console, disk_img, capsule_files)
> +
> +            verify_content(u_boot_console, '100000', 'u-boot:Old')
> +            verify_content(u_boot_console, '150000', 'u-boot-env:Old')
>
>      def test_efi_capsule_fw2(
>              self, u_boot_config, u_boot_console, efi_capsule_data):
> @@ -112,38 +78,12 @@ class TestEfiCapsuleFirmwareFit():
>          """
>
>          disk_img = efi_capsule_data
> +        capsule_files = ['Test04']
>          with u_boot_console.log.section('Test Case 2-a, before reboot'):
> -            output = u_boot_console.run_command_list([
> -                'host bind 0 %s' % disk_img,
> -                'printenv -e PlatformLangCodes', # workaround for terminal size determination
> -                'efidebug boot add -b 1 TEST host 0:1 /helloworld.efi -s ""',
> -                'efidebug boot order 1',
> -                'env set -e -nv -bs -rt OsIndications =0x0000000000000004',
> -                'env set dfu_alt_info "sf 0:0=u-boot-bin raw 0x100000 0x50000;u-boot-env raw 0x150000 0x200000"',
> -                'env save'])
> -
> -            # initialize contents
> -            output = u_boot_console.run_command_list([
> -                'sf probe 0:0',
> -                'fatload host 0:1 4000000 %s/u-boot.bin.old' % CAPSULE_DATA_DIR,
> -                'sf write 4000000 100000 10',
> -                'sf read 5000000 100000 10',
> -                'md.b 5000000 10'])
> -            assert 'Old' in ''.join(output)
> -            output = u_boot_console.run_command_list([
> -                'sf probe 0:0',
> -                'fatload host 0:1 4000000 %s/u-boot.env.old' % CAPSULE_DATA_DIR,
> -                'sf write 4000000 150000 10',
> -                'sf read 5000000 150000 10',
> -                'md.b 5000000 10'])
> -            assert 'Old' in ''.join(output)
> -
> -            # place a capsule file
> -            output = u_boot_console.run_command_list([
> -                'fatload host 0:1 4000000 %s/Test04' % CAPSULE_DATA_DIR,
> -                'fatwrite host 0:1 4000000 %s/Test04 $filesize' % CAPSULE_INSTALL_DIR,
> -                'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
> -            assert 'Test04' in ''.join(output)
> +            setup(u_boot_console, disk_img, '0x0000000000000004')
> +            init_content(u_boot_console, '100000', 'u-boot.bin.old', 'Old')
> +            init_content(u_boot_console, '150000', 'u-boot.env.old', 'Old')
> +            place_capsule_file(u_boot_console, capsule_files)
>
>          capsule_early = u_boot_config.buildconfig.get(
>              'config_efi_capsule_on_disk_early')
> @@ -155,36 +95,12 @@ class TestEfiCapsuleFirmwareFit():
>
>          with u_boot_console.log.section('Test Case 2-b, after reboot'):
>              if not capsule_early:
> -                # make sure that dfu_alt_info exists even persistent variables
> -                # are not available.
> -                output = u_boot_console.run_command_list([
> -                    'env set dfu_alt_info "sf 0:0=u-boot-bin raw 0x100000 0x50000;u-boot-env raw 0x150000 0x200000"',
> -                    'host bind 0 %s' % disk_img,
> -                    'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
> -                assert 'Test04' in ''.join(output)
> -
> -                # need to run uefi command to initiate capsule handling
> -                output = u_boot_console.run_command(
> -                    'env print -e Capsule0000', wait_for_reboot = True)
> -
> -            output = u_boot_console.run_command_list([
> -                'host bind 0 %s' % disk_img,
> -                'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
> -            assert 'Test04' not in ''.join(output)
> -
> -            output = u_boot_console.run_command_list([
> -                'sf probe 0:0',
> -                'sf read 4000000 100000 10',
> -                'md.b 4000000 10'])
> -            if capsule_auth:
> -                assert 'u-boot:Old' in ''.join(output)
> -            else:
> -                assert 'u-boot:New' in ''.join(output)
> -
> -            output = u_boot_console.run_command_list([
> -                'sf read 4000000 150000 10',
> -                'md.b 4000000 10'])
> -            if capsule_auth:
> -                assert 'u-boot-env:Old' in ''.join(output)
> -            else:
> -                assert 'u-boot-env:New' in ''.join(output)
> +                exec_manual_update(u_boot_console, disk_img, capsule_files)
> +
> +            check_file_removed(u_boot_console, disk_img, capsule_files)
> +
> +            expected = 'u-boot:Old' if capsule_auth else 'u-boot:New'
> +            verify_content(u_boot_console, '100000', expected)
> +
> +            expected = 'u-boot-env:Old' if capsule_auth else 'u-boot-env:New'
> +            verify_content(u_boot_console, '150000', expected)
> diff --git a/test/py/tests/test_efi_capsule/test_capsule_firmware_raw.py b/test/py/tests/test_efi_capsule/test_capsule_firmware_raw.py
> index 92bfb14932..e525096d8e 100644
> --- a/test/py/tests/test_efi_capsule/test_capsule_firmware_raw.py
> +++ b/test/py/tests/test_efi_capsule/test_capsule_firmware_raw.py
> @@ -7,7 +7,15 @@ This test verifies capsule-on-disk firmware update for raw images
>  """
>
>  import pytest
> -from capsule_defs import CAPSULE_DATA_DIR, CAPSULE_INSTALL_DIR
> +from capsule_common import (
> +    setup,
> +    init_content,
> +    place_capsule_file,
> +    exec_manual_update,
> +    check_file_removed,
> +    check_file_exist,
> +    verify_content
> +)
>
>  @pytest.mark.boardspec('sandbox')
>  @pytest.mark.buildconfigspec('efi_capsule_firmware_raw')
> @@ -40,37 +48,12 @@ class TestEfiCapsuleFirmwareRaw:
>          u_boot_console.restart_uboot()
>
>          disk_img = efi_capsule_data
> +        capsule_files = ['Test03']
>          with u_boot_console.log.section('Test Case 1-a, before reboot'):
> -            output = u_boot_console.run_command_list([
> -                'host bind 0 %s' % disk_img,
> -                'efidebug boot add -b 1 TEST host 0:1 /helloworld.efi -s ""',
> -                'efidebug boot order 1',
> -                'env set -e -nv -bs -rt OsIndications =0x0000000000000004',
> -                'env set dfu_alt_info "sf 0:0=u-boot-bin raw 0x100000 0x50000;u-boot-env raw 0x150000 0x200000"',
> -                'env save'])
> -
> -            # initialize contents
> -            output = u_boot_console.run_command_list([
> -                'sf probe 0:0',
> -                'fatload host 0:1 4000000 %s/u-boot.bin.old' % CAPSULE_DATA_DIR,
> -                'sf write 4000000 100000 10',
> -                'sf read 5000000 100000 10',
> -                'md.b 5000000 10'])
> -            assert 'Old' in ''.join(output)
> -            output = u_boot_console.run_command_list([
> -                'sf probe 0:0',
> -                'fatload host 0:1 4000000 %s/u-boot.env.old' % CAPSULE_DATA_DIR,
> -                'sf write 4000000 150000 10',
> -                'sf read 5000000 150000 10',
> -                'md.b 5000000 10'])
> -            assert 'Old' in ''.join(output)
> -
> -            # place a capsule file
> -            output = u_boot_console.run_command_list([
> -                'fatload host 0:1 4000000 %s/Test03' % CAPSULE_DATA_DIR,
> -                'fatwrite host 0:1 4000000 %s/Test03 $filesize' % CAPSULE_INSTALL_DIR,
> -                'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
> -            assert 'Test03' in ''.join(output)
> +            setup(u_boot_console, disk_img, '0x0000000000000004')
> +            init_content(u_boot_console, '100000', 'u-boot.bin.old', 'Old')
> +            init_content(u_boot_console, '150000', 'u-boot.env.old', 'Old')
> +            place_capsule_file(u_boot_console, capsule_files)
>
>          # reboot
>          u_boot_console.restart_uboot()
> @@ -80,28 +63,13 @@ class TestEfiCapsuleFirmwareRaw:
>
>          with u_boot_console.log.section('Test Case 1-b, after reboot'):
>              if not capsule_early:
> -                # make sure that dfu_alt_info exists even persistent variables
> -                # are not available.
> -                output = u_boot_console.run_command_list([
> -                    'env set dfu_alt_info "sf 0:0=u-boot-bin raw 0x100000 0x50000;u-boot-env raw 0x150000 0x200000"',
> -                    'host bind 0 %s' % disk_img,
> -                    'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
> -                assert 'Test03' in ''.join(output)
> +                exec_manual_update(u_boot_console, disk_img, capsule_files)
>
> -                # need to run uefi command to initiate capsule handling
> -                output = u_boot_console.run_command(
> -                    'env print -e Capsule0000', wait_for_reboot = True)
> +            # deleted anyway
> +            check_file_removed(u_boot_console, disk_img, capsule_files)
>
> -            output = u_boot_console.run_command_list([
> -                'sf probe 0:0',
> -                'sf read 4000000 100000 10',
> -                'md.b 4000000 10'])
> -            assert 'u-boot:Old' in ''.join(output)
> -
> -            output = u_boot_console.run_command_list([
> -                'sf read 4000000 150000 10',
> -                'md.b 4000000 10'])
> -            assert 'u-boot-env:Old' in ''.join(output)
> +            verify_content(u_boot_console, '100000', 'u-boot:Old')
> +            verify_content(u_boot_console, '150000', 'u-boot-env:Old')
>
>      def test_efi_capsule_fw2(
>              self, u_boot_config, u_boot_console, efi_capsule_data):
> @@ -112,44 +80,12 @@ class TestEfiCapsuleFirmwareRaw:
>          0x150000-0x200000: U-Boot environment (but dummy)
>          """
>          disk_img = efi_capsule_data
> +        capsule_files = ['Test01', 'Test02']
>          with u_boot_console.log.section('Test Case 2-a, before reboot'):
> -            output = u_boot_console.run_command_list([
> -                'host bind 0 %s' % disk_img,
> -                'printenv -e PlatformLangCodes', # workaround for terminal size determination
> -                'efidebug boot add -b 1 TEST host 0:1 /helloworld.efi -s ""',
> -                'efidebug boot order 1',
> -                'env set -e OsIndications',
> -                'env set dfu_alt_info "sf 0:0=u-boot-bin raw 0x100000 0x50000;u-boot-env raw 0x150000 0x200000"',
> -                'env save'])
> -
> -            # initialize contents
> -            output = u_boot_console.run_command_list([
> -                'sf probe 0:0',
> -                'fatload host 0:1 4000000 %s/u-boot.bin.old' % CAPSULE_DATA_DIR,
> -                'sf write 4000000 100000 10',
> -                'sf read 5000000 100000 10',
> -                'md.b 5000000 10'])
> -            assert 'Old' in ''.join(output)
> -            output = u_boot_console.run_command_list([
> -                'sf probe 0:0',
> -                'fatload host 0:1 4000000 %s/u-boot.env.old' % CAPSULE_DATA_DIR,
> -                'sf write 4000000 150000 10',
> -                'sf read 5000000 150000 10',
> -                'md.b 5000000 10'])
> -            assert 'Old' in ''.join(output)
> -
> -            # place the capsule files
> -            output = u_boot_console.run_command_list([
> -                'fatload host 0:1 4000000 %s/Test01' % CAPSULE_DATA_DIR,
> -                'fatwrite host 0:1 4000000 %s/Test01 $filesize' % CAPSULE_INSTALL_DIR,
> -                'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
> -            assert 'Test01' in ''.join(output)
> -
> -            output = u_boot_console.run_command_list([
> -                'fatload host 0:1 4000000 %s/Test02' % CAPSULE_DATA_DIR,
> -                'fatwrite host 0:1 4000000 %s/Test02 $filesize' % CAPSULE_INSTALL_DIR,
> -                'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
> -            assert 'Test02' in ''.join(output)
> +            setup(u_boot_console, disk_img, None)
> +            init_content(u_boot_console, '100000', 'u-boot.bin.old', 'Old')
> +            init_content(u_boot_console, '150000', 'u-boot.env.old', 'Old')
> +            place_capsule_file(u_boot_console, capsule_files)
>
>          # reboot
>          u_boot_console.restart_uboot()
> @@ -158,35 +94,12 @@ class TestEfiCapsuleFirmwareRaw:
>              'config_efi_capsule_on_disk_early')
>          with u_boot_console.log.section('Test Case 2-b, after reboot'):
>              if not capsule_early:
> -                # make sure that dfu_alt_info exists even persistent variables
> -                # are not available.
> -                output = u_boot_console.run_command_list([
> -                    'env set dfu_alt_info "sf 0:0=u-boot-bin raw 0x100000 0x50000;u-boot-env raw 0x150000 0x200000"',
> -                    'host bind 0 %s' % disk_img,
> -                    'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
> -                assert 'Test01' in ''.join(output)
> -                assert 'Test02' in ''.join(output)
> +                exec_manual_update(u_boot_console, disk_img, capsule_files, False)
>
> -                # need to run uefi command to initiate capsule handling
> -                output = u_boot_console.run_command(
> -                    'env print -e Capsule0000')
> +            check_file_exist(u_boot_console, disk_img, capsule_files)
>
> -            output = u_boot_console.run_command_list([
> -                'host bind 0 %s' % disk_img,
> -                'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
> -            assert 'Test01' in ''.join(output)
> -            assert 'Test02' in ''.join(output)
> -
> -            output = u_boot_console.run_command_list([
> -                'sf probe 0:0',
> -                'sf read 4000000 100000 10',
> -                'md.b 4000000 10'])
> -            assert 'u-boot:Old' in ''.join(output)
> -
> -            output = u_boot_console.run_command_list([
> -                'sf read 4000000 150000 10',
> -                'md.b 4000000 10'])
> -            assert 'u-boot-env:Old' in ''.join(output)
> +            verify_content(u_boot_console, '100000', 'u-boot:Old')
> +            verify_content(u_boot_console, '150000', 'u-boot-env:Old')
>
>      def test_efi_capsule_fw3(
>              self, u_boot_config, u_boot_console, efi_capsule_data):
> @@ -195,45 +108,12 @@ class TestEfiCapsuleFirmwareRaw:
>          0x100000-0x150000: U-Boot binary (but dummy)
>          """
>          disk_img = efi_capsule_data
> +        capsule_files = ['Test01', 'Test02']
>          with u_boot_console.log.section('Test Case 3-a, before reboot'):
> -            output = u_boot_console.run_command_list([
> -                'host bind 0 %s' % disk_img,
> -                'printenv -e PlatformLangCodes', # workaround for terminal size determination
> -                'efidebug boot add -b 1 TEST host 0:1 /helloworld.efi -s ""',
> -                'efidebug boot order 1',
> -                'env set -e -nv -bs -rt OsIndications =0x0000000000000004',
> -                'env set dfu_alt_info "sf 0:0=u-boot-bin raw 0x100000 0x50000;u-boot-env raw 0x150000 0x200000"',
> -                'env save'])
> -
> -            # initialize contents
> -            output = u_boot_console.run_command_list([
> -                'sf probe 0:0',
> -                'fatload host 0:1 4000000 %s/u-boot.bin.old' % CAPSULE_DATA_DIR,
> -                'sf write 4000000 100000 10',
> -                'sf read 5000000 100000 10',
> -                'md.b 5000000 10'])
> -            assert 'Old' in ''.join(output)
> -
> -            output = u_boot_console.run_command_list([
> -                'sf probe 0:0',
> -                'fatload host 0:1 4000000 %s/u-boot.env.old' % CAPSULE_DATA_DIR,
> -                'sf write 4000000 150000 10',
> -                'sf read 5000000 100000 10',
> -                'md.b 5000000 10'])
> -            assert 'Old' in ''.join(output)
> -
> -            # place the capsule files
> -            output = u_boot_console.run_command_list([
> -                'fatload host 0:1 4000000 %s/Test01' % CAPSULE_DATA_DIR,
> -                'fatwrite host 0:1 4000000 %s/Test01 $filesize' % CAPSULE_INSTALL_DIR,
> -                'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
> -            assert 'Test01' in ''.join(output)
> -
> -            output = u_boot_console.run_command_list([
> -                'fatload host 0:1 4000000 %s/Test02' % CAPSULE_DATA_DIR,
> -                'fatwrite host 0:1 4000000 %s/Test02 $filesize' % CAPSULE_INSTALL_DIR,
> -                'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
> -            assert 'Test02' in ''.join(output)
> +            setup(u_boot_console, disk_img, '0x0000000000000004')
> +            init_content(u_boot_console, '100000', 'u-boot.bin.old', 'Old')
> +            init_content(u_boot_console, '150000', 'u-boot.env.old', 'Old')
> +            place_capsule_file(u_boot_console, capsule_files)
>
>          capsule_early = u_boot_config.buildconfig.get(
>              'config_efi_capsule_on_disk_early')
> @@ -245,18 +125,7 @@ class TestEfiCapsuleFirmwareRaw:
>
>          with u_boot_console.log.section('Test Case 3-b, after reboot'):
>              if not capsule_early:
> -                # make sure that dfu_alt_info exists even persistent variables
> -                # are not available.
> -                output = u_boot_console.run_command_list([
> -                    'env set dfu_alt_info "sf 0:0=u-boot-bin raw 0x100000 0x50000;u-boot-env raw 0x150000 0x200000"',
> -                    'host bind 0 %s' % disk_img,
> -                    'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
> -                assert 'Test01' in ''.join(output)
> -                assert 'Test02' in ''.join(output)
> -
> -                # need to run uefi command to initiate capsule handling
> -                output = u_boot_console.run_command(
> -                    'env print -e Capsule0000', wait_for_reboot = True)
> +                exec_manual_update(u_boot_console, disk_img, capsule_files)
>
>              # make sure the dfu_alt_info exists because it is required for making ESRT.
>              output = u_boot_console.run_command_list([
> @@ -269,26 +138,10 @@ class TestEfiCapsuleFirmwareRaw:
>              # ensure that SANDBOX_UBOOT_IMAGE_GUID is in the ESRT.
>              assert '09D7CF52-0720-4710-91D1-08469B7FE9C8' in ''.join(output)
>
> -            output = u_boot_console.run_command_list([
> -                'host bind 0 %s' % disk_img,
> -                'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
> -            assert 'Test01' not in ''.join(output)
> -            assert 'Test02' not in ''.join(output)
> +            check_file_removed(u_boot_console, disk_img, capsule_files)
>
> -            output = u_boot_console.run_command_list([
> -                'sf probe 0:0',
> -                'sf read 4000000 100000 10',
> -                'md.b 4000000 10'])
> -            if capsule_auth:
> -                assert 'u-boot:Old' in ''.join(output)
> -            else:
> -                assert 'u-boot:New' in ''.join(output)
> +            expected = 'u-boot:Old' if capsule_auth else 'u-boot:New'
> +            verify_content(u_boot_console, '100000', expected)
>
> -            output = u_boot_console.run_command_list([
> -                'sf probe 0:0',
> -                'sf read 4000000 150000 10',
> -                'md.b 4000000 10'])
> -            if capsule_auth:
> -                assert 'u-boot-env:Old' in ''.join(output)
> -            else:
> -                assert 'u-boot-env:New' in ''.join(output)
> +            expected = 'u-boot-env:Old' if capsule_auth else 'u-boot-env:New'
> +            verify_content(u_boot_console, '150000', expected)
> diff --git a/test/py/tests/test_efi_capsule/test_capsule_firmware_signed_fit.py b/test/py/tests/test_efi_capsule/test_capsule_firmware_signed_fit.py
> index ba8429e83c..70f24e8ce7 100644
> --- a/test/py/tests/test_efi_capsule/test_capsule_firmware_signed_fit.py
> +++ b/test/py/tests/test_efi_capsule/test_capsule_firmware_signed_fit.py
> @@ -10,7 +10,15 @@ with signed capsule files containing FIT images
>  """
>
>  import pytest
> -from capsule_defs import CAPSULE_DATA_DIR, CAPSULE_INSTALL_DIR
> +from capsule_common import (
> +    setup,
> +    init_content,
> +    place_capsule_file,
> +    exec_manual_update,
> +    check_file_removed,
> +    verify_content,
> +    do_reboot_dtb_specified
> +)
>
>  @pytest.mark.boardspec('sandbox_flattree')
>  @pytest.mark.buildconfigspec('efi_capsule_firmware_fit')
> @@ -37,70 +45,23 @@ class TestEfiCapsuleFirmwareSignedFit():
>          should pass and the firmware be updated.
>          """
>          disk_img = efi_capsule_data
> +        capsule_files = ['Test13']
>          with u_boot_console.log.section('Test Case 1-a, before reboot'):
> -            output = u_boot_console.run_command_list([
> -                'host bind 0 %s' % disk_img,
> -                'printenv -e PlatformLangCodes', # workaround for terminal size determination
> -                'efidebug boot add -b 1 TEST host 0:1 /helloworld.efi',
> -                'efidebug boot order 1',
> -                'env set -e -nv -bs -rt OsIndications =0x0000000000000004',
> -                'env set dfu_alt_info '
> -                        '"sf 0:0=u-boot-bin raw 0x100000 '
> -                        '0x50000;u-boot-env raw 0x150000 0x200000"',
> -                'env save'])
> +            setup(u_boot_console, disk_img, '0x0000000000000004')
> +            init_content(u_boot_console, '100000', 'u-boot.bin.old', 'Old')
> +            place_capsule_file(u_boot_console, capsule_files)
>
> -            # initialize content
> -            output = u_boot_console.run_command_list([
> -                'sf probe 0:0',
> -                'fatload host 0:1 4000000 %s/u-boot.bin.old'
> -                        % CAPSULE_DATA_DIR,
> -                'sf write 4000000 100000 10',
> -                'sf read 5000000 100000 10',
> -                'md.b 5000000 10'])
> -            assert 'Old' in ''.join(output)
> -
> -            # place a capsule file
> -            output = u_boot_console.run_command_list([
> -                'fatload host 0:1 4000000 %s/Test13' % CAPSULE_DATA_DIR,
> -                'fatwrite host 0:1 4000000 %s/Test13 $filesize'
> -                        % CAPSULE_INSTALL_DIR,
> -                'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
> -            assert 'Test13' in ''.join(output)
> -
> -        # reboot
> -        mnt_point = u_boot_config.persistent_data_dir + '/test_efi_capsule'
> -        u_boot_console.config.dtb = mnt_point + CAPSULE_DATA_DIR \
> -                                    + '/test_sig.dtb'
> -        u_boot_console.restart_uboot()
> +        do_reboot_dtb_specified(u_boot_config, u_boot_console, 'test_sig.dtb')
>
>          capsule_early = u_boot_config.buildconfig.get(
>              'config_efi_capsule_on_disk_early')
>          with u_boot_console.log.section('Test Case 1-b, after reboot'):
>              if not capsule_early:
> -                # make sure that dfu_alt_info exists even persistent variables
> -                # are not available.
> -                output = u_boot_console.run_command_list([
> -                    'env set dfu_alt_info '
> -                            '"sf 0:0=u-boot-bin raw 0x100000 '
> -                            '0x50000;u-boot-env raw 0x150000 0x200000"',
> -                    'host bind 0 %s' % disk_img,
> -                    'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
> -                assert 'Test13' in ''.join(output)
> -
> -                # need to run uefi command to initiate capsule handling
> -                output = u_boot_console.run_command(
> -                    'env print -e Capsule0000', wait_for_reboot = True)
> +                exec_manual_update(u_boot_console, disk_img, capsule_files)
>
> -            output = u_boot_console.run_command_list([
> -                'host bind 0 %s' % disk_img,
> -                'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
> -            assert 'Test13' not in ''.join(output)
> +            check_file_removed(u_boot_console, disk_img, capsule_files)
>
> -            output = u_boot_console.run_command_list([
> -                'sf probe 0:0',
> -                'sf read 4000000 100000 10',
> -                'md.b 4000000 10'])
> -            assert 'u-boot:New' in ''.join(output)
> +            verify_content(u_boot_console, '100000', 'u-boot:New')
>
>      def test_efi_capsule_auth2(
>              self, u_boot_config, u_boot_console, efi_capsule_data):
> @@ -113,73 +74,26 @@ class TestEfiCapsuleFirmwareSignedFit():
>          not be updated.
>          """
>          disk_img = efi_capsule_data
> +        capsule_files = ['Test14']
>          with u_boot_console.log.section('Test Case 2-a, before reboot'):
> -            output = u_boot_console.run_command_list([
> -                'host bind 0 %s' % disk_img,
> -                'printenv -e PlatformLangCodes', # workaround for terminal size determination
> -                'efidebug boot add -b 1 TEST host 0:1 /helloworld.efi',
> -                'efidebug boot order 1',
> -                'env set -e -nv -bs -rt OsIndications =0x0000000000000004',
> -                'env set dfu_alt_info '
> -                        '"sf 0:0=u-boot-bin raw 0x100000 '
> -                        '0x50000;u-boot-env raw 0x150000 0x200000"',
> -                'env save'])
> -
> -            # initialize content
> -            output = u_boot_console.run_command_list([
> -                'sf probe 0:0',
> -                'fatload host 0:1 4000000 %s/u-boot.bin.old'
> -                        % CAPSULE_DATA_DIR,
> -                'sf write 4000000 100000 10',
> -                'sf read 5000000 100000 10',
> -                'md.b 5000000 10'])
> -            assert 'Old' in ''.join(output)
> -
> -            # place a capsule file
> -            output = u_boot_console.run_command_list([
> -                'fatload host 0:1 4000000 %s/Test14' % CAPSULE_DATA_DIR,
> -                'fatwrite host 0:1 4000000 %s/Test14 $filesize'
> -                                % CAPSULE_INSTALL_DIR,
> -                'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
> -            assert 'Test14' in ''.join(output)
> +            setup(u_boot_console, disk_img, '0x0000000000000004')
> +            init_content(u_boot_console, '100000', 'u-boot.bin.old', 'Old')
> +            place_capsule_file(u_boot_console, capsule_files)
>
> -        # reboot
> -        mnt_point = u_boot_config.persistent_data_dir + '/test_efi_capsule'
> -        u_boot_console.config.dtb = mnt_point + CAPSULE_DATA_DIR \
> -                                    + '/test_sig.dtb'
> -        u_boot_console.restart_uboot()
> +        do_reboot_dtb_specified(u_boot_config, u_boot_console, 'test_sig.dtb')
>
>          capsule_early = u_boot_config.buildconfig.get(
>              'config_efi_capsule_on_disk_early')
>          with u_boot_console.log.section('Test Case 2-b, after reboot'):
>              if not capsule_early:
> -                # make sure that dfu_alt_info exists even persistent variables
> -                # are not available.
> -                output = u_boot_console.run_command_list([
> -                    'env set dfu_alt_info '
> -                        '"sf 0:0=u-boot-bin raw 0x100000 '
> -                        '0x50000;u-boot-env raw 0x150000 0x200000"',
> -                    'host bind 0 %s' % disk_img,
> -                    'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
> -                assert 'Test14' in ''.join(output)
> -
> -                # need to run uefi command to initiate capsule handling
> -                output = u_boot_console.run_command(
> -                    'env print -e Capsule0000', wait_for_reboot = True)
> +                exec_manual_update(u_boot_console, disk_img, capsule_files)
>
>              # deleted any way
> -            output = u_boot_console.run_command_list([
> -                'host bind 0 %s' % disk_img,
> -                'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
> -            assert 'Test14' not in ''.join(output)
> +            check_file_removed(u_boot_console, disk_img, capsule_files)
>
>              # TODO: check CapsuleStatus in CapsuleXXXX
>
> -            output = u_boot_console.run_command_list([
> -                'sf probe 0:0',
> -                'sf read 4000000 100000 10',
> -                'md.b 4000000 10'])
> -            assert 'u-boot:Old' in ''.join(output)
> +            verify_content(u_boot_console, '100000', 'u-boot:Old')
>
>      def test_efi_capsule_auth3(
>              self, u_boot_config, u_boot_console, efi_capsule_data):
> @@ -191,70 +105,23 @@ class TestEfiCapsuleFirmwareSignedFit():
>          should fail and the firmware not be updated.
>          """
>          disk_img = efi_capsule_data
> +        capsule_files = ['Test02']
>          with u_boot_console.log.section('Test Case 3-a, before reboot'):
> -            output = u_boot_console.run_command_list([
> -                'host bind 0 %s' % disk_img,
> -                'printenv -e PlatformLangCodes', # workaround for terminal size determination
> -                'efidebug boot add -b 1 TEST host 0:1 /helloworld.efi',
> -                'efidebug boot order 1',
> -                'env set -e -nv -bs -rt OsIndications =0x0000000000000004',
> -                'env set dfu_alt_info '
> -                        '"sf 0:0=u-boot-bin raw 0x100000 '
> -                        '0x50000;u-boot-env raw 0x150000 0x200000"',
> -                'env save'])
> -
> -            # initialize content
> -            output = u_boot_console.run_command_list([
> -                'sf probe 0:0',
> -                'fatload host 0:1 4000000 %s/u-boot.bin.old'
> -                        % CAPSULE_DATA_DIR,
> -                'sf write 4000000 100000 10',
> -                'sf read 5000000 100000 10',
> -                'md.b 5000000 10'])
> -            assert 'Old' in ''.join(output)
> +            setup(u_boot_console, disk_img, '0x0000000000000004')
> +            init_content(u_boot_console, '100000', 'u-boot.bin.old', 'Old')
> +            place_capsule_file(u_boot_console, capsule_files)
>
> -            # place a capsule file
> -            output = u_boot_console.run_command_list([
> -                'fatload host 0:1 4000000 %s/Test02' % CAPSULE_DATA_DIR,
> -                'fatwrite host 0:1 4000000 %s/Test02 $filesize'
> -                            % CAPSULE_INSTALL_DIR,
> -                'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
> -            assert 'Test02' in ''.join(output)
> -
> -        # reboot
> -        mnt_point = u_boot_config.persistent_data_dir + '/test_efi_capsule'
> -        u_boot_console.config.dtb = mnt_point + CAPSULE_DATA_DIR \
> -                                    + '/test_sig.dtb'
> -        u_boot_console.restart_uboot()
> +        do_reboot_dtb_specified(u_boot_config, u_boot_console, 'test_sig.dtb')
>
>          capsule_early = u_boot_config.buildconfig.get(
>              'config_efi_capsule_on_disk_early')
>          with u_boot_console.log.section('Test Case 3-b, after reboot'):
>              if not capsule_early:
> -                # make sure that dfu_alt_info exists even persistent variables
> -                # are not available.
> -                output = u_boot_console.run_command_list([
> -                    'env set dfu_alt_info '
> -                            '"sf 0:0=u-boot-bin raw 0x100000 '
> -                            '0x50000;u-boot-env raw 0x150000 0x200000"',
> -                    'host bind 0 %s' % disk_img,
> -                    'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
> -                assert 'Test02' in ''.join(output)
> -
> -                # need to run uefi command to initiate capsule handling
> -                output = u_boot_console.run_command(
> -                    'env print -e Capsule0000', wait_for_reboot = True)
> +                exec_manual_update(u_boot_console, disk_img, capsule_files)
>
>              # deleted any way
> -            output = u_boot_console.run_command_list([
> -                'host bind 0 %s' % disk_img,
> -                'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
> -            assert 'Test02' not in ''.join(output)
> +            check_file_removed(u_boot_console, disk_img, capsule_files)
>
>              # TODO: check CapsuleStatus in CapsuleXXXX
>
> -            output = u_boot_console.run_command_list([
> -                'sf probe 0:0',
> -                'sf read 4000000 100000 10',
> -                'md.b 4000000 10'])
> -            assert 'u-boot:Old' in ''.join(output)
> +            verify_content(u_boot_console, '100000', 'u-boot:Old')
> diff --git a/test/py/tests/test_efi_capsule/test_capsule_firmware_signed_raw.py b/test/py/tests/test_efi_capsule/test_capsule_firmware_signed_raw.py
> index 710d9925a3..c6109e2103 100644
> --- a/test/py/tests/test_efi_capsule/test_capsule_firmware_signed_raw.py
> +++ b/test/py/tests/test_efi_capsule/test_capsule_firmware_signed_raw.py
> @@ -8,7 +8,15 @@ with signed capsule files containing raw images
>  """
>
>  import pytest
> -from capsule_defs import CAPSULE_DATA_DIR, CAPSULE_INSTALL_DIR
> +from capsule_common import (
> +    setup,
> +    init_content,
> +    place_capsule_file,
> +    exec_manual_update,
> +    check_file_removed,
> +    verify_content,
> +    do_reboot_dtb_specified
> +)
>
>  @pytest.mark.boardspec('sandbox')
>  @pytest.mark.buildconfigspec('efi_capsule_firmware_raw')
> @@ -34,69 +42,23 @@ class TestEfiCapsuleFirmwareSignedRaw():
>          should pass and the firmware be updated.
>          """
>          disk_img = efi_capsule_data
> +        capsule_files =  ['Test11']
>          with u_boot_console.log.section('Test Case 1-a, before reboot'):
> -            output = u_boot_console.run_command_list([
> -                'host bind 0 %s' % disk_img,
> -                'efidebug boot add -b 1 TEST host 0:1 /helloworld.efi',
> -                'efidebug boot order 1',
> -                'env set -e -nv -bs -rt OsIndications =0x0000000000000004',
> -                'env set dfu_alt_info '
> -                        '"sf 0:0=u-boot-bin raw 0x100000 '
> -                        '0x50000;u-boot-env raw 0x150000 0x200000"',
> -                'env save'])
> -
> -            # initialize content
> -            output = u_boot_console.run_command_list([
> -                'sf probe 0:0',
> -                'fatload host 0:1 4000000 %s/u-boot.bin.old'
> -                        % CAPSULE_DATA_DIR,
> -                'sf write 4000000 100000 10',
> -                'sf read 5000000 100000 10',
> -                'md.b 5000000 10'])
> -            assert 'Old' in ''.join(output)
> -
> -            # place a capsule file
> -            output = u_boot_console.run_command_list([
> -                'fatload host 0:1 4000000 %s/Test11' % CAPSULE_DATA_DIR,
> -                'fatwrite host 0:1 4000000 %s/Test11 $filesize'
> -                        % CAPSULE_INSTALL_DIR,
> -                'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
> -            assert 'Test11' in ''.join(output)
> -
> -        # reboot
> -        mnt_point = u_boot_config.persistent_data_dir + '/test_efi_capsule'
> -        u_boot_console.config.dtb = mnt_point + CAPSULE_DATA_DIR \
> -                                    + '/test_sig.dtb'
> -        u_boot_console.restart_uboot()
> +            setup(u_boot_console, disk_img, '0x0000000000000004')
> +            init_content(u_boot_console, '100000', 'u-boot.bin.old', 'Old')
> +            place_capsule_file(u_boot_console, capsule_files)
> +
> +        do_reboot_dtb_specified(u_boot_config, u_boot_console, 'test_sig.dtb')
>
>          capsule_early = u_boot_config.buildconfig.get(
>              'config_efi_capsule_on_disk_early')
>          with u_boot_console.log.section('Test Case 1-b, after reboot'):
>              if not capsule_early:
> -                # make sure that dfu_alt_info exists even persistent variables
> -                # are not available.
> -                output = u_boot_console.run_command_list([
> -                    'env set dfu_alt_info '
> -                            '"sf 0:0=u-boot-bin raw 0x100000 '
> -                            '0x50000;u-boot-env raw 0x150000 0x200000"',
> -                    'host bind 0 %s' % disk_img,
> -                    'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
> -                assert 'Test11' in ''.join(output)
> -
> -                # need to run uefi command to initiate capsule handling
> -                output = u_boot_console.run_command(
> -                    'env print -e Capsule0000', wait_for_reboot = True)
> -
> -            output = u_boot_console.run_command_list([
> -                'host bind 0 %s' % disk_img,
> -                'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
> -            assert 'Test11' not in ''.join(output)
> -
> -            output = u_boot_console.run_command_list([
> -                'sf probe 0:0',
> -                'sf read 4000000 100000 10',
> -                'md.b 4000000 10'])
> -            assert 'u-boot:New' in ''.join(output)
> +                exec_manual_update(u_boot_console, disk_img, capsule_files)
> +
> +            check_file_removed(u_boot_console, disk_img, capsule_files)
> +
> +            verify_content(u_boot_console, '100000', 'u-boot:New')
>
>      def test_efi_capsule_auth2(
>              self, u_boot_config, u_boot_console, efi_capsule_data):
> @@ -108,73 +70,25 @@ class TestEfiCapsuleFirmwareSignedRaw():
>          not be updated.
>          """
>          disk_img = efi_capsule_data
> +        capsule_files = ['Test12']
>          with u_boot_console.log.section('Test Case 2-a, before reboot'):
> -            output = u_boot_console.run_command_list([
> -                'host bind 0 %s' % disk_img,
> -                'printenv -e PlatformLangCodes', # workaround for terminal size determination
> -                'efidebug boot add -b 1 TEST host 0:1 /helloworld.efi',
> -                'efidebug boot order 1',
> -                'env set -e -nv -bs -rt OsIndications =0x0000000000000004',
> -                'env set dfu_alt_info '
> -                        '"sf 0:0=u-boot-bin raw 0x100000 '
> -                        '0x50000;u-boot-env raw 0x150000 0x200000"',
> -                'env save'])
> -
> -            # initialize content
> -            output = u_boot_console.run_command_list([
> -                'sf probe 0:0',
> -                'fatload host 0:1 4000000 %s/u-boot.bin.old'
> -                        % CAPSULE_DATA_DIR,
> -                'sf write 4000000 100000 10',
> -                'sf read 5000000 100000 10',
> -                'md.b 5000000 10'])
> -            assert 'Old' in ''.join(output)
> -
> -            # place a capsule file
> -            output = u_boot_console.run_command_list([
> -                'fatload host 0:1 4000000 %s/Test12' % CAPSULE_DATA_DIR,
> -                'fatwrite host 0:1 4000000 %s/Test12 $filesize'
> -                                % CAPSULE_INSTALL_DIR,
> -                'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
> -            assert 'Test12' in ''.join(output)
> -
> -        # reboot
> -        mnt_point = u_boot_config.persistent_data_dir + '/test_efi_capsule'
> -        u_boot_console.config.dtb = mnt_point + CAPSULE_DATA_DIR \
> -                                    + '/test_sig.dtb'
> -        u_boot_console.restart_uboot()
> +            setup(u_boot_console, disk_img, '0x0000000000000004')
> +            init_content(u_boot_console, '100000', 'u-boot.bin.old', 'Old')
> +            place_capsule_file(u_boot_console, capsule_files)
> +
> +        do_reboot_dtb_specified(u_boot_config, u_boot_console, 'test_sig.dtb')
>
>          capsule_early = u_boot_config.buildconfig.get(
>              'config_efi_capsule_on_disk_early')
>          with u_boot_console.log.section('Test Case 2-b, after reboot'):
>              if not capsule_early:
> -                # make sure that dfu_alt_info exists even persistent variables
> -                # are not available.
> -                output = u_boot_console.run_command_list([
> -                    'env set dfu_alt_info '
> -                        '"sf 0:0=u-boot-bin raw 0x100000 '
> -                        '0x50000;u-boot-env raw 0x150000 0x200000"',
> -                    'host bind 0 %s' % disk_img,
> -                    'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
> -                assert 'Test12' in ''.join(output)
> -
> -                # need to run uefi command to initiate capsule handling
> -                output = u_boot_console.run_command(
> -                    'env print -e Capsule0000', wait_for_reboot = True)
> -
> -            # deleted any way
> -            output = u_boot_console.run_command_list([
> -                'host bind 0 %s' % disk_img,
> -                'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
> -            assert 'Test12' not in ''.join(output)
> +                exec_manual_update(u_boot_console, disk_img, capsule_files)
> +
> +            check_file_removed(u_boot_console, disk_img, capsule_files)
>
>              # TODO: check CapsuleStatus in CapsuleXXXX
>
> -            output = u_boot_console.run_command_list([
> -                'sf probe 0:0',
> -                'sf read 4000000 100000 10',
> -                'md.b 4000000 10'])
> -            assert 'u-boot:Old' in ''.join(output)
> +            verify_content(u_boot_console, '100000', 'u-boot:Old')
>
>      def test_efi_capsule_auth3(
>              self, u_boot_config, u_boot_console, efi_capsule_data):
> @@ -185,70 +99,23 @@ class TestEfiCapsuleFirmwareSignedRaw():
>          should fail and the firmware not be updated.
>          """
>          disk_img = efi_capsule_data
> +        capsule_files = ['Test02']
>          with u_boot_console.log.section('Test Case 3-a, before reboot'):
> -            output = u_boot_console.run_command_list([
> -                'host bind 0 %s' % disk_img,
> -                'printenv -e PlatformLangCodes', # workaround for terminal size determination
> -                'efidebug boot add -b 1 TEST host 0:1 /helloworld.efi',
> -                'efidebug boot order 1',
> -                'env set -e -nv -bs -rt OsIndications =0x0000000000000004',
> -                'env set dfu_alt_info '
> -                        '"sf 0:0=u-boot-bin raw 0x100000 '
> -                        '0x50000;u-boot-env raw 0x150000 0x200000"',
> -                'env save'])
> -
> -            # initialize content
> -            output = u_boot_console.run_command_list([
> -                'sf probe 0:0',
> -                'fatload host 0:1 4000000 %s/u-boot.bin.old'
> -                        % CAPSULE_DATA_DIR,
> -                'sf write 4000000 100000 10',
> -                'sf read 5000000 100000 10',
> -                'md.b 5000000 10'])
> -            assert 'Old' in ''.join(output)
> -
> -            # place a capsule file
> -            output = u_boot_console.run_command_list([
> -                'fatload host 0:1 4000000 %s/Test02' % CAPSULE_DATA_DIR,
> -                'fatwrite host 0:1 4000000 %s/Test02 $filesize'
> -                            % CAPSULE_INSTALL_DIR,
> -                'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
> -            assert 'Test02' in ''.join(output)
> -
> -        # reboot
> -        mnt_point = u_boot_config.persistent_data_dir + '/test_efi_capsule'
> -        u_boot_console.config.dtb = mnt_point + CAPSULE_DATA_DIR \
> -                                    + '/test_sig.dtb'
> -        u_boot_console.restart_uboot()
> +            setup(u_boot_console, disk_img, '0x0000000000000004')
> +            init_content(u_boot_console, '100000', 'u-boot.bin.old', 'Old')
> +            place_capsule_file(u_boot_console, capsule_files)
> +
> +        do_reboot_dtb_specified(u_boot_config, u_boot_console, 'test_sig.dtb')
>
>          capsule_early = u_boot_config.buildconfig.get(
>              'config_efi_capsule_on_disk_early')
>          with u_boot_console.log.section('Test Case 3-b, after reboot'):
>              if not capsule_early:
> -                # make sure that dfu_alt_info exists even persistent variables
> -                # are not available.
> -                output = u_boot_console.run_command_list([
> -                    'env set dfu_alt_info '
> -                            '"sf 0:0=u-boot-bin raw 0x100000 '
> -                            '0x50000;u-boot-env raw 0x150000 0x200000"',
> -                    'host bind 0 %s' % disk_img,
> -                    'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
> -                assert 'Test02' in ''.join(output)
> -
> -                # need to run uefi command to initiate capsule handling
> -                output = u_boot_console.run_command(
> -                    'env print -e Capsule0000', wait_for_reboot = True)
> +                exec_manual_update(u_boot_console, disk_img, capsule_files)
>
>              # deleted anyway
> -            output = u_boot_console.run_command_list([
> -                'host bind 0 %s' % disk_img,
> -                'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
> -            assert 'Test02' not in ''.join(output)
> +            check_file_removed(u_boot_console, disk_img, capsule_files)
>
>              # TODO: check CapsuleStatus in CapsuleXXXX
>
> -            output = u_boot_console.run_command_list([
> -                'sf probe 0:0',
> -                'sf read 4000000 100000 10',
> -                'md.b 4000000 10'])
> -            assert 'u-boot:Old' in ''.join(output)
> +            verify_content(u_boot_console, '100000', 'u-boot:Old')
> --
> 2.34.1
>
Acked-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>
diff mbox series

Patch

diff --git a/test/py/tests/test_efi_capsule/capsule_common.py b/test/py/tests/test_efi_capsule/capsule_common.py
new file mode 100644
index 0000000000..9eef6767a6
--- /dev/null
+++ b/test/py/tests/test_efi_capsule/capsule_common.py
@@ -0,0 +1,142 @@ 
+# SPDX-License-Identifier:      GPL-2.0+
+# Copyright (c) 2023, Linaro Limited
+
+
+"""Common function for UEFI capsule test."""
+
+from capsule_defs import CAPSULE_DATA_DIR, CAPSULE_INSTALL_DIR
+
+def setup(u_boot_console, disk_img, osindications):
+    """setup the test
+
+    Args:
+        u_boot_console -- A console connection to U-Boot.
+        disk_img -- A path to disk image to be used for testing.
+        osindications -- String of osindications value.
+    """
+    u_boot_console.run_command_list([
+        f'host bind 0 {disk_img}',
+        'printenv -e PlatformLangCodes', # workaround for terminal size determination
+        'efidebug boot add -b 1 TEST host 0:1 /helloworld.efi',
+        'efidebug boot order 1',
+        'env set dfu_alt_info "sf 0:0=u-boot-bin raw 0x100000 0x50000;'
+        'u-boot-env raw 0x150000 0x200000"'])
+
+    if osindications is None:
+        u_boot_console.run_command('env set -e OsIndications')
+    else:
+        u_boot_console.run_command(f'env set -e -nv -bs -rt OsIndications ={osindications}')
+
+    u_boot_console.run_command('env save')
+
+def init_content(u_boot_console, target, filename, expected):
+    """initialize test content
+
+    Args:
+        u_boot_console -- A console connection to U-Boot.
+        target -- Target address to place the content.
+        filename -- File name of the content.
+        expected -- Expected string of the content.
+    """
+    output = u_boot_console.run_command_list([
+        'sf probe 0:0',
+        f'fatload host 0:1 4000000 {CAPSULE_DATA_DIR}/{filename}',
+        f'sf write 4000000 {target} 10',
+        'sf read 5000000 100000 10',
+        'md.b 5000000 10'])
+    assert expected in ''.join(output)
+
+def place_capsule_file(u_boot_console, filenames):
+    """place the capsule file
+
+    Args:
+        u_boot_console -- A console connection to U-Boot.
+        filenames -- File name array of the target capsule files.
+    """
+    for name in filenames:
+        u_boot_console.run_command_list([
+            f'fatload host 0:1 4000000 {CAPSULE_DATA_DIR}/{name}',
+            f'fatwrite host 0:1 4000000 {CAPSULE_INSTALL_DIR}/{name} $filesize'])
+
+    output = u_boot_console.run_command(f'fatls host 0:1 {CAPSULE_INSTALL_DIR}')
+    for name in filenames:
+        assert name in ''.join(output)
+
+def exec_manual_update(u_boot_console, disk_img, filenames, need_reboot = True):
+    """execute capsule update manually
+
+    Args:
+        u_boot_console -- A console connection to U-Boot.
+        disk_img -- A path to disk image to be used for testing.
+        filenames -- File name array of the target capsule files.
+        need_reboot -- Flag indicates whether system reboot is required.
+    """
+    # make sure that dfu_alt_info exists even persistent variables
+    # are not available.
+    output = u_boot_console.run_command_list([
+        'env set dfu_alt_info '
+                '"sf 0:0=u-boot-bin raw 0x100000 0x50000;'
+                'u-boot-env raw 0x150000 0x200000"',
+        f'host bind 0 {disk_img}',
+        f'fatls host 0:1 {CAPSULE_INSTALL_DIR}'])
+    for name in filenames:
+        assert name in ''.join(output)
+
+    # need to run uefi command to initiate capsule handling
+    u_boot_console.run_command(
+        'env print -e Capsule0000', wait_for_reboot = need_reboot)
+
+def check_file_removed(u_boot_console, disk_img, filenames):
+    """check files are removed
+
+    Args:
+        u_boot_console -- A console connection to U-Boot.
+        disk_img -- A path to disk image to be used for testing.
+        filenames -- File name array of the target capsule files.
+    """
+    output = u_boot_console.run_command_list([
+        f'host bind 0 {disk_img}',
+        f'fatls host 0:1 {CAPSULE_INSTALL_DIR}'])
+    for name in filenames:
+        assert name not in ''.join(output)
+
+def check_file_exist(u_boot_console, disk_img, filenames):
+    """check files exist
+
+    Args:
+        u_boot_console -- A console connection to U-Boot.
+        disk_img -- A path to disk image to be used for testing.
+        filenames -- File name array of the target capsule files.
+    """
+    output = u_boot_console.run_command_list([
+        f'host bind 0 {disk_img}',
+        f'fatls host 0:1 {CAPSULE_INSTALL_DIR}'])
+    for name in filenames:
+        assert name in ''.join(output)
+
+def verify_content(u_boot_console, target, expected):
+    """verify the content
+
+    Args:
+        u_boot_console -- A console connection to U-Boot.
+        target -- Target address to verify.
+        expected -- Expected string of the content.
+    """
+    output = u_boot_console.run_command_list([
+        'sf probe 0:0',
+        f'sf read 4000000 {target} 10',
+        'md.b 4000000 10'])
+    assert expected in ''.join(output)
+
+def do_reboot_dtb_specified(u_boot_config, u_boot_console, dtb_filename):
+    """do reboot with specified DTB
+
+    Args:
+        u_boot_config -- U-boot configuration.
+        u_boot_console -- A console connection to U-Boot.
+        dtb_filename -- DTB file name.
+    """
+    mnt_point = u_boot_config.persistent_data_dir + '/test_efi_capsule'
+    u_boot_console.config.dtb = mnt_point + CAPSULE_DATA_DIR \
+                                + f'/{dtb_filename}'
+    u_boot_console.restart_uboot()
diff --git a/test/py/tests/test_efi_capsule/test_capsule_firmware_fit.py b/test/py/tests/test_efi_capsule/test_capsule_firmware_fit.py
index 9ee152818d..dd3dfdc047 100644
--- a/test/py/tests/test_efi_capsule/test_capsule_firmware_fit.py
+++ b/test/py/tests/test_efi_capsule/test_capsule_firmware_fit.py
@@ -7,8 +7,14 @@  This test verifies capsule-on-disk firmware update for FIT images
 """
 
 import pytest
-from capsule_defs import CAPSULE_DATA_DIR, CAPSULE_INSTALL_DIR
-
+from capsule_common import (
+    setup,
+    init_content,
+    place_capsule_file,
+    exec_manual_update,
+    check_file_removed,
+    verify_content
+)
 
 @pytest.mark.boardspec('sandbox_flattree')
 @pytest.mark.buildconfigspec('efi_capsule_firmware_fit')
@@ -40,37 +46,12 @@  class TestEfiCapsuleFirmwareFit():
         u_boot_console.restart_uboot()
 
         disk_img = efi_capsule_data
+        capsule_files = ['Test05']
         with u_boot_console.log.section('Test Case 1-a, before reboot'):
-            output = u_boot_console.run_command_list([
-                'host bind 0 %s' % disk_img,
-                'efidebug boot add -b 1 TEST host 0:1 /helloworld.efi -s ""',
-                'efidebug boot order 1',
-                'env set -e -nv -bs -rt OsIndications =0x0000000000000004',
-                'env set dfu_alt_info "sf 0:0=u-boot-bin raw 0x100000 0x50000;u-boot-env raw 0x150000 0x200000"',
-                'env save'])
-
-            # initialize contents
-            output = u_boot_console.run_command_list([
-                'sf probe 0:0',
-                'fatload host 0:1 4000000 %s/u-boot.bin.old' % CAPSULE_DATA_DIR,
-                'sf write 4000000 100000 10',
-                'sf read 5000000 100000 10',
-                'md.b 5000000 10'])
-            assert 'Old' in ''.join(output)
-            output = u_boot_console.run_command_list([
-                'sf probe 0:0',
-                'fatload host 0:1 4000000 %s/u-boot.env.old' % CAPSULE_DATA_DIR,
-                'sf write 4000000 150000 10',
-                'sf read 5000000 150000 10',
-                'md.b 5000000 10'])
-            assert 'Old' in ''.join(output)
-
-            # place a capsule file
-            output = u_boot_console.run_command_list([
-                'fatload host 0:1 4000000 %s/Test05' % CAPSULE_DATA_DIR,
-                'fatwrite host 0:1 4000000 %s/Test05 $filesize' % CAPSULE_INSTALL_DIR,
-                'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
-            assert 'Test05' in ''.join(output)
+            setup(u_boot_console, disk_img, '0x0000000000000004')
+            init_content(u_boot_console, '100000', 'u-boot.bin.old', 'Old')
+            init_content(u_boot_console, '150000', 'u-boot.env.old', 'Old')
+            place_capsule_file(u_boot_console, capsule_files)
 
         capsule_early = u_boot_config.buildconfig.get(
             'config_efi_capsule_on_disk_early')
@@ -80,28 +61,13 @@  class TestEfiCapsuleFirmwareFit():
 
         with u_boot_console.log.section('Test Case 1-b, after reboot'):
             if not capsule_early:
-                # make sure that dfu_alt_info exists even persistent variables
-                # are not available.
-                output = u_boot_console.run_command_list([
-                    'env set dfu_alt_info "sf 0:0=u-boot-bin raw 0x100000 0x50000;u-boot-env raw 0x150000 0x200000"',
-                    'host bind 0 %s' % disk_img,
-                    'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
-                assert 'Test05' in ''.join(output)
-
-                # need to run uefi command to initiate capsule handling
-                output = u_boot_console.run_command(
-                    'env print -e Capsule0000', wait_for_reboot = True)
-
-            output = u_boot_console.run_command_list([
-                'sf probe 0:0',
-                'sf read 4000000 100000 10',
-                'md.b 4000000 10'])
-            assert 'u-boot:Old' in ''.join(output)
-
-            output = u_boot_console.run_command_list([
-                'sf read 4000000 150000 10',
-                'md.b 4000000 10'])
-            assert 'u-boot-env:Old' in ''.join(output)
+                exec_manual_update(u_boot_console, disk_img, capsule_files)
+
+            # deleted anyway
+            check_file_removed(u_boot_console, disk_img, capsule_files)
+
+            verify_content(u_boot_console, '100000', 'u-boot:Old')
+            verify_content(u_boot_console, '150000', 'u-boot-env:Old')
 
     def test_efi_capsule_fw2(
             self, u_boot_config, u_boot_console, efi_capsule_data):
@@ -112,38 +78,12 @@  class TestEfiCapsuleFirmwareFit():
         """
 
         disk_img = efi_capsule_data
+        capsule_files = ['Test04']
         with u_boot_console.log.section('Test Case 2-a, before reboot'):
-            output = u_boot_console.run_command_list([
-                'host bind 0 %s' % disk_img,
-                'printenv -e PlatformLangCodes', # workaround for terminal size determination
-                'efidebug boot add -b 1 TEST host 0:1 /helloworld.efi -s ""',
-                'efidebug boot order 1',
-                'env set -e -nv -bs -rt OsIndications =0x0000000000000004',
-                'env set dfu_alt_info "sf 0:0=u-boot-bin raw 0x100000 0x50000;u-boot-env raw 0x150000 0x200000"',
-                'env save'])
-
-            # initialize contents
-            output = u_boot_console.run_command_list([
-                'sf probe 0:0',
-                'fatload host 0:1 4000000 %s/u-boot.bin.old' % CAPSULE_DATA_DIR,
-                'sf write 4000000 100000 10',
-                'sf read 5000000 100000 10',
-                'md.b 5000000 10'])
-            assert 'Old' in ''.join(output)
-            output = u_boot_console.run_command_list([
-                'sf probe 0:0',
-                'fatload host 0:1 4000000 %s/u-boot.env.old' % CAPSULE_DATA_DIR,
-                'sf write 4000000 150000 10',
-                'sf read 5000000 150000 10',
-                'md.b 5000000 10'])
-            assert 'Old' in ''.join(output)
-
-            # place a capsule file
-            output = u_boot_console.run_command_list([
-                'fatload host 0:1 4000000 %s/Test04' % CAPSULE_DATA_DIR,
-                'fatwrite host 0:1 4000000 %s/Test04 $filesize' % CAPSULE_INSTALL_DIR,
-                'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
-            assert 'Test04' in ''.join(output)
+            setup(u_boot_console, disk_img, '0x0000000000000004')
+            init_content(u_boot_console, '100000', 'u-boot.bin.old', 'Old')
+            init_content(u_boot_console, '150000', 'u-boot.env.old', 'Old')
+            place_capsule_file(u_boot_console, capsule_files)
 
         capsule_early = u_boot_config.buildconfig.get(
             'config_efi_capsule_on_disk_early')
@@ -155,36 +95,12 @@  class TestEfiCapsuleFirmwareFit():
 
         with u_boot_console.log.section('Test Case 2-b, after reboot'):
             if not capsule_early:
-                # make sure that dfu_alt_info exists even persistent variables
-                # are not available.
-                output = u_boot_console.run_command_list([
-                    'env set dfu_alt_info "sf 0:0=u-boot-bin raw 0x100000 0x50000;u-boot-env raw 0x150000 0x200000"',
-                    'host bind 0 %s' % disk_img,
-                    'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
-                assert 'Test04' in ''.join(output)
-
-                # need to run uefi command to initiate capsule handling
-                output = u_boot_console.run_command(
-                    'env print -e Capsule0000', wait_for_reboot = True)
-
-            output = u_boot_console.run_command_list([
-                'host bind 0 %s' % disk_img,
-                'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
-            assert 'Test04' not in ''.join(output)
-
-            output = u_boot_console.run_command_list([
-                'sf probe 0:0',
-                'sf read 4000000 100000 10',
-                'md.b 4000000 10'])
-            if capsule_auth:
-                assert 'u-boot:Old' in ''.join(output)
-            else:
-                assert 'u-boot:New' in ''.join(output)
-
-            output = u_boot_console.run_command_list([
-                'sf read 4000000 150000 10',
-                'md.b 4000000 10'])
-            if capsule_auth:
-                assert 'u-boot-env:Old' in ''.join(output)
-            else:
-                assert 'u-boot-env:New' in ''.join(output)
+                exec_manual_update(u_boot_console, disk_img, capsule_files)
+
+            check_file_removed(u_boot_console, disk_img, capsule_files)
+
+            expected = 'u-boot:Old' if capsule_auth else 'u-boot:New'
+            verify_content(u_boot_console, '100000', expected)
+
+            expected = 'u-boot-env:Old' if capsule_auth else 'u-boot-env:New'
+            verify_content(u_boot_console, '150000', expected)
diff --git a/test/py/tests/test_efi_capsule/test_capsule_firmware_raw.py b/test/py/tests/test_efi_capsule/test_capsule_firmware_raw.py
index 92bfb14932..e525096d8e 100644
--- a/test/py/tests/test_efi_capsule/test_capsule_firmware_raw.py
+++ b/test/py/tests/test_efi_capsule/test_capsule_firmware_raw.py
@@ -7,7 +7,15 @@  This test verifies capsule-on-disk firmware update for raw images
 """
 
 import pytest
-from capsule_defs import CAPSULE_DATA_DIR, CAPSULE_INSTALL_DIR
+from capsule_common import (
+    setup,
+    init_content,
+    place_capsule_file,
+    exec_manual_update,
+    check_file_removed,
+    check_file_exist,
+    verify_content
+)
 
 @pytest.mark.boardspec('sandbox')
 @pytest.mark.buildconfigspec('efi_capsule_firmware_raw')
@@ -40,37 +48,12 @@  class TestEfiCapsuleFirmwareRaw:
         u_boot_console.restart_uboot()
 
         disk_img = efi_capsule_data
+        capsule_files = ['Test03']
         with u_boot_console.log.section('Test Case 1-a, before reboot'):
-            output = u_boot_console.run_command_list([
-                'host bind 0 %s' % disk_img,
-                'efidebug boot add -b 1 TEST host 0:1 /helloworld.efi -s ""',
-                'efidebug boot order 1',
-                'env set -e -nv -bs -rt OsIndications =0x0000000000000004',
-                'env set dfu_alt_info "sf 0:0=u-boot-bin raw 0x100000 0x50000;u-boot-env raw 0x150000 0x200000"',
-                'env save'])
-
-            # initialize contents
-            output = u_boot_console.run_command_list([
-                'sf probe 0:0',
-                'fatload host 0:1 4000000 %s/u-boot.bin.old' % CAPSULE_DATA_DIR,
-                'sf write 4000000 100000 10',
-                'sf read 5000000 100000 10',
-                'md.b 5000000 10'])
-            assert 'Old' in ''.join(output)
-            output = u_boot_console.run_command_list([
-                'sf probe 0:0',
-                'fatload host 0:1 4000000 %s/u-boot.env.old' % CAPSULE_DATA_DIR,
-                'sf write 4000000 150000 10',
-                'sf read 5000000 150000 10',
-                'md.b 5000000 10'])
-            assert 'Old' in ''.join(output)
-
-            # place a capsule file
-            output = u_boot_console.run_command_list([
-                'fatload host 0:1 4000000 %s/Test03' % CAPSULE_DATA_DIR,
-                'fatwrite host 0:1 4000000 %s/Test03 $filesize' % CAPSULE_INSTALL_DIR,
-                'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
-            assert 'Test03' in ''.join(output)
+            setup(u_boot_console, disk_img, '0x0000000000000004')
+            init_content(u_boot_console, '100000', 'u-boot.bin.old', 'Old')
+            init_content(u_boot_console, '150000', 'u-boot.env.old', 'Old')
+            place_capsule_file(u_boot_console, capsule_files)
 
         # reboot
         u_boot_console.restart_uboot()
@@ -80,28 +63,13 @@  class TestEfiCapsuleFirmwareRaw:
 
         with u_boot_console.log.section('Test Case 1-b, after reboot'):
             if not capsule_early:
-                # make sure that dfu_alt_info exists even persistent variables
-                # are not available.
-                output = u_boot_console.run_command_list([
-                    'env set dfu_alt_info "sf 0:0=u-boot-bin raw 0x100000 0x50000;u-boot-env raw 0x150000 0x200000"',
-                    'host bind 0 %s' % disk_img,
-                    'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
-                assert 'Test03' in ''.join(output)
+                exec_manual_update(u_boot_console, disk_img, capsule_files)
 
-                # need to run uefi command to initiate capsule handling
-                output = u_boot_console.run_command(
-                    'env print -e Capsule0000', wait_for_reboot = True)
+            # deleted anyway
+            check_file_removed(u_boot_console, disk_img, capsule_files)
 
-            output = u_boot_console.run_command_list([
-                'sf probe 0:0',
-                'sf read 4000000 100000 10',
-                'md.b 4000000 10'])
-            assert 'u-boot:Old' in ''.join(output)
-
-            output = u_boot_console.run_command_list([
-                'sf read 4000000 150000 10',
-                'md.b 4000000 10'])
-            assert 'u-boot-env:Old' in ''.join(output)
+            verify_content(u_boot_console, '100000', 'u-boot:Old')
+            verify_content(u_boot_console, '150000', 'u-boot-env:Old')
 
     def test_efi_capsule_fw2(
             self, u_boot_config, u_boot_console, efi_capsule_data):
@@ -112,44 +80,12 @@  class TestEfiCapsuleFirmwareRaw:
         0x150000-0x200000: U-Boot environment (but dummy)
         """
         disk_img = efi_capsule_data
+        capsule_files = ['Test01', 'Test02']
         with u_boot_console.log.section('Test Case 2-a, before reboot'):
-            output = u_boot_console.run_command_list([
-                'host bind 0 %s' % disk_img,
-                'printenv -e PlatformLangCodes', # workaround for terminal size determination
-                'efidebug boot add -b 1 TEST host 0:1 /helloworld.efi -s ""',
-                'efidebug boot order 1',
-                'env set -e OsIndications',
-                'env set dfu_alt_info "sf 0:0=u-boot-bin raw 0x100000 0x50000;u-boot-env raw 0x150000 0x200000"',
-                'env save'])
-
-            # initialize contents
-            output = u_boot_console.run_command_list([
-                'sf probe 0:0',
-                'fatload host 0:1 4000000 %s/u-boot.bin.old' % CAPSULE_DATA_DIR,
-                'sf write 4000000 100000 10',
-                'sf read 5000000 100000 10',
-                'md.b 5000000 10'])
-            assert 'Old' in ''.join(output)
-            output = u_boot_console.run_command_list([
-                'sf probe 0:0',
-                'fatload host 0:1 4000000 %s/u-boot.env.old' % CAPSULE_DATA_DIR,
-                'sf write 4000000 150000 10',
-                'sf read 5000000 150000 10',
-                'md.b 5000000 10'])
-            assert 'Old' in ''.join(output)
-
-            # place the capsule files
-            output = u_boot_console.run_command_list([
-                'fatload host 0:1 4000000 %s/Test01' % CAPSULE_DATA_DIR,
-                'fatwrite host 0:1 4000000 %s/Test01 $filesize' % CAPSULE_INSTALL_DIR,
-                'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
-            assert 'Test01' in ''.join(output)
-
-            output = u_boot_console.run_command_list([
-                'fatload host 0:1 4000000 %s/Test02' % CAPSULE_DATA_DIR,
-                'fatwrite host 0:1 4000000 %s/Test02 $filesize' % CAPSULE_INSTALL_DIR,
-                'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
-            assert 'Test02' in ''.join(output)
+            setup(u_boot_console, disk_img, None)
+            init_content(u_boot_console, '100000', 'u-boot.bin.old', 'Old')
+            init_content(u_boot_console, '150000', 'u-boot.env.old', 'Old')
+            place_capsule_file(u_boot_console, capsule_files)
 
         # reboot
         u_boot_console.restart_uboot()
@@ -158,35 +94,12 @@  class TestEfiCapsuleFirmwareRaw:
             'config_efi_capsule_on_disk_early')
         with u_boot_console.log.section('Test Case 2-b, after reboot'):
             if not capsule_early:
-                # make sure that dfu_alt_info exists even persistent variables
-                # are not available.
-                output = u_boot_console.run_command_list([
-                    'env set dfu_alt_info "sf 0:0=u-boot-bin raw 0x100000 0x50000;u-boot-env raw 0x150000 0x200000"',
-                    'host bind 0 %s' % disk_img,
-                    'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
-                assert 'Test01' in ''.join(output)
-                assert 'Test02' in ''.join(output)
+                exec_manual_update(u_boot_console, disk_img, capsule_files, False)
 
-                # need to run uefi command to initiate capsule handling
-                output = u_boot_console.run_command(
-                    'env print -e Capsule0000')
+            check_file_exist(u_boot_console, disk_img, capsule_files)
 
-            output = u_boot_console.run_command_list([
-                'host bind 0 %s' % disk_img,
-                'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
-            assert 'Test01' in ''.join(output)
-            assert 'Test02' in ''.join(output)
-
-            output = u_boot_console.run_command_list([
-                'sf probe 0:0',
-                'sf read 4000000 100000 10',
-                'md.b 4000000 10'])
-            assert 'u-boot:Old' in ''.join(output)
-
-            output = u_boot_console.run_command_list([
-                'sf read 4000000 150000 10',
-                'md.b 4000000 10'])
-            assert 'u-boot-env:Old' in ''.join(output)
+            verify_content(u_boot_console, '100000', 'u-boot:Old')
+            verify_content(u_boot_console, '150000', 'u-boot-env:Old')
 
     def test_efi_capsule_fw3(
             self, u_boot_config, u_boot_console, efi_capsule_data):
@@ -195,45 +108,12 @@  class TestEfiCapsuleFirmwareRaw:
         0x100000-0x150000: U-Boot binary (but dummy)
         """
         disk_img = efi_capsule_data
+        capsule_files = ['Test01', 'Test02']
         with u_boot_console.log.section('Test Case 3-a, before reboot'):
-            output = u_boot_console.run_command_list([
-                'host bind 0 %s' % disk_img,
-                'printenv -e PlatformLangCodes', # workaround for terminal size determination
-                'efidebug boot add -b 1 TEST host 0:1 /helloworld.efi -s ""',
-                'efidebug boot order 1',
-                'env set -e -nv -bs -rt OsIndications =0x0000000000000004',
-                'env set dfu_alt_info "sf 0:0=u-boot-bin raw 0x100000 0x50000;u-boot-env raw 0x150000 0x200000"',
-                'env save'])
-
-            # initialize contents
-            output = u_boot_console.run_command_list([
-                'sf probe 0:0',
-                'fatload host 0:1 4000000 %s/u-boot.bin.old' % CAPSULE_DATA_DIR,
-                'sf write 4000000 100000 10',
-                'sf read 5000000 100000 10',
-                'md.b 5000000 10'])
-            assert 'Old' in ''.join(output)
-
-            output = u_boot_console.run_command_list([
-                'sf probe 0:0',
-                'fatload host 0:1 4000000 %s/u-boot.env.old' % CAPSULE_DATA_DIR,
-                'sf write 4000000 150000 10',
-                'sf read 5000000 100000 10',
-                'md.b 5000000 10'])
-            assert 'Old' in ''.join(output)
-
-            # place the capsule files
-            output = u_boot_console.run_command_list([
-                'fatload host 0:1 4000000 %s/Test01' % CAPSULE_DATA_DIR,
-                'fatwrite host 0:1 4000000 %s/Test01 $filesize' % CAPSULE_INSTALL_DIR,
-                'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
-            assert 'Test01' in ''.join(output)
-
-            output = u_boot_console.run_command_list([
-                'fatload host 0:1 4000000 %s/Test02' % CAPSULE_DATA_DIR,
-                'fatwrite host 0:1 4000000 %s/Test02 $filesize' % CAPSULE_INSTALL_DIR,
-                'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
-            assert 'Test02' in ''.join(output)
+            setup(u_boot_console, disk_img, '0x0000000000000004')
+            init_content(u_boot_console, '100000', 'u-boot.bin.old', 'Old')
+            init_content(u_boot_console, '150000', 'u-boot.env.old', 'Old')
+            place_capsule_file(u_boot_console, capsule_files)
 
         capsule_early = u_boot_config.buildconfig.get(
             'config_efi_capsule_on_disk_early')
@@ -245,18 +125,7 @@  class TestEfiCapsuleFirmwareRaw:
 
         with u_boot_console.log.section('Test Case 3-b, after reboot'):
             if not capsule_early:
-                # make sure that dfu_alt_info exists even persistent variables
-                # are not available.
-                output = u_boot_console.run_command_list([
-                    'env set dfu_alt_info "sf 0:0=u-boot-bin raw 0x100000 0x50000;u-boot-env raw 0x150000 0x200000"',
-                    'host bind 0 %s' % disk_img,
-                    'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
-                assert 'Test01' in ''.join(output)
-                assert 'Test02' in ''.join(output)
-
-                # need to run uefi command to initiate capsule handling
-                output = u_boot_console.run_command(
-                    'env print -e Capsule0000', wait_for_reboot = True)
+                exec_manual_update(u_boot_console, disk_img, capsule_files)
 
             # make sure the dfu_alt_info exists because it is required for making ESRT.
             output = u_boot_console.run_command_list([
@@ -269,26 +138,10 @@  class TestEfiCapsuleFirmwareRaw:
             # ensure that SANDBOX_UBOOT_IMAGE_GUID is in the ESRT.
             assert '09D7CF52-0720-4710-91D1-08469B7FE9C8' in ''.join(output)
 
-            output = u_boot_console.run_command_list([
-                'host bind 0 %s' % disk_img,
-                'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
-            assert 'Test01' not in ''.join(output)
-            assert 'Test02' not in ''.join(output)
+            check_file_removed(u_boot_console, disk_img, capsule_files)
 
-            output = u_boot_console.run_command_list([
-                'sf probe 0:0',
-                'sf read 4000000 100000 10',
-                'md.b 4000000 10'])
-            if capsule_auth:
-                assert 'u-boot:Old' in ''.join(output)
-            else:
-                assert 'u-boot:New' in ''.join(output)
+            expected = 'u-boot:Old' if capsule_auth else 'u-boot:New'
+            verify_content(u_boot_console, '100000', expected)
 
-            output = u_boot_console.run_command_list([
-                'sf probe 0:0',
-                'sf read 4000000 150000 10',
-                'md.b 4000000 10'])
-            if capsule_auth:
-                assert 'u-boot-env:Old' in ''.join(output)
-            else:
-                assert 'u-boot-env:New' in ''.join(output)
+            expected = 'u-boot-env:Old' if capsule_auth else 'u-boot-env:New'
+            verify_content(u_boot_console, '150000', expected)
diff --git a/test/py/tests/test_efi_capsule/test_capsule_firmware_signed_fit.py b/test/py/tests/test_efi_capsule/test_capsule_firmware_signed_fit.py
index ba8429e83c..70f24e8ce7 100644
--- a/test/py/tests/test_efi_capsule/test_capsule_firmware_signed_fit.py
+++ b/test/py/tests/test_efi_capsule/test_capsule_firmware_signed_fit.py
@@ -10,7 +10,15 @@  with signed capsule files containing FIT images
 """
 
 import pytest
-from capsule_defs import CAPSULE_DATA_DIR, CAPSULE_INSTALL_DIR
+from capsule_common import (
+    setup,
+    init_content,
+    place_capsule_file,
+    exec_manual_update,
+    check_file_removed,
+    verify_content,
+    do_reboot_dtb_specified
+)
 
 @pytest.mark.boardspec('sandbox_flattree')
 @pytest.mark.buildconfigspec('efi_capsule_firmware_fit')
@@ -37,70 +45,23 @@  class TestEfiCapsuleFirmwareSignedFit():
         should pass and the firmware be updated.
         """
         disk_img = efi_capsule_data
+        capsule_files = ['Test13']
         with u_boot_console.log.section('Test Case 1-a, before reboot'):
-            output = u_boot_console.run_command_list([
-                'host bind 0 %s' % disk_img,
-                'printenv -e PlatformLangCodes', # workaround for terminal size determination
-                'efidebug boot add -b 1 TEST host 0:1 /helloworld.efi',
-                'efidebug boot order 1',
-                'env set -e -nv -bs -rt OsIndications =0x0000000000000004',
-                'env set dfu_alt_info '
-                        '"sf 0:0=u-boot-bin raw 0x100000 '
-                        '0x50000;u-boot-env raw 0x150000 0x200000"',
-                'env save'])
+            setup(u_boot_console, disk_img, '0x0000000000000004')
+            init_content(u_boot_console, '100000', 'u-boot.bin.old', 'Old')
+            place_capsule_file(u_boot_console, capsule_files)
 
-            # initialize content
-            output = u_boot_console.run_command_list([
-                'sf probe 0:0',
-                'fatload host 0:1 4000000 %s/u-boot.bin.old'
-                        % CAPSULE_DATA_DIR,
-                'sf write 4000000 100000 10',
-                'sf read 5000000 100000 10',
-                'md.b 5000000 10'])
-            assert 'Old' in ''.join(output)
-
-            # place a capsule file
-            output = u_boot_console.run_command_list([
-                'fatload host 0:1 4000000 %s/Test13' % CAPSULE_DATA_DIR,
-                'fatwrite host 0:1 4000000 %s/Test13 $filesize'
-                        % CAPSULE_INSTALL_DIR,
-                'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
-            assert 'Test13' in ''.join(output)
-
-        # reboot
-        mnt_point = u_boot_config.persistent_data_dir + '/test_efi_capsule'
-        u_boot_console.config.dtb = mnt_point + CAPSULE_DATA_DIR \
-                                    + '/test_sig.dtb'
-        u_boot_console.restart_uboot()
+        do_reboot_dtb_specified(u_boot_config, u_boot_console, 'test_sig.dtb')
 
         capsule_early = u_boot_config.buildconfig.get(
             'config_efi_capsule_on_disk_early')
         with u_boot_console.log.section('Test Case 1-b, after reboot'):
             if not capsule_early:
-                # make sure that dfu_alt_info exists even persistent variables
-                # are not available.
-                output = u_boot_console.run_command_list([
-                    'env set dfu_alt_info '
-                            '"sf 0:0=u-boot-bin raw 0x100000 '
-                            '0x50000;u-boot-env raw 0x150000 0x200000"',
-                    'host bind 0 %s' % disk_img,
-                    'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
-                assert 'Test13' in ''.join(output)
-
-                # need to run uefi command to initiate capsule handling
-                output = u_boot_console.run_command(
-                    'env print -e Capsule0000', wait_for_reboot = True)
+                exec_manual_update(u_boot_console, disk_img, capsule_files)
 
-            output = u_boot_console.run_command_list([
-                'host bind 0 %s' % disk_img,
-                'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
-            assert 'Test13' not in ''.join(output)
+            check_file_removed(u_boot_console, disk_img, capsule_files)
 
-            output = u_boot_console.run_command_list([
-                'sf probe 0:0',
-                'sf read 4000000 100000 10',
-                'md.b 4000000 10'])
-            assert 'u-boot:New' in ''.join(output)
+            verify_content(u_boot_console, '100000', 'u-boot:New')
 
     def test_efi_capsule_auth2(
             self, u_boot_config, u_boot_console, efi_capsule_data):
@@ -113,73 +74,26 @@  class TestEfiCapsuleFirmwareSignedFit():
         not be updated.
         """
         disk_img = efi_capsule_data
+        capsule_files = ['Test14']
         with u_boot_console.log.section('Test Case 2-a, before reboot'):
-            output = u_boot_console.run_command_list([
-                'host bind 0 %s' % disk_img,
-                'printenv -e PlatformLangCodes', # workaround for terminal size determination
-                'efidebug boot add -b 1 TEST host 0:1 /helloworld.efi',
-                'efidebug boot order 1',
-                'env set -e -nv -bs -rt OsIndications =0x0000000000000004',
-                'env set dfu_alt_info '
-                        '"sf 0:0=u-boot-bin raw 0x100000 '
-                        '0x50000;u-boot-env raw 0x150000 0x200000"',
-                'env save'])
-
-            # initialize content
-            output = u_boot_console.run_command_list([
-                'sf probe 0:0',
-                'fatload host 0:1 4000000 %s/u-boot.bin.old'
-                        % CAPSULE_DATA_DIR,
-                'sf write 4000000 100000 10',
-                'sf read 5000000 100000 10',
-                'md.b 5000000 10'])
-            assert 'Old' in ''.join(output)
-
-            # place a capsule file
-            output = u_boot_console.run_command_list([
-                'fatload host 0:1 4000000 %s/Test14' % CAPSULE_DATA_DIR,
-                'fatwrite host 0:1 4000000 %s/Test14 $filesize'
-                                % CAPSULE_INSTALL_DIR,
-                'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
-            assert 'Test14' in ''.join(output)
+            setup(u_boot_console, disk_img, '0x0000000000000004')
+            init_content(u_boot_console, '100000', 'u-boot.bin.old', 'Old')
+            place_capsule_file(u_boot_console, capsule_files)
 
-        # reboot
-        mnt_point = u_boot_config.persistent_data_dir + '/test_efi_capsule'
-        u_boot_console.config.dtb = mnt_point + CAPSULE_DATA_DIR \
-                                    + '/test_sig.dtb'
-        u_boot_console.restart_uboot()
+        do_reboot_dtb_specified(u_boot_config, u_boot_console, 'test_sig.dtb')
 
         capsule_early = u_boot_config.buildconfig.get(
             'config_efi_capsule_on_disk_early')
         with u_boot_console.log.section('Test Case 2-b, after reboot'):
             if not capsule_early:
-                # make sure that dfu_alt_info exists even persistent variables
-                # are not available.
-                output = u_boot_console.run_command_list([
-                    'env set dfu_alt_info '
-                        '"sf 0:0=u-boot-bin raw 0x100000 '
-                        '0x50000;u-boot-env raw 0x150000 0x200000"',
-                    'host bind 0 %s' % disk_img,
-                    'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
-                assert 'Test14' in ''.join(output)
-
-                # need to run uefi command to initiate capsule handling
-                output = u_boot_console.run_command(
-                    'env print -e Capsule0000', wait_for_reboot = True)
+                exec_manual_update(u_boot_console, disk_img, capsule_files)
 
             # deleted any way
-            output = u_boot_console.run_command_list([
-                'host bind 0 %s' % disk_img,
-                'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
-            assert 'Test14' not in ''.join(output)
+            check_file_removed(u_boot_console, disk_img, capsule_files)
 
             # TODO: check CapsuleStatus in CapsuleXXXX
 
-            output = u_boot_console.run_command_list([
-                'sf probe 0:0',
-                'sf read 4000000 100000 10',
-                'md.b 4000000 10'])
-            assert 'u-boot:Old' in ''.join(output)
+            verify_content(u_boot_console, '100000', 'u-boot:Old')
 
     def test_efi_capsule_auth3(
             self, u_boot_config, u_boot_console, efi_capsule_data):
@@ -191,70 +105,23 @@  class TestEfiCapsuleFirmwareSignedFit():
         should fail and the firmware not be updated.
         """
         disk_img = efi_capsule_data
+        capsule_files = ['Test02']
         with u_boot_console.log.section('Test Case 3-a, before reboot'):
-            output = u_boot_console.run_command_list([
-                'host bind 0 %s' % disk_img,
-                'printenv -e PlatformLangCodes', # workaround for terminal size determination
-                'efidebug boot add -b 1 TEST host 0:1 /helloworld.efi',
-                'efidebug boot order 1',
-                'env set -e -nv -bs -rt OsIndications =0x0000000000000004',
-                'env set dfu_alt_info '
-                        '"sf 0:0=u-boot-bin raw 0x100000 '
-                        '0x50000;u-boot-env raw 0x150000 0x200000"',
-                'env save'])
-
-            # initialize content
-            output = u_boot_console.run_command_list([
-                'sf probe 0:0',
-                'fatload host 0:1 4000000 %s/u-boot.bin.old'
-                        % CAPSULE_DATA_DIR,
-                'sf write 4000000 100000 10',
-                'sf read 5000000 100000 10',
-                'md.b 5000000 10'])
-            assert 'Old' in ''.join(output)
+            setup(u_boot_console, disk_img, '0x0000000000000004')
+            init_content(u_boot_console, '100000', 'u-boot.bin.old', 'Old')
+            place_capsule_file(u_boot_console, capsule_files)
 
-            # place a capsule file
-            output = u_boot_console.run_command_list([
-                'fatload host 0:1 4000000 %s/Test02' % CAPSULE_DATA_DIR,
-                'fatwrite host 0:1 4000000 %s/Test02 $filesize'
-                            % CAPSULE_INSTALL_DIR,
-                'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
-            assert 'Test02' in ''.join(output)
-
-        # reboot
-        mnt_point = u_boot_config.persistent_data_dir + '/test_efi_capsule'
-        u_boot_console.config.dtb = mnt_point + CAPSULE_DATA_DIR \
-                                    + '/test_sig.dtb'
-        u_boot_console.restart_uboot()
+        do_reboot_dtb_specified(u_boot_config, u_boot_console, 'test_sig.dtb')
 
         capsule_early = u_boot_config.buildconfig.get(
             'config_efi_capsule_on_disk_early')
         with u_boot_console.log.section('Test Case 3-b, after reboot'):
             if not capsule_early:
-                # make sure that dfu_alt_info exists even persistent variables
-                # are not available.
-                output = u_boot_console.run_command_list([
-                    'env set dfu_alt_info '
-                            '"sf 0:0=u-boot-bin raw 0x100000 '
-                            '0x50000;u-boot-env raw 0x150000 0x200000"',
-                    'host bind 0 %s' % disk_img,
-                    'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
-                assert 'Test02' in ''.join(output)
-
-                # need to run uefi command to initiate capsule handling
-                output = u_boot_console.run_command(
-                    'env print -e Capsule0000', wait_for_reboot = True)
+                exec_manual_update(u_boot_console, disk_img, capsule_files)
 
             # deleted any way
-            output = u_boot_console.run_command_list([
-                'host bind 0 %s' % disk_img,
-                'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
-            assert 'Test02' not in ''.join(output)
+            check_file_removed(u_boot_console, disk_img, capsule_files)
 
             # TODO: check CapsuleStatus in CapsuleXXXX
 
-            output = u_boot_console.run_command_list([
-                'sf probe 0:0',
-                'sf read 4000000 100000 10',
-                'md.b 4000000 10'])
-            assert 'u-boot:Old' in ''.join(output)
+            verify_content(u_boot_console, '100000', 'u-boot:Old')
diff --git a/test/py/tests/test_efi_capsule/test_capsule_firmware_signed_raw.py b/test/py/tests/test_efi_capsule/test_capsule_firmware_signed_raw.py
index 710d9925a3..c6109e2103 100644
--- a/test/py/tests/test_efi_capsule/test_capsule_firmware_signed_raw.py
+++ b/test/py/tests/test_efi_capsule/test_capsule_firmware_signed_raw.py
@@ -8,7 +8,15 @@  with signed capsule files containing raw images
 """
 
 import pytest
-from capsule_defs import CAPSULE_DATA_DIR, CAPSULE_INSTALL_DIR
+from capsule_common import (
+    setup,
+    init_content,
+    place_capsule_file,
+    exec_manual_update,
+    check_file_removed,
+    verify_content,
+    do_reboot_dtb_specified
+)
 
 @pytest.mark.boardspec('sandbox')
 @pytest.mark.buildconfigspec('efi_capsule_firmware_raw')
@@ -34,69 +42,23 @@  class TestEfiCapsuleFirmwareSignedRaw():
         should pass and the firmware be updated.
         """
         disk_img = efi_capsule_data
+        capsule_files =  ['Test11']
         with u_boot_console.log.section('Test Case 1-a, before reboot'):
-            output = u_boot_console.run_command_list([
-                'host bind 0 %s' % disk_img,
-                'efidebug boot add -b 1 TEST host 0:1 /helloworld.efi',
-                'efidebug boot order 1',
-                'env set -e -nv -bs -rt OsIndications =0x0000000000000004',
-                'env set dfu_alt_info '
-                        '"sf 0:0=u-boot-bin raw 0x100000 '
-                        '0x50000;u-boot-env raw 0x150000 0x200000"',
-                'env save'])
-
-            # initialize content
-            output = u_boot_console.run_command_list([
-                'sf probe 0:0',
-                'fatload host 0:1 4000000 %s/u-boot.bin.old'
-                        % CAPSULE_DATA_DIR,
-                'sf write 4000000 100000 10',
-                'sf read 5000000 100000 10',
-                'md.b 5000000 10'])
-            assert 'Old' in ''.join(output)
-
-            # place a capsule file
-            output = u_boot_console.run_command_list([
-                'fatload host 0:1 4000000 %s/Test11' % CAPSULE_DATA_DIR,
-                'fatwrite host 0:1 4000000 %s/Test11 $filesize'
-                        % CAPSULE_INSTALL_DIR,
-                'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
-            assert 'Test11' in ''.join(output)
-
-        # reboot
-        mnt_point = u_boot_config.persistent_data_dir + '/test_efi_capsule'
-        u_boot_console.config.dtb = mnt_point + CAPSULE_DATA_DIR \
-                                    + '/test_sig.dtb'
-        u_boot_console.restart_uboot()
+            setup(u_boot_console, disk_img, '0x0000000000000004')
+            init_content(u_boot_console, '100000', 'u-boot.bin.old', 'Old')
+            place_capsule_file(u_boot_console, capsule_files)
+
+        do_reboot_dtb_specified(u_boot_config, u_boot_console, 'test_sig.dtb')
 
         capsule_early = u_boot_config.buildconfig.get(
             'config_efi_capsule_on_disk_early')
         with u_boot_console.log.section('Test Case 1-b, after reboot'):
             if not capsule_early:
-                # make sure that dfu_alt_info exists even persistent variables
-                # are not available.
-                output = u_boot_console.run_command_list([
-                    'env set dfu_alt_info '
-                            '"sf 0:0=u-boot-bin raw 0x100000 '
-                            '0x50000;u-boot-env raw 0x150000 0x200000"',
-                    'host bind 0 %s' % disk_img,
-                    'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
-                assert 'Test11' in ''.join(output)
-
-                # need to run uefi command to initiate capsule handling
-                output = u_boot_console.run_command(
-                    'env print -e Capsule0000', wait_for_reboot = True)
-
-            output = u_boot_console.run_command_list([
-                'host bind 0 %s' % disk_img,
-                'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
-            assert 'Test11' not in ''.join(output)
-
-            output = u_boot_console.run_command_list([
-                'sf probe 0:0',
-                'sf read 4000000 100000 10',
-                'md.b 4000000 10'])
-            assert 'u-boot:New' in ''.join(output)
+                exec_manual_update(u_boot_console, disk_img, capsule_files)
+
+            check_file_removed(u_boot_console, disk_img, capsule_files)
+
+            verify_content(u_boot_console, '100000', 'u-boot:New')
 
     def test_efi_capsule_auth2(
             self, u_boot_config, u_boot_console, efi_capsule_data):
@@ -108,73 +70,25 @@  class TestEfiCapsuleFirmwareSignedRaw():
         not be updated.
         """
         disk_img = efi_capsule_data
+        capsule_files = ['Test12']
         with u_boot_console.log.section('Test Case 2-a, before reboot'):
-            output = u_boot_console.run_command_list([
-                'host bind 0 %s' % disk_img,
-                'printenv -e PlatformLangCodes', # workaround for terminal size determination
-                'efidebug boot add -b 1 TEST host 0:1 /helloworld.efi',
-                'efidebug boot order 1',
-                'env set -e -nv -bs -rt OsIndications =0x0000000000000004',
-                'env set dfu_alt_info '
-                        '"sf 0:0=u-boot-bin raw 0x100000 '
-                        '0x50000;u-boot-env raw 0x150000 0x200000"',
-                'env save'])
-
-            # initialize content
-            output = u_boot_console.run_command_list([
-                'sf probe 0:0',
-                'fatload host 0:1 4000000 %s/u-boot.bin.old'
-                        % CAPSULE_DATA_DIR,
-                'sf write 4000000 100000 10',
-                'sf read 5000000 100000 10',
-                'md.b 5000000 10'])
-            assert 'Old' in ''.join(output)
-
-            # place a capsule file
-            output = u_boot_console.run_command_list([
-                'fatload host 0:1 4000000 %s/Test12' % CAPSULE_DATA_DIR,
-                'fatwrite host 0:1 4000000 %s/Test12 $filesize'
-                                % CAPSULE_INSTALL_DIR,
-                'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
-            assert 'Test12' in ''.join(output)
-
-        # reboot
-        mnt_point = u_boot_config.persistent_data_dir + '/test_efi_capsule'
-        u_boot_console.config.dtb = mnt_point + CAPSULE_DATA_DIR \
-                                    + '/test_sig.dtb'
-        u_boot_console.restart_uboot()
+            setup(u_boot_console, disk_img, '0x0000000000000004')
+            init_content(u_boot_console, '100000', 'u-boot.bin.old', 'Old')
+            place_capsule_file(u_boot_console, capsule_files)
+
+        do_reboot_dtb_specified(u_boot_config, u_boot_console, 'test_sig.dtb')
 
         capsule_early = u_boot_config.buildconfig.get(
             'config_efi_capsule_on_disk_early')
         with u_boot_console.log.section('Test Case 2-b, after reboot'):
             if not capsule_early:
-                # make sure that dfu_alt_info exists even persistent variables
-                # are not available.
-                output = u_boot_console.run_command_list([
-                    'env set dfu_alt_info '
-                        '"sf 0:0=u-boot-bin raw 0x100000 '
-                        '0x50000;u-boot-env raw 0x150000 0x200000"',
-                    'host bind 0 %s' % disk_img,
-                    'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
-                assert 'Test12' in ''.join(output)
-
-                # need to run uefi command to initiate capsule handling
-                output = u_boot_console.run_command(
-                    'env print -e Capsule0000', wait_for_reboot = True)
-
-            # deleted any way
-            output = u_boot_console.run_command_list([
-                'host bind 0 %s' % disk_img,
-                'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
-            assert 'Test12' not in ''.join(output)
+                exec_manual_update(u_boot_console, disk_img, capsule_files)
+
+            check_file_removed(u_boot_console, disk_img, capsule_files)
 
             # TODO: check CapsuleStatus in CapsuleXXXX
 
-            output = u_boot_console.run_command_list([
-                'sf probe 0:0',
-                'sf read 4000000 100000 10',
-                'md.b 4000000 10'])
-            assert 'u-boot:Old' in ''.join(output)
+            verify_content(u_boot_console, '100000', 'u-boot:Old')
 
     def test_efi_capsule_auth3(
             self, u_boot_config, u_boot_console, efi_capsule_data):
@@ -185,70 +99,23 @@  class TestEfiCapsuleFirmwareSignedRaw():
         should fail and the firmware not be updated.
         """
         disk_img = efi_capsule_data
+        capsule_files = ['Test02']
         with u_boot_console.log.section('Test Case 3-a, before reboot'):
-            output = u_boot_console.run_command_list([
-                'host bind 0 %s' % disk_img,
-                'printenv -e PlatformLangCodes', # workaround for terminal size determination
-                'efidebug boot add -b 1 TEST host 0:1 /helloworld.efi',
-                'efidebug boot order 1',
-                'env set -e -nv -bs -rt OsIndications =0x0000000000000004',
-                'env set dfu_alt_info '
-                        '"sf 0:0=u-boot-bin raw 0x100000 '
-                        '0x50000;u-boot-env raw 0x150000 0x200000"',
-                'env save'])
-
-            # initialize content
-            output = u_boot_console.run_command_list([
-                'sf probe 0:0',
-                'fatload host 0:1 4000000 %s/u-boot.bin.old'
-                        % CAPSULE_DATA_DIR,
-                'sf write 4000000 100000 10',
-                'sf read 5000000 100000 10',
-                'md.b 5000000 10'])
-            assert 'Old' in ''.join(output)
-
-            # place a capsule file
-            output = u_boot_console.run_command_list([
-                'fatload host 0:1 4000000 %s/Test02' % CAPSULE_DATA_DIR,
-                'fatwrite host 0:1 4000000 %s/Test02 $filesize'
-                            % CAPSULE_INSTALL_DIR,
-                'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
-            assert 'Test02' in ''.join(output)
-
-        # reboot
-        mnt_point = u_boot_config.persistent_data_dir + '/test_efi_capsule'
-        u_boot_console.config.dtb = mnt_point + CAPSULE_DATA_DIR \
-                                    + '/test_sig.dtb'
-        u_boot_console.restart_uboot()
+            setup(u_boot_console, disk_img, '0x0000000000000004')
+            init_content(u_boot_console, '100000', 'u-boot.bin.old', 'Old')
+            place_capsule_file(u_boot_console, capsule_files)
+
+        do_reboot_dtb_specified(u_boot_config, u_boot_console, 'test_sig.dtb')
 
         capsule_early = u_boot_config.buildconfig.get(
             'config_efi_capsule_on_disk_early')
         with u_boot_console.log.section('Test Case 3-b, after reboot'):
             if not capsule_early:
-                # make sure that dfu_alt_info exists even persistent variables
-                # are not available.
-                output = u_boot_console.run_command_list([
-                    'env set dfu_alt_info '
-                            '"sf 0:0=u-boot-bin raw 0x100000 '
-                            '0x50000;u-boot-env raw 0x150000 0x200000"',
-                    'host bind 0 %s' % disk_img,
-                    'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
-                assert 'Test02' in ''.join(output)
-
-                # need to run uefi command to initiate capsule handling
-                output = u_boot_console.run_command(
-                    'env print -e Capsule0000', wait_for_reboot = True)
+                exec_manual_update(u_boot_console, disk_img, capsule_files)
 
             # deleted anyway
-            output = u_boot_console.run_command_list([
-                'host bind 0 %s' % disk_img,
-                'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
-            assert 'Test02' not in ''.join(output)
+            check_file_removed(u_boot_console, disk_img, capsule_files)
 
             # TODO: check CapsuleStatus in CapsuleXXXX
 
-            output = u_boot_console.run_command_list([
-                'sf probe 0:0',
-                'sf read 4000000 100000 10',
-                'md.b 4000000 10'])
-            assert 'u-boot:Old' in ''.join(output)
+            verify_content(u_boot_console, '100000', 'u-boot:Old')