Merge remote-tracking branch 'upstream/master' into hf_mf_sim

This commit is contained in:
vratiskol 2019-04-03 01:48:46 +02:00
commit 1c77185fb9
50 changed files with 1182 additions and 863 deletions

View file

@ -5,6 +5,8 @@ define KNOWN_PLATFORMS
+--------------------------------------------------------+
| PM3RDV4 (def) | Proxmark3 rdv4 with AT91SAM7S512 |
+--------------------------------------------------------+
| PM3RDV4FPC | Proxmark3 rdv4+FPC (experimental) |
+--------------------------------------------------------+
| PM3EVO | Proxmark3 EVO with AT91SAM7S512 |
+--------------------------------------------------------+
| PM3EASY | Proxmark3 rdv3 Easy with AT91SAM7S256 |
@ -31,6 +33,10 @@ ifeq ($(PLATFORM),PM3RDV4)
MCU = AT91SAM7S512
PLATFORM_DEFS = -DWITH_SMARTCARD -DWITH_FLASH
PLTNAME = Proxmark3 rdv4
else ifeq ($(PLATFORM),PM3RDV4FPC)
MCU = AT91SAM7S512
PLATFORM_DEFS = -DWITH_SMARTCARD -DWITH_FLASH -DWITH_FPC_HOST
PLTNAME = Proxmark3 rdv4
else ifeq ($(PLATFORM),PM3EVO)
MCU = AT91SAM7S512
PLTNAME = Proxmark3 EVO
@ -50,6 +56,11 @@ else
$(error Invalid or empty PLATFORM: $(PLATFORM). Known platforms: $(KNOWN_PLATFORMS))
endif
# Add flags dependencies
ifneq (,$(findstring WITH_FPC_,$(PLATFORM_DEFS)))
PLATFORM_DEFS += -DWITH_FPC
endif
export PLATFORM
export PLTNAME
export MCU

View file

@ -31,6 +31,17 @@
*/
#include "cmd.h"
#ifdef WITH_FPC_HOST
// "Session" flag, to tell via which interface next msgs should be sent: USB or FPC USART
bool reply_via_fpc = 0;
extern void Dbprintf(const char *fmt, ...);
#define Dbprintf_usb(...) {\
reply_via_fpc = 0;\
Dbprintf(__VA_ARGS__);\
reply_via_fpc = 1;}
#endif
uint8_t cmd_send(uint64_t cmd, uint64_t arg0, uint64_t arg1, uint64_t arg2, void *data, size_t len) {
UsbCommand txcmd;
@ -53,11 +64,16 @@ uint8_t cmd_send(uint64_t cmd, uint64_t arg0, uint64_t arg1, uint64_t arg2, void
uint32_t sendlen = 0;
// Send frame and make sure all bytes are transmitted
sendlen = usb_write((uint8_t *)&txcmd, sizeof(UsbCommand));
#ifdef WITH_FPC
// usart_init();
// usart_writebuffer( (uint8_t*)&txcmd, sizeof(UsbCommand) );
#ifdef WITH_FPC_HOST
if (reply_via_fpc) {
sendlen = usart_writebuffer((uint8_t *)&txcmd, sizeof(UsbCommand));
Dbprintf_usb("Sent %i bytes over usart", len);
} else {
sendlen = usb_write((uint8_t *)&txcmd, sizeof(UsbCommand));
}
#else
sendlen = usb_write((uint8_t *)&txcmd, sizeof(UsbCommand));
#endif
return sendlen;

View file

@ -489,21 +489,21 @@ int DetectStrongAskClock(uint8_t *dest, size_t size, int high, int low, int *clo
if (i == size)
return -1;
// clock, numoftimes, first idx
uint16_t tmpclk[10][3] = {
{8, 0, 0},
{16, 0, 0},
{32, 0, 0},
{40, 0, 0},
{50, 0, 0},
{64, 0, 0},
{100, 0, 0},
{8, 0, 0},
{16, 0, 0},
{32, 0, 0},
{40, 0, 0},
{50, 0, 0},
{64, 0, 0},
{100, 0, 0},
{128, 0, 0},
{256, 0, 0},
{256, 0, 0},
{384, 0, 0},
};
};
// loop through all samples (well, we don't want to go out-of-bounds)
while (i < size - 512) {
// measure from low to low
@ -517,14 +517,14 @@ int DetectStrongAskClock(uint8_t *dest, size_t size, int high, int low, int *clo
minClk = i - startwave;
shortestWaveIdx = startwave;
}
int foo = getClosestClock(minClk);
if (foo > 0 ) {
if (foo > 0) {
for (uint8_t i = 0; i < 10; i++) {
if ( tmpclk[i][0] == foo ) {
if (tmpclk[i][0] == foo) {
tmpclk[i][1]++;
if ( tmpclk[i][2] == 0) {
if (tmpclk[i][2] == 0) {
tmpclk[i][2] = shortestWaveIdx;
}
break;
@ -538,17 +538,17 @@ int DetectStrongAskClock(uint8_t *dest, size_t size, int high, int low, int *clo
for (uint8_t i = 0; i < 10; i++) {
if (g_debugMode == 2) {
prnt("DEBUG, ASK, clocks %u | hits %u | idx %u"
, tmpclk[i][0]
, tmpclk[i][1]
, tmpclk[i][2]
);
, tmpclk[i][0]
, tmpclk[i][1]
, tmpclk[i][2]
);
}
if ( max < tmpclk[i][1] ) {
if (max < tmpclk[i][1]) {
*clock = tmpclk[i][0];
shortestWaveIdx = tmpclk[i][2];
max = tmpclk[i][1];
}
}
}
if (*clock == 0)
return -1;
@ -1517,10 +1517,10 @@ int askdemod_ext(uint8_t *bits, size_t *size, int *clk, int *invert, int maxErr,
if (g_debugMode == 2) prnt("DEBUG (askdemod_ext) just noise detected - aborting");
return -2;
}
int start = DetectASKClock(bits, *size, clk, maxErr);
if (*clk == 0 || start < 0) return -3;
if (*invert != 1) *invert = 0;
// amplify signal data.

View file

@ -10,6 +10,7 @@
//-----------------------------------------------------------------------------
#include "usart.h"
#include "string.h"
#include "apps.h" // for Dbprintf
#define AT91_BAUD_RATE 115200
@ -39,61 +40,63 @@ void usart_close(void) {
}
*/
static uint8_t us_inbuf[sizeof(UsbCommand)];
static uint8_t us_outbuf[sizeof(UsbCommand)];
/// Reads data from an USART peripheral
/// \param data Pointer to the buffer where the received data will be stored.
/// \param len Size of the data buffer (in bytes).
inline int16_t usart_readbuffer(uint8_t *data, size_t len) {
// Check if the first PDC bank is free
if (!(pUS1->US_RCR)) {
pUS1->US_RPR = (uint32_t)data;
pUS1->US_RCR = len;
pUS1->US_PTCR = AT91C_PDC_RXTEN | AT91C_PDC_TXTDIS;
return 2;
}
// Check if the second PDC bank is free
else if (!(pUS1->US_RNCR)) {
pUS1->US_RNPR = (uint32_t)data;
pUS1->US_RNCR = len;
pUS1->US_PTCR = AT91C_PDC_RXTEN | AT91C_PDC_TXTDIS;
return 1;
// transfer from client to device
inline int16_t usart_readbuffer(uint8_t *data) {
uint32_t rcr = pUS1->US_RCR;
if (rcr < sizeof(us_inbuf)) {
pUS1->US_PTCR = AT91C_PDC_RXTDIS;
memcpy(data, us_inbuf, sizeof(us_inbuf) - rcr);
// Reset DMA buffer
pUS1->US_RPR = (uint32_t)us_inbuf;
pUS1->US_RCR = sizeof(us_inbuf);
pUS1->US_PTCR = AT91C_PDC_RXTEN;
return sizeof(us_inbuf) - rcr;
} else {
return 0;
}
}
inline bool usart_dataavailable(void) {
return pUS1->US_RCR < sizeof(us_inbuf);
}
inline int16_t usart_readcommand(uint8_t *data) {
if (pUS1->US_RCR == 0)
return usart_readbuffer(data);
else
return 0;
}
inline bool usart_commandavailable(void) {
return pUS1->US_RCR == 0;
}
// transfer from device to client
inline int16_t usart_writebuffer(uint8_t *data, size_t len) {
// Check if the first PDC bank is free
if (!(pUS1->US_TCR)) {
if (pUS1->US_CSR & AT91C_US_ENDTX) {
memcpy(us_outbuf, data, len);
pUS1->US_TPR = (uint32_t)us_outbuf;
pUS1->US_TCR = sizeof(us_outbuf);
pUS1->US_PTCR = AT91C_PDC_TXTEN | AT91C_PDC_RXTDIS;
return 2;
}
// Check if the second PDC bank is free
else if (!(pUS1->US_TNCR)) {
memcpy(us_outbuf, data, len);
pUS1->US_TNPR = (uint32_t)us_outbuf;
pUS1->US_TNCR = sizeof(us_outbuf);
pUS1->US_PTCR = AT91C_PDC_TXTEN | AT91C_PDC_RXTDIS;
return 1;
pUS1->US_TCR = len;
pUS1->US_PTCR = AT91C_PDC_TXTEN;
while (!(pUS1->US_CSR & AT91C_US_ENDTX)) {};
pUS1->US_PTCR = AT91C_PDC_TXTDIS;
return len;
} else {
return 0;
}
}
void usart_init(void) {
// For a nice detailed sample, interrupt driven but still relevant.
// See https://www.sparkfun.com/datasheets/DevTools/SAM7/at91sam7%20serial%20communications.pdf
// disable & reset receiver / transmitter for configuration
pUS1->US_CR = (AT91C_US_RSTRX | AT91C_US_RSTTX | AT91C_US_RXDIS | AT91C_US_TXDIS);
@ -113,6 +116,7 @@ void usart_init(void) {
// set mode
pUS1->US_MR = AT91C_US_USMODE_NORMAL | // normal mode
AT91C_US_CLKS_CLOCK | // MCK (48MHz)
AT91C_US_OVER | // oversampling
AT91C_US_CHRL_8_BITS | // 8 bits
AT91C_US_PAR_NONE | // parity: none
AT91C_US_NBSTOP_1_BIT | // 1 stop bit
@ -121,16 +125,9 @@ void usart_init(void) {
// all interrupts disabled
pUS1->US_IDR = 0xFFFF;
// iceman, setting 115200 doesn't work. Only speed I got to work is 9600.
// something fishy with the AT91SAM7S512 USART.. Or I missed something
// For a nice detailed sample, interrupt driven but still relevant.
// See https://www.sparkfun.com/datasheets/DevTools/SAM7/at91sam7%20serial%20communications.pdf
// set baudrate to 115200
// 115200 * 16 == 1843200
//
//pUS1->US_BRGR = (48UL*1000*1000) / (9600*16);
pUS1->US_BRGR = 48054841 / (9600 << 4);
pUS1->US_BRGR = 48054841 / (115200 << 3);
// Need speed?
//pUS1->US_BRGR = 48054841 / (460800 << 3);
// Write the Timeguard Register
pUS1->US_TTGR = 0;
@ -138,6 +135,17 @@ void usart_init(void) {
pUS1->US_FIDI = 0;
pUS1->US_IF = 0;
// Disable double buffers for now
pUS1->US_TNPR = (uint32_t)0;
pUS1->US_TNCR = 0;
pUS1->US_RNPR = (uint32_t)0;
pUS1->US_RNCR = 0;
// re-enable receiver / transmitter
pUS1->US_CR = (AT91C_US_RXEN | AT91C_US_TXEN);
// ready to receive
pUS1->US_RPR = (uint32_t)us_inbuf;
pUS1->US_RCR = sizeof(us_inbuf);
pUS1->US_PTCR = AT91C_PDC_RXTEN;
}

View file

@ -7,6 +7,9 @@
void usart_init(void);
void usart_close(void);
int16_t usart_readbuffer(uint8_t *data, size_t len);
int16_t usart_readbuffer(uint8_t *data);
int16_t usart_writebuffer(uint8_t *data, size_t len);
bool usart_dataavailable(void);
int16_t usart_readcommand(uint8_t *data);
bool usart_commandavailable(void);
#endif