From 0046522eaddf040a33dc0da90a8049ba6fa8bf64 Mon Sep 17 00:00:00 2001 From: pwpiwi Date: Wed, 4 Dec 2019 18:14:33 +0100 Subject: [PATCH] fix 'hf 15 csetuid' and 'hf 15 cmd write -o' * allow longer timeout for write commands * add function to send EOF only * modify 'hf list 15' to display "" * add tracing of Reader commands * if REQ_OPTION is set on write commands, send separate EOF to request tag response * use #defines instead of hex constants * some whitespace fixes --- armsrc/iso15693.c | 148 ++++++++++++++++++++++------------ armsrc/iso15693.h | 2 - client/cmdhf15.c | 193 +++++++++++++++++++++------------------------ client/cmdhflist.c | 53 +++++++------ common/protocols.h | 1 + 5 files changed, 216 insertions(+), 181 deletions(-) diff --git a/armsrc/iso15693.c b/armsrc/iso15693.c index 9ce27a18..453e7a96 100644 --- a/armsrc/iso15693.c +++ b/armsrc/iso15693.c @@ -75,6 +75,12 @@ #define DELAY_TAG_TO_ARM_SNOOP 32 #define DELAY_READER_TO_ARM_SNOOP 32 +// times in samples @ 212kHz when acting as reader +//#define ISO15693_READER_TIMEOUT 80 // 80/212kHz = 378us, nominal t1_max=313,9us +#define ISO15693_READER_TIMEOUT 330 // 330/212kHz = 1558us, should be even enough for iClass tags responding to ACTALL +#define ISO15693_READER_TIMEOUT_WRITE 4700 // 4700/212kHz = 22ms, nominal 20ms + + static int DEBUG = 0; @@ -140,6 +146,15 @@ void CodeIso15693AsReader(uint8_t *cmd, int n) { ToSendMax++; } + +// Encode EOF only +static void CodeIso15693AsReaderEOF() { + ToSendReset(); + ToSend[++ToSendMax] = 0x20; + ToSendMax++; +} + + // encode data using "1 out of 256" scheme // data rate is 1,66 kbit/s (fc/8192) // is designed for more robust communication over longer distances @@ -1105,16 +1120,12 @@ int GetIso15693CommandFromReader(uint8_t *received, size_t max_len, uint32_t *eo } -// Encode (into the ToSend buffers) an identify request, which is the first +// Construct an identify (Inventory) request, which is the first // thing that you must send to a tag to get a response. -static void BuildIdentifyRequest(void) -{ - uint8_t cmd[5]; - +static void BuildIdentifyRequest(uint8_t *cmd) { uint16_t crc; // one sub-carrier, inventory, 1 slot, fast rate - // AFI is at bit 5 (1<<4) when doing an INVENTORY - cmd[0] = (1 << 2) | (1 << 5) | (1 << 1); + cmd[0] = ISO15693_REQ_INVENTORY | ISO15693_REQINV_SLOT1 | ISO15693_REQ_DATARATE_HIGH; // inventory command code cmd[1] = 0x01; // no mask @@ -1123,8 +1134,6 @@ static void BuildIdentifyRequest(void) crc = Iso15693Crc(cmd, 3); cmd[3] = crc & 0xff; cmd[4] = crc >> 8; - - CodeIso15693AsReader(cmd, sizeof(cmd)); } @@ -1133,8 +1142,7 @@ static void BuildIdentifyRequest(void) // for the response. The response is not demodulated, just left in the buffer // so that it can be downloaded to a PC and processed there. //----------------------------------------------------------------------------- -void AcquireRawAdcSamplesIso15693(void) -{ +void AcquireRawAdcSamplesIso15693(void) { LED_A_ON(); uint8_t *dest = BigBuf_get_addr(); @@ -1145,7 +1153,9 @@ void AcquireRawAdcSamplesIso15693(void) FpgaSetupSsc(FPGA_MAJOR_MODE_HF_READER); SetAdcMuxFor(GPIO_MUXSEL_HIPKD); - BuildIdentifyRequest(); + uint8_t cmd[5]; + BuildIdentifyRequest(cmd); + CodeIso15693AsReader(cmd, sizeof(cmd)); // Give the tags time to energize SpinDelay(100); @@ -1371,10 +1381,7 @@ void Iso15693InitReader() { // uid is in transmission order (which is reverse of display order) -static void BuildReadBlockRequest(uint8_t *uid, uint8_t blockNumber ) -{ - uint8_t cmd[13]; - +static void BuildReadBlockRequest(uint8_t *uid, uint8_t blockNumber, uint8_t *cmd) { uint16_t crc; // If we set the Option_Flag in this request, the VICC will respond with the security status of the block // followed by the block data @@ -1398,13 +1405,11 @@ static void BuildReadBlockRequest(uint8_t *uid, uint8_t blockNumber ) cmd[11] = crc & 0xff; cmd[12] = crc >> 8; - CodeIso15693AsReader(cmd, sizeof(cmd)); } // Now the VICC>VCD responses when we are simulating a tag -static void BuildInventoryResponse(uint8_t *uid) -{ +static void BuildInventoryResponse(uint8_t *uid) { uint8_t cmd[12]; uint16_t crc; @@ -1433,7 +1438,7 @@ static void BuildInventoryResponse(uint8_t *uid) // speed ... 0 low speed, 1 hi speed // *recv will contain the tag's answer // return: length of received data, or -1 for timeout -int SendDataTag(uint8_t *send, int sendlen, bool init, int speed, uint8_t *recv, uint16_t max_recv_len, uint32_t start_time, uint32_t *eof_time) { +int SendDataTag(uint8_t *send, int sendlen, bool init, bool speed_fast, uint8_t *recv, uint16_t max_recv_len, uint32_t start_time, uint16_t timeout, uint32_t *eof_time) { if (init) { Iso15693InitReader(); @@ -1442,19 +1447,40 @@ int SendDataTag(uint8_t *send, int sendlen, bool init, int speed, uint8_t *recv, int answerLen = 0; - if (!speed) { - // low speed (1 out of 256) - CodeIso15693AsReader256(send, sendlen); - } else { + if (speed_fast) { // high speed (1 out of 4) CodeIso15693AsReader(send, sendlen); + } else { + // low speed (1 out of 256) + CodeIso15693AsReader256(send, sendlen); } TransmitTo15693Tag(ToSend, ToSendMax, &start_time); + uint32_t end_time = start_time + 32*(8*ToSendMax-4); // substract the 4 padding bits after EOF + LogTrace_ISO15693(send, sendlen, start_time*4, end_time*4, NULL, true); // Now wait for a response if (recv != NULL) { - answerLen = GetIso15693AnswerFromTag(recv, max_recv_len, ISO15693_READER_TIMEOUT, eof_time); + answerLen = GetIso15693AnswerFromTag(recv, max_recv_len, timeout, eof_time); + } + + return answerLen; +} + + +int SendDataTagEOF(uint8_t *recv, uint16_t max_recv_len, uint32_t start_time, uint16_t timeout, uint32_t *eof_time) { + + int answerLen = 0; + + CodeIso15693AsReaderEOF(); + + TransmitTo15693Tag(ToSend, ToSendMax, &start_time); + uint32_t end_time = start_time + 32*(8*ToSendMax-4); // substract the 4 padding bits after EOF + LogTrace_ISO15693(NULL, 0, start_time*4, end_time*4, NULL, true); + + // Now wait for a response + if (recv != NULL) { + answerLen = GetIso15693AnswerFromTag(recv, max_recv_len, timeout, eof_time); } return answerLen; @@ -1546,7 +1572,6 @@ void ReaderIso15693(uint32_t parameter) { set_tracing(true); - int answerLen = 0; uint8_t TagUID[8] = {0x00}; FpgaDownloadAndGo(FPGA_BITSTREAM_HF); @@ -1572,13 +1597,11 @@ void ReaderIso15693(uint32_t parameter) { // THIS MEANS WE CAN PRE-BUILD REQUESTS TO SAVE CPU TIME // Now send the IDENTIFY command - BuildIdentifyRequest(); + uint8_t cmd[5]; + BuildIdentifyRequest(cmd); uint32_t start_time = 0; - TransmitTo15693Tag(ToSend, ToSendMax, &start_time); - - // Now wait for a response uint32_t eof_time; - answerLen = GetIso15693AnswerFromTag(answer, sizeof(answer), DELAY_ISO15693_VCD_TO_VICC_READER * 2, &eof_time) ; + int answerLen = SendDataTag(cmd, sizeof(cmd), true, true, answer, sizeof(answer), start_time, ISO15693_READER_TIMEOUT, &eof_time); start_time = eof_time + DELAY_ISO15693_VICC_TO_VCD_READER; if (answerLen >= 12) { // we should do a better check than this @@ -1605,9 +1628,9 @@ void ReaderIso15693(uint32_t parameter) { // read all pages if (answerLen >= 12 && DEBUG) { for (int i = 0; i < 32; i++) { // sanity check, assume max 32 pages - BuildReadBlockRequest(TagUID, i); - TransmitTo15693Tag(ToSend, ToSendMax, &start_time); - int answerLen = GetIso15693AnswerFromTag(answer, sizeof(answer), DELAY_ISO15693_VCD_TO_VICC_READER * 2, &eof_time); + uint8_t cmd[13]; + BuildReadBlockRequest(TagUID, i, cmd); + answerLen = SendDataTag(cmd, sizeof(cmd), false, true, answer, sizeof(answer), start_time, ISO15693_READER_TIMEOUT, &eof_time); start_time = eof_time + DELAY_ISO15693_VICC_TO_VCD_READER; if (answerLen > 0) { Dbprintf("READ SINGLE BLOCK %d returned %d octets:", i, answerLen); @@ -1669,8 +1692,7 @@ void SimTagIso15693(uint32_t parameter, uint8_t *uid) { // Since there is no standardized way of reading the AFI out of a tag, we will brute force it // (some manufactures offer a way to read the AFI, though) -void BruteforceIso15693Afi(uint32_t speed) -{ +void BruteforceIso15693Afi(uint32_t speed) { LED_A_ON(); uint8_t data[6]; @@ -1686,7 +1708,7 @@ void BruteforceIso15693Afi(uint32_t speed) data[2] = 0; // mask length datalen = Iso15693AddCrc(data,3); uint32_t start_time = GetCountSspClk(); - recvlen = SendDataTag(data, datalen, true, speed, recv, sizeof(recv), 0, &eof_time); + recvlen = SendDataTag(data, datalen, true, speed, recv, sizeof(recv), 0, ISO15693_READER_TIMEOUT, &eof_time); start_time = eof_time + DELAY_ISO15693_VICC_TO_VCD_READER; WDT_HIT(); if (recvlen>=12) { @@ -1703,7 +1725,7 @@ void BruteforceIso15693Afi(uint32_t speed) for (int i = 0; i < 256; i++) { data[2] = i & 0xFF; datalen = Iso15693AddCrc(data,4); - recvlen = SendDataTag(data, datalen, false, speed, recv, sizeof(recv), start_time, &eof_time); + recvlen = SendDataTag(data, datalen, false, speed, recv, sizeof(recv), start_time, ISO15693_READER_TIMEOUT, &eof_time); start_time = eof_time + DELAY_ISO15693_VICC_TO_VCD_READER; WDT_HIT(); if (recvlen >= 12) { @@ -1727,13 +1749,35 @@ void DirectTag15693Command(uint32_t datalen, uint32_t speed, uint32_t recv, uint uint8_t recvbuf[ISO15693_MAX_RESPONSE_LENGTH]; uint32_t eof_time; + uint16_t timeout; + bool request_answer = false; + + switch (data[1]) { + case ISO15693_WRITEBLOCK: + case ISO15693_LOCKBLOCK: + case ISO15693_WRITE_MULTI_BLOCK: + case ISO15693_WRITE_AFI: + case ISO15693_LOCK_AFI: + case ISO15693_WRITE_DSFID: + case ISO15693_LOCK_DSFID: + timeout = ISO15693_READER_TIMEOUT_WRITE; + request_answer = data[0] & ISO15693_REQ_OPTION; + break; + default: + timeout = ISO15693_READER_TIMEOUT; + } + if (DEBUG) { Dbprintf("SEND:"); Dbhexdump(datalen, data, false); } - recvlen = SendDataTag(data, datalen, true, speed, (recv?recvbuf:NULL), sizeof(recvbuf), 0, &eof_time); + recvlen = SendDataTag(data, datalen, true, speed, (recv?recvbuf:NULL), sizeof(recvbuf), 0, timeout, &eof_time); + if (request_answer) { // send a single EOF to get the tag response + recvlen = SendDataTagEOF((recv?recvbuf:NULL), sizeof(recvbuf), 0, ISO15693_READER_TIMEOUT, &eof_time); + } + // for the time being, switch field off to protect rdv4.0 // note: this prevents using hf 15 cmd with s option - which isn't implemented yet anyway FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF); @@ -1761,16 +1805,16 @@ void DirectTag15693Command(uint32_t datalen, uint32_t speed, uint32_t recv, uint // //----------------------------------------------------------------------------- -// Set the UID to the tag (based on Iceman work). +// Set the UID on Magic ISO15693 tag (based on Iceman's LUA-script). void SetTag15693Uid(uint8_t *uid) { LED_A_ON(); uint8_t cmd[4][9] = { - {0x02, 0x21, 0x3e, 0x00, 0x00, 0x00, 0x00}, - {0x02, 0x21, 0x3f, 0x69, 0x96, 0x00, 0x00}, - {0x02, 0x21, 0x38}, - {0x02, 0x21, 0x39} + {ISO15693_REQ_DATARATE_HIGH, ISO15693_WRITEBLOCK, 0x3e, 0x00, 0x00, 0x00, 0x00}, + {ISO15693_REQ_DATARATE_HIGH, ISO15693_WRITEBLOCK, 0x3f, 0x69, 0x96, 0x00, 0x00}, + {ISO15693_REQ_DATARATE_HIGH, ISO15693_WRITEBLOCK, 0x38}, + {ISO15693_REQ_DATARATE_HIGH, ISO15693_WRITEBLOCK, 0x39} }; uint16_t crc; @@ -1791,30 +1835,32 @@ void SetTag15693Uid(uint8_t *uid) { cmd[3][5] = uid[1]; cmd[3][6] = uid[0]; + uint32_t start_time = 0; + for (int i = 0; i < 4; i++) { // Add the CRC crc = Iso15693Crc(cmd[i], 7); cmd[i][7] = crc & 0xff; cmd[i][8] = crc >> 8; - recvlen = SendDataTag(cmd[i], sizeof(cmd[i]), true, 1, recvbuf, sizeof(recvbuf), 0, &eof_time); - + recvlen = SendDataTag(cmd[i], sizeof(cmd[i]), i==0?true:false, true, recvbuf, sizeof(recvbuf), start_time, ISO15693_READER_TIMEOUT_WRITE, &eof_time); + start_time = eof_time + DELAY_ISO15693_VICC_TO_VCD_READER; if (DEBUG) { Dbprintf("SEND:"); Dbhexdump(sizeof(cmd[i]), cmd[i], false); - } - - if (DEBUG) { Dbprintf("RECV:"); if (recvlen > 0) { Dbhexdump(recvlen, recvbuf, false); DbdecodeIso15693Answer(recvlen, recvbuf); } } - - cmd_send(CMD_ACK, recvlen>ISO15693_MAX_RESPONSE_LENGTH?ISO15693_MAX_RESPONSE_LENGTH:recvlen, 0, 0, recvbuf, ISO15693_MAX_RESPONSE_LENGTH); + + if (recvlen <= 0) { + break; + } } + cmd_send(CMD_ACK, recvlen, 0, 0, NULL, 0); LED_A_OFF(); } diff --git a/armsrc/iso15693.h b/armsrc/iso15693.h index 5175ff20..e48e4d4a 100644 --- a/armsrc/iso15693.h +++ b/armsrc/iso15693.h @@ -21,8 +21,6 @@ //SSP_CLK runs at 13.56MHz / 4 = 3,39MHz when acting as reader. All values should be multiples of 16 #define DELAY_ISO15693_VCD_TO_VICC_READER 1056 // 1056/3,39MHz = 311.5us from end of command EOF to start of tag response #define DELAY_ISO15693_VICC_TO_VCD_READER 1024 // 1024/3.39MHz = 302.1us between end of tag response and next reader command -// times in samples @ 212kHz when acting as reader -#define ISO15693_READER_TIMEOUT 330 // 330/212kHz = 1558us, should be even enough for iClass tags responding to ACTALL void Iso15693InitReader(); void CodeIso15693AsReader(uint8_t *cmd, int n); diff --git a/client/cmdhf15.c b/client/cmdhf15.c index 5e7b3e82..1e165101 100644 --- a/client/cmdhf15.c +++ b/client/cmdhf15.c @@ -137,8 +137,7 @@ static char* TagErrorStr(uint8_t error) { // Mode 3 -int CmdHF15Demod(const char *Cmd) -{ +static int CmdHF15Demod(const char *Cmd) { // The sampling rate is 106.353 ksps/s, for T = 18.8 us int i, j; @@ -222,25 +221,23 @@ int CmdHF15Demod(const char *Cmd) } - // * Acquire Samples as Reader (enables carrier, sends inquiry) -int CmdHF15Read(const char *Cmd) -{ +static int CmdHF15Read(const char *Cmd) { UsbCommand c = {CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_15693}; SendCommand(&c); return 0; } + // Record Activity without enabling carrier -int CmdHF15Snoop(const char *Cmd) -{ +static int CmdHF15Snoop(const char *Cmd) { UsbCommand c = {CMD_SNOOP_ISO_15693}; SendCommand(&c); return 0; } -int HF15Reader(const char *Cmd, bool verbose) -{ + +int HF15Reader(const char *Cmd, bool verbose) { uint8_t uid[8]; if (!getUID(uid)) { @@ -254,16 +251,16 @@ int HF15Reader(const char *Cmd, bool verbose) return 1; } -int CmdHF15Reader(const char *Cmd) -{ + +static int CmdHF15Reader(const char *Cmd) { UsbCommand c = {CMD_READER_ISO_15693, {strtol(Cmd, NULL, 0), 0, 0}}; SendCommand(&c); return 0; } + // Simulation is still not working very good -int CmdHF15Sim(const char *Cmd) -{ +static int CmdHF15Sim(const char *Cmd) { char cmdp = param_getchar(Cmd, 0); uint8_t uid[8] = {0x00}; @@ -291,17 +288,18 @@ int CmdHF15Sim(const char *Cmd) return 0; } + // finds the AFI (Application Family Idendifier) of a card, by trying all values // (There is no standard way of reading the AFI, allthough some tags support this) -int CmdHF15Afi(const char *Cmd) -{ +static int CmdHF15Afi(const char *Cmd) { UsbCommand c = {CMD_ISO_15693_FIND_AFI, {strtol(Cmd, NULL, 0), 0, 0}}; SendCommand(&c); return 0; } + // Reads all memory pages -int CmdHF15DumpMem(const char*Cmd) { +static int CmdHF15DumpMem(const char*Cmd) { UsbCommand resp; uint8_t uid[8]; uint8_t *recv=NULL; @@ -367,8 +365,7 @@ int CmdHF15DumpMem(const char*Cmd) { } -int CmdHF15CmdInquiry(const char *Cmd) -{ +static int CmdHF15CmdInquiry(const char *Cmd) { UsbCommand resp; uint8_t *recv; UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv? @@ -400,7 +397,7 @@ int CmdHF15CmdInquiry(const char *Cmd) // Turns debugging on(1)/off(0) -int CmdHF15CmdDebug( const char *cmd) { +static int CmdHF15CmdDebug( const char *cmd) { int debug = atoi(cmd); if (strlen(cmd) < 1) { PrintAndLog("Usage: hf 15 debug <0|1>"); @@ -415,7 +412,7 @@ int CmdHF15CmdDebug( const char *cmd) { } -int CmdHF15CmdRaw (const char *cmd) { +static int CmdHF15CmdRaw (const char *cmd) { UsbCommand resp; uint8_t *recv; UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv? @@ -525,30 +522,30 @@ int CmdHF15CmdRaw (const char *cmd) { * Parameters: * **cmd command line */ -int prepareHF15Cmd(char **cmd, UsbCommand *c, uint8_t iso15cmd[], int iso15cmdlen) { +static int prepareHF15Cmd(char **cmd, UsbCommand *c, uint8_t iso15cmd[], int iso15cmdlen) { int temp; - uint8_t *req=c->d.asBytes; + uint8_t *req = c->d.asBytes; uint8_t uid[8] = {0x00}; - uint32_t reqlen=0; + uint32_t reqlen = 0; // strip while (**cmd==' ' || **cmd=='\t') (*cmd)++; - if (strstr(*cmd,"-2")==*cmd) { - c->arg[1]=0; // use 1of256 - (*cmd)+=2; + if (strstr(*cmd, "-2") == *cmd) { + c->arg[1] = 0; // use 1of256 + (*cmd) += 2; } // strip - while (**cmd==' ' || **cmd=='\t') (*cmd)++; + while (**cmd == ' ' || **cmd == '\t') (*cmd)++; - if (strstr(*cmd,"-o")==*cmd) { - req[reqlen]=ISO15693_REQ_OPTION; - (*cmd)+=2; + if (strstr(*cmd, "-o") == *cmd) { + req[reqlen] = ISO15693_REQ_OPTION; + (*cmd) += 2; } // strip - while (**cmd==' ' || **cmd=='\t') (*cmd)++; + while (**cmd == ' ' || **cmd == '\t') (*cmd)++; switch (**cmd) { case 0: @@ -559,54 +556,54 @@ int prepareHF15Cmd(char **cmd, UsbCommand *c, uint8_t iso15cmd[], int iso15cmdle case 'S': // you must have selected the tag earlier req[reqlen++] |= ISO15693_REQ_DATARATE_HIGH | ISO15693_REQ_SELECT; - memcpy(&req[reqlen],&iso15cmd[0],iso15cmdlen); + memcpy(&req[reqlen], &iso15cmd[0], iso15cmdlen); reqlen += iso15cmdlen; break; case 'u': case 'U': // unaddressed mode may not be supported by all vendors req[reqlen++] |= ISO15693_REQ_DATARATE_HIGH; - memcpy(&req[reqlen],&iso15cmd[0],iso15cmdlen); + memcpy(&req[reqlen], &iso15cmd[0], iso15cmdlen); reqlen += iso15cmdlen; break; case '*': // we scan for the UID ourself req[reqlen++] |= ISO15693_REQ_DATARATE_HIGH | ISO15693_REQ_ADDRESS; - memcpy(&req[reqlen],&iso15cmd[0],iso15cmdlen); - reqlen+=iso15cmdlen; + memcpy(&req[reqlen], &iso15cmd[0], iso15cmdlen); + reqlen += iso15cmdlen; if (!getUID(uid)) { PrintAndLog("No Tag found"); return 0; } - memcpy(req+reqlen,uid,8); + memcpy(req+reqlen ,uid, 8); PrintAndLog("Detected UID %s",sprintUID(NULL,uid)); - reqlen+=8; + reqlen += 8; break; default: req[reqlen++] |= ISO15693_REQ_DATARATE_HIGH | ISO15693_REQ_ADDRESS; - memcpy(&req[reqlen],&iso15cmd[0],iso15cmdlen); - reqlen+=iso15cmdlen; + memcpy(&req[reqlen], &iso15cmd[0], iso15cmdlen); + reqlen += iso15cmdlen; /* sscanf(cmd,"%hX%hX%hX%hX%hX%hX%hX%hX", (short unsigned int *)&uid[7],(short unsigned int *)&uid[6], (short unsigned int *)&uid[5],(short unsigned int *)&uid[4], (short unsigned int *)&uid[3],(short unsigned int *)&uid[2], (short unsigned int *)&uid[1],(short unsigned int *)&uid[0]); */ - for (int i=0;i<8 && (*cmd)[i*2] && (*cmd)[i*2+1];i++) { // parse UID + for (int i=0; i<8 && (*cmd)[i*2] && (*cmd)[i*2+1]; i++) { // parse UID sscanf((char[]){(*cmd)[i*2],(*cmd)[i*2+1],0},"%X",&temp); uid[7-i]=temp&0xff; } - PrintAndLog("Using UID %s",sprintUID(NULL,uid)); - memcpy(&req[reqlen],&uid[0],8); - reqlen+=8; + PrintAndLog("Using UID %s", sprintUID(NULL, uid)); + memcpy(&req[reqlen], &uid[0], 8); + reqlen += 8; } // skip to next space - while (**cmd!=' ' && **cmd!='\t') (*cmd)++; + while (**cmd != ' ' && **cmd != '\t') (*cmd)++; // skip over the space - while (**cmd==' ' || **cmd=='\t') (*cmd)++; + while (**cmd == ' ' || **cmd == '\t') (*cmd)++; - c->arg[0]=reqlen; + c->arg[0] = reqlen; return 1; } @@ -614,7 +611,7 @@ int prepareHF15Cmd(char **cmd, UsbCommand *c, uint8_t iso15cmd[], int iso15cmdle * Commandline handling: HF15 CMD SYSINFO * get system information from tag/VICC */ -int CmdHF15CmdSysinfo(const char *Cmd) { +static int CmdHF15CmdSysinfo(const char *Cmd) { UsbCommand resp; uint8_t *recv; UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv? @@ -650,9 +647,9 @@ int CmdHF15CmdSysinfo(const char *Cmd) { SendCommand(&c); - if (WaitForResponseTimeout(CMD_ACK,&resp,1000) && resp.arg[0]>2) { + if (WaitForResponseTimeout(CMD_ACK, &resp, 1000) && resp.arg[0] > 2) { recv = resp.d.asBytes; - if (ISO15693_CRC_CHECK==Crc(recv,resp.arg[0])) { + if (ISO15693_CRC_CHECK == Crc(recv, resp.arg[0])) { if (!(recv[0] & ISO15693_RES_ERROR)) { *output=0; // reset outputstring PrintAndLog("UID: %s", sprintUID(NULL,recv+2)); @@ -692,11 +689,12 @@ int CmdHF15CmdSysinfo(const char *Cmd) { return 0; } + /** * Commandline handling: HF15 CMD READMULTI * Read multiple blocks at once (not all tags support this) */ -int CmdHF15CmdReadmulti(const char *Cmd) { +static int CmdHF15CmdReadmulti(const char *Cmd) { UsbCommand resp; uint8_t *recv; UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv? @@ -771,11 +769,12 @@ int CmdHF15CmdReadmulti(const char *Cmd) { return 0; } + /** * Commandline handling: HF15 CMD READ * Reads a single Block */ -int CmdHF15CmdRead(const char *Cmd) { +static int CmdHF15CmdRead(const char *Cmd) { UsbCommand resp; uint8_t *recv; UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv? @@ -848,22 +847,21 @@ int CmdHF15CmdRead(const char *Cmd) { /** * Commandline handling: HF15 CMD WRITE - * Writes a single Block - might run into timeout, even when successful - */ -int CmdHF15CmdWrite(const char *Cmd) { + * Writes a single Block +**/ +static int CmdHF15CmdWrite(const char *Cmd) { UsbCommand resp; - uint8_t *recv; - UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv? - uint8_t *req=c.d.asBytes; - int reqlen=0, pagenum, temp; + UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len, speed, recv + uint8_t *req = c.d.asBytes; + int reqlen = 0, pagenum, temp; char cmdbuf[100]; - char *cmd=cmdbuf; + char *cmd = cmdbuf; char *cmd2; - strncpy(cmd,Cmd,99); + strncpy(cmd, Cmd, 99); // usage: - if (strlen(cmd)<3) { + if (strlen(cmd) < 3) { PrintAndLog("Usage: hf 15 cmd write [options] "); PrintAndLog(" options:"); PrintAndLog(" -2 use slower '1 out of 256' mode"); @@ -878,59 +876,63 @@ int CmdHF15CmdWrite(const char *Cmd) { return 0; } - prepareHF15Cmd(&cmd, &c,(uint8_t[]){ISO15693_WRITEBLOCK},1); - reqlen=c.arg[0]; + prepareHF15Cmd(&cmd, &c, (uint8_t[]){ISO15693_WRITEBLOCK}, 1); + reqlen = c.arg[0]; // *cmd -> page num ; *cmd2 -> data - cmd2=cmd; - while (*cmd2!=' ' && *cmd2!='\t' && *cmd2) cmd2++; - *cmd2=0; + cmd2 = cmd; + while (*cmd2 != ' ' && *cmd2 != '\t' && *cmd2) cmd2++; + *cmd2 = 0; cmd2++; - pagenum=strtol(cmd,NULL,0); + pagenum = strtol(cmd, NULL, 0); /*if (pagenum<0) { PrintAndLog("invalid pagenum"); return 0; } */ - req[reqlen++]=(uint8_t)pagenum; + req[reqlen++] = (uint8_t)pagenum; while (cmd2[0] && cmd2[1]) { // hexdata, read by 2 hexchars - if (*cmd2==' ') { + if (*cmd2 == ' ') { cmd2++; continue; } - sscanf((char[]){cmd2[0],cmd2[1],0},"%X",&temp); - req[reqlen++]=temp & 0xff; - cmd2+=2; + sscanf((char[]){cmd2[0], cmd2[1], 0}, "%X", &temp); + req[reqlen++] = temp & 0xff; + cmd2 += 2; } - reqlen=AddCrc(req,reqlen); - - c.arg[0]=reqlen; + reqlen = AddCrc(req, reqlen); + c.arg[0] = reqlen; SendCommand(&c); - if (WaitForResponseTimeout(CMD_ACK,&resp,2000) && resp.arg[0]>2) { - recv = resp.d.asBytes; - if (ISO15693_CRC_CHECK==Crc(recv,resp.arg[0])) { - if (!(recv[0] & ISO15693_RES_ERROR)) { - PrintAndLog("OK"); - } else { - PrintAndLog("Tag returned Error %i: %s",recv[1],TagErrorStr(recv[1])); - } + if (WaitForResponseTimeout(CMD_ACK, &resp, 2000)) { + int recv_len = resp.arg[0]; + uint8_t *recv = resp.d.asBytes; + if (recv_len == 0) { + PrintAndLog("Received SOF only. Maybe Picopass/iCLASS?"); + } else if (recv_len == -1) { + PrintAndLog("Tag didn't respond"); + } else if (recv_len == -2) { + PrintAndLog("Receive buffer overflow"); + } else if (ISO15693_CRC_CHECK != Crc(recv, resp.arg[0])) { + PrintAndLog("CRC check failed on Tag response"); + } else if (!(recv[0] & ISO15693_RES_ERROR)) { + PrintAndLog("Tag returned OK"); } else { - PrintAndLog("CRC failed"); + PrintAndLog("Tag returned Error %i: %s", recv[1], TagErrorStr(recv[1])); } } else { - PrintAndLog("timeout: no answer - data may be written anyway"); + PrintAndLog("No answer from Proxmark"); } return 0; } -int CmdHF15CSetUID(const char *Cmd) { +static int CmdHF15CSetUID(const char *Cmd) { uint8_t uid[8] = {0x00}; uint8_t oldUid[8], newUid[8] = {0x00}; @@ -969,8 +971,6 @@ int CmdHF15CSetUID(const char *Cmd) { return 0; } - PrintAndLog(""); - PrintAndLog("new UID | %s", sprint_hex(uid, 8)); PrintAndLog("Using backdoor Magic tag function"); if (!getUID(oldUid)) { @@ -978,29 +978,16 @@ int CmdHF15CSetUID(const char *Cmd) { return 1; } - UsbCommand resp; - uint8_t *recv; - char *hexout; UsbCommand c = {CMD_CSETUID_ISO_15693, {0, 0, 0}}; memcpy(c.d.asBytes, uid, 8); SendCommand(&c); + UsbCommand resp; if (WaitForResponseTimeout(CMD_ACK, &resp, 1000)) { - recv = resp.d.asBytes; int recv_len = resp.arg[0]; if (recv_len == 0) { PrintAndLog("received SOF only. Maybe Picopass/iCLASS?"); - } else if (recv_len > 0) { - PrintAndLog("received %i octets", recv_len); - hexout = (char *)malloc(resp.arg[0] * 3 + 1); - if (hexout != NULL) { - for (int i = 0; i < resp.arg[0]; i++) { // data in hex - sprintf(&hexout[i * 3], "%02X ", recv[i]); - } - PrintAndLog("%s", hexout); - free(hexout); - } } else if (recv_len == -1) { PrintAndLog("card didn't respond"); } else if (recv_len == -2) { @@ -1043,7 +1030,7 @@ static command_t CommandTable15Cmd[] = { }; -int CmdHF15Cmd(const char *Cmd) { +static int CmdHF15Cmd(const char *Cmd) { CmdsParse(CommandTable15Cmd, Cmd); return 0; } diff --git a/client/cmdhflist.c b/client/cmdhflist.c index ee21945f..61f2fa66 100644 --- a/client/cmdhflist.c +++ b/client/cmdhflist.c @@ -243,32 +243,33 @@ void annotateIclass(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize) { } -void annotateIso15693(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize) -{ - switch(cmd[1]){ - // Mandatory Commands, all Tags must support them: - case ISO15693_INVENTORY :snprintf(exp, size, "INVENTORY");return; - case ISO15693_STAYQUIET :snprintf(exp, size, "STAY_QUIET");return; - // Optional Commands, Tags may support them: - case ISO15693_READBLOCK :snprintf(exp, size, "READBLOCK");return; - case ISO15693_WRITEBLOCK :snprintf(exp, size, "WRITEBLOCK");return; - case ISO15693_LOCKBLOCK :snprintf(exp, size, "LOCKBLOCK");return; - case ISO15693_READ_MULTI_BLOCK :snprintf(exp, size, "READ_MULTI_BLOCK");return; - case ISO15693_SELECT :snprintf(exp, size, "SELECT");return; - case ISO15693_RESET_TO_READY :snprintf(exp, size, "RESET_TO_READY");return; - case ISO15693_WRITE_AFI :snprintf(exp, size, "WRITE_AFI");return; - case ISO15693_LOCK_AFI :snprintf(exp, size, "LOCK_AFI");return; - case ISO15693_WRITE_DSFID :snprintf(exp, size, "WRITE_DSFID");return; - case ISO15693_LOCK_DSFID :snprintf(exp, size, "LOCK_DSFID");return; - case ISO15693_GET_SYSTEM_INFO :snprintf(exp, size, "GET_SYSTEM_INFO");return; - case ISO15693_READ_MULTI_SECSTATUS :snprintf(exp, size, "READ_MULTI_SECSTATUS");return; - default: break; - } +void annotateIso15693(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize) { + if (cmdsize >= 2) { + switch (cmd[1]) { + // Mandatory Commands, all Tags must support them: + case ISO15693_INVENTORY :snprintf(exp, size, "INVENTORY");return; + case ISO15693_STAYQUIET :snprintf(exp, size, "STAY_QUIET");return; + // Optional Commands, Tags may support them: + case ISO15693_READBLOCK :snprintf(exp, size, "READBLOCK");return; + case ISO15693_WRITEBLOCK :snprintf(exp, size, "WRITEBLOCK");return; + case ISO15693_LOCKBLOCK :snprintf(exp, size, "LOCKBLOCK");return; + case ISO15693_READ_MULTI_BLOCK :snprintf(exp, size, "READ_MULTI_BLOCK");return; + case ISO15693_SELECT :snprintf(exp, size, "SELECT");return; + case ISO15693_RESET_TO_READY :snprintf(exp, size, "RESET_TO_READY");return; + case ISO15693_WRITE_AFI :snprintf(exp, size, "WRITE_AFI");return; + case ISO15693_LOCK_AFI :snprintf(exp, size, "LOCK_AFI");return; + case ISO15693_WRITE_DSFID :snprintf(exp, size, "WRITE_DSFID");return; + case ISO15693_LOCK_DSFID :snprintf(exp, size, "LOCK_DSFID");return; + case ISO15693_GET_SYSTEM_INFO :snprintf(exp, size, "GET_SYSTEM_INFO");return; + case ISO15693_READ_MULTI_SECSTATUS :snprintf(exp, size, "READ_MULTI_SECSTATUS");return; + default: break; + } - if (cmd[1] > ISO15693_STAYQUIET && cmd[1] < ISO15693_READBLOCK) snprintf(exp, size, "Mandatory RFU"); - else if (cmd[1] > ISO15693_READ_MULTI_SECSTATUS && cmd[1] <= 0x9F) snprintf(exp, size, "Optional RFU"); - else if ( cmd[1] >= 0xA0 && cmd[1] <= 0xDF ) snprintf(exp, size, "Custom command"); - else if ( cmd[1] >= 0xE0 && cmd[1] <= 0xFF ) snprintf(exp, size, "Proprietary command"); + if (cmd[1] > ISO15693_STAYQUIET && cmd[1] < ISO15693_READBLOCK) snprintf(exp, size, "Mandatory RFU"); + else if (cmd[1] > ISO15693_READ_MULTI_SECSTATUS && cmd[1] <= 0x9F) snprintf(exp, size, "Optional RFU"); + else if ( cmd[1] >= 0xA0 && cmd[1] <= 0xDF ) snprintf(exp, size, "Custom command"); + else if ( cmd[1] >= 0xE0 && cmd[1] <= 0xFF ) snprintf(exp, size, "Proprietary command"); + } } @@ -969,6 +970,8 @@ uint16_t printTraceLine(uint16_t tracepos, uint16_t traceLen, uint8_t *trace, ui if (data_len == 0) { if (protocol == ICLASS && duration == 2048) { sprintf(line[0], " "); + } else if (protocol == ISO_15693 && duration == 512) { + sprintf(line[0], " "); } else { sprintf(line[0], " "); } diff --git a/common/protocols.h b/common/protocols.h index 9a854363..6c46a192 100644 --- a/common/protocols.h +++ b/common/protocols.h @@ -187,6 +187,7 @@ NXP/Philips CUSTOM COMMANDS #define ISO15693_WRITEBLOCK 0x21 #define ISO15693_LOCKBLOCK 0x22 #define ISO15693_READ_MULTI_BLOCK 0x23 +#define ISO15693_WRITE_MULTI_BLOCK 0x23 #define ISO15693_SELECT 0x25 #define ISO15693_RESET_TO_READY 0x26 #define ISO15693_WRITE_AFI 0x27