errcount was declared before the key type loop and only reset by a
non -11 result, so eleven scattered card errors during the DES pass left
the AES pass to break on its first error without trying a key. Count
per key type instead.
With no -n, detect only ever looked at key 0. Sweep the key numbers the
application declares (low nibble of the key settings) instead, falling
back to 0x00..0x0D when the settings are unreadable. All keys in an
application share an algo, so the first key number that succeeds narrows
keytypes[] for the rest. --save now stores the first key found rather
than whatever dctx was left holding, and a lost card aborts the sweep.
-n <num> behaves exactly as before.
Co-Authored-By: Claude Opus 5 (1M context)
secureChannel was the constant DACEV1, and there was no way to override
it, so a card in LRP mode could never be authenticated - chk reported no
keys on a card 'hf mfdes detect' handles fine. When the key settings
were unreadable it gave up instead of probing.
Work the channel out per AID: the key settings give the algo, and for an
AES app one AuthenticateLRPFirst probe separates EV1/EV2 from LRP, which
the settings byte cannot. When the settings are unreadable, fall back to
DesfireCheckAuthCommands() the way detect does. --schann d40|ev1|ev2|lrp
pins it and skips detection. Only AES is tried on an LRP channel.
Also clear the session after a found key, so the next key number starts a
first auth rather than an EV2/LRP non-first one.
Co-Authored-By: Claude Opus 5 (1M context)
The FPGA trace loop was the last OLD reply on the device outside the two
the bootrom also serves. It stayed OLD because the DMA double-buffer was
sized to the frame payload and an NG header did not obviously fit in
front of it.
DMA straight into chunk->data of a download_chunk_t instead, so a filled
buffer is already a complete NG payload and needs no copy. Chunking now
follows DOWNLOAD_CHUNK_MAX and scales with PM3_CMD_DATA_SIZE. The
terminator carries download_done_t like the other bulk downloads. No
client change needed, dl_it already had the NG branch.
Two fixes fall out of it:
FPGA_TRACE_SIZE is 3072, an exact multiple of 512 but not of
DOWNLOAD_CHUNK_MAX. Each transfer is now armed for exactly the bytes
still expected - arming a full chunk for the short last one would spin in
FPGA_SSC_DMA_RX_Done() forever. This also drops the stray extra DMA the
old loop left armed.
get_tosend() moved after FpgaDownloadAndGo(). The loader calls
BigBuf_free(), which nulls s_toSend.buf, then reuses that same region for
its decompression ring buffer - the old code captured the pointer before
the free and only worked because the loader was done with it in time.
3072 bytes goes from 6 OLD frames to 7 NG frames at PM3_CMD_DATA_SIZE
512, and would be 5 at 688.