Files
proxmark3/armsrc
Antiklesys d5d00bce0a smartcard-i2c-fixes
## armsrc: smartcard / I2C correctness + SAM compatibility fixes

A focused pass over `armsrc/i2c.{c,h}` and `armsrc/i2c_direct.c` addressing correctness bugs (NULL deref on alloc failure, unbounded recursion, write-failure silently falling through to read), and removing two long-standing SAM-compatibility blockers (`SmartCardSetBaud` was a stub; float in firmware hot path).

### Fixed

- **`SmartCardRaw`: BigBuf_calloc result was unchecked.** NULL deref on memory pressure crashed the firmware. Now bails with `PM3_EMALLOC` before touching `resp[]`.
- **`SmartCardRaw`: `I2C_BufferWrite` failure was only acted on at `dbglevel > 3`.** Lower debug levels silently fell through to `sc_rx_bytes()` reading from a card we never wrote to. The `reply_ng` / `goto OUT` path now runs on any write failure; the `Dbprintf` is the only thing gated on dbglevel.
- **`SmartCardRaw`: replaced float division** `(p->wait_delay * 1000) / 3.07` with integer math via `uint64_t`. Avoids pulling `__aeabi_fdiv` into the ARM image and removes overflow at large `wait_delay` values: `wait = (ms * 100000 + 153) / 307`.
- **`SmartCardUpgrade`: BigBuf_calloc result was unchecked.** Now bails with `PM3_EMALLOC`. Renamed local `verfiydata` typo to `verifydata`. Failure messages now include the offset where the upgrade aborted (`Writing failed at offset 0x%04X` instead of `Writing failed`).
- **`I2C_BufferRead`: rejected `len == 0` but accepted `len == 1`**, which cannot represent the SIM module's 2-byte BE length header (returned -1 cast through int16_t). Tightened to `len < 2`.
- **`WaitSCL_L_timeout`: `return (delay == 0)` was dead code** — post-decrement underflows `delay` to `UINT32_MAX` after the loop, so the expression was always false. Replaced with explicit `return false` and corrected the comment that claimed an 1800 ms cap (actual cap is 1200 ms).
- **`i2c.h`: SIM_WAIT_DELAY comment was wrong** — `150000 * 3.07us = 460 ms`, not 270 ms. Updated comment to match the actual value; constant unchanged.
- **`SmartCardDirectSend`: BigBuf_calloc result for `resp[]` was unchecked.** The next `resp[0] = prepend` would NULL-deref on alloc failure. Now bails.
- **`SmartCardDirectSend`: GET RESPONSE (61xx) chain recursed without bound**, allocating a fresh `smart_card_raw_t` from BigBuf each round. A misbehaving card returning 61xx every time would wedge the device. Added `depth` parameter capped at `SC_DIRECT_MAX_DEPTH` (8); both call sites updated. The GET RESPONSE inner allocation is also now NULL-checked.
- **`SmartCardDirectSend`: same write-failure-silent fix** as `SmartCardRaw`.
- **`SmartCardDirectSend`: same float → integer-math conversion** as `SmartCardRaw`.

### Added

- **`SmartCardSetBaud` is now functional.** Was an empty stub (wired through `pm3_cmd.h` / `appmain.c` dispatch but did nothing). Now sends `I2C_DEVICE_CMD_SETBAUD` (0x04) to the SIM module's main address.

### Notes for follow-up

- `SmartCardSetBaud` is now functional on the firmware side, but no client command exposes it — `client/src/cmdsmartcard.c` has no `setbaud` subcommand. A separate change can wire `CMD_SMART_SETBAUD` into the client if needed; leaving it out of this patch keeps it firmware-only and safe to backport.
- The proxmark3 SIM module's stock asm firmware (`sim013`) silently drops `CMD_SETBAUD`. gentilkiwi's `sim_c` v4.50+ also currently dispatches it to a no-op. Full PPS follow-through requires a SIM-firmware-side patch in addition to this commit.
- Mid-byte SCL stretch timeouts in `I2C_SendByte` / `I2C_ReadByte` are still swallowed (return void / -1, both ignored at most callers). Fixing this requires changing those signatures and verifying every caller — out of scope here, merits its own PR.
2026-05-10 14:35:16 +08:00
..
…
2026-04-13 09:35:02 +02:00
2026-03-31 12:02:23 +03:00
2025-12-21 02:58:56 -07:00
2025-12-21 00:34:11 -07:00
2026-03-24 14:21:54 +07:00
2024-10-04 23:25:15 +08:00
2024-05-14 12:32:57 +02:00
…
2025-03-16 01:05:55 -07:00
2025-03-21 11:25:31 +01:00
2026-03-29 09:38:09 +07:00
…
2026-04-26 14:53:29 +03:00
…
2026-03-31 12:02:23 +03:00
2026-02-06 13:43:41 +01:00
…
…
…
…
2024-05-27 20:29:02 +02:00
2024-08-22 02:01:43 +08:00
2025-03-19 11:26:25 +01:00
2026-04-07 08:08:36 +07:00
2026-05-10 14:35:16 +08:00
2024-11-21 19:23:03 +00:00
2026-05-10 14:35:16 +08:00
2026-05-10 14:35:16 +08:00
2026-04-04 17:15:13 -04:00
2026-04-19 11:23:58 -07:00
2026-04-13 09:35:02 +02:00
…
2024-02-02 20:51:56 +01:00
2025-06-11 18:14:19 +02:00
…
2025-09-12 22:00:46 +02:00
2023-12-01 10:59:18 +01:00
2023-11-14 10:46:45 +08:00
…
2026-03-25 19:10:59 +07:00
2025-09-12 22:00:46 +02:00
2025-09-12 22:00:46 +02:00
2026-04-13 09:35:02 +02:00
…
…
2024-05-14 10:10:44 +02:00
2025-03-18 08:11:06 +01:00
2025-03-18 08:11:06 +01:00
2025-03-25 10:12:16 +01:00
…
2025-06-11 01:06:38 +08:00
2025-06-11 01:06:38 +08:00
2026-04-13 09:35:02 +02:00
2026-04-13 09:35:02 +02:00
2026-04-13 09:35:02 +02:00
2025-12-21 04:26:44 -07:00
2025-01-14 18:01:09 -08:00
2025-01-14 18:01:09 -08:00
…
…
2025-04-21 23:17:47 +10:00
2025-04-21 23:17:47 +10:00
2024-09-18 18:10:53 +08:00
2024-09-19 19:14:23 +08:00
…
…
…
…