From 18da534554c50efdf5439056b58a17db04a393d8 Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Sun, 21 Jun 2020 18:13:14 +0200 Subject: [PATCH] chg: 'hf sniff' - now malloc and is interupable --- armsrc/appmain.c | 7 ++++++- armsrc/hfsnoop.c | 29 +++++++++++++++++------------ client/src/cmdhf.c | 39 ++++++++++++++++++++++++++------------- 3 files changed, 49 insertions(+), 26 deletions(-) diff --git a/armsrc/appmain.c b/armsrc/appmain.c index f49452514..ab18aa168 100644 --- a/armsrc/appmain.c +++ b/armsrc/appmain.c @@ -1424,7 +1424,12 @@ static void PacketReceived(PacketCommandNG *packet) { uint16_t len = 0; 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; } #endif diff --git a/armsrc/hfsnoop.c b/armsrc/hfsnoop.c index c3e63876c..177a1f99e 100644 --- a/armsrc/hfsnoop.c +++ b/armsrc/hfsnoop.c @@ -19,13 +19,13 @@ #include "cmd.h" 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) { *dest = (uint16_t)(AT91C_BASE_SSC->SSC_RHR); dest++; + dsize -= sizeof(dsize); } } - Dbprintf("collected %u samples", dsize); } 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); uint8_t *mem = BigBuf_malloc(*len); - int trigger_cnt = 0; + uint32_t trigger_cnt = 0; uint16_t r = 0, interval = 0; - bool pressed = false; while (pressed == false) { WDT_HIT(); // cancel w usb command. - if (interval == 1000) { + if (interval == 2000) { if (data_available()) break; + interval = 0; } else { 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. if (r > 180) { - if (++trigger_cnt > triggersToSkip) + + if (++trigger_cnt > triggersToSkip) { break; + } } } @@ -88,16 +90,19 @@ int HfSniff(uint32_t samplesToSkip, uint32_t triggersToSkip, uint16_t *len) { if (pressed == false) { // skip samples loop - int waitcount = samplesToSkip; - while (waitcount != 0) { + while (samplesToSkip != 0) { - if (AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) - waitcount--; + if (AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) { + 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) diff --git a/client/src/cmdhf.c b/client/src/cmdhf.c index f94fd7b6d..26723ca6a 100644 --- a/client/src/cmdhf.c +++ b/client/src/cmdhf.c @@ -46,7 +46,8 @@ static int CmdHelp(const char *Cmd); static int usage_hf_search(void) { 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, " h - This help"); PrintAndLogEx(NORMAL, ""); @@ -64,18 +65,21 @@ static int usage_hf_sniff(void) { PrintAndLogEx(NORMAL, " - skip number of triggers"); PrintAndLogEx(NORMAL, ""); PrintAndLogEx(NORMAL, "Examples:"); - PrintAndLogEx(NORMAL, " hf sniff"); - PrintAndLogEx(NORMAL, " hf sniff 1000 0"); + PrintAndLogEx(NORMAL, _YELLOW_(" hf sniff")); + PrintAndLogEx(NORMAL, _YELLOW_(" hf sniff 1000 0")); + PrintAndLogEx(NORMAL, ""); return PM3_SUCCESS; } static int usage_hf_tune(void) { 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] []"); PrintAndLogEx(NORMAL, "Options:"); PrintAndLogEx(NORMAL, " h - This help"); PrintAndLogEx(NORMAL, " - number of iterations (default: 0=infinite)"); + PrintAndLogEx(NORMAL, "Examples:"); + PrintAndLogEx(NORMAL, _YELLOW_(" hf tune 1")); PrintAndLogEx(NORMAL, ""); return PM3_SUCCESS; } @@ -195,7 +199,7 @@ int CmdHFSearch(const char *Cmd) { int CmdHFTune(const char *Cmd) { char cmdp = tolower(param_getchar(Cmd, 0)); 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"); PacketResponseNG resp; @@ -263,28 +267,37 @@ int CmdHFSniff(const char *Cmd) { for (;;) { if (kbd_enter_pressed()) { + SendCommandNG(CMD_BREAK_LOOP, NULL, 0); PrintAndLogEx(INFO, "User aborted"); break; } PacketResponseNG resp; - if (WaitForResponseTimeout(CMD_HF_SNIFF, &resp, 4000)) { + if (WaitForResponseTimeout(CMD_HF_SNIFF, &resp, 1000)) { if (resp.status == PM3_EOPABORTED) { + PrintAndLogEx(INFO, "Button pressed, user aborted"); break; } if (resp.status == PM3_SUCCESS) { - uint16_t len = resp.data.asDwords[0] & 0xFFFF; - PrintAndLogEx(INFO, "HF sniff len %u bytes", len); + struct r { + 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 save") "` to save"); - // download bigbuf_malloced.. - // it reservs mem from the higher range. ie we can't start from beginning idx 0. - // but from - uint32_t start = pm3_capabilities.bigbuf_size - len; - int res = getSamplesEx(start, start + len, false); + // download bigbuf_malloc:d. + // it reserve memory from the higher end. + // At the moment, sniff takes all free memory in bigbuff. If this changes, + // we can't start from beginning idx 0 but from that hi-to-start-of-allocated. + uint32_t start = pm3_capabilities.bigbuf_size - retval->len; + int res = getSamplesEx(start, start, false); if (res != PM3_SUCCESS) { PrintAndLogEx(WARNING, "failed to download samples to client"); return res;