test: dm: Add test cases for FWU Metadata uclass
[platform/kernel/u-boot.git] / test / py / tests / test_efi_capsule / test_capsule_firmware_fit.py
1 # SPDX-License-Identifier:      GPL-2.0+
2 # Copyright (c) 2020, Linaro Limited
3 # Author: AKASHI Takahiro <takahiro.akashi@linaro.org>
4 #
5 # U-Boot UEFI: Firmware Update Test
6
7 """
8 This test verifies capsule-on-disk firmware update for FIT images
9 """
10
11 from subprocess import check_call, check_output, CalledProcessError
12 import pytest
13 from capsule_defs import *
14
15
16 @pytest.mark.boardspec('sandbox_flattree')
17 @pytest.mark.buildconfigspec('efi_capsule_firmware_fit')
18 @pytest.mark.buildconfigspec('efi_capsule_on_disk')
19 @pytest.mark.buildconfigspec('dfu')
20 @pytest.mark.buildconfigspec('dfu_sf')
21 @pytest.mark.buildconfigspec('cmd_efidebug')
22 @pytest.mark.buildconfigspec('cmd_fat')
23 @pytest.mark.buildconfigspec('cmd_memory')
24 @pytest.mark.buildconfigspec('cmd_nvedit_efi')
25 @pytest.mark.buildconfigspec('cmd_sf')
26 @pytest.mark.slow
27 class TestEfiCapsuleFirmwareFit(object):
28     def test_efi_capsule_fw1(
29             self, u_boot_config, u_boot_console, efi_capsule_data):
30         """
31         Test Case 1 - Update U-Boot and U-Boot environment on SPI Flash
32                       but with an incorrect GUID value in the capsule
33                       No update should happen
34                       0x100000-0x150000: U-Boot binary (but dummy)
35                       0x150000-0x200000: U-Boot environment (but dummy)
36         """
37         # other tests might have run and the
38         # system might not be in a clean state.
39         # Restart before starting the tests.
40         u_boot_console.restart_uboot()
41
42         disk_img = efi_capsule_data
43         with u_boot_console.log.section('Test Case 1-a, before reboot'):
44             output = u_boot_console.run_command_list([
45                 'host bind 0 %s' % disk_img,
46                 'efidebug boot add -b 1 TEST host 0:1 /helloworld.efi -s ""',
47                 'efidebug boot order 1',
48                 'env set -e -nv -bs -rt OsIndications =0x0000000000000004',
49                 'env set dfu_alt_info "sf 0:0=u-boot-bin raw 0x100000 0x50000;u-boot-env raw 0x150000 0x200000"',
50                 'env save'])
51
52             # initialize contents
53             output = u_boot_console.run_command_list([
54                 'sf probe 0:0',
55                 'fatload host 0:1 4000000 %s/u-boot.bin.old' % CAPSULE_DATA_DIR,
56                 'sf write 4000000 100000 10',
57                 'sf read 5000000 100000 10',
58                 'md.b 5000000 10'])
59             assert 'Old' in ''.join(output)
60             output = u_boot_console.run_command_list([
61                 'sf probe 0:0',
62                 'fatload host 0:1 4000000 %s/u-boot.env.old' % CAPSULE_DATA_DIR,
63                 'sf write 4000000 150000 10',
64                 'sf read 5000000 150000 10',
65                 'md.b 5000000 10'])
66             assert 'Old' in ''.join(output)
67
68             # place a capsule file
69             output = u_boot_console.run_command_list([
70                 'fatload host 0:1 4000000 %s/Test05' % CAPSULE_DATA_DIR,
71                 'fatwrite host 0:1 4000000 %s/Test05 $filesize' % CAPSULE_INSTALL_DIR,
72                 'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
73             assert 'Test05' in ''.join(output)
74
75         capsule_early = u_boot_config.buildconfig.get(
76             'config_efi_capsule_on_disk_early')
77         capsule_auth = u_boot_config.buildconfig.get(
78             'config_efi_capsule_authenticate')
79
80         # reboot
81         u_boot_console.restart_uboot(expect_reset = capsule_early)
82
83         with u_boot_console.log.section('Test Case 1-b, after reboot'):
84             if not capsule_early:
85                 # make sure that dfu_alt_info exists even persistent variables
86                 # are not available.
87                 output = u_boot_console.run_command_list([
88                     'env set dfu_alt_info "sf 0:0=u-boot-bin raw 0x100000 0x50000;u-boot-env raw 0x150000 0x200000"',
89                     'host bind 0 %s' % disk_img,
90                     'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
91                 assert 'Test05' in ''.join(output)
92
93                 # need to run uefi command to initiate capsule handling
94                 output = u_boot_console.run_command(
95                     'env print -e Capsule0000', wait_for_reboot = True)
96
97             output = u_boot_console.run_command_list([
98                 'sf probe 0:0',
99                 'sf read 4000000 100000 10',
100                 'md.b 4000000 10'])
101             assert 'u-boot:Old' in ''.join(output)
102
103             output = u_boot_console.run_command_list([
104                 'sf read 4000000 150000 10',
105                 'md.b 4000000 10'])
106             assert 'u-boot-env:Old' in ''.join(output)
107
108     def test_efi_capsule_fw2(
109             self, u_boot_config, u_boot_console, efi_capsule_data):
110         """
111         Test Case 2 - Update U-Boot and U-Boot environment on SPI Flash
112                       0x100000-0x150000: U-Boot binary (but dummy)
113                       0x150000-0x200000: U-Boot environment (but dummy)
114         """
115         disk_img = efi_capsule_data
116         with u_boot_console.log.section('Test Case 2-a, before reboot'):
117             output = u_boot_console.run_command_list([
118                 'host bind 0 %s' % disk_img,
119                 'printenv -e PlatformLangCodes', # workaround for terminal size determination
120                 'efidebug boot add -b 1 TEST host 0:1 /helloworld.efi -s ""',
121                 'efidebug boot order 1',
122                 'env set -e -nv -bs -rt OsIndications =0x0000000000000004',
123                 'env set dfu_alt_info "sf 0:0=u-boot-bin raw 0x100000 0x50000;u-boot-env raw 0x150000 0x200000"',
124                 'env save'])
125
126             # initialize contents
127             output = u_boot_console.run_command_list([
128                 'sf probe 0:0',
129                 'fatload host 0:1 4000000 %s/u-boot.bin.old' % CAPSULE_DATA_DIR,
130                 'sf write 4000000 100000 10',
131                 'sf read 5000000 100000 10',
132                 'md.b 5000000 10'])
133             assert 'Old' in ''.join(output)
134             output = u_boot_console.run_command_list([
135                 'sf probe 0:0',
136                 'fatload host 0:1 4000000 %s/u-boot.env.old' % CAPSULE_DATA_DIR,
137                 'sf write 4000000 150000 10',
138                 'sf read 5000000 150000 10',
139                 'md.b 5000000 10'])
140             assert 'Old' in ''.join(output)
141
142             # place a capsule file
143             output = u_boot_console.run_command_list([
144                 'fatload host 0:1 4000000 %s/Test04' % CAPSULE_DATA_DIR,
145                 'fatwrite host 0:1 4000000 %s/Test04 $filesize' % CAPSULE_INSTALL_DIR,
146                 'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
147             assert 'Test04' in ''.join(output)
148
149         capsule_early = u_boot_config.buildconfig.get(
150             'config_efi_capsule_on_disk_early')
151         capsule_auth = u_boot_config.buildconfig.get(
152             'config_efi_capsule_authenticate')
153
154         # reboot
155         u_boot_console.restart_uboot(expect_reset = capsule_early)
156
157         with u_boot_console.log.section('Test Case 2-b, after reboot'):
158             if not capsule_early:
159                 # make sure that dfu_alt_info exists even persistent variables
160                 # are not available.
161                 output = u_boot_console.run_command_list([
162                     'env set dfu_alt_info "sf 0:0=u-boot-bin raw 0x100000 0x50000;u-boot-env raw 0x150000 0x200000"',
163                     'host bind 0 %s' % disk_img,
164                     'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
165                 assert 'Test04' in ''.join(output)
166
167                 # need to run uefi command to initiate capsule handling
168                 output = u_boot_console.run_command(
169                     'env print -e Capsule0000', wait_for_reboot = True)
170
171             output = u_boot_console.run_command_list([
172                 'host bind 0 %s' % disk_img,
173                 'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
174             assert 'Test04' not in ''.join(output)
175
176             output = u_boot_console.run_command_list([
177                 'sf probe 0:0',
178                 'sf read 4000000 100000 10',
179                 'md.b 4000000 10'])
180             if capsule_auth:
181                 assert 'u-boot:Old' in ''.join(output)
182             else:
183                 assert 'u-boot:New' in ''.join(output)
184
185             output = u_boot_console.run_command_list([
186                 'sf read 4000000 150000 10',
187                 'md.b 4000000 10'])
188             if capsule_auth:
189                 assert 'u-boot-env:Old' in ''.join(output)
190             else:
191                 assert 'u-boot-env:New' in ''.join(output)