firmware: bugfix: disable cardemu comms in local SIM mode

This change prevents contention on the ISO7816 bus by disabling the card emulation state machine when the SIM switch is in the local mode. Without this change, the card emulation firmware can clobber ISO7816 communications and cause contention with certain (but not all) SIM cards.

Changes:
- Add 'enabled' flag to cardemu instance that is set/cleared by usb_command_sim_select() (the only place where sim switch occurs).
- Flag is initialized as false (disabled) by default, to match local SIM mode default.
- When card emulation is disabled, force SIM VCC to be "OFF",  SIM RESET as "not in RESET", and drop bytes bytes received on the ISO7816 interface (but do service buffers).

Change-Id: I4010f988712eac4a6af8568ccd60062f9de62449
This commit is contained in:
James Tavares 2022-01-10 21:12:54 -05:00 committed by jtavares
parent 2ceba0fdc4
commit ff434e4f12
1 changed files with 31 additions and 13 deletions

View File

@ -83,10 +83,21 @@ struct cardem_inst {
#ifdef DETECT_VCC_BY_ADC
uint32_t vcc_uv;
#endif
/*! real-time state of VCC I/O line, irrespective of enabled flag */
bool vcc_active;
/*! last VCC state we reported to the card emu state machine (conditioned by enabled flag) */
bool vcc_active_last;
/*! real-time state of RST I/O line, irrespective of enabled flag */
bool rst_active;
/*! last RST state we reported to the card emu state machine (conditioned by enabled flag) */
bool rst_active_last;
/*! flag indicating whether this instance should perform card emulation, or not */
bool enabled;
};
struct cardem_inst cardem_inst[] = {
@ -515,19 +526,25 @@ void ADC_IrqHandler(void)
#endif /* DETECT_VCC_BY_ADC */
/* called from main loop; dispatches card I/O state changes to card_emu from main loop */
/**
* called from main loop; dispatches card I/O state changes to card_emu from main loop.
* NOTE: conditions I/O state on the ci->enabled flag; if the instance is disabled, we assume VCC is
* disabled and the device is not in reset
*/
static void process_io_statechg(struct cardem_inst *ci)
{
if (ci->vcc_active != ci->vcc_active_last) {
card_emu_io_statechg(ci->ch, CARD_IO_VCC, ci->vcc_active);
const bool vcc_active = ci->vcc_active && ci->enabled;
if (vcc_active != ci->vcc_active_last) {
card_emu_io_statechg(ci->ch, CARD_IO_VCC, vcc_active);
/* FIXME do this for real */
card_emu_io_statechg(ci->ch, CARD_IO_CLK, ci->vcc_active);
ci->vcc_active_last = ci->vcc_active;
card_emu_io_statechg(ci->ch, CARD_IO_CLK, vcc_active);
ci->vcc_active_last = vcc_active;
}
if (ci->rst_active != ci->rst_active_last) {
card_emu_io_statechg(ci->ch, CARD_IO_RST, ci->rst_active);
ci->rst_active_last = ci->rst_active;
const bool rst_active = ci->rst_active && ci->enabled;
if (rst_active != ci->rst_active_last) {
card_emu_io_statechg(ci->ch, CARD_IO_RST, rst_active);
ci->rst_active_last = rst_active;
}
}
@ -778,10 +795,8 @@ static int usb_command_sim_select(struct msgb *msg, struct cardem_inst *ci)
if (msgb_l2len(msg) < sizeof(*mss))
return -1;
if (mss->remote_sim)
sim_switch_use_physical(ci->num, 0);
else
sim_switch_use_physical(ci->num, 1);
ci->enabled = mss->remote_sim ? true : false;
sim_switch_use_physical(ci->num, !ci->enabled);
return 0;
}
@ -925,7 +940,10 @@ void mode_cardemu_run(void)
}
uint8_t byte = rbuf_read(&ci->rb);
__enable_irq();
if (ci->enabled) {
card_emu_process_rx_byte(ci->ch, byte);
}
//TRACE_ERROR("%uRx%02x\r\n", i, byte);
}