18#include "../NvmExpress.h"
19#include "../NvmExpressBlockIo.h"
20#include "../NvmExpressMediaSanitize.h"
21#include "../NvmExpressHci.h"
34NvmeDeviceUnitTestPassthru (
36 IN UINT32 NamespaceId,
53 Command = Packet->NvmeCmd;
54 Completion = Packet->NvmeCompletion;
60 switch (Command->Cdw0.Opcode) {
61 case NVME_ADMIN_FORMAT_NVM_CMD:
62 UT_LOG_VERBOSE (
"%a: Opcode = NVME_ADMIN_FORMAT_NVM_CMD\n", __func__);
69 if (FormatNvmCdw10.Ses > 0x2) {
70 Cqe->Sct = NVME_CQE_SCT_GENERIC_CMD_STATUS;
71 Cqe->Sc = NVME_CQE_SC_INVALID_FIELD_IN_CMD;
73 return EFI_INVALID_PARAMETER;
79 if (FormatNvmCdw10.Lbaf > 0x1) {
80 Cqe->Sct = NVME_CQE_SCT_GENERIC_CMD_STATUS;
81 Cqe->Sc = NVME_CQE_SC_INVALID_FIELD_IN_CMD;
83 return EFI_INVALID_PARAMETER;
87 case NVME_ADMIN_SANITIZE_CMD:
88 UT_LOG_VERBOSE (
"%a: Opcode = NVME_ADMIN_SANITIZE_CMD\n", __func__);
95 if (SanitizeCdw1011.Sanact > 0x4) {
96 Cqe->Sct = NVME_CQE_SCT_GENERIC_CMD_STATUS;
97 Cqe->Sc = NVME_CQE_SC_INVALID_FIELD_IN_CMD;
99 return EFI_INVALID_PARAMETER;
105 if (((SanitizeCdw1011.Sanact == SANITIZE_ACTION_OVERWRITE) && (SanitizeCdw1011.Ovrpat != 0xDEADBEEF)) ||
106 ((SanitizeCdw1011.Sanact != SANITIZE_ACTION_OVERWRITE) && (SanitizeCdw1011.Ovrpat != 0)))
108 Cqe->Sct = NVME_CQE_SCT_GENERIC_CMD_STATUS;
109 Cqe->Sc = NVME_CQE_SC_INVALID_FIELD_IN_CMD;
111 return EFI_INVALID_PARAMETER;
116 UT_LOG_VERBOSE (
"%a: Invalid Opcode = 0x%x!!!\n", __func__, Command->Cdw0.Opcode);
138 IN UINT32 NamespaceId,
150 CommandPacket.NvmeCmd = &Command;
151 CommandPacket.NvmeCompletion = &Completion;
152 Command.Cdw0.Opcode = NVME_ADMIN_IDENTIFY_CMD;
153 Command.Nsid = NamespaceId;
154 CommandPacket.TransferBuffer = Buffer;
156 CommandPacket.CommandTimeout = NVME_GENERIC_TIMEOUT;
157 CommandPacket.QueueType = NVME_ADMIN_QUEUE;
162 CommandPacket.NvmeCmd->Cdw10 = 0;
163 CommandPacket.NvmeCmd->Flags = CDW10_VALID;
242 UINTN NumberOfBlocks;
249 return EFI_INVALID_PARAMETER;
254 if (MediaId != Media->
MediaId) {
255 return EFI_MEDIA_CHANGED;
258 if (Buffer ==
NULL) {
259 return EFI_INVALID_PARAMETER;
262 if (BufferSize == 0) {
267 if ((BufferSize % BlockSize) != 0) {
268 return EFI_BAD_BUFFER_SIZE;
271 NumberOfBlocks = BufferSize / BlockSize;
272 if ((Lba + NumberOfBlocks - 1) > Media->
LastBlock) {
273 return EFI_INVALID_PARAMETER;
277 if ((IoAlign > 0) && (((
UINTN)Buffer & (IoAlign - 1)) != 0)) {
278 return EFI_INVALID_PARAMETER;
281 Device = NVME_DEVICE_PRIVATE_DATA_FROM_BLOCK_IO (This);
282 Status = NvmeUnitTestRead (Device, Buffer, Lba, NumberOfBlocks);
311 UINTN NumberOfBlocks;
318 return EFI_INVALID_PARAMETER;
323 if (MediaId != Media->
MediaId) {
324 return EFI_MEDIA_CHANGED;
327 if (Buffer ==
NULL) {
328 return EFI_INVALID_PARAMETER;
331 if (BufferSize == 0) {
336 if ((BufferSize % BlockSize) != 0) {
337 return EFI_BAD_BUFFER_SIZE;
340 NumberOfBlocks = BufferSize / BlockSize;
341 if ((Lba + NumberOfBlocks - 1) > Media->
LastBlock) {
342 return EFI_INVALID_PARAMETER;
346 if ((IoAlign > 0) && (((
UINTN)Buffer & (IoAlign - 1)) != 0)) {
347 return EFI_INVALID_PARAMETER;
350 Device = NVME_DEVICE_PRIVATE_DATA_FROM_BLOCK_IO (This);
351 Status = NvmeUnitTestWrite (Device, Buffer, Lba, NumberOfBlocks);
382 UINTN NumberOfBlocks;
390 return EFI_INVALID_PARAMETER;
395 if (MediaId != Media->
MediaId) {
396 return EFI_MEDIA_CHANGED;
399 if (Buffer ==
NULL) {
400 return EFI_INVALID_PARAMETER;
404 if ((BufferSize % BlockSize) != 0) {
405 return EFI_BAD_BUFFER_SIZE;
408 NumberOfBlocks = BufferSize / BlockSize;
409 if ((Lba + NumberOfBlocks - 1) > Media->
LastBlock) {
410 return EFI_INVALID_PARAMETER;
414 if ((IoAlign > 0) && (((
UINTN)Buffer & (IoAlign - 1)) != 0)) {
415 return EFI_INVALID_PARAMETER;
418 Device = NVME_DEVICE_PRIVATE_DATA_FROM_BLOCK_IO2 (This);
419 Status = NvmeUnitTestRead (Device, Buffer, Lba, NumberOfBlocks);
450 UINTN NumberOfBlocks;
458 return EFI_INVALID_PARAMETER;
463 if (MediaId != Media->
MediaId) {
464 return EFI_MEDIA_CHANGED;
467 if (Buffer ==
NULL) {
468 return EFI_INVALID_PARAMETER;
472 if ((BufferSize % BlockSize) != 0) {
473 return EFI_BAD_BUFFER_SIZE;
476 NumberOfBlocks = BufferSize / BlockSize;
477 if ((Lba + NumberOfBlocks - 1) > Media->
LastBlock) {
478 return EFI_INVALID_PARAMETER;
482 if ((IoAlign > 0) && (((
UINTN)Buffer & (IoAlign - 1)) != 0)) {
483 return EFI_INVALID_PARAMETER;
486 Device = NVME_DEVICE_PRIVATE_DATA_FROM_BLOCK_IO2 (This);
487 Status = NvmeUnitTestWrite (Device, Buffer, Lba, NumberOfBlocks);
499NvmeDestroyDeviceInstance (
510 FreePool ((*ppDevice)->Controller->ControllerData);
511 (*ppDevice)->Controller->ControllerData =
NULL;
514 (*ppDevice)->Controller =
NULL;
519 return UNIT_TEST_PASSED;
529NvmeCreateDeviceInstance (
539 Private->Signature = NVME_CONTROLLER_PRIVATE_DATA_SIGNATURE;
546 Private->SqTdbl[0].Sqt = 0;
547 Private->SqTdbl[1].Sqt = 0;
548 Private->SqTdbl[2].Sqt = 0;
549 Private->CqHdbl[0].Cqh = 0;
550 Private->CqHdbl[1].Cqh = 0;
551 Private->CqHdbl[2].Cqh = 0;
552 Private->AsyncSqHead = 0;
556 UT_LOG_VERBOSE (
"%a: Allocated and Initialized NVME_CONTROLLER_PRIVATE_DATA\n", __func__);
557 UT_LOG_VERBOSE (
"%a: Allocated and Initialized NVME_ADMIN_CONTROLLER_DATA\n", __func__);
559 Private->ControllerData->Nn = 1;
560 Private->ControllerData->Sanicap.Bes = 1;
561 Private->ControllerData->Sanicap.Ces = 1;
562 Private->ControllerData->Sanicap.Ows = 1;
565 UT_LOG_VERBOSE (
"%a: Allocated and Initialized NVME_ADMIN_NAMESPACE_DATA\n", __func__);
572 Device->Signature = NVME_DEVICE_PRIVATE_DATA_SIGNATURE;
573 Device->NamespaceId = 0;
574 Device->NamespaceUuid = 1;
576 Device->Controller = Private;
587 Device->Media.
BlockSize = (UINT32)(1 << 9);
593 Device->BlockIo.
Revision = EFI_BLOCK_IO_PROTOCOL_REVISION2;
594 Device->BlockIo.
Media = &Device->Media;
598 Device->BlockIo2.
Media = &Device->Media;
602 Device->MediaSanitize.
Revision = MEDIA_SANITIZE_PROTOCOL_REVISION;
603 Device->MediaSanitize.
Media = &Device->Media;
604 Device->MediaSanitize.MediaClear = NvmExpressMediaClear;
605 Device->MediaSanitize.MediaPurge = NvmExpressMediaPurge;
606 Device->MediaSanitize.MediaFormat = NvmExpressMediaFormat;
608 Device->Controller->Passthru.Mode = 0;
609 Device->Controller->Passthru.PassThru = NvmeDeviceUnitTestPassthru;
610 Device->Controller->Passthru.BuildDevicePath =
NULL;
611 Device->Controller->Passthru.GetNamespace =
NULL;
612 Device->Controller->Passthru.GetNextNamespace =
NULL;
617 UT_LOG_VERBOSE (
"%a: Allocated and Initialized NVME_DEVICE_PRIVATE_DATA\n", __func__);
619 return UNIT_TEST_PASSED;
629MediaSanitizePurgeUnitTest (
634 UINT32 OverwritePattern;
639 UnitTestStatus = UNIT_TEST_PASSED;
643 UnitTestStatus = NvmeCreateDeviceInstance (&NvmeDevice);
648 UT_LOG_VERBOSE (
"%a: Create Device Instance Status = 0x%x\n", __func__, UnitTestStatus);
650 UT_LOG_VERBOSE (
"%a: Device->BlockIo = 0x%x\n", __func__, NvmeDevice->BlockIo);
651 UT_LOG_VERBOSE (
"%a: Device->Signature = 0x%x\n", __func__, NvmeDevice->Signature);
656 PurgeAction = SANITIZE_ACTION_BLOCK_ERASE;
657 OverwritePattern = 0;
659 Status = NvmExpressMediaPurge (
660 &NvmeDevice->MediaSanitize,
668 UnitTestStatus = NvmeDestroyDeviceInstance (&NvmeDevice);
670 return UNIT_TEST_PASSED;
680NvmeSanitizeUnitTest (
685 UINT32 SanitizeAction;
686 UINT32 NoDeallocateAfterSanitize;
687 UINT32 OverwritePattern;
693 UnitTestStatus = UNIT_TEST_PASSED;
696 SanitizeAction = SANITIZE_ACTION_BLOCK_ERASE;
697 NoDeallocateAfterSanitize = 0;
698 OverwritePattern = 0;
700 UnitTestStatus = NvmeCreateDeviceInstance (&NvmeDevice);
705 UT_LOG_VERBOSE (
"%a: Create Device Instance Status = 0x%x\n", __func__, UnitTestStatus);
707 UT_LOG_VERBOSE (
"%a: Device->BlockIo = 0x%x\n", __func__, NvmeDevice->BlockIo);
708 UT_LOG_VERBOSE (
"%a: Device->Signature = 0x%x\n", __func__, NvmeDevice->Signature);
713 SanitizeAction = SANITIZE_ACTION_BLOCK_ERASE;
714 NoDeallocateAfterSanitize = 0;
715 OverwritePattern = 0;
717 Status = NvmExpressSanitize (
718 &NvmeDevice->BlockIo,
721 NoDeallocateAfterSanitize,
730 SanitizeAction = SANITIZE_ACTION_CRYPTO_ERASE;
731 NoDeallocateAfterSanitize = 0;
732 OverwritePattern = 0;
734 Status = NvmExpressSanitize (
735 &NvmeDevice->BlockIo,
738 NoDeallocateAfterSanitize,
747 SanitizeAction = SANITIZE_ACTION_OVERWRITE;
748 NoDeallocateAfterSanitize = 0;
749 OverwritePattern = 0xDEADBEEF;
751 Status = NvmExpressSanitize (
752 &NvmeDevice->BlockIo,
755 NoDeallocateAfterSanitize,
764 SanitizeAction = SANITIZE_ACTION_BLOCK_ERASE;
765 NoDeallocateAfterSanitize = 0;
766 OverwritePattern = 0xDEADBEEF;
768 Status = NvmExpressSanitize (
769 &NvmeDevice->BlockIo,
772 NoDeallocateAfterSanitize,
781 SanitizeAction = SANITIZE_ACTION_OVERWRITE;
782 NoDeallocateAfterSanitize = 0;
783 OverwritePattern = 0;
785 Status = NvmExpressSanitize (
786 &NvmeDevice->BlockIo,
789 NoDeallocateAfterSanitize,
795 UnitTestStatus = NvmeDestroyDeviceInstance (&NvmeDevice);
797 return UNIT_TEST_PASSED;
807NvmeFormatNvmUnitTest (
820 UnitTestStatus = UNIT_TEST_PASSED;
823 UnitTestStatus = NvmeCreateDeviceInstance (&NvmeDevice);
828 UT_LOG_VERBOSE (
"%a: Create Device Instance Status = 0x%x\n", __func__, UnitTestStatus);
830 UT_LOG_VERBOSE (
"%a: Device->BlockIo = 0x%x\n", __func__, NvmeDevice->BlockIo);
831 UT_LOG_VERBOSE (
"%a: Device->Signature = 0x%x\n", __func__, NvmeDevice->Signature);
836 Ses = SES_USER_DATA_ERASE;
838 Status = NvmExpressFormatNvm (
839 &NvmeDevice->BlockIo,
850 Ses = SES_CRYPTO_ERASE;
852 Status = NvmExpressFormatNvm (
853 &NvmeDevice->BlockIo,
864 Ses = SES_USER_DATA_ERASE;
866 Status = NvmExpressFormatNvm (
867 &NvmeDevice->BlockIo,
880 Status = NvmExpressFormatNvm (
881 &NvmeDevice->BlockIo,
889 UnitTestStatus = NvmeDestroyDeviceInstance (&NvmeDevice);
891 return UNIT_TEST_PASSED;
916 return UNIT_TEST_PASSED;
930MediaSanitizeUnitTestEntry (
935 UNIT_TEST_FRAMEWORK_HANDLE Framework;
936 UNIT_TEST_SUITE_HANDLE NvmeFormatNvmTestSuite;
937 UNIT_TEST_SUITE_HANDLE NvmeSanitizeTestSuite;
938 UNIT_TEST_SUITE_HANDLE MediaSanitizeProtocolTestSuite;
942 #define UNIT_TEST_NAME "Media Sanitize Protocol Unit Test"
943 #define UNIT_TEST_VERSION "1.0"
945 DEBUG ((DEBUG_INFO,
"%a v%a\n", UNIT_TEST_NAME, UNIT_TEST_VERSION));
957 if (EFI_ERROR (Status)) {
958 DEBUG ((DEBUG_ERROR,
"Failed in InitUnitTestFramework. Status = %r\n", Status));
966 &NvmeFormatNvmTestSuite,
968 "NVM Express Format NVM Test Suite",
969 "Nvm.Express.Format.Nvm",
974 if (EFI_ERROR (Status)) {
975 DEBUG ((DEBUG_ERROR,
"Failed in CreateUnitTestSuite for NvmeFormatNvmTestSuite. Status = %r\n", Status));
976 Status = EFI_OUT_OF_RESOURCES;
984 NvmeFormatNvmTestSuite,
985 "Baseline Format NVM Unit Test",
997 NvmeFormatNvmTestSuite,
998 "Admin Format NVM Command Unit Test",
1000 NvmeFormatNvmUnitTest,
1010 &NvmeSanitizeTestSuite,
1012 "NVM Express Sanitize Test Suite",
1013 "Nvm.Express.Sanitize",
1018 if (EFI_ERROR (Status)) {
1019 DEBUG ((DEBUG_ERROR,
"Failed in CreateUnitTestSuite for NvmeSanitizTestSuite. Status = %r\n", Status));
1020 Status = EFI_OUT_OF_RESOURCES;
1028 NvmeSanitizeTestSuite,
1029 "Baseline Sanitize Unit Test",
1041 NvmeSanitizeTestSuite,
1042 "Admin Sanitize Command Unit Test",
1044 NvmeSanitizeUnitTest,
1054 &MediaSanitizeProtocolTestSuite,
1056 "Media Sanitize Protocol Test Suite",
1057 "Media.Sanitize.Protocol",
1062 if (EFI_ERROR (Status)) {
1063 DEBUG ((DEBUG_ERROR,
"Failed in CreateUnitTestSuite for MediaSanitizeProtocolTestSuite. Status = %r\n", Status));
1064 Status = EFI_OUT_OF_RESOURCES;
1072 MediaSanitizeProtocolTestSuite,
1073 "Baseline MediaSanitize Unit Test",
1085 MediaSanitizeProtocolTestSuite,
1086 "Protocol Media Sanitize Unit Test",
1088 MediaSanitizePurgeUnitTest,
1110#define MediaSanitizeUnitTestMain main
1122MediaSanitizeUnitTestMain (
1127 return MediaSanitizeUnitTestEntry ();
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
VOID *EFIAPI ZeroMem(OUT VOID *Buffer, IN UINTN Length)
VOID *EFIAPI AllocateZeroPool(IN UINTN AllocationSize)
VOID EFIAPI FreePool(IN VOID *Buffer)
#define DEBUG(Expression)
EFI_STATUS EFIAPI NvmeBlockIoWriteBlocks(IN EFI_BLOCK_IO_PROTOCOL *This, IN UINT32 MediaId, IN EFI_LBA Lba, IN UINTN BufferSize, IN VOID *Buffer)
EFI_STATUS EFIAPI NvmeBlockIoReadBlocksEx(IN EFI_BLOCK_IO2_PROTOCOL *This, IN UINT32 MediaId, IN EFI_LBA Lba, IN OUT EFI_BLOCK_IO2_TOKEN *Token, IN UINTN BufferSize, OUT VOID *Buffer)
EFI_STATUS EFIAPI NvmeBlockIoReadBlocks(IN EFI_BLOCK_IO_PROTOCOL *This, IN UINT32 MediaId, IN EFI_LBA Lba, IN UINTN BufferSize, OUT VOID *Buffer)
EFI_STATUS EFIAPI NvmeBlockIoWriteBlocksEx(IN EFI_BLOCK_IO2_PROTOCOL *This, IN UINT32 MediaId, IN EFI_LBA Lba, IN OUT EFI_BLOCK_IO2_TOKEN *Token, IN UINTN BufferSize, IN VOID *Buffer)
EFI_STATUS NvmeIdentifyNamespace(IN NVME_CONTROLLER_PRIVATE_DATA *Private, IN UINT32 NamespaceId, IN VOID *Buffer)
EFI_STATUS EFIAPI RunAllTestSuites(IN UNIT_TEST_FRAMEWORK_HANDLE FrameworkHandle)
#define UT_ASSERT_NOT_NULL(Pointer)
#define UT_ASSERT_NOT_EQUAL(ValueA, ValueB)
#define UT_ASSERT_EQUAL(ValueA, ValueB)
#define UT_ASSERT_STATUS_EQUAL(Status, Expected)
EFI_STATUS EFIAPI CreateUnitTestSuite(OUT UNIT_TEST_SUITE_HANDLE *SuiteHandle, IN UNIT_TEST_FRAMEWORK_HANDLE FrameworkHandle, IN CHAR8 *Title, IN CHAR8 *Name, IN UNIT_TEST_SUITE_SETUP Setup OPTIONAL, IN UNIT_TEST_SUITE_TEARDOWN Teardown OPTIONAL)
EFI_STATUS EFIAPI FreeUnitTestFramework(IN UNIT_TEST_FRAMEWORK_HANDLE FrameworkHandle)
#define UT_LOG_VERBOSE(Format,...)
EFI_STATUS EFIAPI AddTestCase(IN UNIT_TEST_SUITE_HANDLE SuiteHandle, IN CHAR8 *Description, IN CHAR8 *Name, IN UNIT_TEST_FUNCTION Function, IN UNIT_TEST_PREREQUISITE Prerequisite OPTIONAL, IN UNIT_TEST_CLEANUP CleanUp OPTIONAL, IN UNIT_TEST_CONTEXT Context OPTIONAL)
EFI_STATUS EFIAPI InitUnitTestFramework(OUT UNIT_TEST_FRAMEWORK_HANDLE *FrameworkHandle, IN CHAR8 *Title, IN CHAR8 *ShortTitle, IN CHAR8 *VersionString)
#define UT_ASSERT_NOT_EFI_ERROR(Status)
EFI_BLOCK_IO_MEDIA * Media
EFI_BLOCK_IO_MEDIA * Media
EFI_BLOCK_IO_MEDIA * Media
UINT32 LogicalBlocksPerPhysicalBlock