]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - SecurityPkg/Tcg/MemoryOverwriteControl/TcgMor.c
SecurityPkg: Change use of EFI_D_* to DEBUG_*
[mirror_edk2.git] / SecurityPkg / Tcg / MemoryOverwriteControl / TcgMor.c
... / ...
CommitLineData
1/** @file\r
2 TCG MOR (Memory Overwrite Request) Control Driver.\r
3\r
4 This driver initialize MemoryOverwriteRequestControl variable. It\r
5 will clear MOR_CLEAR_MEMORY_BIT bit if it is set. It will also do TPer Reset for\r
6 those encrypted drives through EFI_STORAGE_SECURITY_COMMAND_PROTOCOL at EndOfDxe.\r
7\r
8Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>\r
9SPDX-License-Identifier: BSD-2-Clause-Patent\r
10\r
11**/\r
12\r
13#include "TcgMor.h"\r
14\r
15UINT8 mMorControl;\r
16\r
17/**\r
18 Ready to Boot Event notification handler.\r
19\r
20 @param[in] Event Event whose notification function is being invoked\r
21 @param[in] Context Pointer to the notification function's context\r
22\r
23**/\r
24VOID\r
25EFIAPI\r
26OnReadyToBoot (\r
27 IN EFI_EVENT Event,\r
28 IN VOID *Context\r
29 )\r
30{\r
31 EFI_STATUS Status;\r
32 UINTN DataSize;\r
33\r
34 if (MOR_CLEAR_MEMORY_VALUE (mMorControl) == 0x0) {\r
35 //\r
36 // MorControl is expected, directly return to avoid unnecessary variable operation\r
37 //\r
38 return ;\r
39 }\r
40 //\r
41 // Clear MOR_CLEAR_MEMORY_BIT\r
42 //\r
43 DEBUG ((DEBUG_INFO, "TcgMor: Clear MorClearMemory bit\n"));\r
44 mMorControl &= 0xFE;\r
45\r
46 DataSize = sizeof (mMorControl);\r
47 Status = gRT->SetVariable (\r
48 MEMORY_OVERWRITE_REQUEST_VARIABLE_NAME,\r
49 &gEfiMemoryOverwriteControlDataGuid,\r
50 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r
51 DataSize,\r
52 &mMorControl\r
53 );\r
54 if (EFI_ERROR (Status)) {\r
55 DEBUG ((DEBUG_ERROR, "TcgMor: Clear MOR_CLEAR_MEMORY_BIT failure, Status = %r\n", Status));\r
56 }\r
57}\r
58\r
59/**\r
60 Send TPer Reset command to reset eDrive to lock all protected bands.\r
61 Typically, there are 2 mechanism for resetting eDrive. They are:\r
62 1. TPer Reset through IEEE 1667 protocol.\r
63 2. TPer Reset through native TCG protocol.\r
64 This routine will detect what protocol the attached eDrive conform to, TCG or\r
65 IEEE 1667 protocol. Then send out TPer Reset command separately.\r
66\r
67 @param[in] Ssp The pointer to EFI_STORAGE_SECURITY_COMMAND_PROTOCOL instance.\r
68 @param[in] MediaId ID of the medium to receive data from or send data to.\r
69\r
70**/\r
71VOID\r
72InitiateTPerReset (\r
73 IN EFI_STORAGE_SECURITY_COMMAND_PROTOCOL *Ssp,\r
74 IN UINT32 MediaId\r
75 )\r
76{\r
77\r
78 EFI_STATUS Status;\r
79 UINT8 *Buffer;\r
80 UINTN XferSize;\r
81 UINTN Len;\r
82 UINTN Index;\r
83 BOOLEAN TcgFlag;\r
84 BOOLEAN IeeeFlag;\r
85 SUPPORTED_SECURITY_PROTOCOLS_PARAMETER_DATA *Data;\r
86\r
87 Buffer = NULL;\r
88 TcgFlag = FALSE;\r
89 IeeeFlag = FALSE;\r
90\r
91 //\r
92 // ATA8-ACS 7.57.6.1 indicates the Transfer Length field requirements a multiple of 512.\r
93 // If the length of the TRUSTED RECEIVE parameter data is greater than the Transfer Length,\r
94 // then the device shall return the TRUSTED RECEIVE parameter data truncated to the requested Transfer Length.\r
95 //\r
96 Len = ROUNDUP512(sizeof(SUPPORTED_SECURITY_PROTOCOLS_PARAMETER_DATA));\r
97 Buffer = AllocateZeroPool(Len);\r
98\r
99 if (Buffer == NULL) {\r
100 return;\r
101 }\r
102\r
103 //\r
104 // When the Security Protocol field is set to 00h, and SP Specific is set to 0000h in a TRUSTED RECEIVE\r
105 // command, the device basic information data shall be returned.\r
106 //\r
107 Status = Ssp->ReceiveData (\r
108 Ssp,\r
109 MediaId,\r
110 100000000, // Timeout 10-sec\r
111 0, // SecurityProtocol\r
112 0, // SecurityProtocolSpecificData\r
113 Len, // PayloadBufferSize,\r
114 Buffer, // PayloadBuffer\r
115 &XferSize\r
116 );\r
117 if (EFI_ERROR (Status)) {\r
118 goto Exit;\r
119 }\r
120\r
121 //\r
122 // In returned data, the ListLength field indicates the total length, in bytes,\r
123 // of the supported security protocol list.\r
124 //\r
125 Data = (SUPPORTED_SECURITY_PROTOCOLS_PARAMETER_DATA*)Buffer;\r
126 Len = ROUNDUP512(sizeof (SUPPORTED_SECURITY_PROTOCOLS_PARAMETER_DATA) +\r
127 (Data->SupportedSecurityListLength[0] << 8) +\r
128 (Data->SupportedSecurityListLength[1])\r
129 );\r
130\r
131 //\r
132 // Free original buffer and allocate new buffer.\r
133 //\r
134 FreePool(Buffer);\r
135 Buffer = AllocateZeroPool(Len);\r
136 if (Buffer == NULL) {\r
137 return;\r
138 }\r
139\r
140 //\r
141 // Read full supported security protocol list from device.\r
142 //\r
143 Status = Ssp->ReceiveData (\r
144 Ssp,\r
145 MediaId,\r
146 100000000, // Timeout 10-sec\r
147 0, // SecurityProtocol\r
148 0, // SecurityProtocolSpecificData\r
149 Len, // PayloadBufferSize,\r
150 Buffer, // PayloadBuffer\r
151 &XferSize\r
152 );\r
153\r
154 if (EFI_ERROR (Status)) {\r
155 goto Exit;\r
156 }\r
157\r
158 Data = (SUPPORTED_SECURITY_PROTOCOLS_PARAMETER_DATA*)Buffer;\r
159 Len = (Data->SupportedSecurityListLength[0] << 8) + Data->SupportedSecurityListLength[1];\r
160\r
161 //\r
162 // Iterate full supported security protocol list to check if TCG or IEEE 1667 protocol\r
163 // is supported.\r
164 //\r
165 for (Index = 0; Index < Len; Index++) {\r
166 if (Data->SupportedSecurityProtocol[Index] == SECURITY_PROTOCOL_TCG) {\r
167 //\r
168 // Found a TCG device.\r
169 //\r
170 TcgFlag = TRUE;\r
171 DEBUG ((DEBUG_INFO, "This device is a TCG protocol device\n"));\r
172 break;\r
173 }\r
174\r
175 if (Data->SupportedSecurityProtocol[Index] == SECURITY_PROTOCOL_IEEE1667) {\r
176 //\r
177 // Found a IEEE 1667 device.\r
178 //\r
179 IeeeFlag = TRUE;\r
180 DEBUG ((DEBUG_INFO, "This device is a IEEE 1667 protocol device\n"));\r
181 break;\r
182 }\r
183 }\r
184\r
185 if (!TcgFlag && !IeeeFlag) {\r
186 DEBUG ((DEBUG_INFO, "Neither a TCG nor IEEE 1667 protocol device is found\n"));\r
187 goto Exit;\r
188 }\r
189\r
190 if (TcgFlag) {\r
191 //\r
192 // As long as TCG protocol is supported, send out a TPer Reset\r
193 // TCG command to the device via the TrustedSend command with a non-zero Transfer Length.\r
194 //\r
195 Status = Ssp->SendData (\r
196 Ssp,\r
197 MediaId,\r
198 100000000, // Timeout 10-sec\r
199 SECURITY_PROTOCOL_TCG, // SecurityProtocol\r
200 0x0400, // SecurityProtocolSpecificData\r
201 512, // PayloadBufferSize,\r
202 Buffer // PayloadBuffer\r
203 );\r
204\r
205 if (!EFI_ERROR (Status)) {\r
206 DEBUG ((DEBUG_INFO, "Send TPer Reset Command Successfully !\n"));\r
207 } else {\r
208 DEBUG ((DEBUG_INFO, "Send TPer Reset Command Fail !\n"));\r
209 }\r
210 }\r
211\r
212 if (IeeeFlag) {\r
213 //\r
214 // TBD : Perform a TPer Reset via IEEE 1667 Protocol\r
215 //\r
216 DEBUG ((DEBUG_INFO, "IEEE 1667 Protocol didn't support yet!\n"));\r
217 }\r
218\r
219Exit:\r
220\r
221 if (Buffer != NULL) {\r
222 FreePool(Buffer);\r
223 }\r
224}\r
225\r
226/**\r
227 Notification function of END_OF_DXE.\r
228\r
229 @param[in] Event Event whose notification function is being invoked.\r
230 @param[in] Context Pointer to the notification function's context.\r
231\r
232**/\r
233VOID\r
234EFIAPI\r
235TPerResetAtEndOfDxe (\r
236 IN EFI_EVENT Event,\r
237 IN VOID *Context\r
238 )\r
239{\r
240 EFI_STORAGE_SECURITY_COMMAND_PROTOCOL *Ssp;\r
241 EFI_BLOCK_IO_PROTOCOL *BlockIo;\r
242 EFI_STATUS Status;\r
243 UINTN HandleCount;\r
244 EFI_HANDLE *HandleBuffer;\r
245 UINTN Index;\r
246\r
247 //\r
248 // Locate all SSP protocol instances.\r
249 //\r
250 HandleCount = 0;\r
251 HandleBuffer = NULL;\r
252\r
253 Status = gBS->LocateHandleBuffer (\r
254 ByProtocol,\r
255 &gEfiStorageSecurityCommandProtocolGuid,\r
256 NULL,\r
257 &HandleCount,\r
258 &HandleBuffer\r
259 );\r
260\r
261 if (EFI_ERROR (Status) || (HandleCount == 0) || (HandleBuffer == NULL)) {\r
262 return;\r
263 }\r
264\r
265 for (Index = 0; Index < HandleCount; Index ++) {\r
266 //\r
267 // Get the SSP interface.\r
268 //\r
269 Status = gBS->HandleProtocol(\r
270 HandleBuffer[Index],\r
271 &gEfiStorageSecurityCommandProtocolGuid,\r
272 (VOID **) &Ssp\r
273 );\r
274\r
275 if (EFI_ERROR (Status)) {\r
276 continue;\r
277 }\r
278\r
279 Status = gBS->HandleProtocol(\r
280 HandleBuffer[Index],\r
281 &gEfiBlockIoProtocolGuid,\r
282 (VOID **) &BlockIo\r
283 );\r
284\r
285 if (EFI_ERROR (Status)) {\r
286 continue;\r
287 }\r
288\r
289 InitiateTPerReset (Ssp, BlockIo->Media->MediaId);\r
290 }\r
291\r
292 FreePool (HandleBuffer);\r
293}\r
294\r
295/**\r
296 Entry Point for TCG MOR Control driver.\r
297\r
298 @param[in] ImageHandle Image handle of this driver.\r
299 @param[in] SystemTable A Pointer to the EFI System Table.\r
300\r
301 @retval EFI_SUCCESS\r
302 @return Others Some error occurs.\r
303**/\r
304EFI_STATUS\r
305EFIAPI\r
306MorDriverEntryPoint (\r
307 IN EFI_HANDLE ImageHandle,\r
308 IN EFI_SYSTEM_TABLE *SystemTable\r
309 )\r
310{\r
311 EFI_STATUS Status;\r
312 UINTN DataSize;\r
313 EFI_EVENT Event;\r
314\r
315 ///\r
316 /// The firmware is required to create the MemoryOverwriteRequestControl UEFI variable.\r
317 ///\r
318\r
319 DataSize = sizeof (mMorControl);\r
320 Status = gRT->GetVariable (\r
321 MEMORY_OVERWRITE_REQUEST_VARIABLE_NAME,\r
322 &gEfiMemoryOverwriteControlDataGuid,\r
323 NULL,\r
324 &DataSize,\r
325 &mMorControl\r
326 );\r
327 if (EFI_ERROR (Status)) {\r
328 //\r
329 // Set default value to 0\r
330 //\r
331 mMorControl = 0;\r
332 Status = gRT->SetVariable (\r
333 MEMORY_OVERWRITE_REQUEST_VARIABLE_NAME,\r
334 &gEfiMemoryOverwriteControlDataGuid,\r
335 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r
336 DataSize,\r
337 &mMorControl\r
338 );\r
339 DEBUG ((DEBUG_INFO, "TcgMor: Create MOR variable! Status = %r\n", Status));\r
340 } else {\r
341 //\r
342 // Create a Ready To Boot Event and Clear the MorControl bit in the call back function.\r
343 //\r
344 DEBUG ((DEBUG_INFO, "TcgMor: Create ReadyToBoot Event for MorControl Bit cleaning!\n"));\r
345 Status = EfiCreateEventReadyToBootEx (\r
346 TPL_CALLBACK,\r
347 OnReadyToBoot,\r
348 NULL,\r
349 &Event\r
350 );\r
351 if (EFI_ERROR (Status)) {\r
352 return Status;\r
353 }\r
354\r
355 //\r
356 // Register EFI_END_OF_DXE_EVENT_GROUP_GUID event.\r
357 //\r
358 DEBUG ((DEBUG_INFO, "TcgMor: Create EndofDxe Event for Mor TPer Reset!\n"));\r
359 Status = gBS->CreateEventEx (\r
360 EVT_NOTIFY_SIGNAL,\r
361 TPL_CALLBACK,\r
362 TPerResetAtEndOfDxe,\r
363 NULL,\r
364 &gEfiEndOfDxeEventGroupGuid,\r
365 &Event\r
366 );\r
367 if (EFI_ERROR (Status)) {\r
368 return Status;\r
369 }\r
370 }\r
371\r
372 return Status;\r
373}\r