]> git.proxmox.com Git - mirror_edk2.git/blame - SecurityPkg/Tcg/Opal/OpalPasswordSmm/OpalPasswordSmm.c
SecurityPkg OpalPasswordSmm: Fix S3 resume failure.
[mirror_edk2.git] / SecurityPkg / Tcg / Opal / OpalPasswordSmm / OpalPasswordSmm.c
CommitLineData
cb274a27
ED
1/** @file\r
2 Opal password smm driver which is used to support Opal security feature at s3 path.\r
3\r
4Copyright (c) 2016, Intel Corporation. All rights reserved.<BR>\r
5This program and the accompanying materials\r
6are licensed and made available under the terms and conditions of the BSD License\r
7which accompanies this distribution. The full text of the license may be found at\r
8http://opensource.org/licenses/bsd-license.php\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#include "OpalPasswordSmm.h"\r
16\r
17#define SMM_SIZE_ALLOC_BYTES (512)\r
18#define RESPONSE_SIZE (200)\r
19\r
20#define PCI_CLASS_MASS_STORAGE_AHCI (0x06)\r
21\r
22#define OPAL_PCIE_ROOTPORT_SAVESIZE (0x40)\r
23#define STORE_INVALID_ROOTPORT_INDEX ((UINT8) -1)\r
24#define OPAL_DEVICE_TYPE_SATA 0x1\r
25#define OPAL_DEVICE_TYPE_NVME 0x2\r
26#define OPAL_DEVICE_TYPE_UNKNOWN 0xFF\r
27\r
28//\r
29// To unlock the Intel SATA controller at S3 Resume, restored the following registers.\r
30//\r
31const OPAL_HC_PCI_REGISTER_SAVE mSataHcRegisterSaveTemplate[] = {\r
32 {0x9, S3BootScriptWidthUint8},\r
33 {0x10, S3BootScriptWidthUint32},\r
34 {0x14, S3BootScriptWidthUint32},\r
35 {0x18, S3BootScriptWidthUint32},\r
36 {0x1C, S3BootScriptWidthUint32},\r
37 {0x20, S3BootScriptWidthUint32},\r
38 {0x24, S3BootScriptWidthUint32},\r
39 {0x3c, S3BootScriptWidthUint8},\r
40 {0x3d, S3BootScriptWidthUint8},\r
41 {0x40, S3BootScriptWidthUint16},\r
42 {0x42, S3BootScriptWidthUint16},\r
43 {0x92, S3BootScriptWidthUint16},\r
44 {0x94, S3BootScriptWidthUint32},\r
45 {0x9C, S3BootScriptWidthUint32},\r
46 {0x4, S3BootScriptWidthUint16},\r
47};\r
48\r
49\r
50UINT8 mSwSmiValue;\r
51LIST_ENTRY *mOpalDeviceList;\r
52LIST_ENTRY mSmmDeviceList = INITIALIZE_LIST_HEAD_VARIABLE (mSmmDeviceList);\r
53\r
54BOOLEAN mSendBlockSID = FALSE;\r
55\r
56// AHCI\r
57UINT32 mAhciBar = 0;\r
58EFI_AHCI_REGISTERS mAhciRegisters;\r
59VOID *mBuffer = NULL; // DMA can not read/write Data to smram, so we pre-allocates Buffer from AcpiNVS.\r
60//\r
61// NVME\r
62NVME_CONTEXT mNvmeContext;\r
63\r
009264f5
ED
64EFI_GCD_MEMORY_SPACE_DESCRIPTOR *mGcdMemSpace = NULL;\r
65UINTN mNumberOfDescriptors = 0;\r
66\r
cb274a27
ED
67/**\r
68 Add new bridge node or nvme device info to the device list.\r
69\r
70 @param[in] BusNum The bus number.\r
71 @param[in] DevNum The device number.\r
72 @param[in] FuncNum The function number.\r
73 @param[in] Dev The device which need to add device node info.\r
74\r
75**/\r
76VOID\r
77AddPciDeviceNode (\r
78 UINT32 BusNum,\r
79 UINT32 DevNum,\r
80 UINT32 FuncNum,\r
81 OPAL_SMM_DEVICE *Dev\r
82 )\r
83{\r
84 UINT8 *DevList;\r
85 PCI_DEVICE *DeviceNode;\r
86\r
87 DevList = AllocateZeroPool (sizeof (PCI_DEVICE) + Dev->Length);\r
88 ASSERT (DevList != NULL);\r
89\r
90 if (Dev->Length != 0) {\r
91 CopyMem (DevList, Dev->PciBridgeNode, Dev->Length);\r
92 FreePool (Dev->PciBridgeNode);\r
93 }\r
94\r
95 DeviceNode = (PCI_DEVICE *) (DevList + Dev->Length);\r
96\r
97 DeviceNode->BusNum = BusNum;\r
98 DeviceNode->DevNum = DevNum;\r
99 DeviceNode->FuncNum = FuncNum;\r
100\r
101 Dev->Length += sizeof (PCI_DEVICE);\r
102 Dev->PciBridgeNode = (PCI_DEVICE *)DevList;\r
103}\r
104\r
105/**\r
106 Extract device info from the input device path.\r
107\r
108 @param[in] DevicePath Device path info for the device.\r
109 @param[in,out] Dev The device which new inputed.\r
110\r
111**/\r
112VOID\r
113ExtractDeviceInfoFromDevicePath (\r
114 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath,\r
115 IN OUT OPAL_SMM_DEVICE *Dev\r
116 )\r
117{\r
118 EFI_DEVICE_PATH_PROTOCOL *TmpDevPath;\r
119 EFI_DEVICE_PATH_PROTOCOL *TmpDevPath2;\r
120 PCI_DEVICE_PATH *PciDevPath;\r
121 SATA_DEVICE_PATH *SataDevPath;\r
122 NVME_NAMESPACE_DEVICE_PATH *NvmeDevPath;\r
123 UINTN BusNum;\r
124\r
125 TmpDevPath = DevicePath;\r
126 Dev->DeviceType = OPAL_DEVICE_TYPE_UNKNOWN;\r
127\r
128 while (!IsDevicePathEnd(TmpDevPath)) {\r
129 if (TmpDevPath->Type == MESSAGING_DEVICE_PATH && TmpDevPath->SubType == MSG_SATA_DP) {\r
130 //\r
131 // SATA\r
132 //\r
133 SataDevPath = ( SATA_DEVICE_PATH* )TmpDevPath;\r
134 Dev->SataPort = SataDevPath->HBAPortNumber;\r
135 Dev->SataPortMultiplierPort = SataDevPath->PortMultiplierPortNumber;\r
136 Dev->DeviceType = OPAL_DEVICE_TYPE_SATA;\r
137 break;\r
138 } else if (TmpDevPath->Type == MESSAGING_DEVICE_PATH && TmpDevPath->SubType == MSG_NVME_NAMESPACE_DP) {\r
139 //\r
140 // NVMe\r
141 //\r
142 NvmeDevPath = ( NVME_NAMESPACE_DEVICE_PATH* )TmpDevPath;\r
143 Dev->NvmeNamespaceId = NvmeDevPath->NamespaceId;\r
144 Dev->DeviceType = OPAL_DEVICE_TYPE_NVME;\r
145 break;\r
146 }\r
147 TmpDevPath = NextDevicePathNode (TmpDevPath);\r
148 }\r
149\r
150 //\r
151 // Get bridge node info for the nvme device.\r
152 //\r
153 BusNum = 0;\r
154 TmpDevPath = DevicePath;\r
155 TmpDevPath2 = NextDevicePathNode (DevicePath);\r
156 while (!IsDevicePathEnd(TmpDevPath2)) {\r
157 if (TmpDevPath->Type == HARDWARE_DEVICE_PATH && TmpDevPath->SubType == HW_PCI_DP) {\r
158 PciDevPath = (PCI_DEVICE_PATH *) TmpDevPath;\r
159 if ((TmpDevPath2->Type == MESSAGING_DEVICE_PATH && TmpDevPath2->SubType == MSG_NVME_NAMESPACE_DP)||\r
160 (TmpDevPath2->Type == MESSAGING_DEVICE_PATH && TmpDevPath2->SubType == MSG_SATA_DP)) {\r
161 Dev->BusNum = (UINT32)BusNum;\r
162 Dev->DevNum = PciDevPath->Device;\r
163 Dev->FuncNum = PciDevPath->Function;\r
164 } else {\r
165 AddPciDeviceNode((UINT32)BusNum, PciDevPath->Device, PciDevPath->Function, Dev);\r
166 if (TmpDevPath2->Type == HARDWARE_DEVICE_PATH && TmpDevPath2->SubType == HW_PCI_DP) {\r
167 BusNum = PciRead8 (PCI_LIB_ADDRESS (BusNum, PciDevPath->Device, PciDevPath->Function, NVME_PCIE_SEC_BNUM));\r
168 }\r
169 }\r
170 }\r
171\r
172 TmpDevPath = NextDevicePathNode (TmpDevPath);\r
173 TmpDevPath2 = NextDevicePathNode (TmpDevPath2);\r
174 }\r
175}\r
176\r
177/**\r
178\r
179 The function returns whether or not the device is Opal Locked.\r
180 TRUE means that the device is partially or fully locked.\r
181 This will perform a Level 0 Discovery and parse the locking feature descriptor\r
182\r
69cd1294
ED
183 @param[in] OpalDev Opal object to determine if locked\r
184 @param[out] BlockSidSupported Whether device support BlockSid feature.\r
cb274a27
ED
185\r
186**/\r
187BOOLEAN\r
188IsOpalDeviceLocked(\r
69cd1294
ED
189 OPAL_SMM_DEVICE *OpalDev,\r
190 BOOLEAN *BlockSidSupported\r
cb274a27
ED
191 )\r
192{\r
193 OPAL_SESSION Session;\r
194 OPAL_DISK_SUPPORT_ATTRIBUTE SupportedAttributes;\r
195 TCG_LOCKING_FEATURE_DESCRIPTOR LockingFeature;\r
196 UINT16 OpalBaseComId;\r
197 TCG_RESULT Ret;\r
198\r
199 Session.Sscp = &OpalDev->Sscp;\r
200 Session.MediaId = 0;\r
201\r
202 Ret = OpalGetSupportedAttributesInfo (&Session, &SupportedAttributes, &OpalBaseComId);\r
203 if (Ret != TcgResultSuccess) {\r
204 return FALSE;\r
205 }\r
206\r
207 OpalDev->OpalBaseComId = OpalBaseComId;\r
69cd1294
ED
208 Session.OpalBaseComId = OpalBaseComId;\r
209 *BlockSidSupported = SupportedAttributes.BlockSid == 1 ? TRUE : FALSE;\r
cb274a27
ED
210\r
211 Ret = OpalGetLockingInfo(&Session, &LockingFeature);\r
212 if (Ret != TcgResultSuccess) {\r
213 return FALSE;\r
214 }\r
215\r
216 return OpalDeviceLocked (&SupportedAttributes, &LockingFeature);\r
217}\r
218\r
219/**\r
220 Save/Restore RootPort configuration space.\r
221\r
222 @param[in] DeviceNode - The device node.\r
223 @param[in] SaveAction - TRUE: Save, FALSE: Restore\r
224 @param[in,out] PcieConfBufferList - Configuration space data buffer for save/restore\r
225\r
226 @retval - PCIE base address of this RootPort\r
227**/\r
228UINTN\r
229SaveRestoreRootportConfSpace (\r
230 IN OPAL_SMM_DEVICE *DeviceNode,\r
231 IN BOOLEAN SaveAction,\r
232 IN OUT UINT8 **PcieConfBufferList\r
233 )\r
234{\r
235 UINTN RpBase;\r
236 UINTN Length;\r
237 PCI_DEVICE *DevNode;\r
238 UINT8 *StorePcieConfData;\r
239 UINTN Index;\r
240\r
241 Length = 0;\r
242 Index = 0;\r
243 RpBase = 0;\r
244\r
245 while (Length < DeviceNode->Length) {\r
246 DevNode = (PCI_DEVICE *)((UINT8*)DeviceNode->PciBridgeNode + Length);\r
247 RpBase = PCI_LIB_ADDRESS (DevNode->BusNum, DevNode->DevNum, DevNode->FuncNum, 0x0);\r
248\r
249 if (PcieConfBufferList != NULL) {\r
250 if (SaveAction) {\r
251 StorePcieConfData = (UINT8 *) AllocateZeroPool (OPAL_PCIE_ROOTPORT_SAVESIZE);\r
252 ASSERT (StorePcieConfData != NULL);\r
253 OpalPciRead (StorePcieConfData, RpBase, OPAL_PCIE_ROOTPORT_SAVESIZE);\r
254 PcieConfBufferList[Index] = StorePcieConfData;\r
255 } else {\r
256 // Skip PCIe Command & Status registers\r
257 StorePcieConfData = PcieConfBufferList[Index];\r
258 OpalPciWrite (RpBase, StorePcieConfData, 4);\r
259 OpalPciWrite (RpBase + 8, StorePcieConfData + 8, OPAL_PCIE_ROOTPORT_SAVESIZE - 8);\r
260\r
261 FreePool (StorePcieConfData);\r
262 }\r
263 }\r
264\r
265 Length += sizeof (PCI_DEVICE);\r
266 Index ++;\r
267 }\r
268\r
269 return RpBase;\r
270}\r
271\r
272/**\r
273 Configure RootPort for downstream PCIe NAND devices.\r
274\r
275 @param[in] RpBase - PCIe configuration space address of this RootPort\r
276 @param[in] BusNumber - Bus number\r
277 @param[in] MemoryBase - Memory base address\r
278 @param[in] MemoryLength - Memory size\r
279\r
280**/\r
281VOID\r
282ConfigureRootPortForPcieNand (\r
283 IN UINTN RpBase,\r
284 IN UINTN BusNumber,\r
285 IN UINT32 MemoryBase,\r
286 IN UINT32 MemoryLength\r
287 )\r
288{\r
289 UINT32 MemoryLimit;\r
290\r
291 DEBUG ((DEBUG_INFO, "ConfigureRootPortForPcieNand, BusNumber: %x, MemoryBase: %x, MemoryLength: %x\n",\r
292 BusNumber, MemoryBase, MemoryLength));\r
293\r
294 if (MemoryLength == 0) {\r
295 MemoryLimit = MemoryBase;\r
296 } else {\r
297 MemoryLimit = MemoryBase + MemoryLength + 0xFFFFF; // 1M\r
298 }\r
299\r
300 ///\r
301 /// Configue PCIE configuration space for RootPort\r
302 ///\r
303 PciWrite8 (RpBase + NVME_PCIE_BNUM + 1, (UINT8) BusNumber); // Secondary Bus Number registers\r
304 PciWrite8 (RpBase + NVME_PCIE_BNUM + 2, (UINT8) BusNumber); // Subordinate Bus Number registers\r
305 PciWrite8 (RpBase + NVME_PCIE_IOBL, 0xFF); // I/O Base registers\r
306 PciWrite8 (RpBase + NVME_PCIE_IOBL + 1, 0x00); // I/O Limit registers\r
307 PciWrite16 (RpBase + NVME_PCIE_MBL, (UINT16) RShiftU64 ((UINTN)MemoryBase, 16)); // Memory Base register\r
308 PciWrite16 (RpBase + NVME_PCIE_MBL + 2, (UINT16) RShiftU64 ((UINTN)MemoryLimit, 16)); // Memory Limit register\r
309 PciWrite16 (RpBase + NVME_PCIE_PMBL, 0xFFFF); // Prefetchable Memory Base registers\r
310 PciWrite16 (RpBase + NVME_PCIE_PMBL + 2, 0x0000); // Prefetchable Memory Limit registers\r
311 PciWrite32 (RpBase + NVME_PCIE_PMBU32, 0xFFFFFFFF); // Prefetchable Memory Upper Base registers\r
312 PciWrite32 (RpBase + NVME_PCIE_PMLU32, 0x00000000); // Prefetchable Memory Upper Limit registers\r
313}\r
314\r
315\r
316/**\r
317 Dispatch function for a Software SMI handler.\r
318\r
319 @param[in] DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().\r
320 @param[in] RegisterContext Points to an optional handler context which was specified when the\r
321 handler was registered.\r
322 @param[in, out] CommBuffer A pointer to a collection of Data in memory that will\r
323 be conveyed from a non-SMM environment into an SMM environment.\r
324 @param[in, out] CommBufferSize The Size of the CommBuffer.\r
325\r
326 @retval EFI_SUCCESS The interrupt was handled and quiesced. No other handlers\r
327 should still be called.\r
328 @retval Others Other execution results.\r
329**/\r
330EFI_STATUS\r
331EFIAPI\r
332SmmUnlockOpalPassword (\r
333 IN EFI_HANDLE DispatchHandle,\r
334 IN CONST VOID *RegisterContext,\r
335 IN OUT VOID *CommBuffer,\r
336 IN OUT UINTN *CommBufferSize\r
337 )\r
338{\r
339 EFI_STATUS Status;\r
340 OPAL_SMM_DEVICE *OpalDev;\r
341 LIST_ENTRY *Entry;\r
342 UINT8 BaseClassCode;\r
343 UINT8 SubClassCode;\r
344 UINT8 ProgInt;\r
345 TCG_RESULT Result;\r
346 UINT8 SataCmdSt;\r
347 UINT8 *StorePcieConfDataList[16];\r
348 UINTN RpBase;\r
349 UINTN MemoryBase;\r
350 UINTN MemoryLength;\r
351 OPAL_SESSION Session;\r
69cd1294 352 BOOLEAN BlockSidSupport;\r
cb274a27
ED
353\r
354 ZeroMem (StorePcieConfDataList, sizeof (StorePcieConfDataList));\r
355 Status = EFI_DEVICE_ERROR;\r
356\r
357 //\r
358 // try to unlock all locked hdd disks.\r
359 //\r
360 for (Entry = mSmmDeviceList.ForwardLink; Entry != &mSmmDeviceList; Entry = Entry->ForwardLink) {\r
361 OpalDev = BASE_CR(Entry, OPAL_SMM_DEVICE, Link);\r
362\r
363 RpBase = 0;\r
364 SataCmdSt = 0;\r
365\r
366 ///\r
367 /// Configure RootPort for PCIe AHCI/NVME devices.\r
368 ///\r
369 if (OpalDev->DeviceType == OPAL_DEVICE_TYPE_NVME) {\r
370 ///\r
371 /// Save original RootPort configuration space to heap\r
372 ///\r
373 RpBase = SaveRestoreRootportConfSpace (\r
374 OpalDev,\r
375 TRUE,\r
376 StorePcieConfDataList\r
377 );\r
378 MemoryBase = mNvmeContext.Nbar;\r
379 MemoryLength = 0;\r
380 ConfigureRootPortForPcieNand (RpBase, OpalDev->BusNum, (UINT32) MemoryBase, (UINT32) MemoryLength);\r
381\r
382 ///\r
383 /// Enable PCIE decode for RootPort\r
384 ///\r
385 SataCmdSt = PciRead8 (RpBase + NVME_PCIE_PCICMD);\r
386 PciWrite8 (RpBase + NVME_PCIE_PCICMD, 0x6);\r
387 } else {\r
388 SataCmdSt = PciRead8 (PCI_LIB_ADDRESS (OpalDev->BusNum, OpalDev->DevNum, OpalDev->FuncNum, NVME_PCIE_PCICMD));\r
389 PciWrite8 (PCI_LIB_ADDRESS (OpalDev->BusNum, OpalDev->DevNum, OpalDev->FuncNum, NVME_PCIE_PCICMD), 0x6);\r
390 }\r
391\r
392 BaseClassCode = PciRead8 (PCI_LIB_ADDRESS (OpalDev->BusNum, OpalDev->DevNum, OpalDev->FuncNum, 0x0B));\r
393 SubClassCode = PciRead8 (PCI_LIB_ADDRESS (OpalDev->BusNum, OpalDev->DevNum, OpalDev->FuncNum, 0x0A));\r
394 ProgInt = PciRead8 (PCI_LIB_ADDRESS (OpalDev->BusNum, OpalDev->DevNum, OpalDev->FuncNum, 0x09));\r
395 if (BaseClassCode != PCI_CLASS_MASS_STORAGE) {\r
396 Status = EFI_INVALID_PARAMETER;\r
397 break;\r
398 }\r
399\r
400 Status = EFI_DEVICE_ERROR;\r
401 if (OpalDev->DeviceType == OPAL_DEVICE_TYPE_SATA) {\r
402 if ((SubClassCode == PCI_CLASS_MASS_STORAGE_AHCI) || (SubClassCode == PCI_CLASS_MASS_STORAGE_RAID)) {\r
403 Status = GetAhciBaseAddress (OpalDev->BusNum, OpalDev->DevNum, OpalDev->FuncNum);\r
404 if (EFI_ERROR (Status)) {\r
405 DEBUG ((DEBUG_ERROR, "GetAhciBaseAddress error, Status: %r\n", Status));\r
406 goto done;\r
407 }\r
408 Status = AhciModeInitialize ((UINT8)OpalDev->SataPort);\r
409 ASSERT_EFI_ERROR (Status);\r
410 if (EFI_ERROR (Status)) {\r
411 DEBUG ((DEBUG_ERROR, "AhciModeInitialize error, Status: %r\n", Status));\r
412 goto done;\r
413 }\r
414 } else {\r
415 DEBUG ((DEBUG_ERROR, "SubClassCode not support for SATA device\n"));\r
416 }\r
417 } else if (OpalDev->DeviceType == OPAL_DEVICE_TYPE_NVME) {\r
418 if (SubClassCode == PCI_CLASS_MASS_STORAGE_NVM) {\r
419 if (ProgInt != PCI_IF_NVMHCI) {\r
420 DEBUG ((DEBUG_ERROR, "PI not support, skipped\n"));\r
421 Status = EFI_NOT_FOUND;\r
422 goto done;\r
423 }\r
424\r
425 mNvmeContext.PciBase = PCI_LIB_ADDRESS (OpalDev->BusNum, OpalDev->DevNum, OpalDev->FuncNum, 0x0);\r
426 mNvmeContext.NvmeInitWaitTime = 0;\r
427 mNvmeContext.Nsid = OpalDev->NvmeNamespaceId;\r
428 Status = NvmeControllerInit (&mNvmeContext);\r
429 } else {\r
430 DEBUG ((DEBUG_ERROR, "SubClassCode not support for NVME device\n"));\r
431 }\r
432 } else {\r
433 DEBUG ((DEBUG_ERROR, "Invalid Devicetype\n"));\r
434 goto done;\r
435 }\r
436\r
437 Status = EFI_DEVICE_ERROR;\r
69cd1294
ED
438 BlockSidSupport = FALSE;\r
439 if (IsOpalDeviceLocked (OpalDev, &BlockSidSupport)) {\r
cb274a27
ED
440 ZeroMem(&Session, sizeof(Session));\r
441 Session.Sscp = &OpalDev->Sscp;\r
442 Session.MediaId = 0;\r
443 Session.OpalBaseComId = OpalDev->OpalBaseComId;\r
444\r
cb274a27
ED
445 Result = OpalSupportUnlock (&Session, OpalDev->Password, OpalDev->PasswordLength, NULL);\r
446 if (Result == TcgResultSuccess) {\r
447 Status = EFI_SUCCESS;\r
448 }\r
449 }\r
450\r
3f250a94
ED
451 if (mSendBlockSID && BlockSidSupport) {\r
452 Result = OpalBlockSid (&Session, TRUE);\r
453 if (Result != TcgResultSuccess) {\r
454 break;\r
455 }\r
456 }\r
457\r
cb274a27
ED
458 if (OpalDev->DeviceType == OPAL_DEVICE_TYPE_NVME) {\r
459 if (SubClassCode == PCI_CLASS_MASS_STORAGE_NVM) {\r
460 Status = NvmeControllerExit (&mNvmeContext);\r
461 }\r
462 }\r
463\r
464done:\r
465 if (OpalDev->DeviceType == OPAL_DEVICE_TYPE_NVME) {\r
466 ASSERT (RpBase != 0);\r
467 PciWrite8 (RpBase + NVME_PCIE_PCICMD, 0);\r
468 RpBase = SaveRestoreRootportConfSpace (\r
469 OpalDev,\r
470 FALSE, // restore\r
471 StorePcieConfDataList\r
472 );\r
473 PciWrite8 (RpBase + NVME_PCIE_PCICMD, SataCmdSt);\r
474 } else {\r
475 PciWrite8 (PCI_LIB_ADDRESS (OpalDev->BusNum, OpalDev->DevNum, OpalDev->FuncNum, NVME_PCIE_PCICMD), SataCmdSt);\r
476 }\r
477\r
478 if (EFI_ERROR (Status)) {\r
479 break;\r
480 }\r
481 }\r
482\r
483 return Status;\r
484}\r
485\r
0acd8df4
ED
486/**\r
487 The function extracts device information from OpalDeviceList and creat SmmDeviceList used for S3.\r
488\r
489 @param[in] OpalDeviceList Opal device list created at POST which contains the information of OPAL_DISK_AND_PASSWORD_INFO\r
490 @param[in,out] SmmDeviceList Opal Smm device list to be created and used for unlocking devices at S3 resume.\r
491\r
492 @retval EFI_SUCCESS Create SmmDeviceList successfully.\r
493 @retval Others Other execution results.\r
494**/\r
495EFI_STATUS\r
496CreateSmmDeviceList (\r
497 IN LIST_ENTRY *OpalDeviceList,\r
498 IN OUT LIST_ENTRY *SmmDeviceList\r
499 )\r
500{\r
501 LIST_ENTRY *Entry;\r
502 OPAL_DISK_AND_PASSWORD_INFO *PciDev;\r
503 OPAL_SMM_DEVICE *SmmDev;\r
504\r
505 for (Entry = OpalDeviceList->ForwardLink; Entry != OpalDeviceList; Entry = Entry->ForwardLink) {\r
506 PciDev = BASE_CR (Entry, OPAL_DISK_AND_PASSWORD_INFO, Link);\r
507\r
508 SmmDev = AllocateZeroPool (sizeof (OPAL_SMM_DEVICE));\r
509 if (SmmDev == NULL) {\r
510 return EFI_OUT_OF_RESOURCES;\r
511 }\r
512 SmmDev->Signature = OPAL_SMM_DEVICE_SIGNATURE;\r
513\r
514 ExtractDeviceInfoFromDevicePath(&PciDev->OpalDevicePath, SmmDev);\r
515\r
516 SmmDev->PasswordLength = PciDev->PasswordLength;\r
517 CopyMem(&(SmmDev->Password), PciDev->Password, OPAL_PASSWORD_MAX_LENGTH);\r
518\r
519 SmmDev->Sscp.ReceiveData = SecurityReceiveData;\r
520 SmmDev->Sscp.SendData = SecuritySendData;\r
521\r
522 DEBUG ((DEBUG_INFO, "Opal SMM: Insert device node to SmmDeviceList:\n"));\r
523 DEBUG ((DEBUG_INFO, "DeviceType:%x, Bus:%d, Dev:%d, Fun:%d\n", \\r
524 SmmDev->DeviceType, SmmDev->BusNum, SmmDev->DevNum, SmmDev->FuncNum));\r
525 DEBUG ((DEBUG_INFO, "SataPort:%x, MultiplierPort:%x, NvmeNamespaceId:%x\n", \\r
526 SmmDev->SataPort, SmmDev->SataPortMultiplierPort, SmmDev->NvmeNamespaceId));\r
527\r
528 InsertHeadList (SmmDeviceList, &SmmDev->Link);\r
529 }\r
530\r
531 return EFI_SUCCESS;\r
532}\r
cb274a27
ED
533\r
534/**\r
535 Main entry point for an SMM handler dispatch or communicate-based callback.\r
536\r
537 @param[in] DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().\r
538 @param[in] Context Points to an optional handler context which was specified when the\r
539 handler was registered.\r
540 @param[in,out] CommBuffer A pointer to a collection of Data in memory that will\r
541 be conveyed from a non-SMM environment into an SMM environment.\r
542 @param[in,out] CommBufferSize The Size of the CommBuffer.\r
543\r
544 @retval EFI_SUCCESS The interrupt was handled and quiesced. No other handlers\r
545 should still be called.\r
546 @retval EFI_WARN_INTERRUPT_SOURCE_QUIESCED The interrupt has been quiesced but other handlers should\r
547 still be called.\r
548 @retval EFI_WARN_INTERRUPT_SOURCE_PENDING The interrupt is still pending and other handlers should still\r
549 be called.\r
550 @retval EFI_INTERRUPT_PENDING The interrupt could not be quiesced.\r
551**/\r
552EFI_STATUS\r
553EFIAPI\r
554S3SleepEntryCallBack (\r
555 IN EFI_HANDLE DispatchHandle,\r
556 IN CONST VOID *Context OPTIONAL,\r
557 IN OUT VOID *CommBuffer OPTIONAL,\r
558 IN OUT UINTN *CommBufferSize OPTIONAL\r
559 )\r
560{\r
561 UINTN Bus;\r
562 UINTN Device;\r
563 UINTN Function;\r
564 UINTN Index;\r
565 EFI_STATUS Status;\r
566 LIST_ENTRY *Entry;\r
567 UINTN Offset;\r
568 UINT64 Address;\r
569 S3_BOOT_SCRIPT_LIB_WIDTH Width;\r
570 UINT32 Data;\r
cb274a27
ED
571 OPAL_HC_PCI_REGISTER_SAVE *HcRegisterSaveListPtr;\r
572 UINTN Count;\r
573 OPAL_SMM_DEVICE *SmmDev;\r
574\r
575 Data = 0;\r
576 Status = EFI_SUCCESS;\r
577\r
578 mOpalDeviceList = OpalSupportGetOpalDeviceList();\r
0acd8df4
ED
579 if (IsListEmpty (mOpalDeviceList)) {\r
580 //\r
581 // No Opal enabled device. Do nothing.\r
582 //\r
583 return EFI_SUCCESS;\r
584 }\r
cb274a27 585\r
0acd8df4
ED
586 if (IsListEmpty (&mSmmDeviceList)) {\r
587 //\r
588 // mSmmDeviceList for S3 is empty, creat it by mOpalDeviceList.\r
589 //\r
590 Status = CreateSmmDeviceList (mOpalDeviceList, &mSmmDeviceList);\r
591 if (EFI_ERROR (Status)) {\r
592 return Status;\r
cb274a27 593 }\r
0acd8df4 594 }\r
cb274a27 595\r
0acd8df4
ED
596 //\r
597 // Go through SmmDeviceList to save register data for S3\r
598 //\r
599 for (Entry = mSmmDeviceList.ForwardLink; Entry != &mSmmDeviceList; Entry = Entry->ForwardLink) {\r
600 SmmDev = BASE_CR (Entry, OPAL_SMM_DEVICE, Link);\r
cb274a27
ED
601\r
602 if (SmmDev->DeviceType == OPAL_DEVICE_TYPE_NVME) {\r
603 continue;\r
604 }\r
605\r
606 //\r
607 // Save register Data for S3. Sata controller only.\r
608 //\r
609 Bus = SmmDev->BusNum;\r
610 Device = SmmDev->DevNum;\r
611 Function = SmmDev->FuncNum;\r
612\r
613 ASSERT (SmmDev->DeviceType == OPAL_DEVICE_TYPE_SATA);\r
614 HcRegisterSaveListPtr = (OPAL_HC_PCI_REGISTER_SAVE *) mSataHcRegisterSaveTemplate;\r
615 Count = sizeof (mSataHcRegisterSaveTemplate) / sizeof (OPAL_HC_PCI_REGISTER_SAVE);\r
616\r
617 for (Index = 0; Index < Count; Index += 1) {\r
618 Offset = HcRegisterSaveListPtr[Index].Address;\r
619 Width = HcRegisterSaveListPtr[Index].Width;\r
620\r
621 switch (Width) {\r
622 case S3BootScriptWidthUint8:\r
623 Data = (UINT32)PciRead8 (PCI_LIB_ADDRESS(Bus,Device,Function,Offset));\r
624 break;\r
625 case S3BootScriptWidthUint16:\r
626 Data = (UINT32)PciRead16 (PCI_LIB_ADDRESS(Bus,Device,Function,Offset));\r
627 break;\r
628 case S3BootScriptWidthUint32:\r
629 Data = PciRead32 (PCI_LIB_ADDRESS(Bus,Device,Function,Offset));\r
630 break;\r
631 default:\r
632 ASSERT (FALSE);\r
633 break;\r
634 }\r
635\r
636 Address = S3_BOOT_SCRIPT_LIB_PCI_ADDRESS (Bus, Device, Function, Offset);\r
637 Status = S3BootScriptSavePciCfgWrite (Width, Address, 1, &Data);\r
638 if (EFI_ERROR (Status)) {\r
639 return Status;\r
640 }\r
641 }\r
642 }\r
643\r
0acd8df4
ED
644 Status = S3BootScriptSaveIoWrite (S3BootScriptWidthUint8, 0xB2, 1, &mSwSmiValue);\r
645 ASSERT_EFI_ERROR (Status);\r
cb274a27
ED
646\r
647 return Status;\r
648}\r
649\r
009264f5
ED
650/**\r
651 OpalPassword Notification for SMM EndOfDxe protocol.\r
652\r
653 @param[in] Protocol Points to the protocol's unique identifier.\r
654 @param[in] Interface Points to the interface instance.\r
655 @param[in] Handle The handle on which the interface was installed.\r
656\r
657 @retval EFI_SUCCESS Notification runs successfully.\r
658**/\r
659EFI_STATUS\r
660EFIAPI\r
661OpalPasswordEndOfDxeNotification (\r
662 IN CONST EFI_GUID *Protocol,\r
663 IN VOID *Interface,\r
664 IN EFI_HANDLE Handle\r
665 )\r
666{\r
667 UINTN NumberOfDescriptors;\r
668 EFI_GCD_MEMORY_SPACE_DESCRIPTOR *MemSpaceMap;\r
669 EFI_STATUS Status;\r
670\r
671 Status = gDS->GetMemorySpaceMap (&NumberOfDescriptors, &MemSpaceMap);\r
672 if (EFI_ERROR (Status)) {\r
673 return Status;\r
674 }\r
675\r
676 mGcdMemSpace = AllocateCopyPool (NumberOfDescriptors * sizeof (EFI_GCD_MEMORY_SPACE_DESCRIPTOR), MemSpaceMap);\r
677 if (EFI_ERROR (Status)) {\r
678 gBS->FreePool (MemSpaceMap);\r
679 return Status;\r
680 }\r
681\r
682 mNumberOfDescriptors = NumberOfDescriptors;\r
683 gBS->FreePool (MemSpaceMap);\r
684\r
685 return EFI_SUCCESS;\r
686}\r
687\r
cb274a27
ED
688/**\r
689 Main entry for this driver.\r
690\r
691 @param ImageHandle Image handle this driver.\r
692 @param SystemTable Pointer to SystemTable.\r
693\r
694 @retval EFI_SUCESS This function always complete successfully.\r
695\r
696**/\r
697EFI_STATUS\r
698EFIAPI\r
699OpalPasswordSmmInit (\r
700 IN EFI_HANDLE ImageHandle,\r
701 IN EFI_SYSTEM_TABLE *SystemTable\r
702 )\r
703{\r
704 EFI_STATUS Status;\r
705 EFI_SMM_SW_DISPATCH2_PROTOCOL *SwDispatch;\r
706 EFI_SMM_SX_DISPATCH2_PROTOCOL *SxDispatch;\r
707 EFI_HANDLE SwHandle;\r
708 EFI_SMM_SW_REGISTER_CONTEXT Context;\r
709 EFI_HANDLE S3SleepEntryHandle;\r
710 EFI_SMM_SX_REGISTER_CONTEXT EntryRegisterContext;\r
711 EFI_SMM_VARIABLE_PROTOCOL *SmmVariable;\r
712 OPAL_EXTRA_INFO_VAR OpalExtraInfo;\r
713 UINTN DataSize;\r
009264f5 714 EFI_EVENT EndOfDxeEvent;\r
cb274a27
ED
715 EFI_PHYSICAL_ADDRESS Address;\r
716\r
717 mBuffer = NULL;\r
718 SwHandle = NULL;\r
719 S3SleepEntryHandle = NULL;\r
720 ZeroMem (&mNvmeContext, sizeof (NVME_CONTEXT));\r
721\r
722 Status = gSmst->SmmLocateProtocol (\r
723 &gEfiSmmSwDispatch2ProtocolGuid,\r
724 NULL,\r
725 (VOID **)&SwDispatch\r
726 );\r
727 ASSERT_EFI_ERROR (Status);\r
728 if (EFI_ERROR (Status)) {\r
729 DEBUG((DEBUG_ERROR, " SmmLocateProtocol gEfiSmmSwDispatch2ProtocolGuid fail, Status: %r\n", Status));\r
730 return Status;\r
731 }\r
732\r
733 Status = gSmst->SmmLocateProtocol (\r
734 &gEfiSmmSxDispatch2ProtocolGuid,\r
735 NULL,\r
736 (VOID **)&SxDispatch\r
737 );\r
738 ASSERT_EFI_ERROR (Status);\r
739 if (EFI_ERROR (Status)) {\r
740 DEBUG((DEBUG_ERROR, " SmmLocateProtocol gEfiSmmSxDispatch2ProtocolGuid fail, Status: %r\n", Status));\r
741 return Status;\r
742 }\r
743\r
744 //\r
745 // Preallocate a 512 bytes Buffer to perform trusted I/O.\r
746 // Assume this is big enough for unlock commands\r
747 // It's because DMA can not access smmram stack at the cmd execution.\r
748 //\r
749 Address = 0xFFFFFFFF;\r
750 Status = gBS->AllocatePages (\r
751 AllocateMaxAddress,\r
752 EfiACPIMemoryNVS,\r
753 EFI_SIZE_TO_PAGES (SMM_SIZE_ALLOC_BYTES),\r
754 &Address\r
755 );\r
756 if (EFI_ERROR (Status)) {\r
757 DEBUG((DEBUG_ERROR, " AllocatePages for SATA DAM fail, Status: %r\n", Status));\r
758 return EFI_OUT_OF_RESOURCES;\r
759 }\r
760\r
761 mBuffer = (VOID *)(UINTN)Address;\r
762 ZeroMem ((VOID *)(UINTN)mBuffer, SMM_SIZE_ALLOC_BYTES);\r
763\r
764 //\r
765 // Preallocate resource for AHCI transfer descriptor.\r
766 //\r
767 Status = AhciAllocateResource ();\r
768 if (EFI_ERROR (Status)) {\r
769 DEBUG((DEBUG_ERROR, " AhciAllocateResource fail, Status: %r\n", Status));\r
770 Status = EFI_OUT_OF_RESOURCES;\r
771 goto EXIT;\r
772 }\r
773\r
774 //\r
775 // Preallocate resource for NVMe configuration space.\r
776 //\r
777 Status = NvmeAllocateResource (ImageHandle, &mNvmeContext);\r
778 if (EFI_ERROR (Status)) {\r
779 DEBUG((DEBUG_ERROR, " NvmeAllocateResource fail, Status: %r\n", Status));\r
780 Status = EFI_OUT_OF_RESOURCES;\r
781 goto EXIT;\r
782 }\r
783\r
784 //\r
785 // Register a S3 entry callback function to store ATA host controller context to boot script.\r
786 // These boot scripts would be invoked at S3 path to recovery ATA host controller h/w context\r
787 // for executing HDD unlock cmd.\r
788 //\r
789 EntryRegisterContext.Type = SxS3;\r
790 EntryRegisterContext.Phase = SxEntry;\r
791 Status = SxDispatch->Register (\r
792 SxDispatch,\r
793 S3SleepEntryCallBack,\r
794 &EntryRegisterContext,\r
795 &S3SleepEntryHandle\r
796 );\r
797 ASSERT_EFI_ERROR (Status);\r
798 if (EFI_ERROR (Status)) {\r
799 goto EXIT;\r
800 }\r
801\r
802 //\r
803 // Register Opal password smm unlock handler\r
804 //\r
805 Context.SwSmiInputValue = (UINTN) -1;\r
806 Status = SwDispatch->Register (\r
807 SwDispatch,\r
808 SmmUnlockOpalPassword,\r
809 &Context,\r
810 &SwHandle\r
811 );\r
812 ASSERT_EFI_ERROR (Status);\r
813 if (EFI_ERROR (Status)) {\r
814 DEBUG((DEBUG_ERROR, " SwDispatch->Register fail, Status: %r\n", Status));\r
815 goto EXIT;\r
816 }\r
817\r
818 //\r
819 // trigger smi to unlock hdd if it's locked.\r
820 //\r
821 mSwSmiValue = (UINT8) Context.SwSmiInputValue;\r
822\r
009264f5
ED
823 //\r
824 // Create event to record GCD descriptors at end of dxe for judging AHCI/NVMe PCI Bar\r
825 // is in MMIO space to avoid attack.\r
826 //\r
827 Status = gSmst->SmmRegisterProtocolNotify (&gEfiSmmEndOfDxeProtocolGuid, OpalPasswordEndOfDxeNotification, &EndOfDxeEvent);\r
828 if (EFI_ERROR (Status)) {\r
829 DEBUG((DEBUG_ERROR, "OpalPasswordSmm: Register SmmEndOfDxe fail, Status: %r\n", Status));\r
830 goto EXIT;\r
831 }\r
cb274a27
ED
832 Status = gSmst->SmmLocateProtocol (&gEfiSmmVariableProtocolGuid, NULL, (VOID**)&SmmVariable);\r
833 if (!EFI_ERROR (Status)) {\r
834 DataSize = sizeof (OPAL_EXTRA_INFO_VAR);\r
835 Status = SmmVariable->SmmGetVariable (\r
836 OPAL_EXTRA_INFO_VAR_NAME,\r
837 &gOpalExtraInfoVariableGuid,\r
838 NULL,\r
839 &DataSize,\r
840 &OpalExtraInfo\r
841 );\r
842 if (!EFI_ERROR (Status)) {\r
843 mSendBlockSID = OpalExtraInfo.EnableBlockSid;\r
844 }\r
845 }\r
846\r
847 return EFI_SUCCESS;\r
848\r
849EXIT:\r
850 if (S3SleepEntryHandle != NULL) {\r
851 SxDispatch->UnRegister (SxDispatch, S3SleepEntryHandle);\r
852 }\r
853\r
854 AhciFreeResource ();\r
855\r
856 NvmeFreeResource (&mNvmeContext);\r
857\r
858 if (mBuffer != NULL) {\r
859 gBS->FreePages ((EFI_PHYSICAL_ADDRESS)(UINTN) mBuffer, EFI_SIZE_TO_PAGES (SMM_SIZE_ALLOC_BYTES));\r
860 }\r
861\r
862 return Status;\r
863}\r
864\r
865/**\r
866 Provide Io action support.\r
867\r
868 @param[in] SmmDev the opal device need to perform trust io.\r
869 @param[in] IoType OPAL_IO_TYPE indicating whether to perform a Trusted Send or Trusted Receive.\r
870 @param[in] SecurityProtocol Security Protocol\r
871 @param[in] SpSpecific Security Protocol Specific\r
872 @param[in] TransferLength Transfer Length of Buffer (in bytes) - always a multiple of 512\r
873 @param[in] Buffer Address of Data to transfer\r
874\r
875 @retval TcgResultSuccess Perform the io action success.\r
876 @retval TcgResultFailure Perform the io action failed.\r
877\r
878**/\r
879EFI_STATUS\r
880PerformTrustedIo (\r
881 OPAL_SMM_DEVICE *SmmDev,\r
882 OPAL_IO_TYPE IoType,\r
883 UINT8 SecurityProtocol,\r
884 UINT16 SpSpecific,\r
885 UINTN TransferLength,\r
886 VOID *Buffer\r
887 )\r
888{\r
889 EFI_STATUS Status;\r
890 UINTN BufferSizeBlocks;\r
891 EFI_ATA_COMMAND_BLOCK AtaCommandBlock;\r
892\r
893 Status = EFI_DEVICE_ERROR;\r
894 if (SmmDev->DeviceType == OPAL_DEVICE_TYPE_SATA) {\r
895 BufferSizeBlocks = TransferLength / 512;\r
896\r
897 ZeroMem( &AtaCommandBlock, sizeof( EFI_ATA_COMMAND_BLOCK ) );\r
898 AtaCommandBlock.AtaCommand = ( IoType == OpalSend ) ? ATA_COMMAND_TRUSTED_SEND : ATA_COMMAND_TRUSTED_RECEIVE;\r
899 AtaCommandBlock.AtaSectorCount = ( UINT8 )BufferSizeBlocks;\r
900 AtaCommandBlock.AtaSectorNumber = ( UINT8 )( BufferSizeBlocks >> 8 );\r
901 AtaCommandBlock.AtaFeatures = SecurityProtocol;\r
902 AtaCommandBlock.AtaCylinderLow = ( UINT8 )( SpSpecific >> 8 );\r
903 AtaCommandBlock.AtaCylinderHigh = ( UINT8 )( SpSpecific );\r
904 AtaCommandBlock.AtaDeviceHead = ATA_DEVICE_LBA;\r
905\r
906\r
907 ZeroMem( mBuffer, HDD_PAYLOAD );\r
908 ASSERT( TransferLength <= HDD_PAYLOAD );\r
909\r
910 if (IoType == OpalSend) {\r
911 CopyMem( mBuffer, Buffer, TransferLength );\r
912 }\r
913\r
914 Status = AhciPioTransfer(\r
915 &mAhciRegisters,\r
916 (UINT8) SmmDev->SataPort,\r
917 (UINT8) SmmDev->SataPortMultiplierPort,\r
918 NULL,\r
919 0,\r
920 ( IoType == OpalSend ) ? FALSE : TRUE, // i/o direction\r
921 &AtaCommandBlock,\r
922 NULL,\r
923 mBuffer,\r
924 (UINT32)TransferLength,\r
925 ATA_TIMEOUT\r
926 );\r
927\r
928 if (IoType == OpalRecv) {\r
929 CopyMem( Buffer, mBuffer, TransferLength );\r
930 }\r
931 } else if (SmmDev->DeviceType == OPAL_DEVICE_TYPE_NVME) {\r
932 Status = NvmeSecuritySendReceive (\r
933 &mNvmeContext,\r
934 IoType == OpalSend,\r
935 SecurityProtocol,\r
936 SwapBytes16(SpSpecific),\r
937 TransferLength,\r
938 Buffer\r
939 );\r
940 } else {\r
941 DEBUG((DEBUG_ERROR, "DeviceType(%x) not support.\n", SmmDev->DeviceType));\r
942 }\r
943\r
944 return Status;\r
945}\r
946\r
947/**\r
948 Send a security protocol command to a device that receives data and/or the result\r
949 of one or more commands sent by SendData.\r
950\r
951 The ReceiveData function sends a security protocol command to the given MediaId.\r
952 The security protocol command sent is defined by SecurityProtocolId and contains\r
953 the security protocol specific data SecurityProtocolSpecificData. The function\r
954 returns the data from the security protocol command in PayloadBuffer.\r
955\r
956 For devices supporting the SCSI command set, the security protocol command is sent\r
957 using the SECURITY PROTOCOL IN command defined in SPC-4.\r
958\r
959 For devices supporting the ATA command set, the security protocol command is sent\r
960 using one of the TRUSTED RECEIVE commands defined in ATA8-ACS if PayloadBufferSize\r
961 is non-zero.\r
962\r
963 If the PayloadBufferSize is zero, the security protocol command is sent using the\r
964 Trusted Non-Data command defined in ATA8-ACS.\r
965\r
966 If PayloadBufferSize is too small to store the available data from the security\r
967 protocol command, the function shall copy PayloadBufferSize bytes into the\r
968 PayloadBuffer and return EFI_WARN_BUFFER_TOO_SMALL.\r
969\r
970 If PayloadBuffer or PayloadTransferSize is NULL and PayloadBufferSize is non-zero,\r
971 the function shall return EFI_INVALID_PARAMETER.\r
972\r
973 If the given MediaId does not support security protocol commands, the function shall\r
974 return EFI_UNSUPPORTED. If there is no media in the device, the function returns\r
975 EFI_NO_MEDIA. If the MediaId is not the ID for the current media in the device,\r
976 the function returns EFI_MEDIA_CHANGED.\r
977\r
978 If the security protocol fails to complete within the Timeout period, the function\r
979 shall return EFI_TIMEOUT.\r
980\r
981 If the security protocol command completes without an error, the function shall\r
982 return EFI_SUCCESS. If the security protocol command completes with an error, the\r
983 function shall return EFI_DEVICE_ERROR.\r
984\r
985 @param This Indicates a pointer to the calling context.\r
986 @param MediaId ID of the medium to receive data from.\r
987 @param Timeout The timeout, in 100ns units, to use for the execution\r
988 of the security protocol command. A Timeout value of 0\r
989 means that this function will wait indefinitely for the\r
990 security protocol command to execute. If Timeout is greater\r
991 than zero, then this function will return EFI_TIMEOUT\r
992 if the time required to execute the receive data command\r
993 is greater than Timeout.\r
994 @param SecurityProtocolId The value of the "Security Protocol" parameter of\r
995 the security protocol command to be sent.\r
996 @param SecurityProtocolSpecificData The value of the "Security Protocol Specific" parameter\r
997 of the security protocol command to be sent.\r
998 @param PayloadBufferSize Size in bytes of the payload data buffer.\r
999 @param PayloadBuffer A pointer to a destination buffer to store the security\r
1000 protocol command specific payload data for the security\r
1001 protocol command. The caller is responsible for having\r
1002 either implicit or explicit ownership of the buffer.\r
1003 @param PayloadTransferSize A pointer to a buffer to store the size in bytes of the\r
1004 data written to the payload data buffer.\r
1005\r
1006 @retval EFI_SUCCESS The security protocol command completed successfully.\r
1007 @retval EFI_WARN_BUFFER_TOO_SMALL The PayloadBufferSize was too small to store the available\r
1008 data from the device. The PayloadBuffer contains the truncated data.\r
1009 @retval EFI_UNSUPPORTED The given MediaId does not support security protocol commands.\r
1010 @retval EFI_DEVICE_ERROR The security protocol command completed with an error.\r
1011 @retval EFI_NO_MEDIA There is no media in the device.\r
1012 @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.\r
1013 @retval EFI_INVALID_PARAMETER The PayloadBuffer or PayloadTransferSize is NULL and\r
1014 PayloadBufferSize is non-zero.\r
1015 @retval EFI_TIMEOUT A timeout occurred while waiting for the security\r
1016 protocol command to execute.\r
1017\r
1018**/\r
1019EFI_STATUS\r
1020EFIAPI\r
1021SecurityReceiveData (\r
1022 IN EFI_STORAGE_SECURITY_COMMAND_PROTOCOL *This,\r
1023 IN UINT32 MediaId,\r
1024 IN UINT64 Timeout,\r
1025 IN UINT8 SecurityProtocolId,\r
1026 IN UINT16 SecurityProtocolSpecificData,\r
1027 IN UINTN PayloadBufferSize,\r
1028 OUT VOID *PayloadBuffer,\r
1029 OUT UINTN *PayloadTransferSize\r
1030 )\r
1031{\r
1032 OPAL_SMM_DEVICE *SmmDev;\r
1033\r
1034 SmmDev = OPAL_SMM_DEVICE_FROM_THIS (This);\r
1035 if (SmmDev == NULL) {\r
1036 return EFI_DEVICE_ERROR;\r
1037 }\r
1038\r
1039 return PerformTrustedIo (\r
1040 SmmDev,\r
1041 OpalRecv,\r
1042 SecurityProtocolId,\r
1043 SecurityProtocolSpecificData,\r
1044 PayloadBufferSize,\r
1045 PayloadBuffer\r
1046 );\r
1047}\r
1048\r
1049/**\r
1050 Send a security protocol command to a device.\r
1051\r
1052 The SendData function sends a security protocol command containing the payload\r
1053 PayloadBuffer to the given MediaId. The security protocol command sent is\r
1054 defined by SecurityProtocolId and contains the security protocol specific data\r
1055 SecurityProtocolSpecificData. If the underlying protocol command requires a\r
1056 specific padding for the command payload, the SendData function shall add padding\r
1057 bytes to the command payload to satisfy the padding requirements.\r
1058\r
1059 For devices supporting the SCSI command set, the security protocol command is sent\r
1060 using the SECURITY PROTOCOL OUT command defined in SPC-4.\r
1061\r
1062 For devices supporting the ATA command set, the security protocol command is sent\r
1063 using one of the TRUSTED SEND commands defined in ATA8-ACS if PayloadBufferSize\r
1064 is non-zero. If the PayloadBufferSize is zero, the security protocol command is\r
1065 sent using the Trusted Non-Data command defined in ATA8-ACS.\r
1066\r
1067 If PayloadBuffer is NULL and PayloadBufferSize is non-zero, the function shall\r
1068 return EFI_INVALID_PARAMETER.\r
1069\r
1070 If the given MediaId does not support security protocol commands, the function\r
1071 shall return EFI_UNSUPPORTED. If there is no media in the device, the function\r
1072 returns EFI_NO_MEDIA. If the MediaId is not the ID for the current media in the\r
1073 device, the function returns EFI_MEDIA_CHANGED.\r
1074\r
1075 If the security protocol fails to complete within the Timeout period, the function\r
1076 shall return EFI_TIMEOUT.\r
1077\r
1078 If the security protocol command completes without an error, the function shall return\r
1079 EFI_SUCCESS. If the security protocol command completes with an error, the function\r
1080 shall return EFI_DEVICE_ERROR.\r
1081\r
1082 @param This Indicates a pointer to the calling context.\r
1083 @param MediaId ID of the medium to receive data from.\r
1084 @param Timeout The timeout, in 100ns units, to use for the execution\r
1085 of the security protocol command. A Timeout value of 0\r
1086 means that this function will wait indefinitely for the\r
1087 security protocol command to execute. If Timeout is greater\r
1088 than zero, then this function will return EFI_TIMEOUT\r
1089 if the time required to execute the send data command\r
1090 is greater than Timeout.\r
1091 @param SecurityProtocolId The value of the "Security Protocol" parameter of\r
1092 the security protocol command to be sent.\r
1093 @param SecurityProtocolSpecificData The value of the "Security Protocol Specific" parameter\r
1094 of the security protocol command to be sent.\r
1095 @param PayloadBufferSize Size in bytes of the payload data buffer.\r
1096 @param PayloadBuffer A pointer to a destination buffer to store the security\r
1097 protocol command specific payload data for the security\r
1098 protocol command.\r
1099\r
1100 @retval EFI_SUCCESS The security protocol command completed successfully.\r
1101 @retval EFI_UNSUPPORTED The given MediaId does not support security protocol commands.\r
1102 @retval EFI_DEVICE_ERROR The security protocol command completed with an error.\r
1103 @retval EFI_NO_MEDIA There is no media in the device.\r
1104 @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.\r
1105 @retval EFI_INVALID_PARAMETER The PayloadBuffer is NULL and PayloadBufferSize is non-zero.\r
1106 @retval EFI_TIMEOUT A timeout occurred while waiting for the security\r
1107 protocol command to execute.\r
1108\r
1109**/\r
1110EFI_STATUS\r
1111EFIAPI\r
1112SecuritySendData (\r
1113 IN EFI_STORAGE_SECURITY_COMMAND_PROTOCOL *This,\r
1114 IN UINT32 MediaId,\r
1115 IN UINT64 Timeout,\r
1116 IN UINT8 SecurityProtocolId,\r
1117 IN UINT16 SecurityProtocolSpecificData,\r
1118 IN UINTN PayloadBufferSize,\r
1119 IN VOID *PayloadBuffer\r
1120 )\r
1121{\r
1122 OPAL_SMM_DEVICE *SmmDev;\r
1123\r
1124 SmmDev = OPAL_SMM_DEVICE_FROM_THIS (This);\r
1125 if (SmmDev == NULL) {\r
1126 return EFI_DEVICE_ERROR;\r
1127 }\r
1128\r
1129 return PerformTrustedIo (\r
1130 SmmDev,\r
1131 OpalSend,\r
1132 SecurityProtocolId,\r
1133 SecurityProtocolSpecificData,\r
1134 PayloadBufferSize,\r
1135 PayloadBuffer\r
1136 );\r
1137\r
1138}\r
1139\r