+ Flush the Block Device.\r
+\r
+ @param This Indicates a pointer to the calling context.\r
+ @param Token A pointer to the token associated with the transaction.\r
+\r
+ @retval EFI_SUCCESS All outstanding data was written to the device.\r
+ @retval EFI_DEVICE_ERROR The device reported an error while attempting to\r
+ write data.\r
+ @retval EFI_WRITE_PROTECTED The device cannot be written to.\r
+ @retval EFI_NO_MEDIA There is no media in the device.\r
+ @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+ScsiDiskFlushBlocksEx (\r
+ IN EFI_BLOCK_IO2_PROTOCOL *This,\r
+ IN OUT EFI_BLOCK_IO2_TOKEN *Token\r
+ )\r
+{\r
+ SCSI_DISK_DEV *ScsiDiskDevice;\r
+ EFI_BLOCK_IO_MEDIA *Media;\r
+ EFI_STATUS Status;\r
+ BOOLEAN MediaChange;\r
+ EFI_TPL OldTpl;\r
+\r
+ MediaChange = FALSE;\r
+ OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
+ ScsiDiskDevice = SCSI_DISK_DEV_FROM_BLKIO2 (This);\r
+ Media = ScsiDiskDevice->BlkIo.Media;\r
+\r
+ if (!IS_DEVICE_FIXED (ScsiDiskDevice)) {\r
+ Status = ScsiDiskDetectMedia (ScsiDiskDevice, FALSE, &MediaChange);\r
+ if (EFI_ERROR (Status)) {\r
+ Status = EFI_DEVICE_ERROR;\r
+ goto Done;\r
+ }\r
+\r
+ if (MediaChange) {\r
+ gBS->ReinstallProtocolInterface (\r
+ ScsiDiskDevice->Handle,\r
+ &gEfiBlockIoProtocolGuid,\r
+ &ScsiDiskDevice->BlkIo,\r
+ &ScsiDiskDevice->BlkIo\r
+ );\r
+ gBS->ReinstallProtocolInterface (\r
+ ScsiDiskDevice->Handle,\r
+ &gEfiBlockIo2ProtocolGuid,\r
+ &ScsiDiskDevice->BlkIo2,\r
+ &ScsiDiskDevice->BlkIo2\r
+ );\r
+ if (DetermineInstallEraseBlock (ScsiDiskDevice, ScsiDiskDevice->Handle)) {\r
+ gBS->ReinstallProtocolInterface (\r
+ ScsiDiskDevice->Handle,\r
+ &gEfiEraseBlockProtocolGuid,\r
+ &ScsiDiskDevice->EraseBlock,\r
+ &ScsiDiskDevice->EraseBlock\r
+ );\r
+ }\r
+\r
+ if (DetermineInstallStorageSecurity (ScsiDiskDevice, ScsiDiskDevice->Handle)) {\r
+ gBS->ReinstallProtocolInterface (\r
+ ScsiDiskDevice->Handle,\r
+ &gEfiStorageSecurityCommandProtocolGuid,\r
+ &ScsiDiskDevice->StorageSecurity,\r
+ &ScsiDiskDevice->StorageSecurity\r
+ );\r
+ }\r
+\r
+ if (Media->MediaPresent) {\r
+ Status = EFI_MEDIA_CHANGED;\r
+ } else {\r
+ Status = EFI_NO_MEDIA;\r
+ }\r
+\r
+ goto Done;\r
+ }\r
+ }\r
+\r
+ if (!(Media->MediaPresent)) {\r
+ Status = EFI_NO_MEDIA;\r
+ goto Done;\r
+ }\r
+\r
+ if (Media->ReadOnly) {\r
+ Status = EFI_WRITE_PROTECTED;\r
+ goto Done;\r
+ }\r
+\r
+ //\r
+ // Wait for the BlockIo2 requests queue to become empty\r
+ //\r
+ while (!IsListEmpty (&ScsiDiskDevice->AsyncTaskQueue)) {\r
+ }\r
+\r
+ Status = EFI_SUCCESS;\r
+\r
+ //\r
+ // Signal caller event\r
+ //\r
+ if ((Token != NULL) && (Token->Event != NULL)) {\r
+ Token->TransactionStatus = EFI_SUCCESS;\r
+ gBS->SignalEvent (Token->Event);\r
+ }\r
+\r
+Done:\r
+ gBS->RestoreTPL (OldTpl);\r
+ return Status;\r
+}\r
+\r
+/**\r
+ Internal helper notify function which process the result of an asynchronous\r
+ SCSI UNMAP Command and signal the event passed from EraseBlocks.\r
+\r
+ @param Event The instance of EFI_EVENT.\r
+ @param Context The parameter passed in.\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+ScsiDiskAsyncUnmapNotify (\r
+ IN EFI_EVENT Event,\r
+ IN VOID *Context\r
+ )\r
+{\r
+ SCSI_ERASEBLK_REQUEST *EraseBlkReq;\r
+ EFI_SCSI_IO_SCSI_REQUEST_PACKET *CommandPacket;\r
+ EFI_ERASE_BLOCK_TOKEN *Token;\r
+ EFI_STATUS Status;\r
+\r
+ gBS->CloseEvent (Event);\r
+\r
+ EraseBlkReq = (SCSI_ERASEBLK_REQUEST *)Context;\r
+ CommandPacket = &EraseBlkReq->CommandPacket;\r
+ Token = EraseBlkReq->Token;\r
+ Token->TransactionStatus = EFI_SUCCESS;\r
+\r
+ Status = CheckHostAdapterStatus (CommandPacket->HostAdapterStatus);\r
+ if (EFI_ERROR (Status)) {\r
+ DEBUG ((\r
+ DEBUG_ERROR,\r
+ "ScsiDiskAsyncUnmapNotify: Host adapter indicating error status 0x%x.\n",\r
+ CommandPacket->HostAdapterStatus\r
+ ));\r
+\r
+ Token->TransactionStatus = Status;\r
+ goto Done;\r
+ }\r
+\r
+ Status = CheckTargetStatus (CommandPacket->TargetStatus);\r
+ if (EFI_ERROR (Status)) {\r
+ DEBUG ((\r
+ DEBUG_ERROR,\r
+ "ScsiDiskAsyncUnmapNotify: Target indicating error status 0x%x.\n",\r
+ CommandPacket->HostAdapterStatus\r
+ ));\r
+\r
+ Token->TransactionStatus = Status;\r
+ goto Done;\r
+ }\r
+\r
+Done:\r
+ RemoveEntryList (&EraseBlkReq->Link);\r
+ FreePool (CommandPacket->OutDataBuffer);\r
+ FreePool (EraseBlkReq->CommandPacket.Cdb);\r
+ FreePool (EraseBlkReq);\r
+\r
+ gBS->SignalEvent (Token->Event);\r
+}\r
+\r
+/**\r
+ Require the device server to cause one or more LBAs to be unmapped.\r
+\r
+ @param ScsiDiskDevice The pointer of ScsiDiskDevice.\r
+ @param Lba The start block number.\r
+ @param Blocks Total block number to be unmapped.\r
+ @param Token The pointer to the token associated with the\r
+ non-blocking erase block request.\r
+\r
+ @retval EFI_SUCCESS Target blocks have been successfully unmapped.\r
+ @retval EFI_DEVICE_ERROR Fail to unmap the target blocks.\r
+\r
+**/\r
+EFI_STATUS\r
+ScsiDiskUnmap (\r
+ IN SCSI_DISK_DEV *ScsiDiskDevice,\r
+ IN UINT64 Lba,\r
+ IN UINTN Blocks,\r
+ IN EFI_ERASE_BLOCK_TOKEN *Token OPTIONAL\r
+ )\r
+{\r
+ EFI_SCSI_IO_PROTOCOL *ScsiIo;\r
+ SCSI_ERASEBLK_REQUEST *EraseBlkReq;\r
+ EFI_SCSI_IO_SCSI_REQUEST_PACKET *CommandPacket;\r
+ EFI_SCSI_DISK_UNMAP_BLOCK_DESP *BlkDespPtr;\r
+ EFI_STATUS Status;\r
+ EFI_STATUS ReturnStatus;\r
+ UINT8 *Cdb;\r
+ UINT32 MaxLbaCnt;\r
+ UINT32 MaxBlkDespCnt;\r
+ UINT32 BlkDespCnt;\r
+ UINT16 UnmapParamListLen;\r
+ VOID *UnmapParamList;\r
+ EFI_EVENT AsyncUnmapEvent;\r
+ EFI_TPL OldTpl;\r
+\r
+ ScsiIo = ScsiDiskDevice->ScsiIo;\r
+ MaxLbaCnt = ScsiDiskDevice->UnmapInfo.MaxLbaCnt;\r
+ MaxBlkDespCnt = ScsiDiskDevice->UnmapInfo.MaxBlkDespCnt;\r
+ EraseBlkReq = NULL;\r
+ UnmapParamList = NULL;\r
+ AsyncUnmapEvent = NULL;\r
+ ReturnStatus = EFI_SUCCESS;\r
+\r
+ if (Blocks / (UINTN)MaxLbaCnt > MaxBlkDespCnt) {\r
+ ReturnStatus = EFI_DEVICE_ERROR;\r
+ goto Done;\r
+ }\r
+\r
+ EraseBlkReq = AllocateZeroPool (sizeof (SCSI_ERASEBLK_REQUEST));\r
+ if (EraseBlkReq == NULL) {\r
+ ReturnStatus = EFI_DEVICE_ERROR;\r
+ goto Done;\r
+ }\r
+\r
+ EraseBlkReq->CommandPacket.Cdb = AllocateZeroPool (0xA);\r
+ if (EraseBlkReq->CommandPacket.Cdb == NULL) {\r
+ ReturnStatus = EFI_DEVICE_ERROR;\r
+ goto Done;\r
+ }\r
+\r
+ BlkDespCnt = (UINT32)((Blocks - 1) / MaxLbaCnt + 1);\r
+ UnmapParamListLen = (UINT16)(sizeof (EFI_SCSI_DISK_UNMAP_PARAM_LIST_HEADER)\r
+ + BlkDespCnt * sizeof (EFI_SCSI_DISK_UNMAP_BLOCK_DESP));\r
+ UnmapParamList = AllocateZeroPool (UnmapParamListLen);\r
+ if (UnmapParamList == NULL) {\r
+ ReturnStatus = EFI_DEVICE_ERROR;\r
+ goto Done;\r
+ }\r
+\r
+ *((UINT16 *)UnmapParamList) = SwapBytes16 (UnmapParamListLen - 2);\r
+ *((UINT16 *)UnmapParamList + 1) = SwapBytes16 (UnmapParamListLen - sizeof (EFI_SCSI_DISK_UNMAP_PARAM_LIST_HEADER));\r
+\r
+ BlkDespPtr = (EFI_SCSI_DISK_UNMAP_BLOCK_DESP *)((UINT8 *)UnmapParamList + sizeof (EFI_SCSI_DISK_UNMAP_PARAM_LIST_HEADER));\r
+ while (Blocks > 0) {\r
+ if (Blocks > MaxLbaCnt) {\r
+ *(UINT64 *)(&BlkDespPtr->Lba) = SwapBytes64 (Lba);\r
+ *(UINT32 *)(&BlkDespPtr->BlockNum) = SwapBytes32 (MaxLbaCnt);\r
+ Blocks -= MaxLbaCnt;\r
+ Lba += MaxLbaCnt;\r
+ } else {\r
+ *(UINT64 *)(&BlkDespPtr->Lba) = SwapBytes64 (Lba);\r
+ *(UINT32 *)(&BlkDespPtr->BlockNum) = SwapBytes32 ((UINT32)Blocks);\r
+ Blocks = 0;\r
+ }\r
+\r
+ BlkDespPtr++;\r
+ }\r
+\r
+ CommandPacket = &EraseBlkReq->CommandPacket;\r
+ CommandPacket->Timeout = SCSI_DISK_TIMEOUT;\r
+ CommandPacket->OutDataBuffer = UnmapParamList;\r
+ CommandPacket->OutTransferLength = UnmapParamListLen;\r
+ CommandPacket->CdbLength = 0xA;\r
+ CommandPacket->DataDirection = EFI_SCSI_DATA_OUT;\r
+ //\r
+ // Fill Cdb for UNMAP Command\r
+ //\r
+ Cdb = CommandPacket->Cdb;\r
+ Cdb[0] = EFI_SCSI_OP_UNMAP;\r
+ WriteUnaligned16 ((UINT16 *)&Cdb[7], SwapBytes16 (UnmapParamListLen));\r
+\r
+ if ((Token != NULL) && (Token->Event != NULL)) {\r
+ //\r
+ // Non-blocking UNMAP request\r
+ //\r
+ Status = gBS->CreateEvent (\r
+ EVT_NOTIFY_SIGNAL,\r
+ TPL_NOTIFY,\r
+ ScsiDiskAsyncUnmapNotify,\r
+ EraseBlkReq,\r
+ &AsyncUnmapEvent\r
+ );\r
+ if (EFI_ERROR (Status)) {\r
+ ReturnStatus = EFI_DEVICE_ERROR;\r
+ goto Done;\r
+ }\r
+\r
+ OldTpl = gBS->RaiseTPL (TPL_NOTIFY);\r
+ InsertTailList (&ScsiDiskDevice->AsyncTaskQueue, &EraseBlkReq->Link);\r
+ gBS->RestoreTPL (OldTpl);\r
+\r
+ EraseBlkReq->Token = Token;\r
+\r
+ Status = ScsiIo->ExecuteScsiCommand (\r
+ ScsiIo,\r
+ CommandPacket,\r
+ AsyncUnmapEvent\r
+ );\r
+ if (EFI_ERROR (Status)) {\r
+ ReturnStatus = EFI_DEVICE_ERROR;\r
+\r
+ OldTpl = gBS->RaiseTPL (TPL_NOTIFY);\r
+ RemoveEntryList (&EraseBlkReq->Link);\r
+ gBS->RestoreTPL (OldTpl);\r
+\r
+ goto Done;\r
+ } else {\r
+ //\r
+ // Directly return if the non-blocking UNMAP request is queued.\r
+ //\r
+ return EFI_SUCCESS;\r
+ }\r
+ } else {\r
+ //\r
+ // Blocking UNMAP request\r
+ //\r
+ Status = ScsiIo->ExecuteScsiCommand (\r
+ ScsiIo,\r
+ CommandPacket,\r
+ NULL\r
+ );\r
+ if (EFI_ERROR (Status)) {\r
+ ReturnStatus = EFI_DEVICE_ERROR;\r
+ goto Done;\r
+ }\r
+ }\r
+\r
+ //\r
+ // Only blocking UNMAP request will reach here.\r
+ //\r
+ Status = CheckHostAdapterStatus (CommandPacket->HostAdapterStatus);\r
+ if (EFI_ERROR (Status)) {\r
+ DEBUG ((\r
+ DEBUG_ERROR,\r
+ "ScsiDiskUnmap: Host adapter indicating error status 0x%x.\n",\r
+ CommandPacket->HostAdapterStatus\r
+ ));\r
+\r
+ ReturnStatus = EFI_DEVICE_ERROR;\r
+ goto Done;\r
+ }\r
+\r
+ Status = CheckTargetStatus (CommandPacket->TargetStatus);\r
+ if (EFI_ERROR (Status)) {\r
+ DEBUG ((\r
+ DEBUG_ERROR,\r
+ "ScsiDiskUnmap: Target indicating error status 0x%x.\n",\r
+ CommandPacket->HostAdapterStatus\r
+ ));\r
+\r
+ ReturnStatus = EFI_DEVICE_ERROR;\r
+ goto Done;\r
+ }\r
+\r
+Done:\r
+ if (EraseBlkReq != NULL) {\r
+ if (EraseBlkReq->CommandPacket.Cdb != NULL) {\r
+ FreePool (EraseBlkReq->CommandPacket.Cdb);\r
+ }\r
+\r
+ FreePool (EraseBlkReq);\r
+ }\r
+\r
+ if (UnmapParamList != NULL) {\r
+ FreePool (UnmapParamList);\r
+ }\r
+\r
+ if (AsyncUnmapEvent != NULL) {\r
+ gBS->CloseEvent (AsyncUnmapEvent);\r
+ }\r
+\r
+ return ReturnStatus;\r
+}\r
+\r
+/**\r
+ Erase a specified number of device blocks.\r
+\r
+ @param[in] This Indicates a pointer to the calling context.\r
+ @param[in] MediaId The media ID that the erase request is for.\r
+ @param[in] Lba The starting logical block address to be\r
+ erased. The caller is responsible for erasing\r
+ only legitimate locations.\r
+ @param[in, out] Token A pointer to the token associated with the\r
+ transaction.\r
+ @param[in] Size The size in bytes to be erased. This must be\r
+ a multiple of the physical block size of the\r
+ device.\r
+\r
+ @retval EFI_SUCCESS The erase request was queued if Event is not\r
+ NULL. The data was erased correctly to the\r
+ device if the Event is NULL.to the device.\r
+ @retval EFI_WRITE_PROTECTED The device cannot be erased due to write\r
+ protection.\r
+ @retval EFI_DEVICE_ERROR The device reported an error while attempting\r
+ to perform the erase operation.\r
+ @retval EFI_INVALID_PARAMETER The erase request contains LBAs that are not\r
+ valid.\r
+ @retval EFI_NO_MEDIA There is no media in the device.\r
+ @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+ScsiDiskEraseBlocks (\r
+ IN EFI_ERASE_BLOCK_PROTOCOL *This,\r
+ IN UINT32 MediaId,\r
+ IN EFI_LBA Lba,\r
+ IN OUT EFI_ERASE_BLOCK_TOKEN *Token,\r
+ IN UINTN Size\r
+ )\r
+{\r
+ SCSI_DISK_DEV *ScsiDiskDevice;\r
+ EFI_BLOCK_IO_MEDIA *Media;\r
+ EFI_STATUS Status;\r
+ UINTN BlockSize;\r
+ UINTN NumberOfBlocks;\r
+ BOOLEAN MediaChange;\r
+ EFI_TPL OldTpl;\r
+\r
+ MediaChange = FALSE;\r
+ OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
+ ScsiDiskDevice = SCSI_DISK_DEV_FROM_ERASEBLK (This);\r
+\r
+ if (!IS_DEVICE_FIXED (ScsiDiskDevice)) {\r
+ Status = ScsiDiskDetectMedia (ScsiDiskDevice, FALSE, &MediaChange);\r
+ if (EFI_ERROR (Status)) {\r
+ Status = EFI_DEVICE_ERROR;\r
+ goto Done;\r
+ }\r
+\r
+ if (MediaChange) {\r
+ gBS->ReinstallProtocolInterface (\r
+ ScsiDiskDevice->Handle,\r
+ &gEfiBlockIoProtocolGuid,\r
+ &ScsiDiskDevice->BlkIo,\r
+ &ScsiDiskDevice->BlkIo\r
+ );\r
+ gBS->ReinstallProtocolInterface (\r
+ ScsiDiskDevice->Handle,\r
+ &gEfiBlockIo2ProtocolGuid,\r
+ &ScsiDiskDevice->BlkIo2,\r
+ &ScsiDiskDevice->BlkIo2\r
+ );\r
+ if (DetermineInstallEraseBlock (ScsiDiskDevice, ScsiDiskDevice->Handle)) {\r
+ gBS->ReinstallProtocolInterface (\r
+ ScsiDiskDevice->Handle,\r
+ &gEfiEraseBlockProtocolGuid,\r
+ &ScsiDiskDevice->EraseBlock,\r
+ &ScsiDiskDevice->EraseBlock\r
+ );\r
+ }\r
+\r
+ if (DetermineInstallStorageSecurity (ScsiDiskDevice, ScsiDiskDevice->Handle)) {\r
+ gBS->ReinstallProtocolInterface (\r
+ ScsiDiskDevice->Handle,\r
+ &gEfiStorageSecurityCommandProtocolGuid,\r
+ &ScsiDiskDevice->StorageSecurity,\r
+ &ScsiDiskDevice->StorageSecurity\r
+ );\r
+ }\r
+\r
+ Status = EFI_MEDIA_CHANGED;\r
+ goto Done;\r
+ }\r
+ }\r
+\r
+ //\r
+ // Get the intrinsic block size\r
+ //\r
+ Media = ScsiDiskDevice->BlkIo.Media;\r
+\r
+ if (!(Media->MediaPresent)) {\r
+ Status = EFI_NO_MEDIA;\r
+ goto Done;\r
+ }\r
+\r
+ if (MediaId != Media->MediaId) {\r
+ Status = EFI_MEDIA_CHANGED;\r
+ goto Done;\r
+ }\r
+\r
+ if (Media->ReadOnly) {\r
+ Status = EFI_WRITE_PROTECTED;\r
+ goto Done;\r
+ }\r
+\r
+ if (Size == 0) {\r
+ if ((Token != NULL) && (Token->Event != NULL)) {\r
+ Token->TransactionStatus = EFI_SUCCESS;\r
+ gBS->SignalEvent (Token->Event);\r
+ }\r
+\r
+ Status = EFI_SUCCESS;\r
+ goto Done;\r
+ }\r
+\r
+ BlockSize = Media->BlockSize;\r
+ if ((Size % BlockSize) != 0) {\r
+ Status = EFI_INVALID_PARAMETER;\r
+ goto Done;\r
+ }\r
+\r
+ NumberOfBlocks = Size / BlockSize;\r
+ if ((Lba + NumberOfBlocks - 1) > Media->LastBlock) {\r
+ Status = EFI_INVALID_PARAMETER;\r
+ goto Done;\r
+ }\r
+\r
+ if ((Token != NULL) && (Token->Event != NULL)) {\r
+ Status = ScsiDiskUnmap (ScsiDiskDevice, Lba, NumberOfBlocks, Token);\r
+ } else {\r
+ Status = ScsiDiskUnmap (ScsiDiskDevice, Lba, NumberOfBlocks, NULL);\r
+ }\r
+\r
+Done:\r
+ gBS->RestoreTPL (OldTpl);\r
+ return Status;\r
+}\r
+\r
+/**\r
+ Send a security protocol command to a device that receives data and/or the result\r
+ of one or more commands sent by SendData.\r
+\r
+ The ReceiveData function sends a security protocol command to the given MediaId.\r
+ The security protocol command sent is defined by SecurityProtocolId and contains\r
+ the security protocol specific data SecurityProtocolSpecificData. The function\r
+ returns the data from the security protocol command in PayloadBuffer.\r
+\r
+ For devices supporting the SCSI command set, the security protocol command is sent\r
+ using the SECURITY PROTOCOL IN command defined in SPC-4.\r
+\r
+ If PayloadBufferSize is too small to store the available data from the security\r
+ protocol command, the function shall copy PayloadBufferSize bytes into the\r
+ PayloadBuffer and return EFI_WARN_BUFFER_TOO_SMALL.\r
+\r
+ If PayloadBuffer or PayloadTransferSize is NULL and PayloadBufferSize is non-zero,\r
+ the function shall return EFI_INVALID_PARAMETER.\r
+\r
+ If the given MediaId does not support security protocol commands, the function shall\r
+ return EFI_UNSUPPORTED. If there is no media in the device, the function returns\r
+ EFI_NO_MEDIA. If the MediaId is not the ID for the current media in the device,\r
+ the function returns EFI_MEDIA_CHANGED.\r
+\r
+ If the security protocol fails to complete within the Timeout period, the function\r
+ shall return EFI_TIMEOUT.\r
+\r
+ If the security protocol command completes without an error, the function shall\r
+ return EFI_SUCCESS. If the security protocol command completes with an error, the\r
+ function shall return EFI_DEVICE_ERROR.\r
+\r
+ @param This Indicates a pointer to the calling context.\r
+ @param MediaId ID of the medium to receive data from.\r
+ @param Timeout The timeout, in 100ns units, to use for the execution\r
+ of the security protocol command. A Timeout value of 0\r
+ means that this function will wait indefinitely for the\r
+ security protocol command to execute. If Timeout is greater\r
+ than zero, then this function will return EFI_TIMEOUT if the\r
+ time required to execute the receive data command is greater than Timeout.\r
+ @param SecurityProtocolId The value of the "Security Protocol" parameter of\r
+ the security protocol command to be sent.\r
+ @param SecurityProtocolSpecificData The value of the "Security Protocol Specific" parameter\r
+ of the security protocol command to be sent.\r
+ @param PayloadBufferSize Size in bytes of the payload data buffer.\r
+ @param PayloadBuffer A pointer to a destination buffer to store the security\r
+ protocol command specific payload data for the security\r
+ protocol command. The caller is responsible for having\r
+ either implicit or explicit ownership of the buffer.\r
+ @param PayloadTransferSize A pointer to a buffer to store the size in bytes of the\r
+ data written to the payload data buffer.\r
+\r
+ @retval EFI_SUCCESS The security protocol command completed successfully.\r
+ @retval EFI_WARN_BUFFER_TOO_SMALL The PayloadBufferSize was too small to store the available\r
+ data from the device. The PayloadBuffer contains the truncated data.\r
+ @retval EFI_UNSUPPORTED The given MediaId does not support security protocol commands.\r
+ @retval EFI_DEVICE_ERROR The security protocol command completed with an error.\r
+ @retval EFI_NO_MEDIA There is no media in the device.\r
+ @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.\r
+ @retval EFI_INVALID_PARAMETER The PayloadBuffer or PayloadTransferSize is NULL and\r
+ PayloadBufferSize is non-zero.\r
+ @retval EFI_TIMEOUT A timeout occurred while waiting for the security\r
+ protocol command to execute.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+ScsiDiskReceiveData (\r
+ IN EFI_STORAGE_SECURITY_COMMAND_PROTOCOL *This,\r
+ IN UINT32 MediaId OPTIONAL,\r
+ IN UINT64 Timeout,\r
+ IN UINT8 SecurityProtocolId,\r
+ IN UINT16 SecurityProtocolSpecificData,\r
+ IN UINTN PayloadBufferSize,\r
+ OUT VOID *PayloadBuffer,\r
+ OUT UINTN *PayloadTransferSize\r
+ )\r
+{\r
+ SCSI_DISK_DEV *ScsiDiskDevice;\r
+ EFI_BLOCK_IO_MEDIA *Media;\r
+ EFI_STATUS Status;\r
+ BOOLEAN MediaChange;\r
+ EFI_TPL OldTpl;\r
+ UINT8 SenseDataLength;\r
+ UINT8 HostAdapterStatus;\r
+ UINT8 TargetStatus;\r
+ VOID *AlignedBuffer;\r
+ BOOLEAN AlignedBufferAllocated;\r
+\r
+ AlignedBuffer = NULL;\r
+ MediaChange = FALSE;\r
+ AlignedBufferAllocated = FALSE;\r
+ OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
+ ScsiDiskDevice = SCSI_DISK_DEV_FROM_STORSEC (This);\r
+ Media = ScsiDiskDevice->BlkIo.Media;\r
+\r
+ SenseDataLength = (UINT8)(ScsiDiskDevice->SenseDataNumber * sizeof (EFI_SCSI_SENSE_DATA));\r
+\r
+ if (!IS_DEVICE_FIXED (ScsiDiskDevice)) {\r
+ Status = ScsiDiskDetectMedia (ScsiDiskDevice, FALSE, &MediaChange);\r
+ if (EFI_ERROR (Status)) {\r
+ Status = EFI_DEVICE_ERROR;\r
+ goto Done;\r
+ }\r
+\r
+ if (MediaChange) {\r
+ gBS->ReinstallProtocolInterface (\r
+ ScsiDiskDevice->Handle,\r
+ &gEfiBlockIoProtocolGuid,\r
+ &ScsiDiskDevice->BlkIo,\r
+ &ScsiDiskDevice->BlkIo\r
+ );\r
+ gBS->ReinstallProtocolInterface (\r
+ ScsiDiskDevice->Handle,\r
+ &gEfiBlockIo2ProtocolGuid,\r
+ &ScsiDiskDevice->BlkIo2,\r
+ &ScsiDiskDevice->BlkIo2\r
+ );\r
+ if (DetermineInstallEraseBlock (ScsiDiskDevice, ScsiDiskDevice->Handle)) {\r
+ gBS->ReinstallProtocolInterface (\r
+ ScsiDiskDevice->Handle,\r
+ &gEfiEraseBlockProtocolGuid,\r
+ &ScsiDiskDevice->EraseBlock,\r
+ &ScsiDiskDevice->EraseBlock\r
+ );\r
+ }\r
+\r
+ if (DetermineInstallStorageSecurity (ScsiDiskDevice, ScsiDiskDevice->Handle)) {\r
+ gBS->ReinstallProtocolInterface (\r
+ ScsiDiskDevice->Handle,\r
+ &gEfiStorageSecurityCommandProtocolGuid,\r
+ &ScsiDiskDevice->StorageSecurity,\r
+ &ScsiDiskDevice->StorageSecurity\r
+ );\r
+ }\r
+\r
+ if (Media->MediaPresent) {\r
+ Status = EFI_MEDIA_CHANGED;\r
+ } else {\r
+ Status = EFI_NO_MEDIA;\r
+ }\r
+\r
+ goto Done;\r
+ }\r
+ }\r
+\r
+ //\r
+ // Validate Media\r
+ //\r
+ if (!(Media->MediaPresent)) {\r
+ Status = EFI_NO_MEDIA;\r
+ goto Done;\r
+ }\r
+\r
+ if ((MediaId != 0) && (MediaId != Media->MediaId)) {\r
+ Status = EFI_MEDIA_CHANGED;\r
+ goto Done;\r
+ }\r
+\r
+ if (PayloadBufferSize != 0) {\r
+ if ((PayloadBuffer == NULL) || (PayloadTransferSize == NULL)) {\r
+ Status = EFI_INVALID_PARAMETER;\r
+ goto Done;\r
+ }\r
+\r
+ if ((ScsiDiskDevice->ScsiIo->IoAlign > 1) && !IS_ALIGNED (PayloadBuffer, ScsiDiskDevice->ScsiIo->IoAlign)) {\r
+ AlignedBuffer = AllocateAlignedBuffer (ScsiDiskDevice, PayloadBufferSize);\r
+ if (AlignedBuffer == NULL) {\r
+ Status = EFI_OUT_OF_RESOURCES;\r
+ goto Done;\r
+ }\r
+\r
+ ZeroMem (AlignedBuffer, PayloadBufferSize);\r
+ AlignedBufferAllocated = TRUE;\r
+ } else {\r
+ AlignedBuffer = PayloadBuffer;\r
+ }\r
+ }\r
+\r
+ Status = ScsiSecurityProtocolInCommand (\r
+ ScsiDiskDevice->ScsiIo,\r
+ Timeout,\r
+ ScsiDiskDevice->SenseData,\r
+ &SenseDataLength,\r
+ &HostAdapterStatus,\r
+ &TargetStatus,\r
+ SecurityProtocolId,\r
+ SecurityProtocolSpecificData,\r
+ FALSE,\r
+ PayloadBufferSize,\r
+ AlignedBuffer,\r
+ PayloadTransferSize\r
+ );\r
+ if (EFI_ERROR (Status)) {\r
+ goto Done;\r
+ }\r
+\r
+ if (AlignedBufferAllocated) {\r
+ CopyMem (PayloadBuffer, AlignedBuffer, PayloadBufferSize);\r
+ }\r
+\r
+ if (PayloadBufferSize < *PayloadTransferSize) {\r
+ Status = EFI_WARN_BUFFER_TOO_SMALL;\r
+ goto Done;\r
+ }\r
+\r
+ Status = CheckHostAdapterStatus (HostAdapterStatus);\r
+ if (EFI_ERROR (Status)) {\r
+ goto Done;\r
+ }\r
+\r
+ Status = CheckTargetStatus (TargetStatus);\r
+ if (EFI_ERROR (Status)) {\r
+ goto Done;\r
+ }\r
+\r
+Done:\r
+ if (AlignedBufferAllocated) {\r
+ ZeroMem (AlignedBuffer, PayloadBufferSize);\r
+ FreeAlignedBuffer (AlignedBuffer, PayloadBufferSize);\r
+ }\r
+\r
+ gBS->RestoreTPL (OldTpl);\r
+ return Status;\r
+}\r
+\r
+/**\r
+ Send a security protocol command to a device.\r
+\r
+ The SendData function sends a security protocol command containing the payload\r
+ PayloadBuffer to the given MediaId. The security protocol command sent is\r
+ defined by SecurityProtocolId and contains the security protocol specific data\r
+ SecurityProtocolSpecificData. If the underlying protocol command requires a\r
+ specific padding for the command payload, the SendData function shall add padding\r
+ bytes to the command payload to satisfy the padding requirements.\r
+\r
+ For devices supporting the SCSI command set, the security protocol command is sent\r
+ using the SECURITY PROTOCOL OUT command defined in SPC-4.\r
+\r
+ If PayloadBuffer is NULL and PayloadBufferSize is non-zero, the function shall\r
+ return EFI_INVALID_PARAMETER.\r
+\r
+ If the given MediaId does not support security protocol commands, the function\r
+ shall return EFI_UNSUPPORTED. If there is no media in the device, the function\r
+ returns EFI_NO_MEDIA. If the MediaId is not the ID for the current media in the\r
+ device, the function returns EFI_MEDIA_CHANGED.\r
+\r
+ If the security protocol fails to complete within the Timeout period, the function\r
+ shall return EFI_TIMEOUT.\r
+\r
+ If the security protocol command completes without an error, the function shall return\r
+ EFI_SUCCESS. If the security protocol command completes with an error, the function\r
+ shall return EFI_DEVICE_ERROR.\r
+\r
+ @param This Indicates a pointer to the calling context.\r
+ @param MediaId ID of the medium to receive data from.\r
+ @param Timeout The timeout, in 100ns units, to use for the execution\r
+ of the security protocol command. A Timeout value of 0\r
+ means that this function will wait indefinitely for the\r
+ security protocol command to execute. If Timeout is greater\r
+ than zero, then this function will return EFI_TIMEOUT if the\r
+ time required to execute the receive data command is greater than Timeout.\r
+ @param SecurityProtocolId The value of the "Security Protocol" parameter of\r
+ the security protocol command to be sent.\r
+ @param SecurityProtocolSpecificData The value of the "Security Protocol Specific" parameter\r
+ of the security protocol command to be sent.\r
+ @param PayloadBufferSize Size in bytes of the payload data buffer.\r
+ @param PayloadBuffer A pointer to a destination buffer to store the security\r
+ protocol command specific payload data for the security\r
+ protocol command.\r
+\r
+ @retval EFI_SUCCESS The security protocol command completed successfully.\r
+ @retval EFI_UNSUPPORTED The given MediaId does not support security protocol commands.\r
+ @retval EFI_DEVICE_ERROR The security protocol command completed with an error.\r
+ @retval EFI_NO_MEDIA There is no media in the device.\r
+ @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.\r
+ @retval EFI_INVALID_PARAMETER The PayloadBuffer is NULL and PayloadBufferSize is non-zero.\r
+ @retval EFI_TIMEOUT A timeout occurred while waiting for the security\r
+ protocol command to execute.\r