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 "<EOF>"
* 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
This commit is contained in:
pwpiwi 2019-12-04 18:14:33 +01:00
commit 0046522ead
5 changed files with 216 additions and 181 deletions

View file

@ -75,6 +75,12 @@
#define DELAY_TAG_TO_ARM_SNOOP 32 #define DELAY_TAG_TO_ARM_SNOOP 32
#define DELAY_READER_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; static int DEBUG = 0;
@ -140,6 +146,15 @@ void CodeIso15693AsReader(uint8_t *cmd, int n) {
ToSendMax++; ToSendMax++;
} }
// Encode EOF only
static void CodeIso15693AsReaderEOF() {
ToSendReset();
ToSend[++ToSendMax] = 0x20;
ToSendMax++;
}
// encode data using "1 out of 256" scheme // encode data using "1 out of 256" scheme
// data rate is 1,66 kbit/s (fc/8192) // data rate is 1,66 kbit/s (fc/8192)
// is designed for more robust communication over longer distances // 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. // thing that you must send to a tag to get a response.
static void BuildIdentifyRequest(void) static void BuildIdentifyRequest(uint8_t *cmd) {
{
uint8_t cmd[5];
uint16_t crc; uint16_t crc;
// one sub-carrier, inventory, 1 slot, fast rate // one sub-carrier, inventory, 1 slot, fast rate
// AFI is at bit 5 (1<<4) when doing an INVENTORY cmd[0] = ISO15693_REQ_INVENTORY | ISO15693_REQINV_SLOT1 | ISO15693_REQ_DATARATE_HIGH;
cmd[0] = (1 << 2) | (1 << 5) | (1 << 1);
// inventory command code // inventory command code
cmd[1] = 0x01; cmd[1] = 0x01;
// no mask // no mask
@ -1123,8 +1134,6 @@ static void BuildIdentifyRequest(void)
crc = Iso15693Crc(cmd, 3); crc = Iso15693Crc(cmd, 3);
cmd[3] = crc & 0xff; cmd[3] = crc & 0xff;
cmd[4] = crc >> 8; 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 // 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. // so that it can be downloaded to a PC and processed there.
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void AcquireRawAdcSamplesIso15693(void) void AcquireRawAdcSamplesIso15693(void) {
{
LED_A_ON(); LED_A_ON();
uint8_t *dest = BigBuf_get_addr(); uint8_t *dest = BigBuf_get_addr();
@ -1145,7 +1153,9 @@ void AcquireRawAdcSamplesIso15693(void)
FpgaSetupSsc(FPGA_MAJOR_MODE_HF_READER); FpgaSetupSsc(FPGA_MAJOR_MODE_HF_READER);
SetAdcMuxFor(GPIO_MUXSEL_HIPKD); SetAdcMuxFor(GPIO_MUXSEL_HIPKD);
BuildIdentifyRequest(); uint8_t cmd[5];
BuildIdentifyRequest(cmd);
CodeIso15693AsReader(cmd, sizeof(cmd));
// Give the tags time to energize // Give the tags time to energize
SpinDelay(100); SpinDelay(100);
@ -1371,10 +1381,7 @@ void Iso15693InitReader() {
// uid is in transmission order (which is reverse of display order) // uid is in transmission order (which is reverse of display order)
static void BuildReadBlockRequest(uint8_t *uid, uint8_t blockNumber ) static void BuildReadBlockRequest(uint8_t *uid, uint8_t blockNumber, uint8_t *cmd) {
{
uint8_t cmd[13];
uint16_t crc; uint16_t crc;
// If we set the Option_Flag in this request, the VICC will respond with the security status of the block // 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 // followed by the block data
@ -1398,13 +1405,11 @@ static void BuildReadBlockRequest(uint8_t *uid, uint8_t blockNumber )
cmd[11] = crc & 0xff; cmd[11] = crc & 0xff;
cmd[12] = crc >> 8; cmd[12] = crc >> 8;
CodeIso15693AsReader(cmd, sizeof(cmd));
} }
// Now the VICC>VCD responses when we are simulating a tag // 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]; uint8_t cmd[12];
uint16_t crc; uint16_t crc;
@ -1433,7 +1438,7 @@ static void BuildInventoryResponse(uint8_t *uid)
// speed ... 0 low speed, 1 hi speed // speed ... 0 low speed, 1 hi speed
// *recv will contain the tag's answer // *recv will contain the tag's answer
// return: length of received data, or -1 for timeout // 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) { if (init) {
Iso15693InitReader(); Iso15693InitReader();
@ -1442,19 +1447,40 @@ int SendDataTag(uint8_t *send, int sendlen, bool init, int speed, uint8_t *recv,
int answerLen = 0; int answerLen = 0;
if (!speed) { if (speed_fast) {
// low speed (1 out of 256)
CodeIso15693AsReader256(send, sendlen);
} else {
// high speed (1 out of 4) // high speed (1 out of 4)
CodeIso15693AsReader(send, sendlen); CodeIso15693AsReader(send, sendlen);
} else {
// low speed (1 out of 256)
CodeIso15693AsReader256(send, sendlen);
} }
TransmitTo15693Tag(ToSend, ToSendMax, &start_time); 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 // Now wait for a response
if (recv != NULL) { 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; return answerLen;
@ -1546,7 +1572,6 @@ void ReaderIso15693(uint32_t parameter) {
set_tracing(true); set_tracing(true);
int answerLen = 0;
uint8_t TagUID[8] = {0x00}; uint8_t TagUID[8] = {0x00};
FpgaDownloadAndGo(FPGA_BITSTREAM_HF); FpgaDownloadAndGo(FPGA_BITSTREAM_HF);
@ -1572,13 +1597,11 @@ void ReaderIso15693(uint32_t parameter) {
// THIS MEANS WE CAN PRE-BUILD REQUESTS TO SAVE CPU TIME // THIS MEANS WE CAN PRE-BUILD REQUESTS TO SAVE CPU TIME
// Now send the IDENTIFY command // Now send the IDENTIFY command
BuildIdentifyRequest(); uint8_t cmd[5];
BuildIdentifyRequest(cmd);
uint32_t start_time = 0; uint32_t start_time = 0;
TransmitTo15693Tag(ToSend, ToSendMax, &start_time);
// Now wait for a response
uint32_t eof_time; 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; start_time = eof_time + DELAY_ISO15693_VICC_TO_VCD_READER;
if (answerLen >= 12) { // we should do a better check than this if (answerLen >= 12) { // we should do a better check than this
@ -1605,9 +1628,9 @@ void ReaderIso15693(uint32_t parameter) {
// read all pages // read all pages
if (answerLen >= 12 && DEBUG) { if (answerLen >= 12 && DEBUG) {
for (int i = 0; i < 32; i++) { // sanity check, assume max 32 pages for (int i = 0; i < 32; i++) { // sanity check, assume max 32 pages
BuildReadBlockRequest(TagUID, i); uint8_t cmd[13];
TransmitTo15693Tag(ToSend, ToSendMax, &start_time); BuildReadBlockRequest(TagUID, i, cmd);
int answerLen = GetIso15693AnswerFromTag(answer, sizeof(answer), DELAY_ISO15693_VCD_TO_VICC_READER * 2, &eof_time); 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; start_time = eof_time + DELAY_ISO15693_VICC_TO_VCD_READER;
if (answerLen > 0) { if (answerLen > 0) {
Dbprintf("READ SINGLE BLOCK %d returned %d octets:", i, answerLen); 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 // 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) // (some manufactures offer a way to read the AFI, though)
void BruteforceIso15693Afi(uint32_t speed) void BruteforceIso15693Afi(uint32_t speed) {
{
LED_A_ON(); LED_A_ON();
uint8_t data[6]; uint8_t data[6];
@ -1686,7 +1708,7 @@ void BruteforceIso15693Afi(uint32_t speed)
data[2] = 0; // mask length data[2] = 0; // mask length
datalen = Iso15693AddCrc(data,3); datalen = Iso15693AddCrc(data,3);
uint32_t start_time = GetCountSspClk(); 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; start_time = eof_time + DELAY_ISO15693_VICC_TO_VCD_READER;
WDT_HIT(); WDT_HIT();
if (recvlen>=12) { if (recvlen>=12) {
@ -1703,7 +1725,7 @@ void BruteforceIso15693Afi(uint32_t speed)
for (int i = 0; i < 256; i++) { for (int i = 0; i < 256; i++) {
data[2] = i & 0xFF; data[2] = i & 0xFF;
datalen = Iso15693AddCrc(data,4); 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; start_time = eof_time + DELAY_ISO15693_VICC_TO_VCD_READER;
WDT_HIT(); WDT_HIT();
if (recvlen >= 12) { if (recvlen >= 12) {
@ -1727,12 +1749,34 @@ void DirectTag15693Command(uint32_t datalen, uint32_t speed, uint32_t recv, uint
uint8_t recvbuf[ISO15693_MAX_RESPONSE_LENGTH]; uint8_t recvbuf[ISO15693_MAX_RESPONSE_LENGTH];
uint32_t eof_time; 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) { if (DEBUG) {
Dbprintf("SEND:"); Dbprintf("SEND:");
Dbhexdump(datalen, data, false); 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 // 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 // note: this prevents using hf 15 cmd with s option - which isn't implemented yet anyway
@ -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) { void SetTag15693Uid(uint8_t *uid) {
LED_A_ON(); LED_A_ON();
uint8_t cmd[4][9] = { uint8_t cmd[4][9] = {
{0x02, 0x21, 0x3e, 0x00, 0x00, 0x00, 0x00}, {ISO15693_REQ_DATARATE_HIGH, ISO15693_WRITEBLOCK, 0x3e, 0x00, 0x00, 0x00, 0x00},
{0x02, 0x21, 0x3f, 0x69, 0x96, 0x00, 0x00}, {ISO15693_REQ_DATARATE_HIGH, ISO15693_WRITEBLOCK, 0x3f, 0x69, 0x96, 0x00, 0x00},
{0x02, 0x21, 0x38}, {ISO15693_REQ_DATARATE_HIGH, ISO15693_WRITEBLOCK, 0x38},
{0x02, 0x21, 0x39} {ISO15693_REQ_DATARATE_HIGH, ISO15693_WRITEBLOCK, 0x39}
}; };
uint16_t crc; uint16_t crc;
@ -1791,20 +1835,19 @@ void SetTag15693Uid(uint8_t *uid) {
cmd[3][5] = uid[1]; cmd[3][5] = uid[1];
cmd[3][6] = uid[0]; cmd[3][6] = uid[0];
uint32_t start_time = 0;
for (int i = 0; i < 4; i++) { for (int i = 0; i < 4; i++) {
// Add the CRC // Add the CRC
crc = Iso15693Crc(cmd[i], 7); crc = Iso15693Crc(cmd[i], 7);
cmd[i][7] = crc & 0xff; cmd[i][7] = crc & 0xff;
cmd[i][8] = crc >> 8; 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) { if (DEBUG) {
Dbprintf("SEND:"); Dbprintf("SEND:");
Dbhexdump(sizeof(cmd[i]), cmd[i], false); Dbhexdump(sizeof(cmd[i]), cmd[i], false);
}
if (DEBUG) {
Dbprintf("RECV:"); Dbprintf("RECV:");
if (recvlen > 0) { if (recvlen > 0) {
Dbhexdump(recvlen, recvbuf, false); Dbhexdump(recvlen, recvbuf, false);
@ -1812,9 +1855,12 @@ void SetTag15693Uid(uint8_t *uid) {
} }
} }
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(); LED_A_OFF();
} }

View file

@ -21,8 +21,6 @@
//SSP_CLK runs at 13.56MHz / 4 = 3,39MHz when acting as reader. All values should be multiples of 16 //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_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 #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 Iso15693InitReader();
void CodeIso15693AsReader(uint8_t *cmd, int n); void CodeIso15693AsReader(uint8_t *cmd, int n);

View file

@ -137,8 +137,7 @@ static char* TagErrorStr(uint8_t error) {
// Mode 3 // 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 // The sampling rate is 106.353 ksps/s, for T = 18.8 us
int i, j; int i, j;
@ -222,25 +221,23 @@ int CmdHF15Demod(const char *Cmd)
} }
// * Acquire Samples as Reader (enables carrier, sends inquiry) // * 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}; UsbCommand c = {CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_15693};
SendCommand(&c); SendCommand(&c);
return 0; return 0;
} }
// Record Activity without enabling carrier // Record Activity without enabling carrier
int CmdHF15Snoop(const char *Cmd) static int CmdHF15Snoop(const char *Cmd) {
{
UsbCommand c = {CMD_SNOOP_ISO_15693}; UsbCommand c = {CMD_SNOOP_ISO_15693};
SendCommand(&c); SendCommand(&c);
return 0; return 0;
} }
int HF15Reader(const char *Cmd, bool verbose)
{ int HF15Reader(const char *Cmd, bool verbose) {
uint8_t uid[8]; uint8_t uid[8];
if (!getUID(uid)) { if (!getUID(uid)) {
@ -254,16 +251,16 @@ int HF15Reader(const char *Cmd, bool verbose)
return 1; 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}}; UsbCommand c = {CMD_READER_ISO_15693, {strtol(Cmd, NULL, 0), 0, 0}};
SendCommand(&c); SendCommand(&c);
return 0; return 0;
} }
// Simulation is still not working very good // Simulation is still not working very good
int CmdHF15Sim(const char *Cmd) static int CmdHF15Sim(const char *Cmd) {
{
char cmdp = param_getchar(Cmd, 0); char cmdp = param_getchar(Cmd, 0);
uint8_t uid[8] = {0x00}; uint8_t uid[8] = {0x00};
@ -291,17 +288,18 @@ int CmdHF15Sim(const char *Cmd)
return 0; return 0;
} }
// finds the AFI (Application Family Idendifier) of a card, by trying all values // 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) // (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}}; UsbCommand c = {CMD_ISO_15693_FIND_AFI, {strtol(Cmd, NULL, 0), 0, 0}};
SendCommand(&c); SendCommand(&c);
return 0; return 0;
} }
// Reads all memory pages // Reads all memory pages
int CmdHF15DumpMem(const char*Cmd) { static int CmdHF15DumpMem(const char*Cmd) {
UsbCommand resp; UsbCommand resp;
uint8_t uid[8]; uint8_t uid[8];
uint8_t *recv=NULL; 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; UsbCommand resp;
uint8_t *recv; uint8_t *recv;
UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,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) // Turns debugging on(1)/off(0)
int CmdHF15CmdDebug( const char *cmd) { static int CmdHF15CmdDebug( const char *cmd) {
int debug = atoi(cmd); int debug = atoi(cmd);
if (strlen(cmd) < 1) { if (strlen(cmd) < 1) {
PrintAndLog("Usage: hf 15 debug <0|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; UsbCommand resp;
uint8_t *recv; uint8_t *recv;
UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv? UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv?
@ -525,30 +522,30 @@ int CmdHF15CmdRaw (const char *cmd) {
* Parameters: * Parameters:
* **cmd command line * **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; int temp;
uint8_t *req=c->d.asBytes; uint8_t *req = c->d.asBytes;
uint8_t uid[8] = {0x00}; uint8_t uid[8] = {0x00};
uint32_t reqlen=0; uint32_t reqlen = 0;
// strip // strip
while (**cmd==' ' || **cmd=='\t') (*cmd)++; while (**cmd==' ' || **cmd=='\t') (*cmd)++;
if (strstr(*cmd,"-2")==*cmd) { if (strstr(*cmd, "-2") == *cmd) {
c->arg[1]=0; // use 1of256 c->arg[1] = 0; // use 1of256
(*cmd)+=2; (*cmd) += 2;
} }
// strip // strip
while (**cmd==' ' || **cmd=='\t') (*cmd)++; while (**cmd == ' ' || **cmd == '\t') (*cmd)++;
if (strstr(*cmd,"-o")==*cmd) { if (strstr(*cmd, "-o") == *cmd) {
req[reqlen]=ISO15693_REQ_OPTION; req[reqlen] = ISO15693_REQ_OPTION;
(*cmd)+=2; (*cmd) += 2;
} }
// strip // strip
while (**cmd==' ' || **cmd=='\t') (*cmd)++; while (**cmd == ' ' || **cmd == '\t') (*cmd)++;
switch (**cmd) { switch (**cmd) {
case 0: case 0:
@ -559,54 +556,54 @@ int prepareHF15Cmd(char **cmd, UsbCommand *c, uint8_t iso15cmd[], int iso15cmdle
case 'S': case 'S':
// you must have selected the tag earlier // you must have selected the tag earlier
req[reqlen++] |= ISO15693_REQ_DATARATE_HIGH | ISO15693_REQ_SELECT; req[reqlen++] |= ISO15693_REQ_DATARATE_HIGH | ISO15693_REQ_SELECT;
memcpy(&req[reqlen],&iso15cmd[0],iso15cmdlen); memcpy(&req[reqlen], &iso15cmd[0], iso15cmdlen);
reqlen += iso15cmdlen; reqlen += iso15cmdlen;
break; break;
case 'u': case 'u':
case 'U': case 'U':
// unaddressed mode may not be supported by all vendors // unaddressed mode may not be supported by all vendors
req[reqlen++] |= ISO15693_REQ_DATARATE_HIGH; req[reqlen++] |= ISO15693_REQ_DATARATE_HIGH;
memcpy(&req[reqlen],&iso15cmd[0],iso15cmdlen); memcpy(&req[reqlen], &iso15cmd[0], iso15cmdlen);
reqlen += iso15cmdlen; reqlen += iso15cmdlen;
break; break;
case '*': case '*':
// we scan for the UID ourself // we scan for the UID ourself
req[reqlen++] |= ISO15693_REQ_DATARATE_HIGH | ISO15693_REQ_ADDRESS; req[reqlen++] |= ISO15693_REQ_DATARATE_HIGH | ISO15693_REQ_ADDRESS;
memcpy(&req[reqlen],&iso15cmd[0],iso15cmdlen); memcpy(&req[reqlen], &iso15cmd[0], iso15cmdlen);
reqlen+=iso15cmdlen; reqlen += iso15cmdlen;
if (!getUID(uid)) { if (!getUID(uid)) {
PrintAndLog("No Tag found"); PrintAndLog("No Tag found");
return 0; return 0;
} }
memcpy(req+reqlen,uid,8); memcpy(req+reqlen ,uid, 8);
PrintAndLog("Detected UID %s",sprintUID(NULL,uid)); PrintAndLog("Detected UID %s",sprintUID(NULL,uid));
reqlen+=8; reqlen += 8;
break; break;
default: default:
req[reqlen++] |= ISO15693_REQ_DATARATE_HIGH | ISO15693_REQ_ADDRESS; req[reqlen++] |= ISO15693_REQ_DATARATE_HIGH | ISO15693_REQ_ADDRESS;
memcpy(&req[reqlen],&iso15cmd[0],iso15cmdlen); memcpy(&req[reqlen], &iso15cmd[0], iso15cmdlen);
reqlen+=iso15cmdlen; reqlen += iso15cmdlen;
/* sscanf(cmd,"%hX%hX%hX%hX%hX%hX%hX%hX", /* sscanf(cmd,"%hX%hX%hX%hX%hX%hX%hX%hX",
(short unsigned int *)&uid[7],(short unsigned int *)&uid[6], (short unsigned int *)&uid[7],(short unsigned int *)&uid[6],
(short unsigned int *)&uid[5],(short unsigned int *)&uid[4], (short unsigned int *)&uid[5],(short unsigned int *)&uid[4],
(short unsigned int *)&uid[3],(short unsigned int *)&uid[2], (short unsigned int *)&uid[3],(short unsigned int *)&uid[2],
(short unsigned int *)&uid[1],(short unsigned int *)&uid[0]); */ (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); sscanf((char[]){(*cmd)[i*2],(*cmd)[i*2+1],0},"%X",&temp);
uid[7-i]=temp&0xff; uid[7-i]=temp&0xff;
} }
PrintAndLog("Using UID %s",sprintUID(NULL,uid)); PrintAndLog("Using UID %s", sprintUID(NULL, uid));
memcpy(&req[reqlen],&uid[0],8); memcpy(&req[reqlen], &uid[0], 8);
reqlen+=8; reqlen += 8;
} }
// skip to next space // skip to next space
while (**cmd!=' ' && **cmd!='\t') (*cmd)++; while (**cmd != ' ' && **cmd != '\t') (*cmd)++;
// skip over the space // skip over the space
while (**cmd==' ' || **cmd=='\t') (*cmd)++; while (**cmd == ' ' || **cmd == '\t') (*cmd)++;
c->arg[0]=reqlen; c->arg[0] = reqlen;
return 1; return 1;
} }
@ -614,7 +611,7 @@ int prepareHF15Cmd(char **cmd, UsbCommand *c, uint8_t iso15cmd[], int iso15cmdle
* Commandline handling: HF15 CMD SYSINFO * Commandline handling: HF15 CMD SYSINFO
* get system information from tag/VICC * get system information from tag/VICC
*/ */
int CmdHF15CmdSysinfo(const char *Cmd) { static int CmdHF15CmdSysinfo(const char *Cmd) {
UsbCommand resp; UsbCommand resp;
uint8_t *recv; uint8_t *recv;
UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv? UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv?
@ -650,9 +647,9 @@ int CmdHF15CmdSysinfo(const char *Cmd) {
SendCommand(&c); 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; 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)) { if (!(recv[0] & ISO15693_RES_ERROR)) {
*output=0; // reset outputstring *output=0; // reset outputstring
PrintAndLog("UID: %s", sprintUID(NULL,recv+2)); PrintAndLog("UID: %s", sprintUID(NULL,recv+2));
@ -692,11 +689,12 @@ int CmdHF15CmdSysinfo(const char *Cmd) {
return 0; return 0;
} }
/** /**
* Commandline handling: HF15 CMD READMULTI * Commandline handling: HF15 CMD READMULTI
* Read multiple blocks at once (not all tags support this) * Read multiple blocks at once (not all tags support this)
*/ */
int CmdHF15CmdReadmulti(const char *Cmd) { static int CmdHF15CmdReadmulti(const char *Cmd) {
UsbCommand resp; UsbCommand resp;
uint8_t *recv; uint8_t *recv;
UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv? UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv?
@ -771,11 +769,12 @@ int CmdHF15CmdReadmulti(const char *Cmd) {
return 0; return 0;
} }
/** /**
* Commandline handling: HF15 CMD READ * Commandline handling: HF15 CMD READ
* Reads a single Block * Reads a single Block
*/ */
int CmdHF15CmdRead(const char *Cmd) { static int CmdHF15CmdRead(const char *Cmd) {
UsbCommand resp; UsbCommand resp;
uint8_t *recv; uint8_t *recv;
UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,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 * Commandline handling: HF15 CMD WRITE
* Writes a single Block - might run into timeout, even when successful * Writes a single Block
*/ **/
int CmdHF15CmdWrite(const char *Cmd) { static int CmdHF15CmdWrite(const char *Cmd) {
UsbCommand resp; UsbCommand resp;
uint8_t *recv; UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len, speed, recv
UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv? uint8_t *req = c.d.asBytes;
uint8_t *req=c.d.asBytes; int reqlen = 0, pagenum, temp;
int reqlen=0, pagenum, temp;
char cmdbuf[100]; char cmdbuf[100];
char *cmd=cmdbuf; char *cmd = cmdbuf;
char *cmd2; char *cmd2;
strncpy(cmd,Cmd,99); strncpy(cmd, Cmd, 99);
// usage: // usage:
if (strlen(cmd)<3) { if (strlen(cmd) < 3) {
PrintAndLog("Usage: hf 15 cmd write [options] <uid|s|u|*> <page#> <hexdata>"); PrintAndLog("Usage: hf 15 cmd write [options] <uid|s|u|*> <page#> <hexdata>");
PrintAndLog(" options:"); PrintAndLog(" options:");
PrintAndLog(" -2 use slower '1 out of 256' mode"); PrintAndLog(" -2 use slower '1 out of 256' mode");
@ -878,59 +876,63 @@ int CmdHF15CmdWrite(const char *Cmd) {
return 0; return 0;
} }
prepareHF15Cmd(&cmd, &c,(uint8_t[]){ISO15693_WRITEBLOCK},1); prepareHF15Cmd(&cmd, &c, (uint8_t[]){ISO15693_WRITEBLOCK}, 1);
reqlen=c.arg[0]; reqlen = c.arg[0];
// *cmd -> page num ; *cmd2 -> data // *cmd -> page num ; *cmd2 -> data
cmd2=cmd; cmd2 = cmd;
while (*cmd2!=' ' && *cmd2!='\t' && *cmd2) cmd2++; while (*cmd2 != ' ' && *cmd2 != '\t' && *cmd2) cmd2++;
*cmd2=0; *cmd2 = 0;
cmd2++; cmd2++;
pagenum=strtol(cmd,NULL,0); pagenum = strtol(cmd, NULL, 0);
/*if (pagenum<0) { /*if (pagenum<0) {
PrintAndLog("invalid pagenum"); PrintAndLog("invalid pagenum");
return 0; return 0;
} */ } */
req[reqlen++]=(uint8_t)pagenum; req[reqlen++] = (uint8_t)pagenum;
while (cmd2[0] && cmd2[1]) { // hexdata, read by 2 hexchars while (cmd2[0] && cmd2[1]) { // hexdata, read by 2 hexchars
if (*cmd2==' ') { if (*cmd2 == ' ') {
cmd2++; cmd2++;
continue; continue;
} }
sscanf((char[]){cmd2[0],cmd2[1],0},"%X",&temp); sscanf((char[]){cmd2[0], cmd2[1], 0}, "%X", &temp);
req[reqlen++]=temp & 0xff; req[reqlen++] = temp & 0xff;
cmd2+=2; cmd2 += 2;
} }
reqlen=AddCrc(req,reqlen); reqlen = AddCrc(req, reqlen);
c.arg[0] = reqlen;
c.arg[0]=reqlen;
SendCommand(&c); SendCommand(&c);
if (WaitForResponseTimeout(CMD_ACK,&resp,2000) && resp.arg[0]>2) { if (WaitForResponseTimeout(CMD_ACK, &resp, 2000)) {
recv = resp.d.asBytes; int recv_len = resp.arg[0];
if (ISO15693_CRC_CHECK==Crc(recv,resp.arg[0])) { uint8_t *recv = resp.d.asBytes;
if (!(recv[0] & ISO15693_RES_ERROR)) { if (recv_len == 0) {
PrintAndLog("OK"); 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 { } else {
PrintAndLog("Tag returned Error %i: %s",recv[1],TagErrorStr(recv[1])); PrintAndLog("Tag returned Error %i: %s", recv[1], TagErrorStr(recv[1]));
} }
} else { } else {
PrintAndLog("CRC failed"); PrintAndLog("No answer from Proxmark");
}
} else {
PrintAndLog("timeout: no answer - data may be written anyway");
} }
return 0; return 0;
} }
int CmdHF15CSetUID(const char *Cmd) { static int CmdHF15CSetUID(const char *Cmd) {
uint8_t uid[8] = {0x00}; uint8_t uid[8] = {0x00};
uint8_t oldUid[8], newUid[8] = {0x00}; uint8_t oldUid[8], newUid[8] = {0x00};
@ -969,8 +971,6 @@ int CmdHF15CSetUID(const char *Cmd) {
return 0; return 0;
} }
PrintAndLog("");
PrintAndLog("new UID | %s", sprint_hex(uid, 8));
PrintAndLog("Using backdoor Magic tag function"); PrintAndLog("Using backdoor Magic tag function");
if (!getUID(oldUid)) { if (!getUID(oldUid)) {
@ -978,29 +978,16 @@ int CmdHF15CSetUID(const char *Cmd) {
return 1; return 1;
} }
UsbCommand resp;
uint8_t *recv;
char *hexout;
UsbCommand c = {CMD_CSETUID_ISO_15693, {0, 0, 0}}; UsbCommand c = {CMD_CSETUID_ISO_15693, {0, 0, 0}};
memcpy(c.d.asBytes, uid, 8); memcpy(c.d.asBytes, uid, 8);
SendCommand(&c); SendCommand(&c);
UsbCommand resp;
if (WaitForResponseTimeout(CMD_ACK, &resp, 1000)) { if (WaitForResponseTimeout(CMD_ACK, &resp, 1000)) {
recv = resp.d.asBytes;
int recv_len = resp.arg[0]; int recv_len = resp.arg[0];
if (recv_len == 0) { if (recv_len == 0) {
PrintAndLog("received SOF only. Maybe Picopass/iCLASS?"); 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) { } else if (recv_len == -1) {
PrintAndLog("card didn't respond"); PrintAndLog("card didn't respond");
} else if (recv_len == -2) { } 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); CmdsParse(CommandTable15Cmd, Cmd);
return 0; return 0;
} }

View file

@ -243,9 +243,9 @@ 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) void annotateIso15693(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize) {
{ if (cmdsize >= 2) {
switch(cmd[1]){ switch (cmd[1]) {
// Mandatory Commands, all Tags must support them: // Mandatory Commands, all Tags must support them:
case ISO15693_INVENTORY :snprintf(exp, size, "INVENTORY");return; case ISO15693_INVENTORY :snprintf(exp, size, "INVENTORY");return;
case ISO15693_STAYQUIET :snprintf(exp, size, "STAY_QUIET");return; case ISO15693_STAYQUIET :snprintf(exp, size, "STAY_QUIET");return;
@ -269,6 +269,7 @@ void annotateIso15693(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize)
else if (cmd[1] > ISO15693_READ_MULTI_SECSTATUS && cmd[1] <= 0x9F) snprintf(exp, size, "Optional 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] >= 0xA0 && cmd[1] <= 0xDF ) snprintf(exp, size, "Custom command");
else if ( cmd[1] >= 0xE0 && cmd[1] <= 0xFF ) snprintf(exp, size, "Proprietary 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 (data_len == 0) {
if (protocol == ICLASS && duration == 2048) { if (protocol == ICLASS && duration == 2048) {
sprintf(line[0], " <SOF>"); sprintf(line[0], " <SOF>");
} else if (protocol == ISO_15693 && duration == 512) {
sprintf(line[0], " <EOF>");
} else { } else {
sprintf(line[0], " <empty trace - possible error>"); sprintf(line[0], " <empty trace - possible error>");
} }

View file

@ -187,6 +187,7 @@ NXP/Philips CUSTOM COMMANDS
#define ISO15693_WRITEBLOCK 0x21 #define ISO15693_WRITEBLOCK 0x21
#define ISO15693_LOCKBLOCK 0x22 #define ISO15693_LOCKBLOCK 0x22
#define ISO15693_READ_MULTI_BLOCK 0x23 #define ISO15693_READ_MULTI_BLOCK 0x23
#define ISO15693_WRITE_MULTI_BLOCK 0x23
#define ISO15693_SELECT 0x25 #define ISO15693_SELECT 0x25
#define ISO15693_RESET_TO_READY 0x26 #define ISO15693_RESET_TO_READY 0x26
#define ISO15693_WRITE_AFI 0x27 #define ISO15693_WRITE_AFI 0x27