Skip to content

Commit f87d041

Browse files
apalosetienne-lms
authored andcommitted
Drivers/OpTeeRpmb: Add an OP-TEE backed RPMB driver
A following patch is adding support for building StMM in order to run it from OP-TEE. OP-TEE in combination with a NS-world supplicant can use the RPMB partition of an eMMC to store EFI variables. The supplicant functionality is currently available in U-Boot only but can be ported into EDK2. Assuming similar functionality is added in EDK2, this will allow any hardware with an RPMB partition to store EFI variables securely. So let's add a driver that enables access of the RPMB partition through OP-TEE. Since the upper layers expect a byte addressable interface, the driver allocates memory and patches the PCDs, while syncing the memory/hardware on read/write callbacks. Signed-off-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>
1 parent db922e1 commit f87d041

File tree

5 files changed

+1162
-0
lines changed

5 files changed

+1162
-0
lines changed

Drivers/OpTeeRpmb/FixupPcd.c

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/** @file
2+
3+
Update the patched PCDs to their correct value
4+
5+
Copyright (c) 2020, Linaro Ltd. All rights reserved.<BR>
6+
7+
SPDX-License-Identifier: BSD-2-Clause-Patent
8+
9+
**/
10+
11+
/**
12+
* Patch the relevant PCDs of the RPMB driver with the correct address of the
13+
* allocated memory
14+
*
15+
**/
16+
#include <Library/BaseLib.h>
17+
#include <Library/DebugLib.h>
18+
#include <Library/MmServicesTableLib.h>
19+
#include <Library/PcdLib.h>
20+
21+
#include <Protocol/FirmwareVolumeBlock.h>
22+
#include <Protocol/SmmFirmwareVolumeBlock.h>
23+
24+
#include "OpTeeRpmbFvb.h"
25+
26+
/**
27+
Fixup the Pcd values for variable storage
28+
29+
Since the upper layers of EDK2 expect a memory mapped interface and we can't
30+
offer that from an RPMB, the driver allocates memory on init and passes that
31+
on the upper layers. Since the memory is dynamically allocated and we can't set the
32+
PCD is StMM context, we need to patch it correctly on each access
33+
34+
@retval EFI_SUCCESS Protocol was found and PCDs patched up
35+
36+
**/
37+
EFI_STATUS
38+
EFIAPI
39+
FixPcdMemory (
40+
VOID
41+
)
42+
{
43+
EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvbProtocol;
44+
MEM_INSTANCE *Instance;
45+
EFI_STATUS Status;
46+
47+
//
48+
// Locate SmmFirmwareVolumeBlockProtocol
49+
//
50+
Status = gMmst->MmLocateProtocol (
51+
&gEfiSmmFirmwareVolumeBlockProtocolGuid,
52+
NULL,
53+
(VOID **) &FvbProtocol
54+
);
55+
ASSERT_EFI_ERROR (Status);
56+
57+
Instance = INSTANCE_FROM_FVB_THIS (FvbProtocol);
58+
// The Pcd is user defined, so make sure we don't overflow
59+
if (Instance->MemBaseAddress > MAX_UINT64 - PcdGet32 (PcdFlashNvStorageVariableSize)) {
60+
return EFI_INVALID_PARAMETER;
61+
}
62+
63+
if (Instance->MemBaseAddress > MAX_UINT64 - PcdGet32 (PcdFlashNvStorageVariableSize) -
64+
PcdGet32 (PcdFlashNvStorageFtwWorkingSize)) {
65+
return EFI_INVALID_PARAMETER;
66+
}
67+
68+
// Patch PCDs with the the correct values
69+
PatchPcdSet64 (PcdFlashNvStorageVariableBase64, Instance->MemBaseAddress);
70+
PatchPcdSet64 (
71+
PcdFlashNvStorageFtwWorkingBase64,
72+
Instance->MemBaseAddress + PcdGet32 (PcdFlashNvStorageVariableSize)
73+
);
74+
PatchPcdSet64 (
75+
PcdFlashNvStorageFtwSpareBase64,
76+
Instance->MemBaseAddress +
77+
PcdGet32 (PcdFlashNvStorageVariableSize) +
78+
PcdGet32 (PcdFlashNvStorageFtwWorkingSize)
79+
);
80+
81+
DEBUG ((DEBUG_INFO, "%a: Fixup PcdFlashNvStorageVariableBase64: 0x%lx\n",
82+
__FUNCTION__, PcdGet64 (PcdFlashNvStorageVariableBase64)));
83+
DEBUG ((DEBUG_INFO, "%a: Fixup PcdFlashNvStorageFtwWorkingBase64: 0x%lx\n",
84+
__FUNCTION__, PcdGet64 (PcdFlashNvStorageFtwWorkingBase64)));
85+
DEBUG ((DEBUG_INFO, "%a: Fixup PcdFlashNvStorageFtwSpareBase64: 0x%lx\n",
86+
__FUNCTION__, PcdGet64 (PcdFlashNvStorageFtwSpareBase64)));
87+
88+
return Status;
89+
}

Drivers/OpTeeRpmb/FixupPcd.inf

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
## @file
2+
# Instance of Base Memory Library without assembly.
3+
#
4+
# Copyright (c) 2020, Linaro Ltd. All rights reserved.<BR>
5+
#
6+
# SPDX-License-Identifier: BSD-2-Clause-Patent
7+
#
8+
#
9+
##
10+
11+
[Defines]
12+
INF_VERSION = 0x0001001A
13+
BASE_NAME = FixupPcd
14+
FILE_GUID = a827c337-a9c6-301b-aeb7-acbc95d8da22
15+
MODULE_TYPE = BASE
16+
VERSION_STRING = 0.1
17+
LIBRARY_CLASS = RpmbPcdFixup|MM_STANDALONE
18+
CONSTRUCTOR = FixPcdMemory
19+
20+
[Sources]
21+
FixupPcd.c
22+
OpTeeRpmbFvb.h
23+
24+
[Packages]
25+
MdeModulePkg/MdeModulePkg.dec
26+
MdePkg/MdePkg.dec
27+
28+
[LibraryClasses]
29+
BaseLib
30+
DebugLib
31+
MmServicesTableLib
32+
PcdLib
33+
34+
[Pcd]
35+
gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareBase64
36+
gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareSize
37+
gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingBase64
38+
gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingSize
39+
gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableBase64
40+
gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableSize
41+
42+
[Protocols]
43+
gEfiSmmFirmwareVolumeBlockProtocolGuid ## CONSUMES

Drivers/OpTeeRpmb/OpTeeRpmbFv.inf

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
## @file
2+
#
3+
# Component description file for OpTeeRpmbFv module
4+
#
5+
# Copyright (c) 2020, Linaro Ltd. All rights reserved.<BR>
6+
#
7+
# SPDX-License-Identifier: BSD-2-Clause-Patent
8+
#
9+
##
10+
11+
[Defines]
12+
INF_VERSION = 0x0001001A
13+
BASE_NAME = OpTeeRpmbFv
14+
FILE_GUID = 4803FC20-E583-3BCD-8C60-141E85C9A2CF
15+
MODULE_TYPE = MM_STANDALONE
16+
VERSION_STRING = 0.1
17+
PI_SPECIFICATION_VERSION = 0x00010032
18+
ENTRY_POINT = OpTeeRpmbFvbInit
19+
20+
[Sources]
21+
OpTeeRpmbFvb.c
22+
OpTeeRpmbFvb.h
23+
24+
[Packages]
25+
ArmPkg/ArmPkg.dec
26+
ArmPlatformPkg/ArmPlatformPkg.dec
27+
MdeModulePkg/MdeModulePkg.dec
28+
MdePkg/MdePkg.dec
29+
StandaloneMmPkg/StandaloneMmPkg.dec
30+
31+
[LibraryClasses]
32+
ArmSvcLib
33+
BaseLib
34+
BaseMemoryLib
35+
DebugLib
36+
MemoryAllocationLib
37+
MmServicesTableLib
38+
PcdLib
39+
StandaloneMmDriverEntryPoint
40+
41+
[Guids]
42+
gEfiAuthenticatedVariableGuid
43+
gEfiSystemNvDataFvGuid
44+
gEfiVariableGuid
45+
46+
[Pcd]
47+
gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareBase64
48+
gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareSize
49+
gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingBase64
50+
gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingSize
51+
gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableBase64
52+
gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableSize
53+
54+
[Protocols]
55+
gEfiSmmFirmwareVolumeBlockProtocolGuid ## PRODUCES
56+
57+
[Depex]
58+
TRUE

0 commit comments

Comments
 (0)