Skip to content

ISSUE-4108: nfc: fix value-block BusFault and add unit test + test file generator#4352

Open
herbenderbler wants to merge 1 commit intoflipperdevices:devfrom
herbenderbler:ISSUE-4108/nfc-bus-fault
Open

ISSUE-4108: nfc: fix value-block BusFault and add unit test + test file generator#4352
herbenderbler wants to merge 1 commit intoflipperdevices:devfrom
herbenderbler:ISSUE-4108/nfc-bus-fault

Conversation

@herbenderbler
Copy link

Summary

This PR fixes a crash during MIFARE Classic write flows when value blocks are encountered (reported as NFC BusFault in issue #4108).

Problem:

During write-condition evaluation, value blocks were flagged as is_value_block = true, but the write/read key type fields in the write context were not initialized on that path. Later logic consumed those fields, which could result in invalid enum usage and a BusFault during NFC write operations.

Affected code path:

Solution:

In the value-block branch of mf_classic_handler_check_write_conditions():

  • Select a valid key type (KeyA or KeyB) based on read access.
  • Initialize both key_type_read and key_type_write before any downstream use.
  • If neither key has read access, skip the block safely.
  • Set need_halt_before_write = false for value-block flow.
  • Transition to ReadBlock and exit early from this handler path to avoid falling through into non-value-block key derivation logic.

Also added:

Verification

Performed

  • Built firmware after fix (./fbt firmware_all) to verify integration/compilation.
  • Compiled the new unit test source directly with GCC to verify syntax and includes:
    • gcc -c -I. -Ilib -Ifuri -Itargets/f7/furi_hal -Itargets/f7/furi -Iapplications/debug/unit_tests/tests applications/debug/unit_tests/tests/nfc/mf_classic_write_value_block_test.c -o /tmp/test_compile.o
  • Manual static path validation of value-block branch to confirm key initialization invariants.

Recommended/Prepared

  • Unit test execution in unit_tests app set.
  • On-device NFC write validation with MIFARE Classic cards (including value-block scenarios) using generated test files.

Checklist (For Reviewer)

  • PR has description of feature/bug or link to Confluence/Jira task
  • Description contains actions to verify feature/bugfix
  • I've built this code, uploaded it to the device and verified feature/bugfix
@herbenderbler herbenderbler changed the title nfc: fix value-block BusFault and add unit test + test file generator ISSUE-4108: nfc: fix value-block BusFault and add unit test + test file generator Mar 12, 2026
@herbenderbler herbenderbler marked this pull request as ready for review March 12, 2026 05:02
@akrylysov
Copy link

akrylysov commented Mar 19, 2026

I tried building a firmware with this change included and it unfortunately didn't fix the crash. It's not impossible the crash I'm seeing is a different bug.

@herbenderbler
Copy link
Author

I tried building a firmware with this change included and it unfortunately didn't fix the crash. It's not impossible the crash I'm seeing is a different bug.

Thanks for that feedback. Let me take another pass at it. How can I replicate the crash/error you received? Would you mind dropping more details in this thread?

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

Labels

None yet

2 participants