make T=0 / T=1 autonegate over sim...

This commit is contained in:
iceman1001 committed 2026-08-29 20:05:19 +02:00
1 parent bb18ff0e49
commit d9b8f75b0c
3 files changed
+47 -3

No files matched your search

+35 -1
View File
@@ -53,6 +53,18 @@ static uint8_t s_card_protocols = 0;
// sc_raw_device_cmd() runs per APDU, so report the choice once per card
static bool s_proto_announced = false;
// The module opcode matching the protocol the card is actually running, taken
// from the active-protocol byte of a PPS response. 0 = nothing negotiated, so
// sc_active_device_cmd() falls back to T=0.
//
// Only a successful PPS sets this, which is what makes it safe to hand back
// SEND_T1 without probing the module version: I2C_DEVICE_CMD_PPS and
// I2C_DEVICE_CMD_SEND_T1 landed in the same module firmware
// (SIM_MODULE_VERS_T1_*), so a module that answered a PPS at all is known to
// understand T=1 too. An older module never answers, this stays 0, and callers
// keep the T=0 they had before.
static uint8_t s_pps_proto_cmd = 0;
// A negotiated rate lives in two places that reset independently: the module's
// UART divisor, which any I2C_Reset_EnterMainProgram() wipes, and the card,
// which only an RST pulse clears. Left alone the two drift apart and every
@@ -147,6 +159,7 @@ void I2C_Reset_EnterMainProgram(void) {
// whatever we knew about the card is no longer trustworthy
s_card_protocols = 0;
s_proto_announced = false;
s_pps_proto_cmd = 0;
StartTicks();
I2C_init(true);
I2C_SetResetStatus(0, 0, 0);
@@ -1107,7 +1120,27 @@ static bool sc_pps(uint8_t proto, uint8_t ta1) {
return false;
}
// resp is [ok][active protocol][ta1 in force]
return ((resp[0] == 1) && (resp[2] == ta1));
if ((resp[0] != 1) || (resp[2] != ta1)) {
return false;
}
// Take the protocol from the module rather than from the request: the
// caller asked for one, this is the one that ended up in force.
s_pps_proto_cmd = ((resp[1] & 0x0f) == 1) ? I2C_DEVICE_CMD_SEND_T1
: I2C_DEVICE_CMD_SEND_T0;
return true;
}
// The opcode to send an APDU with, for callers that have no protocol
// preference of their own and just want to talk to the card the way it is
// currently configured - the SAM path, which has no CLI flags to carry one.
//
// sc_raw_device_cmd() is the other half of this: it starts from a caller's
// explicit SC_RAW_T0 / SC_RAW_T1 and only overrides it when the ATR says that
// choice cannot work. Here there is nothing to override, so follow what PPS
// actually negotiated and default to T=0 when nothing has been.
uint8_t sc_active_device_cmd(void) {
return (s_pps_proto_cmd != 0) ? s_pps_proto_cmd : I2C_DEVICE_CMD_SEND_T0;
}
void sc_pps_remember(const uint8_t *atr, uint8_t atr_len, uint8_t proto, uint8_t ta1) {
@@ -1124,6 +1157,7 @@ void sc_pps_forget(void) {
s_pps.atr_len = 0;
s_pps.ta1 = 0;
s_pps.tried = false;
s_pps_proto_cmd = 0;
}
bool GetATR(smart_card_atr_t *card_ptr, bool verbose) {
+5
View File
@@ -112,6 +112,11 @@ bool I2C_WriteFW(const uint8_t *data, uint8_t len, uint8_t msb, uint8_t lsb, uin
// Which SIM module opcode a set of SC_RAW* flags asks for.
uint8_t sc_raw_device_cmd(smartcard_command_t flags);
// Which SIM module opcode matches the protocol the card is currently running,
// for callers that carry no SC_RAW* flags of their own. T=0 until a PPS says
// otherwise.
uint8_t sc_active_device_cmd(void);
// Log one smartcard frame, timestamped from the tick counter. Start is where
// the previous frame ended, so a Tag frame's span is how long the card took to
// answer and a Rdr frame's is how long the host took to ask.
+7 -2
View File
@@ -201,7 +201,12 @@ uint16_t sam_response_payload(const uint8_t *rx, uint16_t rx_len, uint16_t *payl
}
int sam_rxtx(const uint8_t *data, uint16_t n, uint8_t *resp, uint16_t *resplen) {
bool res = I2C_BufferWrite(data, n, I2C_DEVICE_CMD_SEND_T0, I2C_DEVICE_ADDRESS_MAIN);
// Whatever protocol GetATR()/PPS left the card on, rather than an assumed
// T=0. Resolved once per exchange so the GET RESPONSE round below cannot
// end up on a different protocol than the command it belongs to.
const uint8_t dev_cmd = sc_active_device_cmd();
bool res = I2C_BufferWrite(data, n, dev_cmd, I2C_DEVICE_ADDRESS_MAIN);
if (res == false) {
DbpString("failed to send to SIM CARD");
goto out;
@@ -259,7 +264,7 @@ int sam_rxtx(const uint8_t *data, uint16_t n, uint8_t *resp, uint16_t *resplen)
uint8_t cmd_getresp[] = {0x00, ISO7816_GET_RESPONSE, 0x00, 0x00, more_len};
sc_log_trace(cmd_getresp, sizeof(cmd_getresp), true);
res = I2C_BufferWrite(cmd_getresp, sizeof(cmd_getresp), I2C_DEVICE_CMD_SEND_T0, I2C_DEVICE_ADDRESS_MAIN);
res = I2C_BufferWrite(cmd_getresp, sizeof(cmd_getresp), dev_cmd, I2C_DEVICE_ADDRESS_MAIN);
if (res == false) {
DbpString("failed to send to SIM CARD 2");
goto out;