summaryrefslogtreecommitdiffstats
path: root/OvmfPkg/PlatformPei/PlatformId.c
blob: afa2f811d9c2ad367aa55c01e8c3bbe74373f6c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/**@file
  PlatformId Event HOB creation

  Copyright (c) 2024, Google LLC. All rights reserved.<BR>

  SPDX-License-Identifier: BSD-2-Clause-Patent

**/

#include <Base.h>
#include <Guid/TcgEventHob.h>
#include <IndustryStandard/UefiTcgPlatform.h>
#include <Library/BaseMemoryLib.h>
#include <Library/BaseLib.h>
#include <Library/DebugLib.h>
#include <Library/HobLib.h>
#include <Library/PeiServicesLib.h>
#include <Library/PrintLib.h>
#include <Library/QemuFwCfgLib.h>

#define DPREFIX  "sp800155evts: "

/**
 * Creates an EFI_HOB_TYPE_GUID_EXTENSION HOB for a given SP800155 event.
 * Associates the string data with gTcg800155PlatformIdEventHobGuid. Any
 * unused bytes or out-of-bounds event sizes are considered corrupted and
 * are discarded.
**/
STATIC
VOID
PlatformIdRegisterSp800155 (
  IN CONST EFI_PEI_SERVICES  **PeiServices,
  IN       UINT8             *Evt,
  IN       UINTN             EvtSize
  )
{
  EFI_STATUS         Status;
  VOID               *Hob;
  EFI_HOB_GUID_TYPE  *GuidHob;
  UINT8              *EvtDest;

  Status = (*PeiServices)->CreateHob (
                             PeiServices,
                             EFI_HOB_TYPE_GUID_EXTENSION,
                             sizeof (EFI_HOB_GUID_TYPE) + (UINT16)EvtSize,
                             &Hob
                             );
  if (EFI_ERROR (Status)) {
    DEBUG ((DEBUG_ERROR, DPREFIX "GUID HOB creation failed, skipping\n"));
    return;
  }

  GuidHob = (EFI_HOB_GUID_TYPE *)Hob;
  CopyGuid (&GuidHob->Name, &gTcg800155PlatformIdEventHobGuid);
  EvtDest = (UINT8 *)GET_GUID_HOB_DATA (Hob);
  CopyMem (EvtDest, Evt, EvtSize);
  // Fill the remaining HOB padding bytes with 0s.
  SetMem (EvtDest + EvtSize, GET_GUID_HOB_DATA_SIZE (Hob) - EvtSize, 0);
}

/**
 * Reads the given path from the fw_cfg file and registers it as an
 * EFI_HOB_GUID_EXTENSION HOB with gTcg800155PlatformIdEventHobGuid.
 * Returns FALSE iff the file does not exist.
**/
BOOLEAN
PlatformIdRegisterEvent (
  IN CONST EFI_PEI_SERVICES  **PeiServices,
  IN CONST CHAR8             *Path
  )
{
  EFI_STATUS            Status;
  UINTN                 NumPages;
  EFI_PHYSICAL_ADDRESS  Pages;
  FIRMWARE_CONFIG_ITEM  FdtItem;
  UINTN                 FdtSize;
  UINT8                 *Evt;

  Status = QemuFwCfgFindFile (Path, &FdtItem, &FdtSize);
  if (EFI_ERROR (Status)) {
    return FALSE;
  }

  if (FdtSize > MAX_UINT16 - sizeof (EFI_HOB_GUID_TYPE)) {
    DEBUG ((DEBUG_ERROR, DPREFIX "Eventdata too large for HOB, skipping\n"));
    return TRUE;
  }

  NumPages = EFI_SIZE_TO_PAGES (FdtSize);
  Status   = (*PeiServices)->AllocatePages (
                               PeiServices,
                               EfiBootServicesData,
                               NumPages,
                               &Pages
                               );
  if (EFI_ERROR (Status)) {
    return TRUE;
  }

  Evt = (UINT8 *)(UINTN)Pages;
  QemuFwCfgSelectItem (FdtItem);
  QemuFwCfgReadBytes (FdtSize, Evt);
  PlatformIdRegisterSp800155 (PeiServices, Evt, FdtSize);

  Status = (*PeiServices)->FreePages (PeiServices, Pages, NumPages);
  ASSERT_EFI_ERROR (Status);
  return TRUE;
}

VOID
PlatformIdInitialization (
  IN CONST EFI_PEI_SERVICES  **PeiServices
  )
{
  UINTN  Index;
  CHAR8  Path[64];

  for (Index = 0; ; Index++) {
    AsciiSPrint (Path, sizeof (Path), "opt/org.tianocode/sp800155evt/%d", Index);
    if (!PlatformIdRegisterEvent (PeiServices, Path)) {
      break;
    }
  }
}