1# SPDX-License-Identifier: GPL-2.0+ 2# Copyright (c) 2020, Linaro Limited 3# Author: AKASHI Takahiro <takahiro.akashi@linaro.org> 4 5import os 6import os.path 7import re 8from subprocess import call, check_call, check_output, CalledProcessError 9import pytest 10from capsule_defs import * 11 12# 13# Fixture for UEFI secure boot test 14# 15 16 17@pytest.fixture(scope='session') 18def efi_capsule_data(request, u_boot_config): 19 """Set up a file system to be used in UEFI capsule test. 20 21 Args: 22 request: Pytest request object. 23 u_boot_config: U-boot configuration. 24 25 Return: 26 A path to disk image to be used for testing 27 """ 28 global CAPSULE_DATA_DIR, CAPSULE_INSTALL_DIR 29 30 mnt_point = u_boot_config.persistent_data_dir + '/test_efi_capsule' 31 data_dir = mnt_point + CAPSULE_DATA_DIR 32 install_dir = mnt_point + CAPSULE_INSTALL_DIR 33 image_path = u_boot_config.persistent_data_dir + '/test_efi_capsule.img' 34 35 try: 36 # Create a target device 37 check_call('dd if=/dev/zero of=./spi.bin bs=1MiB count=16', shell=True) 38 39 check_call('rm -rf %s' % mnt_point, shell=True) 40 check_call('mkdir -p %s' % data_dir, shell=True) 41 check_call('mkdir -p %s' % install_dir, shell=True) 42 43 # Create capsule files 44 # two regions: one for u-boot.bin and the other for u-boot.env 45 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, 46 shell=True) 47 check_call('sed -e \"s?BINFILE1?u-boot.bin.new?\" -e \"s?BINFILE2?u-boot.env.new?\" %s/test/py/tests/test_efi_capsule/uboot_bin_env.its > %s/uboot_bin_env.its' % 48 (u_boot_config.source_dir, data_dir), 49 shell=True) 50 check_call('cd %s; %s/tools/mkimage -f uboot_bin_env.its uboot_bin_env.itb' % 51 (data_dir, u_boot_config.build_dir), 52 shell=True) 53 check_call('cd %s; %s/tools/mkeficapsule --fit uboot_bin_env.itb --index 1 Test01' % 54 (data_dir, u_boot_config.build_dir), 55 shell=True) 56 check_call('cd %s; %s/tools/mkeficapsule --raw u-boot.bin.new --index 1 Test02' % 57 (data_dir, u_boot_config.build_dir), 58 shell=True) 59 60 # Create a disk image with EFI system partition 61 check_call('virt-make-fs --partition=gpt --size=+1M --type=vfat %s %s' % 62 (mnt_point, image_path), shell=True) 63 check_call('sgdisk %s -A 1:set:0 -t 1:C12A7328-F81F-11D2-BA4B-00A0C93EC93B' % 64 image_path, shell=True) 65 66 except CalledProcessError as exception: 67 pytest.skip('Setup failed: %s' % exception.cmd) 68 return 69 else: 70 yield image_path 71 finally: 72 call('rm -rf %s' % mnt_point, shell=True) 73 call('rm -f %s' % image_path, shell=True) 74 call('rm -f ./spi.bin', shell=True) 75