proxmark3/armsrc/hfsnoop.c

131 lines
4.2 KiB
C
Raw Normal View History

//-----------------------------------------------------------------------------
// piwi, 2019
//
// This code is licensed to you under the terms of the GNU GPL, version 2 or,
// at your option, any later version. See the LICENSE.txt file for the text of
// the license.
//-----------------------------------------------------------------------------
// Routines to get sample data from FPGA.
//-----------------------------------------------------------------------------
2019-10-27 00:56:36 +08:00
#include "hfsnoop.h"
#include "proxmark3_arm.h"
#include "BigBuf.h"
#include "fpgaloader.h"
#include "ticks.h"
#include "dbprint.h"
#include "util.h"
#include "fpga.h"
#include "appmain.h"
#include "cmd.h"
2019-03-12 20:15:39 +08:00
static void RAMFUNC optimizedSniff(void) {
2019-03-10 03:34:41 +08:00
int n = BigBuf_max_traceLen() / sizeof(uint16_t); // take all memory
2019-03-10 03:34:41 +08:00
uint16_t *dest = (uint16_t *)BigBuf_get_addr();
2019-03-10 07:00:59 +08:00
uint16_t *destend = dest + n - 1;
2019-03-10 03:34:41 +08:00
// Reading data loop
2019-03-10 07:00:59 +08:00
while (dest <= destend) {
if (AT91C_BASE_SSC->SSC_SR & AT91C_SSC_RXRDY) {
2019-03-10 03:34:41 +08:00
*dest = (uint16_t)(AT91C_BASE_SSC->SSC_RHR);
dest++;
}
}
2019-03-09 18:51:46 +08:00
//setting tracelen - important! it was set by buffer overflow before
2019-03-10 07:00:59 +08:00
set_tracelen(BigBuf_max_traceLen());
}
2019-03-12 20:15:39 +08:00
void HfSniff(int samplesToSkip, int triggersToSkip) {
2019-03-10 07:00:59 +08:00
BigBuf_free();
BigBuf_Clear();
2019-03-10 03:34:41 +08:00
Dbprintf("Skipping first %d sample pairs, Skipping %d triggers.\n", samplesToSkip, triggersToSkip);
int trigger_cnt = 0;
2019-03-10 03:34:41 +08:00
LED_D_ON();
2019-03-10 03:34:41 +08:00
FpgaDownloadAndGo(FPGA_BITSTREAM_HF);
2019-03-10 03:34:41 +08:00
SetAdcMuxFor(GPIO_MUXSEL_HIPKD);
2019-03-10 03:34:41 +08:00
// Set up the synchronous serial port
FpgaSetupSsc();
2019-03-10 03:34:41 +08:00
// Setting Frame Mode For better performance on high speed data transfer.
AT91C_BASE_SSC->SSC_RFMR = SSC_FRAME_MODE_BITS_IN_WORD(16);
2019-03-10 03:34:41 +08:00
FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_SNOOP);
SpinDelay(100);
2019-03-10 03:34:41 +08:00
uint16_t r = 0;
while (!BUTTON_PRESS() && !data_available()) {
2019-03-10 03:34:41 +08:00
WDT_HIT();
2019-03-10 03:34:41 +08:00
if (AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) {
r = (uint16_t)AT91C_BASE_SSC->SSC_RHR;
r = MAX(r & 0xff, r >> 8);
if (r >= 180) { // 0xB4 ??
if (++trigger_cnt > triggersToSkip)
2019-03-10 07:00:59 +08:00
break;
2019-03-10 03:34:41 +08:00
}
}
}
if (!BUTTON_PRESS()) {
int waitcount = samplesToSkip; // lets wait 40000 ticks of pck0
2019-03-10 07:00:59 +08:00
while (waitcount != 0) {
2019-03-10 03:34:41 +08:00
2019-03-10 07:00:59 +08:00
if (AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY))
2019-03-10 03:34:41 +08:00
waitcount--;
}
2019-03-12 20:15:39 +08:00
optimizedSniff();
2019-03-10 03:34:41 +08:00
Dbprintf("Trigger kicked! Value: %d, Dumping Samples Hispeed now.", r);
}
//Resetting Frame mode (First set in fpgaloader.c)
AT91C_BASE_SSC->SSC_RFMR = SSC_FRAME_MODE_BITS_IN_WORD(8) | AT91C_SSC_MSBF | SSC_FRAME_MODE_WORDS_PER_TRANSFER(0);
2019-03-12 20:15:39 +08:00
DbpString("HF Sniffing end");
2019-03-10 03:34:41 +08:00
FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
LED_D_OFF();
}
void HfPlotDownload(void) {
2020-01-13 00:28:12 +08:00
uint8_t *buf = ToSend;
uint8_t *this_buf = buf;
2020-01-13 00:28:12 +08:00
FpgaDownloadAndGo(FPGA_BITSTREAM_HF);
FpgaSetupSsc();
2020-01-13 00:28:12 +08:00
AT91C_BASE_PDC_SSC->PDC_PTCR = AT91C_PDC_RXTDIS; // Disable DMA Transfer
AT91C_BASE_PDC_SSC->PDC_RPR = (uint32_t) this_buf; // start transfer to this memory address
AT91C_BASE_PDC_SSC->PDC_RCR = PM3_CMD_DATA_SIZE; // transfer this many samples
buf[0] = (uint8_t)AT91C_BASE_SSC->SSC_RHR; // clear receive register
AT91C_BASE_PDC_SSC->PDC_PTCR = AT91C_PDC_RXTEN; // Start DMA transfer
2020-01-13 00:28:12 +08:00
FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_GET_TRACE); // let FPGA transfer its internal Block-RAM
2020-01-13 00:28:12 +08:00
LED_B_ON();
for (size_t i = 0; i < FPGA_TRACE_SIZE; i += PM3_CMD_DATA_SIZE) {
// prepare next DMA transfer:
uint8_t *next_buf = buf + ((i + PM3_CMD_DATA_SIZE) % (2 * PM3_CMD_DATA_SIZE));
2020-01-13 00:28:12 +08:00
AT91C_BASE_PDC_SSC->PDC_RNPR = (uint32_t)next_buf;
AT91C_BASE_PDC_SSC->PDC_RNCR = PM3_CMD_DATA_SIZE;
2020-01-13 00:28:12 +08:00
size_t len = MIN(FPGA_TRACE_SIZE - i, PM3_CMD_DATA_SIZE);
2020-01-13 00:28:12 +08:00
while (!(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_ENDRX))) {}; // wait for DMA transfer to complete
2020-01-13 00:28:12 +08:00
reply_old(CMD_FPGAMEM_DOWNLOADED, i, len, FPGA_TRACE_SIZE, this_buf, len);
this_buf = next_buf;
}
FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
2020-01-13 00:28:12 +08:00
// Trigger a finish downloading signal with an ACK frame
reply_mix(CMD_ACK, 1, 0, FPGA_TRACE_SIZE, 0, 0);
LED_B_OFF();
}