chg: 'hf sniff' - now malloc and is interupable

This commit is contained in:
iceman1001 2020-06-21 18:13:14 +02:00
commit 18da534554
3 changed files with 49 additions and 26 deletions

View file

@ -1424,7 +1424,12 @@ static void PacketReceived(PacketCommandNG *packet) {
uint16_t len = 0; uint16_t len = 0;
int res = HfSniff(payload->samplesToSkip, payload->triggersToSkip, &len); int res = HfSniff(payload->samplesToSkip, payload->triggersToSkip, &len);
reply_ng(CMD_HF_SNIFF, res, (uint8_t *)&len, sizeof(len));
struct {
uint16_t len;
} PACKED retval;
retval.len = len;
reply_ng(CMD_HF_SNIFF, res, (uint8_t *)&retval, sizeof(retval));
break; break;
} }
#endif #endif

View file

@ -19,13 +19,13 @@
#include "cmd.h" #include "cmd.h"
static void RAMFUNC optimizedSniff(uint16_t *dest, uint16_t dsize) { static void RAMFUNC optimizedSniff(uint16_t *dest, uint16_t dsize) {
for (; dsize > 0; dsize -= sizeof(dsize)) { while (dsize > 0) {
if (AT91C_BASE_SSC->SSC_SR & AT91C_SSC_RXRDY) { if (AT91C_BASE_SSC->SSC_SR & AT91C_SSC_RXRDY) {
*dest = (uint16_t)(AT91C_BASE_SSC->SSC_RHR); *dest = (uint16_t)(AT91C_BASE_SSC->SSC_RHR);
dest++; dest++;
dsize -= sizeof(dsize);
} }
} }
Dbprintf("collected %u samples", dsize);
} }
int HfSniff(uint32_t samplesToSkip, uint32_t triggersToSkip, uint16_t *len) { int HfSniff(uint32_t samplesToSkip, uint32_t triggersToSkip, uint16_t *len) {
@ -52,18 +52,18 @@ int HfSniff(uint32_t samplesToSkip, uint32_t triggersToSkip, uint16_t *len) {
*len = (BigBuf_max_traceLen() & 0xFFFE); *len = (BigBuf_max_traceLen() & 0xFFFE);
uint8_t *mem = BigBuf_malloc(*len); uint8_t *mem = BigBuf_malloc(*len);
int trigger_cnt = 0; uint32_t trigger_cnt = 0;
uint16_t r = 0, interval = 0; uint16_t r = 0, interval = 0;
bool pressed = false; bool pressed = false;
while (pressed == false) { while (pressed == false) {
WDT_HIT(); WDT_HIT();
// cancel w usb command. // cancel w usb command.
if (interval == 1000) { if (interval == 2000) {
if (data_available()) if (data_available())
break; break;
interval = 0; interval = 0;
} else { } else {
interval++; interval++;
@ -77,8 +77,10 @@ int HfSniff(uint32_t samplesToSkip, uint32_t triggersToSkip, uint16_t *len) {
// 180 (0xB4) arbitary value to see if a strong RF field is near. // 180 (0xB4) arbitary value to see if a strong RF field is near.
if (r > 180) { if (r > 180) {
if (++trigger_cnt > triggersToSkip)
if (++trigger_cnt > triggersToSkip) {
break; break;
}
} }
} }
@ -88,16 +90,19 @@ int HfSniff(uint32_t samplesToSkip, uint32_t triggersToSkip, uint16_t *len) {
if (pressed == false) { if (pressed == false) {
// skip samples loop // skip samples loop
int waitcount = samplesToSkip; while (samplesToSkip != 0) {
while (waitcount != 0) {
if (AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) if (AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) {
waitcount--; samplesToSkip--;
}
} }
optimizedSniff((uint16_t *)mem, (*len) >> 2); optimizedSniff((uint16_t*)mem, *len);
Dbprintf("Trigger kicked in (%d >= 180)", r); if (DBGLEVEL >= DBG_INFO) {
Dbprintf("Trigger kicked in (%d >= 180)", r);
Dbprintf("Collected %u samples", *len);
}
} }
//Resetting Frame mode (First set in fpgaloader.c) //Resetting Frame mode (First set in fpgaloader.c)

View file

@ -46,7 +46,8 @@ static int CmdHelp(const char *Cmd);
static int usage_hf_search(void) { static int usage_hf_search(void) {
PrintAndLogEx(NORMAL, "Usage: hf search"); PrintAndLogEx(NORMAL, "Usage: hf search");
PrintAndLogEx(NORMAL, "Will try to find a HF read out of the unknown tag. Stops when found."); PrintAndLogEx(NORMAL, "Will try to find a HF read out of the unknown tag.");
PrintAndLogEx(NORMAL, "Continues to search for all different HF protocols");
PrintAndLogEx(NORMAL, "Options:"); PrintAndLogEx(NORMAL, "Options:");
PrintAndLogEx(NORMAL, " h - This help"); PrintAndLogEx(NORMAL, " h - This help");
PrintAndLogEx(NORMAL, ""); PrintAndLogEx(NORMAL, "");
@ -64,18 +65,21 @@ static int usage_hf_sniff(void) {
PrintAndLogEx(NORMAL, " <skip triggers> - skip number of triggers"); PrintAndLogEx(NORMAL, " <skip triggers> - skip number of triggers");
PrintAndLogEx(NORMAL, ""); PrintAndLogEx(NORMAL, "");
PrintAndLogEx(NORMAL, "Examples:"); PrintAndLogEx(NORMAL, "Examples:");
PrintAndLogEx(NORMAL, " hf sniff"); PrintAndLogEx(NORMAL, _YELLOW_(" hf sniff"));
PrintAndLogEx(NORMAL, " hf sniff 1000 0"); PrintAndLogEx(NORMAL, _YELLOW_(" hf sniff 1000 0"));
PrintAndLogEx(NORMAL, "");
return PM3_SUCCESS; return PM3_SUCCESS;
} }
static int usage_hf_tune(void) { static int usage_hf_tune(void) {
PrintAndLogEx(NORMAL, "Continuously measure HF antenna tuning."); PrintAndLogEx(NORMAL, "Continuously measure HF antenna tuning.");
PrintAndLogEx(NORMAL, "Press button or Enter to interrupt."); PrintAndLogEx(NORMAL, "Press button or `enter` to interrupt.");
PrintAndLogEx(NORMAL, "Usage: hf tune [h] [<iter>]"); PrintAndLogEx(NORMAL, "Usage: hf tune [h] [<iter>]");
PrintAndLogEx(NORMAL, "Options:"); PrintAndLogEx(NORMAL, "Options:");
PrintAndLogEx(NORMAL, " h - This help"); PrintAndLogEx(NORMAL, " h - This help");
PrintAndLogEx(NORMAL, " <iter> - number of iterations (default: 0=infinite)"); PrintAndLogEx(NORMAL, " <iter> - number of iterations (default: 0=infinite)");
PrintAndLogEx(NORMAL, "Examples:");
PrintAndLogEx(NORMAL, _YELLOW_(" hf tune 1"));
PrintAndLogEx(NORMAL, ""); PrintAndLogEx(NORMAL, "");
return PM3_SUCCESS; return PM3_SUCCESS;
} }
@ -195,7 +199,7 @@ int CmdHFSearch(const char *Cmd) {
int CmdHFTune(const char *Cmd) { int CmdHFTune(const char *Cmd) {
char cmdp = tolower(param_getchar(Cmd, 0)); char cmdp = tolower(param_getchar(Cmd, 0));
if (cmdp == 'h') return usage_hf_tune(); if (cmdp == 'h') return usage_hf_tune();
int iter = param_get32ex(Cmd, 0, 0, 10); int iter = param_get32ex(Cmd, 0, 0, 10);
PrintAndLogEx(INFO, "Measuring HF antenna, click " _GREEN_("pm3 button") " or press " _GREEN_("Enter") " to exit"); PrintAndLogEx(INFO, "Measuring HF antenna, click " _GREEN_("pm3 button") " or press " _GREEN_("Enter") " to exit");
PacketResponseNG resp; PacketResponseNG resp;
@ -263,28 +267,37 @@ int CmdHFSniff(const char *Cmd) {
for (;;) { for (;;) {
if (kbd_enter_pressed()) { if (kbd_enter_pressed()) {
SendCommandNG(CMD_BREAK_LOOP, NULL, 0);
PrintAndLogEx(INFO, "User aborted"); PrintAndLogEx(INFO, "User aborted");
break; break;
} }
PacketResponseNG resp; PacketResponseNG resp;
if (WaitForResponseTimeout(CMD_HF_SNIFF, &resp, 4000)) { if (WaitForResponseTimeout(CMD_HF_SNIFF, &resp, 1000)) {
if (resp.status == PM3_EOPABORTED) { if (resp.status == PM3_EOPABORTED) {
PrintAndLogEx(INFO, "Button pressed, user aborted");
break; break;
} }
if (resp.status == PM3_SUCCESS) { if (resp.status == PM3_SUCCESS) {
uint16_t len = resp.data.asDwords[0] & 0xFFFF; struct r {
PrintAndLogEx(INFO, "HF sniff len %u bytes", len); uint16_t len;
} PACKED;
struct r *retval = (struct r *)resp.data.asBytes;
PrintAndLogEx(INFO, "HF sniff (%u samples)", retval->len);
PrintAndLogEx(HINT, "Use `" _YELLOW_("data hpf") "` to remove offset");
PrintAndLogEx(HINT, "Use `" _YELLOW_("data plot") "` to view"); PrintAndLogEx(HINT, "Use `" _YELLOW_("data plot") "` to view");
PrintAndLogEx(HINT, "Use `" _YELLOW_("data save") "` to save"); PrintAndLogEx(HINT, "Use `" _YELLOW_("data save") "` to save");
// download bigbuf_malloced.. // download bigbuf_malloc:d.
// it reservs mem from the higher range. ie we can't start from beginning idx 0. // it reserve memory from the higher end.
// but from // At the moment, sniff takes all free memory in bigbuff. If this changes,
uint32_t start = pm3_capabilities.bigbuf_size - len; // we can't start from beginning idx 0 but from that hi-to-start-of-allocated.
int res = getSamplesEx(start, start + len, false); uint32_t start = pm3_capabilities.bigbuf_size - retval->len;
int res = getSamplesEx(start, start, false);
if (res != PM3_SUCCESS) { if (res != PM3_SUCCESS) {
PrintAndLogEx(WARNING, "failed to download samples to client"); PrintAndLogEx(WARNING, "failed to download samples to client");
return res; return res;