blob: 4879f2b5c24c06d3910694d4241af5b925f92571 [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
5import os
6import os.path
7import re
8from subprocess import call, check_call, check_output, CalledProcessError
9import pytest
10from capsule_defs import *
11
12#
AKASHI Takahiro0bdde5f2022-02-09 19:10:38 +090013# Fixture for UEFI capsule test
AKASHI Takahiro0f626ce2020-11-30 18:12:16 +090014#
15
AKASHI Takahiro0f626ce2020-11-30 18:12:16 +090016@pytest.fixture(scope='session')
17def efi_capsule_data(request, u_boot_config):
AKASHI Takahiro0bdde5f2022-02-09 19:10:38 +090018 """Set up a file system to be used in UEFI capsule and
19 authentication test.
AKASHI Takahiro0f626ce2020-11-30 18:12:16 +090020
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
AKASHI Takahiro0bdde5f2022-02-09 19:10:38 +090043 capsule_auth_enabled = u_boot_config.buildconfig.get(
44 'config_efi_capsule_authenticate')
45 if capsule_auth_enabled:
46 # Create private key (SIGNER.key) and certificate (SIGNER.crt)
47 check_call('cd %s; '
48 'openssl req -x509 -sha256 -newkey rsa:2048 '
49 '-subj /CN=TEST_SIGNER/ -keyout SIGNER.key '
50 '-out SIGNER.crt -nodes -days 365'
51 % data_dir, shell=True)
52 check_call('cd %s; %scert-to-efi-sig-list SIGNER.crt SIGNER.esl'
53 % (data_dir, EFITOOLS_PATH), shell=True)
54
55 # Update dtb adding capsule certificate
56 check_call('cd %s; '
57 'cp %s/test/py/tests/test_efi_capsule/signature.dts .'
58 % (data_dir, u_boot_config.source_dir), shell=True)
59 check_call('cd %s; '
60 'dtc -@ -I dts -O dtb -o signature.dtbo signature.dts; '
61 'fdtoverlay -i %s/arch/sandbox/dts/test.dtb '
62 '-o test_sig.dtb signature.dtbo'
63 % (data_dir, u_boot_config.build_dir), shell=True)
64
65 # Create *malicious* private key (SIGNER2.key) and certificate
66 # (SIGNER2.crt)
67 check_call('cd %s; '
68 'openssl req -x509 -sha256 -newkey rsa:2048 '
69 '-subj /CN=TEST_SIGNER/ -keyout SIGNER2.key '
70 '-out SIGNER2.crt -nodes -days 365'
71 % data_dir, shell=True)
72
AKASHI Takahiro0f626ce2020-11-30 18:12:16 +090073 # Create capsule files
74 # two regions: one for u-boot.bin and the other for u-boot.env
Sughosh Ganu2db313d2022-04-15 11:29:38 +053075 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 +090076 shell=True)
77 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' %
78 (u_boot_config.source_dir, data_dir),
79 shell=True)
80 check_call('cd %s; %s/tools/mkimage -f uboot_bin_env.its uboot_bin_env.itb' %
81 (data_dir, u_boot_config.build_dir),
82 shell=True)
Sughosh Ganu2db313d2022-04-15 11:29:38 +053083 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 +090084 (data_dir, u_boot_config.build_dir),
85 shell=True)
Sughosh Ganu2db313d2022-04-15 11:29:38 +053086 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 +090087 (data_dir, u_boot_config.build_dir),
88 shell=True)
Sughosh Ganu2db313d2022-04-15 11:29:38 +053089 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 +090090 (data_dir, u_boot_config.build_dir),
91 shell=True)
Sughosh Ganu2db313d2022-04-15 11:29:38 +053092 check_call('cd %s; %s/tools/mkeficapsule --index 1 --guid 3673B45D-6A7C-46F3-9E60-ADABB03F7937 uboot_bin_env.itb Test04' %
93 (data_dir, u_boot_config.build_dir),
94 shell=True)
95 check_call('cd %s; %s/tools/mkeficapsule --index 1 --guid 058B7D83-50D5-4C47-A195-60D86AD341C4 uboot_bin_env.itb Test05' %
96 (data_dir, u_boot_config.build_dir),
97 shell=True)
98
AKASHI Takahiro0bdde5f2022-02-09 19:10:38 +090099 if capsule_auth_enabled:
Vincent Stehlé6a4625e2022-05-31 09:55:34 +0200100 # raw firmware signed with proper 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 SIGNER.key --certificate SIGNER.crt '
Vincent Stehléf0c7daa2022-05-31 09:55:33 +0200104 '--guid 09D7CF52-0720-4710-91D1-08469B7FE9C8 '
Sughosh Ganu2db313d2022-04-15 11:29:38 +0530105 'u-boot.bin.new Test11'
AKASHI Takahiro0bdde5f2022-02-09 19:10:38 +0900106 % (data_dir, u_boot_config.build_dir),
107 shell=True)
Vincent Stehlé6a4625e2022-05-31 09:55:34 +0200108 # raw firmware signed with *mal* key
AKASHI Takahiro0bdde5f2022-02-09 19:10:38 +0900109 check_call('cd %s; '
110 '%s/tools/mkeficapsule --index 1 --monotonic-count 1 '
111 '--private-key SIGNER2.key '
112 '--certificate SIGNER2.crt '
Vincent Stehléf0c7daa2022-05-31 09:55:33 +0200113 '--guid 09D7CF52-0720-4710-91D1-08469B7FE9C8 '
Sughosh Ganu2db313d2022-04-15 11:29:38 +0530114 'u-boot.bin.new Test12'
AKASHI Takahiro0bdde5f2022-02-09 19:10:38 +0900115 % (data_dir, u_boot_config.build_dir),
116 shell=True)
Vincent Stehlé6a4625e2022-05-31 09:55:34 +0200117 # FIT firmware signed with proper key
118 check_call('cd %s; '
119 '%s/tools/mkeficapsule --index 1 --monotonic-count 1 '
120 '--private-key SIGNER.key --certificate SIGNER.crt '
121 '--guid 3673B45D-6A7C-46F3-9E60-ADABB03F7937 '
122 'uboot_bin_env.itb Test13'
123 % (data_dir, u_boot_config.build_dir),
124 shell=True)
125 # FIT firmware signed with *mal* key
126 check_call('cd %s; '
127 '%s/tools/mkeficapsule --index 1 --monotonic-count 1 '
128 '--private-key SIGNER2.key '
129 '--certificate SIGNER2.crt '
130 '--guid 3673B45D-6A7C-46F3-9E60-ADABB03F7937 '
131 'uboot_bin_env.itb Test14'
132 % (data_dir, u_boot_config.build_dir),
133 shell=True)
AKASHI Takahiro0f626ce2020-11-30 18:12:16 +0900134
135 # Create a disk image with EFI system partition
136 check_call('virt-make-fs --partition=gpt --size=+1M --type=vfat %s %s' %
137 (mnt_point, image_path), shell=True)
138 check_call('sgdisk %s -A 1:set:0 -t 1:C12A7328-F81F-11D2-BA4B-00A0C93EC93B' %
139 image_path, shell=True)
140
141 except CalledProcessError as exception:
142 pytest.skip('Setup failed: %s' % exception.cmd)
143 return
144 else:
145 yield image_path
146 finally:
147 call('rm -rf %s' % mnt_point, shell=True)
148 call('rm -f %s' % image_path, shell=True)
149 call('rm -f ./spi.bin', shell=True)