2014-09-18 19:58:01 +02:00
|
|
|
/** @file
|
|
|
|
*
|
2014-11-11 01:43:03 +01:00
|
|
|
* Copyright (c) 2011-2014, ARM Limited. All rights reserved.
|
2020-02-26 20:05:08 +01:00
|
|
|
* Copyright (c) 2014-2020, Linaro Limited. All rights reserved.
|
2014-09-18 19:58:01 +02:00
|
|
|
*
|
2019-04-04 01:03:23 +02:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause-Patent
|
2014-09-18 19:58:01 +02:00
|
|
|
*
|
|
|
|
**/
|
|
|
|
|
|
|
|
#include <PiPei.h>
|
|
|
|
|
|
|
|
#include <Library/MemoryAllocationLib.h>
|
|
|
|
#include <Library/DebugLib.h>
|
|
|
|
#include <Library/HobLib.h>
|
|
|
|
#include <Library/PcdLib.h>
|
2020-02-26 20:05:08 +01:00
|
|
|
#include <Library/PeiServicesLib.h>
|
2014-09-18 19:58:01 +02:00
|
|
|
#include <libfdt.h>
|
|
|
|
|
2015-02-28 21:25:48 +01:00
|
|
|
#include <Guid/EarlyPL011BaseAddress.h>
|
2015-02-28 21:26:00 +01:00
|
|
|
#include <Guid/FdtHob.h>
|
2015-02-28 21:25:48 +01:00
|
|
|
|
2020-02-26 20:05:08 +01:00
|
|
|
STATIC CONST EFI_PEI_PPI_DESCRIPTOR mTpm2DiscoveredPpi = {
|
|
|
|
EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST,
|
|
|
|
&gOvmfTpmDiscoveredPpiGuid,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
STATIC CONST EFI_PEI_PPI_DESCRIPTOR mTpm2InitializationDonePpi = {
|
|
|
|
EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST,
|
|
|
|
&gPeiTpmInitializationDonePpiGuid,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2014-09-18 19:58:01 +02:00
|
|
|
EFI_STATUS
|
|
|
|
EFIAPI
|
|
|
|
PlatformPeim (
|
|
|
|
VOID
|
|
|
|
)
|
|
|
|
{
|
|
|
|
VOID *Base;
|
|
|
|
VOID *NewBase;
|
|
|
|
UINTN FdtSize;
|
2015-02-28 21:26:10 +01:00
|
|
|
UINTN FdtPages;
|
2015-02-28 21:26:00 +01:00
|
|
|
UINT64 *FdtHobData;
|
2015-02-28 21:25:48 +01:00
|
|
|
UINT64 *UartHobData;
|
|
|
|
INT32 Node, Prev;
|
2020-02-26 20:05:08 +01:00
|
|
|
INT32 Parent, Depth;
|
2015-02-28 21:25:48 +01:00
|
|
|
CONST CHAR8 *Compatible;
|
|
|
|
CONST CHAR8 *CompItem;
|
2017-04-04 15:20:12 +02:00
|
|
|
CONST CHAR8 *NodeStatus;
|
2015-02-28 21:25:48 +01:00
|
|
|
INT32 Len;
|
2020-02-26 20:05:08 +01:00
|
|
|
INT32 RangesLen;
|
2017-04-04 15:20:12 +02:00
|
|
|
INT32 StatusLen;
|
2015-02-28 21:25:48 +01:00
|
|
|
CONST UINT64 *RegProp;
|
2020-02-26 20:05:08 +01:00
|
|
|
CONST UINT32 *RangesProp;
|
2015-02-28 21:25:48 +01:00
|
|
|
UINT64 UartBase;
|
2020-02-26 20:05:08 +01:00
|
|
|
UINT64 TpmBase;
|
|
|
|
EFI_STATUS Status;
|
2014-09-18 19:58:01 +02:00
|
|
|
|
2015-02-28 21:26:00 +01:00
|
|
|
Base = (VOID*)(UINTN)PcdGet64 (PcdDeviceTreeInitialBaseAddress);
|
|
|
|
ASSERT (Base != NULL);
|
2014-09-18 19:58:01 +02:00
|
|
|
ASSERT (fdt_check_header (Base) == 0);
|
|
|
|
|
2015-02-28 21:26:10 +01:00
|
|
|
FdtSize = fdt_totalsize (Base) + PcdGet32 (PcdDeviceTreeAllocationPadding);
|
|
|
|
FdtPages = EFI_SIZE_TO_PAGES (FdtSize);
|
|
|
|
NewBase = AllocatePages (FdtPages);
|
2014-09-18 19:58:01 +02:00
|
|
|
ASSERT (NewBase != NULL);
|
2015-02-28 21:26:10 +01:00
|
|
|
fdt_open_into (Base, NewBase, EFI_PAGES_TO_SIZE (FdtPages));
|
2015-02-28 21:26:00 +01:00
|
|
|
|
|
|
|
FdtHobData = BuildGuidHob (&gFdtHobGuid, sizeof *FdtHobData);
|
|
|
|
ASSERT (FdtHobData != NULL);
|
|
|
|
*FdtHobData = (UINTN)NewBase;
|
2014-09-18 19:58:01 +02:00
|
|
|
|
2015-02-28 21:25:48 +01:00
|
|
|
UartHobData = BuildGuidHob (&gEarlyPL011BaseAddressGuid, sizeof *UartHobData);
|
|
|
|
ASSERT (UartHobData != NULL);
|
|
|
|
*UartHobData = 0;
|
|
|
|
|
2020-02-26 20:05:08 +01:00
|
|
|
TpmBase = 0;
|
|
|
|
|
2020-03-25 10:30:07 +01:00
|
|
|
//
|
|
|
|
// Set Parent to suppress incorrect compiler/analyzer warnings.
|
|
|
|
//
|
|
|
|
Parent = 0;
|
|
|
|
|
2020-02-26 20:05:08 +01:00
|
|
|
for (Prev = Depth = 0;; Prev = Node) {
|
|
|
|
Node = fdt_next_node (Base, Prev, &Depth);
|
2015-02-28 21:25:48 +01:00
|
|
|
if (Node < 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-02-26 20:05:08 +01:00
|
|
|
if (Depth == 1) {
|
|
|
|
Parent = Node;
|
|
|
|
}
|
|
|
|
|
2015-02-28 21:25:48 +01:00
|
|
|
Compatible = fdt_getprop (Base, Node, "compatible", &Len);
|
|
|
|
|
|
|
|
//
|
|
|
|
// Iterate over the NULL-separated items in the compatible string
|
|
|
|
//
|
|
|
|
for (CompItem = Compatible; CompItem != NULL && CompItem < Compatible + Len;
|
|
|
|
CompItem += 1 + AsciiStrLen (CompItem)) {
|
|
|
|
|
|
|
|
if (AsciiStrCmp (CompItem, "arm,pl011") == 0) {
|
2017-04-04 15:20:12 +02:00
|
|
|
NodeStatus = fdt_getprop (Base, Node, "status", &StatusLen);
|
|
|
|
if (NodeStatus != NULL && AsciiStrCmp (NodeStatus, "okay") != 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-02-28 21:25:48 +01:00
|
|
|
RegProp = fdt_getprop (Base, Node, "reg", &Len);
|
|
|
|
ASSERT (Len == 16);
|
|
|
|
|
|
|
|
UartBase = fdt64_to_cpu (ReadUnaligned64 (RegProp));
|
|
|
|
|
|
|
|
DEBUG ((EFI_D_INFO, "%a: PL011 UART @ 0x%lx\n", __FUNCTION__, UartBase));
|
|
|
|
|
|
|
|
*UartHobData = UartBase;
|
|
|
|
break;
|
2020-02-26 20:05:08 +01:00
|
|
|
} else if (FeaturePcdGet (PcdTpm2SupportEnabled) &&
|
|
|
|
AsciiStrCmp (CompItem, "tcg,tpm-tis-mmio") == 0) {
|
|
|
|
|
|
|
|
RegProp = fdt_getprop (Base, Node, "reg", &Len);
|
|
|
|
ASSERT (Len == 8 || Len == 16);
|
|
|
|
if (Len == 8) {
|
|
|
|
TpmBase = fdt32_to_cpu (RegProp[0]);
|
|
|
|
} else if (Len == 16) {
|
|
|
|
TpmBase = fdt64_to_cpu (ReadUnaligned64 ((UINT64 *)RegProp));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Depth > 1) {
|
|
|
|
//
|
|
|
|
// QEMU/mach-virt may put the TPM on the platform bus, in which case
|
|
|
|
// we have to take its 'ranges' property into account to translate the
|
|
|
|
// MMIO address. This consists of a <child base, parent base, size>
|
|
|
|
// tuple, where the child base and the size use the same number of
|
|
|
|
// cells as the 'reg' property above, and the parent base uses 2 cells
|
|
|
|
//
|
|
|
|
RangesProp = fdt_getprop (Base, Parent, "ranges", &RangesLen);
|
|
|
|
ASSERT (RangesProp != NULL);
|
|
|
|
|
|
|
|
//
|
|
|
|
// a plain 'ranges' attribute without a value implies a 1:1 mapping
|
|
|
|
//
|
|
|
|
if (RangesLen != 0) {
|
|
|
|
//
|
|
|
|
// assume a single translated range with 2 cells for the parent base
|
|
|
|
//
|
|
|
|
if (RangesLen != Len + 2 * sizeof (UINT32)) {
|
|
|
|
DEBUG ((DEBUG_WARN,
|
|
|
|
"%a: 'ranges' property has unexpected size %d\n",
|
|
|
|
__FUNCTION__, RangesLen));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Len == 8) {
|
|
|
|
TpmBase -= fdt32_to_cpu (RangesProp[0]);
|
|
|
|
} else {
|
|
|
|
TpmBase -= fdt64_to_cpu (ReadUnaligned64 ((UINT64 *)RangesProp));
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// advance RangesProp to the parent bus address
|
|
|
|
//
|
|
|
|
RangesProp = (UINT32 *)((UINT8 *)RangesProp + Len / 2);
|
|
|
|
TpmBase += fdt64_to_cpu (ReadUnaligned64 ((UINT64 *)RangesProp));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2015-02-28 21:25:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-26 20:05:08 +01:00
|
|
|
if (FeaturePcdGet (PcdTpm2SupportEnabled)) {
|
|
|
|
if (TpmBase != 0) {
|
|
|
|
DEBUG ((DEBUG_INFO, "%a: TPM @ 0x%lx\n", __FUNCTION__, TpmBase));
|
|
|
|
|
|
|
|
Status = (EFI_STATUS)PcdSet64S (PcdTpmBaseAddress, TpmBase);
|
|
|
|
ASSERT_EFI_ERROR (Status);
|
|
|
|
|
|
|
|
Status = PeiServicesInstallPpi (&mTpm2DiscoveredPpi);
|
|
|
|
} else {
|
|
|
|
Status = PeiServicesInstallPpi (&mTpm2InitializationDonePpi);
|
|
|
|
}
|
|
|
|
ASSERT_EFI_ERROR (Status);
|
|
|
|
}
|
|
|
|
|
2014-11-11 01:43:03 +01:00
|
|
|
BuildFvHob (PcdGet64 (PcdFvBaseAddress), PcdGet32 (PcdFvSize));
|
2014-09-18 19:58:01 +02:00
|
|
|
|
|
|
|
return EFI_SUCCESS;
|
|
|
|
}
|