Merge branch 'next' of https://source.denx.de/u-boot/custodians/u-boot-net
[platform/kernel/u-boot.git] / test / py / tests / test_efi_capsule / test_capsule_firmware_raw.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 raw 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')
17 @pytest.mark.buildconfigspec('efi_capsule_firmware_raw')
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 TestEfiCapsuleFirmwareRaw(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
38         # other tests might have run and the
39         # system might not be in a clean state.
40         # Restart before starting the tests.
41         u_boot_console.restart_uboot()
42
43         disk_img = efi_capsule_data
44         with u_boot_console.log.section('Test Case 1-a, before reboot'):
45             output = u_boot_console.run_command_list([
46                 'host bind 0 %s' % disk_img,
47                 'efidebug boot add -b 1 TEST host 0:1 /helloworld.efi -s ""',
48                 'efidebug boot order 1',
49                 'env set -e -nv -bs -rt OsIndications =0x0000000000000004',
50                 'env set dfu_alt_info "sf 0:0=u-boot-bin raw 0x100000 0x50000;u-boot-env raw 0x150000 0x200000"',
51                 'env save'])
52
53             # initialize contents
54             output = u_boot_console.run_command_list([
55                 'sf probe 0:0',
56                 'fatload host 0:1 4000000 %s/u-boot.bin.old' % CAPSULE_DATA_DIR,
57                 'sf write 4000000 100000 10',
58                 'sf read 5000000 100000 10',
59                 'md.b 5000000 10'])
60             assert 'Old' in ''.join(output)
61             output = u_boot_console.run_command_list([
62                 'sf probe 0:0',
63                 'fatload host 0:1 4000000 %s/u-boot.env.old' % CAPSULE_DATA_DIR,
64                 'sf write 4000000 150000 10',
65                 'sf read 5000000 150000 10',
66                 'md.b 5000000 10'])
67             assert 'Old' in ''.join(output)
68
69             # place a capsule file
70             output = u_boot_console.run_command_list([
71                 'fatload host 0:1 4000000 %s/Test03' % CAPSULE_DATA_DIR,
72                 'fatwrite host 0:1 4000000 %s/Test03 $filesize' % CAPSULE_INSTALL_DIR,
73                 'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
74             assert 'Test03' in ''.join(output)
75
76         # reboot
77         u_boot_console.restart_uboot()
78
79         capsule_early = u_boot_config.buildconfig.get(
80             'config_efi_capsule_on_disk_early')
81
82         with u_boot_console.log.section('Test Case 1-b, after reboot'):
83             if not capsule_early:
84                 # make sure that dfu_alt_info exists even persistent variables
85                 # are not available.
86                 output = u_boot_console.run_command_list([
87                     'env set dfu_alt_info "sf 0:0=u-boot-bin raw 0x100000 0x50000;u-boot-env raw 0x150000 0x200000"',
88                     'host bind 0 %s' % disk_img,
89                     'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
90                 assert 'Test03' in ''.join(output)
91
92                 # need to run uefi command to initiate capsule handling
93                 output = u_boot_console.run_command(
94                     'env print -e Capsule0000', wait_for_reboot = True)
95
96             output = u_boot_console.run_command_list([
97                 'sf probe 0:0',
98                 'sf read 4000000 100000 10',
99                 'md.b 4000000 10'])
100             assert 'u-boot:Old' in ''.join(output)
101
102             output = u_boot_console.run_command_list([
103                 'sf read 4000000 150000 10',
104                 'md.b 4000000 10'])
105             assert 'u-boot-env:Old' in ''.join(output)
106
107     def test_efi_capsule_fw2(
108             self, u_boot_config, u_boot_console, efi_capsule_data):
109         """
110         Test Case 2 - Update U-Boot and U-Boot environment on SPI Flash
111                       but with OsIndications unset
112                       No update should happen
113                       0x100000-0x150000: U-Boot binary (but dummy)
114                       0x150000-0x200000: U-Boot environment (but dummy)
115         """
116         disk_img = efi_capsule_data
117         with u_boot_console.log.section('Test Case 2-a, before reboot'):
118             output = u_boot_console.run_command_list([
119                 'host bind 0 %s' % disk_img,
120                 'efidebug boot add -b 1 TEST host 0:1 /helloworld.efi -s ""',
121                 'efidebug boot order 1',
122                 'env set -e OsIndications',
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 the capsule files
143             output = u_boot_console.run_command_list([
144                 'fatload host 0:1 4000000 %s/Test01' % CAPSULE_DATA_DIR,
145                 'fatwrite host 0:1 4000000 %s/Test01 $filesize' % CAPSULE_INSTALL_DIR,
146                 'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
147             assert 'Test01' in ''.join(output)
148
149             output = u_boot_console.run_command_list([
150                 'fatload host 0:1 4000000 %s/Test02' % CAPSULE_DATA_DIR,
151                 'fatwrite host 0:1 4000000 %s/Test02 $filesize' % CAPSULE_INSTALL_DIR,
152                 'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
153             assert 'Test02' in ''.join(output)
154
155         # reboot
156         u_boot_console.restart_uboot()
157
158         capsule_early = u_boot_config.buildconfig.get(
159             'config_efi_capsule_on_disk_early')
160         with u_boot_console.log.section('Test Case 2-b, after reboot'):
161             if not capsule_early:
162                 # make sure that dfu_alt_info exists even persistent variables
163                 # are not available.
164                 output = u_boot_console.run_command_list([
165                     'env set dfu_alt_info "sf 0:0=u-boot-bin raw 0x100000 0x50000;u-boot-env raw 0x150000 0x200000"',
166                     'host bind 0 %s' % disk_img,
167                     'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
168                 assert 'Test01' in ''.join(output)
169                 assert 'Test02' in ''.join(output)
170
171                 # need to run uefi command to initiate capsule handling
172                 output = u_boot_console.run_command(
173                     'env print -e Capsule0000')
174
175             output = u_boot_console.run_command_list([
176                 'host bind 0 %s' % disk_img,
177                 'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
178             assert 'Test01' in ''.join(output)
179             assert 'Test02' in ''.join(output)
180
181             output = u_boot_console.run_command_list([
182                 'sf probe 0:0',
183                 'sf read 4000000 100000 10',
184                 'md.b 4000000 10'])
185             assert 'u-boot:Old' in ''.join(output)
186
187             output = u_boot_console.run_command_list([
188                 'sf read 4000000 150000 10',
189                 'md.b 4000000 10'])
190             assert 'u-boot-env:Old' in ''.join(output)
191
192     def test_efi_capsule_fw3(
193             self, u_boot_config, u_boot_console, efi_capsule_data):
194         """
195         Test Case 3 - Update U-Boot on SPI Flash, raw image format
196                       0x100000-0x150000: U-Boot binary (but dummy)
197         """
198         disk_img = efi_capsule_data
199         with u_boot_console.log.section('Test Case 3-a, before reboot'):
200             output = u_boot_console.run_command_list([
201                 'host bind 0 %s' % disk_img,
202                 'efidebug boot add -b 1 TEST host 0:1 /helloworld.efi -s ""',
203                 'efidebug boot order 1',
204                 'env set -e -nv -bs -rt OsIndications =0x0000000000000004',
205                 'env set dfu_alt_info "sf 0:0=u-boot-bin raw 0x100000 0x50000;u-boot-env raw 0x150000 0x200000"',
206                 'env save'])
207
208             # initialize contents
209             output = u_boot_console.run_command_list([
210                 'sf probe 0:0',
211                 'fatload host 0:1 4000000 %s/u-boot.bin.old' % CAPSULE_DATA_DIR,
212                 'sf write 4000000 100000 10',
213                 'sf read 5000000 100000 10',
214                 'md.b 5000000 10'])
215             assert 'Old' in ''.join(output)
216
217             output = u_boot_console.run_command_list([
218                 'sf probe 0:0',
219                 'fatload host 0:1 4000000 %s/u-boot.env.old' % CAPSULE_DATA_DIR,
220                 'sf write 4000000 150000 10',
221                 'sf read 5000000 100000 10',
222                 'md.b 5000000 10'])
223             assert 'Old' in ''.join(output)
224
225             # place the capsule files
226             output = u_boot_console.run_command_list([
227                 'fatload host 0:1 4000000 %s/Test01' % CAPSULE_DATA_DIR,
228                 'fatwrite host 0:1 4000000 %s/Test01 $filesize' % CAPSULE_INSTALL_DIR,
229                 'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
230             assert 'Test01' in ''.join(output)
231
232             output = u_boot_console.run_command_list([
233                 'fatload host 0:1 4000000 %s/Test02' % CAPSULE_DATA_DIR,
234                 'fatwrite host 0:1 4000000 %s/Test02 $filesize' % CAPSULE_INSTALL_DIR,
235                 'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
236             assert 'Test02' in ''.join(output)
237
238         capsule_early = u_boot_config.buildconfig.get(
239             'config_efi_capsule_on_disk_early')
240         capsule_auth = u_boot_config.buildconfig.get(
241             'config_efi_capsule_authenticate')
242
243         # reboot
244         u_boot_console.restart_uboot(expect_reset = capsule_early)
245
246         with u_boot_console.log.section('Test Case 3-b, after reboot'):
247             if not capsule_early:
248                 # make sure that dfu_alt_info exists even persistent variables
249                 # are not available.
250                 output = u_boot_console.run_command_list([
251                     'env set dfu_alt_info "sf 0:0=u-boot-bin raw 0x100000 0x50000;u-boot-env raw 0x150000 0x200000"',
252                     'host bind 0 %s' % disk_img,
253                     'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
254                 assert 'Test01' in ''.join(output)
255                 assert 'Test02' in ''.join(output)
256
257                 # need to run uefi command to initiate capsule handling
258                 output = u_boot_console.run_command(
259                     'env print -e Capsule0000', wait_for_reboot = True)
260
261             # make sure the dfu_alt_info exists because it is required for making ESRT.
262             output = u_boot_console.run_command_list([
263                 'env set dfu_alt_info "sf 0:0=u-boot-bin raw 0x100000 0x50000;u-boot-env raw 0x150000 0x200000"',
264                 'efidebug capsule esrt'])
265
266             # ensure that SANDBOX_UBOOT_ENV_IMAGE_GUID is in the ESRT.
267             assert '5A7021F5-FEF2-48B4-AABA-832E777418C0' in ''.join(output)
268
269             # ensure that SANDBOX_UBOOT_IMAGE_GUID is in the ESRT.
270             assert '09D7CF52-0720-4710-91D1-08469B7FE9C8' in ''.join(output)
271
272             output = u_boot_console.run_command_list([
273                 'host bind 0 %s' % disk_img,
274                 'fatls host 0:1 %s' % CAPSULE_INSTALL_DIR])
275             assert 'Test01' not in ''.join(output)
276             assert 'Test02' not in ''.join(output)
277
278             output = u_boot_console.run_command_list([
279                 'sf probe 0:0',
280                 'sf read 4000000 100000 10',
281                 'md.b 4000000 10'])
282             if capsule_auth:
283                 assert 'u-boot:Old' in ''.join(output)
284             else:
285                 assert 'u-boot:New' in ''.join(output)
286
287             output = u_boot_console.run_command_list([
288                 'sf probe 0:0',
289                 'sf read 4000000 150000 10',
290                 'md.b 4000000 10'])
291             if capsule_auth:
292                 assert 'u-boot-env:Old' in ''.join(output)
293             else:
294                 assert 'u-boot-env:New' in ''.join(output)