mirror of
https://github.com/Proxmark/proxmark3.git
synced 2025-08-22 22:23:38 -07:00
LF t55xx and LF em4x commands now should manchester decode data. However t55xx can have other settings and different encodings.
This commit is contained in:
parent
35147d51e3
commit
b44e523300
9 changed files with 351 additions and 242 deletions
|
@ -993,7 +993,7 @@ void __attribute__((noreturn)) AppMain(void)
|
||||||
LED_B_OFF();
|
LED_B_OFF();
|
||||||
LED_A_OFF();
|
LED_A_OFF();
|
||||||
|
|
||||||
// Init USB device`
|
// Init USB device
|
||||||
usb_enable();
|
usb_enable();
|
||||||
|
|
||||||
// The FPGA gets its clock from us from PCK0 output, so set that up.
|
// The FPGA gets its clock from us from PCK0 output, so set that up.
|
||||||
|
|
|
@ -131,16 +131,15 @@ void MifareDesfireGetInformation(){
|
||||||
void MifareDES_Auth1(uint8_t mode, uint8_t algo, uint8_t keyno, uint8_t *datain){
|
void MifareDES_Auth1(uint8_t mode, uint8_t algo, uint8_t keyno, uint8_t *datain){
|
||||||
|
|
||||||
uint8_t null_key_data[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
|
uint8_t null_key_data[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
|
||||||
uint8_t new_key_data[8] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77 };
|
//uint8_t new_key_data[8] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77 };
|
||||||
int res;
|
int res = 0;
|
||||||
|
|
||||||
MifareDESFireKey default_key = mifare_desfire_des_key_new_with_version (null_key_data);
|
desfirekey_t default_key = Desfire_des_key_new_with_version (null_key_data);
|
||||||
|
|
||||||
res = mifare_desfire_select_application (tags[i], aid);
|
// res = Desfire_select_application (tags[i], aid);
|
||||||
if (res < 0) {
|
if (res < 0) {
|
||||||
freefare_perror (tags[i], "mifare_desfire_select_application");
|
print_result("default key: ", default_key->data, 24 );
|
||||||
error = EXIT_FAILURE;
|
return;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -12,5 +12,4 @@
|
||||||
#include "mifareutil.h"
|
#include "mifareutil.h"
|
||||||
#include "../include/common.h"
|
#include "../include/common.h"
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -588,9 +588,12 @@ int CmdManchesterDemod(const char *Cmd)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
PrintAndLog("Clock: %d", clock);
|
||||||
|
|
||||||
/* If we're not working with 1/0s, demod based off clock */
|
/* If we're not working with 1/0s, demod based off clock */
|
||||||
if (high != 1)
|
if (high != 1)
|
||||||
{
|
{
|
||||||
|
PrintAndLog("Entering path A");
|
||||||
bit = 0; /* We assume the 1st bit is zero, it may not be
|
bit = 0; /* We assume the 1st bit is zero, it may not be
|
||||||
* the case: this routine (I think) has an init problem.
|
* the case: this routine (I think) has an init problem.
|
||||||
* Ed.
|
* Ed.
|
||||||
|
|
|
@ -524,7 +524,7 @@ int CmdHF14AMfDump(const char *Cmd)
|
||||||
|
|
||||||
PrintAndLog("Got %d",size);
|
PrintAndLog("Got %d",size);
|
||||||
|
|
||||||
return;
|
return 0;
|
||||||
|
|
||||||
if ( size > -1)
|
if ( size > -1)
|
||||||
cmdp = (char)48+size;
|
cmdp = (char)48+size;
|
||||||
|
@ -1027,6 +1027,18 @@ int CmdHF14AMfNested(const char *Cmd)
|
||||||
|
|
||||||
int CmdHF14AMfChk(const char *Cmd)
|
int CmdHF14AMfChk(const char *Cmd)
|
||||||
{
|
{
|
||||||
|
if (strlen(Cmd)<3) {
|
||||||
|
PrintAndLog("Usage: hf mf chk <block number>|<*card memory> <key type (A/B/?)> [t|d] [<key (12 hex symbols)>] [<dic (*.dic)>]");
|
||||||
|
PrintAndLog(" * - all sectors");
|
||||||
|
PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");
|
||||||
|
PrintAndLog("d - write keys to binary file\n");
|
||||||
|
PrintAndLog("t - write keys to emulator memory");
|
||||||
|
PrintAndLog(" sample: hf mf chk 0 A 1234567890ab keys.dic");
|
||||||
|
PrintAndLog(" hf mf chk *1 ? t");
|
||||||
|
PrintAndLog(" hf mf chk *1 ? d");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
FILE * f;
|
FILE * f;
|
||||||
char filename[256]={0};
|
char filename[256]={0};
|
||||||
char buf[13];
|
char buf[13];
|
||||||
|
@ -1070,16 +1082,6 @@ int CmdHF14AMfChk(const char *Cmd)
|
||||||
num_to_bytes(defaultKeys[defaultKeyCounter], 6, (uint8_t*)(keyBlock + defaultKeyCounter * 6));
|
num_to_bytes(defaultKeys[defaultKeyCounter], 6, (uint8_t*)(keyBlock + defaultKeyCounter * 6));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strlen(Cmd)<3) {
|
|
||||||
PrintAndLog("Usage: hf mf chk <block number>|<*card memory> <key type (A/B/?)> [t] [<key (12 hex symbols)>] [<dic (*.dic)>]");
|
|
||||||
PrintAndLog(" * - all sectors");
|
|
||||||
PrintAndLog("card memory - 0 - MINI(320 bytes), 1 - 1K, 2 - 2K, 4 - 4K, <other> - 1K");
|
|
||||||
PrintAndLog("d - write keys to binary file\n");
|
|
||||||
PrintAndLog(" sample: hf mf chk 0 A 1234567890ab keys.dic");
|
|
||||||
PrintAndLog(" hf mf chk *1 ? t");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (param_getchar(Cmd, 0)=='*') {
|
if (param_getchar(Cmd, 0)=='*') {
|
||||||
blockNo = 3;
|
blockNo = 3;
|
||||||
switch(param_getchar(Cmd+1, 0)) {
|
switch(param_getchar(Cmd+1, 0)) {
|
||||||
|
@ -2090,51 +2092,58 @@ int GetCardSize()
|
||||||
|
|
||||||
|
|
||||||
// NXP MIFARE Mini 0.3k
|
// NXP MIFARE Mini 0.3k
|
||||||
if ( (atqa && 0xff0f == 0x0004) && (sak == 0x09) ) return 0;
|
if ( ( (atqa & 0xff0f) == 0x0004) && (sak == 0x09) ) return 0;
|
||||||
|
|
||||||
// MIFARE Classic 1K
|
// MIFARE Classic 1K
|
||||||
if ( (atqa && 0xff0f == 0x0004) && (sak == 0x08) ) return 1;
|
if ( ((atqa & 0xff0f) == 0x0004) && (sak == 0x08) ) return 1;
|
||||||
|
|
||||||
// MIFARE Classik 4K
|
// MIFARE Classik 4K
|
||||||
if ( (atqa && 0xff0f == 0x0002) && (sak == 0x18) ) return 4;
|
if ( ((atqa & 0xff0f) == 0x0002) && (sak == 0x18) ) return 4;
|
||||||
|
|
||||||
// SmartMX with MIFARE 1K emulation
|
// SmartMX with MIFARE 1K emulation
|
||||||
if ( (atqa && 0xf0ff == 0x0004) ) return 1;
|
if ( ((atqa & 0xf0ff) == 0x0004) ) return 1;
|
||||||
|
|
||||||
// SmartMX with MIFARE 4K emulation
|
// SmartMX with MIFARE 4K emulation
|
||||||
if ( (atqa && 0xf0ff == 0x0002) ) return 4;
|
if ( ((atqa & 0xf0ff) == 0x0002) ) return 4;
|
||||||
|
|
||||||
// Infineon MIFARE CLASSIC 1K
|
// Infineon MIFARE CLASSIC 1K
|
||||||
if ( (atqa && 0xffff == 0x0004) && (sak == 0x88) ) return 1;
|
if ( ((atqa & 0xffff) == 0x0004) && (sak == 0x88) ) return 1;
|
||||||
|
|
||||||
// MFC 4K emulated by Nokia 6212 Classic
|
// MFC 4K emulated by Nokia 6212 Classic
|
||||||
if ( (atqa && 0xffff == 0x0002) && (sak == 0x38) ) return 4;
|
if ( ((atqa & 0xffff) == 0x0002) && (sak == 0x38) ) return 4;
|
||||||
|
|
||||||
// MFC 4K emulated by Nokia 6131 NFC
|
// MFC 4K emulated by Nokia 6131 NFC
|
||||||
if ( (atqa && 0xffff == 0x0008) && (sak == 0x38) ) return 4;
|
if ( ((atqa & 0xffff) == 0x0008) && (sak == 0x38) ) return 4;
|
||||||
|
|
||||||
|
|
||||||
|
PrintAndLog("BEFOOO 1K %02X", (atqa & 0xff0f));
|
||||||
|
|
||||||
// MIFARE Plus (4 Byte UID or 4 Byte RID)
|
// MIFARE Plus (4 Byte UID or 4 Byte RID)
|
||||||
// MIFARE Plus (7 Byte UID)
|
// MIFARE Plus (7 Byte UID)
|
||||||
if (
|
if (
|
||||||
(atqa && 0xffff == 0x0002) ||
|
((atqa & 0xffff) == 0x0002) |
|
||||||
(atqa && 0xffff == 0x0004) ||
|
((atqa & 0xffff) == 0x0004) |
|
||||||
(atqa && 0xffff == 0x0042) ||
|
((atqa & 0xffff) == 0x0042) |
|
||||||
(atqa && 0xffff == 0x0044)
|
((atqa & 0xffff) == 0x0044)
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
switch(sak){
|
switch(sak){
|
||||||
case 0x08:
|
case 0x08:
|
||||||
case 0x10:
|
case 0x10: {
|
||||||
//case 0x20:
|
//case 0x20:
|
||||||
|
PrintAndLog("2");
|
||||||
return 2;
|
return 2;
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
case 0x11:
|
case 0x11:
|
||||||
case 0x18:
|
case 0x18:{
|
||||||
//case 0x20:
|
//case 0x20:
|
||||||
|
PrintAndLog("4");
|
||||||
return 4;
|
return 4;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,6 +21,7 @@
|
||||||
#include "cmdlfem4x.h"
|
#include "cmdlfem4x.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
#include "data.h"
|
#include "data.h"
|
||||||
|
#define LF_TRACE_BUFF_SIZE 16000
|
||||||
|
|
||||||
char *global_em410xId;
|
char *global_em410xId;
|
||||||
|
|
||||||
|
@ -525,22 +526,28 @@ int CmdReadWord(const char *Cmd)
|
||||||
SendCommand(&c);
|
SendCommand(&c);
|
||||||
WaitForResponse(CMD_ACK, NULL);
|
WaitForResponse(CMD_ACK, NULL);
|
||||||
|
|
||||||
size_t bytelength = 4096;
|
uint8_t data[LF_TRACE_BUFF_SIZE];
|
||||||
uint8_t data[bytelength];
|
memset(data, 0x00, LF_TRACE_BUFF_SIZE);
|
||||||
memset(data, 0x00, bytelength);
|
|
||||||
|
|
||||||
GetFromBigBuf(data,bytelength,3560); //3560 -- should be offset..
|
GetFromBigBuf(data,LF_TRACE_BUFF_SIZE,3560); //3560 -- should be offset..
|
||||||
WaitForResponseTimeout(CMD_ACK,NULL, 1500);
|
WaitForResponseTimeout(CMD_ACK,NULL, 1500);
|
||||||
|
|
||||||
for (int j = 0; j < bytelength; j++) {
|
for (int j = 0; j < LF_TRACE_BUFF_SIZE; j++) {
|
||||||
GraphBuffer[j] = ((int)data[j]) - 128;
|
GraphBuffer[j] = ((int)data[j]) - 128;
|
||||||
}
|
}
|
||||||
GraphTraceLen = bytelength;
|
GraphTraceLen = LF_TRACE_BUFF_SIZE;
|
||||||
RepaintGraphWindow();
|
|
||||||
|
|
||||||
manchester_decode(data, bytelength);
|
// BiDirectional
|
||||||
|
//CmdDirectionalThreshold("70 -60");
|
||||||
|
|
||||||
free(data);
|
// Askdemod
|
||||||
|
//Cmdaskdemod("1");
|
||||||
|
|
||||||
|
uint8_t bits[1000];
|
||||||
|
uint8_t * bitstream = bits;
|
||||||
|
memset(bitstream, 0x00, sizeof(bits));
|
||||||
|
|
||||||
|
manchester_decode(GraphBuffer, LF_TRACE_BUFF_SIZE, bitstream);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -568,22 +575,28 @@ int CmdReadWordPWD(const char *Cmd)
|
||||||
SendCommand(&c);
|
SendCommand(&c);
|
||||||
WaitForResponse(CMD_ACK, NULL);
|
WaitForResponse(CMD_ACK, NULL);
|
||||||
|
|
||||||
size_t bytelength = 4096;
|
uint8_t data[LF_TRACE_BUFF_SIZE];
|
||||||
uint8_t data[bytelength];
|
memset(data, 0x00, LF_TRACE_BUFF_SIZE);
|
||||||
memset(data, 0x00, bytelength);
|
|
||||||
|
|
||||||
GetFromBigBuf(data,bytelength,3560); //3560 -- should be offset..
|
GetFromBigBuf(data,LF_TRACE_BUFF_SIZE,3560); //3560 -- should be offset..
|
||||||
WaitForResponseTimeout(CMD_ACK,NULL, 1500);
|
WaitForResponseTimeout(CMD_ACK,NULL, 1500);
|
||||||
|
|
||||||
for (int j = 0; j < bytelength; j++) {
|
for (int j = 0; j < LF_TRACE_BUFF_SIZE; j++) {
|
||||||
GraphBuffer[j] = ((int)data[j]) - 128;
|
GraphBuffer[j] = ((int)data[j]) - 128;
|
||||||
}
|
}
|
||||||
GraphTraceLen = bytelength;
|
GraphTraceLen = LF_TRACE_BUFF_SIZE;
|
||||||
RepaintGraphWindow();
|
|
||||||
|
|
||||||
manchester_decode(data, bytelength);
|
// BiDirectional
|
||||||
|
//CmdDirectionalThreshold("70 -60");
|
||||||
|
|
||||||
free(data);
|
// Askdemod
|
||||||
|
//Cmdaskdemod("1");
|
||||||
|
|
||||||
|
uint8_t bits[1000];
|
||||||
|
uint8_t * bitstream = bits;
|
||||||
|
memset(bitstream, 0x00, sizeof(bits));
|
||||||
|
|
||||||
|
manchester_decode(GraphBuffer, LF_TRACE_BUFF_SIZE, bitstream);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -636,8 +649,6 @@ int CmdWriteWordPWD(const char *Cmd)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static command_t CommandTable[] =
|
static command_t CommandTable[] =
|
||||||
{
|
{
|
||||||
{"help", CmdHelp, 1, "This help"},
|
{"help", CmdHelp, 1, "This help"},
|
||||||
|
|
|
@ -62,17 +62,16 @@ int CmdReadBlk(const char *Cmd)
|
||||||
GraphTraceLen = LF_TRACE_BUFF_SIZE;
|
GraphTraceLen = LF_TRACE_BUFF_SIZE;
|
||||||
|
|
||||||
// BiDirectional
|
// BiDirectional
|
||||||
CmdDirectionalThreshold("70 -60");
|
//CmdDirectionalThreshold("70 60");
|
||||||
|
|
||||||
// Askdemod
|
// Askdemod
|
||||||
Cmdaskdemod("1");
|
//Cmdaskdemod("1");
|
||||||
|
|
||||||
uint8_t bits[1000];
|
uint8_t bits[1000];
|
||||||
uint8_t * bitstream = bits;
|
uint8_t * bitstream = bits;
|
||||||
uint8_t len = 0;
|
memset(bitstream, 0x00, sizeof(bits));
|
||||||
len = manchester_decode(data, LF_TRACE_BUFF_SIZE, bitstream);
|
|
||||||
if ( len > 0 )
|
manchester_decode(GraphBuffer, LF_TRACE_BUFF_SIZE, bitstream);
|
||||||
PrintPaddedManchester(bitstream, len, 32);
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -113,17 +112,16 @@ int CmdReadBlkPWD(const char *Cmd)
|
||||||
GraphTraceLen = LF_TRACE_BUFF_SIZE;
|
GraphTraceLen = LF_TRACE_BUFF_SIZE;
|
||||||
|
|
||||||
// BiDirectional
|
// BiDirectional
|
||||||
CmdDirectionalThreshold("70 -60");
|
//CmdDirectionalThreshold("70 -60");
|
||||||
|
|
||||||
// Askdemod
|
// Askdemod
|
||||||
Cmdaskdemod("1");
|
//Cmdaskdemod("1");
|
||||||
|
|
||||||
uint8_t bits[1000];
|
uint8_t bits[1000];
|
||||||
uint8_t len = 0;
|
uint8_t * bitstream = bits;
|
||||||
len = manchester_decode(data, LF_TRACE_BUFF_SIZE, bits);
|
memset(bitstream, 0x00, sizeof(bits));
|
||||||
if ( len > 0 )
|
|
||||||
PrintPaddedManchester(bits, len, 32);
|
|
||||||
|
|
||||||
|
manchester_decode(GraphBuffer, LF_TRACE_BUFF_SIZE, bitstream);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -197,16 +195,17 @@ int CmdReadTrace(const char *Cmd)
|
||||||
GraphTraceLen = LF_TRACE_BUFF_SIZE;
|
GraphTraceLen = LF_TRACE_BUFF_SIZE;
|
||||||
|
|
||||||
// BiDirectional
|
// BiDirectional
|
||||||
CmdDirectionalThreshold("70 -60");
|
//CmdDirectionalThreshold("70 -60");
|
||||||
|
|
||||||
// Askdemod
|
// Askdemod
|
||||||
Cmdaskdemod("1");
|
//Cmdaskdemod("1");
|
||||||
|
|
||||||
uint8_t bits[512];
|
|
||||||
uint8_t len = 0;
|
uint8_t bits[1000];
|
||||||
len = manchester_decode(data,LF_TRACE_BUFF_SIZE,bits);
|
uint8_t * bitstream = bits;
|
||||||
if ( len > 0 )
|
memset(bitstream, 0x00, sizeof(bits));
|
||||||
PrintPaddedManchester(bits, len, 64);
|
|
||||||
|
manchester_decode(GraphBuffer, LF_TRACE_BUFF_SIZE, bitstream);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -214,11 +213,11 @@ int CmdReadTrace(const char *Cmd)
|
||||||
static command_t CommandTable[] =
|
static command_t CommandTable[] =
|
||||||
{
|
{
|
||||||
{"help", CmdHelp, 1, "This help"},
|
{"help", CmdHelp, 1, "This help"},
|
||||||
{"readblock", CmdReadBlk, 1, "<Block> -- Read T55xx block data (page 0)"},
|
{"rd", CmdReadBlk, 0, "<Block> -- Read T55xx block data (page 0)"},
|
||||||
{"readblockPWD", CmdReadBlkPWD, 1, "<Block> <Password> -- Read T55xx block data in password mode(page 0)"},
|
{"rdPWD", CmdReadBlkPWD, 0, "<Block> <Password> -- Read T55xx block data in password mode(page 0)"},
|
||||||
{"writeblock", CmdWriteBlk, 1, "<Data> <Block> -- Write T55xx block data (page 0)"},
|
{"wr", CmdWriteBlk, 0, "<Data> <Block> -- Write T55xx block data (page 0)"},
|
||||||
{"writeblockPWD", CmdWriteBlkPWD, 1, "<Data> <Block> <Password> -- Write T55xx block data in password mode(page 0)"},
|
{"wrPWD", CmdWriteBlkPWD, 0, "<Data> <Block> <Password> -- Write T55xx block data in password mode(page 0)"},
|
||||||
{"readtrace", CmdReadTrace, 1, "Read T55xx traceability data (page 1)"},
|
{"trace", CmdReadTrace, 0, "Read T55xx traceability data (page 1)"},
|
||||||
{NULL, NULL, 0, NULL}
|
{NULL, NULL, 0, NULL}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
244
client/ui.c
244
client/ui.c
|
@ -92,24 +92,18 @@ void SetLogFilename(char *fn)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
uint8_t manchester_decode(const uint8_t * data, const size_t len, uint8_t * dataout){
|
int manchester_decode(const int * data, const size_t len, uint8_t * dataout){
|
||||||
|
|
||||||
size_t bytelength = len;
|
int bitlength = 0;
|
||||||
|
int i, clock, high, low, startindex;
|
||||||
uint8_t bitStream[bytelength];
|
low = startindex = 0;
|
||||||
memset(bitStream, 0x00, bytelength);
|
|
||||||
|
|
||||||
int clock,high, low, bit, hithigh, hitlow, first, bit2idx, lastpeak;
|
|
||||||
int i,invert, lastval;
|
|
||||||
int bitidx = 0;
|
|
||||||
int lc = 0;
|
|
||||||
int warnings = 0;
|
|
||||||
high = 1;
|
high = 1;
|
||||||
low = bit = bit2idx = lastpeak = invert = lastval = hithigh = hitlow = first = 0;
|
uint8_t bitStream[len];
|
||||||
clock = 0xFFFF;
|
|
||||||
|
memset(bitStream, 0x00, len);
|
||||||
|
|
||||||
/* Detect high and lows */
|
/* Detect high and lows */
|
||||||
for (i = 0; i < bytelength; i++) {
|
for (i = 0; i < len; i++) {
|
||||||
if (data[i] > high)
|
if (data[i] > high)
|
||||||
high = data[i];
|
high = data[i];
|
||||||
else if (data[i] < low)
|
else if (data[i] < low)
|
||||||
|
@ -117,46 +111,105 @@ uint8_t manchester_decode(const uint8_t * data, const size_t len, uint8_t * data
|
||||||
}
|
}
|
||||||
|
|
||||||
/* get clock */
|
/* get clock */
|
||||||
int j=0;
|
clock = GetT55x7Clock( data, len, high );
|
||||||
for (i = 1; i < bytelength; i++) {
|
startindex = DetectFirstTransition(data, len, high, low);
|
||||||
|
|
||||||
|
PrintAndLog(" Clock : %d", clock);
|
||||||
|
PrintAndLog(" startindex : %d", startindex);
|
||||||
|
|
||||||
|
if (high != 1)
|
||||||
|
bitlength = ManchesterConvertFrom255(data, len, bitStream, high, low, clock, startindex);
|
||||||
|
else
|
||||||
|
bitlength= ManchesterConvertFrom1(data, len, bitStream, clock, startindex);
|
||||||
|
|
||||||
|
if ( bitlength > 0 ){
|
||||||
|
PrintPaddedManchester(bitStream, bitlength, clock);
|
||||||
|
}
|
||||||
|
|
||||||
|
memcpy(dataout, bitStream, bitlength);
|
||||||
|
|
||||||
|
free(bitStream);
|
||||||
|
return bitlength;
|
||||||
|
}
|
||||||
|
|
||||||
|
int GetT55x7Clock( const int * data, const size_t len, int peak ){
|
||||||
|
|
||||||
|
int i,lastpeak,clock;
|
||||||
|
clock = 0xFFFF;
|
||||||
|
lastpeak = 0;
|
||||||
|
|
||||||
|
/* Detect peak if we don't have one */
|
||||||
|
if (!peak) {
|
||||||
|
for (i = 0; i < len; ++i) {
|
||||||
|
if (data[i] > peak) {
|
||||||
|
peak = data[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (i = 1; i < len; ++i) {
|
||||||
/* if this is the beginning of a peak */
|
/* if this is the beginning of a peak */
|
||||||
j = i-1;
|
if ( data[i-1] != data[i] && data[i] == peak) {
|
||||||
if ( data[j] != data[i] &&
|
|
||||||
data[i] == high)
|
|
||||||
{
|
|
||||||
/* find lowest difference between peaks */
|
/* find lowest difference between peaks */
|
||||||
if (lastpeak && i - lastpeak < clock)
|
if (lastpeak && i - lastpeak < clock)
|
||||||
clock = i - lastpeak;
|
clock = i - lastpeak;
|
||||||
lastpeak = i;
|
lastpeak = i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
//return clock;
|
||||||
|
//defaults clock to precise values.
|
||||||
|
switch(clock){
|
||||||
|
case 8:
|
||||||
|
case 16:
|
||||||
|
case 32:
|
||||||
|
case 40:
|
||||||
|
case 50:
|
||||||
|
case 64:
|
||||||
|
case 100:
|
||||||
|
case 128:
|
||||||
|
return clock;
|
||||||
|
break;
|
||||||
|
default: break;
|
||||||
|
}
|
||||||
|
return 32;
|
||||||
|
}
|
||||||
|
|
||||||
int tolerance = clock/4;
|
int DetectFirstTransition(const int * data, const size_t len, int high, int low){
|
||||||
PrintAndLog(" Detected clock: %d",clock);
|
|
||||||
|
|
||||||
/* Detect first transition */
|
int i, retval;
|
||||||
/* Lo-Hi (arbitrary) */
|
retval = 0;
|
||||||
/* skip to the first high */
|
/*
|
||||||
for (i= 0; i < bytelength; i++)
|
Detect first transition Lo-Hi (arbitrary)
|
||||||
|
skip to the first high
|
||||||
|
*/
|
||||||
|
for (i = 0; i < len; ++i)
|
||||||
if (data[i] == high)
|
if (data[i] == high)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
/* now look for the first low */
|
/* now look for the first low */
|
||||||
for (; i < bytelength; i++) {
|
for (; i < len; ++i) {
|
||||||
if (data[i] == low) {
|
if (data[i] == low) {
|
||||||
lastval = i;
|
retval = i;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return retval;
|
||||||
|
}
|
||||||
|
|
||||||
/* If we're not working with 1/0s, demod based off clock */
|
int ManchesterConvertFrom255(const int * data, const size_t len, uint8_t * dataout, int high, int low, int clock, int startIndex){
|
||||||
if (high != 1)
|
|
||||||
{
|
int i, j, hithigh, hitlow, first, bit, bitIndex;
|
||||||
bit = 0; /* We assume the 1st bit is zero, it may not be
|
i = startIndex;
|
||||||
|
bitIndex = 0;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* We assume the 1st bit is zero, it may not be
|
||||||
* the case: this routine (I think) has an init problem.
|
* the case: this routine (I think) has an init problem.
|
||||||
* Ed.
|
* Ed.
|
||||||
*/
|
*/
|
||||||
for (; i < (int)(bytelength / clock); i++)
|
bit = 0;
|
||||||
|
|
||||||
|
for (; i < (int)(len / clock); i++)
|
||||||
{
|
{
|
||||||
hithigh = 0;
|
hithigh = 0;
|
||||||
hitlow = 0;
|
hitlow = 0;
|
||||||
|
@ -185,37 +238,45 @@ uint8_t manchester_decode(const uint8_t * data, const size_t len, uint8_t * data
|
||||||
if (!hithigh || !hitlow)
|
if (!hithigh || !hitlow)
|
||||||
bit ^= 1;
|
bit ^= 1;
|
||||||
|
|
||||||
bitStream[bit2idx++] = bit ^ invert;
|
dataout[bitIndex++] = bit;
|
||||||
}
|
}
|
||||||
|
return bitIndex;
|
||||||
}
|
}
|
||||||
/* standard 1/0 bitstream */
|
|
||||||
else {
|
int ManchesterConvertFrom1(const int * data, const size_t len, uint8_t * dataout, int clock, int startIndex){
|
||||||
|
|
||||||
|
int i,j, bitindex, lc, tolerance, warnings;
|
||||||
|
warnings = 0;
|
||||||
|
int upperlimit = len*2/clock+8;
|
||||||
|
i = startIndex;
|
||||||
|
j = 0;
|
||||||
|
tolerance = clock/4;
|
||||||
|
uint8_t decodedArr[len];
|
||||||
|
|
||||||
/* Then detect duration between 2 successive transitions */
|
/* Then detect duration between 2 successive transitions */
|
||||||
for (bitidx = 1; i < bytelength; i++) {
|
for (bitindex = 1; i < len; i++) {
|
||||||
|
|
||||||
if (data[i-1] != data[i]) {
|
if (data[i-1] != data[i]) {
|
||||||
lc = i-lastval;
|
lc = i - startIndex;
|
||||||
lastval = i;
|
startIndex = i;
|
||||||
|
|
||||||
// Error check: if bitidx becomes too large, we do not
|
// Error check: if bitindex becomes too large, we do not
|
||||||
// have a Manchester encoded bitstream or the clock is really
|
// have a Manchester encoded bitstream or the clock is really wrong!
|
||||||
// wrong!
|
if (bitindex > upperlimit ) {
|
||||||
if (bitidx > (bytelength*2/clock+8) ) {
|
|
||||||
PrintAndLog("Error: the clock you gave is probably wrong, aborting.");
|
PrintAndLog("Error: the clock you gave is probably wrong, aborting.");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
// Then switch depending on lc length:
|
// Then switch depending on lc length:
|
||||||
// Tolerance is 1/4 of clock rate (arbitrary)
|
// Tolerance is 1/4 of clock rate (arbitrary)
|
||||||
if (abs(lc-clock/2) < tolerance) {
|
if (abs((lc-clock)/2) < tolerance) {
|
||||||
// Short pulse : either "1" or "0"
|
// Short pulse : either "1" or "0"
|
||||||
bitStream[bitidx++] = data[i-1];
|
decodedArr[bitindex++] = data[i-1];
|
||||||
} else if (abs(lc-clock) < tolerance) {
|
} else if (abs(lc-clock) < tolerance) {
|
||||||
// Long pulse: either "11" or "00"
|
// Long pulse: either "11" or "00"
|
||||||
bitStream[bitidx++] = data[i-1];
|
decodedArr[bitindex++] = data[i-1];
|
||||||
bitStream[bitidx++] = data[i-1];
|
decodedArr[bitindex++] = data[i-1];
|
||||||
} else {
|
} else {
|
||||||
// Error
|
++warnings;
|
||||||
warnings++;
|
|
||||||
PrintAndLog("Warning: Manchester decode error for pulse width detection.");
|
PrintAndLog("Warning: Manchester decode error for pulse width detection.");
|
||||||
if (warnings > 10) {
|
if (warnings > 10) {
|
||||||
PrintAndLog("Error: too many detection errors, aborting.");
|
PrintAndLog("Error: too many detection errors, aborting.");
|
||||||
|
@ -224,23 +285,23 @@ uint8_t manchester_decode(const uint8_t * data, const size_t len, uint8_t * data
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
// At this stage, we now have a bitstream of "01" ("1") or "10" ("0"), parse it into final decoded bitstream
|
/*
|
||||||
// Actually, we overwrite BitStream with the new decoded bitstream, we just need to be careful
|
* We have a decodedArr of "01" ("1") or "10" ("0")
|
||||||
// to stop output at the final bitidx2 value, not bitidx
|
* parse it into final decoded dataout
|
||||||
for (i = 0; i < bitidx; i += 2) {
|
*/
|
||||||
if ((bitStream[i] == 0) && (bitStream[i+1] == 1)) {
|
for (i = 0; i < bitindex; i += 2) {
|
||||||
bitStream[bit2idx++] = 1 ^ invert;
|
|
||||||
}
|
if ((decodedArr[i] == 0) && (decodedArr[i+1] == 1)) {
|
||||||
else if ((bitStream[i] == 1) && (bitStream[i+1] == 0)) {
|
dataout[j++] = 1;
|
||||||
bitStream[bit2idx++] = 0 ^ invert;
|
} else if ((decodedArr[i] == 1) && (decodedArr[i+1] == 0)) {
|
||||||
}
|
dataout[j++] = 0;
|
||||||
else {
|
} else {
|
||||||
// We cannot end up in this state, this means we are unsynchronized,
|
|
||||||
// move up 1 bit:
|
|
||||||
i++;
|
i++;
|
||||||
warnings++;
|
warnings++;
|
||||||
PrintAndLog("Unsynchronized, resync...");
|
PrintAndLog("Unsynchronized, resync...");
|
||||||
|
PrintAndLog("(too many of those messages mean the stream is not Manchester encoded)");
|
||||||
|
|
||||||
if (warnings > 10) {
|
if (warnings > 10) {
|
||||||
PrintAndLog("Error: too many decode errors, aborting.");
|
PrintAndLog("Error: too many decode errors, aborting.");
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -248,25 +309,48 @@ uint8_t manchester_decode(const uint8_t * data, const size_t len, uint8_t * data
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// PrintAndLog(" Manchester decoded bitstream : %d bits", (bit2idx-16));
|
PrintAndLog("%s", sprint_hex(dataout, j));
|
||||||
// uint8_t mod = (bit2idx-16) % blocksize;
|
return j;
|
||||||
// uint8_t div = (bit2idx-16) / blocksize;
|
|
||||||
|
|
||||||
// // Now output the bitstream to the scrollback by line of 16 bits
|
|
||||||
// for (i = 0; i < div*blocksize; i+=blocksize) {
|
|
||||||
// PrintAndLog(" %s", sprint_bin(bitStream+i,blocksize) );
|
|
||||||
// }
|
|
||||||
// if ( mod > 0 ){
|
|
||||||
// PrintAndLog(" %s", sprint_bin(bitStream+i, mod) );
|
|
||||||
// }
|
|
||||||
|
|
||||||
if ( bit2idx > 0 )
|
|
||||||
memcpy(dataout, bitStream, bit2idx);
|
|
||||||
|
|
||||||
free(bitStream);
|
|
||||||
return bit2idx;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ManchesterDiffDecodedString(const uint8_t* bitstream, size_t len, uint8_t invert){
|
||||||
|
/*
|
||||||
|
* We have a bitstream of "01" ("1") or "10" ("0")
|
||||||
|
* parse it into final decoded bitstream
|
||||||
|
*/
|
||||||
|
int i, j, warnings;
|
||||||
|
uint8_t decodedArr[(len/2)+1];
|
||||||
|
|
||||||
|
j = warnings = 0;
|
||||||
|
|
||||||
|
uint8_t lastbit = 0;
|
||||||
|
|
||||||
|
for (i = 0; i < len; i += 2) {
|
||||||
|
|
||||||
|
uint8_t first = bitstream[i];
|
||||||
|
uint8_t second = bitstream[i+1];
|
||||||
|
|
||||||
|
if ( first == second ) {
|
||||||
|
++i;
|
||||||
|
++warnings;
|
||||||
|
if (warnings > 10) {
|
||||||
|
PrintAndLog("Error: too many decode errors, aborting.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if ( lastbit != first ) {
|
||||||
|
decodedArr[j++] = 0 ^ invert;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
decodedArr[j++] = 1 ^ invert;
|
||||||
|
}
|
||||||
|
lastbit = second;
|
||||||
|
}
|
||||||
|
|
||||||
|
PrintAndLog("%s", sprint_hex(decodedArr, j));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void PrintPaddedManchester( uint8_t* bitStream, size_t len, size_t blocksize){
|
void PrintPaddedManchester( uint8_t* bitStream, size_t len, size_t blocksize){
|
||||||
|
|
||||||
PrintAndLog(" Manchester decoded bitstream : %d bits", len);
|
PrintAndLog(" Manchester decoded bitstream : %d bits", len);
|
||||||
|
|
|
@ -25,6 +25,11 @@ extern int PlotGridX, PlotGridY, PlotGridXdefault, PlotGridYdefault;
|
||||||
extern int offline;
|
extern int offline;
|
||||||
extern int flushAfterWrite; //buzzy
|
extern int flushAfterWrite; //buzzy
|
||||||
|
|
||||||
uint8_t manchester_decode(const uint8_t * data, const size_t len, uint8_t * dataout);
|
int manchester_decode(const int * data, const size_t len, uint8_t * dataout);
|
||||||
|
int GetT55x7Clock( const int * data, const size_t len, int high );
|
||||||
|
int DetectFirstTransition(const int * data, const size_t len, int high, int low);
|
||||||
void PrintPaddedManchester( uint8_t * bitStream, size_t len, size_t blocksize);
|
void PrintPaddedManchester( uint8_t * bitStream, size_t len, size_t blocksize);
|
||||||
|
void ManchesterDiffDecodedString( const uint8_t *bitStream, size_t len, uint8_t invert );
|
||||||
|
int ManchesterConvertFrom255(const int * data, const size_t len, uint8_t * dataout, int high, int low, int clock, int startIndex);
|
||||||
|
int ManchesterConvertFrom1(const int * data, const size_t len, uint8_t * dataout, int clock, int startIndex);
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue