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