+ return 0;
+ }
+
+ if (!power)
+ flags |= ISO14B_DISCONNECT;
+
+ if (datalen>0)
+ flags |= ISO14B_RAW;
+
+ // Max buffer is USB_CMD_DATA_SIZE
+ datalen = (datalen > USB_CMD_DATA_SIZE) ? USB_CMD_DATA_SIZE : datalen;
+
+ UsbCommand c = {CMD_ISO_14443B_COMMAND, {flags, datalen, 0}};
+ memcpy(c.d.asBytes, data, datalen);
+ clearCommandBuffer();
+ SendCommand(&c);
+
+ if (!reply) return 1;
+
+ bool success = TRUE;
+ // get back iso14b_card_select_t, don't print it.
+ if (select)
+ success = waitCmd(FALSE);
+
+ // get back response from the raw bytes you sent.
+ if (success && datalen>0) waitCmd(TRUE);
+
+ return 1;
+}
+
+// print full atqb info
+// bytes
+// 0,1,2,3 = application data
+// 4 = bit rate capacity
+// 5 = max frame size / -4 info
+// 6 = FWI / Coding options
+static void print_atqb_resp(uint8_t *data, uint8_t cid){
+ //PrintAndLog(" UID: %s", sprint_hex(data+1,4));
+ PrintAndLog(" App Data: %s", sprint_hex(data,4));
+ PrintAndLog(" Protocol: %s", sprint_hex(data+4,3));
+ uint8_t BitRate = data[4];
+ if (!BitRate) PrintAndLog(" Bit Rate: 106 kbit/s only PICC <-> PCD");
+ if (BitRate & 0x10) PrintAndLog(" Bit Rate: 212 kbit/s PICC -> PCD supported");
+ if (BitRate & 0x20) PrintAndLog(" Bit Rate: 424 kbit/s PICC -> PCD supported");
+ if (BitRate & 0x40) PrintAndLog(" Bit Rate: 847 kbit/s PICC -> PCD supported");
+ if (BitRate & 0x01) PrintAndLog(" Bit Rate: 212 kbit/s PICC <- PCD supported");
+ if (BitRate & 0x02) PrintAndLog(" Bit Rate: 424 kbit/s PICC <- PCD supported");
+ if (BitRate & 0x04) PrintAndLog(" Bit Rate: 847 kbit/s PICC <- PCD supported");
+ if (BitRate & 0x80) PrintAndLog(" Same bit rate <-> required");
+
+ uint16_t maxFrame = data[5] >> 4;
+ if (maxFrame < 5) maxFrame = 8 * maxFrame + 16;
+ else if (maxFrame == 5) maxFrame = 64;
+ else if (maxFrame == 6) maxFrame = 96;
+ else if (maxFrame == 7) maxFrame = 128;
+ else if (maxFrame == 8) maxFrame = 256;
+ else maxFrame = 257;
+
+ PrintAndLog("Max Frame Size: %u%s bytes", maxFrame, (maxFrame == 257) ? "+ RFU" : "");
+
+ uint8_t protocolT = data[5] & 0xF;
+ PrintAndLog(" Protocol Type: Protocol is %scompliant with ISO/IEC 14443-4",(protocolT) ? "" : "not " );
+
+ uint8_t fwt = data[6]>>4;
+ if ( fwt < 16 ){
+ uint32_t etus = (32 << fwt);
+ uint32_t fwt_time = (302 << fwt);
+ PrintAndLog("Frame Wait Integer: %u - %u ETUs | %u us", fwt, etus, fwt_time);
+ } else {
+ PrintAndLog("Frame Wait Integer: %u - RFU", fwt);
+ }
+
+ PrintAndLog(" App Data Code: Application is %s",(data[6]&4) ? "Standard" : "Proprietary");
+ PrintAndLog(" Frame Options: NAD is %ssupported",(data[6]&2) ? "" : "not ");
+ PrintAndLog(" Frame Options: CID is %ssupported",(data[6]&1) ? "" : "not ");
+ PrintAndLog("Tag :");
+ PrintAndLog(" Max Buf Length: %u (MBLI) %s", cid>>4, (cid & 0xF0) ? "" : "chained frames not supported");
+ PrintAndLog(" CDI : %u", cid & 0x0f);
+ return;
+}
+
+// get SRx chip model (from UID) // from ST Microelectronics
+char *get_ST_Chip_Model(uint8_t data){
+ static char model[20];
+ char *retStr = model;
+ memset(model,0, sizeof(model));
+
+ switch (data) {
+ case 0x0: sprintf(retStr, "SRIX4K (Special)"); break;
+ case 0x2: sprintf(retStr, "SR176"); break;
+ case 0x3: sprintf(retStr, "SRIX4K"); break;
+ case 0x4: sprintf(retStr, "SRIX512"); break;
+ case 0x6: sprintf(retStr, "SRI512"); break;
+ case 0x7: sprintf(retStr, "SRI4K"); break;
+ case 0xC: sprintf(retStr, "SRT512"); break;
+ default : sprintf(retStr, "Unknown"); break;
+ }
+ return retStr;
+}
+
+// REMAKE:
+int print_ST_Lock_info(uint8_t model){
+
+ // PrintAndLog("Chip Write Protection Bits:");
+ // // now interpret the data
+ // switch (model){
+ // case 0x0: //fall through (SRIX4K special)
+ // case 0x3: //fall through (SRIx4K)
+ // case 0x7: // (SRI4K)
+ // //only need data[3]
+ // blk1 = 9;
+ // PrintAndLog(" raw: %s", sprint_bin(data+3, 1));
+ // PrintAndLog(" 07/08:%slocked", (data[3] & 1) ? " not " : " " );
+ // for (uint8_t i = 1; i<8; i++){
+ // PrintAndLog(" %02u:%slocked", blk1, (data[3] & (1 << i)) ? " not " : " " );
+ // blk1++;
+ // }
+ // break;
+ // case 0x4: //fall through (SRIX512)
+ // case 0x6: //fall through (SRI512)
+ // case 0xC: // (SRT512)
+ // //need data[2] and data[3]
+ // blk1 = 0;
+ // PrintAndLog(" raw: %s", sprint_bin(data+2, 2));
+ // for (uint8_t b=2; b<4; b++){
+ // for (uint8_t i=0; i<8; i++){
+ // PrintAndLog(" %02u:%slocked", blk1, (data[b] & (1 << i)) ? " not " : " " );
+ // blk1++;
+ // }
+ // }
+ // break;
+ // case 0x2: // (SR176)
+ // //need data[2]
+ // blk1 = 0;
+ // PrintAndLog(" raw: %s", sprint_bin(data+2, 1));
+ // for (uint8_t i = 0; i<8; i++){
+ // PrintAndLog(" %02u/%02u:%slocked", blk1, blk1+1, (data[2] & (1 << i)) ? " " : " not " );
+ // blk1+=2;
+ // }
+ // break;
+ // default:
+ // return rawClose();
+ // }
+ return 1;
+}
+
+// print UID info from SRx chips (ST Microelectronics)
+static void print_st_general_info(uint8_t *data, uint8_t len){
+ //uid = first 8 bytes in data
+ PrintAndLog(" UID: %s", sprint_hex(SwapEndian64(data,8,8), len));
+ PrintAndLog(" MFG: %02X, %s", data[6], getTagInfo(data[6]));
+ PrintAndLog("Chip: %02X, %s", data[5]>>2, get_ST_Chip_Model(data[5]>>2));
+ return;
+}
+
+//05 00 00 = find one tag in field
+//1d xx xx xx xx 00 08 01 00 = attrib xx=UID (resp 10 [f9 e0])
+//a3 = ? (resp 03 [e2 c2])
+//02 = ? (resp 02 [6a d3])
+// 022b (resp 02 67 00 [29 5b])
+// 0200a40400 (resp 02 67 00 [29 5b])
+// 0200a4040c07a0000002480300 (resp 02 67 00 [29 5b])
+// 0200a4040c07a0000002480200 (resp 02 67 00 [29 5b])
+// 0200a4040006a0000000010100 (resp 02 6a 82 [4b 4c])
+// 0200a4040c09d27600002545500200 (resp 02 67 00 [29 5b])
+// 0200a404000cd2760001354b414e4d30310000 (resp 02 6a 82 [4b 4c])
+// 0200a404000ca000000063504b43532d313500 (resp 02 6a 82 [4b 4c])
+// 0200a4040010a000000018300301000000000000000000 (resp 02 6a 82 [4b 4c])
+//03 = ? (resp 03 [e3 c2])
+//c2 = ? (resp c2 [66 15])
+//b2 = ? (resp a3 [e9 67])
+//a2 = ? (resp 02 [6a d3])
+
+// 14b get and print Full Info (as much as we know)
+bool HF14B_Std_Info(bool verbose){
+ //add more info here
+ return FALSE;
+}
+
+// SRx get and print full info (needs more info...)
+bool HF14B_ST_Info(bool verbose){
+
+ UsbCommand c = {CMD_ISO_14443B_COMMAND, {ISO14B_CONNECT | ISO14B_SELECT_SR | ISO14B_DISCONNECT, 0, 0}};
+ clearCommandBuffer();
+ SendCommand(&c);
+ UsbCommand resp;
+
+ if (!WaitForResponseTimeout(CMD_ACK, &resp, TIMEOUT)) {
+ if (verbose) PrintAndLog("timeout while waiting for reply.");
+ return FALSE;
+ }
+
+ iso14b_card_select_t card;
+ memcpy(&card, (iso14b_card_select_t *)resp.d.asBytes, sizeof(iso14b_card_select_t));
+
+ uint64_t status = resp.arg[0];
+ if ( status > 0 ) return switch_off_field_14b();
+
+ //add locking bit information here. uint8_t data[16] = {0x00};
+ // uint8_t datalen = 2;
+ // uint8_t resplen;
+ // uint8_t blk1;
+ // data[0] = 0x08;
+
+ //
+ // if (model == 0x2) { //SR176 has special command:
+ // data[1] = 0xf;
+ // resplen = 4;
+ // } else {
+ // data[1] = 0xff;
+ // resplen = 6;
+ // }
+
+ // //std read cmd
+ // if (HF14BCmdRaw(true, true, data, &datalen, false)==0)
+ // return rawClose();
+
+ // if (datalen != resplen || !crc) return rawClose();
+ //print_ST_Lock_info(data[5]>>2);
+ switch_off_field_14b();
+ return TRUE;
+}
+
+// get and print all info known about any known 14b tag
+bool HF14BInfo(bool verbose){
+
+ // try std 14b (atqb)
+ if (HF14B_Std_Info(verbose)) return TRUE;
+
+ // try st 14b
+ if (HF14B_ST_Info(verbose)) return TRUE;
+
+ // try unknown 14b read commands (to be identified later)
+ // could be read of calypso, CEPAS, moneo, or pico pass.
+
+ if (verbose) PrintAndLog("no 14443B tag found");
+ return FALSE;
+}
+
+// menu command to get and print all info known about any known 14b tag
+int CmdHF14Binfo(const char *Cmd){
+ char cmdp = param_getchar(Cmd, 0);
+ if (cmdp == 'h' || cmdp == 'H') return usage_hf_14b_info();
+
+ bool verbose = !((cmdp == 's') || (cmdp == 'S'));
+ return HF14BInfo(verbose);
+}
+
+bool HF14B_ST_Reader(bool verbose){
+
+ bool isSuccess = FALSE;
+
+ switch_on_field_14b();
+
+ // SRx get and print general info about SRx chip from UID
+ UsbCommand c = {CMD_ISO_14443B_COMMAND, {ISO14B_SELECT_SR, 0, 0}};
+ clearCommandBuffer();
+ SendCommand(&c);
+ UsbCommand resp;
+ if (!WaitForResponseTimeout(CMD_ACK, &resp, TIMEOUT)) {
+ if (verbose) PrintAndLog("timeout while waiting for reply.");
+ return FALSE;
+ }
+
+ iso14b_card_select_t card;
+ memcpy(&card, (iso14b_card_select_t *)resp.d.asBytes, sizeof(iso14b_card_select_t));
+
+ uint64_t status = resp.arg[0];
+
+ switch( status ){
+ case 0:
+ print_st_general_info(card.uid, card.uidlen);
+ isSuccess = TRUE;
+ break;
+ case 1:
+ if (verbose) PrintAndLog("iso14443-3 random chip id fail");
+ break;
+ case 2:
+ if (verbose) PrintAndLog("iso14443-3 ATTRIB fail");
+ break;
+ case 3:
+ if (verbose) PrintAndLog("iso14443-3 CRC fail");
+ break;
+ default:
+ if (verbose) PrintAndLog("iso14443b card select SRx failed");
+ break;
+ }
+
+ switch_off_field_14b();
+ return isSuccess;
+}
+
+bool HF14B_Std_Reader(bool verbose){
+
+ bool isSuccess = FALSE;
+
+ // 14b get and print UID only (general info)
+ UsbCommand c = {CMD_ISO_14443B_COMMAND, {ISO14B_CONNECT | ISO14B_SELECT_STD | ISO14B_DISCONNECT, 0, 0}};
+ clearCommandBuffer();
+ SendCommand(&c);
+ UsbCommand resp;
+
+ if (!WaitForResponseTimeout(CMD_ACK, &resp, TIMEOUT)) {
+ if (verbose) PrintAndLog("timeout while waiting for reply.");
+ return FALSE;