]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Bus/Pci/NvmExpressPei/NvmExpressPeiStorageSecurity.c
MdeModulePkg: Apply uncrustify changes
[mirror_edk2.git] / MdeModulePkg / Bus / Pci / NvmExpressPei / NvmExpressPeiStorageSecurity.c
CommitLineData
2e15b750
HW
1/** @file\r
2 The NvmExpressPei driver is used to manage non-volatile memory subsystem\r
3 which follows NVM Express specification at PEI phase.\r
4\r
5 Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>\r
6\r
9d510e61 7 SPDX-License-Identifier: BSD-2-Clause-Patent\r
2e15b750
HW
8\r
9**/\r
10\r
11#include "NvmExpressPei.h"\r
12\r
13/**\r
14 Trust transfer data from/to NVM Express device.\r
15\r
16 This function performs one NVMe transaction to do a trust transfer from/to NVM\r
17 Express device.\r
18\r
19 @param[in] Private The pointer to the PEI_NVME_CONTROLLER_PRIVATE_DATA\r
20 data structure.\r
21 @param[in,out] Buffer The pointer to the current transaction buffer.\r
22 @param[in] SecurityProtocolId\r
23 The value of the "Security Protocol" parameter\r
24 of the security protocol command to be sent.\r
25 @param[in] SecurityProtocolSpecificData\r
26 The value of the "Security Protocol Specific"\r
27 parameter of the security protocol command to\r
28 be sent.\r
29 @param[in] TransferLength The block number or sector count of the transfer.\r
30 @param[in] IsTrustSend Indicates whether it is a trust send operation\r
31 or not.\r
32 @param[in] Timeout The timeout, in 100ns units, to use for the\r
33 execution of the security protocol command.\r
34 A Timeout value of 0 means that this function\r
35 will wait indefinitely for the security protocol\r
36 command to execute. If Timeout is greater than\r
37 zero, then this function will return EFI_TIMEOUT\r
38 if the time required to execute the receive\r
39 data command is greater than Timeout.\r
40 @param[out] TransferLengthOut A pointer to a buffer to store the size in bytes\r
41 of the data written to the buffer. Ignore it\r
42 when IsTrustSend is TRUE.\r
43\r
44 @retval EFI_SUCCESS The data transfer is complete successfully.\r
45 @return others Some error occurs when transferring data.\r
46\r
47**/\r
48EFI_STATUS\r
49TrustTransferNvmeDevice (\r
1436aea4
MK
50 IN PEI_NVME_CONTROLLER_PRIVATE_DATA *Private,\r
51 IN OUT VOID *Buffer,\r
52 IN UINT8 SecurityProtocolId,\r
53 IN UINT16 SecurityProtocolSpecificData,\r
54 IN UINTN TransferLength,\r
55 IN BOOLEAN IsTrustSend,\r
56 IN UINT64 Timeout,\r
57 OUT UINTN *TransferLengthOut\r
2e15b750
HW
58 )\r
59{\r
1436aea4
MK
60 EFI_NVM_EXPRESS_PASS_THRU_COMMAND_PACKET CommandPacket;\r
61 EFI_NVM_EXPRESS_COMMAND Command;\r
62 EFI_NVM_EXPRESS_COMPLETION Completion;\r
63 EFI_STATUS Status;\r
64 UINT16 SpecificData;\r
65 EDKII_PEI_NVM_EXPRESS_PASS_THRU_PPI *NvmePassThru;\r
2e15b750 66\r
ba3aa1c4 67 NvmePassThru = &Private->NvmePassThruPpi;\r
1436aea4
MK
68 ZeroMem (&CommandPacket, sizeof (EFI_NVM_EXPRESS_PASS_THRU_COMMAND_PACKET));\r
69 ZeroMem (&Command, sizeof (EFI_NVM_EXPRESS_COMMAND));\r
70 ZeroMem (&Completion, sizeof (EFI_NVM_EXPRESS_COMPLETION));\r
2e15b750
HW
71\r
72 CommandPacket.NvmeCmd = &Command;\r
73 CommandPacket.NvmeCompletion = &Completion;\r
74\r
75 //\r
76 // Change Endianness of SecurityProtocolSpecificData\r
77 //\r
78 SpecificData = (((SecurityProtocolSpecificData << 8) & 0xFF00) | (SecurityProtocolSpecificData >> 8));\r
79\r
80 if (IsTrustSend) {\r
81 Command.Cdw0.Opcode = NVME_ADMIN_SECURITY_SEND_CMD;\r
82 CommandPacket.TransferBuffer = Buffer;\r
83 CommandPacket.TransferLength = (UINT32)TransferLength;\r
84 CommandPacket.NvmeCmd->Cdw10 = (UINT32)((SecurityProtocolId << 24) | (SpecificData << 8));\r
85 CommandPacket.NvmeCmd->Cdw11 = (UINT32)TransferLength;\r
86 } else {\r
87 Command.Cdw0.Opcode = NVME_ADMIN_SECURITY_RECEIVE_CMD;\r
88 CommandPacket.TransferBuffer = Buffer;\r
89 CommandPacket.TransferLength = (UINT32)TransferLength;\r
90 CommandPacket.NvmeCmd->Cdw10 = (UINT32)((SecurityProtocolId << 24) | (SpecificData << 8));\r
91 CommandPacket.NvmeCmd->Cdw11 = (UINT32)TransferLength;\r
92 }\r
93\r
94 CommandPacket.NvmeCmd->Flags = CDW10_VALID | CDW11_VALID;\r
95 CommandPacket.NvmeCmd->Nsid = NVME_CONTROLLER_NSID;\r
96 CommandPacket.CommandTimeout = Timeout;\r
97 CommandPacket.QueueType = NVME_ADMIN_QUEUE;\r
98\r
ba3aa1c4
MC
99 Status = NvmePassThru->PassThru (\r
100 NvmePassThru,\r
101 NVME_CONTROLLER_NSID,\r
102 &CommandPacket\r
103 );\r
2e15b750
HW
104\r
105 if (!IsTrustSend) {\r
1436aea4 106 if (EFI_ERROR (Status)) {\r
2e15b750
HW
107 *TransferLengthOut = 0;\r
108 } else {\r
1436aea4 109 *TransferLengthOut = (UINTN)TransferLength;\r
2e15b750
HW
110 }\r
111 }\r
112\r
113 return Status;\r
114}\r
115\r
116/**\r
117 Gets the count of storage security devices that one specific driver detects.\r
118\r
119 @param[in] This The PPI instance pointer.\r
120 @param[out] NumberofDevices The number of storage security devices discovered.\r
121\r
122 @retval EFI_SUCCESS The operation performed successfully.\r
123 @retval EFI_INVALID_PARAMETER The parameters are invalid.\r
124\r
125**/\r
126EFI_STATUS\r
127EFIAPI\r
128NvmeStorageSecurityGetDeviceNo (\r
1436aea4
MK
129 IN EDKII_PEI_STORAGE_SECURITY_CMD_PPI *This,\r
130 OUT UINTN *NumberofDevices\r
2e15b750
HW
131 )\r
132{\r
1436aea4 133 PEI_NVME_CONTROLLER_PRIVATE_DATA *Private;\r
2e15b750 134\r
1436aea4 135 if ((This == NULL) || (NumberofDevices == NULL)) {\r
2e15b750
HW
136 return EFI_INVALID_PARAMETER;\r
137 }\r
138\r
1436aea4 139 Private = GET_NVME_PEIM_HC_PRIVATE_DATA_FROM_THIS_STROAGE_SECURITY (This);\r
2e15b750
HW
140 *NumberofDevices = Private->ActiveNamespaceNum;\r
141\r
142 return EFI_SUCCESS;\r
143}\r
144\r
145/**\r
146 Gets the device path of a specific storage security device.\r
147\r
148 @param[in] This The PPI instance pointer.\r
149 @param[in] DeviceIndex Specifies the storage security device to which\r
150 the function wants to talk. Because the driver\r
151 that implements Storage Security Command PPIs\r
152 will manage multiple storage devices, the PPIs\r
153 that want to talk to a single device must specify\r
154 the device index that was assigned during the\r
155 enumeration process. This index is a number from\r
156 one to NumberofDevices.\r
157 @param[out] DevicePathLength The length of the device path in bytes specified\r
158 by DevicePath.\r
159 @param[out] DevicePath The device path of storage security device.\r
160 This field re-uses EFI Device Path Protocol as\r
161 defined by Section 10.2 EFI Device Path Protocol\r
162 of UEFI 2.7 Specification.\r
163\r
164 @retval EFI_SUCCESS The operation succeeds.\r
165 @retval EFI_INVALID_PARAMETER DevicePathLength or DevicePath is NULL.\r
166 @retval EFI_NOT_FOUND The specified storage security device not found.\r
167 @retval EFI_OUT_OF_RESOURCES The operation fails due to lack of resources.\r
168\r
169**/\r
170EFI_STATUS\r
171EFIAPI\r
172NvmeStorageSecurityGetDevicePath (\r
173 IN EDKII_PEI_STORAGE_SECURITY_CMD_PPI *This,\r
174 IN UINTN DeviceIndex,\r
175 OUT UINTN *DevicePathLength,\r
176 OUT EFI_DEVICE_PATH_PROTOCOL **DevicePath\r
177 )\r
178{\r
1436aea4 179 PEI_NVME_CONTROLLER_PRIVATE_DATA *Private;\r
2e15b750 180\r
1436aea4 181 if ((This == NULL) || (DevicePathLength == NULL) || (DevicePath == NULL)) {\r
2e15b750
HW
182 return EFI_INVALID_PARAMETER;\r
183 }\r
184\r
185 Private = GET_NVME_PEIM_HC_PRIVATE_DATA_FROM_THIS_STROAGE_SECURITY (This);\r
186 if ((DeviceIndex == 0) || (DeviceIndex > Private->ActiveNamespaceNum)) {\r
187 return EFI_INVALID_PARAMETER;\r
188 }\r
189\r
190 return NvmeBuildDevicePath (\r
191 Private,\r
192 Private->NamespaceInfo[DeviceIndex-1].NamespaceId,\r
193 Private->NamespaceInfo[DeviceIndex-1].NamespaceUuid,\r
194 DevicePathLength,\r
195 DevicePath\r
196 );\r
197}\r
198\r
199/**\r
200 Send a security protocol command to a device that receives data and/or the result\r
201 of one or more commands sent by SendData.\r
202\r
203 The ReceiveData function sends a security protocol command to the given DeviceIndex.\r
204 The security protocol command sent is defined by SecurityProtocolId and contains\r
205 the security protocol specific data SecurityProtocolSpecificData. The function\r
206 returns the data from the security protocol command in PayloadBuffer.\r
207\r
208 For devices supporting the SCSI command set, the security protocol command is sent\r
209 using the SECURITY PROTOCOL IN command defined in SPC-4.\r
210\r
211 For devices supporting the ATA command set, the security protocol command is sent\r
212 using one of the TRUSTED RECEIVE commands defined in ATA8-ACS if PayloadBufferSize\r
213 is non-zero.\r
214\r
215 If the PayloadBufferSize is zero, the security protocol command is sent using the\r
216 Trusted Non-Data command defined in ATA8-ACS.\r
217\r
218 If PayloadBufferSize is too small to store the available data from the security\r
219 protocol command, the function shall copy PayloadBufferSize bytes into the\r
220 PayloadBuffer and return EFI_WARN_BUFFER_TOO_SMALL.\r
221\r
222 If PayloadBuffer or PayloadTransferSize is NULL and PayloadBufferSize is non-zero,\r
223 the function shall return EFI_INVALID_PARAMETER.\r
224\r
225 If the given DeviceIndex does not support security protocol commands, the function\r
226 shall return EFI_UNSUPPORTED.\r
227\r
228 If the security protocol fails to complete within the Timeout period, the function\r
229 shall return EFI_TIMEOUT.\r
230\r
231 If the security protocol command completes without an error, the function shall\r
232 return EFI_SUCCESS. If the security protocol command completes with an error, the\r
233 function shall return EFI_DEVICE_ERROR.\r
234\r
235 @param[in] This The PPI instance pointer.\r
236 @param[in] DeviceIndex Specifies the storage security device to which the\r
237 function wants to talk. Because the driver that\r
238 implements Storage Security Command PPIs will manage\r
239 multiple storage devices, the PPIs that want to talk\r
240 to a single device must specify the device index\r
241 that was assigned during the enumeration process.\r
242 This index is a number from one to NumberofDevices.\r
243 @param[in] Timeout The timeout, in 100ns units, to use for the execution\r
244 of the security protocol command. A Timeout value\r
245 of 0 means that this function will wait indefinitely\r
246 for the security protocol command to execute. If\r
247 Timeout is greater than zero, then this function\r
248 will return EFI_TIMEOUT if the time required to\r
249 execute the receive data command is greater than\r
250 Timeout.\r
251 @param[in] SecurityProtocolId\r
252 The value of the "Security Protocol" parameter of\r
253 the security protocol command to be sent.\r
254 @param[in] SecurityProtocolSpecificData\r
255 The value of the "Security Protocol Specific"\r
256 parameter of the security protocol command to be\r
257 sent.\r
258 @param[in] PayloadBufferSize\r
259 Size in bytes of the payload data buffer.\r
260 @param[out] PayloadBuffer A pointer to a destination buffer to store the\r
261 security protocol command specific payload data\r
262 for the security protocol command. The caller is\r
263 responsible for having either implicit or explicit\r
264 ownership of the buffer.\r
265 @param[out] PayloadTransferSize\r
266 A pointer to a buffer to store the size in bytes\r
267 of the data written to the payload data buffer.\r
268\r
269 @retval EFI_SUCCESS The security protocol command completed\r
270 successfully.\r
271 @retval EFI_WARN_BUFFER_TOO_SMALL The PayloadBufferSize was too small to\r
272 store the available data from the device.\r
273 The PayloadBuffer contains the truncated\r
274 data.\r
275 @retval EFI_UNSUPPORTED The given DeviceIndex does not support\r
276 security protocol commands.\r
277 @retval EFI_DEVICE_ERROR The security protocol command completed\r
278 with an error.\r
279 @retval EFI_INVALID_PARAMETER The PayloadBuffer or PayloadTransferSize\r
280 is NULL and PayloadBufferSize is non-zero.\r
281 @retval EFI_TIMEOUT A timeout occurred while waiting for the\r
282 security protocol command to execute.\r
283\r
284**/\r
285EFI_STATUS\r
286EFIAPI\r
287NvmeStorageSecurityReceiveData (\r
288 IN EDKII_PEI_STORAGE_SECURITY_CMD_PPI *This,\r
289 IN UINTN DeviceIndex,\r
290 IN UINT64 Timeout,\r
291 IN UINT8 SecurityProtocolId,\r
292 IN UINT16 SecurityProtocolSpecificData,\r
293 IN UINTN PayloadBufferSize,\r
294 OUT VOID *PayloadBuffer,\r
295 OUT UINTN *PayloadTransferSize\r
296 )\r
297{\r
1436aea4
MK
298 PEI_NVME_CONTROLLER_PRIVATE_DATA *Private;\r
299 EFI_STATUS Status;\r
2e15b750
HW
300\r
301 if ((PayloadBuffer == NULL) || (PayloadTransferSize == NULL) || (PayloadBufferSize == 0)) {\r
302 return EFI_INVALID_PARAMETER;\r
303 }\r
304\r
305 Private = GET_NVME_PEIM_HC_PRIVATE_DATA_FROM_THIS_STROAGE_SECURITY (This);\r
306\r
307 Status = TrustTransferNvmeDevice (\r
308 Private,\r
309 PayloadBuffer,\r
310 SecurityProtocolId,\r
311 SecurityProtocolSpecificData,\r
312 PayloadBufferSize,\r
313 FALSE,\r
314 Timeout,\r
315 PayloadTransferSize\r
316 );\r
317\r
318 return Status;\r
319}\r
320\r
321/**\r
322 Send a security protocol command to a device.\r
323\r
324 The SendData function sends a security protocol command containing the payload\r
325 PayloadBuffer to the given DeviceIndex. The security protocol command sent is\r
326 defined by SecurityProtocolId and contains the security protocol specific data\r
327 SecurityProtocolSpecificData. If the underlying protocol command requires a\r
328 specific padding for the command payload, the SendData function shall add padding\r
329 bytes to the command payload to satisfy the padding requirements.\r
330\r
331 For devices supporting the SCSI command set, the security protocol command is\r
332 sent using the SECURITY PROTOCOL OUT command defined in SPC-4.\r
333\r
334 For devices supporting the ATA command set, the security protocol command is\r
335 sent using one of the TRUSTED SEND commands defined in ATA8-ACS if PayloadBufferSize\r
336 is non-zero. If the PayloadBufferSize is zero, the security protocol command\r
337 is sent using the Trusted Non-Data command defined in ATA8-ACS.\r
338\r
339 If PayloadBuffer is NULL and PayloadBufferSize is non-zero, the function shall\r
340 return EFI_INVALID_PARAMETER.\r
341\r
342 If the given DeviceIndex does not support security protocol commands, the function\r
343 shall return EFI_UNSUPPORTED.\r
344\r
345 If the security protocol fails to complete within the Timeout period, the function\r
346 shall return EFI_TIMEOUT.\r
347\r
348 If the security protocol command completes without an error, the function shall\r
349 return EFI_SUCCESS. If the security protocol command completes with an error,\r
350 the functio shall return EFI_DEVICE_ERROR.\r
351\r
352 @param[in] This The PPI instance pointer.\r
353 @param[in] DeviceIndex The ID of the device.\r
354 @param[in] Timeout The timeout, in 100ns units, to use for the execution\r
355 of the security protocol command. A Timeout value\r
356 of 0 means that this function will wait indefinitely\r
357 for the security protocol command to execute. If\r
358 Timeout is greater than zero, then this function\r
359 will return EFI_TIMEOUT if the time required to\r
360 execute the receive data command is greater than\r
361 Timeout.\r
362 @param[in] SecurityProtocolId\r
363 The value of the "Security Protocol" parameter of\r
364 the security protocol command to be sent.\r
365 @param[in] SecurityProtocolSpecificData\r
366 The value of the "Security Protocol Specific"\r
367 parameter of the security protocol command to be\r
368 sent.\r
369 @param[in] PayloadBufferSize Size in bytes of the payload data buffer.\r
370 @param[in] PayloadBuffer A pointer to a destination buffer to store the\r
371 security protocol command specific payload data\r
372 for the security protocol command.\r
373\r
374 @retval EFI_SUCCESS The security protocol command completed successfully.\r
375 @retval EFI_UNSUPPORTED The given DeviceIndex does not support security\r
376 protocol commands.\r
377 @retval EFI_DEVICE_ERROR The security protocol command completed with\r
378 an error.\r
379 @retval EFI_INVALID_PARAMETER The PayloadBuffer is NULL and PayloadBufferSize\r
380 is non-zero.\r
381 @retval EFI_TIMEOUT A timeout occurred while waiting for the security\r
382 protocol command to execute.\r
383\r
384**/\r
385EFI_STATUS\r
386EFIAPI\r
387NvmeStorageSecuritySendData (\r
388 IN EDKII_PEI_STORAGE_SECURITY_CMD_PPI *This,\r
389 IN UINTN DeviceIndex,\r
390 IN UINT64 Timeout,\r
391 IN UINT8 SecurityProtocolId,\r
392 IN UINT16 SecurityProtocolSpecificData,\r
393 IN UINTN PayloadBufferSize,\r
394 IN VOID *PayloadBuffer\r
395 )\r
396{\r
1436aea4
MK
397 PEI_NVME_CONTROLLER_PRIVATE_DATA *Private;\r
398 EFI_STATUS Status;\r
2e15b750
HW
399\r
400 if ((PayloadBuffer == NULL) && (PayloadBufferSize != 0)) {\r
401 return EFI_INVALID_PARAMETER;\r
402 }\r
403\r
404 Private = GET_NVME_PEIM_HC_PRIVATE_DATA_FROM_THIS_STROAGE_SECURITY (This);\r
405\r
406 Status = TrustTransferNvmeDevice (\r
407 Private,\r
408 PayloadBuffer,\r
409 SecurityProtocolId,\r
410 SecurityProtocolSpecificData,\r
411 PayloadBufferSize,\r
412 TRUE,\r
413 Timeout,\r
414 NULL\r
415 );\r
416\r
417 return Status;\r
418}\r