blob: a337e629362525e516ad14c20c99df3f0841f384 [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
Heinrich Schuchardt2275a412023-04-13 18:13:32 +02007from subprocess import call, check_call, CalledProcessError
8import pytest
9from capsule_defs import CAPSULE_DATA_DIR, CAPSULE_INSTALL_DIR, EFITOOLS_PATH
AKASHI Takahiro0f626ce2020-11-30 18:12:16 +090010
AKASHI Takahiro0f626ce2020-11-30 18:12:16 +090011@pytest.fixture(scope='session')
12def efi_capsule_data(request, u_boot_config):
Heinrich Schuchardt14dee8b2023-05-03 07:08:05 +020013 """Set up a file system and return path to image.
14
15 The function sets up a file system to be used in UEFI capsule and
16 authentication test and returns a path to disk image to be used
17 for testing.
AKASHI Takahiro0f626ce2020-11-30 18:12:16 +090018
Heinrich Schuchardt2275a412023-04-13 18:13:32 +020019 request -- Pytest request object.
20 u_boot_config -- U-boot configuration.
AKASHI Takahiro0f626ce2020-11-30 18:12:16 +090021 """
AKASHI Takahiro0f626ce2020-11-30 18:12:16 +090022 mnt_point = u_boot_config.persistent_data_dir + '/test_efi_capsule'
23 data_dir = mnt_point + CAPSULE_DATA_DIR
24 install_dir = mnt_point + CAPSULE_INSTALL_DIR
25 image_path = u_boot_config.persistent_data_dir + '/test_efi_capsule.img'
26
27 try:
28 # Create a target device
29 check_call('dd if=/dev/zero of=./spi.bin bs=1MiB count=16', shell=True)
30
31 check_call('rm -rf %s' % mnt_point, shell=True)
32 check_call('mkdir -p %s' % data_dir, shell=True)
33 check_call('mkdir -p %s' % install_dir, shell=True)
34
AKASHI Takahiro0bdde5f2022-02-09 19:10:38 +090035 capsule_auth_enabled = u_boot_config.buildconfig.get(
36 'config_efi_capsule_authenticate')
37 if capsule_auth_enabled:
38 # Create private key (SIGNER.key) and certificate (SIGNER.crt)
39 check_call('cd %s; '
40 'openssl req -x509 -sha256 -newkey rsa:2048 '
41 '-subj /CN=TEST_SIGNER/ -keyout SIGNER.key '
42 '-out SIGNER.crt -nodes -days 365'
43 % data_dir, shell=True)
44 check_call('cd %s; %scert-to-efi-sig-list SIGNER.crt SIGNER.esl'
45 % (data_dir, EFITOOLS_PATH), shell=True)
46
47 # Update dtb adding capsule certificate
48 check_call('cd %s; '
49 'cp %s/test/py/tests/test_efi_capsule/signature.dts .'
50 % (data_dir, u_boot_config.source_dir), shell=True)
51 check_call('cd %s; '
52 'dtc -@ -I dts -O dtb -o signature.dtbo signature.dts; '
53 'fdtoverlay -i %s/arch/sandbox/dts/test.dtb '
54 '-o test_sig.dtb signature.dtbo'
55 % (data_dir, u_boot_config.build_dir), shell=True)
56
57 # Create *malicious* private key (SIGNER2.key) and certificate
58 # (SIGNER2.crt)
59 check_call('cd %s; '
60 'openssl req -x509 -sha256 -newkey rsa:2048 '
61 '-subj /CN=TEST_SIGNER/ -keyout SIGNER2.key '
62 '-out SIGNER2.crt -nodes -days 365'
63 % data_dir, shell=True)
64
AKASHI Takahiro0f626ce2020-11-30 18:12:16 +090065 # Create capsule files
66 # two regions: one for u-boot.bin and the other for u-boot.env
Sughosh Ganu2db313d2022-04-15 11:29:38 +053067 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 +090068 shell=True)
69 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' %
70 (u_boot_config.source_dir, data_dir),
71 shell=True)
72 check_call('cd %s; %s/tools/mkimage -f uboot_bin_env.its uboot_bin_env.itb' %
73 (data_dir, u_boot_config.build_dir),
74 shell=True)
Sughosh Ganu2db313d2022-04-15 11:29:38 +053075 check_call('cd %s; %s/tools/mkeficapsule --index 1 --guid 09D7CF52-0720-4710-91D1-08469B7FE9C8 u-boot.bin.new Test01' %
AKASHI Takahiro0f626ce2020-11-30 18:12:16 +090076 (data_dir, u_boot_config.build_dir),
77 shell=True)
Sughosh Ganu2db313d2022-04-15 11:29:38 +053078 check_call('cd %s; %s/tools/mkeficapsule --index 2 --guid 5A7021F5-FEF2-48B4-AABA-832E777418C0 u-boot.env.new Test02' %
AKASHI Takahiro30a7c612020-11-30 18:12:17 +090079 (data_dir, u_boot_config.build_dir),
80 shell=True)
Sughosh Ganu2db313d2022-04-15 11:29:38 +053081 check_call('cd %s; %s/tools/mkeficapsule --index 1 --guid 058B7D83-50D5-4C47-A195-60D86AD341C4 u-boot.bin.new Test03' %
AKASHI Takahiro20d91552022-02-09 19:10:41 +090082 (data_dir, u_boot_config.build_dir),
83 shell=True)
Sughosh Ganu2db313d2022-04-15 11:29:38 +053084 check_call('cd %s; %s/tools/mkeficapsule --index 1 --guid 3673B45D-6A7C-46F3-9E60-ADABB03F7937 uboot_bin_env.itb Test04' %
85 (data_dir, u_boot_config.build_dir),
86 shell=True)
87 check_call('cd %s; %s/tools/mkeficapsule --index 1 --guid 058B7D83-50D5-4C47-A195-60D86AD341C4 uboot_bin_env.itb Test05' %
88 (data_dir, u_boot_config.build_dir),
89 shell=True)
90
AKASHI Takahiro0bdde5f2022-02-09 19:10:38 +090091 if capsule_auth_enabled:
Vincent Stehlé6a4625e2022-05-31 09:55:34 +020092 # raw firmware signed with proper key
AKASHI Takahiro0bdde5f2022-02-09 19:10:38 +090093 check_call('cd %s; '
94 '%s/tools/mkeficapsule --index 1 --monotonic-count 1 '
95 '--private-key SIGNER.key --certificate SIGNER.crt '
Vincent Stehléf0c7daa2022-05-31 09:55:33 +020096 '--guid 09D7CF52-0720-4710-91D1-08469B7FE9C8 '
Sughosh Ganu2db313d2022-04-15 11:29:38 +053097 'u-boot.bin.new Test11'
AKASHI Takahiro0bdde5f2022-02-09 19:10:38 +090098 % (data_dir, u_boot_config.build_dir),
99 shell=True)
Vincent Stehlé6a4625e2022-05-31 09:55:34 +0200100 # raw firmware signed with *mal* key
AKASHI Takahiro0bdde5f2022-02-09 19:10:38 +0900101 check_call('cd %s; '
102 '%s/tools/mkeficapsule --index 1 --monotonic-count 1 '
103 '--private-key SIGNER2.key '
104 '--certificate SIGNER2.crt '
Vincent Stehléf0c7daa2022-05-31 09:55:33 +0200105 '--guid 09D7CF52-0720-4710-91D1-08469B7FE9C8 '
Sughosh Ganu2db313d2022-04-15 11:29:38 +0530106 'u-boot.bin.new Test12'
AKASHI Takahiro0bdde5f2022-02-09 19:10:38 +0900107 % (data_dir, u_boot_config.build_dir),
108 shell=True)
Vincent Stehlé6a4625e2022-05-31 09:55:34 +0200109 # FIT firmware signed with proper key
110 check_call('cd %s; '
111 '%s/tools/mkeficapsule --index 1 --monotonic-count 1 '
112 '--private-key SIGNER.key --certificate SIGNER.crt '
113 '--guid 3673B45D-6A7C-46F3-9E60-ADABB03F7937 '
114 'uboot_bin_env.itb Test13'
115 % (data_dir, u_boot_config.build_dir),
116 shell=True)
117 # FIT firmware signed with *mal* key
118 check_call('cd %s; '
119 '%s/tools/mkeficapsule --index 1 --monotonic-count 1 '
120 '--private-key SIGNER2.key '
121 '--certificate SIGNER2.crt '
122 '--guid 3673B45D-6A7C-46F3-9E60-ADABB03F7937 '
123 'uboot_bin_env.itb Test14'
124 % (data_dir, u_boot_config.build_dir),
125 shell=True)
AKASHI Takahiro0f626ce2020-11-30 18:12:16 +0900126
127 # Create a disk image with EFI system partition
128 check_call('virt-make-fs --partition=gpt --size=+1M --type=vfat %s %s' %
129 (mnt_point, image_path), shell=True)
130 check_call('sgdisk %s -A 1:set:0 -t 1:C12A7328-F81F-11D2-BA4B-00A0C93EC93B' %
131 image_path, shell=True)
132
133 except CalledProcessError as exception:
134 pytest.skip('Setup failed: %s' % exception.cmd)
135 return
136 else:
137 yield image_path
138 finally:
139 call('rm -rf %s' % mnt_point, shell=True)
140 call('rm -f %s' % image_path, shell=True)
141 call('rm -f ./spi.bin', shell=True)