diff --git a/armsrc/i2c.c b/armsrc/i2c.c index 6a4289ccf..0a54b575c 100644 --- a/armsrc/i2c.c +++ b/armsrc/i2c.c @@ -41,6 +41,9 @@ // cannot drift apart. #define I2C_DELAY_1CLK SpinDelayUsPrecision(I2C_DELAY_1CLK_US) #define I2C_DELAY_2CLK SpinDelayUsPrecision(I2C_DELAY_2CLK_US) +#define I2C_DELAY_SDA SpinDelayUsPrecision(I2C_DELAY_SDA_US) +#define I2C_DELAY_HOLD SpinDelayUsPrecision(I2C_DELAY_HOLD_US) +#define I2C_DELAY_HIGH SpinDelayUsPrecision(I2C_DELAY_HIGH_US) #define SC_PROTO_T0 (1 << 0) #define SC_PROTO_T1 (1 << 1) @@ -188,15 +191,11 @@ static bool WaitSCL_L(void) { #define SIM_START_TIMEOUT_MS 50 static bool WaitSCL_L_timeout(void) { - volatile uint32_t delay = SIM_START_TIMEOUT_MS; - while (delay--) { - // exit on SCL LOW - if (SCL_read == false) - return true; - - WaitMS(1); - } - return false; + // How long the module may take to *start* stretching, not how long it may + // hold. Polled at bus granularity: a command the module already finished + // never shows SCL low at all, and at 1 ms a step that cost the full + // timeout on every fast exchange. + return WaitSCL_L_delay(I2C_ITERS_FOR_MS(SIM_START_TIMEOUT_MS)); } static bool I2C_Start(void) { @@ -318,7 +317,7 @@ static void I2C_SendByte(uint8_t data) { while (bits--) { SCL_L; - I2C_DELAY_1CLK; + I2C_DELAY_HOLD; if (data & 0x80) SDA_H; @@ -327,14 +326,14 @@ static void I2C_SendByte(uint8_t data) { data <<= 1; - I2C_DELAY_1CLK; + I2C_DELAY_SDA; SCL_H; if (WaitSCL_H() == false) { return; } - I2C_DELAY_2CLK; + I2C_DELAY_HIGH; } SCL_L; } @@ -351,13 +350,13 @@ static int16_t I2C_ReadByte(void) { return -2; } - I2C_DELAY_1CLK; + I2C_DELAY_SDA; SCL_H; if (WaitSCL_H() == false) { return -1; } - I2C_DELAY_1CLK; + I2C_DELAY_HIGH; if (SDA_read) { b |= 0x01; } diff --git a/armsrc/i2c.h b/armsrc/i2c.h index 62dc1fd1a..30d320704 100644 --- a/armsrc/i2c.h +++ b/armsrc/i2c.h @@ -60,6 +60,16 @@ #define I2C_DELAY_1CLK_US 20 #define I2C_DELAY_2CLK_US 22 +// Only one delay per bit is rise time critical: the one bracketing an SDA +// transition, where a released line has to charge through the pull-up before +// it reads as a 1. Sampling before that is what corrupted the bus every time +// these were simply scaled down together. The others are padding - hold after +// SCL falls (spec 0.3 us) and SCL high width (spec 4 us) - so they get the +// standard mode minimum with margin instead of a full clock. +#define I2C_DELAY_SDA_US I2C_DELAY_1CLK_US +#define I2C_DELAY_HOLD_US 2 +#define I2C_DELAY_HIGH_US 6 + // The SCL wait loops spend one 1CLK per iteration, so their timeouts are // iteration counts. Written in ms and converted here so the two cannot drift. #define I2C_ITERS_PER_MS (1000U / I2C_DELAY_1CLK_US) diff --git a/client/resources/sim017.bin b/client/resources/sim017.bin new file mode 100644 index 000000000..11f324200 Binary files /dev/null and b/client/resources/sim017.bin differ diff --git a/client/resources/sim017.sha512.txt b/client/resources/sim017.sha512.txt new file mode 100644 index 000000000..a36cc3581 --- /dev/null +++ b/client/resources/sim017.sha512.txt @@ -0,0 +1 @@ +f7fc647de354c8bad56587e10d7a906e0c2efa13db441bb2fe04df9bfeffb4f8e4bd2a12ba3394b9832fec30dd367ce2319178f960afcfc3637a6eb19b9704da *sim017.bin diff --git a/client/src/emv/cmdemv.c b/client/src/emv/cmdemv.c index da9966dda..7309d30fe 100644 --- a/client/src/emv/cmdemv.c +++ b/client/src/emv/cmdemv.c @@ -2951,18 +2951,27 @@ static int CmdEMVReader(const char *Cmd) { const char *al = "Applets"; struct tlvdb *tlvSelect = tlvdb_fixed(1, strlen(al), (const unsigned char *)al); - // Search the expected directory then the other - a contact card may - // carry 1PAY or 2PAY and plenty carry only one. First call activates - // the field, the fallback reuses it. + // Search the expected directory then the other - a card may carry 1PAY + // or 2PAY and plenty carry only one. First call activates the field, + // the fallback reuses it. Both stay quiet: a card without the + // directory we guessed at is ordinary, and the AID sweep below is the + // real fallback, so its failure is the one worth reporting. res = EMVSearchPSE(channel, true, true, psenum, false, tlvSelect, true); if (res) { - res = EMVSearchPSE(channel, false, true, (psenum == 1) ? 2 : 1, false, tlvSelect, false); + res = EMVSearchPSE(channel, false, true, (psenum == 1) ? 2 : 1, false, tlvSelect, true); } if (res) { // EMV SEARCH with AID list DropFieldEx(channel); if (EMVSearch(channel, true, true, false, tlvSelect, false)) { + PrintAndLogEx(FAILED, "No EMV application found (no PSE, no PPSE, no known AID)"); + if (channel == CC_CONTACT) { + smart_card_atr_t atr; + if (smart_select(false, &atr) == false) { + PrintAndLogEx(HINT, "Hint: no answer at all - is a card in the slot? try `" _YELLOW_("smart info") "`"); + } + } tlvdb_free(tlvSelect); DropFieldEx(channel); continue;