Skip to content

Last LBA missed on NvmExpressMediaClear#12424

Open
spbrogan wants to merge 1 commit intotianocore:masterfrom
spbrogan:fix_tc_12396
Open

Last LBA missed on NvmExpressMediaClear#12424
spbrogan wants to merge 1 commit intotianocore:masterfrom
spbrogan:fix_tc_12396

Conversation

@spbrogan
Copy link
Copy Markdown
Member

@spbrogan spbrogan commented Apr 9, 2026

Off by one error in the code of NvmExpressMediaClear() causes the last LBA to be missed when clearing the media. This patch fixes the issue by adjusting the loop condition to ensure that all LBAs are cleared properly.

  • Breaking change?
    • Breaking change - Does this PR cause a break in build or boot behavior?
    • Examples: Does it add a new library class or move a module to a different repo.
  • Impacts security?
    • Security - Does this PR have a direct security impact?
    • Examples: Crypto algorithm change or buffer overflow fix.
  • Includes tests?
    • Tests - Does this PR include any explicit test code?
    • Examples: Unit tests or integration tests.

How This Was Tested

Host based unit tests and code inspection

Integration Instructions

n/a

@spbrogan
Copy link
Copy Markdown
Member Author

spbrogan commented Apr 9, 2026

resolves #12396

@spbrogan spbrogan linked an issue Apr 9, 2026 that may be closed by this pull request
4 tasks
Copy link
Copy Markdown
Member

@mikebeaton mikebeaton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is correct that LastBlock is (almost, see below) universally initialized to Size -1 (or copied from another last block value) in the codebase.

Could the comment be updated?
https://github.com/tianocore/edk2/blob/master/MdePkg/Include/Protocol/BlockIo.h#L173-L177
e.g.

/// The last logical block address on the device (inclusive).

Additionally, Is this test wrong?

https://github.com/tianocore/edk2/blob/master/MdeModulePkg/Bus/Pci/NvmExpressDxe/UnitTest/MediaSanitizeUnitTest.c#L589

Off by one error in the code of NvmExpressMediaClear() causes the last
LBA to be missed when clearing the media.

This patch fixes the issue by adjusting the loop condition to ensure
that all LBAs are cleared properly.

Signed-off-by: Sean Brogan <sebrogan@microsoft.com>
@spbrogan
Copy link
Copy Markdown
Member Author

spbrogan commented Apr 9, 2026

It is correct that LastBlock is (almost, see below) universally initialized to Size -1 (or copied from another last block value) in the codebase.

Could the comment be updated? https://github.com/tianocore/edk2/blob/master/MdePkg/Include/Protocol/BlockIo.h#L173-L177 e.g.

/// The last logical block address on the device (inclusive).

Additionally, Is this test wrong?

https://github.com/tianocore/edk2/blob/master/MdeModulePkg/Bus/Pci/NvmExpressDxe/UnitTest/MediaSanitizeUnitTest.c#L589

This PR #12426 makes an update. I don't think it really mattered for the host-based unit test as the device was essentially mocked but I think it should be clear for intent.

@mikebeaton
Copy link
Copy Markdown
Member

It is correct that LastBlock is (almost, see below) universally initialized to Size -1 (or copied from another last block value) in the codebase.
Could the comment be updated? https://github.com/tianocore/edk2/blob/master/MdePkg/Include/Protocol/BlockIo.h#L173-L177 e.g.

/// The last logical block address on the device (inclusive).

Additionally, Is this test wrong?
https://github.com/tianocore/edk2/blob/master/MdeModulePkg/Bus/Pci/NvmExpressDxe/UnitTest/MediaSanitizeUnitTest.c#L589

This PR #12426 makes an update. I don't think it really mattered for the host-based unit test as the device was essentially mocked but I think it should be clear for intent.

Thank you.

It is true that the UEFI spec. for EFI_BLOCK_IO_MEDIA is also unambiguous. The comment for LastBlock in the header file is not, however. Could/should it be updated, e.g. to:

  ///
  /// The last logical block address on the device (i.e. the
  /// total number of user addressable blocks minus one).
  /// If the media changes, then this field is updated.
  ///
  EFI_LBA    LastBlock;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: NvmeDxe::NvmExpressMediaClear: off-by-one skips last LBA

2 participants