AhciPeiStorageSecurity.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /** @file
  2. The AhciPei driver is used to manage ATA hard disk device working under AHCI
  3. mode at PEI phase.
  4. Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
  5. SPDX-License-Identifier: BSD-2-Clause-Patent
  6. **/
  7. #include "AhciPei.h"
  8. /**
  9. Traverse the attached ATA devices list to find out the device with given trust
  10. computing device index.
  11. @param[in] Private A pointer to the PEI_AHCI_CONTROLLER_PRIVATE_DATA
  12. instance.
  13. @param[in] TrustComputingDeviceIndex The trust computing device index.
  14. @retval The pointer to the PEI_AHCI_ATA_DEVICE_DATA structure of the device
  15. info to access.
  16. **/
  17. PEI_AHCI_ATA_DEVICE_DATA *
  18. SearchTrustComputingDeviceByIndex (
  19. IN PEI_AHCI_CONTROLLER_PRIVATE_DATA *Private,
  20. IN UINTN TrustComputingDeviceIndex
  21. )
  22. {
  23. PEI_AHCI_ATA_DEVICE_DATA *DeviceData;
  24. LIST_ENTRY *Node;
  25. Node = GetFirstNode (&Private->DeviceList);
  26. while (!IsNull (&Private->DeviceList, Node)) {
  27. DeviceData = AHCI_PEI_ATA_DEVICE_INFO_FROM_THIS (Node);
  28. if (DeviceData->TrustComputingDeviceIndex == TrustComputingDeviceIndex) {
  29. return DeviceData;
  30. }
  31. Node = GetNextNode (&Private->DeviceList, Node);
  32. }
  33. return NULL;
  34. }
  35. /**
  36. Gets the count of storage security devices that one specific driver detects.
  37. @param[in] This The PPI instance pointer.
  38. @param[out] NumberofDevices The number of storage security devices discovered.
  39. @retval EFI_SUCCESS The operation performed successfully.
  40. @retval EFI_INVALID_PARAMETER The parameters are invalid.
  41. **/
  42. EFI_STATUS
  43. EFIAPI
  44. AhciStorageSecurityGetDeviceNo (
  45. IN EDKII_PEI_STORAGE_SECURITY_CMD_PPI *This,
  46. OUT UINTN *NumberofDevices
  47. )
  48. {
  49. PEI_AHCI_CONTROLLER_PRIVATE_DATA *Private;
  50. if (This == NULL || NumberofDevices == NULL) {
  51. return EFI_INVALID_PARAMETER;
  52. }
  53. Private = GET_AHCI_PEIM_HC_PRIVATE_DATA_FROM_THIS_STROAGE_SECURITY (This);
  54. *NumberofDevices = Private->TrustComputingDevices;
  55. return EFI_SUCCESS;
  56. }
  57. /**
  58. Gets the device path of a specific storage security device.
  59. @param[in] This The PPI instance pointer.
  60. @param[in] DeviceIndex Specifies the storage security device to which
  61. the function wants to talk. Because the driver
  62. that implements Storage Security Command PPIs
  63. will manage multiple storage devices, the PPIs
  64. that want to talk to a single device must specify
  65. the device index that was assigned during the
  66. enumeration process. This index is a number from
  67. one to NumberofDevices.
  68. @param[out] DevicePathLength The length of the device path in bytes specified
  69. by DevicePath.
  70. @param[out] DevicePath The device path of storage security device.
  71. This field re-uses EFI Device Path Protocol as
  72. defined by Section 10.2 EFI Device Path Protocol
  73. of UEFI 2.7 Specification.
  74. @retval EFI_SUCCESS The operation succeeds.
  75. @retval EFI_INVALID_PARAMETER DevicePathLength or DevicePath is NULL.
  76. @retval EFI_NOT_FOUND The specified storage security device not found.
  77. @retval EFI_OUT_OF_RESOURCES The operation fails due to lack of resources.
  78. **/
  79. EFI_STATUS
  80. EFIAPI
  81. AhciStorageSecurityGetDevicePath (
  82. IN EDKII_PEI_STORAGE_SECURITY_CMD_PPI *This,
  83. IN UINTN DeviceIndex,
  84. OUT UINTN *DevicePathLength,
  85. OUT EFI_DEVICE_PATH_PROTOCOL **DevicePath
  86. )
  87. {
  88. PEI_AHCI_CONTROLLER_PRIVATE_DATA *Private;
  89. PEI_AHCI_ATA_DEVICE_DATA *DeviceData;
  90. EFI_STATUS Status;
  91. if (This == NULL || DevicePathLength == NULL || DevicePath == NULL) {
  92. return EFI_INVALID_PARAMETER;
  93. }
  94. Private = GET_AHCI_PEIM_HC_PRIVATE_DATA_FROM_THIS_STROAGE_SECURITY (This);
  95. if ((DeviceIndex == 0) || (DeviceIndex > Private->TrustComputingDevices)) {
  96. return EFI_INVALID_PARAMETER;
  97. }
  98. DeviceData = SearchTrustComputingDeviceByIndex (Private, DeviceIndex);
  99. if (DeviceData == NULL) {
  100. return EFI_NOT_FOUND;
  101. }
  102. Status = AhciBuildDevicePath (
  103. Private,
  104. DeviceData->Port,
  105. DeviceData->PortMultiplier,
  106. DevicePathLength,
  107. DevicePath
  108. );
  109. if (EFI_ERROR (Status)) {
  110. return Status;
  111. }
  112. return EFI_SUCCESS;
  113. }
  114. /**
  115. Send a security protocol command to a device that receives data and/or the result
  116. of one or more commands sent by SendData.
  117. The ReceiveData function sends a security protocol command to the given DeviceIndex.
  118. The security protocol command sent is defined by SecurityProtocolId and contains
  119. the security protocol specific data SecurityProtocolSpecificData. The function
  120. returns the data from the security protocol command in PayloadBuffer.
  121. For devices supporting the SCSI command set, the security protocol command is sent
  122. using the SECURITY PROTOCOL IN command defined in SPC-4.
  123. For devices supporting the ATA command set, the security protocol command is sent
  124. using one of the TRUSTED RECEIVE commands defined in ATA8-ACS if PayloadBufferSize
  125. is non-zero.
  126. If the PayloadBufferSize is zero, the security protocol command is sent using the
  127. Trusted Non-Data command defined in ATA8-ACS.
  128. If PayloadBufferSize is too small to store the available data from the security
  129. protocol command, the function shall copy PayloadBufferSize bytes into the
  130. PayloadBuffer and return EFI_WARN_BUFFER_TOO_SMALL.
  131. If PayloadBuffer or PayloadTransferSize is NULL and PayloadBufferSize is non-zero,
  132. the function shall return EFI_INVALID_PARAMETER.
  133. If the given DeviceIndex does not support security protocol commands, the function
  134. shall return EFI_UNSUPPORTED.
  135. If the security protocol fails to complete within the Timeout period, the function
  136. shall return EFI_TIMEOUT.
  137. If the security protocol command completes without an error, the function shall
  138. return EFI_SUCCESS. If the security protocol command completes with an error, the
  139. function shall return EFI_DEVICE_ERROR.
  140. @param[in] This The PPI instance pointer.
  141. @param[in] DeviceIndex Specifies the storage security device to which the
  142. function wants to talk. Because the driver that
  143. implements Storage Security Command PPIs will manage
  144. multiple storage devices, the PPIs that want to talk
  145. to a single device must specify the device index
  146. that was assigned during the enumeration process.
  147. This index is a number from one to NumberofDevices.
  148. @param[in] Timeout The timeout, in 100ns units, to use for the execution
  149. of the security protocol command. A Timeout value
  150. of 0 means that this function will wait indefinitely
  151. for the security protocol command to execute. If
  152. Timeout is greater than zero, then this function
  153. will return EFI_TIMEOUT if the time required to
  154. execute the receive data command is greater than
  155. Timeout.
  156. @param[in] SecurityProtocolId
  157. The value of the "Security Protocol" parameter of
  158. the security protocol command to be sent.
  159. @param[in] SecurityProtocolSpecificData
  160. The value of the "Security Protocol Specific"
  161. parameter of the security protocol command to be
  162. sent.
  163. @param[in] PayloadBufferSize
  164. Size in bytes of the payload data buffer.
  165. @param[out] PayloadBuffer A pointer to a destination buffer to store the
  166. security protocol command specific payload data
  167. for the security protocol command. The caller is
  168. responsible for having either implicit or explicit
  169. ownership of the buffer.
  170. @param[out] PayloadTransferSize
  171. A pointer to a buffer to store the size in bytes
  172. of the data written to the payload data buffer.
  173. @retval EFI_SUCCESS The security protocol command completed
  174. successfully.
  175. @retval EFI_WARN_BUFFER_TOO_SMALL The PayloadBufferSize was too small to
  176. store the available data from the device.
  177. The PayloadBuffer contains the truncated
  178. data.
  179. @retval EFI_UNSUPPORTED The given DeviceIndex does not support
  180. security protocol commands.
  181. @retval EFI_DEVICE_ERROR The security protocol command completed
  182. with an error.
  183. @retval EFI_INVALID_PARAMETER The PayloadBuffer or PayloadTransferSize
  184. is NULL and PayloadBufferSize is non-zero.
  185. @retval EFI_TIMEOUT A timeout occurred while waiting for the
  186. security protocol command to execute.
  187. **/
  188. EFI_STATUS
  189. EFIAPI
  190. AhciStorageSecurityReceiveData (
  191. IN EDKII_PEI_STORAGE_SECURITY_CMD_PPI *This,
  192. IN UINTN DeviceIndex,
  193. IN UINT64 Timeout,
  194. IN UINT8 SecurityProtocolId,
  195. IN UINT16 SecurityProtocolSpecificData,
  196. IN UINTN PayloadBufferSize,
  197. OUT VOID *PayloadBuffer,
  198. OUT UINTN *PayloadTransferSize
  199. )
  200. {
  201. PEI_AHCI_CONTROLLER_PRIVATE_DATA *Private;
  202. PEI_AHCI_ATA_DEVICE_DATA *DeviceData;
  203. if ((PayloadBuffer == NULL) || (PayloadTransferSize == NULL) || (PayloadBufferSize == 0)) {
  204. return EFI_INVALID_PARAMETER;
  205. }
  206. Private = GET_AHCI_PEIM_HC_PRIVATE_DATA_FROM_THIS_STROAGE_SECURITY (This);
  207. if ((DeviceIndex == 0) || (DeviceIndex > Private->TrustComputingDevices)) {
  208. return EFI_INVALID_PARAMETER;
  209. }
  210. DeviceData = SearchTrustComputingDeviceByIndex (Private, DeviceIndex);
  211. if (DeviceData == NULL) {
  212. return EFI_NOT_FOUND;
  213. }
  214. ASSERT ((DeviceData->IdentifyData->trusted_computing_support & BIT0) != 0);
  215. if ((DeviceData->IdentifyData->trusted_computing_support & BIT0) == 0) {
  216. return EFI_UNSUPPORTED;
  217. }
  218. return TrustTransferAtaDevice (
  219. DeviceData,
  220. PayloadBuffer,
  221. SecurityProtocolId,
  222. SecurityProtocolSpecificData,
  223. PayloadBufferSize,
  224. FALSE,
  225. Timeout,
  226. PayloadTransferSize
  227. );
  228. }
  229. /**
  230. Send a security protocol command to a device.
  231. The SendData function sends a security protocol command containing the payload
  232. PayloadBuffer to the given DeviceIndex. The security protocol command sent is
  233. defined by SecurityProtocolId and contains the security protocol specific data
  234. SecurityProtocolSpecificData. If the underlying protocol command requires a
  235. specific padding for the command payload, the SendData function shall add padding
  236. bytes to the command payload to satisfy the padding requirements.
  237. For devices supporting the SCSI command set, the security protocol command is
  238. sent using the SECURITY PROTOCOL OUT command defined in SPC-4.
  239. For devices supporting the ATA command set, the security protocol command is
  240. sent using one of the TRUSTED SEND commands defined in ATA8-ACS if PayloadBufferSize
  241. is non-zero. If the PayloadBufferSize is zero, the security protocol command
  242. is sent using the Trusted Non-Data command defined in ATA8-ACS.
  243. If PayloadBuffer is NULL and PayloadBufferSize is non-zero, the function shall
  244. return EFI_INVALID_PARAMETER.
  245. If the given DeviceIndex does not support security protocol commands, the function
  246. shall return EFI_UNSUPPORTED.
  247. If the security protocol fails to complete within the Timeout period, the function
  248. shall return EFI_TIMEOUT.
  249. If the security protocol command completes without an error, the function shall
  250. return EFI_SUCCESS. If the security protocol command completes with an error,
  251. the functio shall return EFI_DEVICE_ERROR.
  252. @param[in] This The PPI instance pointer.
  253. @param[in] DeviceIndex The ID of the device.
  254. @param[in] Timeout The timeout, in 100ns units, to use for the execution
  255. of the security protocol command. A Timeout value
  256. of 0 means that this function will wait indefinitely
  257. for the security protocol command to execute. If
  258. Timeout is greater than zero, then this function
  259. will return EFI_TIMEOUT if the time required to
  260. execute the receive data command is greater than
  261. Timeout.
  262. @param[in] SecurityProtocolId
  263. The value of the "Security Protocol" parameter of
  264. the security protocol command to be sent.
  265. @param[in] SecurityProtocolSpecificData
  266. The value of the "Security Protocol Specific"
  267. parameter of the security protocol command to be
  268. sent.
  269. @param[in] PayloadBufferSize Size in bytes of the payload data buffer.
  270. @param[in] PayloadBuffer A pointer to a destination buffer to store the
  271. security protocol command specific payload data
  272. for the security protocol command.
  273. @retval EFI_SUCCESS The security protocol command completed successfully.
  274. @retval EFI_UNSUPPORTED The given DeviceIndex does not support security
  275. protocol commands.
  276. @retval EFI_DEVICE_ERROR The security protocol command completed with
  277. an error.
  278. @retval EFI_INVALID_PARAMETER The PayloadBuffer is NULL and PayloadBufferSize
  279. is non-zero.
  280. @retval EFI_TIMEOUT A timeout occurred while waiting for the security
  281. protocol command to execute.
  282. **/
  283. EFI_STATUS
  284. EFIAPI
  285. AhciStorageSecuritySendData (
  286. IN EDKII_PEI_STORAGE_SECURITY_CMD_PPI *This,
  287. IN UINTN DeviceIndex,
  288. IN UINT64 Timeout,
  289. IN UINT8 SecurityProtocolId,
  290. IN UINT16 SecurityProtocolSpecificData,
  291. IN UINTN PayloadBufferSize,
  292. IN VOID *PayloadBuffer
  293. )
  294. {
  295. PEI_AHCI_CONTROLLER_PRIVATE_DATA *Private;
  296. PEI_AHCI_ATA_DEVICE_DATA *DeviceData;
  297. if ((PayloadBuffer == NULL) && (PayloadBufferSize != 0)) {
  298. return EFI_INVALID_PARAMETER;
  299. }
  300. Private = GET_AHCI_PEIM_HC_PRIVATE_DATA_FROM_THIS_STROAGE_SECURITY (This);
  301. if ((DeviceIndex == 0) || (DeviceIndex > Private->TrustComputingDevices)) {
  302. return EFI_INVALID_PARAMETER;
  303. }
  304. DeviceData = SearchTrustComputingDeviceByIndex (Private, DeviceIndex);
  305. if (DeviceData == NULL) {
  306. return EFI_NOT_FOUND;
  307. }
  308. ASSERT ((DeviceData->IdentifyData->trusted_computing_support & BIT0) != 0);
  309. if ((DeviceData->IdentifyData->trusted_computing_support & BIT0) == 0) {
  310. return EFI_UNSUPPORTED;
  311. }
  312. return TrustTransferAtaDevice (
  313. DeviceData,
  314. PayloadBuffer,
  315. SecurityProtocolId,
  316. SecurityProtocolSpecificData,
  317. PayloadBufferSize,
  318. TRUE,
  319. Timeout,
  320. NULL
  321. );
  322. }