예제 #1
0
파일: TcgMor.c 프로젝트: b-man/edk2
/**
  Send TPer Reset command to reset eDrive to lock all protected bands.
  Typically, there are 2 mechanism for resetting eDrive. They are:
  1. TPer Reset through IEEE 1667 protocol.
  2. TPer Reset through native TCG protocol.
  This routine will detect what protocol the attached eDrive comform to, TCG or
  IEEE 1667 protocol. Then send out TPer Reset command separately.

  @param[in] Ssp      The pointer to EFI_STORAGE_SECURITY_COMMAND_PROTOCOL instance.
  @param[in] MediaId  ID of the medium to receive data from or send data to.

**/
VOID
InitiateTPerReset (
  IN  EFI_STORAGE_SECURITY_COMMAND_PROTOCOL    *Ssp,
  IN  UINT32                                   MediaId
  )
{

  EFI_STATUS                                   Status;
  UINT8                                        *Buffer;
  UINTN                                        XferSize;
  UINTN                                        Len;
  UINTN                                        Index;
  BOOLEAN                                      TcgFlag;
  BOOLEAN                                      IeeeFlag;
  SUPPORTED_SECURITY_PROTOCOLS_PARAMETER_DATA  *Data;

  Buffer        = NULL;
  TcgFlag       = FALSE;
  IeeeFlag      = FALSE;

  //
  // ATA8-ACS 7.57.6.1 indicates the Transfer Length field requirements a multiple of 512.
  // If the length of the TRUSTED RECEIVE parameter data is greater than the Transfer Length,
  // then the device shall return the TRUSTED RECEIVE parameter data truncated to the requested Transfer Length.
  //
  Len           = ROUNDUP512(sizeof(SUPPORTED_SECURITY_PROTOCOLS_PARAMETER_DATA));
  Buffer        = AllocateZeroPool(Len);

  if (Buffer == NULL) {
    return;
  }

  //
  // When the Security Protocol field is set to 00h, and SP Specific is set to 0000h in a TRUSTED RECEIVE
  // command, the device basic information data shall be returned.
  //
  Status = Ssp->ReceiveData (
                  Ssp,
                  MediaId,
                  100000000,                    // Timeout 10-sec
                  0,                            // SecurityProtocol
                  0,                            // SecurityProtocolSpecifcData
                  Len,                          // PayloadBufferSize,
                  Buffer,                       // PayloadBuffer
                  &XferSize
                  );
  if (EFI_ERROR (Status)) {
    goto Exit;
  }

  //
  // In returned data, the ListLength field indicates the total length, in bytes,
  // of the supported security protocol list.
  //
  Data = (SUPPORTED_SECURITY_PROTOCOLS_PARAMETER_DATA*)Buffer;
  Len  = ROUNDUP512(sizeof (SUPPORTED_SECURITY_PROTOCOLS_PARAMETER_DATA) +
                    (Data->SupportedSecurityListLength[0] << 8) +
                    (Data->SupportedSecurityListLength[1])
                    );

  //
  // Free original buffer and allocate new buffer.
  //
  FreePool(Buffer);
  Buffer = AllocateZeroPool(Len);
  if (Buffer == NULL) {
    return;
  }

  //
  // Read full supported security protocol list from device.
  //
  Status = Ssp->ReceiveData (
                  Ssp,
                  MediaId,
                  100000000,                    // Timeout 10-sec
                  0,                            // SecurityProtocol
                  0,                            // SecurityProtocolSpecifcData
                  Len,                          // PayloadBufferSize,
                  Buffer,                       // PayloadBuffer
                  &XferSize
                  );

  if (EFI_ERROR (Status)) {
    goto Exit;
  }

  Data = (SUPPORTED_SECURITY_PROTOCOLS_PARAMETER_DATA*)Buffer;
  Len  = (Data->SupportedSecurityListLength[0] << 8) + Data->SupportedSecurityListLength[1];

  //
  // Iterate full supported security protocol list to check if TCG or IEEE 1667 protocol
  // is supported.
  //
  for (Index = 0; Index < Len; Index++) {
    if (Data->SupportedSecurityProtocol[Index] == SECURITY_PROTOCOL_TCG) {
      //
      // Found a  TCG device.
      //
      TcgFlag = TRUE;
      DEBUG ((EFI_D_INFO, "This device is a TCG protocol device\n"));
      break;
    }

    if (Data->SupportedSecurityProtocol[Index] == SECURITY_PROTOCOL_IEEE1667) {
      //
      // Found a IEEE 1667 device.
      //
      IeeeFlag = TRUE;
      DEBUG ((EFI_D_INFO, "This device is a IEEE 1667 protocol device\n"));
      break;
    }
  }

  if (!TcgFlag && !IeeeFlag) {
    DEBUG ((EFI_D_INFO, "Neither a TCG nor IEEE 1667 protocol device is found\n"));
    goto Exit;
  }

  if (TcgFlag) {
    //
    // As long as TCG protocol is supported, send out a TPer Reset
    // TCG command to the device via the TrustedSend command with a non-zero Transfer Length.
    //
    Status = Ssp->SendData (
                    Ssp,
                    MediaId,
                    100000000,                    // Timeout 10-sec
                    SECURITY_PROTOCOL_TCG,        // SecurityProtocol
                    0x0400,                       // SecurityProtocolSpecifcData
                    512,                          // PayloadBufferSize,
                    Buffer                        // PayloadBuffer
                    );

    if (!EFI_ERROR (Status)) {
      DEBUG ((EFI_D_INFO, "Send TPer Reset Command Successfully !\n"));
    } else {
      DEBUG ((EFI_D_INFO, "Send TPer Reset Command Fail !\n"));
    }
  }

  if (IeeeFlag) {
    //
    // TBD : Perform a TPer Reset via IEEE 1667 Protocol
    //
    DEBUG ((EFI_D_INFO, "IEEE 1667 Protocol didn't support yet!\n"));
  }

Exit:

  if (Buffer != NULL) {
    FreePool(Buffer);
  }
}
//
// TDS 
//
EFI_STATUS
BBTestSendDataConformanceAutoTest (
  IN EFI_BB_TEST_PROTOCOL       *This,
  IN VOID                       *ClientInterface,
  IN EFI_TEST_LEVEL             TestLevel,
  IN EFI_HANDLE                 SupportHandle
  )
{ 
  EFI_STANDARD_TEST_LIBRARY_PROTOCOL            *StandardLib = NULL;
  EFI_STATUS                                    Status;
  EFI_STORAGE_SECURITY_COMMAND_PROTOCOL         *StorageSecurityCommand = NULL;
  EFI_STORAGE_SECURITY_COMMAND_PROTOCOL         *StorageSecurityTemp = NULL;
  EFI_BLOCK_IO_PROTOCOL                         *BlockIo = NULL;
  UINTN                                         Index;
  UINTN                                         NoHandles;
  EFI_HANDLE                                    *HandleBuffer = NULL;
  EFI_TEST_ASSERTION                            AssertionType;
  UINT8                                         *DataBuffer = NULL;
  UINTN                                         RcvDataSize;
  SUPPORTED_SECURITY_PROTOCOLS_PARAMETER_DATA   *Data = NULL; 
  UINTN                                         Len;
  BOOLEAN                                       TcgFlag = FALSE;
  BOOLEAN                                       IeeeFlag = FALSE;  


  //
  // Get the Standard Library Interface
  //
  Status = gtBS->HandleProtocol (
                   SupportHandle,
                   &gEfiStandardTestLibraryGuid,
                   &StandardLib
                   );

  if (EFI_ERROR(Status)) {
    StandardLib->RecordAssertion (
                   StandardLib,
                   EFI_TEST_ASSERTION_FAILED,
                   gTestGenericFailureGuid,
                   L"BS.HandleProtocol - Handle standard test library",
                   L"%a:%d:Status - %r",
                   __FILE__,
                   (UINTN)__LINE__,
                   Status
                   );
    return Status;
  }

  StorageSecurityCommand = (EFI_STORAGE_SECURITY_COMMAND_PROTOCOL *)ClientInterface;

  //
  // Locate Block IO protocol on same handler for test
  //
  Status = gtBS->LocateHandleBuffer (
                   ByProtocol,
                   &gEfiStorageSecurityCommandProtocolGuid,
                   NULL,
                   &NoHandles,
                   &HandleBuffer
                   );
  for (Index = 0; Index < NoHandles; Index++) {
     Status = gtBS->HandleProtocol (
                      HandleBuffer[Index],
                      &gEfiStorageSecurityCommandProtocolGuid,
                      &StorageSecurityTemp
                      );
     if (Status == EFI_SUCCESS && StorageSecurityTemp == StorageSecurityCommand) {
       Status = gtBS->HandleProtocol (
                        HandleBuffer[Index],
                        &gEfiBlockIoProtocolGuid,
                        &BlockIo
                        );
       if (Status != EFI_SUCCESS) {
         StandardLib->RecordAssertion (
                        StandardLib,
                        EFI_TEST_ASSERTION_FAILED,
                        gTestGenericFailureGuid,
                        L"Can not find BlockIo Protocol on Current Handler",
                        L"%a:%d:",
                        __FILE__,
                        (UINTN)__LINE__
                        );
         goto EXIT;
       }
       
    }
  }

  
  DataBuffer = AllocateZeroPool(512);
  if (DataBuffer == NULL) {
    StandardLib->RecordAssertion (
                   StandardLib,
                   EFI_TEST_ASSERTION_FAILED,
                   gTestGenericFailureGuid,
                   L"BS.AllocateMemory - Allocate Memory Fail",
                   L"%a:%d:",
                   __FILE__,
                   (UINTN)__LINE__
                   );
    goto EXIT;
  }

  //
  // Get device capiblity 
  //
  Status = StorageSecurityCommand->ReceiveData (
                                     StorageSecurityCommand,
                                     BlockIo->Media->MediaId,
                                     100000000,                    // Timeout 10-sec
                                     0,                            // SecurityProtocol
                                     0,                            // SecurityProtocolSpecifcData
                                     512,                          // PayloadBufferSize,
                                     DataBuffer,                   // PayloadBuffer
                                     &RcvDataSize
                                     );
  
  if (EFI_ERROR (Status)) {
    goto EXIT;
  }
  
  //
  // In returned data, the ListLength field indicates the total length, in bytes, 
  // of the supported security protocol list.
  //
  Data = (SUPPORTED_SECURITY_PROTOCOLS_PARAMETER_DATA*)DataBuffer;
  Len  = ROUNDUP512(sizeof (SUPPORTED_SECURITY_PROTOCOLS_PARAMETER_DATA) + \
                    (Data->SupportedSecurityListLength[0] << 8) +          \
                    (Data->SupportedSecurityListLength[1]));
  
  //
  // Free original buffer and allocate new buffer.
  //
  FreePool(DataBuffer);
  DataBuffer = NULL;
  DataBuffer = AllocateZeroPool(Len);
  if (DataBuffer == NULL) {
    StandardLib->RecordAssertion (
                    StandardLib,
                    EFI_TEST_ASSERTION_FAILED,
                    gTestGenericFailureGuid,
                    L"BS.AllocateMemory - Allocate Memory Fail",
                    L"%a:%d:",
                    __FILE__,
                    (UINTN)__LINE__
                    );
    goto EXIT;
  }
  
  //
  // Read full supported security protocol list from device.
  //
  Status = StorageSecurityCommand->ReceiveData (
                                     StorageSecurityCommand,
                                     BlockIo->Media->MediaId,
                                     100000000,                    // Timeout 10-sec
                                     0,                            // SecurityProtocol
                                     0,                            // SecurityProtocolSpecifcData
                                     Len,                          // PayloadBufferSize,
                                     DataBuffer,                   // PayloadBuffer
                                     &RcvDataSize
                                     );
  
  if (EFI_ERROR (Status)) {
    goto EXIT;
  }
  
  Data = (SUPPORTED_SECURITY_PROTOCOLS_PARAMETER_DATA*)DataBuffer;
  Len  = (Data->SupportedSecurityListLength[0] << 8) + Data->SupportedSecurityListLength[1];

  //
  // Iterate full supported security protocol list to check if TCG or IEEE 1667 protocol
  // is supported.
  //
  for (Index = 0; Index < Len; Index++) {
    if (Data->SupportedSecurityProtocol[Index] == SECURITY_PROTOCOL_TCG) {
      //
      // Found a  TCG device.
      //
      TcgFlag = TRUE;
      DEBUG ((EFI_D_INFO, "This device is a TCG protocol device\n"));
      break;
    }

    if (Data->SupportedSecurityProtocol[Index] == SECURITY_PROTOCOL_IEEE1667) {
      //
      // Found a IEEE 1667 device.
      //
      IeeeFlag = TRUE;
      DEBUG ((EFI_D_INFO, "This device is a IEEE 1667 protocol device\n"));
      break;
    }    
  }


  //
  // Main test process 
  //
  if (TcgFlag) {
    if (BlockIo->Media->MediaPresent) {
      //
      // EFI_MEDIA_CHANGED
      //
      //
      // As long as TCG protocol is supported, send out a TPer Reset 
      // TCG command to the device via the TrustedSend command with a non-zero Transfer Length. 
      //
      Status = StorageSecurityCommand->SendData (
                                         StorageSecurityCommand,
                                         BlockIo->Media->MediaId + 1,
                                         100000000,                  // Timeout 10-sec
                                         SECURITY_PROTOCOL_TCG,      // SecurityProtocol
                                         0x0400,                     // SecurityProtocolSpecifcData
                                         Len,                        // PayloadBufferSize,
                                         DataBuffer                  // PayloadBuffer
                                         );
      if (Status == EFI_MEDIA_CHANGED) {
        AssertionType = EFI_TEST_ASSERTION_PASSED;
      } else {
        AssertionType = EFI_TEST_ASSERTION_FAILED;
      }
      StandardLib->RecordAssertion (
                     StandardLib,
                     AssertionType,
                     gStorageSecurityCommandConformanceTestAssertionGuid006,
                     L"EFI_STORAGE_SECURITY_COMMAND.SendData - TRUSTED SendData to send a command with invalid media ID",
                     L"%a:%d: Status: %r ExpectedStatus: %r",
                     __FILE__,
                     (UINTN)__LINE__,
                     Status,
                     EFI_MEDIA_CHANGED
                     );

      //
      // EFI_INVALID_PARAMETER
      //
      Status = StorageSecurityCommand->SendData (
                                         StorageSecurityCommand,
                                         BlockIo->Media->MediaId,
                                         100000000,                  // Timeout 10-sec
                                         SECURITY_PROTOCOL_TCG,      // SecurityProtocol
                                         0x0400,                     // SecurityProtocolSpecifcData
                                         Len,                        // PayloadBufferSize,
                                         NULL                        // PayloadBuffer
                                         );
      if (Status == EFI_INVALID_PARAMETER) {
        AssertionType = EFI_TEST_ASSERTION_PASSED;
      } else {
        AssertionType = EFI_TEST_ASSERTION_FAILED;
      }
      StandardLib->RecordAssertion (
                     StandardLib,
                     AssertionType,
                     gStorageSecurityCommandConformanceTestAssertionGuid007,
                     L"EFI_STORAGE_SECURITY_COMMAND.SendData - TRUSTED SendData to send a command with null payload buffer",
                     L"%a:%d: Status: %r ExpectedStatus: %r",
                     __FILE__,
                     (UINTN)__LINE__,
                     Status,
                     EFI_INVALID_PARAMETER
                     );
    }else { 
      //
      // EFI_NO_MEDIA
      //
      //
      // As long as TCG protocol is supported, send out a TPer Reset 
      // TCG command to the device via the TrustedSend command with a non-zero Transfer Length. 
      //
      Status = StorageSecurityCommand->SendData (
                                         StorageSecurityCommand,
                                         BlockIo->Media->MediaId,
                                         100000000,                  // Timeout 10-sec
                                         SECURITY_PROTOCOL_TCG,      // SecurityProtocol
                                         0x0400,                     // SecurityProtocolSpecifcData
                                         Len,                        // PayloadBufferSize,
                                         DataBuffer                  // PayloadBuffer
                                         );
      if (Status == EFI_NO_MEDIA) {
        AssertionType = EFI_TEST_ASSERTION_PASSED;
      } else {
        AssertionType = EFI_TEST_ASSERTION_FAILED;
      }
      StandardLib->RecordAssertion (
                     StandardLib,
                     AssertionType,
                     gStorageSecurityCommandConformanceTestAssertionGuid008,
                     L"EFI_STORAGE_SECURITY_COMMAND.SendData - TRUSTED SendData to send command to a device with no media",
                     L"%a:%d: Status: %r ExpectedStatus: %r",
                     __FILE__,
                     (UINTN)__LINE__,
                     Status,
                     EFI_NO_MEDIA
                     );

    }
  }
  
EXIT:
  if (HandleBuffer != NULL) {
    gtBS->FreePool (HandleBuffer);
  }

  if (DataBuffer != NULL) {
    gtBS->FreePool(DataBuffer);
  }

  return EFI_SUCCESS;
}