Merge remote-tracking branch 'upstream/master'

This commit is contained in:
Colin J. Brigato 2018-09-05 20:39:56 +02:00
commit 76e2d7502a
17 changed files with 509 additions and 407 deletions

View file

@ -3,10 +3,16 @@ 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]
- Changed 'proxmark3 client threading' - remake from official repo (@micolous)
- Add 'rem' - new command that adds a line to the log file (@didierStevens)
- Fix 'EM410xdemod empty tag id in lfops.c' (@Defensor7)
- Fix 'usb device descriptor' - some android phones will enumerate better when iSerialnumber isn't a multiple of 8 (@micolous, @megabug)
- Fix 'StandaloneMode LF' - when collecting signal, justNoise detection is needed (@didierStevens, @Megabug)
- Fix 'StandAloneMode Colin' - mifare1ksim called with right params (@cjbrigato)
- Improved 'install.sh' to install dependencies for Ubuntu 18.04 and using max number of processors during compilation (@joanbono)
- Modified 'install.sh' script to work in macOS and Linux + added the 'update.sh' and 'proxmark3.sh' from joanbono (@TomHarkness)
- Fix 'hf emv' - some cards need to have Le=0x00, some need to not to have (@merlokk)
- Fix 'hf emv' - some cards need to have Le=0x00, some don't need to have (@merlokk)
- Fix 'hf legic' enhancement of rx / tx in legic commands (@drandreas)
- Fix 'data buffclear' - now frees bigbuff also (@iceman)
- Fix GET_TICKS and signess while shifting (@drandreas)

View file

@ -34,14 +34,12 @@ static uint16_t traceLen = 0;
int tracing = 1; //Last global one.. todo static?
// get the address of BigBuf
uint8_t *BigBuf_get_addr(void)
{
uint8_t *BigBuf_get_addr(void) {
return (uint8_t *)BigBuf;
}
// get the address of the emulator memory. Allocate part of Bigbuf for it, if not yet done
uint8_t *BigBuf_get_EM_addr(void)
{
uint8_t *BigBuf_get_EM_addr(void) {
// not yet allocated
if (emulator_memory == NULL)
emulator_memory = BigBuf_malloc(CARD_MEMORY_SIZE);
@ -50,53 +48,45 @@ uint8_t *BigBuf_get_EM_addr(void)
}
// clear ALL of BigBuf
void BigBuf_Clear(void)
{
void BigBuf_Clear(void) {
BigBuf_Clear_ext(true);
}
// clear ALL of BigBuf
void BigBuf_Clear_ext(bool verbose)
{
void BigBuf_Clear_ext(bool verbose) {
memset(BigBuf, 0, BIGBUF_SIZE);
if (verbose)
Dbprintf("Buffer cleared (%i bytes)", BIGBUF_SIZE);
}
void BigBuf_Clear_EM(void){
void BigBuf_Clear_EM(void) {
memset(BigBuf_get_EM_addr(), 0, CARD_MEMORY_SIZE);
}
void BigBuf_Clear_keep_EM(void)
{
void BigBuf_Clear_keep_EM(void) {
memset(BigBuf, 0, BigBuf_hi);
}
// allocate a chunk of memory from BigBuf. We allocate high memory first. The unallocated memory
// at the beginning of BigBuf is always for traces/samples
uint8_t *BigBuf_malloc(uint16_t chunksize)
{
if (BigBuf_hi - chunksize < 0) {
uint8_t *BigBuf_malloc(uint16_t chunksize) {
if (BigBuf_hi - chunksize < 0)
return NULL; // no memory left
} else {
chunksize = (chunksize + 3) & 0xfffc; // round to next multiple of 4
BigBuf_hi -= chunksize; // aligned to 4 Byte boundary
return (uint8_t *)BigBuf + BigBuf_hi;
}
chunksize = (chunksize + 3) & 0xfffc; // round to next multiple of 4
BigBuf_hi -= chunksize; // aligned to 4 Byte boundary
return (uint8_t *)BigBuf + BigBuf_hi;
}
// free ALL allocated chunks. The whole BigBuf is available for traces or samples again.
void BigBuf_free(void)
{
void BigBuf_free(void){
BigBuf_hi = BIGBUF_SIZE;
emulator_memory = NULL;
// shouldn't this empty BigBuf also?
}
// free allocated chunks EXCEPT the emulator memory
void BigBuf_free_keep_EM(void)
{
void BigBuf_free_keep_EM(void) {
if (emulator_memory != NULL)
BigBuf_hi = emulator_memory - (uint8_t *)BigBuf;
else
@ -105,8 +95,7 @@ void BigBuf_free_keep_EM(void)
// shouldn't this empty BigBuf also?
}
void BigBuf_print_status(void)
{
void BigBuf_print_status(void) {
Dbprintf("Memory");
Dbprintf(" BIGBUF_SIZE.............%d", BIGBUF_SIZE);
Dbprintf(" Available memory........%d", BigBuf_hi);
@ -116,12 +105,11 @@ void BigBuf_print_status(void)
}
// return the maximum trace length (i.e. the unallocated size of BigBuf)
uint16_t BigBuf_max_traceLen(void)
{
uint16_t BigBuf_max_traceLen(void) {
return BigBuf_hi;
}
void clear_trace() {
void clear_trace(void) {
traceLen = 0;
}
void set_tracelen(uint16_t value) {
@ -139,8 +127,7 @@ bool get_tracing(void) {
* Get the number of bytes traced
* @return
*/
uint16_t BigBuf_get_traceLen(void)
{
uint16_t BigBuf_get_traceLen(void) {
return traceLen;
}
@ -150,8 +137,7 @@ uint16_t BigBuf_get_traceLen(void)
by 'hf list raw', alternatively 'hf list <proto>' for protocol-specific
annotation of commands/responses.
**/
bool RAMFUNC LogTrace(const uint8_t *btBytes, uint16_t iLen, uint32_t timestamp_start, uint32_t timestamp_end, uint8_t *parity, bool readerToTag)
{
bool RAMFUNC LogTrace(const uint8_t *btBytes, uint16_t iLen, uint32_t timestamp_start, uint32_t timestamp_end, uint8_t *parity, bool readerToTag) {
if (!tracing) return false;
uint8_t *trace = BigBuf_get_addr();
@ -209,9 +195,7 @@ bool RAMFUNC LogTrace(const uint8_t *btBytes, uint16_t iLen, uint32_t timestamp_
return true;
}
int LogTraceHitag(const uint8_t * btBytes, int iBits, int iSamples, uint32_t dwParity, int readerToTag)
{
int LogTraceHitag(const uint8_t * btBytes, int iBits, int iSamples, uint32_t dwParity, int readerToTag) {
/**
Todo, rewrite the logger to use the generic functionality instead. It should be noted, however,
that this logger takes number of bits as argument, not number of bytes.
@ -252,15 +236,13 @@ int LogTraceHitag(const uint8_t * btBytes, int iBits, int iSamples, uint32_t dwP
return true;
}
// Emulator memory
uint8_t emlSet(uint8_t *data, uint32_t offset, uint32_t length){
uint8_t* mem = BigBuf_get_EM_addr();
if(offset+length < CARD_MEMORY_SIZE) {
if (offset + length < CARD_MEMORY_SIZE) {
memcpy(mem+offset, data, length);
return 0;
} else {
Dbprintf("Error, trying to set memory outside of bounds! %d > %d", (offset+length), CARD_MEMORY_SIZE);
return 1;
}
}
Dbprintf("Error, trying to set memory outside of bounds! %d > %d", (offset + length), CARD_MEMORY_SIZE);
return 1;
}

View file

@ -47,7 +47,7 @@ void RunMod() {
// Was our button held down or pressed?
int button_pressed = BUTTON_HELD(1000);
//SpinDelay(300);
SpinDelay(300);
// Button was held for a second, begin recording
if (button_pressed > 0 && cardRead == 0) {
@ -56,7 +56,7 @@ void RunMod() {
LED(LED_RED2, 0);
// record
DbpString("[+] starting recording");
DbpString("[=] starting recording");
// wait for button to be released
while(BUTTON_PRESS())
@ -66,7 +66,7 @@ void RunMod() {
SpinDelay(500);
CmdHIDdemodFSK(1, &high[selected], &low[selected], 0);
Dbprintf("[+] recorded %x %x %08x", selected, high[selected], low[selected]);
Dbprintf("[=] recorded %x %x %08x", selected, high[selected], low[selected]);
LEDsoff();
LED(selected + 1, 0);
@ -82,7 +82,7 @@ void RunMod() {
LED(LED_ORANGE, 0);
// record
Dbprintf("[+] cloning %x %x %08x", selected, high[selected], low[selected]);
Dbprintf("[=] cloning %x %x %08x", selected, high[selected], low[selected]);
// wait for button to be released
while(BUTTON_PRESS())
@ -92,7 +92,7 @@ void RunMod() {
SpinDelay(500);
CopyHIDtoT55x7(0, high[selected], low[selected], 0);
Dbprintf("[+] cloned %x %x %08x", selected, high[selected], low[selected]);
Dbprintf("[=] cloned %x %x %08x", selected, high[selected], low[selected]);
LEDsoff();
LED(selected + 1, 0);
@ -109,6 +109,7 @@ void RunMod() {
// Next option if we were previously playing
if (playing)
selected = (selected + 1) % OPTS;
playing = !playing;
LEDsoff();
@ -118,21 +119,18 @@ void RunMod() {
if (playing && selected != 2) {
LED(LED_GREEN, 0);
DbpString("[+] playing");
DbpString("[=] playing");
// wait for button to be released
while (BUTTON_PRESS())
WDT_HIT();
Dbprintf("[+] %x %x %08x", selected, high[selected], low[selected]);
Dbprintf("[=] %x %x %08x", selected, high[selected], low[selected]);
CmdHIDsimTAG(high[selected], low[selected], 0);
DbpString("[+] done playing");
DbpString("[=] done playing");
if (BUTTON_HELD(1000) > 0) {
DbpString("[+] exiting");
LEDsoff();
return;
}
if (BUTTON_HELD(1000) > 0)
goto out;
/* We pressed a button so ignore it here with a delay */
SpinDelay(300);
@ -166,18 +164,18 @@ void RunMod() {
uint32_t fc = ((high[selected] & 1 ) << 11 ) | (low[selected] >> 21);
uint32_t original_cardnum = cardnum;
Dbprintf("[+] Proxbrute - starting decrementing card number");
Dbprintf("[=] Proxbrute - starting decrementing card number");
while (cardnum >= 0) {
// Needed for exiting from proxbrute when button is pressed
if (BUTTON_PRESS()) {
if (BUTTON_HELD(1000) > 0) {
DbpString("[+] exiting");
LEDsoff();
return;
goto out;
} else {
while (BUTTON_PRESS()) { WDT_HIT(); }
while (BUTTON_PRESS()) {
WDT_HIT();
}
break;
}
}
@ -189,23 +187,21 @@ void RunMod() {
hid_corporate_1000_calculate_checksum_and_set(&high[selected], &low[selected], cardnum, fc);
// Print actual code to brute
Dbprintf("[+] TAG ID: %x%08x (%d) - FC: %u - Card: %u", high[selected], low[selected], (low[selected] >> 1) & 0xFFFF, fc, cardnum);
Dbprintf("[=] TAG ID: %x%08x (%d) - FC: %u - Card: %u", high[selected], low[selected], (low[selected] >> 1) & 0xFFFF, fc, cardnum);
CmdHIDsimTAGEx(high[selected], low[selected], 1, 50000);
}
cardnum = original_cardnum;
Dbprintf("[+] Proxbrute - starting incrementing card number");
Dbprintf("[=] Proxbrute - starting incrementing card number");
while (cardnum <= 0xFFFFF) {
// Needed for exiting from proxbrute when button is pressed
if (BUTTON_PRESS()) {
if (BUTTON_HELD(1000) > 0) {
DbpString("[+] exiting");
LEDsoff();
return;
goto out;
} else {
while (BUTTON_PRESS()) { WDT_HIT(); }
break;
@ -219,17 +215,14 @@ void RunMod() {
hid_corporate_1000_calculate_checksum_and_set(&high[selected], &low[selected], cardnum, fc);
// Print actual code to brute
Dbprintf("[+] TAG ID: %x%08x (%d) - FC: %u - Card: %u", high[selected], low[selected], (low[selected] >> 1) & 0xFFFF, fc, cardnum);
Dbprintf("[=] TAG ID: %x%08x (%d) - FC: %u - Card: %u", high[selected], low[selected], (low[selected] >> 1) & 0xFFFF, fc, cardnum);
CmdHIDsimTAGEx(high[selected], low[selected], 1, 50000);
}
DbpString("[+] done bruteforcing");
if (BUTTON_HELD(1000) > 0) {
DbpString("Exiting");
LEDsoff();
return;
}
DbpString("[=] done bruteforcing");
if (BUTTON_HELD(1000) > 0)
goto out;
/* We pressed a button so ignore it here with a delay */
SpinDelay(300);
@ -246,6 +239,10 @@ void RunMod() {
}
}
}
out:
DbpString("[=] exiting");
LEDsoff();
}
// Function that calculate next value for the brutforce of HID corporate 1000

View file

@ -32,7 +32,7 @@ void RunMod() {
// Was our button held down or pressed?
int button_pressed = BUTTON_HELD(1000);
//SpinDelay(300);
SpinDelay(300);
// Button was held for a second, begin recording
if (button_pressed > 0 && cardRead == 0) {
@ -41,7 +41,7 @@ void RunMod() {
LED(LED_RED2, 0);
// record
DbpString("[+] starting recording");
DbpString("[=] starting recording");
// wait for button to be released
while (BUTTON_PRESS())
@ -51,7 +51,7 @@ void RunMod() {
SpinDelay(500);
CmdHIDdemodFSK(1, &high[selected], &low[selected], 0);
Dbprintf("[+] recorded %x %x %08x", selected, high[selected], low[selected]);
Dbprintf("[=] recorded %x %x %08x", selected, high[selected], low[selected]);
LEDsoff();
LED(selected + 1, 0);
@ -67,7 +67,7 @@ void RunMod() {
LED(LED_ORANGE, 0);
// record
Dbprintf("[+] cloning %x %x %08x", selected, high[selected], low[selected]);
Dbprintf("[=] cloning %x %x %08x", selected, high[selected], low[selected]);
// wait for button to be released
while (BUTTON_PRESS())
@ -77,7 +77,7 @@ void RunMod() {
SpinDelay(500);
CopyHIDtoT55x7(0, high[selected], low[selected], 0);
Dbprintf("[+] cloned %x %x %08x", selected, high[selected], low[selected]);
Dbprintf("[=] cloned %x %x %08x", selected, high[selected], low[selected]);
LEDsoff();
LED(selected + 1, 0);
@ -102,7 +102,7 @@ void RunMod() {
// Begin transmitting
if (playing) {
LED(LED_GREEN, 0);
DbpString("[+] playing");
DbpString("[=] playing");
// wait for button to be released
while (BUTTON_PRESS())
WDT_HIT();
@ -120,7 +120,7 @@ void RunMod() {
*/
if ( selected == 1 ) {
DbpString("[=] entering ProxBrute Mode");
Dbprintf("[+] current Tag: Selected = %x Facility = %08x ID = %08x", selected, high[selected], low[selected]);
Dbprintf("[=] current Tag: Selected = %x Facility = %08x ID = %08x", selected, high[selected], low[selected]);
LED(LED_ORANGE, 0);
LED(LED_RED, 0);
for (uint16_t i = low[selected]-1; i > 0; i--) {
@ -135,20 +135,17 @@ void RunMod() {
}
} else {
DbpString("[+] RED is lit, not entering ProxBrute Mode");
Dbprintf("[+] %x %x %x", selected, high[selected], low[selected]);
DbpString("[=] RED is lit, not entering ProxBrute Mode");
Dbprintf("[=] %x %x %x", selected, high[selected], low[selected]);
CmdHIDsimTAGEx(high[selected], low[selected], 0, 20000);
DbpString("[+] done playing");
DbpString("[=] done playing");
}
/* END PROXBRUTE */
if (BUTTON_HELD(1000) > 0) {
DbpString("[+] exiting");
LEDsoff();
return;
}
if (BUTTON_HELD(1000) > 0)
goto out;
/* We pressed a button so ignore it here with a delay */
SpinDelay(300);
@ -165,4 +162,7 @@ void RunMod() {
}
}
}
out:
DbpString("[=] exiting");
LEDsoff();
}

View file

@ -19,7 +19,7 @@ void RunMod() {
int selected = 0;
int playing = 0;
int cardRead = 0;
bool gotCard;
// Turn on selected LED
LED(selected + 1, 0);
@ -31,7 +31,9 @@ void RunMod() {
// Was our button held down or pressed?
int button_pressed = BUTTON_HELD(1000);
//SpinDelay(300);
Dbprintf("button %d", button_pressed);
SpinDelay(300);
// Button was held for a second, begin recording
if (button_pressed > 0 && cardRead == 0) {
@ -40,7 +42,7 @@ void RunMod() {
LED(LED_RED2, 0);
// record
DbpString("[+] starting recording");
DbpString("[=] starting recording");
// wait for button to be released
while (BUTTON_PRESS())
@ -50,7 +52,7 @@ void RunMod() {
SpinDelay(500);
CmdHIDdemodFSK(1, &high[selected], &low[selected], 0);
Dbprintf("[+] recorded %x %x %08x", selected, high[selected], low[selected]);
Dbprintf("[=] recorded bank %x | %x %08x", selected, high[selected], low[selected]);
LEDsoff();
LED(selected + 1, 0);
@ -58,7 +60,9 @@ void RunMod() {
// If we were previously playing, set playing off
// so next button push begins playing what we recorded
playing = 0;
cardRead = 1;
cardRead = 1;
gotCard = true;
}
else if (button_pressed > 0 && cardRead == 1) {
LEDsoff();
@ -66,7 +70,7 @@ void RunMod() {
LED(LED_ORANGE, 0);
// record
Dbprintf("[+] cloning %x %x %08x", selected, high[selected], low[selected]);
Dbprintf("[=] cloning %x %x %08x", selected, high[selected], low[selected]);
// wait for button to be released
while (BUTTON_PRESS())
@ -76,7 +80,7 @@ void RunMod() {
SpinDelay(500);
CopyHIDtoT55x7(0, high[selected], low[selected], 0);
Dbprintf("[+] cloned %x %x %08x", selected, high[selected], low[selected]);
Dbprintf("[=] cloned %x %x %08x", selected, high[selected], low[selected]);
LEDsoff();
LED(selected + 1, 0);
@ -89,10 +93,11 @@ void RunMod() {
}
// Change where to record (or begin playing)
else if (button_pressed) {
else if (button_pressed && gotCard) {
// Next option if we were previously playing
if (playing)
selected = (selected + 1) % OPTS;
playing = !playing;
LEDsoff();
@ -100,21 +105,20 @@ void RunMod() {
// Begin transmitting
if (playing) {
LED(LED_GREEN, 0);
DbpString("[+] playing");
DbpString("[=] playing");
// wait for button to be released
while (BUTTON_PRESS())
WDT_HIT();
Dbprintf("[+] %x %x %08x", selected, high[selected], low[selected]);
Dbprintf("[=] %x %x %08x", selected, high[selected], low[selected]);
CmdHIDsimTAG(high[selected], low[selected], false);
DbpString("[+] done playing");
DbpString("[=] done playing");
if (BUTTON_HELD(1000) > 0) {
DbpString("[+] exiting");
LEDsoff();
return;
}
if (BUTTON_HELD(1000) > 0)
goto out;
/* We pressed a button so ignore it here with a delay */
SpinDelay(300);
@ -131,4 +135,8 @@ void RunMod() {
}
}
}
out:
DbpString("[=] exiting");
LEDsoff();
}

View file

@ -6,8 +6,25 @@ If you want to implement a new standalone mode, you need to implement the method
## Implementing a standalone mode
Each standalone mod needs to have its own compiler flag to be added in `armsrc\makefile` and inside the function `AppMain` inside AppMain.c. Inside Appmain a call to RunMod is needed. It looks strange because of what kinds of dependencies your mode will have.
The RunMod function is your "main" function when running. You need to check for Usb commands, in order to let the pm3 client break the standalone mode.
Each standalone mod needs to have its own compiler flag to be added in `armsrc\makefile` and inside the function `AppMain` inside AppMain.c. Inside Appmain a call to RunMod is needed. It looks strange because of what kinds of dependencies your mode will have.
The RunMod function is your "main" function when running. You need to check for Usb commands, in order to let the pm3 client break the standalone mode. See this basic skeleton of main function RunMod().
````
void RunMod() {
// led show
StandAloneMode();
FpgaDownloadAndGo(FPGA_BITSTREAM_LF);
// main loop
for (;;) {
WDT_HIT();
// exit from standalone mode, just send a usbcommand
if (usb_poll_validate_length()) break;
// do your standalone stuff..
}
````
As it is now, you can only have one standalone mode installed at the time.
@ -15,14 +32,71 @@ As it is now, you can only have one standalone mode installed at the time.
Use HF/LF to denote which frequence your mod is targeting.
Use you own github name/similar for perpetual honour to denote your mod
Samples:
Samples of directive flag used in the `armsrc\makefile`:
```
### -DWITH_LF_ICERUN
### -DWITH_LF_SAMYRUN
### -DWITH_LF_PROXBRUTE
### -DWITH_LF_HIDBRUTE
### -DWITH_HF_COLIN
### -DWITH_HF_YOUNG
### -DWITH_HF_MATTYRUN
```
Add your source code file like the following sample in the `armsrc\makefile`
```
# WITH_HF_COLIN
ifneq (,$(findstring WITH_HF_COLIN,$(APP_CFLAGS)))
SRC_STANDALONE = hf_colin.c vtsend.c
else
SRC_STANDALONE =
endif
```
## Adding identification of your mode
Do please add a identification string in the function `printStandAloneModes` inside `armsrc\appmain.c`
This will enable an easy way to detect on client side which standalone mods has been installed on the device.
```
#if defined(WITH_HF_COLIN)
DbpString(" HF Mifare ultra fast sniff/sim/clone - aka VIGIKPWN (Colin Brigato)");
#endif
````
Once all this is done, you and others can now easily compile different standalone modes by just swapping the -D directive in `armsrc\makefile`
````
#remove one of the following defines and comment out the relevant line
#in the next section to remove that particular feature from compilation.
# NO space,TABs after the "\" sign.
APP_CFLAGS = -DWITH_CRC \
-DON_DEVICE \
-DWITH_LF \
-DWITH_HITAG \
-DWITH_ISO15693 \
-DWITH_LEGICRF \
-DWITH_ISO14443b \
-DWITH_ISO14443a \
-DWITH_ICLASS \
-DWITH_FELICA \
-DWITH_FLASH \
-DWITH_SMARTCARD \
-DWITH_HFSNOOP \
-DWITH_HF_COLIN\
-DWITH_FPC \
-fno-strict-aliasing -ffunction-sections -fdata-sections
### IMPORTANT - move the commented variable below this line
# -DWITH_LCD \
# -DWITH_EMV \
# -DWITH_FPC \
#
# Standalone Mods
#-------------------------------------------------------
# -DWITH_LF_ICERUN
# -DWITH_LF_SAMYRUN
# -DWITH_LF_PROXBRUTE
# -DWITH_LF_HIDBRUTE
# -DWITH_HF_YOUNG
# -DWITH_HF_MATTYRUN
# -DWITH_HF_COLIN
````

View file

@ -27,10 +27,10 @@
#endif
#define START_GAP 31*8 // was 250 // SPEC: 1*8 to 50*8 - typ 15*8 (15fc)
#define WRITE_GAP 20*8 // was 160 // SPEC: 1*8 to 20*8 - typ 10*8 (10fc)
#define WRITE_0 18*8 // was 144 // SPEC: 16*8 to 32*8 - typ 24*8 (24fc)
#define WRITE_1 50*8 // was 400 // SPEC: 48*8 to 64*8 - typ 56*8 (56fc) 432 for T55x7; 448 for E5550
#define START_GAP 48*8 // was 250 // SPEC: 1*8 to 50*8 - typ 15*8 (15fc)
#define WRITE_GAP 18*8 // was 160 // SPEC: 1*8 to 20*8 - typ 10*8 (10fc)
#define WRITE_0 24*8 // was 144 // SPEC: 16*8 to 32*8 - typ 24*8 (24fc)
#define WRITE_1 54*8 // was 400 // SPEC: 48*8 to 64*8 - typ 56*8 (56fc) 432 for T55x7; 448 for E5550
#define READ_GAP 15*8
// VALUES TAKEN FROM EM4x function: SendForward
@ -474,10 +474,10 @@ void WriteTItag(uint32_t idhi, uint32_t idlo, uint16_t crc)
StopTicks();
}
// note: a call to FpgaDownloadAndGo(FPGA_BITSTREAM_LF) must be done before, but
// this may destroy the bigbuf so be sure this is called before calling SimulateTagLowFrequencyEx
void SimulateTagLowFrequencyEx(int period, int gap, int ledcontrol, int numcycles) {
// note this may destroy the bigbuf so be sure this is called before now...
//FpgaDownloadAndGo(FPGA_BITSTREAM_LF);
//FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_EDGE_DETECT | FPGA_LF_EDGE_DETECT_TOGGLE_MODE );
FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_EDGE_DETECT);
SpinDelay(20);
@ -514,7 +514,7 @@ void SimulateTagLowFrequencyEx(int period, int gap, int ledcontrol, int numcycle
// wait until SSC_CLK goes HIGH
// used as a simple detection of a reader field?
while(!(AT91C_BASE_PIOA->PIO_PDSR & GPIO_SSC_CLK)) {
while (!(AT91C_BASE_PIOA->PIO_PDSR & GPIO_SSC_CLK)) {
WDT_HIT();
if ( usb_poll_validate_length() || BUTTON_PRESS() )
goto OUT;
@ -526,7 +526,7 @@ void SimulateTagLowFrequencyEx(int period, int gap, int ledcontrol, int numcycle
SHORT_COIL();
//wait until SSC_CLK goes LOW
while(AT91C_BASE_PIOA->PIO_PDSR & GPIO_SSC_CLK) {
while (AT91C_BASE_PIOA->PIO_PDSR & GPIO_SSC_CLK) {
WDT_HIT();
//if ( usb_poll_validate_length() || BUTTON_PRESS() )
if ( BUTTON_PRESS() )
@ -918,7 +918,7 @@ void CmdHIDdemodFSK(int findone, uint32_t *high, uint32_t *low, int ledcontrol)
idx = HIDdemodFSK(dest, &size, &hi2, &hi, &lo, &dummyIdx);
if ( idx < 0 ) continue;
if (idx>0 && lo>0 && (size==96 || size==192)){
if (idx > 0 && lo > 0 && (size == 96 || size == 192)){
// go over previously decoded manchester data and decode into usable tag ID
if (hi2 != 0){ //extra large HID tags 88/192 bits
Dbprintf("TAG ID: %x%08x%08x (%d)",
@ -979,7 +979,6 @@ void CmdHIDdemodFSK(int findone, uint32_t *high, uint32_t *low, int ledcontrol)
);
}
if (findone){
if (ledcontrol) LED_A_OFF();
*high = hi;
*low = lo;
break;
@ -1007,7 +1006,7 @@ void CmdAWIDdemodFSK(int findone, uint32_t *high, uint32_t *low, int ledcontrol)
LFSetupFPGAForADC(95, true);
while(!BUTTON_PRESS() && !usb_poll_validate_length()) {
while (!BUTTON_PRESS() && !usb_poll_validate_length()) {
WDT_HIT();
if (ledcontrol) LED_A_ON();
@ -1107,6 +1106,7 @@ void CmdEM410xdemod(int findone, uint32_t *high, uint64_t *low, int ledcontrol)
if (ledcontrol) LED_A_ON();
DoAcquisition_default(-1, true);
size = BigBuf_max_traceLen();
//askdemod and manchester decode
if (size > 16385) size = 16385; //big enough to catch 2 sequences of largest format
@ -1116,7 +1116,7 @@ void CmdEM410xdemod(int findone, uint32_t *high, uint64_t *low, int ledcontrol)
if (errCnt < 0) continue;
errCnt = Em410xDecode(dest, &size, &idx, &hi, &lo);
if (errCnt){
if (errCnt == 1){
if (size == 128){
Dbprintf("EM XL TAG ID: %06x%08x%08x - (%05d_%03d_%08d)",
hi,
@ -1169,7 +1169,9 @@ void CmdIOdemodFSK(int findone, uint32_t *high, uint32_t *low, int ledcontrol) {
while (!BUTTON_PRESS() && !usb_poll_validate_length()) {
WDT_HIT();
if (ledcontrol) LED_A_ON();
DoAcquisition_default(-1,true);
DoAcquisition_default(-1, true);
//fskdemod and get start index
WDT_HIT();
idx = detectIOProx(dest, &size, &dummyIdx);

View file

@ -202,6 +202,10 @@ uint32_t DoAcquisition(uint8_t decimation, uint32_t bits_per_sample, bool averag
Dbprintf("buffer samples: %02x %02x %02x %02x %02x %02x %02x %02x ...",
dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]);
}
// Ensure that noise check is performed for any device-side processing
justNoise(dest, bufsize);
return data.numbits;
}
/**

View file

@ -237,14 +237,14 @@ int usage_data_fsktonrz() {
//set the demod buffer with given array of binary (one bit per byte)
//by marshmellow
void setDemodBuf(uint8_t *buf, size_t size, size_t startIdx) {
void setDemodBuf(uint8_t *buf, size_t size, size_t start_idx) {
if (buf == NULL) return;
if ( size > MAX_DEMOD_BUF_LEN - startIdx)
size = MAX_DEMOD_BUF_LEN - startIdx;
if ( size > MAX_DEMOD_BUF_LEN - start_idx)
size = MAX_DEMOD_BUF_LEN - start_idx;
for (size_t i = 0; i < size; i++)
DemodBuffer[i] = buf[startIdx++];
DemodBuffer[i] = buf[start_idx++];
DemodBufferLen = size;
}
@ -346,10 +346,11 @@ void save_restoreDB(uint8_t saveOpt) {
memcpy(SavedDB, DemodBuffer, sizeof(DemodBuffer));
SavedDBlen = DemodBufferLen;
DB_Saved=true;
DB_Saved = true;
savedDemodStartIdx = g_DemodStartIdx;
savedDemodClock = g_DemodClock;
} else if (DB_Saved) { //restore
memcpy(DemodBuffer, SavedDB, sizeof(DemodBuffer));
DemodBufferLen = SavedDBlen;
g_DemodClock = savedDemodClock;
@ -422,10 +423,12 @@ int CmdPrintDemodBuff(const char *Cmd) {
char *buf = (char *) (DemodBuffer + offset);
numBits = (numBits > sizeof(hex)) ? sizeof(hex) : numBits;
numBits = binarraytohex(hex, buf, numBits);
if (numBits==0) return 0;
PrintAndLogEx(NORMAL, "DemodBuffer: %s",hex);
if (numBits == 0) {
return 0;
}
PrintAndLogEx(NORMAL, "DemodBuffer: %s", hex);
} else {
PrintAndLogEx(NORMAL, "DemodBuffer:\n%s", sprint_bin_break(DemodBuffer+offset,numBits,16));
PrintAndLogEx(NORMAL, "DemodBuffer:\n%s", sprint_bin_break(DemodBuffer+offset, numBits, 16));
}
return 1;
}
@ -544,14 +547,14 @@ int ASKDemod(const char *Cmd, bool verbose, bool emSearch, uint8_t askType) {
//prints binary found and saves in graphbuffer for further commands
int Cmdaskmandemod(const char *Cmd)
{
char cmdp = param_getchar(Cmd, 0);
if (strlen(Cmd) > 45 || cmdp == 'h' || cmdp == 'H') return usage_data_rawdemod_am();
char cmdp = tolower(param_getchar(Cmd, 0));
if (strlen(Cmd) > 45 || cmdp == 'h') return usage_data_rawdemod_am();
bool st = true;
if (Cmd[0]=='s')
if (Cmd[0] == 's')
return ASKDemod_ext(Cmd++, true, true, 1, &st);
else if (Cmd[1] == 's')
return ASKDemod_ext(Cmd+=2, true, true, 1, &st);
return ASKDemod_ext(Cmd += 2, true, true, 1, &st);
return ASKDemod(Cmd, true, true, 1);
}
@ -559,27 +562,26 @@ int Cmdaskmandemod(const char *Cmd)
//by marshmellow
//manchester decode
//stricktly take 10 and 01 and convert to 0 and 1
int Cmdmandecoderaw(const char *Cmd)
{
int i = 0;
int errCnt = 0;
int Cmdmandecoderaw(const char *Cmd) {
size_t size = 0;
int invert = 0;
int maxErr = 20;
char cmdp = param_getchar(Cmd, 0);
if (strlen(Cmd) > 5 || cmdp == 'h' || cmdp == 'H') return usage_data_manrawdecode();
if (DemodBufferLen==0) return 0;
uint8_t BitStream[MAX_DEMOD_BUF_LEN]={0};
int high = 0, low = 0;
int i = 0, errCnt = 0, invert = 0, maxErr = 20;
char cmdp = tolower(param_getchar(Cmd, 0));
if (strlen(Cmd) > 5 || cmdp == 'h') return usage_data_manrawdecode();
if (DemodBufferLen == 0) return 0;
uint8_t bits[MAX_DEMOD_BUF_LEN] = {0};
for (; i < DemodBufferLen; ++i){
if (DemodBuffer[i] > high)
high=DemodBuffer[i];
high = DemodBuffer[i];
else if(DemodBuffer[i] < low)
low=DemodBuffer[i];
BitStream[i] = DemodBuffer[i];
low = DemodBuffer[i];
bits[i] = DemodBuffer[i];
}
if (high>7 || low <0 ){
if (high > 7 || low < 0 ){
PrintAndLogEx(WARNING, "Error: please raw demod the wave first then manchester raw decode");
return 0;
}
@ -587,20 +589,22 @@ int Cmdmandecoderaw(const char *Cmd)
sscanf(Cmd, "%i %i", &invert, &maxErr);
size = i;
uint8_t alignPos = 0;
errCnt = manrawdecode(BitStream, &size, invert, &alignPos);
errCnt = manrawdecode(bits, &size, invert, &alignPos);
if (errCnt >= maxErr){
PrintAndLogEx(WARNING, "Too many errors: %d",errCnt);
return 0;
}
PrintAndLogEx(NORMAL, "Manchester Decoded - # errors:%d - data:",errCnt);
PrintAndLogEx(NORMAL, "%s", sprint_bin_break(BitStream, size, 16));
PrintAndLogEx(NORMAL, "%s", sprint_bin_break(bits, size, 16));
if (errCnt == 0){
uint64_t id = 0;
uint32_t hi = 0;
size_t idx=0;
if (Em410xDecode(BitStream, &size, &idx, &hi, &id)){
size_t idx = 0;
if (Em410xDecode(bits, &size, &idx, &hi, &id) == 1){
//need to adjust to set bitstream back to manchester encoded data
//setDemodBuf(BitStream, size, idx);
//setDemodBuf(bits, size, idx);
printEM410x(hi, id);
}
}
@ -613,22 +617,23 @@ int Cmdmandecoderaw(const char *Cmd)
//takes 2 arguments "offset" default = 0 if 1 it will shift the decode by one bit
// and "invert" default = 0 if 1 it will invert output
// the argument offset allows us to manually shift if the output is incorrect - [EDIT: now auto detects]
int CmdBiphaseDecodeRaw(const char *Cmd)
{
size_t size=0;
int offset=0, invert=0, maxErr=20, errCnt=0;
char cmdp = param_getchar(Cmd, 0);
if (strlen(Cmd) > 3 || cmdp == 'h' || cmdp == 'H') return usage_data_biphaserawdecode();
int CmdBiphaseDecodeRaw(const char *Cmd) {
size_t size = 0;
int offset = 0, invert = 0, maxErr = 20, errCnt = 0;
char cmdp = tolower(param_getchar(Cmd, 0));
if (strlen(Cmd) > 3 || cmdp == 'h') return usage_data_biphaserawdecode();
sscanf(Cmd, "%i %i %i", &offset, &invert, &maxErr);
if (DemodBufferLen==0){
if (DemodBufferLen == 0){
PrintAndLogEx(NORMAL, "DemodBuffer Empty - run 'data rawdemod ar' first");
return 0;
}
uint8_t BitStream[MAX_DEMOD_BUF_LEN]={0};
size = sizeof(BitStream);
if ( !getDemodBuf(BitStream, &size) ) return 0;
errCnt=BiphaseRawDecode(BitStream, &size, &offset, invert);
uint8_t bits[MAX_DEMOD_BUF_LEN] = {0};
size = sizeof(bits);
if ( !getDemodBuf(bits, &size) ) return 0;
errCnt = BiphaseRawDecode(bits, &size, &offset, invert);
if (errCnt < 0){
PrintAndLogEx(WARNING, "Error during decode:%d", errCnt);
return 0;
@ -642,10 +647,12 @@ int CmdBiphaseDecodeRaw(const char *Cmd)
PrintAndLogEx(WARNING, "# Errors found during Demod (shown as 7 in bit stream): %d",errCnt);
PrintAndLogEx(NORMAL, "Biphase Decoded using offset: %d - # invert:%d - data:",offset,invert);
PrintAndLogEx(NORMAL, "%s", sprint_bin_break(BitStream, size, 16));
PrintAndLogEx(NORMAL, "%s", sprint_bin_break(bits, size, 16));
//remove first bit from raw demod
if (offset)
setDemodBuf(DemodBuffer,DemodBufferLen-offset, offset); //remove first bit from raw demod
setDemodBuf(DemodBuffer,DemodBufferLen-offset, offset);
setClockGrid(g_DemodClock, g_DemodStartIdx + g_DemodClock*offset/2);
return 1;
}
@ -1040,21 +1047,19 @@ int FSKrawDemod(const char *Cmd, bool verbose)
//fsk raw demod and print binary
//takes 4 arguments - Clock, invert, fchigh, fclow
//defaults: clock = 50, invert=1, fchigh=10, fclow=8 (RF/10 RF/8 (fsk2a))
int CmdFSKrawdemod(const char *Cmd)
{
char cmdp = param_getchar(Cmd, 0);
if (strlen(Cmd) > 20 || cmdp == 'h' || cmdp == 'H') return usage_data_rawdemod_fs();
int CmdFSKrawdemod(const char *Cmd) {
char cmdp = tolower(param_getchar(Cmd, 0));
if (strlen(Cmd) > 20 || cmdp == 'h') return usage_data_rawdemod_fs();
return FSKrawDemod(Cmd, true);
}
//by marshmellow
//attempt to psk1 demod graph buffer
int PSKDemod(const char *Cmd, bool verbose)
{
int PSKDemod(const char *Cmd, bool verbose) {
int invert = 0, clk = 0, maxErr = 100;
sscanf(Cmd, "%i %i %i", &clk, &invert, &maxErr);
if (clk == 1){
if (clk == 1) {
invert = 1;
clk=0;
}
@ -1072,14 +1077,14 @@ int PSKDemod(const char *Cmd, bool verbose)
if (g_debugMode || verbose) PrintAndLogEx(DEBUG, "DEBUG: (PSKdemod) Too many errors found, clk: %d, invert: %d, numbits: %d, errCnt: %d", clk, invert, BitLen, errCnt);
return 0;
}
if (errCnt<0|| BitLen<16){ //throw away static - allow 1 and -1 (in case of threshold command first)
if (errCnt < 0|| BitLen < 16){ //throw away static - allow 1 and -1 (in case of threshold command first)
if (g_debugMode || verbose) PrintAndLogEx(DEBUG, "DEBUG: (PSKdemod) no data found, clk: %d, invert: %d, numbits: %d, errCnt: %d", clk, invert, BitLen, errCnt);
return 0;
}
if (verbose || g_debugMode){
PrintAndLogEx(DEBUG, "DEBUG: (PSKdemod) Using Clock:%d, invert:%d, Bits Found:%d",clk,invert,BitLen);
PrintAndLogEx(DEBUG, "DEBUG: (PSKdemod) Using Clock:%d, invert:%d, Bits Found:%d",clk, invert, BitLen);
if (errCnt > 0){
PrintAndLogEx(DEBUG, "DEBUG: (PSKdemod) errors during Demoding (shown as 7 in bit stream): %d",errCnt);
PrintAndLogEx(DEBUG, "DEBUG: (PSKdemod) errors during Demoding (shown as 7 in bit stream): %d", errCnt);
}
}
//prime demod buffer for output
@ -1103,8 +1108,10 @@ int CmdPSKIdteck(const char *Cmd) {
if (idx == -1)
PrintAndLogEx(DEBUG, "DEBUG: Error - Idteck: not enough samples");
else if (idx == -2)
PrintAndLogEx(DEBUG, "DEBUG: Error - Idteck: preamble not found");
PrintAndLogEx(DEBUG, "DEBUG: Error - Idteck: just noise");
else if (idx == -3)
PrintAndLogEx(DEBUG, "DEBUG: Error - Idteck: preamble not found");
else if (idx == -4)
PrintAndLogEx(DEBUG, "DEBUG: Error - Idteck: size not correct: %d", size);
else
PrintAndLogEx(DEBUG, "DEBUG: Error - Idteck: idx: %d",idx);
@ -1120,8 +1127,10 @@ int CmdPSKIdteck(const char *Cmd) {
if (idx == -1)
PrintAndLogEx(DEBUG, "DEBUG: Error - Idteck: not enough samples");
else if (idx == -2)
PrintAndLogEx(DEBUG, "DEBUG: Error - Idteck: preamble not found");
PrintAndLogEx(DEBUG, "DEBUG: Error - Idteck: just noise");
else if (idx == -3)
PrintAndLogEx(DEBUG, "DEBUG: Error - Idteck: preamble not found");
else if (idx == -4)
PrintAndLogEx(DEBUG, "DEBUG: Error - Idteck: size not correct: %d", size);
else
PrintAndLogEx(DEBUG, "DEBUG: Error - Idteck: idx: %d",idx);
@ -1148,41 +1157,42 @@ int CmdPSKIdteck(const char *Cmd) {
// takes 3 arguments - clock, invert, maxErr as integers
// attempts to demodulate nrz only
// prints binary found and saves in demodbuffer for further commands
int NRZrawDemod(const char *Cmd, bool verbose)
{
int invert=0;
int clk=0;
int maxErr=100;
int NRZrawDemod(const char *Cmd, bool verbose) {
int errCnt = 0, clkStartIdx = 0;
int invert = 0, clk = 0, maxErr = 100;
sscanf(Cmd, "%i %i %i", &clk, &invert, &maxErr);
if (clk==1){
invert=1;
clk=0;
if (clk == 1){
invert = 1;
clk = 0;
}
if (invert != 0 && invert != 1) {
PrintAndLogEx(WARNING, "(NRZrawDemod) Invalid argument: %s", Cmd);
return 0;
}
uint8_t BitStream[MAX_GRAPH_TRACE_LEN]={0};
size_t BitLen = getFromGraphBuf(BitStream);
if (BitLen==0) return 0;
int errCnt=0;
int clkStartIdx = 0;
errCnt = nrzRawDemod(BitStream, &BitLen, &clk, &invert, &clkStartIdx);
uint8_t bits[MAX_GRAPH_TRACE_LEN] = {0};
size_t BitLen = getFromGraphBuf(bits);
if (BitLen == 0) return 0;
errCnt = nrzRawDemod(bits, &BitLen, &clk, &invert, &clkStartIdx);
if (errCnt > maxErr){
PrintAndLogEx(DEBUG, "DEBUG: (NRZrawDemod) Too many errors found, clk: %d, invert: %d, numbits: %d, errCnt: %d",clk,invert,BitLen,errCnt);
return 0;
}
if (errCnt<0 || BitLen<16){ //throw away static - allow 1 and -1 (in case of threshold command first)
PrintAndLogEx(DEBUG, "DEBUG: (NRZrawDemod) no data found, clk: %d, invert: %d, numbits: %d, errCnt: %d",clk,invert,BitLen,errCnt);
if (errCnt < 0 || BitLen < 16){ //throw away static - allow 1 and -1 (in case of threshold command first)
PrintAndLogEx(DEBUG, "DEBUG: (NRZrawDemod) no data found, clk: %d, invert: %d, numbits: %d, errCnt: %d", clk, invert, BitLen, errCnt);
return 0;
}
if (verbose || g_debugMode) PrintAndLogEx(DEBUG, "DEBUG: (NRZrawDemod) Tried NRZ Demod using Clock: %d - invert: %d - Bits Found: %d",clk,invert,BitLen);
if (verbose || g_debugMode) PrintAndLogEx(DEBUG, "DEBUG: (NRZrawDemod) Tried NRZ Demod using Clock: %d - invert: %d - Bits Found: %d", clk, invert, BitLen);
//prime demod buffer for output
setDemodBuf(BitStream,BitLen,0);
setDemodBuf(bits, BitLen, 0);
setClockGrid(clk, clkStartIdx);
if (errCnt>0 && (verbose || g_debugMode)) PrintAndLogEx(DEBUG, "DEBUG: (NRZrawDemod) Errors during Demoding (shown as 7 in bit stream): %d",errCnt);
if (errCnt > 0 && (verbose || g_debugMode)) PrintAndLogEx(DEBUG, "DEBUG: (NRZrawDemod) Errors during Demoding (shown as 7 in bit stream): %d", errCnt);
if (verbose || g_debugMode) {
PrintAndLogEx(NORMAL, "NRZ demoded bitstream:");
// Now output the bitstream to the scrollback by line of 16 bits
@ -1191,10 +1201,9 @@ int NRZrawDemod(const char *Cmd, bool verbose)
return 1;
}
int CmdNRZrawDemod(const char *Cmd)
{
char cmdp = param_getchar(Cmd, 0);
if (strlen(Cmd) > 16 || cmdp == 'h' || cmdp == 'H') return usage_data_rawdemod_nr();
int CmdNRZrawDemod(const char *Cmd) {
char cmdp = tolower(param_getchar(Cmd, 0));
if (strlen(Cmd) > 16 || cmdp == 'h') return usage_data_rawdemod_nr();
return NRZrawDemod(Cmd, true);
}
@ -1203,13 +1212,11 @@ int CmdNRZrawDemod(const char *Cmd)
// takes 3 arguments - clock, invert, maxErr as integers
// attempts to demodulate psk only
// prints binary found and saves in demodbuffer for further commands
int CmdPSK1rawDemod(const char *Cmd)
{
int ans;
char cmdp = param_getchar(Cmd, 0);
if (strlen(Cmd) > 16 || cmdp == 'h' || cmdp == 'H') return usage_data_rawdemod_p1();
int CmdPSK1rawDemod(const char *Cmd) {
char cmdp = tolower(param_getchar(Cmd, 0));
if (strlen(Cmd) > 16 || cmdp == 'h') return usage_data_rawdemod_p1();
ans = PSKDemod(Cmd, true);
int ans = PSKDemod(Cmd, true);
//output
if (!ans){
if (g_debugMode) PrintAndLogEx(WARNING, "Error demoding: %d",ans);
@ -1223,13 +1230,11 @@ int CmdPSK1rawDemod(const char *Cmd)
// by marshmellow
// takes same args as cmdpsk1rawdemod
int CmdPSK2rawDemod(const char *Cmd)
{
int ans = 0;
char cmdp = param_getchar(Cmd, 0);
if (strlen(Cmd) > 16 || cmdp == 'h' || cmdp == 'H') return usage_data_rawdemod_p2();
int CmdPSK2rawDemod(const char *Cmd) {
char cmdp = tolower(param_getchar(Cmd, 0));
if (strlen(Cmd) > 16 || cmdp == 'h') return usage_data_rawdemod_p2();
ans = PSKDemod(Cmd, true);
int ans = PSKDemod(Cmd, true);
if (!ans){
if (g_debugMode) PrintAndLogEx(WARNING, "Error demoding: %d",ans);
return 0;
@ -1242,31 +1247,22 @@ int CmdPSK2rawDemod(const char *Cmd)
}
// by marshmellow - combines all raw demod functions into one menu command
int CmdRawDemod(const char *Cmd)
{
char cmdp = Cmd[0]; //param_getchar(Cmd, 0);
char cmdp2 = Cmd[1];
int CmdRawDemod(const char *Cmd) {
int ans = 0;
if (strlen(Cmd) > 35 || cmdp == 'h' || cmdp == 'H' || strlen(Cmd) < 2)
if (strlen(Cmd) > 35 || strlen(Cmd) < 2)
return usage_data_rawdemod();
str_lower( (char *)Cmd);
if (cmdp == 'f' && cmdp2 == 's')
ans = CmdFSKrawdemod(Cmd+2);
else if(cmdp == 'a' && cmdp2 == 'b')
ans = Cmdaskbiphdemod(Cmd+2);
else if(cmdp == 'a' && cmdp2 == 'm')
ans = Cmdaskmandemod(Cmd+2);
else if(cmdp == 'a' && cmdp2 == 'r')
ans = Cmdaskrawdemod(Cmd+2);
else if(cmdp == 'n' && cmdp2 == 'r')
ans = CmdNRZrawDemod(Cmd+2);
else if(cmdp == 'p' && cmdp2 == '1')
ans = CmdPSK1rawDemod(Cmd+2);
else if(cmdp == 'p' && cmdp2 == '2')
ans = CmdPSK2rawDemod(Cmd+2);
else
PrintAndLogEx(WARNING, "Unknown modulation entered - see help ('h') for parameter structure");
if (str_startswith(Cmd, "fs")) ans = CmdFSKrawdemod(Cmd+2);
else if(str_startswith(Cmd, "ab")) ans = Cmdaskbiphdemod(Cmd+2);
else if(str_startswith(Cmd, "am")) ans = Cmdaskmandemod(Cmd+2);
else if(str_startswith(Cmd, "ar")) ans = Cmdaskrawdemod(Cmd+2);
else if(str_startswith(Cmd, "nr")) ans = CmdNRZrawDemod(Cmd+2);
else if(str_startswith(Cmd, "p1")) ans = CmdPSK1rawDemod(Cmd+2);
else if(str_startswith(Cmd, "p2")) ans = CmdPSK2rawDemod(Cmd+2);
else PrintAndLogEx(WARNING, "Unknown modulation entered - see help ('h') for parameter structure");
return ans;
}
@ -1295,8 +1291,7 @@ void setClockGrid(int clk, int offset) {
}
}
int CmdGrid(const char *Cmd)
{
int CmdGrid(const char *Cmd) {
sscanf(Cmd, "%i %i", &PlotGridX, &PlotGridY);
PlotGridXdefault = PlotGridX;
PlotGridYdefault = PlotGridY;
@ -1310,11 +1305,8 @@ int CmdSetGraphMarkers(const char *Cmd) {
return 0;
}
int CmdHexsamples(const char *Cmd)
{
int i, j;
int requested = 0;
int offset = 0;
int CmdHexsamples(const char *Cmd) {
int i, j, requested = 0, offset = 0;
char string_buf[25];
char* string_ptr = string_buf;
uint8_t got[BIGBUF_SIZE];
@ -1322,9 +1314,9 @@ int CmdHexsamples(const char *Cmd)
sscanf(Cmd, "%i %i", &requested, &offset);
/* if no args send something */
if (requested == 0) {
if (requested == 0)
requested = 8;
}
if (offset + requested > sizeof(got)) {
PrintAndLogEx(NORMAL, "Tried to read past end of buffer, <bytes> + <offset> > %d", BIGBUF_SIZE);
return 0;
@ -1355,17 +1347,14 @@ int CmdHexsamples(const char *Cmd)
return 0;
}
int CmdHide(const char *Cmd)
{
int CmdHide(const char *Cmd) {
HideGraphWindow();
return 0;
}
//zero mean GraphBuffer
int CmdHpf(const char *Cmd)
{
int i;
int accum = 0;
int CmdHpf(const char *Cmd) {
int i, accum = 0;
for (i = 10; i < GraphTraceLen; ++i)
accum += GraphBuffer[i];
@ -1379,18 +1368,15 @@ int CmdHpf(const char *Cmd)
return 0;
}
bool _headBit( BitstreamOut *stream)
{
bool _headBit( BitstreamOut *stream) {
int bytepos = stream->position >> 3; // divide by 8
int bitpos = (stream->position++) & 7; // mask out 00000111
return (*(stream->buffer + bytepos) >> (7-bitpos)) & 1;
}
uint8_t getByte(uint8_t bits_per_sample, BitstreamOut* b)
{
int i;
uint8_t getByte(uint8_t bits_per_sample, BitstreamOut* b) {
uint8_t val = 0;
for(i = 0 ; i < bits_per_sample; i++)
for(int i = 0 ; i < bits_per_sample; i++)
val |= (_headBit(b) << (7-i));
return val;
@ -1426,7 +1412,9 @@ int getSamples(int n, bool silent) {
}
if (bits_per_sample < 8) {
if (!silent) PrintAndLogEx(NORMAL, "Unpacking...");
BitstreamOut bout = { got, bits_per_sample * n, 0};
int j =0;
for (j = 0; j * bits_per_sample < n * 8 && j < n; j++) {
@ -1434,6 +1422,7 @@ int getSamples(int n, bool silent) {
GraphBuffer[j] = ((int) sample )- 128;
}
GraphTraceLen = j;
if (!silent) PrintAndLogEx(NORMAL, "Unpacked %d samples" , j );
} else {
for (int j = 0; j < n; j++) {
@ -1444,9 +1433,7 @@ int getSamples(int n, bool silent) {
//ICEMAN todo
// set signal properties low/high/mean/amplitude and is_noice detection
justNoise(got, n);
// set signal properties low/high/mean/amplitude and isnoice detection
//justNoise_int(GraphBuffer, GraphTraceLen);
justNoise(GraphBuffer, GraphTraceLen);
setClockGrid(0, 0);
DemodBufferLen = 0;
@ -1454,8 +1441,7 @@ int getSamples(int n, bool silent) {
return 0;
}
int CmdSamples(const char *Cmd)
{
int CmdSamples(const char *Cmd) {
int n = strtol(Cmd, NULL, 0);
return getSamples(n, false);
}
@ -1550,8 +1536,7 @@ int CmdTuneSamples(const char *Cmd) {
return 0;
}
int CmdLoad(const char *Cmd)
{
int CmdLoad(const char *Cmd) {
char filename[FILE_PATH_SIZE] = {0x00};
int len = 0;
@ -1579,14 +1564,16 @@ int CmdLoad(const char *Cmd)
DemodBufferLen = 0;
RepaintGraphWindow();
//ICEMAN todo
// set signal properties low/high/mean/amplitude and isnoice detection
justNoise_int(GraphBuffer, GraphTraceLen);
justNoise(GraphBuffer, GraphTraceLen);
return 0;
}
int CmdLtrim(const char *Cmd)
{
if (GraphTraceLen <= 0) return 0;
// trim graph from the end
int CmdLtrim(const char *Cmd) {
// sanitycheck
if (GraphTraceLen <= 0) return 1;
int ds = atoi(Cmd);
for (int i = ds; i < GraphTraceLen; ++i)
@ -1597,10 +1584,14 @@ int CmdLtrim(const char *Cmd)
return 0;
}
// trim graph to input argument length
int CmdRtrim(const char *Cmd)
{
// trim graph from the beginning
int CmdRtrim(const char *Cmd) {
int ds = atoi(Cmd);
// sanitycheck
if (GraphTraceLen <= ds) return 1;
GraphTraceLen = ds;
RepaintGraphWindow();
return 0;
@ -1611,22 +1602,23 @@ int CmdMtrim(const char *Cmd) {
int start = 0, stop = 0;
sscanf(Cmd, "%i %i", &start, &stop);
if (start > GraphTraceLen || stop > GraphTraceLen || start > stop) return 0;
start++; //leave start position sample
if (start > GraphTraceLen || stop > GraphTraceLen || start > stop) return 1;
// leave start position sample
start++;
GraphTraceLen = stop - start;
for (int i = 0; i < GraphTraceLen; i++) {
for (int i = 0; i < GraphTraceLen; i++)
GraphBuffer[i] = GraphBuffer[start+i];
}
return 0;
}
int CmdNorm(const char *Cmd)
{
int CmdNorm(const char *Cmd) {
int i;
int max = INT_MIN, min = INT_MAX;
// Find local min, max
for (i = 10; i < GraphTraceLen; ++i) {
if (GraphBuffer[i] > max) max = GraphBuffer[i];
if (GraphBuffer[i] < min) min = GraphBuffer[i];
@ -1639,19 +1631,22 @@ int CmdNorm(const char *Cmd)
}
}
RepaintGraphWindow();
//ICEMAN todo
// set signal properties low/high/mean/amplitude and isnoice detection
justNoise(GraphBuffer, GraphTraceLen);
return 0;
}
int CmdPlot(const char *Cmd)
{
int CmdPlot(const char *Cmd) {
ShowGraphWindow();
return 0;
}
int CmdSave(const char *Cmd)
{
char filename[FILE_PATH_SIZE] = {0x00};
int CmdSave(const char *Cmd) {
int len = 0;
char filename[FILE_PATH_SIZE] = {0x00};
len = strlen(Cmd);
if (len > FILE_PATH_SIZE) len = FILE_PATH_SIZE;
@ -1659,7 +1654,7 @@ int CmdSave(const char *Cmd)
FILE *f = fopen(filename, "w");
if(!f) {
PrintAndLogEx(NORMAL, "couldn't open '%s'", filename);
PrintAndLogEx(WARNING, "couldn't open '%s'", filename);
return 0;
}
@ -1669,12 +1664,11 @@ int CmdSave(const char *Cmd)
if (f)
fclose(f);
PrintAndLogEx(NORMAL, "saved to '%s'", Cmd);
PrintAndLogEx(SUCCESS, "saved to '%s'", Cmd);
return 0;
}
int CmdScale(const char *Cmd)
{
int CmdScale(const char *Cmd) {
CursorScaleFactor = atoi(Cmd);
if (CursorScaleFactor == 0) {
PrintAndLogEx(FAILED, "bad, can't have zero scale");
@ -1684,10 +1678,13 @@ int CmdScale(const char *Cmd)
return 0;
}
int directionalThreshold(const int* in, int *out, size_t len, int8_t up, int8_t down)
{
int directionalThreshold(const int* in, int *out, size_t len, int8_t up, int8_t down) {
int lastValue = in[0];
out[0] = 0; // Will be changed at the end, but init 0 as we adjust to last samples value if no threshold kicks in.
// Will be changed at the end, but init 0 as we adjust to last samples
// value if no threshold kicks in.
out[0] = 0;
for (size_t i = 1; i < len; ++i) {
// Apply first threshold to samples heading up
@ -1708,30 +1705,28 @@ int directionalThreshold(const int* in, int *out, size_t len, int8_t up, int8_t
out[i] = out[i-1];
}
}
out[0] = out[1]; // Align with first edited sample.
// Align with first edited sample.
out[0] = out[1];
return 0;
}
int CmdDirectionalThreshold(const char *Cmd)
{
int8_t upThres = param_get8(Cmd, 0);
int8_t downThres = param_get8(Cmd, 1);
int CmdDirectionalThreshold(const char *Cmd) {
int8_t up = param_get8(Cmd, 0);
int8_t down = param_get8(Cmd, 1);
PrintAndLogEx(NORMAL, "Applying Up Threshold: %d, Down Threshold: %d\n", upThres, downThres);
PrintAndLogEx(INFO, "Applying Up Threshold: %d, Down Threshold: %d\n", up, down);
directionalThreshold(GraphBuffer, GraphBuffer,GraphTraceLen, upThres, downThres);
directionalThreshold(GraphBuffer, GraphBuffer,GraphTraceLen, up, down);
RepaintGraphWindow();
return 0;
}
int CmdZerocrossings(const char *Cmd)
{
int CmdZerocrossings(const char *Cmd) {
// Zero-crossings aren't meaningful unless the signal is zero-mean.
CmdHpf("");
int sign = 1;
int zc = 0;
int lastZc = 0;
int sign = 1, zc = 0, lastZc = 0;
for (int i = 0; i < GraphTraceLen; ++i) {
if (GraphBuffer[i] * sign >= 0) {
@ -1749,6 +1744,10 @@ int CmdZerocrossings(const char *Cmd)
}
}
//ICEMAN todo
// set signal properties low/high/mean/amplitude and isnoice detection
justNoise(GraphBuffer, GraphTraceLen);
RepaintGraphWindow();
return 0;
}
@ -1758,44 +1757,39 @@ int CmdZerocrossings(const char *Cmd)
* @param Cmd
* @return
*/
int Cmdbin2hex(const char *Cmd)
{
int bg =0, en =0;
if(param_getptr(Cmd, &bg, &en, 0))
int Cmdbin2hex(const char *Cmd) {
int bg = 0, en = 0;
if (param_getptr(Cmd, &bg, &en, 0))
return usage_data_bin2hex();
//Number of digits supplied as argument
size_t length = en - bg +1;
size_t length = en - bg + 1;
size_t bytelen = (length+7) / 8;
uint8_t* arr = (uint8_t *) malloc(bytelen);
memset(arr, 0, bytelen);
BitstreamOut bout = { arr, 0, 0 };
for (; bg <= en ;bg++) {
for (; bg <= en; bg++) {
char c = Cmd[bg];
if( c == '1') pushBit(&bout, 1);
else if( c == '0') pushBit(&bout, 0);
else PrintAndLogEx(NORMAL, "Ignoring '%c'", c);
if( c == '1')
pushBit(&bout, 1);
else if( c == '0')
pushBit(&bout, 0);
else
PrintAndLogEx(NORMAL, "Ignoring '%c'", c);
}
if (bout.numbits % 8 != 0)
PrintAndLogEx(NORMAL, "[padded with %d zeroes]\n", 8-(bout.numbits % 8));
PrintAndLogEx(NORMAL, "[padded with %d zeroes]", 8 - (bout.numbits % 8));
//Uses printf instead of PrintAndLog since the latter
// adds linebreaks to each printout - this way was more convenient since we don't have to
// allocate a string and write to that first...
for(size_t x = 0; x < bytelen ; x++)
PrintAndLogEx(NORMAL, "%02X", arr[x]);
PrintAndLogEx(NORMAL, "\n");
PrintAndLogEx(NORMAL, "%s", sprint_hex(arr, bytelen));
free(arr);
return 0;
}
int Cmdhex2bin(const char *Cmd)
{
int bg =0, en =0;
if(param_getptr(Cmd, &bg, &en, 0)) return usage_data_hex2bin();
int Cmdhex2bin(const char *Cmd) {
int bg = 0, en = 0;
if (param_getptr(Cmd, &bg, &en, 0)) return usage_data_hex2bin();
while (bg <= en ) {
char x = Cmd[bg++];
@ -1813,12 +1807,10 @@ int Cmdhex2bin(const char *Cmd)
//Uses printf instead of PrintAndLog since the latter
// adds linebreaks to each printout - this way was more convenient since we don't have to
// allocate a string and write to that first...
for(int i= 0 ; i < 4 ; ++i)
for(int i = 0 ; i < 4 ; ++i)
PrintAndLogEx(NORMAL, "%d",(x >> (3 - i)) & 1);
}
PrintAndLogEx(NORMAL, "\n");
return 0;
}
@ -1845,7 +1837,7 @@ void GetHiLoTone(int *LowTone, int *HighTone, int clk, int LowToneFC, int HighTo
int Right_Modifier = (clk % LowToneFC) / 2;
//int HighToneMod = clk mod HighToneFC;
int LeftHalfFCCnt = (LowToneFC % 2) + (LowToneFC/2); //truncate
int FCs_per_clk = clk/LowToneFC;
int FCs_per_clk = clk / LowToneFC;
// need to correctly split up the clock to field clocks.
// First attempt uses modifiers on each end to make up for when FCs don't evenly divide into Clk
@ -1860,14 +1852,14 @@ void GetHiLoTone(int *LowTone, int *HighTone, int clk, int LowToneFC, int HighTo
for (i = 0; i < (FCs_per_clk); i++) {
// loop # of samples per field clock
for (j = 0; j < LowToneFC; j++) {
LowTone[(i*LowToneFC)+Left_Modifier+j] = ( j < LeftHalfFCCnt ) ? 1 : -1;
LowTone[ (i * LowToneFC) + Left_Modifier + j] = ( j < LeftHalfFCCnt ) ? 1 : -1;
}
}
int k;
// add last -1 modifiers
for (k = 0; k < Right_Modifier; k++) {
LowTone[((i-1)*LowToneFC)+Left_Modifier+j+k] = -1;
LowTone[ ( (i-1) * LowToneFC) + Left_Modifier + j + k] = -1;
}
// now do hightone
@ -1884,18 +1876,18 @@ void GetHiLoTone(int *LowTone, int *HighTone, int clk, int LowToneFC, int HighTo
for (i = 0; i < (FCs_per_clk); i++) {
// loop # of samples per field clock
for (j = 0; j < HighToneFC; j++) {
HighTone[(i*HighToneFC)+Left_Modifier+j] = ( j < LeftHalfFCCnt ) ? 1 : -1;
HighTone[(i * HighToneFC) + Left_Modifier + j] = ( j < LeftHalfFCCnt ) ? 1 : -1;
}
}
// add last -1 modifiers
for (k = 0; k < Right_Modifier; k++) {
PrintAndLogEx(NORMAL, "(i-1)*HighToneFC+lm+j+k %i",((i-1)*HighToneFC)+Left_Modifier+j+k);
HighTone[((i-1)*HighToneFC)+Left_Modifier+j+k] = -1;
PrintAndLogEx(NORMAL, "(i-1)*HighToneFC+lm+j+k %i", ((i-1) * HighToneFC) + Left_Modifier + j + k);
HighTone[ ( (i-1) * HighToneFC) + Left_Modifier + j + k] = -1;
}
if (g_debugMode == 2) {
for ( i = 0; i < clk; i++) {
PrintAndLogEx(NORMAL, "Low: %i, High: %i",LowTone[i],HighTone[i]);
PrintAndLogEx(NORMAL, "Low: %i, High: %i", LowTone[i], HighTone[i]);
}
}
}
@ -1903,7 +1895,7 @@ void GetHiLoTone(int *LowTone, int *HighTone, int clk, int LowToneFC, int HighTo
//old CmdFSKdemod adapted by marshmellow
//converts FSK to clear NRZ style wave. (or demodulates)
int FSKToNRZ(int *data, int *dataLen, int clk, int LowToneFC, int HighToneFC) {
uint8_t ans=0;
uint8_t ans = 0;
if (clk == 0 || LowToneFC == 0 || HighToneFC == 0) {
int firstClockEdge=0;
ans = fskClocks((uint8_t *) &LowToneFC, (uint8_t *) &HighToneFC, (uint8_t *) &clk, &firstClockEdge);
@ -1912,17 +1904,17 @@ int FSKToNRZ(int *data, int *dataLen, int clk, int LowToneFC, int HighToneFC) {
}
}
// currently only know fsk modulations with field clocks < 10 samples and > 4 samples. filter out to remove false positives (and possibly destroying ask/psk modulated waves...)
if (ans == 0 || clk == 0 || LowToneFC == 0 || HighToneFC == 0 || LowToneFC > 10 || HighToneFC < 4) {
if (ans == 0 || clk == 0 || LowToneFC == 0 || HighToneFC == 0 || LowToneFC > 10 || HighToneFC < 4) {
if (g_debugMode > 1) {
PrintAndLog ("DEBUG FSKtoNRZ: no fsk clocks found");
}
return 0;
}
int i, j;
int LowTone[clk];
int HighTone[clk];
GetHiLoTone(LowTone, HighTone, clk, LowToneFC, HighToneFC);
int i, j;
// loop through ([all samples] - clk)
for (i = 0; i < *dataLen - clk; ++i) {
@ -1930,7 +1922,7 @@ int FSKToNRZ(int *data, int *dataLen, int clk, int LowToneFC, int HighToneFC) {
// sum all samples together starting from this sample for [clk] samples for each tone (multiply tone value with sample data)
for (j = 0; j < clk; ++j) {
lowSum += LowTone[j] * data[i+j];
lowSum += LowTone[j] * data[i + j];
highSum += HighTone[j] * data[i + j];
}
// get abs( [average sample value per clk] * 100 ) (or a rolling average of sorts)
@ -1963,18 +1955,14 @@ int FSKToNRZ(int *data, int *dataLen, int clk, int LowToneFC, int HighToneFC) {
return 0;
}
int CmdFSKToNRZ(const char *Cmd) {
// take clk, fc_low, fc_high
// blank = auto;
bool errors = false;
int clk = 0;
char cmdp = 0;
int fc_low = 10, fc_high = 8;
while(param_getchar(Cmd, cmdp) != 0x00)
{
switch (tolower(param_getchar(Cmd, cmdp)))
{
int clk = 0, fc_low = 10, fc_high = 8;
while (param_getchar(Cmd, cmdp) != 0x00) {
switch (tolower(param_getchar(Cmd, cmdp))) {
case 'h':
return usage_data_fsktonrz();
case 'c':
@ -1997,9 +1985,9 @@ int CmdFSKToNRZ(const char *Cmd) {
if(errors) break;
}
//Validations
if(errors) return usage_data_fsktonrz();
if (errors) return usage_data_fsktonrz();
setClockGrid(0,0);
setClockGrid(0, 0);
DemodBufferLen = 0;
int ans = FSKToNRZ(GraphBuffer, &GraphTraceLen, clk, fc_low, fc_high);
CmdNorm("");
@ -2007,17 +1995,15 @@ int CmdFSKToNRZ(const char *Cmd) {
return ans;
}
int CmdDataIIR(const char *Cmd){
uint8_t k = param_get8(Cmd,0);
uint8_t k = param_get8(Cmd, 0);
//iceIIR_Butterworth(GraphBuffer, GraphTraceLen);
iceSimple_Filter(GraphBuffer, GraphTraceLen, k);
RepaintGraphWindow();
return 0;
}
static command_t CommandTable[] =
{
static command_t CommandTable[] = {
{"help", CmdHelp, 1, "This help"},
{"askedgedetect", CmdAskEdgeDetect, 1, "[threshold] Adjust Graph for manual ASK demod using the length of sample differences to detect the edge of a wave (use 20-45, def:25)"},
{"autocorr", CmdAutoCorr, 1, "[window length] [g] -- Autocorrelation over window - g to save back to GraphBuffer (overwrite)"},
@ -2058,14 +2044,13 @@ static command_t CommandTable[] =
{NULL, NULL, 0, NULL}
};
int CmdData(const char *Cmd){
int CmdData(const char *Cmd) {
clearCommandBuffer();
CmdsParse(CommandTable, Cmd);
return 0;
}
int CmdHelp(const char *Cmd)
{
int CmdHelp(const char *Cmd) {
CmdsHelp(CommandTable);
return 0;
}

View file

@ -1148,7 +1148,7 @@ int EM4x05ReadWord_ext(uint8_t addr, uint32_t pwd, bool usePwd, uint32_t *word)
}
int testLen = (GraphTraceLen < 1000) ? GraphTraceLen : 1000;
if (justNoise_int(GraphBuffer, testLen)) {
if (justNoise(GraphBuffer, testLen)) {
PrintAndLogEx(DEBUG, "No tag found");
return -1;
}

View file

@ -1315,7 +1315,7 @@ bool AquireData( uint8_t page, uint8_t block, bool pwdmode, uint32_t password ){
}
setGraphBuf(got, sizeof(got));
return !justNoise_int(GraphBuffer, sizeof(got));
return !justNoise(GraphBuffer, sizeof(got));
}
char * GetBitRateStr(uint32_t id, bool xmode) {

View file

@ -13,6 +13,7 @@
static int CmdHelp(const char *Cmd);
static int CmdQuit(const char *Cmd);
static int CmdRev(const char *Cmd);
static int CmdRem(const char *Cmd);
//For storing command that are received from the device
static UsbCommand cmdBuffer[CMD_BUFFER_SIZE];
@ -33,6 +34,7 @@ static command_t CommandTable[] = {
{"hf", CmdHF, 1, "{ High Frequency commands... }"},
{"hw", CmdHW, 1, "{ Hardware commands... }"},
{"lf", CmdLF, 1, "{ Low Frequency commands... }"},
{"rem", CmdRem, 1, "{ Add text to row in log file }"},
{"reveng", CmdRev, 1, "{ Crc calculations from the software reveng 1.53... }"},
{"script", CmdScript, 1, "{ Scripting commands }"},
{"trace", CmdTrace, 1, "{ Trace manipulation... }"},
@ -51,6 +53,18 @@ command_t* getTopLevelCommandTable() {
return CommandTable;
}
int CmdRem(const char *Cmd) {
char buf[22];
memset(buf, 0x00, sizeof(buf));
struct tm *curTime;
time_t now = time(0);
curTime = gmtime(&now);
strftime (buf, sizeof(buf), "%Y-%m-%dT%H:%M:%SZ", curTime); // ISO8601
PrintAndLogEx(NORMAL, "%s remark: %s", buf, Cmd);
return 0;
}
int CmdHelp(const char *Cmd) {
CmdsHelp(CommandTable);
return 0;

View file

@ -817,6 +817,9 @@ extern void str_lower(char *s ){
for(int i=0; i < strlen(s); i++)
s[i] = tolower( s[i] );
}
extern bool str_startswith(const char *s, const char *pre) {
return strncmp(pre, s, strlen(pre)) == 0;
}
// Replace unprintable characters with a dot in char buffer
extern void clean_ascii(unsigned char *buf, size_t len) {

View file

@ -247,6 +247,7 @@ extern uint64_t HornerScheme(uint64_t num, uint64_t divider, uint64_t factor);
extern int num_CPUs(void); // number of logical CPUs
extern void str_lower(char* s); // converts string to lower case
extern bool str_startswith(const char *s, const char *pre); // check for prefix in string
extern void strcleanrn(char *buf, size_t len);
extern void strcreplace(char *buf, size_t len, char from, char to);
extern char *strmcopy(char *buf);

View file

@ -51,7 +51,9 @@
#define NOICE_AMPLITUDE_THRESHOLD 10
//to allow debug print calls when used not on dev
void dummy(char *fmt, ...){}
//void dummy(char *fmt, ...){}
extern void Dbprintf(const char *fmt, ...);
#ifndef ON_DEVICE
#include "ui.h"
# include "cmdparser.h"
@ -59,7 +61,7 @@ void dummy(char *fmt, ...){}
# define prnt PrintAndLog
#else
uint8_t g_debugMode = 0;
# define prnt dummy
# define prnt Dbprintf
#endif
signal_t signalprop = { 255, -255, 0, 0, true };
@ -105,10 +107,9 @@ int32_t compute_mean_int(int *in, size_t N) {
//test samples are not just noise
// By measuring mean and look at amplitude of signal from HIGH / LOW, we can detect noise
bool justNoise_int(int *bits, uint32_t size) {
bool isNoise_int(int *bits, uint32_t size) {
resetSignal();
if ( bits == NULL ) return true;
if ( size < 100 ) return true;
if ( bits == NULL || size < 100 ) return true;
int32_t sum = 0;
for ( size_t i = 0; i < size; i++) {
@ -130,10 +131,9 @@ bool justNoise_int(int *bits, uint32_t size) {
//test samples are not just noise
// By measuring mean and look at amplitude of signal from HIGH / LOW,
// we can detect noise
bool justNoise(uint8_t *bits, uint32_t size) {
bool isNoise(uint8_t *bits, uint32_t size) {
resetSignal();
if ( bits == NULL ) return true;
if ( size < 100 ) return true;
if ( bits == NULL || size < 100 ) return true;
uint32_t sum = 0;
for ( uint32_t i = 0; i < size; i++) {
@ -1553,16 +1553,15 @@ size_t fsk_wave_demod(uint8_t *dest, size_t size, uint8_t fchigh, uint8_t fclow,
size_t currSample = 0;
size_t last_transition = 0;
size_t idx = 1;
size_t numBits = 0;
//find start of modulating data in trace
idx = findModStart(dest, size, fchigh);
// Need to threshold first sample
if(dest[idx] < FSK_PSK_THRESHOLD) dest[0] = 0;
else dest[0] = 1;
dest[0] = (dest[idx] < FSK_PSK_THRESHOLD) ? 0 : 1;
last_transition = idx;
idx++;
size_t numBits = 0;
// Definition: cycles between consecutive lo-hi transitions
// Lets define some expected lengths. FSK1 is easier since it has bigger differences between.
@ -1595,9 +1594,9 @@ size_t fsk_wave_demod(uint8_t *dest, size_t size, uint8_t fchigh, uint8_t fclow,
// the 1-0 to 0-1 width should be divided with exp_zero. Ie: 3+5+6+7 = 21/6 = 3
for(; idx < size-20; idx++) {
// threshold current value
if (dest[idx] < FSK_PSK_THRESHOLD) dest[idx] = 0;
else dest[idx] = 1;
dest[idx] = (dest[idx] < FSK_PSK_THRESHOLD) ? 0 : 1;
// Check for 0->1 transition
if (dest[idx-1] < dest[idx]) {
@ -1612,16 +1611,24 @@ size_t fsk_wave_demod(uint8_t *dest, size_t size, uint8_t fchigh, uint8_t fclow,
dest[numBits-1]=1;
}
dest[numBits++]=1;
if (numBits > 0 && *startIdx==0) *startIdx = idx - fclow;
if (numBits > 0 && *startIdx == 0)
*startIdx = idx - fclow;
} else if (currSample > (fchigh+1) && numBits < 3) { //12 + and first two bit = unusable garbage
//do nothing with beginning garbage and reset.. should be rare..
numBits = 0;
} else if (currSample == (fclow+1) && LastSample == (fclow-1)) { // had a 7 then a 9 should be two 8's (or 4 then a 6 should be two 5's)
dest[numBits++]=1;
if (numBits > 0 && *startIdx==0) *startIdx = idx - fclow;
if (numBits > 0 && *startIdx == 0) {
*startIdx = idx - fclow;
}
} else { //9+ = 10 sample waves (or 6+ = 7)
dest[numBits++]=0;
if (numBits > 0 && *startIdx==0) *startIdx = idx - fchigh;
if (numBits > 0 && *startIdx == 0) {
*startIdx = idx - fchigh;
}
}
last_transition = idx;
}
@ -1632,6 +1639,7 @@ size_t fsk_wave_demod(uint8_t *dest, size_t size, uint8_t fchigh, uint8_t fclow,
//translate 11111100000 to 10
//rfLen = clock, fchigh = larger field clock, fclow = smaller field clock
size_t aggregate_bits(uint8_t *dest, size_t size, uint8_t clk, uint8_t invert, uint8_t fchigh, uint8_t fclow, int *startIdx) {
uint8_t lastval = dest[0];
size_t i = 0;
size_t numBits = 0;
@ -1643,13 +1651,16 @@ size_t aggregate_bits(uint8_t *dest, size_t size, uint8_t clk, uint8_t invert, u
if (dest[i] == lastval) continue; //skip until we hit a transition
//find out how many bits (n) we collected (use 1/2 clk tolerance)
if (dest[i-1] == 1)
//if lastval was 1, we have a 1->0 crossing
if (dest[i-1] == 1) {
n = (n * fclow + hclk) / clk;
} else {// 0->1 crossing
else
// 0->1 crossing
n = (n * fchigh + hclk) / clk;
}
if (n == 0) n = 1;
if (n == 0)
n = 1;
//first transition - save startidx
if (numBits == 0) {
@ -1664,12 +1675,13 @@ size_t aggregate_bits(uint8_t *dest, size_t size, uint8_t clk, uint8_t invert, u
//add to our destination the bits we collected
memset(dest+numBits, dest[i-1] ^ invert , n);
//if (g_debugMode == 2) prnt("ICCE:: n %u | numbits %u", n, numBits);
numBits += n;
n = 0;
lastval = dest[i];
}//end for
// if valid extra bits at the end were all the same frequency - add them in
if (n > clk/fchigh) {
if (dest[i-2] == 1) {
@ -1696,14 +1708,15 @@ size_t fskdemod(uint8_t *dest, size_t size, uint8_t rfLen, uint8_t invert, uint8
// by marshmellow
// convert psk1 demod to psk2 demod
// only transition waves are 1s
//TODO: Iceman - hard coded value 7, should be #define
void psk1TOpsk2(uint8_t *bits, size_t size) {
uint8_t lastBit = bits[0];
uint8_t lastbit = bits[0];
for (size_t i = 1; i < size; i++){
//ignore errors
if (bits[i] == 7) continue;
if (lastBit != bits[i]){
lastBit = bits[i];
if (lastbit != bits[i]){
lastbit = bits[i];
bits[i] = 1;
} else {
bits[i] = 0;
@ -1726,6 +1739,7 @@ void psk2TOpsk1(uint8_t *bits, size_t size) {
//by marshmellow - demodulate PSK1 wave
//uses wave lengths (# Samples)
//TODO: Iceman - hard coded value 7, should be #define
int pskRawDemod_ext(uint8_t *dest, size_t *size, int *clock, int *invert, int *startIdx) {
// sanity check
@ -1763,7 +1777,7 @@ int pskRawDemod_ext(uint8_t *dest, size_t *size, int *clock, int *invert, int *s
//set start of wave as clock align
lastClkBit = firstFullWave;
if (g_debugMode==2) prnt("DEBUG PSK: firstFullWave: %u, waveLen: %u, startIdx %i",firstFullWave,fullWaveLen, *startIdx);
if (g_debugMode==2) prnt("DEBUG PSK: clk: %d, lastClkBit: %u, fc: %u", *clock, lastClkBit,(unsigned int) fc);
if (g_debugMode == 2) prnt("DEBUG PSK: clk: %d, lastClkBit: %u, fc: %u", *clock, lastClkBit, fc);
waveStart = 0;
dest[numBits++] = curPhase; //set first read bit
for (i = firstFullWave + fullWaveLen - 1; i < *size-3; i++){
@ -1809,8 +1823,8 @@ int pskRawDemod_ext(uint8_t *dest, size_t *size, int *clock, int *invert, int *s
}
int pskRawDemod(uint8_t *dest, size_t *size, int *clock, int *invert) {
int startIdx = 0;
return pskRawDemod_ext(dest, size, clock, invert, &startIdx);
int start_idx = 0;
return pskRawDemod_ext(dest, size, clock, invert, &start_idx);
}
@ -1833,38 +1847,38 @@ int detectAWID(uint8_t *dest, size_t *size, int *waveStartIdx) {
//did we get a good demod?
if (*size < 96) return -3;
size_t startIdx = 0;
size_t start_idx = 0;
uint8_t preamble[] = {0,0,0,0,0,0,0,1};
if (!preambleSearch(dest, preamble, sizeof(preamble), size, &startIdx))
if (!preambleSearch(dest, preamble, sizeof(preamble), size, &start_idx))
return -4; //preamble not found
// wrong size? (between to preambles)
if (*size != 96) return -5;
return (int)startIdx;
return (int)start_idx;
}
//by marshmellow
//takes 1s and 0s and searches for EM410x format - output EM ID
int Em410xDecode(uint8_t *bits, size_t *size, size_t *startIdx, uint32_t *hi, uint64_t *lo) {
int Em410xDecode(uint8_t *bits, size_t *size, size_t *start_idx, uint32_t *hi, uint64_t *lo) {
// sanity check
if (bits[1] > 1) return -1;
if (*size < 64) return -2;
uint8_t fmtlen;
*startIdx = 0;
*start_idx = 0;
// preamble 0111111111
// include 0 in front to help get start pos
uint8_t preamble[] = {0,1,1,1,1,1,1,1,1,1};
if (!preambleSearch(bits, preamble, sizeof(preamble), size, startIdx))
if (!preambleSearch(bits, preamble, sizeof(preamble), size, start_idx))
return -4;
// (iceman) if the preamble doesn't find two occuriences, this identification fails.
fmtlen = (*size == 128) ? 22 : 10;
//skip last 4bit parity row for simplicity
*size = removeParity(bits, *startIdx + sizeof(preamble), 5, 0, fmtlen * 5);
*size = removeParity(bits, *start_idx + sizeof(preamble), 5, 0, fmtlen * 5);
switch (*size) {
case 40: {
@ -1898,14 +1912,17 @@ int HIDdemodFSK(uint8_t *dest, size_t *size, uint32_t *hi2, uint32_t *hi, uint32
if (*size < 96*2) return -3;
// 00011101 bit pattern represent start of frame, 01 pattern represents a 0 and 10 represents a 1
size_t startIdx = 0;
size_t start_idx = 0;
uint8_t preamble[] = {0,0,0,1,1,1,0,1};
if (!preambleSearch(dest, preamble, sizeof(preamble), size, &startIdx))
if (!preambleSearch(dest, preamble, sizeof(preamble), size, &start_idx))
return -4; //preamble not found
size_t numStart = startIdx + sizeof(preamble);
// wrong size? (between to preambles)
//if (*size != 96) return -5;
size_t num_start = start_idx + sizeof(preamble);
// final loop, go over previously decoded FSK data and manchester decode into usable tag ID
for (size_t idx = numStart; (idx-numStart) < *size - sizeof(preamble); idx+=2){
for (size_t idx = num_start; (idx - num_start) < *size - sizeof(preamble); idx += 2) {
if (dest[idx] == dest[idx+1]){
return -5; //not manchester data
}
@ -1918,7 +1935,7 @@ int HIDdemodFSK(uint8_t *dest, size_t *size, uint32_t *hi2, uint32_t *hi, uint32
else // 0 1
*lo |= 0;
}
return (int)startIdx;
return (int)start_idx;
}
// Find IDTEC PSK1, RF Preamble == 0x4944544B, Demodsize 64bits
@ -1943,7 +1960,7 @@ int detectIOProx(uint8_t *dest, size_t *size, int *waveStartIdx) {
// FSK demodulator RF/64, fsk2a so invert, and fc/10/8
*size = fskdemod(dest, *size, 64, 1, 10, 8, waveStartIdx); //io fsk2a
//did we get a good demod?
//did we get enough demod data?
if (*size < 64) return -3;
//Index map
@ -1955,18 +1972,23 @@ int detectIOProx(uint8_t *dest, size_t *size, int *waveStartIdx) {
//
//XSF(version)facility:codeone+codetwo
size_t startIdx = 0;
size_t start_idx = 0;
uint8_t preamble[] = {0,0,0,0,0,0,0,0,0,1};
if (! preambleSearch(dest, preamble, sizeof(preamble), size, &startIdx))
if (!preambleSearch(dest, preamble, sizeof(preamble), size, &start_idx))
return -4; //preamble not found
// wrong size? (between to preambles)
if (*size != 64) return -5;
if (!dest[startIdx+8] && dest[startIdx+17]==1 && dest[startIdx+26]==1 && dest[startIdx+35]==1 && dest[startIdx+44]==1 && dest[startIdx+53]==1){
if ( !dest[start_idx + 8]
&& dest[start_idx + 17] == 1
&& dest[start_idx + 26] == 1
&& dest[start_idx + 35] == 1
&& dest[start_idx + 44] == 1
&& dest[start_idx + 53] == 1) {
//confirmed proper separator bits found
//return start position
return (int) startIdx;
return (int) start_idx;
}
return -6;
}

View file

@ -32,10 +32,16 @@ extern signal_t* getSignalProperties(void);
extern uint32_t compute_mean_uint(uint8_t *in, size_t N);
extern int32_t compute_mean_int(int *in, size_t N);
bool isNoise_int(int *bits, uint32_t size);
bool isNoise(uint8_t *bits, uint32_t size);
extern bool justNoise_int(int *bits, uint32_t size);
extern bool justNoise(uint8_t *bits, uint32_t size);
// buffer is unsigned on DEVIE
#ifdef ON_DEVICE
#define justNoise(a, b) isNoise((a), (b))
#else
#define justNoise(a, b) isNoise_int((a), (b))
#endif
void getNextLow(uint8_t *samples, size_t size, int low, size_t *i);
void getNextHigh(uint8_t *samples, size_t size, int high, size_t *i);
bool loadWaveCounters(uint8_t *samples, size_t size, int lowToLowWaveLen[], int highToLowWaveLen[], int *waveCnt, int *skip, int *minClk, int *high, int *low);

View file

@ -373,17 +373,15 @@ static const char StrManufacturer[] = {
};
static const char StrProduct[] = {
22, // Length
20, // Length
0x03, // Type is string
'P',0,'M',0,'3',0,' ',0,'D',0,'e',0,'v',0,'i',0,'c',0,'e',0
'p',0,'r',0,'o',0,'x',0,'m',0,'a',0,'r',0,'k',0,'3',0
};
static const char StrSerialNumber[] = {
8, // Length
14, // Length
0x03, // Type is string
'8',0,
'8',0,
'8',0
'i',0,'c',0,'e',0,'m',0,'a',0,'n',0
};
// size includes their own field.