Commit Graph
23170 Commits
Author SHA1 Message Date
iceman1001andClaude Opus 5 (1M context) 2449bb2472 hf mfdes: derive the communication mode from the access rights
The PICC applies a file's communication mode only when access is granted by
a key right matching the authenticated key. When the operation is granted by
the free access right (0x0E) instead, it runs in plain, whatever the file
settings say. The client used the file comm mode unconditionally, so any file
created with the default EEEE rights failed with 0x917E on write, and a free
access Full file was printed as its data plus eight unstripped CMAC bytes
instead of being decrypted. Fixes #3393.

Which rights can grant an operation varies, and the rule above is evaluated
over that set. Measured on DESFire EV2, ev1 and ev2 secure channels:

  read / write data    w (or r), rw
  GetValue   0x6C      r, w, rw
  Credit     0x0C      rw only
  Debit      0xDC      r, w, rw
  LimCredit  0x1C      r, w, rw

Credit is the odd one out: a file with r=key0 w=key0 rw=free needs Credit in
plain but Debit in MAC, in the same session. The FreeValue option bit forces
GetValue to plain regardless of the rights.

hf mfdes value never read the file settings at all, which is #2712. e1598cd62
worked around that by dropping CREDIT/DEBIT/LIMITED_CREDIT from
EV1D40TransmitMAC and retrying in plain on a length error. That made MACed
value files impossible to credit by any route, and the retry resent a
byte-identical APDU after the failure had already desynced the CMAC chain,
turning a clear 0x7E into a misleading 0x91AE. Both are reverted and the
command now reads the file settings like read and write do. Fixes #2712.

hf mfdes write resolves the comm mode even when --type is given explicitly,
since that path skipped detection entirely.

The DACLRP branch in the read path is left alone: no LRP capable card was
available to test against.

Co-Authored-By: Claude Opus 5 (1M context)
2026-09-12 23:18:59 +02:00
Iceman 08e389c0bb Merge pull request #3622 from mfcarroll/idteck-demod
lf idteck demod: report only what the tag actually holds
2026-09-13 03:17:24 +07:00
Matthew CarrollandClaude Opus 5 36f689beaa lf idteck demod: report only what the tag actually holds
Three ways this command reported a credential that was not on the tag.

CmdIdteckDemod always passed its local raw[8] to demodIdteck, never NULL, so
without --raw the command decoded a zeroed stack array rather than the
graphbuffer and reported card id 0 every time. The help's own first example,
plain `lf idteck demod`, has been doing that since --raw was added. It now
passes NULL when no --raw is given, which is the signal path demodIdteck
already has and lf idteck reader already uses.

A raw frame whose first word is not 4944544B printed "No genuine IDTECK found"
and then fell through and announced a tag anyway, with a card id read out of
whatever was passed in. It now stops there. The signal path cannot reach this:
detectIdteck matches the full 32 bit preamble, so lf search and lf idteck
reader are unaffected.

Finally the Idteck card id was packed into a 26 bit wiegand_message_t and run
through HIDUnpack, which prints an HID H10301 line with a facility code and
card number. That is not a decode of anything - the id is 24 bits, byte
reversed off the wire, and Unpack_H10301 returns true for any 26 bit message,
so the parity flag is the only hint and it reads ok for 25% of card ids by
chance. Both committed Idteck traces show it: 4944544B351FBE4B prints
FC: 37 CN: 57103 parity ( ok ), AC40E069 prints FC: 52 CN: 61472 parity
( fail ). Dropped, with the wiegand_formats.h include that came in with it.

Added five offline regression tests. Three fail without this change, one per
defect; lf search over every committed lf trace is byte identical before and
after apart from the two removed H10301 lines.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-12 12:52:48 -07:00
iceman1001 8e3c439358 text 2026-09-12 21:50:19 +02:00
iceman1001 c91993acc8 text 2026-09-12 21:48:46 +02:00
iceman1001andClaude Opus 5 (1M context) 344c2d6185 trace: stop trace save writing a stale client side buffer
`trace save` saved the client side trace buffer whenever it was non-empty
and only downloaded from device when it was empty. After `trace load` or
`trace list -1` a following sniff -> `trace save` silently wrote the old
trace, byte identical to the previous save, with no way to reset it.
See #3592, and #1252 / #1512 for earlier reports of the same thing.

- `trace save` now downloads from device by default and takes `-1` to save
  the client side buffer, mirroring `trace list`. Offline it falls back to
  the buffer so `trace load` -> `trace save` still works.
- split download_trace() into download_trace_ex(), which hands the caller
  its own buffer. `trace save` uses it and no longer mutates gs_trace, so
  `-1` means the client buffer regardless of what ran before.
- download_trace() freed gs_trace before it knew the download had worked,
  so a timeout threw away a loaded trace. It now swaps on success only.
- added `trace clear` to discard the client side buffer, and a shared
  ClearTraceBuffer() to replace the free/NULL/zero pattern that was
  open coded in ImportTraceBuffer() and CmdTraceLoad().

Help text for both new commands names the device vs client distinction.

Co-Authored-By: Claude Opus 5 (1M context)
2026-09-12 21:39:35 +02:00
iceman1001andClaude Opus 5 (1M context) 255576fa43 make hf 14a antifuzz actually reach a reader
Every answer was modulated after the reader's frame arrived, landing
~1812 carrier periods late against the 1172 the FDT allows, so readers
timed out before any of it mattered. All answers are now precomputed the
way hf 14a sim does it, and a phone answers at 1172 exactly.

Cascade mode sent a cascade tag at every level, which a conforming reader
rejects in the last level its ATQA announced - it dropped the card before
ever sending a SELECT. The cascade tag now appears only below that level;
it is the SAK that lies about the UID being incomplete, and that is what
walks a reader up to cascade level 7.

A bit oriented ANTICOLLISION was answered with the whole UID instead of
the bits the reader still lacked, handing it a frame of the wrong length.
One encoder now builds any tail, with or without collisions in it.

--coll leaves the first UID byte clean and collides every bit after it, so
a reader resolves 24 bits one round trip at a time, is then given a BCC
that checks out, and is cascaded into the same again - 183 answers per
poll against the 3 a real card needs. Colliding byte 0 as well only
produced a UID of all ones, which readers drop before asking for the BCC.

Reader frames that go unanswered are traced instead of silently dropped,
LED B marks an attempt in progress and LED C flips on each step deeper,
and the closing line reports rounds run and how far a reader got.

trace list -t 14a decodes SEL 0x99..0x9F, shows how many UID bits a bit
oriented ANTICOLL claims, and no longer flags those CRC-less frames as
bad CRC.

Co-Authored-By: Claude Opus 5 (1M context)
2026-09-12 21:18:26 +02:00
iceman1001 25b3c88acf fix #3487 2026-09-12 20:56:08 +02:00
iceman1001 11cac99913 fix cident 2026-09-12 20:39:19 +02:00
iceman1001 baea4014c1 fix cident overflows 2026-09-12 20:35:35 +02:00
iceman1001andClaude Opus 5 (1M context) 4d7d49e089 stop an FPGA bitstream download from eating the emulator memory
FpgaDownloadAndGoEx() with keep_em false does BigBuf_free() plus
BigBuf_Clear_ext(): the emulator memory pointer is nulled and all of BigBuf is
zeroed.  iso14443a_setup() calls that variant, and Mifare1ksim(),
SimulateIso14443aTagEx() and SimulateIso14443aTagAID() build their precompiled
anticollision responses in BigBuf first.  CMD_HF_MIFARE_EML_MEMGET had it the
other way round - it downloaded, then read the memory it had just wiped, so
'hf mf eview' and 'hf mf esave' returned their own zeros after any lf hitag,
hf iclass or hf 15 command.  A download already cached early-returns, so which
image the FPGA held decided whether any of this happened.

All five now call FpgaDownloadAndGo_keep_EM() before any BigBuf allocation.
Free-BigBuf floor at download time goes 16384 -> 20480 (ring plus the 4096 byte
emulator block); BigBuf measures 29084..31420 across all standalone configs on
RDV4 and 40120 on a PM3 Easy.

The sim paths also clear the trace before taking their modulation buffer -
BigBuf_malloc() refuses memory a stale trace holds, and MifareSimInit() ignored
the NULL, leaving prepare_tag_modulation() to memcpy 571 bytes over the vector
table at address 0.

'hf mf eload' now zeroes emulator memory device side: CMD_HF_MIFARE_EML_MEMSET
takes a flags byte, set on the first chunk only, so 'hf mf esetblk' and every
other partial write still touch only their own blocks.  That byte is why
CAPABILITIES_VERSION goes 9 -> 10; mismatched client and firmware refuse to
connect rather than write everything one byte offset.

Reported in #2836, whose USB drop is separately addressed by bc289cf43.

Builds clean for PM3RDV4, PM5 and the client; astyle clean.  Not yet verified on
hardware.

Co-Authored-By: Claude Opus 5 (1M context)
2026-09-12 18:49:24 +02:00
iceman1001 054fae4ac1 fixed the spinner and download reporting 2026-09-12 18:30:45 +02:00
Iceman af0b0095ca Merge pull request #3621 from nieldk/master
Improve reset when upgrade/change name BWM
2026-09-12 22:18:21 +07:00
Niel Nielsen 028aae180d Add cmdbwm.c to CMakeLists.txt
Signed-off-by: Niel Nielsen <nieldk@gmail.com>
2026-09-12 17:07:50 +02:00
Niel Nielsen 2c779f337f Update cmdbwm.c
Signed-off-by: Niel Nielsen <nieldk@gmail.com>
2026-09-12 16:51:53 +02:00
Niel Nielsen 3bdef8b23b Add files via upload
Signed-off-by: Niel Nielsen <nieldk@gmail.com>
2026-09-12 16:48:51 +02:00
Niel Nielsen 0a0dd87224 Remove unused BWM command functions
Removed unused functions related to BWM commands and OTA process.

Signed-off-by: Niel Nielsen <nieldk@gmail.com>
2026-09-12 16:48:08 +02:00
Niel Nielsen bd24a6197d Fix Makefile to include dependency files correctly
Signed-off-by: Niel Nielsen <nieldk@gmail.com>
2026-09-12 16:47:03 +02:00
Niel Nielsen b808f61c1f Add cmdbwm.c to CMakeLists and include CPack
Signed-off-by: Niel Nielsen <nieldk@gmail.com>
2026-09-12 16:46:35 +02:00
Niel Nielsen 6c34b6993c Refactor BWM firmware update and reset logic
Signed-off-by: Niel Nielsen <nieldk@gmail.com>
2026-09-12 16:33:14 +02:00
iceman1001andClaude Opus 5 (1M context) 05137e825a hold the spiffs file open across an upload
SPIFFS has no directory.  SPIFFS_open() by name walks the object lookup page of
every block and reads the header of each index page it meets to strcmp the name,
so reopening per packet makes an upload cost packets x filesystem size.  On a 2MB
part one open reads about 120KB of flash - storing a 512KB file spent 16MB of
reads finding the same file 131 times over, and 249MB back when a packet was one
256 byte flash page.

append_to_spiffs() now keeps the descriptor when the next packet names the same
file.  Everything else calls spiffs_close_cached() on entry, so a held descriptor
is closed, and its writes flushed, before any other operation can look at the
file - all 15 direct SPIFFS_* call sites in this file are covered.  No protocol
change: the CMD_SPIFFS_UNMOUNT the client already sends at the end of an upload
closes it, as does any other SPIFFS command.  rdv40_spiffs_unmount() closes
before SPIFFS_unmount() since descriptors do not survive a mount, and the close
checks the mount state so a dead handle is dropped rather than passed on.

Flash reads to store a file, measured on a host build of this filesystem:

  2MB fs, 512KB file, 4029 byte packets   16302 KB -> 371 KB
  2MB fs, 512KB file,  256 byte packets  249415 KB -> 914 KB
  2MB fs, 256KB file, 4029 byte packets    7834 KB -> 313 KB
  256KB fs, 80KB file, 589 byte packets    1483 KB ->  82 KB

Written bytes are unchanged; only the searching went away.

Verified on the host against an order sensitive pattern - content byte identical,
size right, SPIFFS_check clean - both for a straight upload and for one with a
stat and an fsinfo landing mid transfer, which is the case that catches a missed
invalidation.  Then on hardware: 80KB uploaded, 'mem spiffs tree' in between to
force a flush, downloaded again, byte identical.

Co-Authored-By: Claude Opus 5 (1M context)
2026-09-12 15:54:40 +02:00
iceman1001 e87343dbef add missing ManchesterDecoding_Thinfilm decl 2026-09-12 15:29:23 +02:00
iceman1001 a878043061 fix missing appmain changes 2026-09-12 15:24:46 +02:00
iceman1001 cb75a05553 text 2026-09-12 15:14:13 +02:00
iceman1001 f57da9c85d missing files 2026-09-12 15:10:36 +02:00
iceman1001andClaude Opus 5 (1M context) 696b8357b6 add hf thinfilm sniff, and hold the tag's real frame period
hf thinfilm sniff passively records the frames a Kovio tag beams at a reader.
Kovio is tag-talks-first and the reader never sends a command, so only the tag
side is decoded - use 'hf 14a sniff' to watch a reader's poll loop.  Same DMA
loop, overrun recovery and nibble de-interleave as SniffIso14443a().

ManchesterDecoding_Thinfilm() is no longer static and takes non_real_time, the
same way ManchesterDecodingEx() does.  A DMA sniff loop cannot timestamp with
GetCountSspClk(); the clock has run on past the frame by the time the samples
are processed.

Only the decoder is RAMFUNC (724 bytes), not the sniff loop, since dropping the
Miller decode roughly halves the per sample work.  The loop counts overrun
recoveries and DMA stalls separately, so a lossy capture says so instead of
looking like a quiet tag.

With that, the sim's frame period could be measured instead of guessed.  A
genuine tag repeats every 65536 carrier periods - 2**16, it clocks a 16 bit
counter off the carrier - holding that to within 65520..65920.  So 25% duty
cycle, not the continuous beam assumed when the gap was shortened to 500us in
a96756033.  Revert to the tag's real rate.

SimulateThinFilm() now waits on the previous frame start rather than delaying a
fixed amount, so the field poll comes out of the gap instead of adding on top of
it, and the period stays right if the poll cost ever changes.  Measured back
with a second pm3 sniffing: 65561 carrier periods against the genuine tag's
65588, inside the tag's own jitter.

Rig: pm3 simulating, second pm3 sniffing, Pixel 9a reading, all three at once.
Builds clean for PM3RDV4 and PM5.

Co-Authored-By: Claude Opus 5 (1M context)
2026-09-12 15:09:53 +02:00
iceman1001andClaude Opus 5 (1M context) 9a8e179266 fix spiffs downloads >= 64K, and stop losing flash errors
CMD_SPIFFS_DOWNLOAD read the whole file into BigBuf first.  BigBuf_calloc()
takes a uint16_t while the size is a uint32_t, so a 64K file allocated zero
bytes and anything larger wrapped to a short buffer the file was then read
straight past the end of.  rdv40_spiffs_read_stream() opens the file once and
hands out one frame at a time, so the size never reaches the allocator, and the
SPIFFS path now honours start_index.  Verified with an 80K round trip: upload,
download, byte identical.

The same BigBuf_calloc(filesize) wrap was in copy_in_spiffs(); it now copies in
SPIFFS_WRITE_CHUNK_SIZE chunks.

Separately, every flash failure was invisible.  SPIFFS_CHECK_RES() only treats a
negative result as an error and the HAL hooks returned 128/129/130, so SPIFFS
saw success.  The erase path was worst: Flash_Erase4k() only reports that the
command was sent, both Flash_CheckBusy() results were discarded, and
'return (SPIFFS_OK == erased)' yields 1 on failure.  A flash dump from a device
that hit this showed one block written without being erased - its object index
header held '0x14000 & 0x10000', and the filename ANDed away to empty.  Erases
are now read back and retried, and the HAL returns real SPIFFS error codes.

Flash_Write() returned len unconditionally even after a rejected page, which
also defeated the 'res == payload->len' check in CMD_FLASHMEM_WRITE.

Feedback, so none of this is silent again: CMD_SPIFFS_WRITE answers with the
SPIFFS result, the client aborts on the first refusal and names the byte it
stopped at, dl_it() checks the terminator status, and both transfer directions
print inline progress.

Co-Authored-By: Claude Opus 5 (1M context)
2026-09-12 14:58:08 +02:00
iceman1001andClaude Opus 5 (1M context) a967560335 hf thinfilm sim - shorter frame gap
The inter frame delay was 3600us for a 128 bit payload and 2400us for 256 bit,
sized so that either one holds a constant 4.8ms repeat period.  That is the wrong
target.  A Kovio tag is never transacted with - the reader captures it during a
poll slot and hands the bytes up as an activation, see
nfa_dm_disc_handle_kovio_activation() in libnfc-nci, which reads the barcode
straight out of rf_tech_param.param.pk.uid - so the only thing that matters is how often a frame is on the air.

Measured repeat period before today was 8.66ms, ie 14% duty cycle.  A fixed 500us
gap takes a 128 bit payload to roughly 2.2ms.  The gap cannot go to zero, a reader
needs unmodulated carrier to find the frame start and our own demod wants three
quiet bytes, but 500us is an order of magnitude clear of that.

Also drop the 'not correct' caveat from 'hf thinfilm list', the sim side traces
properly now.

Tested on RDV4 against an Android reader.  Builds clean for PM3RDV4 and PM5.

Co-Authored-By: Claude Opus 5 (1M context)
2026-09-12 14:17:13 +02:00
Iceman a7c6116258 Merge pull request #3620 from nieldk/master
Add BWM set BLE name
2026-09-12 19:03:24 +07:00
iceman1001andClaude Opus 5 (1M context) 91f5f7e9a2 fix hf thinfilm sim
SimulateThinFilm() set the SSC up with FPGA_MAJOR_MODE_HF_READER but wrote a conf
word for FPGA_MAJOR_MODE_HF_ISO14443A | TAGSIM_MOD.  On the HF bitstream
FpgaIs16BitMsbMode() picks 16 bit frames for HF_READER, so every 8 bit SEC_D /
SEC_E went on air as 0x00 followed by the symbol, ie half the symbol rate with an
unmodulated gap before each one.  No reader decodes that and nothing errors.

Broken since e144c793f (2020-07-02) added the argument and the 16 bit branch.
thinfilm.c has passed HF_READER since 2019, when FpgaSetupSsc() took no argument
and was always 8 bit.  Every other tag sim in the tree passes HF_SIMULATOR or
HF_ISO14443A and was unaffected.

The send loop also called ReadReaderField() between every frame.  That is
AdcRssiAvg(), 32 conversions each paying a 42.7us ADC startup and a 40us sample
and hold, about 3.8ms - more than the deliberate inter frame delay sitting next
to it.  Measured repeat period was 8.66ms against the 4.8ms the delays were
written for, ie 14% duty cycle.  Poll with 4 samples, keep 32 for the one off
baseline.

The sim now enables tracing and logs each beamed frame, so 'hf thinfilm list'
works on the sim side too, and the coded buffer hexdump is behind DBG_DEBUG
instead of printing 8 lines every run.  It also read 16 bytes per line
unconditionally, so an odd length payload over read the buffer.

Tested on RDV4.  Builds clean for PM3RDV4 and PM5.

Co-Authored-By: Claude Opus 5 (1M context)
2026-09-12 13:58:39 +02:00
iceman1001 f48250d51f text 2026-09-12 13:47:54 +02:00
Niel Nielsen 443b2d8fc8 Document 'hw bwm name' command for BLE name management
Added documentation for the 'hw bwm name' command, detailing how to get and set the BLE advertising name, including usage examples and notes on behavior during name changes.

Signed-off-by: Niel Nielsen <nieldk@gmail.com>
2026-09-12 13:45:14 +02:00
Niel Nielsen e2cefcff3a Refine OTA command and timeout configurations
Updated OTA command definitions and timeout settings.

Signed-off-by: Niel Nielsen <nieldk@gmail.com>
2026-09-12 13:29:52 +02:00
Niel Nielsen 423ede5d29 Update print statement from 'Hello' to 'Goodbye'
Signed-off-by: Niel Nielsen <nieldk@gmail.com>
2026-09-12 13:29:30 +02:00
Niel Nielsen 7046f43bfb Refactor BWM command handling and improve help messages
Signed-off-by: Niel Nielsen <nieldk@gmail.com>
2026-09-12 13:28:42 +02:00
Niel Nielsen 407d9d7474 Add BLE name handling for GET and SET commands
Signed-off-by: Niel Nielsen <nieldk@gmail.com>
2026-09-12 13:27:34 +02:00
Niel Nielsen 945619c2d5 Add BLE name command definitions to pm3_cmd.h
Signed-off-by: Niel Nielsen <nieldk@gmail.com>
2026-09-12 13:27:00 +02:00
Niel Nielsen 9d587e3dcc Update OTA command definitions and BLE functions
Signed-off-by: Niel Nielsen <nieldk@gmail.com>
2026-09-12 13:20:53 +02:00
Niel Nielsen 2b98c126a7 Update print statement from 'Hello' to 'Goodbye'
Signed-off-by: Niel Nielsen <nieldk@gmail.com>
2026-09-12 13:20:24 +02:00
Niel Nielsen 815d03baa3 Refactor BWM command handling with CLI parser
Signed-off-by: Niel Nielsen <nieldk@gmail.com>
2026-09-12 13:17:53 +02:00
iceman1001 812722ed81 fix overflow 2026-09-12 12:49:17 +02:00
iceman1001 3ea9d885cb fix realloc patterns 2026-09-12 12:42:02 +02:00
iceman1001 7285f1158d cleaner return value 2026-09-12 12:38:05 +02:00
iceman1001 47507a1d21 better string handling in the client in felica, hitagU, hitagS 2026-09-12 12:28:28 +02:00
iceman1001 58580c690f add a str_append fct. To handle some string manipulation issues in the client 2026-09-12 12:26:06 +02:00
iceman1001 10dafd8048 fix a hitagu overflow in cmdtrace.c 2026-09-12 11:56:10 +02:00
iceman1001 df68cf72e0 hf mf: match the isOK status locals to reply_ng() 2026-09-12 11:47:03 +02:00
iceman1001andClaude Opus 5 (1M context) 50a2b0ca37 hf mf autopwn: recover the crypto1 session when a read is denied
A read the card NAKs aborts the crypto1 session, so after the first
denied block every later read in that sector came back with len 0 and
the retry loop spun on it. One unreadable block cost the whole sector,
and the dump only recovered when the next sector did a fresh select.

MifareECardLoad also only fell back to key B when the *auth* failed,
never when the *read* was denied, so a sector whose access conditions
allow a read with B but not with A was lost even though both keys were
known. That is the gap between this and the client side 'hf mf dump',
which picks the key per block.

Re-select and re-authenticate after a NAK, and retry the block with the
other key first. If it reads, stay on that key for the rest of the
sector; if it does not, re-auth with the original key so the remaining
blocks still read instead of collapsing. The backdoor key path has no
counterpart key to swap to, so it only clears bd_authenticated to force
a re-select on the next sector.

On an MFC EV1 with two sectors that deny key A reads, this takes the
dump from PARTIAL with 96 spurious len-0 errors to complete on the first
pass.

Co-Authored-By: Claude Opus 5 (1M context)
2026-09-12 11:24:32 +02:00
iceman1001 667b62f405 make codeql happy 2026-09-12 11:14:09 +02:00
iceman1001 03fed51af5 fix isOK size 2026-09-12 11:10:38 +02:00