TianoCore EDK2 master
Loading...
Searching...
No Matches
MediaSanitizeUnitTest.c
1
8#include <Uefi.h>
9#include <Library/BaseLib.h>
11#include <Library/DebugLib.h>
12#include <Library/UnitTestLib.h>
14#include <Protocol/BlockIo.h>
17
18#include "../NvmExpress.h"
19#include "../NvmExpressBlockIo.h"
20#include "../NvmExpressMediaSanitize.h"
21#include "../NvmExpressHci.h"
22
33EFIAPI
34NvmeDeviceUnitTestPassthru (
36 IN UINT32 NamespaceId,
38 IN EFI_EVENT Event OPTIONAL
39 )
40{
41 //
42 // Parse command packet for unit testing
43 //
46 NVME_CQ *Cqe;
47 NVME_ADMIN_FORMAT_NVM FormatNvmCdw10;
48 NVME_ADMIN_SANITIZE SanitizeCdw1011;
49
50 ASSERT (This);
51 ASSERT (Packet);
52
53 Command = Packet->NvmeCmd;
54 Completion = Packet->NvmeCompletion;
55 Cqe = (NVME_CQ *)Completion;
56
57 ZeroMem (&FormatNvmCdw10, sizeof (NVME_ADMIN_FORMAT_NVM));
58 ZeroMem (&SanitizeCdw1011, sizeof (NVME_ADMIN_SANITIZE));
59
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__);
63
64 CopyMem (&FormatNvmCdw10, &Command->Cdw10, sizeof (NVME_ADMIN_FORMAT_NVM));
65
66 //
67 // FormatNVM Check 1: Validate SES parameter
68 //
69 if (FormatNvmCdw10.Ses > 0x2) {
70 Cqe->Sct = NVME_CQE_SCT_GENERIC_CMD_STATUS;
71 Cqe->Sc = NVME_CQE_SC_INVALID_FIELD_IN_CMD;
72
73 return EFI_INVALID_PARAMETER;
74 }
75
76 //
77 // FormatNVM Check 2: Validate LbaIndex parameter
78 //
79 if (FormatNvmCdw10.Lbaf > 0x1) {
80 Cqe->Sct = NVME_CQE_SCT_GENERIC_CMD_STATUS;
81 Cqe->Sc = NVME_CQE_SC_INVALID_FIELD_IN_CMD;
82
83 return EFI_INVALID_PARAMETER;
84 }
85
86 break;
87 case NVME_ADMIN_SANITIZE_CMD:
88 UT_LOG_VERBOSE ("%a: Opcode = NVME_ADMIN_SANITIZE_CMD\n", __func__);
89
90 CopyMem (&SanitizeCdw1011, &Command->Cdw10, sizeof (NVME_ADMIN_SANITIZE));
91
92 //
93 // Sanitize Check 1: Validate Sanitize Action parameter
94 //
95 if (SanitizeCdw1011.Sanact > 0x4) {
96 Cqe->Sct = NVME_CQE_SCT_GENERIC_CMD_STATUS;
97 Cqe->Sc = NVME_CQE_SC_INVALID_FIELD_IN_CMD;
98
99 return EFI_INVALID_PARAMETER;
100 }
101
102 //
103 // Sanitize Check 2: Validate overwrite action with non-NULL overwrite pattern
104 //
105 if (((SanitizeCdw1011.Sanact == SANITIZE_ACTION_OVERWRITE) && (SanitizeCdw1011.Ovrpat != 0xDEADBEEF)) ||
106 ((SanitizeCdw1011.Sanact != SANITIZE_ACTION_OVERWRITE) && (SanitizeCdw1011.Ovrpat != 0)))
107 {
108 Cqe->Sct = NVME_CQE_SCT_GENERIC_CMD_STATUS;
109 Cqe->Sc = NVME_CQE_SC_INVALID_FIELD_IN_CMD;
110
111 return EFI_INVALID_PARAMETER;
112 }
113
114 break;
115 default:
116 UT_LOG_VERBOSE ("%a: Invalid Opcode = 0x%x!!!\n", __func__, Command->Cdw0.Opcode);
117 break;
118 }
119
120 //
121 // Populate CQE (completion queue entry based on opcode and parameters
122 //
123
124 return EFI_SUCCESS;
125}
126
138 IN UINT32 NamespaceId,
139 IN VOID *Buffer
140 )
141{
145
146 ZeroMem (&CommandPacket, sizeof (EFI_NVM_EXPRESS_PASS_THRU_COMMAND_PACKET));
147 ZeroMem (&Command, sizeof (EFI_NVM_EXPRESS_COMMAND));
148 ZeroMem (&Completion, sizeof (EFI_NVM_EXPRESS_COMPLETION));
149
150 CommandPacket.NvmeCmd = &Command;
151 CommandPacket.NvmeCompletion = &Completion;
152 Command.Cdw0.Opcode = NVME_ADMIN_IDENTIFY_CMD;
153 Command.Nsid = NamespaceId;
154 CommandPacket.TransferBuffer = Buffer;
155 CommandPacket.TransferLength = sizeof (NVME_ADMIN_NAMESPACE_DATA);
156 CommandPacket.CommandTimeout = NVME_GENERIC_TIMEOUT;
157 CommandPacket.QueueType = NVME_ADMIN_QUEUE;
158
159 //
160 // Set bit 0 (Cns bit) to 1 to identify a namespace
161 //
162 CommandPacket.NvmeCmd->Cdw10 = 0;
163 CommandPacket.NvmeCmd->Flags = CDW10_VALID;
164
165 return EFI_SUCCESS;
166}
167
178NvmeUnitTestRead (
180 OUT VOID *Buffer,
181 IN UINT64 Lba,
182 IN UINTN Blocks
183 )
184{
185 UT_ASSERT_NOT_NULL (Device);
186 Buffer = NULL;
187 Lba = 0;
188 Blocks = 0;
189
190 return EFI_SUCCESS;
191}
192
203NvmeUnitTestWrite (
205 IN VOID *Buffer,
206 IN UINT64 Lba,
207 IN UINTN Blocks
208 )
209{
210 UT_ASSERT_NOT_NULL (Device);
211 Buffer = NULL;
212 Lba = 0;
213 Blocks = 0;
214
215 return EFI_SUCCESS;
216}
217
229EFIAPI
232 IN UINT32 MediaId,
233 IN EFI_LBA Lba,
234 IN UINTN BufferSize,
235 OUT VOID *Buffer
236 )
237{
239 EFI_STATUS Status;
240 EFI_BLOCK_IO_MEDIA *Media;
241 UINTN BlockSize;
242 UINTN NumberOfBlocks;
243 UINTN IoAlign;
244
245 //
246 // Check parameters.
247 //
248 if (This == NULL) {
249 return EFI_INVALID_PARAMETER;
250 }
251
252 Media = This->Media;
253
254 if (MediaId != Media->MediaId) {
255 return EFI_MEDIA_CHANGED;
256 }
257
258 if (Buffer == NULL) {
259 return EFI_INVALID_PARAMETER;
260 }
261
262 if (BufferSize == 0) {
263 return EFI_SUCCESS;
264 }
265
266 BlockSize = Media->BlockSize;
267 if ((BufferSize % BlockSize) != 0) {
268 return EFI_BAD_BUFFER_SIZE;
269 }
270
271 NumberOfBlocks = BufferSize / BlockSize;
272 if ((Lba + NumberOfBlocks - 1) > Media->LastBlock) {
273 return EFI_INVALID_PARAMETER;
274 }
275
276 IoAlign = Media->IoAlign;
277 if ((IoAlign > 0) && (((UINTN)Buffer & (IoAlign - 1)) != 0)) {
278 return EFI_INVALID_PARAMETER;
279 }
280
281 Device = NVME_DEVICE_PRIVATE_DATA_FROM_BLOCK_IO (This);
282 Status = NvmeUnitTestRead (Device, Buffer, Lba, NumberOfBlocks);
283
284 return Status;
285}
286
298EFIAPI
301 IN UINT32 MediaId,
302 IN EFI_LBA Lba,
303 IN UINTN BufferSize,
304 IN VOID *Buffer
305 )
306{
308 EFI_STATUS Status;
309 EFI_BLOCK_IO_MEDIA *Media;
310 UINTN BlockSize;
311 UINTN NumberOfBlocks;
312 UINTN IoAlign;
313
314 //
315 // Check parameters.
316 //
317 if (This == NULL) {
318 return EFI_INVALID_PARAMETER;
319 }
320
321 Media = This->Media;
322
323 if (MediaId != Media->MediaId) {
324 return EFI_MEDIA_CHANGED;
325 }
326
327 if (Buffer == NULL) {
328 return EFI_INVALID_PARAMETER;
329 }
330
331 if (BufferSize == 0) {
332 return EFI_SUCCESS;
333 }
334
335 BlockSize = Media->BlockSize;
336 if ((BufferSize % BlockSize) != 0) {
337 return EFI_BAD_BUFFER_SIZE;
338 }
339
340 NumberOfBlocks = BufferSize / BlockSize;
341 if ((Lba + NumberOfBlocks - 1) > Media->LastBlock) {
342 return EFI_INVALID_PARAMETER;
343 }
344
345 IoAlign = Media->IoAlign;
346 if ((IoAlign > 0) && (((UINTN)Buffer & (IoAlign - 1)) != 0)) {
347 return EFI_INVALID_PARAMETER;
348 }
349
350 Device = NVME_DEVICE_PRIVATE_DATA_FROM_BLOCK_IO (This);
351 Status = NvmeUnitTestWrite (Device, Buffer, Lba, NumberOfBlocks);
352
353 return Status;
354}
355
369EFIAPI
372 IN UINT32 MediaId,
373 IN EFI_LBA Lba,
375 IN UINTN BufferSize,
376 OUT VOID *Buffer
377 )
378{
380 EFI_BLOCK_IO_MEDIA *Media;
381 UINTN BlockSize;
382 UINTN NumberOfBlocks;
383 UINTN IoAlign;
384 EFI_STATUS Status;
385
386 //
387 // Check parameters.
388 //
389 if (This == NULL) {
390 return EFI_INVALID_PARAMETER;
391 }
392
393 Media = This->Media;
394
395 if (MediaId != Media->MediaId) {
396 return EFI_MEDIA_CHANGED;
397 }
398
399 if (Buffer == NULL) {
400 return EFI_INVALID_PARAMETER;
401 }
402
403 BlockSize = Media->BlockSize;
404 if ((BufferSize % BlockSize) != 0) {
405 return EFI_BAD_BUFFER_SIZE;
406 }
407
408 NumberOfBlocks = BufferSize / BlockSize;
409 if ((Lba + NumberOfBlocks - 1) > Media->LastBlock) {
410 return EFI_INVALID_PARAMETER;
411 }
412
413 IoAlign = Media->IoAlign;
414 if ((IoAlign > 0) && (((UINTN)Buffer & (IoAlign - 1)) != 0)) {
415 return EFI_INVALID_PARAMETER;
416 }
417
418 Device = NVME_DEVICE_PRIVATE_DATA_FROM_BLOCK_IO2 (This);
419 Status = NvmeUnitTestRead (Device, Buffer, Lba, NumberOfBlocks);
420
421 return Status;
422}
423
437EFIAPI
440 IN UINT32 MediaId,
441 IN EFI_LBA Lba,
443 IN UINTN BufferSize,
444 IN VOID *Buffer
445 )
446{
448 EFI_BLOCK_IO_MEDIA *Media;
449 UINTN BlockSize;
450 UINTN NumberOfBlocks;
451 UINTN IoAlign;
452 EFI_STATUS Status;
453
454 //
455 // Check parameters.
456 //
457 if (This == NULL) {
458 return EFI_INVALID_PARAMETER;
459 }
460
461 Media = This->Media;
462
463 if (MediaId != Media->MediaId) {
464 return EFI_MEDIA_CHANGED;
465 }
466
467 if (Buffer == NULL) {
468 return EFI_INVALID_PARAMETER;
469 }
470
471 BlockSize = Media->BlockSize;
472 if ((BufferSize % BlockSize) != 0) {
473 return EFI_BAD_BUFFER_SIZE;
474 }
475
476 NumberOfBlocks = BufferSize / BlockSize;
477 if ((Lba + NumberOfBlocks - 1) > Media->LastBlock) {
478 return EFI_INVALID_PARAMETER;
479 }
480
481 IoAlign = Media->IoAlign;
482 if ((IoAlign > 0) && (((UINTN)Buffer & (IoAlign - 1)) != 0)) {
483 return EFI_INVALID_PARAMETER;
484 }
485
486 Device = NVME_DEVICE_PRIVATE_DATA_FROM_BLOCK_IO2 (This);
487 Status = NvmeUnitTestWrite (Device, Buffer, Lba, NumberOfBlocks);
488
489 return Status;
490}
491
498EFIAPI
499NvmeDestroyDeviceInstance (
500 NVME_DEVICE_PRIVATE_DATA **ppDevice
501 )
502{
503 //
504 // Free in following order to to avoid dangling pointers:
505 //
506 // 1 - NVME_ADMIN_CONTROLLER_DATA
507 // 2 - NVME_CONTROLLER_PRIVATE_DATA
508 // 3 - NVME_DEVICE_PRIVATE_DATA
509 //
510 FreePool ((*ppDevice)->Controller->ControllerData);
511 (*ppDevice)->Controller->ControllerData = NULL;
512
513 FreePool ((*ppDevice)->Controller);
514 (*ppDevice)->Controller = NULL;
515
516 FreePool ((*ppDevice));
517 *ppDevice = NULL;
518
519 return UNIT_TEST_PASSED;
520}
521
528EFIAPI
529NvmeCreateDeviceInstance (
530 NVME_DEVICE_PRIVATE_DATA **ppDevice
531 )
532{
533 NVME_ADMIN_NAMESPACE_DATA *NamespaceData;
536
538
539 Private->Signature = NVME_CONTROLLER_PRIVATE_DATA_SIGNATURE;
540 Private->Cid[0] = 0;
541 Private->Cid[1] = 0;
542 Private->Cid[2] = 0;
543 Private->Pt[0] = 0;
544 Private->Pt[1] = 0;
545 Private->Pt[2] = 0;
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;
553
554 Private->ControllerData = (NVME_ADMIN_CONTROLLER_DATA *)AllocateZeroPool (sizeof (NVME_ADMIN_CONTROLLER_DATA));
555
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__);
558
559 Private->ControllerData->Nn = 1; // One namespace
560 Private->ControllerData->Sanicap.Bes = 1; // Block Erase Supported
561 Private->ControllerData->Sanicap.Ces = 1; // Crypto Erase Supported
562 Private->ControllerData->Sanicap.Ows = 1; // Overwrite Supported
563
564 NamespaceData = AllocateZeroPool (sizeof (NVME_ADMIN_NAMESPACE_DATA));
565 UT_LOG_VERBOSE ("%a: Allocated and Initialized NVME_ADMIN_NAMESPACE_DATA\n", __func__);
566
568
569 //
570 // Initialize SSD namespace instance data
571 //
572 Device->Signature = NVME_DEVICE_PRIVATE_DATA_SIGNATURE;
573 Device->NamespaceId = 0;
574 Device->NamespaceUuid = 1;
575
576 Device->Controller = Private;
577
578 //
579 // Build BlockIo media structure
580 //
581 Device->Media.MediaId = 0;
582 Device->Media.RemovableMedia = FALSE;
583 Device->Media.MediaPresent = TRUE;
584 Device->Media.LogicalPartition = FALSE;
585 Device->Media.ReadOnly = FALSE;
586 Device->Media.WriteCaching = FALSE;
587 Device->Media.BlockSize = (UINT32)(1 << 9); // 512 byte sector size
588
589 Device->Media.LastBlock = 0x4000; // NamespaceData=>Nsze
590 Device->Media.LogicalBlocksPerPhysicalBlock = 1;
591 Device->Media.LowestAlignedLba = 1;
592
593 Device->BlockIo.Revision = EFI_BLOCK_IO_PROTOCOL_REVISION2;
594 Device->BlockIo.Media = &Device->Media;
595 Device->BlockIo.ReadBlocks = NvmeBlockIoReadBlocks;
596 Device->BlockIo.WriteBlocks = NvmeBlockIoWriteBlocks;
597
598 Device->BlockIo2.Media = &Device->Media;
599 Device->BlockIo2.ReadBlocksEx = NvmeBlockIoReadBlocksEx;
600 Device->BlockIo2.WriteBlocksEx = NvmeBlockIoWriteBlocksEx;
601
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;
607
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;
613
614 CopyMem (&Device->NamespaceData, NamespaceData, sizeof (NVME_ADMIN_NAMESPACE_DATA));
615 *ppDevice = Device;
616
617 UT_LOG_VERBOSE ("%a: Allocated and Initialized NVME_DEVICE_PRIVATE_DATA\n", __func__);
618
619 return UNIT_TEST_PASSED;
620}
621
628EFIAPI
629MediaSanitizePurgeUnitTest (
630 IN UNIT_TEST_CONTEXT Context
631 )
632{
633 UINT32 PurgeAction;
634 UINT32 OverwritePattern;
635 UNIT_TEST_STATUS UnitTestStatus;
636 NVME_DEVICE_PRIVATE_DATA *NvmeDevice;
637 EFI_STATUS Status;
638
639 UnitTestStatus = UNIT_TEST_PASSED;
640 NvmeDevice = NULL;
641 Status = EFI_SUCCESS;
642
643 UnitTestStatus = NvmeCreateDeviceInstance (&NvmeDevice);
644
645 UT_ASSERT_STATUS_EQUAL (UnitTestStatus, UNIT_TEST_PASSED);
646 UT_ASSERT_NOT_NULL (NvmeDevice);
647
648 UT_LOG_VERBOSE ("%a: Create Device Instance Status = 0x%x\n", __func__, UnitTestStatus);
649 UT_LOG_VERBOSE ("%a: Device = 0x%x\n", __func__, NvmeDevice);
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);
652
653 //
654 // Case 1: Block Erase
655 //
656 PurgeAction = SANITIZE_ACTION_BLOCK_ERASE;
657 OverwritePattern = 0;
658
659 Status = NvmExpressMediaPurge (
660 &NvmeDevice->MediaSanitize,
661 NvmeDevice->Media.MediaId,
662 PurgeAction,
663 OverwritePattern
664 );
665
667
668 UnitTestStatus = NvmeDestroyDeviceInstance (&NvmeDevice);
669
670 return UNIT_TEST_PASSED;
671}
672
679EFIAPI
680NvmeSanitizeUnitTest (
681 IN UNIT_TEST_CONTEXT Context
682 )
683{
684 UINT32 NamespaceId;
685 UINT32 SanitizeAction;
686 UINT32 NoDeallocateAfterSanitize;
687 UINT32 OverwritePattern;
688 UNIT_TEST_STATUS UnitTestStatus;
689 NVME_DEVICE_PRIVATE_DATA *NvmeDevice;
690 EFI_STATUS Status;
691
692 NamespaceId = 0;
693 UnitTestStatus = UNIT_TEST_PASSED;
694 NvmeDevice = NULL;
695 Status = EFI_SUCCESS;
696 SanitizeAction = SANITIZE_ACTION_BLOCK_ERASE;
697 NoDeallocateAfterSanitize = 0;
698 OverwritePattern = 0;
699
700 UnitTestStatus = NvmeCreateDeviceInstance (&NvmeDevice);
701
702 UT_ASSERT_STATUS_EQUAL (UnitTestStatus, UNIT_TEST_PASSED);
703 UT_ASSERT_NOT_NULL (NvmeDevice);
704
705 UT_LOG_VERBOSE ("%a: Create Device Instance Status = 0x%x\n", __func__, UnitTestStatus);
706 UT_LOG_VERBOSE ("%a: Device = 0x%x\n", __func__, NvmeDevice);
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);
709
710 //
711 // Case 1: Block Erase
712 //
713 SanitizeAction = SANITIZE_ACTION_BLOCK_ERASE;
714 NoDeallocateAfterSanitize = 0;
715 OverwritePattern = 0;
716
717 Status = NvmExpressSanitize (
718 &NvmeDevice->BlockIo,
719 NamespaceId,
720 SanitizeAction,
721 NoDeallocateAfterSanitize,
722 OverwritePattern
723 );
724
726
727 //
728 // Case 2: Crypto Erase
729 //
730 SanitizeAction = SANITIZE_ACTION_CRYPTO_ERASE;
731 NoDeallocateAfterSanitize = 0;
732 OverwritePattern = 0;
733
734 Status = NvmExpressSanitize (
735 &NvmeDevice->BlockIo,
736 NamespaceId,
737 SanitizeAction,
738 NoDeallocateAfterSanitize,
739 OverwritePattern
740 );
741
743
744 //
745 // Case 3: Overwrite
746 //
747 SanitizeAction = SANITIZE_ACTION_OVERWRITE;
748 NoDeallocateAfterSanitize = 0;
749 OverwritePattern = 0xDEADBEEF;
750
751 Status = NvmExpressSanitize (
752 &NvmeDevice->BlockIo,
753 NamespaceId,
754 SanitizeAction,
755 NoDeallocateAfterSanitize,
756 OverwritePattern
757 );
758
760
761 //
762 // Case 4: Block Erase (invalid overwrite pattern)
763 //
764 SanitizeAction = SANITIZE_ACTION_BLOCK_ERASE;
765 NoDeallocateAfterSanitize = 0;
766 OverwritePattern = 0xDEADBEEF;
767
768 Status = NvmExpressSanitize (
769 &NvmeDevice->BlockIo,
770 NamespaceId,
771 SanitizeAction,
772 NoDeallocateAfterSanitize,
773 OverwritePattern
774 );
775
776 UT_ASSERT_STATUS_EQUAL (Status, EFI_INVALID_PARAMETER);
777
778 //
779 // Case 5: Overwrite (invalid overwrite pattern)
780 //
781 SanitizeAction = SANITIZE_ACTION_OVERWRITE;
782 NoDeallocateAfterSanitize = 0;
783 OverwritePattern = 0;
784
785 Status = NvmExpressSanitize (
786 &NvmeDevice->BlockIo,
787 NamespaceId,
788 SanitizeAction,
789 NoDeallocateAfterSanitize,
790 OverwritePattern
791 );
792
793 UT_ASSERT_STATUS_EQUAL (Status, EFI_INVALID_PARAMETER);
794
795 UnitTestStatus = NvmeDestroyDeviceInstance (&NvmeDevice);
796
797 return UNIT_TEST_PASSED;
798}
799
806EFIAPI
807NvmeFormatNvmUnitTest (
808 IN UNIT_TEST_CONTEXT Context
809 )
810{
811 UINT32 NamespaceId;
812 UINT32 Ses;
813 UINT32 Flbas;
814 NVME_DEVICE_PRIVATE_DATA *NvmeDevice;
815 UNIT_TEST_STATUS UnitTestStatus;
816 EFI_STATUS Status;
817
818 NamespaceId = 0;
819 NvmeDevice = NULL;
820 UnitTestStatus = UNIT_TEST_PASSED;
821 Status = EFI_SUCCESS;
822
823 UnitTestStatus = NvmeCreateDeviceInstance (&NvmeDevice);
824
825 UT_ASSERT_STATUS_EQUAL (UnitTestStatus, UNIT_TEST_PASSED);
826 UT_ASSERT_NOT_NULL (NvmeDevice);
827
828 UT_LOG_VERBOSE ("%a: Create Device Instance Status = 0x%x\n", __func__, UnitTestStatus);
829 UT_LOG_VERBOSE ("%a: Device = 0x%x\n", __func__, NvmeDevice);
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);
832
833 //
834 // Case 1: User Data Erase (Flbas = 0)
835 //
836 Ses = SES_USER_DATA_ERASE;
837 Flbas = 0;
838 Status = NvmExpressFormatNvm (
839 &NvmeDevice->BlockIo,
840 NamespaceId,
841 Ses,
842 Flbas
843 );
844
846
847 //
848 // Case 2: Crypto Erase (Flbas = 0)
849 //
850 Ses = SES_CRYPTO_ERASE;
851 Flbas = 0;
852 Status = NvmExpressFormatNvm (
853 &NvmeDevice->BlockIo,
854 NamespaceId,
855 Ses,
856 Flbas
857 );
858
860
861 //
862 // Case 3: User Data Erase (Invalid Flbas = 3)
863 //
864 Ses = SES_USER_DATA_ERASE;
865 Flbas = 3;
866 Status = NvmExpressFormatNvm (
867 &NvmeDevice->BlockIo,
868 NamespaceId,
869 Ses,
870 Flbas
871 );
872
873 UT_ASSERT_STATUS_EQUAL (Status, EFI_INVALID_PARAMETER);
874
875 //
876 // Case 4: Invalid SES (Flba = 0)
877 //
878 Ses = 0xFF;
879 Flbas = 0;
880 Status = NvmExpressFormatNvm (
881 &NvmeDevice->BlockIo,
882 NamespaceId,
883 Ses,
884 Flbas
885 );
886
887 UT_ASSERT_STATUS_EQUAL (Status, EFI_INVALID_PARAMETER);
888
889 UnitTestStatus = NvmeDestroyDeviceInstance (&NvmeDevice);
890
891 return UNIT_TEST_PASSED;
892}
893
900EFIAPI
901UnitTestBaseline (
902 IN UNIT_TEST_CONTEXT Context
903 )
904{
905 UINT32 A;
906 UINT32 B;
907 UINT32 C;
908
909 A = 1;
910 B = 1;
911 C = A + B;
912
913 UT_ASSERT_EQUAL (C, 2);
914 UT_ASSERT_NOT_EQUAL (0, 1);
915
916 return UNIT_TEST_PASSED;
917}
918
929EFIAPI
930MediaSanitizeUnitTestEntry (
931 VOID
932 )
933{
934 EFI_STATUS Status;
935 UNIT_TEST_FRAMEWORK_HANDLE Framework;
936 UNIT_TEST_SUITE_HANDLE NvmeFormatNvmTestSuite;
937 UNIT_TEST_SUITE_HANDLE NvmeSanitizeTestSuite;
938 UNIT_TEST_SUITE_HANDLE MediaSanitizeProtocolTestSuite;
939
940 Framework = NULL;
941
942 #define UNIT_TEST_NAME "Media Sanitize Protocol Unit Test"
943 #define UNIT_TEST_VERSION "1.0"
944
945 DEBUG ((DEBUG_INFO, "%a v%a\n", UNIT_TEST_NAME, UNIT_TEST_VERSION));
946
947 //
948 // Start setting up the test framework for running the tests.
949 //
950 Status = InitUnitTestFramework (
951 &Framework,
952 UNIT_TEST_NAME,
953 gEfiCallerBaseName,
954 UNIT_TEST_VERSION
955 );
956
957 if (EFI_ERROR (Status)) {
958 DEBUG ((DEBUG_ERROR, "Failed in InitUnitTestFramework. Status = %r\n", Status));
959 goto EXIT;
960 }
961
962 //
963 // Populate the NVM Express Format NVM Unit Test Suite.
964 //
965 Status = CreateUnitTestSuite (
966 &NvmeFormatNvmTestSuite,
967 Framework,
968 "NVM Express Format NVM Test Suite",
969 "Nvm.Express.Format.Nvm",
970 NULL,
971 NULL
972 );
973
974 if (EFI_ERROR (Status)) {
975 DEBUG ((DEBUG_ERROR, "Failed in CreateUnitTestSuite for NvmeFormatNvmTestSuite. Status = %r\n", Status));
976 Status = EFI_OUT_OF_RESOURCES;
977 goto EXIT;
978 }
979
980 //
981 // Add baseline sanity test case
982 //
984 NvmeFormatNvmTestSuite, // Test Suite Handle
985 "Baseline Format NVM Unit Test", // Test Description
986 "FormatNVM", // Test Class
987 UnitTestBaseline, // UNIT_TEST_FUNCTION()
988 NULL, // (Optional) UNIT_TEST_PREREQUISITE()
989 NULL, // (Optional) UNIT_TEST_CLEANUP()
990 NULL // (Optional) UNIT_TEST_CONTEXT
991 );
992
993 //
994 // Add test case for NvmExpressFormatNvm()
995 //
997 NvmeFormatNvmTestSuite, // Test Suite Handle
998 "Admin Format NVM Command Unit Test", // Test Description
999 "FormatNVM", // Test Class
1000 NvmeFormatNvmUnitTest, // UNIT_TEST_FUNCTION()
1001 NULL, // (Optional) UNIT_TEST_PREREQUISITE()
1002 NULL, // (Optional) UNIT_TEST_CLEANUP()
1003 NULL // (Optional) UNIT_TEST_CONTEXT
1004 );
1005
1006 //
1007 // Populate the NVM Express Sanitize Unit Test Suite.
1008 //
1009 Status = CreateUnitTestSuite (
1010 &NvmeSanitizeTestSuite,
1011 Framework,
1012 "NVM Express Sanitize Test Suite",
1013 "Nvm.Express.Sanitize",
1014 NULL,
1015 NULL
1016 );
1017
1018 if (EFI_ERROR (Status)) {
1019 DEBUG ((DEBUG_ERROR, "Failed in CreateUnitTestSuite for NvmeSanitizTestSuite. Status = %r\n", Status));
1020 Status = EFI_OUT_OF_RESOURCES;
1021 goto EXIT;
1022 }
1023
1024 //
1025 // Add baseline sanity test
1026 //
1027 AddTestCase (
1028 NvmeSanitizeTestSuite, // Test Suite Handle
1029 "Baseline Sanitize Unit Test", // Test Description
1030 "Sanitize", // Test Class
1031 UnitTestBaseline, // UNIT_TEST_FUNCTION()
1032 NULL, // (Optional) UNIT_TEST_PREREQUISITE()
1033 NULL, // (Optional) UNIT_TEST_CLEANUP()
1034 NULL // (Optional) UNIT_TEST_CONTEXT
1035 );
1036
1037 //
1038 // Add test case for NvmExressSanitize()
1039 //
1040 AddTestCase (
1041 NvmeSanitizeTestSuite, // Test Suite Handle
1042 "Admin Sanitize Command Unit Test", // Test Description
1043 "Sanitize", // Test Class
1044 NvmeSanitizeUnitTest, // UNIT_TEST_FUNCTION()
1045 NULL, // (Optional) UNIT_TEST_PREREQUISITE()
1046 NULL, // (Optional) UNIT_TEST_CLEANUP()
1047 NULL // (Optional) UNIT_TEST_CONTEXT
1048 );
1049
1050 //
1051 // Populate the Media Sanitize Protocol Unit Test Suite.
1052 //
1053 Status = CreateUnitTestSuite (
1054 &MediaSanitizeProtocolTestSuite,
1055 Framework,
1056 "Media Sanitize Protocol Test Suite",
1057 "Media.Sanitize.Protocol",
1058 NULL,
1059 NULL
1060 );
1061
1062 if (EFI_ERROR (Status)) {
1063 DEBUG ((DEBUG_ERROR, "Failed in CreateUnitTestSuite for MediaSanitizeProtocolTestSuite. Status = %r\n", Status));
1064 Status = EFI_OUT_OF_RESOURCES;
1065 goto EXIT;
1066 }
1067
1068 //
1069 // Add test case for Media Purge
1070 //
1071 AddTestCase (
1072 MediaSanitizeProtocolTestSuite, // Test Suite Handle
1073 "Baseline MediaSanitize Unit Test", // Test Description
1074 "MediaSanitize", // Test Class
1075 UnitTestBaseline, // UNIT_TEST_FUNCTION()
1076 NULL, // (Optional) UNIT_TEST_PREREQUISITE()
1077 NULL, // (Optional) UNIT_TEST_CLEANUP()
1078 NULL // (Optional) UNIT_TEST_CONTEXT
1079 );
1080
1081 //
1082 // Add test case for Media Purge
1083 //
1084 AddTestCase (
1085 MediaSanitizeProtocolTestSuite, // Test Suite Handle
1086 "Protocol Media Sanitize Unit Test", // Test Description
1087 "MediaPurge", // Test Class
1088 MediaSanitizePurgeUnitTest, // UNIT_TEST_FUNCTION()
1089 NULL, // (Optional) UNIT_TEST_PREREQUISITE()
1090 NULL, // (Optional) UNIT_TEST_CLEANUP()
1091 NULL // (Optional) UNIT_TEST_CONTEXT
1092 );
1093
1094 //
1095 // Execute the tests.
1096 //
1097 Status = RunAllTestSuites (Framework);
1098
1099EXIT:
1100 if (Framework) {
1101 FreeUnitTestFramework (Framework);
1102 }
1103
1104 return Status;
1105}
1106
1110#define MediaSanitizeUnitTestMain main
1111
1121INT32
1122MediaSanitizeUnitTestMain (
1123 IN INT32 Argc,
1124 IN CHAR8 *Argv[]
1125 )
1126{
1127 return MediaSanitizeUnitTestEntry ();
1128}
UINT64 UINTN
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 NULL
Definition: Base.h:319
#define TRUE
Definition: Base.h:301
#define FALSE
Definition: Base.h:307
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
#define DEBUG(Expression)
Definition: DebugLib.h:434
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)
UINT64 EFI_LBA
Definition: UefiBaseType.h:45
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
VOID * EFI_EVENT
Definition: UefiBaseType.h:37
#define EFI_SUCCESS
Definition: UefiBaseType.h:112
EFI_STATUS EFIAPI RunAllTestSuites(IN UNIT_TEST_FRAMEWORK_HANDLE FrameworkHandle)
Definition: RunTests.c:145
#define UT_ASSERT_NOT_NULL(Pointer)
Definition: UnitTestLib.h:439
#define UT_ASSERT_NOT_EQUAL(ValueA, ValueB)
Definition: UnitTestLib.h:402
VOID * UNIT_TEST_CONTEXT
Definition: UnitTestLib.h:54
#define UT_ASSERT_EQUAL(ValueA, ValueB)
Definition: UnitTestLib.h:375
#define UT_ASSERT_STATUS_EQUAL(Status, Expected)
Definition: UnitTestLib.h:427
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)
Definition: UnitTestLib.c:326
EFI_STATUS EFIAPI FreeUnitTestFramework(IN UNIT_TEST_FRAMEWORK_HANDLE FrameworkHandle)
Definition: UnitTestLib.c:150
#define UT_LOG_VERBOSE(Format,...)
Definition: UnitTestLib.h:824
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)
Definition: UnitTestLib.c:426
EFI_STATUS EFIAPI InitUnitTestFramework(OUT UNIT_TEST_FRAMEWORK_HANDLE *FrameworkHandle, IN CHAR8 *Title, IN CHAR8 *ShortTitle, IN CHAR8 *VersionString)
Definition: UnitTestLib.c:204
UINT32 UNIT_TEST_STATUS
Definition: UnitTestLib.h:16
#define UT_ASSERT_NOT_EFI_ERROR(Status)
Definition: UnitTestLib.h:414
EFI_BLOCK_IO_MEDIA * Media
Definition: BlockIo2.h:187
EFI_BLOCK_IO_MEDIA * Media
Definition: BlockIo.h:224
EFI_BLOCK_IO_MEDIA * Media
BOOLEAN RemovableMedia
Definition: BlockIo.h:137
UINT32 LogicalBlocksPerPhysicalBlock
Definition: BlockIo.h:192
BOOLEAN LogicalPartition
Definition: BlockIo.h:150
UINT32 BlockSize
Definition: BlockIo.h:167
EFI_LBA LastBlock
Definition: BlockIo.h:178
BOOLEAN WriteCaching
Definition: BlockIo.h:161
BOOLEAN MediaPresent
Definition: BlockIo.h:144
BOOLEAN ReadOnly
Definition: BlockIo.h:156
EFI_LBA LowestAlignedLba
Definition: BlockIo.h:185
Definition: Nvme.h:901