First pass rewrite of flashmem driver for optimization. Lot of changes here. Provides PoC of saving and recalling a tag in Standalone mode. Added some printing passthrough to client to azccomodate for vt100 eye-candyness. FastREAD mode implemented for flashmem, testable from client. Beta but functionnal. Reading the whole flash with 1Kb to 32kb buffers was ~730ms, now 380ms Max (even at 24Mhz spi baudrate)

This commit is contained in:
Colin J. Brigato 2018-09-03 00:02:44 +02:00
commit 8d673fa1bf
8 changed files with 865 additions and 292 deletions

View file

@ -33,10 +33,6 @@
#include "i2c.h"
#endif
#ifdef WITH_FPC
#include "usart.h"
#endif
//=============================================================================
// A buffer where we can queue things up to be sent through the FPGA, for
// any purpose (fake tag, as reader, whatever). We go MSB first, since that
@ -78,26 +74,24 @@ void PrintToSendBuffer(void) {
}
void print_result(char *name, uint8_t *buf, size_t len) {
uint8_t *p = buf;
uint16_t tmp = len & 0xFFF0;
for(; p-buf < tmp; p += 16) {
Dbprintf("[%s: %02d/%02d] %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
if ( len % 16 == 0 ) {
for(; p-buf < len; p += 16)
Dbprintf("[%s:%d/%d] %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
name,
p-buf,
len,
p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]
);
}
if (len % 16 != 0) {
char s[46] = {0};
char *sp = s;
for (; p-buf < len; p++ ) {
sprintf(sp, "%02x ", p[0] );
sp += 3;
}
Dbprintf("[%s: %02d/%02d] %s", name, p-buf, len, s);
else {
for(; p-buf < len; p += 8)
Dbprintf("[%s:%d/%d] %02x %02x %02x %02x %02x %02x %02x %02x",
name,
p-buf,
len,
p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
}
}
@ -107,7 +101,7 @@ void print_result(char *name, uint8_t *buf, size_t len) {
void DbpStringEx(char *str, uint32_t cmd) {
#if DEBUG
uint8_t len = strlen(str);
byte_t len = strlen(str);
cmd_send(CMD_DEBUG_PRINT_STRING, len, cmd, 0, (byte_t*)str, len);
#endif
}
@ -119,7 +113,7 @@ void DbpString(char *str) {
}
#if 0
void DbpIntegers(int x1, int x2, int x3) {
void DbpIntegers(inst x1, int x2, int x3) {
cmd_send(CMD_DEBUG_PRINT_INTEGERS,x1,x2,x3,0,0);
}
#endif
@ -440,7 +434,7 @@ void printStandAloneModes(void) {
DbpString(" LF HID corporate 1000 bruteforce - (Federico dotta & Maurizio Agazzini)");
#endif
#if defined(WITH_HF_MATTYRUN)
DbpString(" HF Mifare sniff/clone - aka MattyRun (Matías A. Ré Medina)");
DbpString(" HF Mifare sniff/clone - aka MattyRun (Mat<EFBFBD>as A. R<> Medina)");
#endif
#if defined(WITH_HF_COLIN)
DbpString(" HF Mifare ultra fast sniff/sim/clone - aka VIGIKPWN (Colin Brigato)");
@ -448,7 +442,6 @@ void printStandAloneModes(void) {
//DbpString("Running ");
//Dbprintf(" Is Device attached to USB| %s", USB_ATTACHED() ? "Yes" : "No");
//Dbprintf(" Is Device attached to FPC| %s", 0 ? "Yes" : "No");
//Dbprintf(" Is USB_reconnect value | %d", GetUSBreconnect() );
//Dbprintf(" Is USB_configured value | %d", GetUSBconfigured() );
@ -1077,7 +1070,6 @@ void UsbPacketReceived(uint8_t *packet, int len) {
#endif
case CMD_BUFF_CLEAR:
BigBuf_Clear();
BigBuf_free();
break;
case CMD_MEASURE_ANTENNA_TUNING:
@ -1114,7 +1106,7 @@ void UsbPacketReceived(uint8_t *packet, int len) {
len = MIN( (numofbytes - i), USB_CMD_DATA_SIZE);
isok = cmd_send(CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K, i, len, BigBuf_get_traceLen(), mem + startidx + i, len);
if (!isok)
Dbprintf("transfer to client failed :: | bytes between %d - %d (%d)", i, i+len, len);
Dbprintf("transfer to client failed :: | bytes between %d - %d", i, len);
}
// Trigger a finish downloading signal with an ACK frame
// iceman, when did sending samplingconfig array got attached here?!?
@ -1160,7 +1152,7 @@ void UsbPacketReceived(uint8_t *packet, int len) {
len = MIN((numofbytes - i), USB_CMD_DATA_SIZE);
isok = cmd_send(CMD_DOWNLOADED_EML_BIGBUF, i, len, 0, mem + startidx + i, len);
if (!isok)
Dbprintf("transfer to client failed :: | bytes between %d - %d (%d)", i, i+len, len);
Dbprintf("transfer to client failed :: | bytes between %d - %d", i, len);
}
// Trigger a finish downloading signal with an ACK frame
cmd_send(CMD_ACK, 1, 0, 0, 0, 0);
@ -1177,6 +1169,7 @@ void UsbPacketReceived(uint8_t *packet, int len) {
uint16_t isok = 0;
uint32_t startidx = c->arg[0];
uint16_t len = c->arg[1];
uint8_t fast = c->arg[2];
Dbprintf("FlashMem read | %d - %d", startidx, len);
@ -1184,14 +1177,23 @@ void UsbPacketReceived(uint8_t *packet, int len) {
uint8_t *mem = BigBuf_malloc(size);
if (fast) {
FlashInit(1);
//FlashInit();
Flash_CheckBusy(BUSY_TIMEOUT);
}
for(size_t i = 0; i < len; i += size) {
len = MIN((len - i), size);
memset(mem, 0, len);
Dbprintf("FlashMem reading | %d | %d | %d", startidx + i, i, len);
if (!fast){
isok = Flash_ReadData(startidx + i, mem, len);
}
if (fast){
isok = Flash_FastReadDataCont(startidx + i, mem, len);
}
if ( isok == len ) {
print_result("Chunk: ", mem, len);
} else {
@ -1199,6 +1201,9 @@ void UsbPacketReceived(uint8_t *packet, int len) {
break;
}
}
if (fast){
FlashStop();
}
LED_B_OFF();
break;
}
@ -1271,21 +1276,36 @@ void UsbPacketReceived(uint8_t *packet, int len) {
size_t len = 0;
uint32_t startidx = c->arg[0];
uint32_t numofbytes = c->arg[1];
uint8_t fast = c->arg[2];
// arg0 = startindex
// arg1 = length bytes to transfer
// arg2 = RFU
if (fast) {
FlashInit(1);
//FlashInit();
Flash_CheckBusy(BUSY_TIMEOUT);
}
for (size_t i = 0; i < numofbytes; i += USB_CMD_DATA_SIZE) {
len = MIN((numofbytes - i), USB_CMD_DATA_SIZE);
if (!fast){
isok = Flash_ReadData(startidx + i, mem, len);
}
if (fast){
isok = Flash_FastReadDataCont(startidx + i, mem, len);
}
if (!isok )
Dbprintf("reading flash memory failed :: | bytes between %d - %d", i, len);
isok = cmd_send(CMD_DOWNLOADED_FLASHMEM, i, len, 0, mem, len);
if (!isok)
Dbprintf("transfer to client failed :: | bytes between %d - %d (%d)", i, i+len, len);
Dbprintf("transfer to client failed :: | bytes between %d - %d", i, len);
}
if (fast){
FlashStop();
}
cmd_send(CMD_ACK, 1, 0, 0, 0, 0);
LED_B_OFF();
break;
@ -1297,7 +1317,7 @@ void UsbPacketReceived(uint8_t *packet, int len) {
bool isok = Flash_ReadData(FLASH_MEM_SIGNATURE_OFFSET, info->signature, FLASH_MEM_SIGNATURE_LEN);
if (FlashInit()) {
if (FlashInit(0)) {
Flash_UniqueID( info->flashid);
FlashStop();
}
@ -1392,8 +1412,6 @@ void __attribute__((noreturn)) AppMain(void) {
LEDsoff();
usb_enable();
// The FPGA gets its clock from us from PCK0 output, so set that up.
AT91C_BASE_PIOA->PIO_BSR = GPIO_PCK0;
AT91C_BASE_PIOA->PIO_PDR = GPIO_PCK0;
@ -1419,16 +1437,22 @@ void __attribute__((noreturn)) AppMain(void) {
StartTickCount();
#ifdef WITH_LCD
LCDInit();
// LCDInit();
#endif
#ifdef WITH_SMARTCARD
I2C_init();
// I2C_init();
#endif
#ifdef WITH_FPC
usart_init();
// usart_init();
#endif
// This is made as late as possible to ensure enumeration without timeout
// against device such as http://www.hobbytronics.co.uk/usb-host-board-v2
usb_disable();
usb_enable();
uint8_t rx[sizeof(UsbCommand)];
for(;;) {
@ -1464,6 +1488,10 @@ void __attribute__((noreturn)) AppMain(void) {
RunMod();
#endif
// when here, we are no longer in standalone mode.
// reseting the variables which keeps track of usb re-attached/configured
//SetUSBreconnect(0);
//SetUSBconfigured(0);
}
}
}