1# SPDX-License-Identifier:      GPL-2.0+
2# Copyright (c) 2023, Linaro Limited
3
4
5"""Common function for UEFI capsule test."""
6
7from capsule_defs import CAPSULE_DATA_DIR, CAPSULE_INSTALL_DIR
8
9def capsule_setup(u_boot_console, disk_img, osindications):
10    """setup the test
11
12    Args:
13        u_boot_console -- A console connection to U-Boot.
14        disk_img -- A path to disk image to be used for testing.
15        osindications -- String of osindications value.
16    """
17    u_boot_console.run_command_list([
18        f'host bind 0 {disk_img}',
19        'printenv -e PlatformLangCodes', # workaround for terminal size determination
20        'efidebug boot add -b 1 TEST host 0:1 /helloworld.efi',
21        'efidebug boot order 1',
22        'env set dfu_alt_info "sf 0:0=u-boot-bin raw 0x100000 0x50000;'
23        'u-boot-env raw 0x150000 0x200000"'])
24
25    if osindications is None:
26        u_boot_console.run_command('env set -e OsIndications')
27    else:
28        u_boot_console.run_command(f'env set -e -nv -bs -rt OsIndications ={osindications}')
29
30    u_boot_console.run_command('env save')
31
32def init_content(u_boot_console, target, filename, expected):
33    """initialize test content
34
35    Args:
36        u_boot_console -- A console connection to U-Boot.
37        target -- Target address to place the content.
38        filename -- File name of the content.
39        expected -- Expected string of the content.
40    """
41    output = u_boot_console.run_command_list([
42        'sf probe 0:0',
43        f'fatload host 0:1 4000000 {CAPSULE_DATA_DIR}/{filename}',
44        f'sf write 4000000 {target} 10',
45        'sf read 5000000 100000 10',
46        'md.b 5000000 10'])
47    assert expected in ''.join(output)
48
49def place_capsule_file(u_boot_console, filenames):
50    """place the capsule file
51
52    Args:
53        u_boot_console -- A console connection to U-Boot.
54        filenames -- File name array of the target capsule files.
55    """
56    for name in filenames:
57        u_boot_console.run_command_list([
58            f'fatload host 0:1 4000000 {CAPSULE_DATA_DIR}/{name}',
59            f'fatwrite host 0:1 4000000 {CAPSULE_INSTALL_DIR}/{name} $filesize'])
60
61    output = u_boot_console.run_command(f'fatls host 0:1 {CAPSULE_INSTALL_DIR}')
62    for name in filenames:
63        assert name in ''.join(output)
64
65def exec_manual_update(u_boot_console, disk_img, filenames, need_reboot = True):
66    """execute capsule update manually
67
68    Args:
69        u_boot_console -- A console connection to U-Boot.
70        disk_img -- A path to disk image to be used for testing.
71        filenames -- File name array of the target capsule files.
72        need_reboot -- Flag indicates whether system reboot is required.
73    """
74    # make sure that dfu_alt_info exists even persistent variables
75    # are not available.
76    output = u_boot_console.run_command_list([
77        'env set dfu_alt_info '
78                '"sf 0:0=u-boot-bin raw 0x100000 0x50000;'
79                'u-boot-env raw 0x150000 0x200000"',
80        f'host bind 0 {disk_img}',
81        f'fatls host 0:1 {CAPSULE_INSTALL_DIR}'])
82    for name in filenames:
83        assert name in ''.join(output)
84
85    # need to run uefi command to initiate capsule handling
86    u_boot_console.run_command(
87        'env print -e Capsule0000', wait_for_reboot = need_reboot)
88
89def check_file_removed(u_boot_console, disk_img, filenames):
90    """check files are removed
91
92    Args:
93        u_boot_console -- A console connection to U-Boot.
94        disk_img -- A path to disk image to be used for testing.
95        filenames -- File name array of the target capsule files.
96    """
97    output = u_boot_console.run_command_list([
98        f'host bind 0 {disk_img}',
99        f'fatls host 0:1 {CAPSULE_INSTALL_DIR}'])
100    for name in filenames:
101        assert name not in ''.join(output)
102
103def check_file_exist(u_boot_console, disk_img, filenames):
104    """check files exist
105
106    Args:
107        u_boot_console -- A console connection to U-Boot.
108        disk_img -- A path to disk image to be used for testing.
109        filenames -- File name array of the target capsule files.
110    """
111    output = u_boot_console.run_command_list([
112        f'host bind 0 {disk_img}',
113        f'fatls host 0:1 {CAPSULE_INSTALL_DIR}'])
114    for name in filenames:
115        assert name in ''.join(output)
116
117def verify_content(u_boot_console, target, expected):
118    """verify the content
119
120    Args:
121        u_boot_console -- A console connection to U-Boot.
122        target -- Target address to verify.
123        expected -- Expected string of the content.
124    """
125    output = u_boot_console.run_command_list([
126        'sf probe 0:0',
127        f'sf read 4000000 {target} 10',
128        'md.b 4000000 10'])
129    assert expected in ''.join(output)
130
131def do_reboot_dtb_specified(u_boot_config, u_boot_console, dtb_filename):
132    """do reboot with specified DTB
133
134    Args:
135        u_boot_config -- U-boot configuration.
136        u_boot_console -- A console connection to U-Boot.
137        dtb_filename -- DTB file name.
138    """
139    mnt_point = u_boot_config.persistent_data_dir + '/test_efi_capsule'
140    u_boot_console.config.dtb = mnt_point + CAPSULE_DATA_DIR \
141                                + f'/{dtb_filename}'
142    u_boot_console.restart_uboot()
143