2020-01-12 23:45:24 +08:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// 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"
|
2019-08-08 22:57:33 +08:00
|
|
|
#include "proxmark3_arm.h"
|
2015-10-28 04:47:21 +08:00
|
|
|
#include "BigBuf.h"
|
2019-08-08 22:57:33 +08:00
|
|
|
#include "fpgaloader.h"
|
|
|
|
#include "ticks.h"
|
|
|
|
#include "dbprint.h"
|
2015-10-28 04:47:21 +08:00
|
|
|
#include "util.h"
|
2020-01-12 23:45:24 +08:00
|
|
|
#include "fpga.h"
|
|
|
|
#include "appmain.h"
|
|
|
|
#include "cmd.h"
|
2015-10-28 04:47:21 +08:00
|
|
|
|
2020-06-18 17:54:19 +08:00
|
|
|
static void RAMFUNC optimizedSniff(uint16_t *dest, uint16_t dsize) {
|
2020-06-22 00:13:14 +08:00
|
|
|
while (dsize > 0) {
|
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
|
|
|
*dest = (uint16_t)(AT91C_BASE_SSC->SSC_RHR);
|
|
|
|
dest++;
|
2020-06-22 00:13:14 +08:00
|
|
|
dsize -= sizeof(dsize);
|
2019-03-10 03:34:41 +08:00
|
|
|
}
|
|
|
|
}
|
2015-10-28 04:47:21 +08:00
|
|
|
}
|
|
|
|
|
2020-06-18 17:54:19 +08:00
|
|
|
int HfSniff(uint32_t samplesToSkip, uint32_t triggersToSkip, uint16_t *len) {
|
2019-03-10 07:00:59 +08:00
|
|
|
BigBuf_free();
|
2020-06-20 00:34:47 +08:00
|
|
|
BigBuf_Clear_ext(false);
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2020-06-18 17:54:19 +08:00
|
|
|
Dbprintf("Skipping first %d sample pairs, Skipping %d triggers", samplesToSkip, triggersToSkip);
|
2016-03-12 16:03:28 +08:00
|
|
|
|
2019-03-10 03:34:41 +08:00
|
|
|
LED_D_ON();
|
2018-08-13 03:54:31 +08:00
|
|
|
|
2019-03-10 03:34:41 +08:00
|
|
|
FpgaDownloadAndGo(FPGA_BITSTREAM_HF);
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2019-03-10 03:34:41 +08:00
|
|
|
SetAdcMuxFor(GPIO_MUXSEL_HIPKD);
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2019-03-10 03:34:41 +08:00
|
|
|
// Set up the synchronous serial port
|
2020-07-06 21:16:00 +08:00
|
|
|
FpgaSetupSsc(FPGA_MAJOR_MODE_HF_SNIFF);
|
2018-08-13 03:54:31 +08:00
|
|
|
|
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-09 15:59:13 +08:00
|
|
|
|
2020-07-06 21:16:00 +08:00
|
|
|
FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_SNIFF);
|
2017-10-08 20:56:04 +08:00
|
|
|
SpinDelay(100);
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2020-06-18 17:54:19 +08:00
|
|
|
*len = (BigBuf_max_traceLen() & 0xFFFE);
|
|
|
|
uint8_t *mem = BigBuf_malloc(*len);
|
|
|
|
|
2020-08-13 18:25:04 +08:00
|
|
|
uint32_t trigger_cnt = 0;
|
2020-06-18 17:54:19 +08:00
|
|
|
uint16_t r = 0, interval = 0;
|
2020-06-20 00:34:47 +08:00
|
|
|
|
2020-06-18 17:54:19 +08:00
|
|
|
bool pressed = false;
|
|
|
|
while (pressed == false) {
|
2019-03-10 03:34:41 +08:00
|
|
|
WDT_HIT();
|
2019-03-09 15:59:13 +08:00
|
|
|
|
2020-06-18 17:54:19 +08:00
|
|
|
// cancel w usb command.
|
2020-06-22 00:13:14 +08:00
|
|
|
if (interval == 2000) {
|
2020-06-18 17:54:19 +08:00
|
|
|
if (data_available())
|
|
|
|
break;
|
2020-06-22 00:13:14 +08:00
|
|
|
|
2020-06-18 17:54:19 +08:00
|
|
|
interval = 0;
|
|
|
|
} else {
|
|
|
|
interval++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if trigger is reached
|
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;
|
2020-06-18 17:54:19 +08:00
|
|
|
|
|
|
|
r = MAX(r & 0xFF, r >> 8);
|
|
|
|
|
|
|
|
// 180 (0xB4) arbitary value to see if a strong RF field is near.
|
|
|
|
if (r > 180) {
|
2020-08-13 18:25:04 +08:00
|
|
|
|
2020-06-22 00:13:14 +08:00
|
|
|
if (++trigger_cnt > triggersToSkip) {
|
2019-03-10 07:00:59 +08:00
|
|
|
break;
|
2020-06-22 00:13:14 +08:00
|
|
|
}
|
2019-03-10 03:34:41 +08:00
|
|
|
}
|
|
|
|
}
|
2020-06-18 17:54:19 +08:00
|
|
|
|
|
|
|
pressed = BUTTON_PRESS();
|
2019-03-10 03:34:41 +08:00
|
|
|
}
|
|
|
|
|
2020-06-18 17:54:19 +08:00
|
|
|
if (pressed == false) {
|
|
|
|
|
|
|
|
// skip samples loop
|
2020-06-22 00:13:14 +08:00
|
|
|
while (samplesToSkip != 0) {
|
2019-03-10 03:34:41 +08:00
|
|
|
|
2020-06-22 00:13:14 +08:00
|
|
|
if (AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) {
|
|
|
|
samplesToSkip--;
|
|
|
|
}
|
2019-03-10 03:34:41 +08:00
|
|
|
}
|
2020-06-18 17:54:19 +08:00
|
|
|
|
2020-08-13 18:25:04 +08:00
|
|
|
optimizedSniff((uint16_t *)mem, *len);
|
2020-06-18 17:54:19 +08:00
|
|
|
|
2020-10-04 08:09:58 +08:00
|
|
|
if (DBGLEVEL >= DBG_INFO) {
|
2020-06-22 00:13:14 +08:00
|
|
|
Dbprintf("Trigger kicked in (%d >= 180)", r);
|
|
|
|
Dbprintf("Collected %u samples", *len);
|
|
|
|
}
|
2019-03-10 03:34:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//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);
|
|
|
|
LED_D_OFF();
|
2020-06-18 17:54:19 +08:00
|
|
|
FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
|
|
|
|
BigBuf_free();
|
|
|
|
return (pressed) ? PM3_EOPABORTED : PM3_SUCCESS;
|
2015-10-28 04:47:21 +08:00
|
|
|
}
|
2020-01-12 23:45:24 +08:00
|
|
|
|
|
|
|
void HfPlotDownload(void) {
|
2020-07-13 23:56:19 +08:00
|
|
|
|
|
|
|
tosend_t *ts = get_tosend();
|
|
|
|
uint8_t *this_buf = ts->buf;
|
2020-01-12 23:45:24 +08:00
|
|
|
|
2020-01-13 00:28:12 +08:00
|
|
|
FpgaDownloadAndGo(FPGA_BITSTREAM_HF);
|
|
|
|
|
2020-07-07 19:18:53 +08:00
|
|
|
FpgaSetupSsc(FPGA_MAJOR_MODE_HF_GET_TRACE);
|
2020-01-12 23:45:24 +08:00
|
|
|
|
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
|
2020-07-13 23:56:19 +08:00
|
|
|
ts->buf[0] = (uint8_t)AT91C_BASE_SSC->SSC_RHR; // clear receive register
|
2020-01-13 00:28:12 +08:00
|
|
|
AT91C_BASE_PDC_SSC->PDC_PTCR = AT91C_PDC_RXTEN; // Start DMA transfer
|
2020-01-12 23:45:24 +08:00
|
|
|
|
2020-01-13 00:28:12 +08:00
|
|
|
FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_GET_TRACE); // let FPGA transfer its internal Block-RAM
|
2020-01-12 23:45:24 +08:00
|
|
|
|
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:
|
2020-07-13 23:56:19 +08:00
|
|
|
uint8_t *next_buf = ts->buf + ((i + PM3_CMD_DATA_SIZE) % (2 * PM3_CMD_DATA_SIZE));
|
2020-01-12 23:45:24 +08:00
|
|
|
|
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-12 23:45:24 +08:00
|
|
|
|
2020-01-13 00:28:12 +08:00
|
|
|
size_t len = MIN(FPGA_TRACE_SIZE - i, PM3_CMD_DATA_SIZE);
|
2020-01-12 23:45:24 +08:00
|
|
|
|
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-12 23:45:24 +08:00
|
|
|
|
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-12 23:45:24 +08:00
|
|
|
|
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();
|
2020-01-12 23:45:24 +08:00
|
|
|
}
|