]>
Commit | Line | Data |
---|---|---|
b3dc26ed MK |
1 | /** @file\r |
2 | PiSmmCommunication SMM Driver.\r | |
3 | \r | |
8b1d1493 | 4 | Copyright (c) 2010 - 2017, Intel Corporation. All rights reserved.<BR>\r |
0acd8697 | 5 | SPDX-License-Identifier: BSD-2-Clause-Patent\r |
b3dc26ed MK |
6 | \r |
7 | **/\r | |
8 | \r | |
9 | #include <PiSmm.h>\r | |
10 | #include <Library/UefiDriverEntryPoint.h>\r | |
11 | #include <Library/UefiBootServicesTableLib.h>\r | |
12 | #include <Library/UefiRuntimeServicesTableLib.h>\r | |
13 | #include <Library/SmmServicesTableLib.h>\r | |
14 | #include <Library/BaseLib.h>\r | |
15 | #include <Library/BaseMemoryLib.h>\r | |
b3dc26ed MK |
16 | #include <Library/DebugLib.h>\r |
17 | #include <Library/SmmMemLib.h>\r | |
b3dc26ed | 18 | #include <Protocol/SmmSwDispatch2.h>\r |
b3dc26ed | 19 | #include <Protocol/SmmCommunication.h>\r |
b3dc26ed | 20 | #include <Ppi/SmmCommunication.h>\r |
b3dc26ed MK |
21 | \r |
22 | #include "PiSmmCommunicationPrivate.h"\r | |
23 | \r | |
24 | EFI_SMM_COMMUNICATION_CONTEXT mSmmCommunicationContext = {\r | |
25 | SMM_COMMUNICATION_SIGNATURE\r | |
26 | };\r | |
27 | \r | |
b3dc26ed MK |
28 | /**\r |
29 | Set SMM communication context.\r | |
30 | **/\r | |
31 | VOID\r | |
32 | SetCommunicationContext (\r | |
33 | VOID\r | |
34 | )\r | |
35 | {\r | |
36 | EFI_STATUS Status;\r | |
37 | \r | |
38 | Status = gSmst->SmmInstallConfigurationTable (\r | |
39 | gSmst,\r | |
40 | &gEfiPeiSmmCommunicationPpiGuid,\r | |
41 | &mSmmCommunicationContext,\r | |
42 | sizeof(mSmmCommunicationContext)\r | |
43 | );\r | |
44 | ASSERT_EFI_ERROR (Status);\r | |
45 | }\r | |
46 | \r | |
47 | /**\r | |
48 | Dispatch function for a Software SMI handler.\r | |
49 | \r | |
50 | @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().\r | |
51 | @param Context Points to an optional handler context which was specified when the\r | |
52 | handler was registered.\r | |
53 | @param CommBuffer A pointer to a collection of data in memory that will\r | |
54 | be conveyed from a non-SMM environment into an SMM environment.\r | |
55 | @param CommBufferSize The size of the CommBuffer.\r | |
56 | \r | |
57 | @retval EFI_SUCCESS Command is handled successfully.\r | |
58 | \r | |
59 | **/\r | |
60 | EFI_STATUS\r | |
61 | EFIAPI\r | |
62 | PiSmmCommunicationHandler (\r | |
63 | IN EFI_HANDLE DispatchHandle,\r | |
64 | IN CONST VOID *Context OPTIONAL,\r | |
65 | IN OUT VOID *CommBuffer OPTIONAL,\r | |
66 | IN OUT UINTN *CommBufferSize OPTIONAL\r | |
67 | )\r | |
68 | {\r | |
69 | UINTN CommSize;\r | |
70 | EFI_STATUS Status;\r | |
71 | EFI_SMM_COMMUNICATE_HEADER *CommunicateHeader;\r | |
72 | EFI_PHYSICAL_ADDRESS *BufferPtrAddress;\r | |
73 | \r | |
74 | DEBUG ((EFI_D_INFO, "PiSmmCommunicationHandler Enter\n"));\r | |
75 | \r | |
76 | BufferPtrAddress = (EFI_PHYSICAL_ADDRESS *)(UINTN)mSmmCommunicationContext.BufferPtrAddress;\r | |
77 | CommunicateHeader = (EFI_SMM_COMMUNICATE_HEADER *)(UINTN)*BufferPtrAddress;\r | |
78 | DEBUG ((EFI_D_INFO, "PiSmmCommunicationHandler CommunicateHeader - %x\n", CommunicateHeader));\r | |
79 | if (CommunicateHeader == NULL) {\r | |
80 | DEBUG ((EFI_D_INFO, "PiSmmCommunicationHandler is NULL, needn't to call dispatch function\n"));\r | |
81 | Status = EFI_SUCCESS;\r | |
82 | } else {\r | |
83 | if (!SmmIsBufferOutsideSmmValid ((UINTN)CommunicateHeader, OFFSET_OF (EFI_SMM_COMMUNICATE_HEADER, Data))) {\r | |
84 | DEBUG ((EFI_D_INFO, "PiSmmCommunicationHandler CommunicateHeader invalid - 0x%x\n", CommunicateHeader));\r | |
85 | Status = EFI_SUCCESS;\r | |
86 | goto Done;\r | |
87 | }\r | |
88 | \r | |
89 | CommSize = (UINTN)CommunicateHeader->MessageLength;\r | |
90 | if (!SmmIsBufferOutsideSmmValid ((UINTN)&CommunicateHeader->Data[0], CommSize)) {\r | |
91 | DEBUG ((EFI_D_INFO, "PiSmmCommunicationHandler CommunicateData invalid - 0x%x\n", &CommunicateHeader->Data[0]));\r | |
92 | Status = EFI_SUCCESS;\r | |
93 | goto Done;\r | |
94 | }\r | |
95 | \r | |
96 | //\r | |
97 | // Call dispatch function\r | |
98 | //\r | |
99 | DEBUG ((EFI_D_INFO, "PiSmmCommunicationHandler Data - %x\n", &CommunicateHeader->Data[0]));\r | |
100 | Status = gSmst->SmiManage (\r | |
101 | &CommunicateHeader->HeaderGuid,\r | |
102 | NULL,\r | |
103 | &CommunicateHeader->Data[0],\r | |
104 | &CommSize\r | |
105 | );\r | |
106 | }\r | |
107 | \r | |
108 | Done:\r | |
109 | DEBUG ((EFI_D_INFO, "PiSmmCommunicationHandler %r\n", Status));\r | |
110 | DEBUG ((EFI_D_INFO, "PiSmmCommunicationHandler Exit\n"));\r | |
111 | \r | |
112 | return (Status == EFI_SUCCESS) ? EFI_SUCCESS : EFI_INTERRUPT_PENDING;\r | |
113 | }\r | |
114 | \r | |
115 | /**\r | |
116 | Allocate EfiACPIMemoryNVS below 4G memory address.\r | |
117 | \r | |
118 | This function allocates EfiACPIMemoryNVS below 4G memory address.\r | |
119 | \r | |
120 | @param Size Size of memory to allocate.\r | |
121 | \r | |
122 | @return Allocated address for output.\r | |
123 | \r | |
124 | **/\r | |
125 | VOID*\r | |
126 | AllocateAcpiNvsMemoryBelow4G (\r | |
127 | IN UINTN Size\r | |
128 | )\r | |
129 | {\r | |
130 | UINTN Pages;\r | |
131 | EFI_PHYSICAL_ADDRESS Address;\r | |
132 | EFI_STATUS Status;\r | |
133 | VOID* Buffer;\r | |
134 | \r | |
135 | Pages = EFI_SIZE_TO_PAGES (Size);\r | |
136 | Address = 0xffffffff;\r | |
137 | \r | |
138 | Status = gBS->AllocatePages (\r | |
139 | AllocateMaxAddress,\r | |
140 | EfiACPIMemoryNVS,\r | |
141 | Pages,\r | |
142 | &Address\r | |
143 | );\r | |
144 | ASSERT_EFI_ERROR (Status);\r | |
145 | \r | |
146 | Buffer = (VOID *) (UINTN) Address;\r | |
147 | ZeroMem (Buffer, Size);\r | |
148 | \r | |
149 | return Buffer;\r | |
150 | }\r | |
151 | \r | |
152 | /**\r | |
153 | Entry Point for PI SMM communication SMM driver.\r | |
154 | \r | |
155 | @param[in] ImageHandle Image handle of this driver.\r | |
156 | @param[in] SystemTable A Pointer to the EFI System Table.\r | |
157 | \r | |
158 | @retval EFI_SUCEESS\r | |
159 | @return Others Some error occurs.\r | |
160 | **/\r | |
161 | EFI_STATUS\r | |
162 | EFIAPI\r | |
163 | PiSmmCommunicationSmmEntryPoint (\r | |
164 | IN EFI_HANDLE ImageHandle,\r | |
165 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
166 | )\r | |
167 | {\r | |
168 | EFI_STATUS Status;\r | |
169 | EFI_SMM_SW_DISPATCH2_PROTOCOL *SmmSwDispatch2;\r | |
170 | EFI_SMM_SW_REGISTER_CONTEXT SmmSwDispatchContext;\r | |
171 | EFI_HANDLE DispatchHandle;\r | |
b3dc26ed MK |
172 | EFI_PHYSICAL_ADDRESS *BufferPtrAddress;\r |
173 | \r | |
b3dc26ed MK |
174 | //\r |
175 | // Register software SMI handler\r | |
176 | //\r | |
177 | Status = gSmst->SmmLocateProtocol (\r | |
178 | &gEfiSmmSwDispatch2ProtocolGuid,\r | |
179 | NULL,\r | |
180 | (VOID **)&SmmSwDispatch2\r | |
181 | );\r | |
182 | ASSERT_EFI_ERROR (Status);\r | |
183 | \r | |
184 | SmmSwDispatchContext.SwSmiInputValue = (UINTN)-1;\r | |
185 | Status = SmmSwDispatch2->Register (\r | |
186 | SmmSwDispatch2,\r | |
187 | PiSmmCommunicationHandler,\r | |
188 | &SmmSwDispatchContext,\r | |
189 | &DispatchHandle\r | |
190 | );\r | |
191 | ASSERT_EFI_ERROR (Status);\r | |
192 | \r | |
193 | DEBUG ((EFI_D_INFO, "SmmCommunication SwSmi: %x\n", (UINTN)SmmSwDispatchContext.SwSmiInputValue));\r | |
194 | \r | |
b3dc26ed MK |
195 | BufferPtrAddress = AllocateAcpiNvsMemoryBelow4G (sizeof(EFI_PHYSICAL_ADDRESS));\r |
196 | ASSERT (BufferPtrAddress != NULL);\r | |
197 | DEBUG ((EFI_D_INFO, "SmmCommunication BufferPtrAddress: 0x%016lx, BufferPtr: 0x%016lx\n", (EFI_PHYSICAL_ADDRESS)(UINTN)BufferPtrAddress, *BufferPtrAddress));\r | |
b3dc26ed MK |
198 | \r |
199 | //\r | |
200 | // Save context\r | |
201 | //\r | |
202 | mSmmCommunicationContext.SwSmiNumber = (UINT32)SmmSwDispatchContext.SwSmiInputValue;\r | |
8b1d1493 | 203 | mSmmCommunicationContext.BufferPtrAddress = (EFI_PHYSICAL_ADDRESS)(UINTN)BufferPtrAddress;\r |
b3dc26ed MK |
204 | SetCommunicationContext ();\r |
205 | \r | |
206 | return Status;\r | |
207 | }\r |