]> git.proxmox.com Git - mirror_edk2.git/blob - SecurityPkg/Tcg/Opal/OpalPassword/OpalPasswordPei.c
SecurityPkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / SecurityPkg / Tcg / Opal / OpalPassword / OpalPasswordPei.c
1 /** @file
2 Opal Password PEI driver which is used to unlock Opal Password for S3.
3
4 Copyright (c) 2016 - 2019, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9 #include "OpalPasswordPei.h"
10
11 EFI_GUID mOpalDeviceLockBoxGuid = OPAL_DEVICE_LOCKBOX_GUID;
12
13
14 /**
15 Send a security protocol command to a device that receives data and/or the result
16 of one or more commands sent by SendData.
17
18 The ReceiveData function sends a security protocol command to the given MediaId.
19 The security protocol command sent is defined by SecurityProtocolId and contains
20 the security protocol specific data SecurityProtocolSpecificData. The function
21 returns the data from the security protocol command in PayloadBuffer.
22
23 For devices supporting the SCSI command set, the security protocol command is sent
24 using the SECURITY PROTOCOL IN command defined in SPC-4.
25
26 For devices supporting the ATA command set, the security protocol command is sent
27 using one of the TRUSTED RECEIVE commands defined in ATA8-ACS if PayloadBufferSize
28 is non-zero.
29
30 If the PayloadBufferSize is zero, the security protocol command is sent using the
31 Trusted Non-Data command defined in ATA8-ACS.
32
33 If PayloadBufferSize is too small to store the available data from the security
34 protocol command, the function shall copy PayloadBufferSize bytes into the
35 PayloadBuffer and return EFI_WARN_BUFFER_TOO_SMALL.
36
37 If PayloadBuffer or PayloadTransferSize is NULL and PayloadBufferSize is non-zero,
38 the function shall return EFI_INVALID_PARAMETER.
39
40 If the given MediaId does not support security protocol commands, the function shall
41 return EFI_UNSUPPORTED. If there is no media in the device, the function returns
42 EFI_NO_MEDIA. If the MediaId is not the ID for the current media in the device,
43 the function returns EFI_MEDIA_CHANGED.
44
45 If the security protocol fails to complete within the Timeout period, the function
46 shall return EFI_TIMEOUT.
47
48 If the security protocol command completes without an error, the function shall
49 return EFI_SUCCESS. If the security protocol command completes with an error, the
50 function shall return EFI_DEVICE_ERROR.
51
52 @param This Indicates a pointer to the calling context.
53 @param MediaId ID of the medium to receive data from.
54 @param Timeout The timeout, in 100ns units, to use for the execution
55 of the security protocol command. A Timeout value of 0
56 means that this function will wait indefinitely for the
57 security protocol command to execute. If Timeout is greater
58 than zero, then this function will return EFI_TIMEOUT
59 if the time required to execute the receive data command
60 is greater than Timeout.
61 @param SecurityProtocolId The value of the "Security Protocol" parameter of
62 the security protocol command to be sent.
63 @param SecurityProtocolSpecificData The value of the "Security Protocol Specific" parameter
64 of the security protocol command to be sent.
65 @param PayloadBufferSize Size in bytes of the payload data buffer.
66 @param PayloadBuffer A pointer to a destination buffer to store the security
67 protocol command specific payload data for the security
68 protocol command. The caller is responsible for having
69 either implicit or explicit ownership of the buffer.
70 @param PayloadTransferSize A pointer to a buffer to store the size in bytes of the
71 data written to the payload data buffer.
72
73 @retval EFI_SUCCESS The security protocol command completed successfully.
74 @retval EFI_WARN_BUFFER_TOO_SMALL The PayloadBufferSize was too small to store the available
75 data from the device. The PayloadBuffer contains the truncated data.
76 @retval EFI_UNSUPPORTED The given MediaId does not support security protocol commands.
77 @retval EFI_DEVICE_ERROR The security protocol command completed with an error.
78 @retval EFI_NO_MEDIA There is no media in the device.
79 @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.
80 @retval EFI_INVALID_PARAMETER The PayloadBuffer or PayloadTransferSize is NULL and
81 PayloadBufferSize is non-zero.
82 @retval EFI_TIMEOUT A timeout occurred while waiting for the security
83 protocol command to execute.
84
85 **/
86 EFI_STATUS
87 EFIAPI
88 SecurityReceiveData (
89 IN EFI_STORAGE_SECURITY_COMMAND_PROTOCOL *This,
90 IN UINT32 MediaId,
91 IN UINT64 Timeout,
92 IN UINT8 SecurityProtocolId,
93 IN UINT16 SecurityProtocolSpecificData,
94 IN UINTN PayloadBufferSize,
95 OUT VOID *PayloadBuffer,
96 OUT UINTN *PayloadTransferSize
97 )
98 {
99 OPAL_PEI_DEVICE *PeiDev;
100
101 PeiDev = OPAL_PEI_DEVICE_FROM_THIS (This);
102 if (PeiDev == NULL) {
103 return EFI_DEVICE_ERROR;
104 }
105
106 return PeiDev->SscPpi->ReceiveData (
107 PeiDev->SscPpi,
108 PeiDev->DeviceIndex,
109 SSC_PPI_GENERIC_TIMEOUT,
110 SecurityProtocolId,
111 SecurityProtocolSpecificData,
112 PayloadBufferSize,
113 PayloadBuffer,
114 PayloadTransferSize
115 );
116 }
117
118 /**
119 Send a security protocol command to a device.
120
121 The SendData function sends a security protocol command containing the payload
122 PayloadBuffer to the given MediaId. The security protocol command sent is
123 defined by SecurityProtocolId and contains the security protocol specific data
124 SecurityProtocolSpecificData. If the underlying protocol command requires a
125 specific padding for the command payload, the SendData function shall add padding
126 bytes to the command payload to satisfy the padding requirements.
127
128 For devices supporting the SCSI command set, the security protocol command is sent
129 using the SECURITY PROTOCOL OUT command defined in SPC-4.
130
131 For devices supporting the ATA command set, the security protocol command is sent
132 using one of the TRUSTED SEND commands defined in ATA8-ACS if PayloadBufferSize
133 is non-zero. If the PayloadBufferSize is zero, the security protocol command is
134 sent using the Trusted Non-Data command defined in ATA8-ACS.
135
136 If PayloadBuffer is NULL and PayloadBufferSize is non-zero, the function shall
137 return EFI_INVALID_PARAMETER.
138
139 If the given MediaId does not support security protocol commands, the function
140 shall return EFI_UNSUPPORTED. If there is no media in the device, the function
141 returns EFI_NO_MEDIA. If the MediaId is not the ID for the current media in the
142 device, the function returns EFI_MEDIA_CHANGED.
143
144 If the security protocol fails to complete within the Timeout period, the function
145 shall return EFI_TIMEOUT.
146
147 If the security protocol command completes without an error, the function shall return
148 EFI_SUCCESS. If the security protocol command completes with an error, the function
149 shall return EFI_DEVICE_ERROR.
150
151 @param This Indicates a pointer to the calling context.
152 @param MediaId ID of the medium to receive data from.
153 @param Timeout The timeout, in 100ns units, to use for the execution
154 of the security protocol command. A Timeout value of 0
155 means that this function will wait indefinitely for the
156 security protocol command to execute. If Timeout is greater
157 than zero, then this function will return EFI_TIMEOUT
158 if the time required to execute the send data command
159 is greater than Timeout.
160 @param SecurityProtocolId The value of the "Security Protocol" parameter of
161 the security protocol command to be sent.
162 @param SecurityProtocolSpecificData The value of the "Security Protocol Specific" parameter
163 of the security protocol command to be sent.
164 @param PayloadBufferSize Size in bytes of the payload data buffer.
165 @param PayloadBuffer A pointer to a destination buffer to store the security
166 protocol command specific payload data for the security
167 protocol command.
168
169 @retval EFI_SUCCESS The security protocol command completed successfully.
170 @retval EFI_UNSUPPORTED The given MediaId does not support security protocol commands.
171 @retval EFI_DEVICE_ERROR The security protocol command completed with an error.
172 @retval EFI_NO_MEDIA There is no media in the device.
173 @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.
174 @retval EFI_INVALID_PARAMETER The PayloadBuffer is NULL and PayloadBufferSize is non-zero.
175 @retval EFI_TIMEOUT A timeout occurred while waiting for the security
176 protocol command to execute.
177
178 **/
179 EFI_STATUS
180 EFIAPI
181 SecuritySendData (
182 IN EFI_STORAGE_SECURITY_COMMAND_PROTOCOL *This,
183 IN UINT32 MediaId,
184 IN UINT64 Timeout,
185 IN UINT8 SecurityProtocolId,
186 IN UINT16 SecurityProtocolSpecificData,
187 IN UINTN PayloadBufferSize,
188 IN VOID *PayloadBuffer
189 )
190 {
191 OPAL_PEI_DEVICE *PeiDev;
192
193 PeiDev = OPAL_PEI_DEVICE_FROM_THIS (This);
194 if (PeiDev == NULL) {
195 return EFI_DEVICE_ERROR;
196 }
197
198 return PeiDev->SscPpi->SendData (
199 PeiDev->SscPpi,
200 PeiDev->DeviceIndex,
201 SSC_PPI_GENERIC_TIMEOUT,
202 SecurityProtocolId,
203 SecurityProtocolSpecificData,
204 PayloadBufferSize,
205 PayloadBuffer
206 );
207 }
208
209 /**
210
211 The function returns whether or not the device is Opal Locked.
212 TRUE means that the device is partially or fully locked.
213 This will perform a Level 0 Discovery and parse the locking feature descriptor
214
215 @param[in] OpalDev Opal object to determine if locked.
216 @param[out] BlockSidSupported Whether device support BlockSid feature.
217
218 **/
219 BOOLEAN
220 IsOpalDeviceLocked(
221 OPAL_PEI_DEVICE *OpalDev,
222 BOOLEAN *BlockSidSupported
223 )
224 {
225 OPAL_SESSION Session;
226 OPAL_DISK_SUPPORT_ATTRIBUTE SupportedAttributes;
227 TCG_LOCKING_FEATURE_DESCRIPTOR LockingFeature;
228 UINT16 OpalBaseComId;
229 TCG_RESULT Ret;
230
231 Session.Sscp = &OpalDev->Sscp;
232 Session.MediaId = 0;
233
234 Ret = OpalGetSupportedAttributesInfo (&Session, &SupportedAttributes, &OpalBaseComId);
235 if (Ret != TcgResultSuccess) {
236 return FALSE;
237 }
238
239 Session.OpalBaseComId = OpalBaseComId;
240 *BlockSidSupported = SupportedAttributes.BlockSid == 1 ? TRUE : FALSE;
241
242 Ret = OpalGetLockingInfo(&Session, &LockingFeature);
243 if (Ret != TcgResultSuccess) {
244 return FALSE;
245 }
246
247 return OpalDeviceLocked (&SupportedAttributes, &LockingFeature);
248 }
249
250 /**
251 Unlock OPAL password for S3.
252
253 @param[in] OpalDev Opal object to unlock.
254
255 **/
256 VOID
257 UnlockOpalPassword (
258 IN OPAL_PEI_DEVICE *OpalDev
259 )
260 {
261 TCG_RESULT Result;
262 OPAL_SESSION Session;
263 BOOLEAN BlockSidSupport;
264 UINT32 PpStorageFlags;
265 BOOLEAN BlockSIDEnabled;
266
267 BlockSidSupport = FALSE;
268 if (IsOpalDeviceLocked (OpalDev, &BlockSidSupport)) {
269 ZeroMem(&Session, sizeof (Session));
270 Session.Sscp = &OpalDev->Sscp;
271 Session.MediaId = 0;
272 Session.OpalBaseComId = OpalDev->Device->OpalBaseComId;
273
274 Result = OpalUtilUpdateGlobalLockingRange (
275 &Session,
276 OpalDev->Device->Password,
277 OpalDev->Device->PasswordLength,
278 FALSE,
279 FALSE
280 );
281 DEBUG ((
282 DEBUG_INFO,
283 "%a() OpalUtilUpdateGlobalLockingRange() Result = 0x%x\n",
284 __FUNCTION__,
285 Result
286 ));
287 }
288
289 PpStorageFlags = Tcg2PhysicalPresenceLibGetManagementFlags ();
290 if ((PpStorageFlags & TCG2_BIOS_STORAGE_MANAGEMENT_FLAG_ENABLE_BLOCK_SID) != 0) {
291 BlockSIDEnabled = TRUE;
292 } else {
293 BlockSIDEnabled = FALSE;
294 }
295 if (BlockSIDEnabled && BlockSidSupport) {
296 DEBUG ((DEBUG_INFO, "OpalPassword: S3 phase send BlockSid command to device!\n"));
297 ZeroMem(&Session, sizeof (Session));
298 Session.Sscp = &OpalDev->Sscp;
299 Session.MediaId = 0;
300 Session.OpalBaseComId = OpalDev->Device->OpalBaseComId;
301 Result = OpalBlockSid (&Session, TRUE);
302 DEBUG ((
303 DEBUG_INFO,
304 "%a() OpalBlockSid() Result = 0x%x\n",
305 __FUNCTION__,
306 Result
307 ));
308 }
309 }
310
311 /**
312 Unlock the OPAL NVM Express and ATA devices for S3.
313
314 @param[in] SscPpi Pointer to the EDKII_PEI_STORAGE_SECURITY_CMD_PPI instance.
315
316 **/
317 VOID
318 UnlockOpalPasswordDevices (
319 IN EDKII_PEI_STORAGE_SECURITY_CMD_PPI *SscPpi
320 )
321 {
322 EFI_STATUS Status;
323 UINT8 *DevInfoBuffer;
324 UINT8 DummyData;
325 OPAL_DEVICE_LOCKBOX_DATA *DevInfo;
326 UINTN DevInfoLength;
327 EFI_DEVICE_PATH_PROTOCOL *SscDevicePath;
328 UINTN SscDevicePathLength;
329 UINTN SscDeviceNum;
330 UINTN SscDeviceIndex;
331 OPAL_PEI_DEVICE OpalDev;
332
333 //
334 // Get OPAL devices info from LockBox.
335 //
336 DevInfoBuffer = &DummyData;
337 DevInfoLength = sizeof (DummyData);
338 Status = RestoreLockBox (&mOpalDeviceLockBoxGuid, DevInfoBuffer, &DevInfoLength);
339 if (Status == EFI_BUFFER_TOO_SMALL) {
340 DevInfoBuffer = AllocatePages (EFI_SIZE_TO_PAGES (DevInfoLength));
341 if (DevInfoBuffer != NULL) {
342 Status = RestoreLockBox (&mOpalDeviceLockBoxGuid, DevInfoBuffer, &DevInfoLength);
343 }
344 }
345 if (DevInfoBuffer == NULL || DevInfoBuffer == &DummyData) {
346 return;
347 } else if (EFI_ERROR (Status)) {
348 FreePages (DevInfoBuffer, EFI_SIZE_TO_PAGES (DevInfoLength));
349 return;
350 }
351
352 //
353 // Go through all the devices managed by the SSC PPI instance.
354 //
355 Status = SscPpi->GetNumberofDevices (SscPpi, &SscDeviceNum);
356 if (EFI_ERROR (Status)) {
357 goto Exit;
358 }
359 for (SscDeviceIndex = 1; SscDeviceIndex <= SscDeviceNum; SscDeviceIndex++) {
360 Status = SscPpi->GetDevicePath (
361 SscPpi,
362 SscDeviceIndex,
363 &SscDevicePathLength,
364 &SscDevicePath
365 );
366 if (SscDevicePathLength <= sizeof (EFI_DEVICE_PATH_PROTOCOL)) {
367 //
368 // Device path validity check.
369 //
370 continue;
371 }
372
373 //
374 // Search the device in the restored LockBox.
375 //
376 for (DevInfo = (OPAL_DEVICE_LOCKBOX_DATA *) DevInfoBuffer;
377 (UINTN) DevInfo < ((UINTN) DevInfoBuffer + DevInfoLength);
378 DevInfo = (OPAL_DEVICE_LOCKBOX_DATA *) ((UINTN) DevInfo + DevInfo->Length)) {
379 //
380 // Find the matching device.
381 //
382 if ((DevInfo->DevicePathLength >= SscDevicePathLength) &&
383 (CompareMem (
384 DevInfo->DevicePath,
385 SscDevicePath,
386 SscDevicePathLength - sizeof (EFI_DEVICE_PATH_PROTOCOL)) == 0)) {
387 OpalDev.Signature = OPAL_PEI_DEVICE_SIGNATURE;
388 OpalDev.Sscp.ReceiveData = SecurityReceiveData;
389 OpalDev.Sscp.SendData = SecuritySendData;
390 OpalDev.Device = DevInfo;
391 OpalDev.Context = NULL;
392 OpalDev.SscPpi = SscPpi;
393 OpalDev.DeviceIndex = SscDeviceIndex;
394 UnlockOpalPassword (&OpalDev);
395 break;
396 }
397 }
398 }
399
400 Exit:
401 ZeroMem (DevInfoBuffer, DevInfoLength);
402 FreePages (DevInfoBuffer, EFI_SIZE_TO_PAGES (DevInfoLength));
403
404 }
405
406 /**
407 One notified function at the installation of EDKII_PEI_STORAGE_SECURITY_CMD_PPI.
408 It is to unlock OPAL password for S3.
409
410 @param[in] PeiServices Indirect reference to the PEI Services Table.
411 @param[in] NotifyDescriptor Address of the notification descriptor data structure.
412 @param[in] Ppi Address of the PPI that was installed.
413
414 @return Status of the notification.
415 The status code returned from this function is ignored.
416
417 **/
418 EFI_STATUS
419 EFIAPI
420 OpalPasswordStorageSecurityPpiNotify (
421 IN EFI_PEI_SERVICES **PeiServices,
422 IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDesc,
423 IN VOID *Ppi
424 )
425 {
426 DEBUG ((DEBUG_INFO, "%a entered at S3 resume!\n", __FUNCTION__));
427
428 UnlockOpalPasswordDevices ((EDKII_PEI_STORAGE_SECURITY_CMD_PPI *) Ppi);
429
430 DEBUG ((DEBUG_INFO, "%a exit at S3 resume!\n", __FUNCTION__));
431
432 return EFI_SUCCESS;
433 }
434
435
436 EFI_PEI_NOTIFY_DESCRIPTOR mOpalPasswordStorageSecurityPpiNotifyDesc = {
437 (EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
438 &gEdkiiPeiStorageSecurityCommandPpiGuid,
439 OpalPasswordStorageSecurityPpiNotify
440 };
441
442
443 /**
444 Main entry for this module.
445
446 @param FileHandle Handle of the file being invoked.
447 @param PeiServices Pointer to PEI Services table.
448
449 @return Status from PeiServicesNotifyPpi.
450
451 **/
452 EFI_STATUS
453 EFIAPI
454 OpalPasswordPeiInit (
455 IN EFI_PEI_FILE_HANDLE FileHandle,
456 IN CONST EFI_PEI_SERVICES **PeiServices
457 )
458 {
459 EFI_STATUS Status;
460 EFI_BOOT_MODE BootMode;
461
462 Status = PeiServicesGetBootMode (&BootMode);
463 if ((EFI_ERROR (Status)) || (BootMode != BOOT_ON_S3_RESUME)) {
464 return EFI_UNSUPPORTED;
465 }
466
467 DEBUG ((DEBUG_INFO, "%a: Enters in S3 path.\n", __FUNCTION__));
468
469 Status = PeiServicesNotifyPpi (&mOpalPasswordStorageSecurityPpiNotifyDesc);
470 ASSERT_EFI_ERROR (Status);
471 return Status;
472 }