1 /** @file
2 
3   Parts of the SMM/MM implementation that are specific to traditional MM
4 
5 Copyright (c) 2011 - 2018, Intel Corporation. All rights reserved. <BR>
6 Copyright (c) 2018, Linaro, Ltd. All rights reserved. <BR>
7 SPDX-License-Identifier: BSD-2-Clause-Patent
8 
9 **/
10 
11 #include <Library/UefiBootServicesTableLib.h>
12 #include <Library/SmmMemLib.h>
13 #include "Variable.h"
14 
15 /**
16   This function checks if the buffer is valid per processor architecture and
17   does not overlap with SMRAM.
18 
19   @param Buffer The buffer start address to be checked.
20   @param Length The buffer length to be checked.
21 
22   @retval TRUE  This buffer is valid per processor architecture and does not
23                 overlap with SMRAM.
24   @retval FALSE This buffer is not valid per processor architecture or overlaps
25                 with SMRAM.
26 **/
27 BOOLEAN
VariableSmmIsBufferOutsideSmmValid(IN EFI_PHYSICAL_ADDRESS Buffer,IN UINT64 Length)28 VariableSmmIsBufferOutsideSmmValid (
29   IN EFI_PHYSICAL_ADDRESS  Buffer,
30   IN UINT64                Length
31   )
32 {
33   return SmmIsBufferOutsideSmmValid (Buffer, Length);
34 }
35 
36 /**
37   Notify the system that the SMM variable driver is ready.
38 **/
39 VOID
VariableNotifySmmReady(VOID)40 VariableNotifySmmReady (
41   VOID
42   )
43 {
44   EFI_STATUS            Status;
45   EFI_HANDLE            Handle;
46 
47   Handle = NULL;
48   Status = gBS->InstallProtocolInterface (
49                   &Handle,
50                   &gEfiSmmVariableProtocolGuid,
51                   EFI_NATIVE_INTERFACE,
52                   NULL
53                   );
54   ASSERT_EFI_ERROR (Status);
55 }
56 
57 /**
58   Notify the system that the SMM variable write driver is ready.
59 **/
60 VOID
VariableNotifySmmWriteReady(VOID)61 VariableNotifySmmWriteReady (
62   VOID
63   )
64 {
65   EFI_STATUS            Status;
66   EFI_HANDLE            Handle;
67 
68   Handle = NULL;
69   Status = gBS->InstallProtocolInterface (
70                   &Handle,
71                   &gSmmVariableWriteGuid,
72                   EFI_NATIVE_INTERFACE,
73                   NULL
74                   );
75   ASSERT_EFI_ERROR (Status);
76 }
77 
78 /**
79   Variable service MM driver entry point
80 
81   @param[in] ImageHandle    A handle for the image that is initializing this
82                             driver
83   @param[in] SystemTable    A pointer to the EFI system table
84 
85   @retval EFI_SUCCESS       Variable service successfully initialized.
86 **/
87 EFI_STATUS
88 EFIAPI
VariableServiceInitialize(IN EFI_HANDLE ImageHandle,IN EFI_SYSTEM_TABLE * SystemTable)89 VariableServiceInitialize (
90   IN EFI_HANDLE                           ImageHandle,
91   IN EFI_SYSTEM_TABLE                     *SystemTable
92   )
93 {
94   return MmVariableServiceInitialize ();
95 }
96 
97 /**
98   Whether the TCG or TCG2 protocols are installed in the UEFI protocol database.
99   This information is used by the MorLock code to infer whether an existing
100   MOR variable is legitimate or not.
101 
102   @retval TRUE  Either the TCG or TCG2 protocol is installed in the UEFI
103                 protocol database
104   @retval FALSE Neither the TCG nor the TCG2 protocol is installed in the UEFI
105                 protocol database
106 **/
107 BOOLEAN
VariableHaveTcgProtocols(VOID)108 VariableHaveTcgProtocols (
109   VOID
110   )
111 {
112   EFI_STATUS            Status;
113   VOID                  *Interface;
114 
115   Status = gBS->LocateProtocol (
116                   &gEfiTcg2ProtocolGuid,
117                   NULL,                     // Registration
118                   &Interface
119                   );
120   if (!EFI_ERROR (Status)) {
121     return TRUE;
122   }
123 
124   Status = gBS->LocateProtocol (
125                   &gEfiTcgProtocolGuid,
126                   NULL,                     // Registration
127                   &Interface
128                   );
129   return !EFI_ERROR (Status);
130 }
131