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