From 0b95c519ff4a034dba64a8340cb63a08dcc39f38 Mon Sep 17 00:00:00 2001 From: merlokk <807634+merlokk@users.noreply.github.com> Date: Sat, 9 Jul 2022 16:45:08 +0300 Subject: [PATCH 1/8] sh sniff skip some bytes from data - arm side --- armsrc/appmain.c | 4 +++- armsrc/hfsnoop.c | 46 ++++++++++++++++++++++++++++++++++++++++++++-- armsrc/hfsnoop.h | 9 ++++++++- 3 files changed, 55 insertions(+), 4 deletions(-) diff --git a/armsrc/appmain.c b/armsrc/appmain.c index 106fe3903..330bf2c48 100644 --- a/armsrc/appmain.c +++ b/armsrc/appmain.c @@ -1784,11 +1784,13 @@ static void PacketReceived(PacketCommandNG *packet) { struct p { uint32_t samplesToSkip; uint32_t triggersToSkip; + uint8_t skipMode; + uint8_t skipRatio; } PACKED; struct p *payload = (struct p *)packet->data.asBytes; uint16_t len = 0; - int res = HfSniff(payload->samplesToSkip, payload->triggersToSkip, &len); + int res = HfSniff(payload->samplesToSkip, payload->triggersToSkip, &len, payload->skipMode, payload->skipRatio); struct { uint16_t len; diff --git a/armsrc/hfsnoop.c b/armsrc/hfsnoop.c index ba9ef0740..f12b4863b 100644 --- a/armsrc/hfsnoop.c +++ b/armsrc/hfsnoop.c @@ -36,7 +36,46 @@ static void RAMFUNC optimizedSniff(uint16_t *dest, uint16_t dsize) { } } -int HfSniff(uint32_t samplesToSkip, uint32_t triggersToSkip, uint16_t *len) { +static void RAMFUNC skipSniff(uint8_t *dest, uint16_t dsize, uint8_t skipMode, uint8_t skipRatio) { + uint32_t accum = (skipMode == HF_SNOOP_SKIP_MIN) ? 0xffffffff : 0; + uint8_t ratioindx = 0; + while (dsize > 0) { + if (AT91C_BASE_SSC->SSC_SR & AT91C_SSC_RXRDY) { + volatile uint16_t val = (uint16_t)(AT91C_BASE_SSC->SSC_RHR); + switch (skipMode) { + case HF_SNOOP_SKIP_MAX: + if (accum < (val & 0xff)) + accum = val & 0xff; + if (accum < (val << 8)) + accum = val << 8; + case HF_SNOOP_SKIP_MIN: + if (accum > (val & 0xff)) + accum = val & 0xff; + if (accum > (val << 8)) + accum = val << 8; + case HF_SNOOP_SKIP_AVG: + accum += (val & 0xff) + (val << 8); + default: { // HF_SNOOP_SKIP_DROP and the rest + if (ratioindx == 0) + accum = val & 0xff; + } + } + + ratioindx++; + if (ratioindx >= skipRatio) { + ratioindx = 0; + if (skipMode == HF_SNOOP_SKIP_AVG) + *dest = accum / (skipRatio * 2); + else + *dest = accum; + dest++; + dsize --; + } + } + } +} + +int HfSniff(uint32_t samplesToSkip, uint32_t triggersToSkip, uint16_t *len, uint8_t skipMode, uint8_t skipRatio) { BigBuf_free(); BigBuf_Clear_ext(false); @@ -105,7 +144,10 @@ int HfSniff(uint32_t samplesToSkip, uint32_t triggersToSkip, uint16_t *len) { } } - optimizedSniff((uint16_t *)mem, *len); + if (skipMode == 0) + optimizedSniff((uint16_t *)mem, *len); + else + skipSniff(mem, *len, skipMode, skipRatio); if (g_dbglevel >= DBG_INFO) { Dbprintf("Trigger kicked in (%d >= 180)", r); diff --git a/armsrc/hfsnoop.h b/armsrc/hfsnoop.h index 4b715753d..a68d28a79 100644 --- a/armsrc/hfsnoop.h +++ b/armsrc/hfsnoop.h @@ -18,6 +18,13 @@ #include "proxmark3_arm.h" -int HfSniff(uint32_t samplesToSkip, uint32_t triggersToSkip, uint16_t *len); +// what to do with skipped data +#define HF_SNOOP_SKIP_NONE (0) +#define HF_SNOOP_SKIP_DROP (1) +#define HF_SNOOP_SKIP_MAX (2) +#define HF_SNOOP_SKIP_MIN (3) +#define HF_SNOOP_SKIP_AVG (4) + +int HfSniff(uint32_t samplesToSkip, uint32_t triggersToSkip, uint16_t *len, uint8_t skipMode, uint8_t skipRatio); void HfPlotDownload(void); #endif From e8b742414e36e94c94faa84c0e1a8d4f1c780b05 Mon Sep 17 00:00:00 2001 From: merlokk <807634+merlokk@users.noreply.github.com> Date: Sat, 9 Jul 2022 17:27:04 +0300 Subject: [PATCH 2/8] client part --- client/src/cmdhf.c | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/client/src/cmdhf.c b/client/src/cmdhf.c index 37560f785..419e52a4d 100644 --- a/client/src/cmdhf.c +++ b/client/src/cmdhf.c @@ -301,6 +301,23 @@ int CmdHFTune(const char *Cmd) { return PM3_SUCCESS; } +typedef enum { + HF_SNOOP_SKIP_NONE = 0x00, + HF_SNOOP_SKIP_DROP = 0x01, + HF_SNOOP_SKIP_MAX = 0x02, + HF_SNOOP_SKIP_MIN = 0x03, + HF_SNOOP_SKIP_AVG = 0x04 +} HFSnoopSkipMode; + +const CLIParserOption HFSnoopSkipModeOpts[] = { + {HF_SNOOP_SKIP_NONE, "none"}, + {HF_SNOOP_SKIP_DROP, "drop"}, + {HF_SNOOP_SKIP_MAX, "min"}, + {HF_SNOOP_SKIP_MIN, "max"}, + {HF_SNOOP_SKIP_AVG, "avg"}, + {0, NULL}, +}; + // Collects pars of u8, // uses 16bit transfers from FPGA for speed // Takes all available bigbuff memory @@ -317,8 +334,10 @@ int CmdHFSniff(const char *Cmd) { ); void *argtable[] = { arg_param_begin, - arg_u64_0(NULL, "sp", "", "skip sample pairs"), - arg_u64_0(NULL, "st", "", "skip number of triggers"), + arg_u64_0(NULL, "sp", "", "skip sample pairs"), + arg_u64_0(NULL, "st", "", "skip number of triggers"), + arg_str0(NULL, "smode", "[none|drop|min|max|avg]", "Skip mode. It switches on the function that applies to several samples before they saved to memory"), + arg_int0(NULL, "sratio", "", "Skip ratio. It applied the function above to (ratio * 2) samples. For ratio = 1 it 2 samples."), arg_param_end }; CLIExecWithReturn(ctx, Cmd, argtable, true); @@ -326,10 +345,20 @@ int CmdHFSniff(const char *Cmd) { struct { uint32_t samplesToSkip; uint32_t triggersToSkip; + uint8_t skipMode; + uint8_t skipRatio; } PACKED params; params.samplesToSkip = arg_get_u32_def(ctx, 1, 0); params.triggersToSkip = arg_get_u32_def(ctx, 2, 0); + int smode = 0; + if (CLIGetOptionList(arg_get_str(ctx, 3), HFSnoopSkipModeOpts, &smode)) + return PM3_EINVARG; + + if (smode > 0) + params.skipMode = smode; + + params.skipRatio = arg_get_int_def(ctx, 4, 0); CLIParserFree(ctx); clearCommandBuffer(); From 660858a02c62b858f60d1df1554990954630135e Mon Sep 17 00:00:00 2001 From: merlokk <807634+merlokk@users.noreply.github.com> Date: Sat, 9 Jul 2022 17:34:26 +0300 Subject: [PATCH 3/8] add some verbosity --- client/src/cmdhf.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/client/src/cmdhf.c b/client/src/cmdhf.c index 419e52a4d..cd58e1647 100644 --- a/client/src/cmdhf.c +++ b/client/src/cmdhf.c @@ -361,6 +361,10 @@ int CmdHFSniff(const char *Cmd) { params.skipRatio = arg_get_int_def(ctx, 4, 0); CLIParserFree(ctx); + if (params.skipMode != HF_SNOOP_SKIP_NONE) + PrintAndLogEx(INFO, "Skip mode. Function: %s, each: %d sample", + CLIGetOptionListStr(HFSnoopSkipModeOpts, params.skipMode), params.skipRatio * 2); + clearCommandBuffer(); SendCommandNG(CMD_HF_SNIFF, (uint8_t *)¶ms, sizeof(params)); From 903af4412a6a0f9aec25dbdcb545b20b0b7eb74b Mon Sep 17 00:00:00 2001 From: merlokk <807634+merlokk@users.noreply.github.com> Date: Sat, 9 Jul 2022 17:36:53 +0300 Subject: [PATCH 4/8] fix accum clear --- armsrc/hfsnoop.c | 1 + 1 file changed, 1 insertion(+) diff --git a/armsrc/hfsnoop.c b/armsrc/hfsnoop.c index f12b4863b..9d155237c 100644 --- a/armsrc/hfsnoop.c +++ b/armsrc/hfsnoop.c @@ -70,6 +70,7 @@ static void RAMFUNC skipSniff(uint8_t *dest, uint16_t dsize, uint8_t skipMode, u *dest = accum; dest++; dsize --; + accum = 0; } } } From e7edefe24e5106b15915b74b53b201d3140d2621 Mon Sep 17 00:00:00 2001 From: merlokk <807634+merlokk@users.noreply.github.com> Date: Sun, 10 Jul 2022 00:29:25 +0300 Subject: [PATCH 5/8] fix avg --- armsrc/hfsnoop.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/armsrc/hfsnoop.c b/armsrc/hfsnoop.c index 9d155237c..e06e89eeb 100644 --- a/armsrc/hfsnoop.c +++ b/armsrc/hfsnoop.c @@ -54,7 +54,7 @@ static void RAMFUNC skipSniff(uint8_t *dest, uint16_t dsize, uint8_t skipMode, u if (accum > (val << 8)) accum = val << 8; case HF_SNOOP_SKIP_AVG: - accum += (val & 0xff) + (val << 8); + accum += (val & 0xff) + (val >> 8); default: { // HF_SNOOP_SKIP_DROP and the rest if (ratioindx == 0) accum = val & 0xff; @@ -64,10 +64,15 @@ static void RAMFUNC skipSniff(uint8_t *dest, uint16_t dsize, uint8_t skipMode, u ratioindx++; if (ratioindx >= skipRatio) { ratioindx = 0; - if (skipMode == HF_SNOOP_SKIP_AVG) - *dest = accum / (skipRatio * 2); - else + if (skipMode == HF_SNOOP_SKIP_AVG && skipRatio > 0) { + accum = accum / (skipRatio * 2); + if (accum <= 0xff) + *dest = accum; + else + *dest = 0xff; + } else { *dest = accum; + } dest++; dsize --; accum = 0; From 374f1907bb70d80fa2f19f3222c9b5e80b51d317 Mon Sep 17 00:00:00 2001 From: merlokk <807634+merlokk@users.noreply.github.com> Date: Sun, 10 Jul 2022 00:47:40 +0300 Subject: [PATCH 6/8] fix max|min --- armsrc/hfsnoop.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/armsrc/hfsnoop.c b/armsrc/hfsnoop.c index e06e89eeb..9acbf2d25 100644 --- a/armsrc/hfsnoop.c +++ b/armsrc/hfsnoop.c @@ -46,13 +46,13 @@ static void RAMFUNC skipSniff(uint8_t *dest, uint16_t dsize, uint8_t skipMode, u case HF_SNOOP_SKIP_MAX: if (accum < (val & 0xff)) accum = val & 0xff; - if (accum < (val << 8)) - accum = val << 8; + if (accum < (val >> 8)) + accum = val >> 8; case HF_SNOOP_SKIP_MIN: if (accum > (val & 0xff)) accum = val & 0xff; - if (accum > (val << 8)) - accum = val << 8; + if (accum > (val >> 8)) + accum = val >> 8; case HF_SNOOP_SKIP_AVG: accum += (val & 0xff) + (val >> 8); default: { // HF_SNOOP_SKIP_DROP and the rest @@ -75,7 +75,7 @@ static void RAMFUNC skipSniff(uint8_t *dest, uint16_t dsize, uint8_t skipMode, u } dest++; dsize --; - accum = 0; + accum = (skipMode == HF_SNOOP_SKIP_MIN) ? 0xffffffff : 0; } } } From fb3990086eb4d9e2aa66e5672e69fe3fbd84a696 Mon Sep 17 00:00:00 2001 From: merlokk <807634+merlokk@users.noreply.github.com> Date: Sun, 10 Jul 2022 00:54:29 +0300 Subject: [PATCH 7/8] fix --- armsrc/hfsnoop.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/armsrc/hfsnoop.c b/armsrc/hfsnoop.c index 9acbf2d25..08860da9d 100644 --- a/armsrc/hfsnoop.c +++ b/armsrc/hfsnoop.c @@ -48,13 +48,16 @@ static void RAMFUNC skipSniff(uint8_t *dest, uint16_t dsize, uint8_t skipMode, u accum = val & 0xff; if (accum < (val >> 8)) accum = val >> 8; + break; case HF_SNOOP_SKIP_MIN: if (accum > (val & 0xff)) accum = val & 0xff; if (accum > (val >> 8)) accum = val >> 8; + break; case HF_SNOOP_SKIP_AVG: - accum += (val & 0xff) + (val >> 8); + accum += (val & 0xff) + (val & 0xff); + break; default: { // HF_SNOOP_SKIP_DROP and the rest if (ratioindx == 0) accum = val & 0xff; @@ -63,7 +66,6 @@ static void RAMFUNC skipSniff(uint8_t *dest, uint16_t dsize, uint8_t skipMode, u ratioindx++; if (ratioindx >= skipRatio) { - ratioindx = 0; if (skipMode == HF_SNOOP_SKIP_AVG && skipRatio > 0) { accum = accum / (skipRatio * 2); if (accum <= 0xff) @@ -73,9 +75,11 @@ static void RAMFUNC skipSniff(uint8_t *dest, uint16_t dsize, uint8_t skipMode, u } else { *dest = accum; } + dest++; dsize --; accum = (skipMode == HF_SNOOP_SKIP_MIN) ? 0xffffffff : 0; + ratioindx = 0; } } } From 321d7cf06984aec7d69ffddca900ffab684ecc3b Mon Sep 17 00:00:00 2001 From: merlokk <807634+merlokk@users.noreply.github.com> Date: Sun, 10 Jul 2022 11:35:34 +0300 Subject: [PATCH 8/8] changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index baa9edc3d..0bc8e9ece 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ All notable changes to this project will be documented in this file. This project uses the changelog in accordance with [keepchangelog](http://keepachangelog.com/). Please use this to write notable changes, which is not the same as git commit log... ## [unreleased][unreleased] + - Added `hf sniff --smode` skip/group adc data to consume less memory. Now it can sniff very long signals (@merlokk) - Added `hf fudan` skeleton commands (@iceman1001) - Added `--reboot-to-bootloader` arg to pm3 - Changed `hf 14b raw` - now supports selecting Fuji/Xerox tag (@horror)