AKASHI Takahiro | 0f626ce | 2020-11-30 18:12:16 +0900 | [diff] [blame] | 1 | # SPDX-License-Identifier: GPL-2.0+ |
| 2 | # Copyright (c) 2020, Linaro Limited |
| 3 | # Author: AKASHI Takahiro <takahiro.akashi@linaro.org> |
| 4 | |
Heinrich Schuchardt | 14dee8b | 2023-05-03 07:08:05 +0200 | [diff] [blame] | 5 | """Fixture for UEFI capsule test.""" |
AKASHI Takahiro | 0f626ce | 2020-11-30 18:12:16 +0900 | [diff] [blame] | 6 | |
Sughosh Ganu | 467130a | 2023-08-22 23:10:00 +0530 | [diff] [blame] | 7 | import os |
| 8 | |
Heinrich Schuchardt | 2275a41 | 2023-04-13 18:13:32 +0200 | [diff] [blame] | 9 | from subprocess import call, check_call, CalledProcessError |
Tom Rini | 38b31ae | 2025-03-20 07:59:28 -0600 | [diff] [blame] | 10 | from tests import fs_helper |
Heinrich Schuchardt | 2275a41 | 2023-04-13 18:13:32 +0200 | [diff] [blame] | 11 | import pytest |
| 12 | from capsule_defs import CAPSULE_DATA_DIR, CAPSULE_INSTALL_DIR, EFITOOLS_PATH |
AKASHI Takahiro | 0f626ce | 2020-11-30 18:12:16 +0900 | [diff] [blame] | 13 | |
Tom Rini | 38b31ae | 2025-03-20 07:59:28 -0600 | [diff] [blame] | 14 | @pytest.fixture(scope='function') |
| 15 | def efi_capsule_data(request, ubman): |
Heinrich Schuchardt | 14dee8b | 2023-05-03 07:08:05 +0200 | [diff] [blame] | 16 | """Set up a file system and return path to image. |
| 17 | |
| 18 | The function sets up a file system to be used in UEFI capsule and |
| 19 | authentication test and returns a path to disk image to be used |
| 20 | for testing. |
AKASHI Takahiro | 0f626ce | 2020-11-30 18:12:16 +0900 | [diff] [blame] | 21 | |
Heinrich Schuchardt | 2275a41 | 2023-04-13 18:13:32 +0200 | [diff] [blame] | 22 | request -- Pytest request object. |
Tom Rini | 38b31ae | 2025-03-20 07:59:28 -0600 | [diff] [blame] | 23 | ubman -- U-Boot configuration. |
AKASHI Takahiro | 0f626ce | 2020-11-30 18:12:16 +0900 | [diff] [blame] | 24 | """ |
AKASHI Takahiro | 0f626ce | 2020-11-30 18:12:16 +0900 | [diff] [blame] | 25 | try: |
Tom Rini | 38b31ae | 2025-03-20 07:59:28 -0600 | [diff] [blame] | 26 | image_path, mnt_point = fs_helper.setup_image(ubman, 0, 0xc, |
| 27 | basename='test_efi_capsule') |
| 28 | data_dir = mnt_point + CAPSULE_DATA_DIR |
| 29 | install_dir = mnt_point + CAPSULE_INSTALL_DIR |
| 30 | |
AKASHI Takahiro | 0f626ce | 2020-11-30 18:12:16 +0900 | [diff] [blame] | 31 | # Create a target device |
| 32 | check_call('dd if=/dev/zero of=./spi.bin bs=1MiB count=16', shell=True) |
| 33 | |
| 34 | check_call('rm -rf %s' % mnt_point, shell=True) |
| 35 | check_call('mkdir -p %s' % data_dir, shell=True) |
| 36 | check_call('mkdir -p %s' % install_dir, shell=True) |
| 37 | |
Tom Rini | 38b31ae | 2025-03-20 07:59:28 -0600 | [diff] [blame] | 38 | capsule_auth_enabled = ubman.config.buildconfig.get( |
AKASHI Takahiro | 0bdde5f | 2022-02-09 19:10:38 +0900 | [diff] [blame] | 39 | 'config_efi_capsule_authenticate') |
Tom Rini | 38b31ae | 2025-03-20 07:59:28 -0600 | [diff] [blame] | 40 | key_dir = ubman.config.source_dir + '/board/sandbox' |
AKASHI Takahiro | 0bdde5f | 2022-02-09 19:10:38 +0900 | [diff] [blame] | 41 | if capsule_auth_enabled: |
Sughosh Ganu | 467130a | 2023-08-22 23:10:00 +0530 | [diff] [blame] | 42 | # Get the keys from the board directory |
| 43 | check_call('cp %s/capsule_priv_key_good.key %s/SIGNER.key' |
| 44 | % (key_dir, data_dir), shell=True) |
| 45 | check_call('cp %s/capsule_pub_key_good.crt %s/SIGNER.crt' |
| 46 | % (key_dir, data_dir), shell=True) |
| 47 | check_call('cp %s/capsule_pub_esl_good.esl %s/SIGNER.esl' |
| 48 | % (key_dir, data_dir), shell=True) |
| 49 | |
| 50 | check_call('cp %s/capsule_priv_key_bad.key %s/SIGNER2.key' |
| 51 | % (key_dir, data_dir), shell=True) |
| 52 | check_call('cp %s/capsule_pub_key_bad.crt %s/SIGNER2.crt' |
| 53 | % (key_dir, data_dir), shell=True) |
AKASHI Takahiro | 0bdde5f | 2022-02-09 19:10:38 +0900 | [diff] [blame] | 54 | |
Masahisa Kojima | 73a8a1d | 2023-06-07 14:42:00 +0900 | [diff] [blame] | 55 | # Update dtb to add the version information |
| 56 | check_call('cd %s; ' |
Rasmus Villemoes | 2ac9b525 | 2024-07-10 09:16:11 +0200 | [diff] [blame] | 57 | 'cp %s/test/py/tests/test_efi_capsule/version.dtso .' |
Tom Rini | 38b31ae | 2025-03-20 07:59:28 -0600 | [diff] [blame] | 58 | % (data_dir, ubman.config.source_dir), shell=True) |
Sughosh Ganu | 33f8386 | 2023-08-22 23:10:07 +0530 | [diff] [blame] | 59 | |
Masahisa Kojima | 73a8a1d | 2023-06-07 14:42:00 +0900 | [diff] [blame] | 60 | if capsule_auth_enabled: |
| 61 | check_call('cd %s; ' |
Sughosh Ganu | 33f8386 | 2023-08-22 23:10:07 +0530 | [diff] [blame] | 62 | 'cp %s/arch/sandbox/dts/test.dtb test_sig.dtb' |
Tom Rini | 38b31ae | 2025-03-20 07:59:28 -0600 | [diff] [blame] | 63 | % (data_dir, ubman.config.build_dir), shell=True) |
Sughosh Ganu | 33f8386 | 2023-08-22 23:10:07 +0530 | [diff] [blame] | 64 | check_call('cd %s; ' |
Rasmus Villemoes | 2ac9b525 | 2024-07-10 09:16:11 +0200 | [diff] [blame] | 65 | 'dtc -@ -I dts -O dtb -o version.dtbo version.dtso; ' |
Masahisa Kojima | 73a8a1d | 2023-06-07 14:42:00 +0900 | [diff] [blame] | 66 | 'fdtoverlay -i test_sig.dtb ' |
| 67 | '-o test_ver.dtb version.dtbo' |
| 68 | % (data_dir), shell=True) |
| 69 | else: |
| 70 | check_call('cd %s; ' |
Rasmus Villemoes | 2ac9b525 | 2024-07-10 09:16:11 +0200 | [diff] [blame] | 71 | 'dtc -@ -I dts -O dtb -o version.dtbo version.dtso; ' |
Masahisa Kojima | 73a8a1d | 2023-06-07 14:42:00 +0900 | [diff] [blame] | 72 | 'fdtoverlay -i %s/arch/sandbox/dts/test.dtb ' |
| 73 | '-o test_ver.dtb version.dtbo' |
Tom Rini | 38b31ae | 2025-03-20 07:59:28 -0600 | [diff] [blame] | 74 | % (data_dir, ubman.config.build_dir), shell=True) |
Masahisa Kojima | 73a8a1d | 2023-06-07 14:42:00 +0900 | [diff] [blame] | 75 | |
AKASHI Takahiro | 0f626ce | 2020-11-30 18:12:16 +0900 | [diff] [blame] | 76 | # two regions: one for u-boot.bin and the other for u-boot.env |
Sughosh Ganu | 2db313d | 2022-04-15 11:29:38 +0530 | [diff] [blame] | 77 | check_call('cd %s; echo -n u-boot:Old > u-boot.bin.old; echo -n u-boot:New > u-boot.bin.new; echo -n u-boot-env:Old > u-boot.env.old; echo -n u-boot-env:New > u-boot.env.new' % data_dir, |
AKASHI Takahiro | 0f626ce | 2020-11-30 18:12:16 +0900 | [diff] [blame] | 78 | shell=True) |
Sughosh Ganu | 2db313d | 2022-04-15 11:29:38 +0530 | [diff] [blame] | 79 | |
Sughosh Ganu | 467130a | 2023-08-22 23:10:00 +0530 | [diff] [blame] | 80 | pythonpath = os.environ.get('PYTHONPATH', '') |
Tom Rini | 38b31ae | 2025-03-20 07:59:28 -0600 | [diff] [blame] | 81 | os.environ['PYTHONPATH'] = pythonpath + ':' + '%s/scripts/dtc/pylibfdt' % ubman.config.build_dir |
Sughosh Ganu | 467130a | 2023-08-22 23:10:00 +0530 | [diff] [blame] | 82 | check_call('cd %s; ' |
| 83 | 'cc -E -I %s/include -x assembler-with-cpp -o capsule_gen_tmp.dts %s/test/py/tests/test_efi_capsule/capsule_gen_binman.dts; ' |
| 84 | 'dtc -I dts -O dtb capsule_gen_tmp.dts -o capsule_binman.dtb;' |
Tom Rini | 38b31ae | 2025-03-20 07:59:28 -0600 | [diff] [blame] | 85 | % (data_dir, ubman.config.source_dir, ubman.config.source_dir), shell=True) |
Sughosh Ganu | 467130a | 2023-08-22 23:10:00 +0530 | [diff] [blame] | 86 | check_call('cd %s; ' |
| 87 | './tools/binman/binman --toolpath %s/tools build -u -d %s/capsule_binman.dtb -O %s -m --allow-missing -I %s -I ./board/sandbox -I ./arch/sandbox/dts' |
Tom Rini | 38b31ae | 2025-03-20 07:59:28 -0600 | [diff] [blame] | 88 | % (ubman.config.source_dir, ubman.config.build_dir, data_dir, data_dir, data_dir), shell=True) |
| 89 | check_call('cp %s/Test* %s' % (ubman.config.build_dir, data_dir), shell=True) |
Sughosh Ganu | 467130a | 2023-08-22 23:10:00 +0530 | [diff] [blame] | 90 | os.environ['PYTHONPATH'] = pythonpath |
AKASHI Takahiro | 0f626ce | 2020-11-30 18:12:16 +0900 | [diff] [blame] | 91 | |
Tom Rini | 38b31ae | 2025-03-20 07:59:28 -0600 | [diff] [blame] | 92 | # Create a 16MiB partition as the EFI system partition in the disk |
| 93 | # image |
| 94 | fsfile = fs_helper.mk_fs(ubman.config, 'vfat', 0x1000000, |
| 95 | 'test_efi_capsule', mnt_point) |
| 96 | check_call(f'dd conv=notrunc if={fsfile} of={image_path} bs=1M seek=1', shell=True) |
| 97 | check_call('sgdisk --mbrtogpt %s' % image_path, shell=True) |
AKASHI Takahiro | 0f626ce | 2020-11-30 18:12:16 +0900 | [diff] [blame] | 98 | check_call('sgdisk %s -A 1:set:0 -t 1:C12A7328-F81F-11D2-BA4B-00A0C93EC93B' % |
| 99 | image_path, shell=True) |
Tom Rini | 38b31ae | 2025-03-20 07:59:28 -0600 | [diff] [blame] | 100 | call('rm -f %s' % fsfile, shell=True) |
AKASHI Takahiro | 0f626ce | 2020-11-30 18:12:16 +0900 | [diff] [blame] | 101 | |
| 102 | except CalledProcessError as exception: |
| 103 | pytest.skip('Setup failed: %s' % exception.cmd) |
| 104 | return |
| 105 | else: |
| 106 | yield image_path |
| 107 | finally: |
| 108 | call('rm -rf %s' % mnt_point, shell=True) |
| 109 | call('rm -f %s' % image_path, shell=True) |
| 110 | call('rm -f ./spi.bin', shell=True) |