+ FpgaDisableSscDma();
+
+ if (DEBUG) Dbprintf("samples = %d, gotFrame = %d, Decoder: state = %d, len = %d, bitCount = %d, posCount = %d",
+ samples, gotFrame, DecodeReader.state, DecodeReader.byteCount, DecodeReader.bitCount, DecodeReader.posCount);
+
+ if (DecodeReader.byteCount > 0) {
+ uint32_t sof_time = *eof_time
+ - DecodeReader.byteCount * (DecodeReader.Coding==CODING_1_OUT_OF_4?128:2048) // time for byte transfers
+ - 32 // time for SOF transfer
+ - 16; // time for EOF transfer
+ LogTrace_ISO15693(DecodeReader.output, DecodeReader.byteCount, sof_time*32, *eof_time*32, NULL, true);
+ }
+
+ return DecodeReader.byteCount;
+}
+
+
+// Construct an identify (Inventory) request, which is the first
+// thing that you must send to a tag to get a response.
+static void BuildIdentifyRequest(uint8_t *cmd) {
+ uint16_t crc;
+ // one sub-carrier, inventory, 1 slot, fast rate
+ cmd[0] = ISO15693_REQ_INVENTORY | ISO15693_REQINV_SLOT1 | ISO15693_REQ_DATARATE_HIGH;
+ // inventory command code
+ cmd[1] = 0x01;
+ // no mask
+ cmd[2] = 0x00;
+ //Now the CRC
+ crc = Iso15693Crc(cmd, 3);
+ cmd[3] = crc & 0xff;
+ cmd[4] = crc >> 8;
+}
+
+
+//-----------------------------------------------------------------------------
+// Start to read an ISO 15693 tag. We send an identify request, then wait
+// 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) {
+ LED_A_ON();
+
+ uint8_t *dest = BigBuf_get_addr();
+
+ FpgaDownloadAndGo(FPGA_BITSTREAM_HF);
+ FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER);
+ LED_D_ON();
+ FpgaSetupSsc(FPGA_MAJOR_MODE_HF_READER);
+ SetAdcMuxFor(GPIO_MUXSEL_HIPKD);
+
+ uint8_t cmd[5];
+ BuildIdentifyRequest(cmd);
+ CodeIso15693AsReader(cmd, sizeof(cmd));
+
+ // Give the tags time to energize
+ SpinDelay(100);
+
+ // Now send the command
+ uint32_t start_time = 0;
+ TransmitTo15693Tag(ToSend, ToSendMax, &start_time);
+
+ // wait for last transfer to complete
+ while (!(AT91C_BASE_SSC->SSC_SR & AT91C_SSC_TXEMPTY)) ;
+
+ FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER | FPGA_HF_READER_SUBCARRIER_424_KHZ | FPGA_HF_READER_MODE_RECEIVE_AMPLITUDE);
+
+ for(int c = 0; c < 4000; ) {
+ if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) {
+ uint16_t r = AT91C_BASE_SSC->SSC_RHR;
+ dest[c++] = r >> 5;
+ }
+ }
+
+ FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
+ LEDsoff();
+}
+
+
+void SnoopIso15693(uint8_t jam_search_len, uint8_t *jam_search_string) {
+
+ LED_A_ON();
+
+ FpgaDownloadAndGo(FPGA_BITSTREAM_HF);
+
+ clear_trace();
+ set_tracing(true);
+
+ // The DMA buffer, used to stream samples from the FPGA
+ uint16_t dmaBuf[ISO15693_DMA_BUFFER_SIZE];
+
+ // Count of samples received so far, so that we can include timing
+ // information in the trace buffer.
+ int samples = 0;
+
+ DecodeTag_t DecodeTag = {0};
+ uint8_t response[ISO15693_MAX_RESPONSE_LENGTH];
+ DecodeTagInit(&DecodeTag, response, sizeof(response));
+
+ DecodeReader_t DecodeReader = {0};
+ uint8_t cmd[ISO15693_MAX_COMMAND_LENGTH];
+ DecodeReaderInit(&DecodeReader, cmd, sizeof(cmd), jam_search_len, jam_search_string);
+
+ // Print some debug information about the buffer sizes
+ if (DEBUG) {
+ Dbprintf("Snooping buffers initialized:");
+ Dbprintf(" Trace: %i bytes", BigBuf_max_traceLen());
+ Dbprintf(" Reader -> tag: %i bytes", ISO15693_MAX_COMMAND_LENGTH);
+ Dbprintf(" tag -> Reader: %i bytes", ISO15693_MAX_RESPONSE_LENGTH);
+ Dbprintf(" DMA: %i bytes", ISO15693_DMA_BUFFER_SIZE * sizeof(uint16_t));
+ }
+ Dbprintf("Snoop started. Press PM3 Button to stop.");
+
+ FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER | FPGA_HF_READER_MODE_SNOOP_AMPLITUDE);
+ LED_D_OFF();
+ SetAdcMuxFor(GPIO_MUXSEL_HIPKD);
+ FpgaSetupSsc(FPGA_MAJOR_MODE_HF_READER);
+ StartCountSspClk();
+ FpgaSetupSscDma((uint8_t*) dmaBuf, ISO15693_DMA_BUFFER_SIZE);
+
+ bool TagIsActive = false;
+ bool ReaderIsActive = false;
+ bool ExpectTagAnswer = false;
+ uint32_t dma_start_time = 0;
+ uint16_t *upTo = dmaBuf;
+
+ uint16_t max_behindBy = 0;
+
+ // And now we loop, receiving samples.
+ for(;;) {
+ uint16_t behindBy = ((uint16_t*)AT91C_BASE_PDC_SSC->PDC_RPR - upTo) & (ISO15693_DMA_BUFFER_SIZE-1);
+ if (behindBy > max_behindBy) {
+ max_behindBy = behindBy;
+ }
+
+ if (behindBy == 0) continue;
+
+ samples++;
+ if (samples == 1) {
+ // DMA has transferred the very first data
+ dma_start_time = GetCountSspClk() & 0xfffffff0;
+ }
+
+ uint16_t snoopdata = *upTo++;
+
+ if (upTo >= dmaBuf + ISO15693_DMA_BUFFER_SIZE) { // we have read all of the DMA buffer content.
+ upTo = dmaBuf; // start reading the circular buffer from the beginning
+ if (behindBy > (9*ISO15693_DMA_BUFFER_SIZE/10)) {
+ // FpgaDisableTracing();
+ Dbprintf("About to blow circular buffer - aborted! behindBy=%d, samples=%d", behindBy, samples);
+ break;
+ }
+ if (AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_ENDRX)) { // DMA Counter Register had reached 0, already rotated.
+ AT91C_BASE_PDC_SSC->PDC_RNPR = (uint32_t) dmaBuf; // refresh the DMA Next Buffer and
+ AT91C_BASE_PDC_SSC->PDC_RNCR = ISO15693_DMA_BUFFER_SIZE; // DMA Next Counter registers
+ WDT_HIT();
+ if (BUTTON_PRESS()) {
+ DbpString("Snoop stopped.");
+ break;
+ }
+ }
+ }
+
+ if (!TagIsActive) { // no need to try decoding reader data if the tag is sending
+ if (Handle15693SampleFromReader(snoopdata & 0x02, &DecodeReader)) {
+ // FpgaDisableSscDma();
+ uint32_t eof_time = dma_start_time + samples*16 + 8 - DELAY_READER_TO_ARM_SNOOP; // end of EOF
+ if (DecodeReader.byteCount > 0) {
+ uint32_t sof_time = eof_time
+ - DecodeReader.byteCount * (DecodeReader.Coding==CODING_1_OUT_OF_4?128*16:2048*16) // time for byte transfers
+ - 32*16 // time for SOF transfer
+ - 16*16; // time for EOF transfer
+ LogTrace_ISO15693(DecodeReader.output, DecodeReader.byteCount, sof_time*4, eof_time*4, NULL, true);
+ }
+ /* And ready to receive another command. */
+ DecodeReaderReset(&DecodeReader);
+ /* And also reset the demod code, which might have been */
+ /* false-triggered by the commands from the reader. */
+ DecodeTagReset(&DecodeTag);
+ ReaderIsActive = false;
+ ExpectTagAnswer = true;
+ // upTo = dmaBuf;
+ // samples = 0;
+ // FpgaSetupSscDma((uint8_t*) dmaBuf, ISO15693_DMA_BUFFER_SIZE);
+ // continue;
+ } else if (Handle15693SampleFromReader(snoopdata & 0x01, &DecodeReader)) {
+ // FpgaDisableSscDma();
+ uint32_t eof_time = dma_start_time + samples*16 + 16 - DELAY_READER_TO_ARM_SNOOP; // end of EOF
+ if (DecodeReader.byteCount > 0) {
+ uint32_t sof_time = eof_time
+ - DecodeReader.byteCount * (DecodeReader.Coding==CODING_1_OUT_OF_4?128*16:2048*16) // time for byte transfers
+ - 32*16 // time for SOF transfer
+ - 16*16; // time for EOF transfer
+ LogTrace_ISO15693(DecodeReader.output, DecodeReader.byteCount, sof_time*4, eof_time*4, NULL, true);
+ }
+ /* And ready to receive another command. */
+ DecodeReaderReset(&DecodeReader);
+ /* And also reset the demod code, which might have been */
+ /* false-triggered by the commands from the reader. */
+ DecodeTagReset(&DecodeTag);
+ ReaderIsActive = false;
+ ExpectTagAnswer = true;
+ // upTo = dmaBuf;
+ // samples = 0;
+ // FpgaSetupSscDma((uint8_t*) dmaBuf, ISO15693_DMA_BUFFER_SIZE);
+ // continue;
+ } else {
+ ReaderIsActive = (DecodeReader.state >= STATE_READER_RECEIVE_DATA_1_OUT_OF_4);
+ }
+ }
+
+ if (!ReaderIsActive && ExpectTagAnswer) { // no need to try decoding tag data if the reader is currently sending or no answer expected yet
+ if (Handle15693SamplesFromTag(snoopdata >> 2, &DecodeTag)) {
+ // FpgaDisableSscDma();
+ uint32_t eof_time = dma_start_time + samples*16 - DELAY_TAG_TO_ARM_SNOOP; // end of EOF
+ if (DecodeTag.lastBit == SOF_PART2) {
+ eof_time -= 8*16; // needed 8 additional samples to confirm single SOF (iCLASS)
+ }
+ uint32_t sof_time = eof_time
+ - DecodeTag.len * 8 * 8 * 16 // time for byte transfers
+ - 32 * 16 // time for SOF transfer
+ - (DecodeTag.lastBit != SOF_PART2?32*16:0); // time for EOF transfer
+ LogTrace_ISO15693(DecodeTag.output, DecodeTag.len, sof_time*4, eof_time*4, NULL, false);
+ // And ready to receive another response.
+ DecodeTagReset(&DecodeTag);
+ DecodeReaderReset(&DecodeReader);
+ ExpectTagAnswer = false;
+ TagIsActive = false;
+ // upTo = dmaBuf;
+ // samples = 0;
+ // FpgaSetupSscDma((uint8_t*) dmaBuf, ISO15693_DMA_BUFFER_SIZE);
+ // continue;
+ } else {
+ TagIsActive = (DecodeTag.state >= STATE_TAG_RECEIVING_DATA);
+ }
+ }
+
+ }
+
+ FpgaDisableSscDma();
+
+ DbpString("Snoop statistics:");
+ Dbprintf(" ExpectTagAnswer: %d, TagIsActive: %d, ReaderIsActive: %d", ExpectTagAnswer, TagIsActive, ReaderIsActive);
+ Dbprintf(" DecodeTag State: %d", DecodeTag.state);
+ Dbprintf(" DecodeTag byteCnt: %d", DecodeTag.len);
+ Dbprintf(" DecodeTag posCount: %d", DecodeTag.posCount);
+ Dbprintf(" DecodeReader State: %d", DecodeReader.state);
+ Dbprintf(" DecodeReader byteCnt: %d", DecodeReader.byteCount);
+ Dbprintf(" DecodeReader posCount: %d", DecodeReader.posCount);
+ Dbprintf(" Trace length: %d", BigBuf_get_traceLen());
+ Dbprintf(" Max behindBy: %d", max_behindBy);
+}
+
+
+// Initialize the proxmark as iso15k reader
+void Iso15693InitReader() {
+ FpgaDownloadAndGo(FPGA_BITSTREAM_HF);
+
+ // Start from off (no field generated)
+ LED_D_OFF();
+ FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
+ SpinDelay(10);
+
+ SetAdcMuxFor(GPIO_MUXSEL_HIPKD);
+ FpgaSetupSsc(FPGA_MAJOR_MODE_HF_READER);
+
+ // Give the tags time to energize
+ LED_D_ON();
+ FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER);
+ SpinDelay(250);
+}
+
+///////////////////////////////////////////////////////////////////////
+// ISO 15693 Part 3 - Air Interface
+// This section basically contains transmission and receiving of bits
+///////////////////////////////////////////////////////////////////////
+
+
+// uid is in transmission order (which is reverse of display order)
+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
+ cmd[0] = ISO15693_REQ_OPTION | ISO15693_REQ_ADDRESS | ISO15693_REQ_DATARATE_HIGH;
+ // READ BLOCK command code
+ cmd[1] = ISO15693_READBLOCK;
+ // UID may be optionally specified here
+ // 64-bit UID
+ cmd[2] = uid[0];
+ cmd[3] = uid[1];
+ cmd[4] = uid[2];
+ cmd[5] = uid[3];
+ cmd[6] = uid[4];
+ cmd[7] = uid[5];
+ cmd[8] = uid[6];
+ cmd[9] = uid[7]; // 0xe0; // always e0 (not exactly unique)
+ // Block number to read
+ cmd[10] = blockNumber;
+ //Now the CRC
+ crc = Iso15693Crc(cmd, 11); // the crc needs to be calculated over 11 bytes
+ cmd[11] = crc & 0xff;
+ cmd[12] = crc >> 8;
+
+}
+
+
+// Now the VICC>VCD responses when we are simulating a tag
+static void BuildInventoryResponse(uint8_t *uid) {
+ uint8_t cmd[12];
+
+ uint16_t crc;
+
+ cmd[0] = 0; // No error, no protocol format extension
+ cmd[1] = 0; // DSFID (data storage format identifier). 0x00 = not supported
+ // 64-bit UID
+ cmd[2] = uid[7]; //0x32;
+ cmd[3] = uid[6]; //0x4b;
+ cmd[4] = uid[5]; //0x03;
+ cmd[5] = uid[4]; //0x01;
+ cmd[6] = uid[3]; //0x00;
+ cmd[7] = uid[2]; //0x10;
+ cmd[8] = uid[1]; //0x05;
+ cmd[9] = uid[0]; //0xe0;
+ //Now the CRC
+ crc = Iso15693Crc(cmd, 10);
+ cmd[10] = crc & 0xff;
+ cmd[11] = crc >> 8;
+
+ CodeIso15693AsTag(cmd, sizeof(cmd));
+}
+
+// Universal Method for sending to and recv bytes from a tag
+// init ... should we initialize the reader?
+// 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, 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();
+ StartCountSspClk();
+ }
+
+ int answerLen = 0;
+
+ 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, 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;
+}
+
+
+// --------------------------------------------------------------------
+// Debug Functions
+// --------------------------------------------------------------------
+
+// Decodes a message from a tag and displays its metadata and content
+#define DBD15STATLEN 48
+void DbdecodeIso15693Answer(int len, uint8_t *d) {
+ char status[DBD15STATLEN+1]={0};
+ uint16_t crc;
+
+ if (len > 3) {
+ if (d[0] & ISO15693_RES_EXT)
+ strncat(status,"ProtExt ", DBD15STATLEN);
+ if (d[0] & ISO15693_RES_ERROR) {
+ // error
+ strncat(status,"Error ", DBD15STATLEN);
+ switch (d[1]) {
+ case 0x01:
+ strncat(status,"01:notSupp", DBD15STATLEN);
+ break;
+ case 0x02:
+ strncat(status,"02:notRecog", DBD15STATLEN);
+ break;
+ case 0x03:
+ strncat(status,"03:optNotSupp", DBD15STATLEN);
+ break;
+ case 0x0f:
+ strncat(status,"0f:noInfo", DBD15STATLEN);
+ break;
+ case 0x10:
+ strncat(status,"10:doesn'tExist", DBD15STATLEN);
+ break;
+ case 0x11:
+ strncat(status,"11:lockAgain", DBD15STATLEN);
+ break;
+ case 0x12:
+ strncat(status,"12:locked", DBD15STATLEN);
+ break;
+ case 0x13:
+ strncat(status,"13:progErr", DBD15STATLEN);
+ break;
+ case 0x14:
+ strncat(status,"14:lockErr", DBD15STATLEN);
+ break;
+ default:
+ strncat(status,"unknownErr", DBD15STATLEN);
+ }
+ strncat(status," ", DBD15STATLEN);
+ } else {
+ strncat(status,"NoErr ", DBD15STATLEN);
+ }
+
+ crc=Iso15693Crc(d,len-2);
+ if ( (( crc & 0xff ) == d[len-2]) && (( crc >> 8 ) == d[len-1]) )
+ strncat(status,"CrcOK",DBD15STATLEN);
+ else
+ strncat(status,"CrcFail!",DBD15STATLEN);
+
+ Dbprintf("%s",status);
+ }
+}
+
+
+
+///////////////////////////////////////////////////////////////////////
+// Functions called via USB/Client
+///////////////////////////////////////////////////////////////////////
+
+void SetDebugIso15693(uint32_t debug) {
+ DEBUG=debug;
+ Dbprintf("Iso15693 Debug is now %s",DEBUG?"on":"off");
+ return;
+}
+
+
+//---------------------------------------------------------------------------------------
+// Simulate an ISO15693 reader, perform anti-collision and then attempt to read a sector.
+// all demodulation performed in arm rather than host. - greg
+//---------------------------------------------------------------------------------------
+void ReaderIso15693(uint32_t parameter) {
+
+ LED_A_ON();
+
+ set_tracing(true);
+
+ uint8_t TagUID[8] = {0x00};
+
+ FpgaDownloadAndGo(FPGA_BITSTREAM_HF);
+
+ uint8_t answer[ISO15693_MAX_RESPONSE_LENGTH];
+
+ SetAdcMuxFor(GPIO_MUXSEL_HIPKD);
+ // Setup SSC
+ FpgaSetupSsc(FPGA_MAJOR_MODE_HF_READER);
+
+ // Start from off (no field generated)
+ FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
+ SpinDelay(200);
+
+ // Give the tags time to energize
+ LED_D_ON();
+ FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER);
+ SpinDelay(200);
+ StartCountSspClk();