]> git.proxmox.com Git - mirror_edk2.git/blob - Vlv2TbltDevicePkg/SmramSaveInfoHandlerSmm/SmramSaveInfoHandlerSmm.c
Upload BSD-licensed Vlv2TbltDevicePkg and Vlv2DeviceRefCodePkg to
[mirror_edk2.git] / Vlv2TbltDevicePkg / SmramSaveInfoHandlerSmm / SmramSaveInfoHandlerSmm.c
1 /** @file
2 A helper driver to save information to SMRAM after SMRR is enabled.
3
4 This driver is for ECP platforms.
5
6 Copyright (c) 2010 - 2014, Intel Corporation. All rights reserved.<BR>
7
8 This program and the accompanying materials are licensed and made available under
9 the terms and conditions of the BSD License that accompanies this distribution.
10 The full text of the license may be found at
11 http://opensource.org/licenses/bsd-license.php.
12
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15
16
17 **/
18
19 #include <PiSmm.h>
20 #include <Library/DebugLib.h>
21 #include <Library/UefiDriverEntryPoint.h>
22 #include <Library/UefiRuntimeServicesTableLib.h>
23 #include <Library/SmmServicesTableLib.h>
24 #include <Library/BaseLib.h>
25 #include <Library/BaseMemoryLib.h>
26 #include <Library/IoLib.h>
27 #include <Protocol/SmmSwDispatch.h>
28 #include <Protocol/SmmReadyToLock.h>
29 #include <Protocol/SmmControl.h>
30
31 #define SMM_FROM_SMBASE_DRIVER 0x55
32 #define SMM_FROM_CPU_DRIVER_SAVE_INFO 0x81
33
34 #define EFI_SMRAM_CPU_NVS_HEADER_GUID \
35 { \
36 0x429501d9, 0xe447, 0x40f4, 0x86, 0x7b, 0x75, 0xc9, 0x3a, 0x1d, 0xb5, 0x4e \
37 }
38
39 UINT8 mSmiDataRegister;
40 BOOLEAN mLocked = FALSE;
41 EFI_GUID mSmramCpuNvsHeaderGuid = EFI_SMRAM_CPU_NVS_HEADER_GUID;
42
43 /**
44 Dispatch function for a Software SMI handler.
45
46 @param DispatchHandle The handle of this dispatch function.
47 @param DispatchContext The pointer to the dispatch function's context.
48 The SwSmiInputValue field is filled in
49 by the software dispatch driver prior to
50 invoking this dispatch function.
51 The dispatch function will only be called
52 for input values for which it is registered.
53
54 @return None
55
56 **/
57 VOID
58 EFIAPI
59 SmramSaveInfoHandler (
60 IN EFI_HANDLE DispatchHandle,
61 IN EFI_SMM_SW_DISPATCH_CONTEXT *DispatchContext
62 )
63 {
64 EFI_STATUS Status;
65 UINT64 VarData[3];
66 UINTN VarSize;
67
68 ASSERT (DispatchContext != NULL);
69 ASSERT (DispatchContext->SwSmiInputValue == SMM_FROM_SMBASE_DRIVER);
70
71 if (!mLocked && IoRead8 (mSmiDataRegister) == SMM_FROM_CPU_DRIVER_SAVE_INFO) {
72 VarSize = sizeof (VarData);
73 Status = gRT->GetVariable (
74 L"SmramCpuNvs",
75 &mSmramCpuNvsHeaderGuid,
76 NULL,
77 &VarSize,
78 VarData
79 );
80 if (!EFI_ERROR (Status) && VarSize == sizeof (VarData)) {
81 CopyMem (
82 (VOID *)(UINTN)(VarData[0]),
83 (VOID *)(UINTN)(VarData[1]),
84 (UINTN)(VarData[2])
85 );
86 }
87 }
88 }
89
90 /**
91 Smm Ready To Lock event notification handler.
92
93 It sets a flag indicating that SMRAM has been locked.
94
95 @param[in] Protocol Points to the protocol's unique identifier.
96 @param[in] Interface Points to the interface instance.
97 @param[in] Handle The handle on which the interface was installed.
98
99 @retval EFI_SUCCESS Notification handler runs successfully.
100 **/
101 EFI_STATUS
102 EFIAPI
103 SmmReadyToLockEventNotify (
104 IN CONST EFI_GUID *Protocol,
105 IN VOID *Interface,
106 IN EFI_HANDLE Handle
107 )
108 {
109 mLocked = TRUE;
110 return EFI_SUCCESS;
111 }
112
113 /**
114 Entry point function of this driver.
115
116 @param[in] ImageHandle The firmware allocated handle for the EFI image.
117 @param[in] SystemTable A pointer to the EFI System Table.
118
119 @retval EFI_SUCCESS The entry point is executed successfully.
120 @retval other Some error occurs when executing this entry point.
121 **/
122 EFI_STATUS
123 EFIAPI
124 SmramSaveInfoHandlerSmmMain (
125 IN EFI_HANDLE ImageHandle,
126 IN EFI_SYSTEM_TABLE *SystemTable
127 )
128 {
129 EFI_STATUS Status;
130 EFI_SMM_SW_DISPATCH_PROTOCOL *SmmSwDispatch;
131 EFI_SMM_SW_DISPATCH_CONTEXT SmmSwDispatchContext;
132 EFI_HANDLE DispatchHandle;
133 EFI_SMM_CONTROL_PROTOCOL *SmmControl;
134 EFI_SMM_CONTROL_REGISTER SmmControlRegister;
135 VOID *Registration;
136
137 //
138 // Get SMI data register
139 //
140 Status = SystemTable->BootServices->LocateProtocol (
141 &gEfiSmmControlProtocolGuid,
142 NULL,
143 (VOID **)&SmmControl
144 );
145 ASSERT_EFI_ERROR (Status);
146 Status = SmmControl->GetRegisterInfo (SmmControl, &SmmControlRegister);
147 ASSERT_EFI_ERROR (Status);
148 mSmiDataRegister = SmmControlRegister.SmiDataRegister;
149
150 //
151 // Register software SMI handler
152 //
153
154 Status = SystemTable->BootServices->LocateProtocol (
155 &gEfiSmmSwDispatchProtocolGuid,
156 NULL,
157 (VOID **)&SmmSwDispatch
158 );
159 ASSERT_EFI_ERROR (Status);
160
161 SmmSwDispatchContext.SwSmiInputValue = SMM_FROM_SMBASE_DRIVER;
162 Status = SmmSwDispatch->Register (
163 SmmSwDispatch,
164 &SmramSaveInfoHandler,
165 &SmmSwDispatchContext,
166 &DispatchHandle
167 );
168 ASSERT_EFI_ERROR (Status);
169
170 //
171 // Register SMM Ready To Lock Protocol notification
172 //
173 Status = gSmst->SmmRegisterProtocolNotify (
174 &gEfiSmmReadyToLockProtocolGuid,
175 SmmReadyToLockEventNotify,
176 &Registration
177 );
178 ASSERT_EFI_ERROR (Status);
179
180 return Status;
181 }
182