1# SPDX-License-Identifier: GPL-2.0+ 2# Copyright (c) 2020, Linaro Limited 3# Author: AKASHI Takahiro <takahiro.akashi@linaro.org> 4 5"""Fixture for UEFI capsule test.""" 6 7import os 8 9from subprocess import call, check_call, CalledProcessError 10from tests import fs_helper 11import pytest 12from capsule_defs import CAPSULE_DATA_DIR, CAPSULE_INSTALL_DIR, EFITOOLS_PATH 13 14@pytest.fixture(scope='function') 15def efi_capsule_data(request, ubman): 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. 21 22 request -- Pytest request object. 23 ubman -- U-Boot configuration. 24 """ 25 try: 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 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 38 capsule_auth_enabled = ubman.config.buildconfig.get( 39 'config_efi_capsule_authenticate') 40 key_dir = ubman.config.source_dir + '/board/sandbox' 41 if capsule_auth_enabled: 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) 54 55 # Update dtb to add the version information 56 check_call('cd %s; ' 57 'cp %s/test/py/tests/test_efi_capsule/version.dtso .' 58 % (data_dir, ubman.config.source_dir), shell=True) 59 60 if capsule_auth_enabled: 61 check_call('cd %s; ' 62 'cp %s/arch/sandbox/dts/test.dtb test_sig.dtb' 63 % (data_dir, ubman.config.build_dir), shell=True) 64 check_call('cd %s; ' 65 'dtc -@ -I dts -O dtb -o version.dtbo version.dtso; ' 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; ' 71 'dtc -@ -I dts -O dtb -o version.dtbo version.dtso; ' 72 'fdtoverlay -i %s/arch/sandbox/dts/test.dtb ' 73 '-o test_ver.dtb version.dtbo' 74 % (data_dir, ubman.config.build_dir), shell=True) 75 76 # two regions: one for u-boot.bin and the other for u-boot.env 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, 78 shell=True) 79 80 pythonpath = os.environ.get('PYTHONPATH', '') 81 os.environ['PYTHONPATH'] = pythonpath + ':' + '%s/scripts/dtc/pylibfdt' % ubman.config.build_dir 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;' 85 % (data_dir, ubman.config.source_dir, ubman.config.source_dir), shell=True) 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' 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) 90 os.environ['PYTHONPATH'] = pythonpath 91 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) 98 check_call('sgdisk %s -A 1:set:0 -t 1:C12A7328-F81F-11D2-BA4B-00A0C93EC93B' % 99 image_path, shell=True) 100 call('rm -f %s' % fsfile, shell=True) 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) 111