]> git.proxmox.com Git - mirror_edk2.git/blob - EdkCompatibilityPkg/Compatibility/SmmBaseHelper/SmramProfileRecord.c
84eba482f453c9889ab52c3ac402fcedfcde1413
[mirror_edk2.git] / EdkCompatibilityPkg / Compatibility / SmmBaseHelper / SmramProfileRecord.c
1 /** @file
2
3 Copyright (c) 2014, Intel Corporation. All rights reserved.<BR>
4 This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php.
8
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12 **/
13
14 #include <PiSmm.h>
15
16 #include <Library/BaseLib.h>
17 #include <Library/MemoryAllocationLib.h>
18 #include <Library/UefiBootServicesTableLib.h>
19 #include <Library/SmmServicesTableLib.h>
20 #include <Library/DevicePathLib.h>
21 #include <Library/BaseMemoryLib.h>
22 #include <Library/DebugLib.h>
23 #include <Protocol/SmmCommunication.h>
24
25 #include <Guid/MemoryProfile.h>
26
27 EFI_SMM_COMMUNICATION_PROTOCOL *mSmmCommunication = NULL;
28
29 /**
30 Get the GUID file name from the file path.
31
32 @param FilePath File path.
33
34 @return The GUID file name from the file path.
35
36 **/
37 EFI_GUID *
38 GetFileNameFromFilePath (
39 IN EFI_DEVICE_PATH_PROTOCOL *FilePath
40 )
41 {
42 MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *ThisFilePath;
43
44 ThisFilePath = (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *) FilePath;
45 while (!IsDevicePathEnd (ThisFilePath)) {
46 if ((DevicePathType (ThisFilePath) == MEDIA_DEVICE_PATH) && (DevicePathSubType (ThisFilePath) == MEDIA_PIWG_FW_FILE_DP)) {
47 return &ThisFilePath->FvFileName;
48 }
49 ThisFilePath = (MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *) NextDevicePathNode (ThisFilePath);
50 }
51
52 return NULL;
53 }
54
55 /**
56 Register SMM image to SMRAM profile.
57
58 @param[in] FilePath File path of the image.
59 @param[in] ImageBuffer Image base address.
60 @param[in] NumberOfPage Number of page.
61
62 @retval TRUE Register success.
63 @retval FALSE Register fail.
64
65 **/
66 BOOLEAN
67 RegisterSmramProfileImage (
68 IN EFI_DEVICE_PATH_PROTOCOL *FilePath,
69 IN PHYSICAL_ADDRESS ImageBuffer,
70 IN UINTN NumberOfPage
71 )
72 {
73 EFI_GUID *FileName;
74 EFI_STATUS Status;
75 UINTN CommSize;
76 UINT8 CommBuffer[sizeof (EFI_GUID) + sizeof (UINTN) + sizeof (SMRAM_PROFILE_PARAMETER_REGISTER_IMAGE)];
77 EFI_SMM_COMMUNICATE_HEADER *CommHeader;
78 SMRAM_PROFILE_PARAMETER_REGISTER_IMAGE *CommRegisterImage;
79
80 if ((PcdGet8 (PcdMemoryProfilePropertyMask) & BIT1) == 0) {
81 return FALSE;
82 }
83
84 FileName = GetFileNameFromFilePath (FilePath);
85
86 if (mSmmCommunication == NULL) {
87 Status = gBS->LocateProtocol (&gEfiSmmCommunicationProtocolGuid, NULL, (VOID **) &mSmmCommunication);
88 ASSERT_EFI_ERROR (Status);
89 }
90
91 CommHeader = (EFI_SMM_COMMUNICATE_HEADER *) &CommBuffer[0];
92 CopyMem (&CommHeader->HeaderGuid, &gEdkiiMemoryProfileGuid, sizeof (gEdkiiMemoryProfileGuid));
93 CommHeader->MessageLength = sizeof (SMRAM_PROFILE_PARAMETER_REGISTER_IMAGE);
94
95 CommRegisterImage = (SMRAM_PROFILE_PARAMETER_REGISTER_IMAGE *)&CommBuffer[OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data)];
96 CommRegisterImage->Header.Command = SMRAM_PROFILE_COMMAND_REGISTER_IMAGE;
97 CommRegisterImage->Header.DataLength = sizeof (*CommRegisterImage);
98 CommRegisterImage->Header.ReturnStatus = (UINT64)-1;
99 CopyMem (&CommRegisterImage->FileName, FileName, sizeof(EFI_GUID));
100 CommRegisterImage->ImageBuffer = ImageBuffer;
101 CommRegisterImage->NumberOfPage = NumberOfPage;
102
103 CommSize = sizeof (EFI_GUID) + sizeof (UINTN) + CommHeader->MessageLength;
104 Status = mSmmCommunication->Communicate (mSmmCommunication, CommBuffer, &CommSize);
105 ASSERT_EFI_ERROR (Status);
106
107 if (CommRegisterImage->Header.ReturnStatus != 0) {
108 return FALSE;
109 }
110
111 return TRUE;
112 }
113
114 /**
115 Unregister SMM image from SMRAM profile.
116
117 @param[in] FilePath File path of the image.
118 @param[in] ImageBuffer Image base address.
119 @param[in] NumberOfPage Number of page.
120
121 @retval TRUE Unregister success.
122 @retval FALSE Unregister fail.
123
124 **/
125 BOOLEAN
126 UnregisterSmramProfileImage (
127 IN EFI_DEVICE_PATH_PROTOCOL *FilePath,
128 IN PHYSICAL_ADDRESS ImageBuffer,
129 IN UINTN NumberOfPage
130 )
131 {
132 EFI_GUID *FileName;
133 EFI_STATUS Status;
134 UINTN CommSize;
135 UINT8 CommBuffer[sizeof (EFI_GUID) + sizeof (UINTN) + sizeof (SMRAM_PROFILE_PARAMETER_UNREGISTER_IMAGE)];
136 EFI_SMM_COMMUNICATE_HEADER *CommHeader;
137 SMRAM_PROFILE_PARAMETER_UNREGISTER_IMAGE *CommUnregisterImage;
138
139 if ((PcdGet8 (PcdMemoryProfilePropertyMask) & BIT1) == 0) {
140 return FALSE;
141 }
142
143 FileName = GetFileNameFromFilePath (FilePath);
144
145 if (mSmmCommunication == NULL) {
146 Status = gBS->LocateProtocol (&gEfiSmmCommunicationProtocolGuid, NULL, (VOID **) &mSmmCommunication);
147 ASSERT_EFI_ERROR (Status);
148 }
149
150 CommHeader = (EFI_SMM_COMMUNICATE_HEADER *)&CommBuffer[0];
151 CopyMem (&CommHeader->HeaderGuid, &gEdkiiMemoryProfileGuid, sizeof(gEdkiiMemoryProfileGuid));
152 CommHeader->MessageLength = sizeof(SMRAM_PROFILE_PARAMETER_UNREGISTER_IMAGE);
153
154 CommUnregisterImage = (SMRAM_PROFILE_PARAMETER_UNREGISTER_IMAGE *)&CommBuffer[OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data)];
155 CommUnregisterImage->Header.Command = SMRAM_PROFILE_COMMAND_UNREGISTER_IMAGE;
156 CommUnregisterImage->Header.DataLength = sizeof (*CommUnregisterImage);
157 CommUnregisterImage->Header.ReturnStatus = (UINT64)-1;
158 CopyMem (&CommUnregisterImage->FileName, FileName, sizeof(EFI_GUID));
159 CommUnregisterImage->ImageBuffer = ImageBuffer;
160 CommUnregisterImage->NumberOfPage = NumberOfPage;
161
162 CommSize = sizeof (EFI_GUID) + sizeof (UINTN) + CommHeader->MessageLength;
163 Status = mSmmCommunication->Communicate (mSmmCommunication, CommBuffer, &CommSize);
164 ASSERT_EFI_ERROR (Status);
165
166 if (CommUnregisterImage->Header.ReturnStatus != 0) {
167 return FALSE;
168 }
169
170 return TRUE;
171 }