+ return 0x11; // default value is 0x11, corresponding to fmax=5 MHz, Fi=372, Di=1.
+}
+
+int DiArray[] = {
+ 0, // b0000 RFU
+ 1, // b0001
+ 2,
+ 4,
+ 8,
+ 16,
+ 32, // b0110
+ 64, // b0111. This was RFU in ISO/IEC 7816-3:1997 and former. Some card readers or drivers may erroneously reject cards using this value
+ 12,
+ 20,
+ 0, // b1010 RFU
+ 0,
+ 0, // ...
+ 0,
+ 0,
+ 0 // b1111 RFU
+};
+
+int FiArray[] = {
+ 372, // b0000 Historical note: in ISO/IEC 7816-3:1989, this was assigned to cards with internal clock
+ 372, // b0001
+ 558, // b0010
+ 744, // b0011
+ 1116, // b0100
+ 1488, // b0101
+ 1860, // b0110
+ 0, // b0111 RFU
+ 0, // b1000 RFU
+ 512, // b1001
+ 768, // b1010
+ 1024, // b1011
+ 1536, // b1100
+ 2048, // b1101
+ 0, // b1110 RFU
+ 0 // b1111 RFU
+};
+
+float FArray[] = {
+ 4, // b0000 Historical note: in ISO/IEC 7816-3:1989, this was assigned to cards with internal clock
+ 5, // b0001
+ 6, // b0010
+ 8, // b0011
+ 12, // b0100
+ 16, // b0101
+ 20, // b0110
+ 0, // b0111 RFU
+ 0, // b1000 RFU
+ 5, // b1001
+ 7.5, // b1010
+ 10, // b1011
+ 15, // b1100
+ 20, // b1101
+ 0, // b1110 RFU
+ 0 // b1111 RFU
+};
+
+static int GetATRDi(uint8_t *atr, size_t atrlen) {
+ uint8_t TA1 = GetATRTA1(atr, atrlen);
+
+ return DiArray[TA1 & 0x0f]; // The 4 low-order bits of TA1 (4th MSbit to 1st LSbit) encode Di
+}
+
+static int GetATRFi(uint8_t *atr, size_t atrlen) {
+ uint8_t TA1 = GetATRTA1(atr, atrlen);
+
+ return FiArray[TA1 >> 4]; // The 4 high-order bits of TA1 (8th MSbit to 5th LSbit) encode fmax and Fi
+}
+
+static float GetATRF(uint8_t *atr, size_t atrlen) {
+ uint8_t TA1 = GetATRTA1(atr, atrlen);
+
+ return FArray[TA1 >> 4]; // The 4 high-order bits of TA1 (8th MSbit to 5th LSbit) encode fmax and Fi
+}
+
+static int PrintATR(uint8_t *atr, size_t atrlen) {
+
+ uint8_t T0 = atr[1];
+ uint8_t K = T0 & 0x0F;
+ uint8_t TD1 = 0, T1len = 0, TD1len = 0, TDilen = 0;
+ bool protocol_T0_present = true;
+ bool protocol_T15_present = false;
+
+ if (T0 & 0x10) {
+ PrintAndLog("\t- TA1 (Maximum clock frequency, proposed bit duration) [ 0x%02x ]", atr[2 + T1len]);
+ T1len++;
+ }
+
+ if (T0 & 0x20) {
+ PrintAndLog("\t- TB1 (Deprecated: VPP requirements) [ 0x%02x ]", atr[2 + T1len]);
+ T1len++;
+ }
+
+ if (T0 & 0x40) {
+ PrintAndLog("\t- TC1 (Extra delay between bytes required by card) [ 0x%02x ]", atr[2 + T1len]);
+ T1len++;
+ }
+
+ if (T0 & 0x80) {
+ TD1 = atr[2 + T1len];
+ PrintAndLog("\t- TD1 (First offered transmission protocol, presence of TA2..TD2) [ 0x%02x ] Protocol T%d", TD1, TD1 & 0x0f);
+ protocol_T0_present = false;
+ if ((TD1 & 0x0f) == 0) {
+ protocol_T0_present = true;
+ }
+ if ((TD1 & 0x0f) == 15) {
+ protocol_T15_present = true;
+ }
+
+ T1len++;
+
+ if (TD1 & 0x10) {
+ PrintAndLog("\t- TA2 (Specific protocol and parameters to be used after the ATR) [ 0x%02x ]", atr[2 + T1len + TD1len]);
+ TD1len++;
+ }
+ if (TD1 & 0x20) {
+ PrintAndLog("\t- TB2 (Deprecated: VPP precise voltage requirement) [ 0x%02x ]", atr[2 + T1len + TD1len]);
+ TD1len++;
+ }
+ if (TD1 & 0x40) {
+ PrintAndLog("\t- TC2 (Maximum waiting time for protocol T=0) [ 0x%02x ]", atr[2 + T1len + TD1len]);
+ TD1len++;
+ }
+ if (TD1 & 0x80) {
+ uint8_t TDi = atr[2 + T1len + TD1len];
+ PrintAndLog("\t- TD2 (A supported protocol or more global parameters, presence of TA3..TD3) [ 0x%02x ] Protocol T%d", TDi, TDi & 0x0f);
+ if ((TDi & 0x0f) == 0) {
+ protocol_T0_present = true;
+ }
+ if ((TDi & 0x0f) == 15) {
+ protocol_T15_present = true;
+ }
+ TD1len++;
+
+ bool nextCycle = true;
+ uint8_t vi = 3;
+ while (nextCycle) {
+ nextCycle = false;
+ if (TDi & 0x10) {
+ PrintAndLog("\t- TA%d: 0x%02x", vi, atr[2 + T1len + TD1len + TDilen]);
+ TDilen++;
+ }
+ if (TDi & 0x20) {
+ PrintAndLog("\t- TB%d: 0x%02x", vi, atr[2 + T1len + TD1len + TDilen]);
+ TDilen++;
+ }
+ if (TDi & 0x40) {
+ PrintAndLog("\t- TC%d: 0x%02x", vi, atr[2 + T1len + TD1len + TDilen]);
+ TDilen++;
+ }
+ if (TDi & 0x80) {
+ TDi = atr[2 + T1len + TD1len + TDilen];
+ PrintAndLog("\t- TD%d [ 0x%02x ] Protocol T%d", vi, TDi, TDi & 0x0f);
+ TDilen++;
+
+ nextCycle = true;
+ vi++;
+ }
+ }
+ }
+ }
+
+ if (!protocol_T0_present || protocol_T15_present) { // there is CRC Check Byte TCK
+ uint8_t vxor = 0;
+ for (int i = 1; i < atrlen; i++)
+ vxor ^= atr[i];
+
+ if (vxor)
+ PrintAndLogEx(WARNING, "Check sum error. Must be 0 got 0x%02X", vxor);
+ else
+ PrintAndLogEx(INFO, "Check sum OK.");
+ }
+
+ if (atr[0] != 0x3b)
+ PrintAndLogEx(WARNING, "Not a direct convention [ 0x%02x ]", atr[0]);
+
+ uint8_t calen = 2 + T1len + TD1len + TDilen + K;
+
+ if (atrlen != calen && atrlen != calen + 1) // may be CRC
+ PrintAndLogEx(ERR, "ATR length error. len: %d, T1len: %d, TD1len: %d, TDilen: %d, K: %d", atrlen, T1len, TD1len, TDilen, K);
+
+ if (K > 0)
+ PrintAndLogEx(INFO, "\nHistorical bytes | len %02d | format %02x", K, atr[2 + T1len + TD1len + TDilen]);
+
+ if (K > 1) {
+ PrintAndLogEx(INFO, "\tHistorical bytes");
+ dump_buffer(&atr[2 + T1len + TD1len + TDilen], K, NULL, 1);
+ }
+
+ return 0;
+}
+
+static bool smart_getATR(smart_card_atr_t *card)
+{
+ if (UseAlternativeSmartcardReader) {
+ return pcscGetATR(card);
+ } else {
+ UsbCommand c = {CMD_SMART_ATR, {0, 0, 0}};
+ SendCommand(&c);
+
+ UsbCommand resp;
+ if ( !WaitForResponseTimeout(CMD_ACK, &resp, 2500) ) {
+ return false;
+ }
+
+ if (resp.arg[0] & 0xff) {
+ return resp.arg[0] & 0xFF;
+ }
+
+ memcpy(card, (smart_card_atr_t *)resp.d.asBytes, sizeof(smart_card_atr_t));
+
+ return true;
+ }
+}
+
+static bool smart_select(bool silent) {
+
+ smart_card_atr_t card;
+ if (!smart_getATR(&card)) {