blob: 961d2e0b3c1967d1db4f288eab05063e0797c225 [file] [log] [blame]
AKASHI Takahiro0f626ce2020-11-30 18:12:16 +09001# SPDX-License-Identifier: GPL-2.0+
2# Copyright (c) 2020, Linaro Limited
3# Author: AKASHI Takahiro <takahiro.akashi@linaro.org>
4
Heinrich Schuchardt14dee8b2023-05-03 07:08:05 +02005"""Fixture for UEFI capsule test."""
AKASHI Takahiro0f626ce2020-11-30 18:12:16 +09006
Sughosh Ganu467130a2023-08-22 23:10:00 +05307import os
8
Heinrich Schuchardt2275a412023-04-13 18:13:32 +02009from subprocess import call, check_call, CalledProcessError
Tom Rini38b31ae2025-03-20 07:59:28 -060010from tests import fs_helper
Heinrich Schuchardt2275a412023-04-13 18:13:32 +020011import pytest
12from capsule_defs import CAPSULE_DATA_DIR, CAPSULE_INSTALL_DIR, EFITOOLS_PATH
AKASHI Takahiro0f626ce2020-11-30 18:12:16 +090013
Tom Rini38b31ae2025-03-20 07:59:28 -060014@pytest.fixture(scope='function')
15def efi_capsule_data(request, ubman):
Heinrich Schuchardt14dee8b2023-05-03 07:08:05 +020016 """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 Takahiro0f626ce2020-11-30 18:12:16 +090021
Heinrich Schuchardt2275a412023-04-13 18:13:32 +020022 request -- Pytest request object.
Tom Rini38b31ae2025-03-20 07:59:28 -060023 ubman -- U-Boot configuration.
AKASHI Takahiro0f626ce2020-11-30 18:12:16 +090024 """
AKASHI Takahiro0f626ce2020-11-30 18:12:16 +090025 try:
Tom Rini38b31ae2025-03-20 07:59:28 -060026 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 Takahiro0f626ce2020-11-30 18:12:16 +090031 # 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 Rini38b31ae2025-03-20 07:59:28 -060038 capsule_auth_enabled = ubman.config.buildconfig.get(
AKASHI Takahiro0bdde5f2022-02-09 19:10:38 +090039 'config_efi_capsule_authenticate')
Tom Rini38b31ae2025-03-20 07:59:28 -060040 key_dir = ubman.config.source_dir + '/board/sandbox'
AKASHI Takahiro0bdde5f2022-02-09 19:10:38 +090041 if capsule_auth_enabled:
Sughosh Ganu467130a2023-08-22 23:10:00 +053042 # 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 Takahiro0bdde5f2022-02-09 19:10:38 +090054
Masahisa Kojima73a8a1d2023-06-07 14:42:00 +090055 # Update dtb to add the version information
56 check_call('cd %s; '
Rasmus Villemoes2ac9b5252024-07-10 09:16:11 +020057 'cp %s/test/py/tests/test_efi_capsule/version.dtso .'
Tom Rini38b31ae2025-03-20 07:59:28 -060058 % (data_dir, ubman.config.source_dir), shell=True)
Sughosh Ganu33f83862023-08-22 23:10:07 +053059
Masahisa Kojima73a8a1d2023-06-07 14:42:00 +090060 if capsule_auth_enabled:
61 check_call('cd %s; '
Sughosh Ganu33f83862023-08-22 23:10:07 +053062 'cp %s/arch/sandbox/dts/test.dtb test_sig.dtb'
Tom Rini38b31ae2025-03-20 07:59:28 -060063 % (data_dir, ubman.config.build_dir), shell=True)
Sughosh Ganu33f83862023-08-22 23:10:07 +053064 check_call('cd %s; '
Rasmus Villemoes2ac9b5252024-07-10 09:16:11 +020065 'dtc -@ -I dts -O dtb -o version.dtbo version.dtso; '
Masahisa Kojima73a8a1d2023-06-07 14:42:00 +090066 '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 Villemoes2ac9b5252024-07-10 09:16:11 +020071 'dtc -@ -I dts -O dtb -o version.dtbo version.dtso; '
Masahisa Kojima73a8a1d2023-06-07 14:42:00 +090072 'fdtoverlay -i %s/arch/sandbox/dts/test.dtb '
73 '-o test_ver.dtb version.dtbo'
Tom Rini38b31ae2025-03-20 07:59:28 -060074 % (data_dir, ubman.config.build_dir), shell=True)
Masahisa Kojima73a8a1d2023-06-07 14:42:00 +090075
AKASHI Takahiro0f626ce2020-11-30 18:12:16 +090076 # two regions: one for u-boot.bin and the other for u-boot.env
Sughosh Ganu2db313d2022-04-15 11:29:38 +053077 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 Takahiro0f626ce2020-11-30 18:12:16 +090078 shell=True)
Sughosh Ganu2db313d2022-04-15 11:29:38 +053079
Sughosh Ganu467130a2023-08-22 23:10:00 +053080 pythonpath = os.environ.get('PYTHONPATH', '')
Tom Rini38b31ae2025-03-20 07:59:28 -060081 os.environ['PYTHONPATH'] = pythonpath + ':' + '%s/scripts/dtc/pylibfdt' % ubman.config.build_dir
Sughosh Ganu467130a2023-08-22 23:10:00 +053082 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 Rini38b31ae2025-03-20 07:59:28 -060085 % (data_dir, ubman.config.source_dir, ubman.config.source_dir), shell=True)
Sughosh Ganu467130a2023-08-22 23:10:00 +053086 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 Rini38b31ae2025-03-20 07:59:28 -060088 % (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 Ganu467130a2023-08-22 23:10:00 +053090 os.environ['PYTHONPATH'] = pythonpath
AKASHI Takahiro0f626ce2020-11-30 18:12:16 +090091
Tom Rini38b31ae2025-03-20 07:59:28 -060092 # 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 Takahiro0f626ce2020-11-30 18:12:16 +090098 check_call('sgdisk %s -A 1:set:0 -t 1:C12A7328-F81F-11D2-BA4B-00A0C93EC93B' %
99 image_path, shell=True)
Tom Rini38b31ae2025-03-20 07:59:28 -0600100 call('rm -f %s' % fsfile, shell=True)
AKASHI Takahiro0f626ce2020-11-30 18:12:16 +0900101
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)