Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bigbufferbinstable #207

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
160 changes: 83 additions & 77 deletions Firmware/ChameleonMini/Application/MifareClassic.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,23 +161,6 @@ enum estate {
/* Init to get sure we have a controlled value wherever we start */
static enum estate State = STATE_IDLE;

#ifdef CONFIG_MF_CLASSIC_LOG_SUPPORT
char *estate_str[] = {
"HALT",
"IDLE",
"CHINESE_IDLE",
"CHINESE_WRITE",
"READY",
"ACTIVE",
"AUTHING",
"AUTHED_IDLE",
"WRITE",
"INCREMENT",
"DECREMENT",
"RESTORE"
};
#endif

static uint8_t CardResponse[MFCLASSIC_MEM_NONCE_SIZE];
static uint8_t ReaderResponse[MFCLASSIC_MEM_NONCE_SIZE];
static uint8_t CurrentAddress;
Expand Down Expand Up @@ -208,13 +191,15 @@ static uint32_t BruteCurrentUid = 0;
#endif
#ifdef CONFIG_MF_CLASSIC_LOG_SUPPORT
static bool isLogEnabled = false;
static uint32_t LogBytesWrote = 0;
static uint16_t LogBytesBuffered = 0;
static uint32_t LogMaxBytes = 0;
static uint8_t LogLineBufferA[MFCLASSIC_LOG_MEM_LINE_BUFFER_LEN] = { 0 };
static uint8_t LogLineBufferB[MFCLASSIC_LOG_MEM_LINE_BUFFER_LEN] = { 0 };
static uint8_t * LogLineBuffer = LogLineBufferA;
static bool LogLineBufferFirst = true;
static uint32_t LogBytesWrote = 0;
static uint8_t LogRecordBuffer[MFCLASSIC_LOG_MEM_BUFFER_LEN] = { 0 };
static uint8_t LogUnwrittendTicks = 0;
#ifdef CONFIG_MF_CLASSIC_LOG_ADAPTIVE_TRIM
static uint16_t LogTickCounter = 0;
static uint16_t PreviousLogTick = 0;
static uint8_t LogTrimLevel = MFCLASSIC_LOG_TRIM_NONE;
#endif
#endif

/* decode Access conditions for a block */
Expand Down Expand Up @@ -314,7 +299,8 @@ uint32_t BytesToUint32(uint8_t * Buffer) {
}

uint16_t BytesToUint16(uint8_t * Buffer) {
return ( (((uint16_t)(Buffer[0])) << 8) | ((uint16_t)(Buffer[1])) );
return ( (((uint16_t)(Buffer[0])) << 8)
| ((uint16_t)(Buffer[1])) );
}

void Uint32ToBytes(uint32_t Uint32, uint8_t * Buffer) {
Expand Down Expand Up @@ -442,74 +428,94 @@ void MifareClassicAppLogWriteHeader(void) {
AppWorkingMemoryWrite(headerLine, MFCLASSIC_LOG_MEM_LOG_HEADER_ADDR, MFCLASSIC_LOG_MEM_LOG_HEADER_LEN);
}

void MifareClassicAppLogBufferLine(const uint8_t * Data, uint16_t BitCount, uint8_t Source) {
uint16_t dataBytesToBuffer = (BitCount / BITS_PER_BYTE);
if(BitCount % BITS_PER_BYTE) dataBytesToBuffer++;

uint16_t logStateStrLen = strlen(estate_str[State]);
uint16_t idx = LogBytesBuffered+MFCLASSIC_LOG_MEM_LINE_START_ADDR;

if( (idx + dataBytesToBuffer*2 + logStateStrLen + 14) < MFCLASSIC_LOG_MEM_LINE_BUFFER_LEN) {
idx += sprintf((char *)&LogLineBuffer[idx],"[%05u] %c:\t%s\t| ",SystemGetSysTick(),Source,estate_str[State]);
BufferToHexString((char *)&LogLineBuffer[idx],dataBytesToBuffer*2+1,Data,dataBytesToBuffer);
idx += dataBytesToBuffer*2;
idx += sprintf((char *)&LogLineBuffer[idx]," ;");
void MifareClassicAppLogBufferFlush(void){
/* circular log */
if( (LogBytesWrote + LogBytesBuffered) >= (AppWorkingMemorySize() - MFCLASSIC_LOG_MEM_LOG_HEADER_LEN)) {
LogBytesWrote = 0;
}
if(MFCLASSIC_LOG_MEM_LINE_BUFFER_LEN - idx > 2){
idx += sprintf((char *)&LogLineBuffer[idx],"\r\n");
/* write log */
AppWorkingMemoryWrite(LogRecordBuffer, MFCLASSIC_LOG_MEM_LOG_HEADER_LEN+LogBytesWrote, LogBytesBuffered);
LogBytesWrote += LogBytesBuffered;
LogBytesBuffered = 0;
/* update header */
MifareClassicAppLogWriteHeader();
LogUnwrittendTicks = 0;
#ifdef CONFIG_MF_CLASSIC_LOG_ADAPTIVE_TRIM
if ((LogTickCounter - PreviousLogTick) <= MFCLASSIC_LOG_ADAPTIVE_TICK_INT && LogTrimLevel <= MFCLASSIC_LOG_MAX_TRIM_LEVEL) {
LogTrimLevel++;
}
LogBytesBuffered = idx;
PreviousLogTick = LogTickCounter;
#endif
}

void MifareClassicAppLogWriteLines(void) {
uint16_t LogBytesToWrite = LogBytesBuffered;
uint8_t * LogLineBufferToWrite = LogLineBuffer;
/* swap buffers */
if ( LogLineBufferFirst ) {
LogLineBuffer = LogLineBufferB;
LogLineBufferFirst = false;
} else {
LogLineBuffer = LogLineBufferA;
LogLineBufferFirst = true;
}
LogBytesBuffered = 0;
void MifareClassicAppLogBufferedRecordWrite(const uint8_t * Data, uint16_t BitCount, uint8_t Source) {
uint16_t timeNow = SystemGetSysTick();
uint8_t dataBytesToBuffer = (BitCount / BITS_PER_BYTE);
if(BitCount % BITS_PER_BYTE) dataBytesToBuffer++;
uint16_t idx = LogBytesBuffered;

if( isLogEnabled && (LogBytesToWrite > 0) ) {
/* circular log */
if( (LogBytesWrote + LogBytesToWrite) >= LogMaxBytes) {
LogBytesWrote = 0;
}
/* write log */
AppWorkingMemoryWrite(LogLineBufferToWrite, MFCLASSIC_LOG_MEM_LOG_HEADER_LEN+LogBytesWrote, LogBytesToWrite);
/* update header */
LogBytesWrote += LogBytesToWrite;
MifareClassicAppLogWriteHeader();
#ifdef CONFIG_MF_CLASSIC_LOG_ADAPTIVE_TRIM
if (LogTrimLevel == MFCLASSIC_LOG_TRIM_ALL_TAG_RECORDS && Source == MFCLASSIC_LOG_TAG) return;
#endif
if( (LogBytesBuffered + MFCLASSIC_LOG_RECORD_HEADER_LEN) < MFCLASSIC_LOG_MEM_BUFFER_LEN) {
#ifdef CONFIG_MF_CLASSIC_LOG_ADAPTIVE_TRIM
if( (LogTrimLevel == MFCLASSIC_LOG_TRIM_TAG_LONG_DATA && Source == MFCLASSIC_LOG_TAG && dataBytesToBuffer >= 9)
|| (LogTrimLevel == MFCLASSIC_LOG_TRIM_ALL_TAG_DATA && Source == MFCLASSIC_LOG_TAG)) {
Source = '+';
dataBytesToBuffer=0;
}
#endif
LogRecordBuffer[idx] = dataBytesToBuffer + MFCLASSIC_LOG_RECORD_HEADER_LEN;
idx += MFCLASSIC_LOG_MEM_CHAR_LEN;
Uint16ToBytes(timeNow, &LogRecordBuffer[idx]);
idx += MFCLASSIC_LOG_MEM_RECORD_TIMESTAMP_LEN;
LogRecordBuffer[idx] = Source;
idx += MFCLASSIC_LOG_MEM_CHAR_LEN;
LogRecordBuffer[idx] = State;
idx += MFCLASSIC_LOG_MEM_CHAR_LEN;
memcpy(&LogRecordBuffer[idx], Data, dataBytesToBuffer);
idx += dataBytesToBuffer;
}
LogBytesBuffered = idx;
/* if there is no space left for another record, let's flush the buffer */
if ((MFCLASSIC_LOG_MEM_BUFFER_LEN - LogBytesBuffered) < MFCLASSIC_LOG_MAX_RECORD_LENGHT){
MifareClassicAppLogBufferFlush();
}
}

void MifareClassicAppLogStop(void) {
isLogEnabled = false;
MifareClassicAppLogWriteHeader();
}

void MifareClassicAppLogStart(void) {
isLogEnabled = true;
MifareClassicAppLogWriteHeader();
void MifareClassicAppLogTick(void) {
/* Flush buffer if there is still some data after a while since last log */
if (LogBytesBuffered > 0){
if (LogUnwrittendTicks >= MFCLASSIC_LOG_MAX_TICK_UNWRITTEN){
MifareClassicAppLogBufferFlush();
}else{
LogUnwrittendTicks++;
}
}
#ifdef CONFIG_MF_CLASSIC_LOG_ADAPTIVE_TRIM
else if ((LogTickCounter - PreviousLogTick) >= MFCLASSIC_LOG_ADAPTIVE_TICK_RST){
LogTrimLevel = MFCLASSIC_LOG_TRIM_NONE;
PreviousLogTick = 0;
LogTickCounter = 0;
}
LogTickCounter++;
#endif
}

void MifareClassicAppLogInit(void) {
MifareClassicAppInit(MFCLASSIC_1K_ATQA_VALUE, MFCLASSIC_1K_SAK_VALUE, false);
MifareClassicAppLogCheck();
LogMaxBytes = ( AppWorkingMemorySize() - MFCLASSIC_LOG_MEM_LOG_HEADER_LEN );
LogBytesBuffered = 0;
#ifdef CONFIG_MF_CLASSIC_LOG_ADAPTIVE_TRIM
LogTrimLevel = MFCLASSIC_LOG_TRIM_NONE;
PreviousLogTick = 0;
LogTickCounter = 0;
#endif
}

void MifareClassicAppLogToggle(void) {
if(isLogEnabled) {
MifareClassicAppLogStop();
} else {
MifareClassicAppLogStart();
}
isLogEnabled ^= true;
MifareClassicAppLogWriteHeader();
}
#endif

Expand Down Expand Up @@ -674,7 +680,7 @@ uint16_t MifareClassicAppProcess(uint8_t* Buffer, uint16_t BitCount) {
#ifdef CONFIG_MF_CLASSIC_LOG_SUPPORT
/* Log what comes from reader if logging enabled */
if(isLogEnabled) {
MifareClassicAppLogBufferLine(Buffer, BitCount, MFCLASSIC_LOG_READER);
MifareClassicAppLogBufferedRecordWrite(Buffer, BitCount, MFCLASSIC_LOG_READER);
}
#endif
/* Size of data (byte) we will send back to reader. Is main process return value */
Expand Down Expand Up @@ -1053,7 +1059,7 @@ uint16_t MifareClassicAppProcess(uint8_t* Buffer, uint16_t BitCount) {
#ifdef CONFIG_MF_CLASSIC_LOG_SUPPORT
/* Log what goes from tag if logging enabled */
if(isLogEnabled) {
MifareClassicAppLogBufferLine(Buffer, (retSize & ISO14443A_APP_CUSTOM_PARITY) ? (retSize & ~ISO14443A_APP_CUSTOM_PARITY) : (retSize), MFCLASSIC_LOG_TAG);
MifareClassicAppLogBufferedRecordWrite(Buffer, (retSize & ISO14443A_APP_CUSTOM_PARITY) ? (retSize & ~ISO14443A_APP_CUSTOM_PARITY) : (retSize), MFCLASSIC_LOG_TAG);
}
#endif
return retSize;
Expand Down
42 changes: 29 additions & 13 deletions Firmware/ChameleonMini/Application/MifareClassic.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,13 +198,6 @@ C1 C2 C3 read write increment decrement,
#endif

#ifdef CONFIG_MF_CLASSIC_LOG_SUPPORT
#define MFCLASSIC_LOG_LINE_START 0x3E
#define MFCLASSIC_LOG_LINE_END 0x3B
#define MFCLASSIC_LOG_TAB 0x09
#define MFCLASSIC_LOG_EOL_CR 0x0D
#define MFCLASSIC_LOG_EOL_LF 0x0A
#define MFCLASSIC_LOG_EOS 0x00
#define MFCLASSIC_LOG_SEPARATOR 0x21
#define MFCLASSIC_LOG_READER 0x52
#define MFCLASSIC_LOG_TAG 0x54
#define MFCLASSIC_LOG_MEM_CHAR_LEN 1
Expand All @@ -213,14 +206,28 @@ C1 C2 C3 read write increment decrement,
#define MFCLASSIC_LOG_MEM_STATUS_RESET 0x70
#define MFCLASSIC_LOG_MEM_STATUS_LEN 1
#define MFCLASSIC_LOG_MEM_WROTEBYTES_ADDR 12
#define MFCLASSIC_LOG_MEM_WROTEBYTES_LEN sizeof(uint32_t)
#define MFCLASSIC_LOG_MEM_WROTEBYTES_LEN 4 /* sizeof(uint32_t) */
#define MFCLASSIC_LOG_MEM_LOG_HEADER_ADDR 0
#define MFCLASSIC_LOG_MEM_LOG_HEADER_LEN 16
#define MFCLASSIC_LOG_MEM_LINE_BUFFER_LEN 256
#define MFCLASSIC_LOG_MEM_LINE_START_ADDR 0
#define MFCLASSIC_LOG_MEM_LINE_TIMESTAMP_LEN sizeof(uint16_t)
#define MFCLASSIC_LOG_LINE_OVERHEAD (MFCLASSIC_LOG_MEM_LINE_TIMESTAMP_LEN+MFCLASSIC_LOG_MEM_CHAR_LEN*10)
#define MFCLASSIC_LOG_MEM_BUFFER_LEN 1400
#define MFCLASSIC_LOG_MEM_RECORD_START_ADDR 0
#define MFCLASSIC_LOG_MEM_RECORD_TIMESTAMP_LEN 2 /* sizeof(uint16_t) */
#define MFCLASSIC_LOG_RECORD_HEADER_LEN 5 /* (MFCLASSIC_LOG_MEM_RECORD_TIMESTAMP_LEN+MFCLASSIC_LOG_MEM_CHAR_LEN*3) */
#define MFCLASSIC_LOG_BUFFER_OVERFLOW 0x0F
#define MFCLASSIC_LOG_MIN_RECORD_LENGHT 5
#define MFCLASSIC_LOG_MAX_RECORD_LENGHT 23
#define MFCLASSIC_LOG_MAX_TICK_UNWRITTEN 5

#ifdef CONFIG_MF_CLASSIC_LOG_ADAPTIVE_TRIM
#define MFCLASSIC_LOG_TRIM_NONE 0
#define MFCLASSIC_LOG_TRIM_TAG_LONG_DATA 1
#define MFCLASSIC_LOG_TRIM_ALL_TAG_DATA 2
#define MFCLASSIC_LOG_TRIM_ALL_TAG_RECORDS 3
#define MFCLASSIC_LOG_MAX_TRIM_LEVEL 3
#define MFCLASSIC_LOG_ADAPTIVE_TICK_INT 5
#define MFCLASSIC_LOG_ADAPTIVE_TICK_RST 20
#endif

#endif

void MifareClassicAppInit1K(void);
Expand Down Expand Up @@ -251,8 +258,17 @@ void MifareClassicAppBruteToggle(void);

#ifdef CONFIG_MF_CLASSIC_LOG_SUPPORT
void MifareClassicAppLogInit(void);
void MifareClassicAppLogWriteLines(void);
void MifareClassicAppLogTick(void);
void MifareClassicAppLogToggle(void);
void MifareClassicAppLogCheck(void);
void MifareClassicAppLogWriteHeader(void);
#endif

#if defined(CONFIG_MF_CLASSIC_BRUTE_SUPPORT) || defined(CONFIG_MF_CLASSIC_LOG_SUPPORT)
uint32_t BytesToUint32(uint8_t * Buffer);
uint16_t BytesToUint16(uint8_t * Buffer);
void Uint32ToBytes(uint32_t Uint32, uint8_t * Buffer);
void Uint16ToBytes(uint16_t Uint16, uint8_t * Buffer);
#endif

#endif /* MIFARECLASSIC_H_ */
Expand Down
2 changes: 1 addition & 1 deletion Firmware/ChameleonMini/Configuration.c
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ static const PROGMEM ConfigurationType ConfigurationTable[] = {
.ApplicationInitFunc = MifareClassicAppLogInit,
.ApplicationResetFunc = MifareClassicAppReset,
.ApplicationTaskFunc = ApplicationTaskDummy,
.ApplicationTickFunc = MifareClassicAppLogWriteLines,
.ApplicationTickFunc = MifareClassicAppLogTick,
.ApplicationButtonFunc = MifareClassicAppLogToggle,
.ApplicationProcessFunc = MifareClassicAppProcess,
.ApplicationGetUidFunc = MifareClassicGetUid,
Expand Down
7 changes: 7 additions & 0 deletions Firmware/ChameleonMini/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ SETTINGS += -DCONFIG_MF_CLASSIC_DETECTION_SUPPORT
# SETTINGS += -DCONFIG_MF_CLASSIC_BRUTE_SUPPORT
# SETTINGS += -DCONFIG_MF_CLASSIC_LOG_SUPPORT

#Extra log subsystem options. CONFIG_MF_CLASSIC_LOG_SUPPORT must be enabled.
#SETTINGS += -DCONFIG_MF_CLASSIC_LOG_ADAPTIVE_TRIM
#SETTINGS += -DCONFIG_MF_CLASSIC_LOGPRINT_COMMAND

#Support magic mode on mifare classic configuration
# SETTINGS += -DSUPPORT_MF_CLASSIC_MAGIC_MODE

Expand All @@ -38,6 +42,9 @@ SETTINGS += -DSUPPORT_FIRMWARE_UPGRADE
#SETTINGS += -DCONFIG_DEBUG_MEMORYTEST_COMMAND
#SETTINGS += -DCONFIG_DEBUG_MEMORYINFO_COMMAND

#Enable WorkingMemory manipulation commands (WORKMEM, WORKMEMUPLOAD, WORKMEMDOWNLOAD)
#SETTINGS += -DCONFIG_ENABLE_WORKMEM_COMMANDS

#Default configuration
SETTINGS += -DDEFAULT_CONFIGURATION=CONFIG_NONE

Expand Down
39 changes: 26 additions & 13 deletions Firmware/ChameleonMini/Terminal/CommandLine.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ const PROGMEM CommandEntryType CommandTable[] = {
.SetFunc = NO_FUNCTION,
.GetFunc = CommandGetMemSize
},
#ifdef CONFIG_ENABLE_WORKMEM_COMMANDS
{
.Command = COMMAND_WORKMEM,
.ExecFunc = CommandExecWorkingMem,
Expand All @@ -140,6 +141,7 @@ const PROGMEM CommandEntryType CommandTable[] = {
.SetFunc = NO_FUNCTION,
.GetFunc = NO_FUNCTION
},
#endif
{
.Command = COMMAND_UIDSIZE,
.ExecFunc = NO_FUNCTION,
Expand All @@ -161,14 +163,7 @@ const PROGMEM CommandEntryType CommandTable[] = {
.SetFunc = CommandSetButtonLong,
.GetFunc = CommandGetButtonLong
},
/*
{
.Command = COMMAND_LOGMODE,
.ExecFunc = NO_FUNCTION,
.ExecParamFunc = NO_FUNCTION,
.SetFunc = CommandSetLogMode,
.GetFunc = CommandGetLogMode
},
#ifdef CONFIG_MF_CLASSIC_LOG_SUPPORT
{
.Command = COMMAND_LOGMEM,
.ExecFunc = NO_FUNCTION,
Expand All @@ -178,21 +173,39 @@ const PROGMEM CommandEntryType CommandTable[] = {
},
{
.Command = COMMAND_LOGDOWNLOAD,
.ExecFunc = CommandExecLogDownload,
.ExecFunc = CommandExecWorkingMemDownload,
.ExecParamFunc = NO_FUNCTION,
.SetFunc = NO_FUNCTION,
.GetFunc = NO_FUNCTION
},
{
.Command = COMMAND_STORELOG,
.ExecFunc = CommandExecStoreLog,
.Command = COMMAND_LOGCLEAR,
.ExecFunc = CommandExecLogClear,
.ExecParamFunc = NO_FUNCTION,
.SetFunc = NO_FUNCTION,
.GetFunc = NO_FUNCTION
},
#ifdef CONFIG_MF_CLASSIC_LOGPRINT_COMMAND
{
.Command = COMMAND_LOGCLEAR,
.ExecFunc = CommandExecLogClear,
.Command = COMMAND_LOGPRINT,
.ExecFunc = NO_FUNCTION,
.ExecParamFunc = NO_FUNCTION,
.SetFunc = NO_FUNCTION,
.GetFunc = CommandGetLog
},
#endif
#endif
/*
{
.Command = COMMAND_LOGMODE,
.ExecFunc = NO_FUNCTION,
.ExecParamFunc = NO_FUNCTION,
.SetFunc = CommandSetLogMode,
.GetFunc = CommandGetLogMode
},
{
.Command = COMMAND_STORELOG,
.ExecFunc = CommandExecStoreLog,
.ExecParamFunc = NO_FUNCTION,
.SetFunc = NO_FUNCTION,
.GetFunc = NO_FUNCTION
Expand Down
Loading