From f7e3ed82874bfd8951c90536bd7185d599d6dbf8 Mon Sep 17 00:00:00 2001 From: marcansoft Date: Sat, 20 Feb 2010 22:51:00 +0000 Subject: [PATCH] Clean up data types, some header cleanup, etc. --- armsrc/appmain.c | 41 ++++++----- armsrc/apps.h | 66 ++++++----------- armsrc/fpgaloader.c | 21 +++--- armsrc/hitag2.c | 4 +- armsrc/iso14443.c | 113 +++++++++++++++-------------- armsrc/iso14443a.c | 173 ++++++++++++++++++++++---------------------- armsrc/iso15693.c | 135 +++++++++++++++++----------------- armsrc/legicrf.c | 5 +- armsrc/lfops.c | 35 ++++----- armsrc/printf.c | 41 +++++------ armsrc/printf.h | 12 +++ armsrc/util.c | 40 +++++----- armsrc/util.h | 32 ++++++++ bootrom/bootrom.c | 8 +- bootrom/fromflash.c | 6 +- common/usb.c | 56 +++++++------- include/proxmark3.h | 20 ++--- 17 files changed, 410 insertions(+), 398 deletions(-) create mode 100644 armsrc/printf.h create mode 100644 armsrc/util.h diff --git a/armsrc/appmain.c b/armsrc/appmain.c index 85661704..c2ea9f7d 100644 --- a/armsrc/appmain.c +++ b/armsrc/appmain.c @@ -7,10 +7,13 @@ #include "proxmark3.h" #include "apps.h" +#include "util.h" + #include "legicrf.h" + #ifdef WITH_LCD -#include "fonts.h" -#include "LCD.h" +# include "fonts.h" +# include "LCD.h" #endif #define va_list __builtin_va_list @@ -28,7 +31,7 @@ int kvsprintf(char const *fmt, void *arg, int radix, va_list ap); // is the order in which they go out on the wire. //============================================================================= -BYTE ToSend[512]; +uint8_t ToSend[512]; int ToSendMax; static int ToSendBit; struct common_area common_area __attribute__((section(".commonarea"))); @@ -83,7 +86,7 @@ void DbpString(char *str) } memcpy(c.d.asBytes, str, c.arg[0]); - UsbSendPacket((BYTE *)&c, sizeof(c)); + UsbSendPacket((uint8_t *)&c, sizeof(c)); // TODO fix USB so stupid things like this aren't req'd SpinDelay(50); } @@ -101,7 +104,7 @@ void DbpIntegers(int x1, int x2, int x3) c.arg[1] = x2; c.arg[2] = x3; - UsbSendPacket((BYTE *)&c, sizeof(c)); + UsbSendPacket((uint8_t *)&c, sizeof(c)); // XXX SpinDelay(50); } @@ -126,7 +129,7 @@ void Dbprintf(const char *fmt, ...) { //----------------------------------------------------------------------------- static int ReadAdc(int ch) { - DWORD d; + uint32_t d; AT91C_BASE_ADC->ADC_CR = AT91C_ADC_SWRST; AT91C_BASE_ADC->ADC_MR = @@ -157,7 +160,7 @@ static int AvgAdc(int ch) void MeasureAntennaTuning(void) { - BYTE *dest = (BYTE *)BigBuf; + uint8_t *dest = (uint8_t *)BigBuf; int i, ptr = 0, adcval = 0, peak = 0, peakv = 0, peakf = 0;; int vLf125 = 0, vLf134 = 0, vHf = 0; // in mV @@ -204,7 +207,7 @@ void MeasureAntennaTuning(void) c.arg[0] = (vLf125 << 0) | (vLf134 << 16); c.arg[1] = vHf; c.arg[2] = peakf | (peakv << 16); - UsbSendPacket((BYTE *)&c, sizeof(c)); + UsbSendPacket((uint8_t *)&c, sizeof(c)); } void MeasureAntennaTuningHf(void) @@ -230,9 +233,9 @@ void MeasureAntennaTuningHf(void) void SimulateTagHfListen(void) { - BYTE *dest = (BYTE *)BigBuf; + uint8_t *dest = (uint8_t *)BigBuf; int n = sizeof(BigBuf); - BYTE v = 0; + uint8_t v = 0; int i; int p = 0; @@ -251,7 +254,7 @@ void SimulateTagHfListen(void) AT91C_BASE_SSC->SSC_THR = 0xff; } if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) { - BYTE r = (BYTE)AT91C_BASE_SSC->SSC_RHR; + uint8_t r = (uint8_t)AT91C_BASE_SSC->SSC_RHR; v <<= 1; if(r & 1) { @@ -276,7 +279,7 @@ void SimulateTagHfListen(void) void ReadMem(int addr) { - const BYTE *data = ((BYTE *)addr); + const uint8_t *data = ((uint8_t *)addr); Dbprintf("%x: %02x %02x %02x %02x %02x %02x %02x %02x", addr, data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7]); @@ -567,7 +570,7 @@ void ListenReaderField(int limit) } } -void UsbPacketReceived(BYTE *packet, int len) +void UsbPacketReceived(uint8_t *packet, int len) { UsbCommand *c = (UsbCommand *)packet; UsbCommand ack; @@ -577,7 +580,7 @@ void UsbPacketReceived(BYTE *packet, int len) #ifdef WITH_LF case CMD_ACQUIRE_RAW_ADC_SAMPLES_125K: AcquireRawAdcSamples125k(c->arg[0]); - UsbSendPacket((BYTE*)&ack, sizeof(ack)); + UsbSendPacket((uint8_t*)&ack, sizeof(ack)); break; #endif @@ -718,16 +721,16 @@ void UsbPacketReceived(BYTE *packet, int len) n.cmd = CMD_DOWNLOADED_RAW_BITS_TI_TYPE; } n.arg[0] = c->arg[0]; - memcpy(n.d.asDwords, BigBuf+c->arg[0], 12*sizeof(DWORD)); - UsbSendPacket((BYTE *)&n, sizeof(n)); + memcpy(n.d.asDwords, BigBuf+c->arg[0], 12*sizeof(uint32_t)); + UsbSendPacket((uint8_t *)&n, sizeof(n)); break; } case CMD_DOWNLOADED_SIM_SAMPLES_125K: { - BYTE *b = (BYTE *)BigBuf; + uint8_t *b = (uint8_t *)BigBuf; memcpy(b+c->arg[0], c->d.asBytes, 48); //Dbprintf("copied 48 bytes to %i",b+c->arg[0]); - UsbSendPacket((BYTE*)&ack, sizeof(ack)); + UsbSendPacket((uint8_t*)&ack, sizeof(ack)); break; } @@ -800,7 +803,7 @@ void UsbPacketReceived(BYTE *packet, int len) c.cmd = CMD_DEVICE_INFO; c.arg[0] = DEVICE_INFO_FLAG_OSIMAGE_PRESENT | DEVICE_INFO_FLAG_CURRENT_MODE_OS; if(common_area.flags.bootrom_present) c.arg[0] |= DEVICE_INFO_FLAG_BOOTROM_PRESENT; - UsbSendPacket((BYTE*)&c, sizeof(c)); + UsbSendPacket((uint8_t*)&c, sizeof(c)); } break; default: diff --git a/armsrc/apps.h b/armsrc/apps.h index 938028e7..8e4b9bfa 100644 --- a/armsrc/apps.h +++ b/armsrc/apps.h @@ -7,13 +7,13 @@ #ifndef __APPS_H #define __APPS_H -#include "stdint.h" -#include "stddef.h" +#include +#include typedef unsigned char byte_t; // The large multi-purpose buffer, typically used to hold A/D samples, // maybe processed in some way. -DWORD BigBuf[8000]; +uint32_t BigBuf[8000]; /// appmain.h void ReadMem(int addr); @@ -26,21 +26,21 @@ void Dbprintf(const char *fmt, ...); void ToSendStuffBit(int b); void ToSendReset(void); void ListenReaderField(int limit); -void AcquireRawAdcSamples125k(BOOL at134khz); +void AcquireRawAdcSamples125k(int at134khz); void DoAcquisition125k(void); extern int ToSendMax; -extern BYTE ToSend[]; -extern DWORD BigBuf[]; +extern uint8_t ToSend[]; +extern uint32_t BigBuf[]; /// fpga.h -void FpgaSendCommand(WORD cmd, WORD v); -void FpgaWriteConfWord(BYTE v); +void FpgaSendCommand(uint16_t cmd, uint16_t v); +void FpgaWriteConfWord(uint8_t v); void FpgaDownloadAndGo(void); void FpgaGatherVersion(char *dst, int len); void FpgaSetupSsc(void); void SetupSpi(int mode); -void FpgaSetupSscDma(BYTE *buf, int len); -void SetAdcMuxFor(DWORD whichGpio); +void FpgaSetupSscDma(uint8_t *buf, int len); +void SetAdcMuxFor(uint32_t whichGpio); // Definitions for the FPGA commands. #define FPGA_CMD_SET_CONFREG (1<<12) @@ -72,10 +72,10 @@ void SetAdcMuxFor(DWORD whichGpio); #define FPGA_HF_ISO14443A_READER_MOD (4<<0) /// lfops.h -void AcquireRawAdcSamples125k(BOOL at134khz); -void ModThenAcquireRawAdcSamples125k(int delay_off,int period_0,int period_1,BYTE *command); +void AcquireRawAdcSamples125k(int at134khz); +void ModThenAcquireRawAdcSamples125k(int delay_off,int period_0,int period_1,uint8_t *command); void ReadTItag(void); -void WriteTItag(DWORD idhi, DWORD idlo, WORD crc); +void WriteTItag(uint32_t idhi, uint32_t idlo, uint16_t crc); void AcquireTiType(void); void AcquireRawBitsTI(void); void SimulateTagLowFrequency(int period, int gap, int ledcontrol); @@ -85,47 +85,23 @@ void SimulateTagLowFrequencyBidir(int divisor, int max_bitlen); /// iso14443.h void SimulateIso14443Tag(void); -void AcquireRawAdcSamplesIso14443(DWORD parameter); -void ReadSRI512Iso14443(DWORD parameter); -void ReadSRIX4KIso14443(DWORD parameter); -void ReadSTMemoryIso14443(DWORD parameter,DWORD dwLast); +void AcquireRawAdcSamplesIso14443(uint32_t parameter); +void ReadSRI512Iso14443(uint32_t parameter); +void ReadSRIX4KIso14443(uint32_t parameter); +void ReadSTMemoryIso14443(uint32_t parameter,uint32_t dwLast); void SnoopIso14443(void); /// iso14443a.h void SnoopIso14443a(void); void SimulateIso14443aTag(int tagType, int TagUid); // ## simulate iso14443a tag -void ReaderIso14443a(DWORD parameter); -void ReaderMifare(DWORD parameter); +void ReaderIso14443a(uint32_t parameter); +void ReaderMifare(uint32_t parameter); /// iso15693.h void AcquireRawAdcSamplesIso15693(void); -void ReaderIso15693(DWORD parameter); // Simulate an ISO15693 reader - greg -void SimTagIso15693(DWORD parameter); // simulate an ISO15693 tag - greg +void ReaderIso15693(uint32_t parameter); // Simulate an ISO15693 reader - greg +void SimTagIso15693(uint32_t parameter); // simulate an ISO15693 tag - greg /// util.h -#define LED_RED 1 -#define LED_ORANGE 2 -#define LED_GREEN 4 -#define LED_RED2 8 -#define BUTTON_HOLD 1 -#define BUTTON_NO_CLICK 0 -#define BUTTON_SINGLE_CLICK -1 -#define BUTTON_DOUBLE_CLICK -2 -#define BUTTON_ERROR -99 -int strlen(char *str); -void *memcpy(void *dest, const void *src, int len); -void *memset(void *dest, int c, int len); -int memcmp(const void *av, const void *bv, int len); -char *strncat(char *dest, const char *src, unsigned int n); -void num_to_bytes(uint64_t n, size_t len, byte_t* dest); -uint64_t bytes_to_num(byte_t* src, size_t len); - -void SpinDelay(int ms); -void SpinDelayUs(int us); -void LED(int led, int ms); -void LEDsoff(); -int BUTTON_CLICKED(int ms); -int BUTTON_HELD(int ms); -void FormatVersionInformation(char *dst, int len, const char *prefix, void *version_information); #endif diff --git a/armsrc/fpgaloader.c b/armsrc/fpgaloader.c index b6f9d566..c093c73c 100644 --- a/armsrc/fpgaloader.c +++ b/armsrc/fpgaloader.c @@ -6,6 +6,7 @@ //----------------------------------------------------------------------------- #include "proxmark3.h" #include "apps.h" +#include "util.h" //----------------------------------------------------------------------------- // Set up the Serial Peripheral Interface as master @@ -129,11 +130,11 @@ void FpgaSetupSsc(void) // ourselves, not to another buffer). The stuff to manipulate those buffers // is in apps.h, because it should be inlined, for speed. //----------------------------------------------------------------------------- -void FpgaSetupSscDma(BYTE *buf, int len) +void FpgaSetupSscDma(uint8_t *buf, int len) { - AT91C_BASE_PDC_SSC->PDC_RPR = (DWORD)buf; + AT91C_BASE_PDC_SSC->PDC_RPR = (uint32_t) buf; AT91C_BASE_PDC_SSC->PDC_RCR = len; - AT91C_BASE_PDC_SSC->PDC_RNPR = (DWORD)buf; + AT91C_BASE_PDC_SSC->PDC_RNPR = (uint32_t) buf; AT91C_BASE_PDC_SSC->PDC_RNCR = len; AT91C_BASE_PDC_SSC->PDC_PTCR = AT91C_PDC_RXTEN; } @@ -207,8 +208,8 @@ static void DownloadFPGA(const char *FpgaImage, int FpgaImageLen, int byterevers } if(bytereversal) { - /* This is only supported for DWORD aligned images */ - if( ((int)FpgaImage % sizeof(DWORD)) == 0 ) { + /* This is only supported for uint32_t aligned images */ + if( ((int)FpgaImage % sizeof(uint32_t)) == 0 ) { i=0; while(FpgaImageLen-->0) DownloadFPGA_byte(FpgaImage[(i++)^0x3]); @@ -330,12 +331,12 @@ void FpgaDownloadAndGo(void) /* Fallback for the old flash image format: Check for the magic marker 0xFFFFFFFF * 0xAA995566 at address 0x102000. This is raw bitstream with a size of 336,768 bits - * = 10,524 DWORDs, stored as DWORDS e.g. little-endian in memory, but each DWORD + * = 10,524 uint32_t, stored as uint32_t e.g. little-endian in memory, but each DWORD * is still to be transmitted in MSBit first order. Set the invert flag to indicate * that the DownloadFPGA function should invert every 4 byte sequence when doing * the bytewise download. */ - if( *(DWORD*)0x102000 == 0xFFFFFFFF && *(DWORD*)0x102004 == 0xAA995566 ) + if( *(uint32_t*)0x102000 == 0xFFFFFFFF && *(uint32_t*)0x102004 == 0xAA995566 ) DownloadFPGA((char*)0x102000, 10524*4, 1); } @@ -375,7 +376,7 @@ void FpgaGatherVersion(char *dst, int len) // The bit format is: C3 C2 C1 C0 D11 D10 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0 // where C is the 4 bit command and D is the 12 bit data //----------------------------------------------------------------------------- -void FpgaSendCommand(WORD cmd, WORD v) +void FpgaSendCommand(uint16_t cmd, uint16_t v) { SetupSpi(SPI_FPGA_MODE); while ((AT91C_BASE_SPI->SPI_SR & AT91C_SPI_TXEMPTY) == 0); // wait for the transfer to complete @@ -386,7 +387,7 @@ void FpgaSendCommand(WORD cmd, WORD v) // vs. clone vs. etc.). This is now a special case of FpgaSendCommand() to // avoid changing this function's occurence everywhere in the source code. //----------------------------------------------------------------------------- -void FpgaWriteConfWord(BYTE v) +void FpgaWriteConfWord(uint8_t v) { FpgaSendCommand(FPGA_CMD_SET_CONFREG, v); } @@ -396,7 +397,7 @@ void FpgaWriteConfWord(BYTE v) // closable, but should only close one at a time. Not an FPGA thing, but // the samples from the ADC always flow through the FPGA. //----------------------------------------------------------------------------- -void SetAdcMuxFor(DWORD whichGpio) +void SetAdcMuxFor(uint32_t whichGpio) { AT91C_BASE_PIOA->PIO_OER = GPIO_MUXSEL_HIPKD | diff --git a/armsrc/hitag2.c b/armsrc/hitag2.c index f2fbac84..1278b758 100644 --- a/armsrc/hitag2.c +++ b/armsrc/hitag2.c @@ -8,9 +8,9 @@ */ #include "proxmark3.h" -#include - #include "apps.h" +#include "util.h" + #include "hitag2.h" struct hitag2_cipher_state { diff --git a/armsrc/iso14443.c b/armsrc/iso14443.c index 4ec3f0aa..0e446abe 100644 --- a/armsrc/iso14443.c +++ b/armsrc/iso14443.c @@ -6,10 +6,11 @@ //----------------------------------------------------------------------------- #include "proxmark3.h" #include "apps.h" -#include "iso14443crc.h" +#include "util.h" +#include "iso14443crc.h" -//static void GetSamplesFor14443(BOOL weTx, int n); +//static void GetSamplesFor14443(int weTx, int n); #define DEMOD_TRACE_SIZE 4096 #define READER_TAG_BUFFER_SIZE 2048 @@ -29,7 +30,7 @@ // that here) so that they can be transmitted to the reader. Doesn't transmit // them yet, just leaves them ready to send in ToSend[]. //----------------------------------------------------------------------------- -static void CodeIso14443bAsTag(const BYTE *cmd, int len) +static void CodeIso14443bAsTag(const uint8_t *cmd, int len) { int i; @@ -62,7 +63,7 @@ static void CodeIso14443bAsTag(const BYTE *cmd, int len) for(i = 0; i < len; i++) { int j; - BYTE b = cmd[i]; + uint8_t b = cmd[i]; // Start bit ToSendStuffBit(0); @@ -126,12 +127,12 @@ static struct { STATE_RECEIVING_DATA, STATE_ERROR_WAIT } state; - WORD shiftReg; + uint16_t shiftReg; int bitCnt; int byteCnt; int byteCntMax; int posCnt; - BYTE *output; + uint8_t *output; } Uart; /* Receive & handle a bit coming from the reader. @@ -143,7 +144,7 @@ static struct { * Returns: true if we received a EOF * false if we are still waiting for some more */ -static BOOL Handle14443UartBit(int bit) +static int Handle14443UartBit(int bit) { switch(Uart.state) { case STATE_UNSYNCD: @@ -275,9 +276,9 @@ static BOOL Handle14443UartBit(int bit) // Assume that we're called with the SSC (to the FPGA) and ADC path set // correctly. //----------------------------------------------------------------------------- -static BOOL GetIso14443CommandFromReader(BYTE *received, int *len, int maxLen) +static int GetIso14443CommandFromReader(uint8_t *received, int *len, int maxLen) { - BYTE mask; + uint8_t mask; int i, bit; // Set FPGA mode to "simulated ISO 14443 tag", no modulation (listen @@ -302,7 +303,7 @@ static BOOL GetIso14443CommandFromReader(BYTE *received, int *len, int maxLen) AT91C_BASE_SSC->SSC_THR = 0x00; } if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) { - BYTE b = (BYTE)AT91C_BASE_SSC->SSC_RHR; + uint8_t b = (uint8_t)AT91C_BASE_SSC->SSC_RHR; mask = 0x80; for(i = 0; i < 8; i++, mask >>= 1) { @@ -322,19 +323,19 @@ static BOOL GetIso14443CommandFromReader(BYTE *received, int *len, int maxLen) //----------------------------------------------------------------------------- void SimulateIso14443Tag(void) { - static const BYTE cmd1[] = { 0x05, 0x00, 0x08, 0x39, 0x73 }; - static const BYTE response1[] = { + static const uint8_t cmd1[] = { 0x05, 0x00, 0x08, 0x39, 0x73 }; + static const uint8_t response1[] = { 0x50, 0x82, 0x0d, 0xe1, 0x74, 0x20, 0x38, 0x19, 0x22, 0x00, 0x21, 0x85, 0x5e, 0xd7 }; - BYTE *resp; + uint8_t *resp; int respLen; - BYTE *resp1 = (((BYTE *)BigBuf) + 800); + uint8_t *resp1 = (((uint8_t *)BigBuf) + 800); int resp1Len; - BYTE *receivedCmd = (BYTE *)BigBuf; + uint8_t *receivedCmd = (uint8_t *)BigBuf; int len; int i; @@ -353,7 +354,7 @@ void SimulateIso14443Tag(void) cmdsRecvd = 0; for(;;) { - BYTE b1, b2; + uint8_t b1, b2; if(!GetIso14443CommandFromReader(receivedCmd, &len, 100)) { Dbprintf("button pressed, received %d commands", cmdsRecvd); @@ -400,7 +401,7 @@ void SimulateIso14443Tag(void) i = 0; for(;;) { if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) { - BYTE b = resp[i]; + uint8_t b = resp[i]; AT91C_BASE_SSC->SSC_THR = b; @@ -410,7 +411,7 @@ void SimulateIso14443Tag(void) } } if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) { - volatile BYTE b = (BYTE)AT91C_BASE_SSC->SSC_RHR; + volatile uint8_t b = (uint8_t)AT91C_BASE_SSC->SSC_RHR; (void)b; } } @@ -439,8 +440,8 @@ static struct { int thisBit; int metric; int metricN; - WORD shiftReg; - BYTE *output; + uint16_t shiftReg; + uint8_t *output; int len; int sumI; int sumQ; @@ -457,7 +458,7 @@ static struct { * false if we are still waiting for some more * */ -static BOOL Handle14443SamplesDemod(int ci, int cq) +static int Handle14443SamplesDemod(int ci, int cq) { int v; @@ -581,9 +582,9 @@ static BOOL Handle14443SamplesDemod(int ci, int cq) Demod.bitCount++; if(Demod.bitCount == 10) { - WORD s = Demod.shiftReg; + uint16_t s = Demod.shiftReg; if((s & 0x200) && !(s & 0x001)) { - BYTE b = (s >> 1); + uint8_t b = (s >> 1); Demod.output[Demod.len] = b; Demod.len++; Demod.state = DEMOD_AWAITING_START_BIT; @@ -615,16 +616,16 @@ static BOOL Handle14443SamplesDemod(int ci, int cq) * set to 'FALSE' if we behave like a snooper * quiet: set to 'TRUE' to disable debug output */ -static void GetSamplesFor14443Demod(BOOL weTx, int n, BOOL quiet) +static void GetSamplesFor14443Demod(int weTx, int n, int quiet) { int max = 0; - BOOL gotFrame = FALSE; + int gotFrame = FALSE; //# define DMA_BUFFER_SIZE 8 - SBYTE *dmaBuf; + int8_t *dmaBuf; int lastRxCounter; - SBYTE *upTo; + int8_t *upTo; int ci, cq; @@ -632,20 +633,20 @@ static void GetSamplesFor14443Demod(BOOL weTx, int n, BOOL quiet) // Clear out the state of the "UART" that receives from the tag. memset(BigBuf, 0x44, 400); - Demod.output = (BYTE *)BigBuf; + Demod.output = (uint8_t *)BigBuf; Demod.len = 0; Demod.state = DEMOD_UNSYNCD; // And the UART that receives from the reader - Uart.output = (((BYTE *)BigBuf) + 1024); + Uart.output = (((uint8_t *)BigBuf) + 1024); Uart.byteCntMax = 100; Uart.state = STATE_UNSYNCD; // Setup for the DMA. - dmaBuf = (SBYTE *)(BigBuf + 32); + dmaBuf = (int8_t *)(BigBuf + 32); upTo = dmaBuf; lastRxCounter = DMA_BUFFER_SIZE; - FpgaSetupSscDma((BYTE *)dmaBuf, DMA_BUFFER_SIZE); + FpgaSetupSscDma((uint8_t *)dmaBuf, DMA_BUFFER_SIZE); // Signal field is ON with the appropriate LED: if (weTx) LED_D_ON(); else LED_D_OFF(); @@ -666,7 +667,7 @@ static void GetSamplesFor14443Demod(BOOL weTx, int n, BOOL quiet) upTo += 2; if(upTo - dmaBuf > DMA_BUFFER_SIZE) { upTo -= DMA_BUFFER_SIZE; - AT91C_BASE_PDC_SSC->PDC_RNPR = (DWORD)upTo; + AT91C_BASE_PDC_SSC->PDC_RNPR = (uint32_t) upTo; AT91C_BASE_PDC_SSC->PDC_RNCR = DMA_BUFFER_SIZE; } lastRxCounter -= 2; @@ -697,9 +698,9 @@ static void GetSamplesFor14443Demod(BOOL weTx, int n, BOOL quiet) // samples from the FPGA, which we will later do some signal processing on, // to get the bits. //----------------------------------------------------------------------------- -/*static void GetSamplesFor14443(BOOL weTx, int n) +/*static void GetSamplesFor14443(int weTx, int n) { - BYTE *dest = (BYTE *)BigBuf; + uint8_t *dest = (uint8_t *)BigBuf; int c; FpgaWriteConfWord( @@ -712,10 +713,10 @@ static void GetSamplesFor14443Demod(BOOL weTx, int n, BOOL quiet) AT91C_BASE_SSC->SSC_THR = 0x43; } if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) { - SBYTE b; - b = (SBYTE)AT91C_BASE_SSC->SSC_RHR; + int8_t b; + b = (int8_t)AT91C_BASE_SSC->SSC_RHR; - dest[c++] = (BYTE)b; + dest[c++] = (uint8_t)b; if(c >= n) { break; @@ -750,7 +751,7 @@ static void TransmitFor14443(void) c++; } if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) { - volatile DWORD r = AT91C_BASE_SSC->SSC_RHR; + volatile uint32_t r = AT91C_BASE_SSC->SSC_RHR; (void)r; } WDT_HIT(); @@ -766,7 +767,7 @@ static void TransmitFor14443(void) } } if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) { - volatile DWORD r = AT91C_BASE_SSC->SSC_RHR; + volatile uint32_t r = AT91C_BASE_SSC->SSC_RHR; (void)r; } WDT_HIT(); @@ -778,10 +779,10 @@ static void TransmitFor14443(void) // Code a layer 2 command (string of octets, including CRC) into ToSend[], // so that it is ready to transmit to the tag using TransmitFor14443(). //----------------------------------------------------------------------------- -void CodeIso14443bAsReader(const BYTE *cmd, int len) +void CodeIso14443bAsReader(const uint8_t *cmd, int len) { int i, j; - BYTE b; + uint8_t b; ToSendReset(); @@ -836,9 +837,9 @@ void CodeIso14443bAsReader(const BYTE *cmd, int len) // The command name is misleading, it actually decodes the reponse in HEX // into the output buffer (read the result using hexsamples, not hisamples) //----------------------------------------------------------------------------- -void AcquireRawAdcSamplesIso14443(DWORD parameter) +void AcquireRawAdcSamplesIso14443(uint32_t parameter) { - BYTE cmd1[] = { 0x05, 0x00, 0x08, 0x39, 0x73 }; + uint8_t cmd1[] = { 0x05, 0x00, 0x08, 0x39, 0x73 }; // Make sure that we start from off, since the tags are stateful; // confusing things will happen if we don't reset them between reads. @@ -872,18 +873,18 @@ void AcquireRawAdcSamplesIso14443(DWORD parameter) // // I tried to be systematic and check every answer of the tag, every CRC, etc... //----------------------------------------------------------------------------- -void ReadSRI512Iso14443(DWORD parameter) +void ReadSRI512Iso14443(uint32_t parameter) { ReadSTMemoryIso14443(parameter,0x0F); } -void ReadSRIX4KIso14443(DWORD parameter) +void ReadSRIX4KIso14443(uint32_t parameter) { ReadSTMemoryIso14443(parameter,0x7F); } -void ReadSTMemoryIso14443(DWORD parameter,DWORD dwLast) +void ReadSTMemoryIso14443(uint32_t parameter,uint32_t dwLast) { - BYTE i = 0x00; + uint8_t i = 0x00; // Make sure that we start from off, since the tags are stateful; // confusing things will happen if we don't reset them between reads. @@ -902,7 +903,7 @@ void ReadSTMemoryIso14443(DWORD parameter,DWORD dwLast) SpinDelay(200); // First command: wake up the tag using the INITIATE command - BYTE cmd1[] = { 0x06, 0x00, 0x97, 0x5b}; + uint8_t cmd1[] = { 0x06, 0x00, 0x97, 0x5b}; CodeIso14443bAsReader(cmd1, sizeof(cmd1)); TransmitFor14443(); // LED_A_ON(); @@ -1027,22 +1028,22 @@ void SnoopIso14443(void) // We won't start recording the frames that we acquire until we trigger; // a good trigger condition to get started is probably when we see a // response from the tag. - BOOL triggered = FALSE; + int triggered = FALSE; // The command (reader -> tag) that we're working on receiving. - BYTE *receivedCmd = (BYTE *)(BigBuf) + DEMOD_TRACE_SIZE; + uint8_t *receivedCmd = (uint8_t *)(BigBuf) + DEMOD_TRACE_SIZE; // The response (tag -> reader) that we're working on receiving. - BYTE *receivedResponse = (BYTE *)(BigBuf) + DEMOD_TRACE_SIZE + READER_TAG_BUFFER_SIZE; + uint8_t *receivedResponse = (uint8_t *)(BigBuf) + DEMOD_TRACE_SIZE + READER_TAG_BUFFER_SIZE; // As we receive stuff, we copy it from receivedCmd or receivedResponse // into trace, along with its length and other annotations. - BYTE *trace = (BYTE *)BigBuf; + uint8_t *trace = (uint8_t *)BigBuf; int traceLen = 0; // The DMA buffer, used to stream samples from the FPGA. - SBYTE *dmaBuf = (SBYTE *)(BigBuf) + DEMOD_TRACE_SIZE + READER_TAG_BUFFER_SIZE + TAG_READER_BUFFER_SIZE; + int8_t *dmaBuf = (int8_t *)(BigBuf) + DEMOD_TRACE_SIZE + READER_TAG_BUFFER_SIZE + TAG_READER_BUFFER_SIZE; int lastRxCounter; - SBYTE *upTo; + int8_t *upTo; int ci, cq; int maxBehindBy = 0; @@ -1087,7 +1088,7 @@ void SnoopIso14443(void) FpgaSetupSsc(); upTo = dmaBuf; lastRxCounter = DMA_BUFFER_SIZE; - FpgaSetupSscDma((BYTE *)dmaBuf, DMA_BUFFER_SIZE); + FpgaSetupSscDma((uint8_t *)dmaBuf, DMA_BUFFER_SIZE); // And now we loop, receiving samples. for(;;) { // Blink the LED while Snooping @@ -1118,7 +1119,7 @@ void SnoopIso14443(void) if(upTo - dmaBuf > DMA_BUFFER_SIZE) { upTo -= DMA_BUFFER_SIZE; lastRxCounter += DMA_BUFFER_SIZE; - AT91C_BASE_PDC_SSC->PDC_RNPR = (DWORD) upTo; + AT91C_BASE_PDC_SSC->PDC_RNPR = (uint32_t) upTo; AT91C_BASE_PDC_SSC->PDC_RNCR = DMA_BUFFER_SIZE; } diff --git a/armsrc/iso14443a.c b/armsrc/iso14443a.c index 3a78241c..409bd3d6 100644 --- a/armsrc/iso14443a.c +++ b/armsrc/iso14443a.c @@ -5,12 +5,13 @@ //----------------------------------------------------------------------------- #include "proxmark3.h" #include "apps.h" +#include "util.h" #include "iso14443crc.h" -static BYTE *trace = (BYTE *) BigBuf; +static uint8_t *trace = (uint8_t *) BigBuf; static int traceLen = 0; static int rsamples = 0; -static BOOL tracing = TRUE; +static int tracing = TRUE; typedef enum { SEC_D = 1, @@ -21,7 +22,7 @@ typedef enum { SEC_Z = 6 } SecType; -static const BYTE OddByteParity[256] = { +static const uint8_t OddByteParity[256] = { 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, @@ -51,10 +52,10 @@ static const BYTE OddByteParity[256] = { // Generate the parity value for a byte sequence // //----------------------------------------------------------------------------- -DWORD GetParity(const BYTE * pbtCmd, int iLen) +uint32_t GetParity(const uint8_t * pbtCmd, int iLen) { int i; - DWORD dwPar = 0; + uint32_t dwPar = 0; // Generate the encrypted data for (i = 0; i < iLen; i++) { @@ -64,12 +65,12 @@ DWORD GetParity(const BYTE * pbtCmd, int iLen) return dwPar; } -static void AppendCrc14443a(BYTE* data, int len) +static void AppendCrc14443a(uint8_t* data, int len) { ComputeCrc14443(CRC_14443_A,data,len,data+len,data+len+1); } -BOOL LogTrace(const BYTE * btBytes, int iLen, int iSamples, DWORD dwParity, BOOL bReader) +int LogTrace(const uint8_t * btBytes, int iLen, int iSamples, uint32_t dwParity, int bReader) { // Return when trace is full if (traceLen >= TRACE_LENGTH) return FALSE; @@ -93,7 +94,7 @@ BOOL LogTrace(const BYTE * btBytes, int iLen, int iSamples, DWORD dwParity, BOOL return TRUE; } -BOOL LogTraceInfo(byte_t* data, size_t len) +int LogTraceInfo(byte_t* data, size_t len) { return LogTrace(data,len,0,GetParity(data,len),TRUE); } @@ -111,7 +112,7 @@ static struct { STATE_MILLER_Z, STATE_ERROR_WAIT } state; - WORD shiftReg; + uint16_t shiftReg; int bitCnt; int byteCnt; int byteCntMax; @@ -126,10 +127,10 @@ static struct { DROP_FIRST_HALF, DROP_SECOND_HALF } drop; - BYTE *output; + uint8_t *output; } Uart; -static BOOL MillerDecoding(int bit) +static int MillerDecoding(int bit) { int error = 0; int bitright; @@ -143,7 +144,7 @@ static BOOL MillerDecoding(int bit) Uart.bitBuffer ^= bit; } - BOOL EOC = FALSE; + int EOC = FALSE; if(Uart.state != STATE_UNSYNCD) { Uart.posCnt++; @@ -380,7 +381,7 @@ static struct { int posCount; int syncBit; int parityBits; - WORD shiftReg; + uint16_t shiftReg; int buffer; int buff; int samples; @@ -390,10 +391,10 @@ static struct { SUB_FIRST_HALF, SUB_SECOND_HALF } sub; - BYTE *output; + uint8_t *output; } Demod; -static BOOL ManchesterDecoding(int v) +static int ManchesterDecoding(int v) { int bit; int modulation; @@ -600,24 +601,24 @@ void SnoopIso14443a(void) // We won't start recording the frames that we acquire until we trigger; // a good trigger condition to get started is probably when we see a // response from the tag. - BOOL triggered = TRUE; // FALSE to wait first for card + int triggered = TRUE; // FALSE to wait first for card // The command (reader -> tag) that we're receiving. // The length of a received command will in most cases be no more than 18 bytes. // So 32 should be enough! - BYTE *receivedCmd = (((BYTE *)BigBuf) + RECV_CMD_OFFSET); + uint8_t *receivedCmd = (((uint8_t *)BigBuf) + RECV_CMD_OFFSET); // The response (tag -> reader) that we're receiving. - BYTE *receivedResponse = (((BYTE *)BigBuf) + RECV_RES_OFFSET); + uint8_t *receivedResponse = (((uint8_t *)BigBuf) + RECV_RES_OFFSET); // As we receive stuff, we copy it from receivedCmd or receivedResponse // into trace, along with its length and other annotations. - //BYTE *trace = (BYTE *)BigBuf; + //uint8_t *trace = (uint8_t *)BigBuf; //int traceLen = 0; // The DMA buffer, used to stream samples from the FPGA - SBYTE *dmaBuf = ((SBYTE *)BigBuf) + DMA_BUFFER_OFFSET; + int8_t *dmaBuf = ((int8_t *)BigBuf) + DMA_BUFFER_OFFSET; int lastRxCounter; - SBYTE *upTo; + int8_t *upTo; int smpl; int maxBehindBy = 0; @@ -649,7 +650,7 @@ void SnoopIso14443a(void) FpgaSetupSsc(); upTo = dmaBuf; lastRxCounter = DMA_BUFFER_SIZE; - FpgaSetupSscDma((BYTE *)dmaBuf, DMA_BUFFER_SIZE); + FpgaSetupSscDma((uint8_t *)dmaBuf, DMA_BUFFER_SIZE); LED_A_ON(); @@ -673,7 +674,7 @@ void SnoopIso14443a(void) if(upTo - dmaBuf > DMA_BUFFER_SIZE) { upTo -= DMA_BUFFER_SIZE; lastRxCounter += DMA_BUFFER_SIZE; - AT91C_BASE_PDC_SSC->PDC_RNPR = (DWORD)upTo; + AT91C_BASE_PDC_SSC->PDC_RNPR = (uint32_t) upTo; AT91C_BASE_PDC_SSC->PDC_RNCR = DMA_BUFFER_SIZE; } @@ -798,7 +799,7 @@ void Sequence(SecType seq) //----------------------------------------------------------------------------- // Prepare tag messages //----------------------------------------------------------------------------- -static void CodeIso14443aAsTag(const BYTE *cmd, int len) +static void CodeIso14443aAsTag(const uint8_t *cmd, int len) { int i; int oddparity; @@ -820,7 +821,7 @@ static void CodeIso14443aAsTag(const BYTE *cmd, int len) for(i = 0; i < len; i++) { int j; - BYTE b = cmd[i]; + uint8_t b = cmd[i]; // Data bits oddparity = 0x01; @@ -912,7 +913,7 @@ static void CodeStrangeAnswer() // Stop when button is pressed // Or return TRUE when command is captured //----------------------------------------------------------------------------- -static BOOL GetIso14443aCommandFromReader(BYTE *received, int *len, int maxLen) +static int GetIso14443aCommandFromReader(uint8_t *received, int *len, int maxLen) { // Set FPGA mode to "simulated ISO 14443 tag", no modulation (listen // only, since we are receiving, not transmitting). @@ -934,7 +935,7 @@ static BOOL GetIso14443aCommandFromReader(BYTE *received, int *len, int maxLen) AT91C_BASE_SSC->SSC_THR = 0x00; } if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) { - BYTE b = (BYTE)AT91C_BASE_SSC->SSC_RHR; + uint8_t b = (uint8_t)AT91C_BASE_SSC->SSC_RHR; if(MillerDecoding((b & 0xf0) >> 4)) { *len = Uart.byteCnt; return TRUE; @@ -956,40 +957,40 @@ void SimulateIso14443aTag(int tagType, int TagUid) // This function contains the tag emulation // Prepare protocol messages - // static const BYTE cmd1[] = { 0x26 }; -// static const BYTE response1[] = { 0x02, 0x00 }; // Says: I am Mifare 4k - original line - greg + // static const uint8_t cmd1[] = { 0x26 }; +// static const uint8_t response1[] = { 0x02, 0x00 }; // Says: I am Mifare 4k - original line - greg // - static const BYTE response1[] = { 0x44, 0x03 }; // Says: I am a DESFire Tag, ph33r me -// static const BYTE response1[] = { 0x44, 0x00 }; // Says: I am a ULTRALITE Tag, 0wn me + static const uint8_t response1[] = { 0x44, 0x03 }; // Says: I am a DESFire Tag, ph33r me +// static const uint8_t response1[] = { 0x44, 0x00 }; // Says: I am a ULTRALITE Tag, 0wn me // UID response - // static const BYTE cmd2[] = { 0x93, 0x20 }; - //static const BYTE response2[] = { 0x9a, 0xe5, 0xe4, 0x43, 0xd8 }; // original value - greg + // static const uint8_t cmd2[] = { 0x93, 0x20 }; + //static const uint8_t response2[] = { 0x9a, 0xe5, 0xe4, 0x43, 0xd8 }; // original value - greg // my desfire - static const BYTE response2[] = { 0x88, 0x04, 0x21, 0x3f, 0x4d }; // known uid - note cascade (0x88), 2nd byte (0x04) = NXP/Phillips + static const uint8_t response2[] = { 0x88, 0x04, 0x21, 0x3f, 0x4d }; // known uid - note cascade (0x88), 2nd byte (0x04) = NXP/Phillips // When reader selects us during cascade1 it will send cmd3 -//BYTE response3[] = { 0x04, 0x00, 0x00 }; // SAK Select (cascade1) successful response (ULTRALITE) -BYTE response3[] = { 0x24, 0x00, 0x00 }; // SAK Select (cascade1) successful response (DESFire) +//uint8_t response3[] = { 0x04, 0x00, 0x00 }; // SAK Select (cascade1) successful response (ULTRALITE) +uint8_t response3[] = { 0x24, 0x00, 0x00 }; // SAK Select (cascade1) successful response (DESFire) ComputeCrc14443(CRC_14443_A, response3, 1, &response3[1], &response3[2]); // send cascade2 2nd half of UID -static const BYTE response2a[] = { 0x51, 0x48, 0x1d, 0x80, 0x84 }; // uid - cascade2 - 2nd half (4 bytes) of UID+ BCCheck +static const uint8_t response2a[] = { 0x51, 0x48, 0x1d, 0x80, 0x84 }; // uid - cascade2 - 2nd half (4 bytes) of UID+ BCCheck // NOTE : THE CRC on the above may be wrong as I have obfuscated the actual UID // When reader selects us during cascade2 it will send cmd3a -//BYTE response3a[] = { 0x00, 0x00, 0x00 }; // SAK Select (cascade2) successful response (ULTRALITE) -BYTE response3a[] = { 0x20, 0x00, 0x00 }; // SAK Select (cascade2) successful response (DESFire) +//uint8_t response3a[] = { 0x00, 0x00, 0x00 }; // SAK Select (cascade2) successful response (ULTRALITE) +uint8_t response3a[] = { 0x20, 0x00, 0x00 }; // SAK Select (cascade2) successful response (DESFire) ComputeCrc14443(CRC_14443_A, response3a, 1, &response3a[1], &response3a[2]); - static const BYTE response5[] = { 0x00, 0x00, 0x00, 0x00 }; // Very random tag nonce + static const uint8_t response5[] = { 0x00, 0x00, 0x00, 0x00 }; // Very random tag nonce - BYTE *resp; + uint8_t *resp; int respLen; // Longest possible response will be 16 bytes + 2 CRC = 18 bytes @@ -1007,40 +1008,40 @@ ComputeCrc14443(CRC_14443_A, response3a, 1, &response3a[1], &response3a[2]); // Respond with card type - BYTE *resp1 = (((BYTE *)BigBuf) + 800); + uint8_t *resp1 = (((uint8_t *)BigBuf) + 800); int resp1Len; // Anticollision cascade1 - respond with uid - BYTE *resp2 = (((BYTE *)BigBuf) + 970); + uint8_t *resp2 = (((uint8_t *)BigBuf) + 970); int resp2Len; // Anticollision cascade2 - respond with 2nd half of uid if asked // we're only going to be asked if we set the 1st byte of the UID (during cascade1) to 0x88 - BYTE *resp2a = (((BYTE *)BigBuf) + 1140); + uint8_t *resp2a = (((uint8_t *)BigBuf) + 1140); int resp2aLen; // Acknowledge select - cascade 1 - BYTE *resp3 = (((BYTE *)BigBuf) + 1310); + uint8_t *resp3 = (((uint8_t *)BigBuf) + 1310); int resp3Len; // Acknowledge select - cascade 2 - BYTE *resp3a = (((BYTE *)BigBuf) + 1480); + uint8_t *resp3a = (((uint8_t *)BigBuf) + 1480); int resp3aLen; // Response to a read request - not implemented atm - BYTE *resp4 = (((BYTE *)BigBuf) + 1550); + uint8_t *resp4 = (((uint8_t *)BigBuf) + 1550); int resp4Len; // Authenticate response - nonce - BYTE *resp5 = (((BYTE *)BigBuf) + 1720); + uint8_t *resp5 = (((uint8_t *)BigBuf) + 1720); int resp5Len; - BYTE *receivedCmd = (BYTE *)BigBuf; + uint8_t *receivedCmd = (uint8_t *)BigBuf; int len; int i; int u; - BYTE b; + uint8_t b; // To control where we are in the protocol int order = 0; @@ -1052,7 +1053,7 @@ ComputeCrc14443(CRC_14443_A, response3a, 1, &response3a[1], &response3a[2]); int cmdsRecvd = 0; - BOOL fdt_indicator; + int fdt_indicator; memset(receivedCmd, 0x44, 400); @@ -1209,7 +1210,7 @@ ComputeCrc14443(CRC_14443_A, response3a, 1, &response3a[1], &response3a[2]); fdt_indicator = FALSE; for(;;) { if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) { - volatile BYTE b = (BYTE)AT91C_BASE_SSC->SSC_RHR; + volatile uint8_t b = (uint8_t)AT91C_BASE_SSC->SSC_RHR; (void)b; } if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_TXRDY)) { @@ -1240,7 +1241,7 @@ ComputeCrc14443(CRC_14443_A, response3a, 1, &response3a[1], &response3a[2]); //----------------------------------------------------------------------------- // Transmit the command (to the tag) that was placed in ToSend[]. //----------------------------------------------------------------------------- -static void TransmitFor14443a(const BYTE *cmd, int len, int *samples, int *wait) +static void TransmitFor14443a(const uint8_t *cmd, int len, int *samples, int *wait) { int c; @@ -1256,7 +1257,7 @@ static void TransmitFor14443a(const BYTE *cmd, int len, int *samples, int *wait) c++; } if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) { - volatile DWORD r = AT91C_BASE_SSC->SSC_RHR; + volatile uint32_t r = AT91C_BASE_SSC->SSC_RHR; (void)r; } WDT_HIT(); @@ -1272,7 +1273,7 @@ static void TransmitFor14443a(const BYTE *cmd, int len, int *samples, int *wait) } } if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) { - volatile DWORD r = AT91C_BASE_SSC->SSC_RHR; + volatile uint32_t r = AT91C_BASE_SSC->SSC_RHR; (void)r; } WDT_HIT(); @@ -1284,12 +1285,12 @@ static void TransmitFor14443a(const BYTE *cmd, int len, int *samples, int *wait) // To generate an arbitrary stream from reader // //----------------------------------------------------------------------------- -void ArbitraryFromReader(const BYTE *cmd, int parity, int len) +void ArbitraryFromReader(const uint8_t *cmd, int parity, int len) { int i; int j; int last; - BYTE b; + uint8_t b; ToSendReset(); @@ -1364,11 +1365,11 @@ void ArbitraryFromReader(const BYTE *cmd, int parity, int len) // Code a 7-bit command without parity bit // This is especially for 0x26 and 0x52 (REQA and WUPA) //----------------------------------------------------------------------------- -void ShortFrameFromReader(const BYTE bt) +void ShortFrameFromReader(const uint8_t bt) { int j; int last; - BYTE b; + uint8_t b; ToSendReset(); @@ -1422,11 +1423,11 @@ void ShortFrameFromReader(const BYTE bt) // Prepare reader command to send to FPGA // //----------------------------------------------------------------------------- -void CodeIso14443aAsReaderPar(const BYTE * cmd, int len, DWORD dwParity) +void CodeIso14443aAsReaderPar(const uint8_t * cmd, int len, uint32_t dwParity) { int i, j; int last; - BYTE b; + uint8_t b; ToSendReset(); @@ -1500,7 +1501,7 @@ void CodeIso14443aAsReaderPar(const BYTE * cmd, int len, DWORD dwParity) // If a response is captured return TRUE // If it takes to long return FALSE //----------------------------------------------------------------------------- -static BOOL GetIso14443aAnswerFromTag(BYTE *receivedResponse, int maxLen, int *samples, int *elapsed) //BYTE *buffer +static int GetIso14443aAnswerFromTag(uint8_t *receivedResponse, int maxLen, int *samples, int *elapsed) //uint8_t *buffer { // buffer needs to be 512 bytes int c; @@ -1516,7 +1517,7 @@ static BOOL GetIso14443aAnswerFromTag(BYTE *receivedResponse, int maxLen, int *s Demod.len = 0; Demod.state = DEMOD_UNSYNCD; - BYTE b; + uint8_t b; if (elapsed) *elapsed = 0; c = 0; @@ -1529,7 +1530,7 @@ static BOOL GetIso14443aAnswerFromTag(BYTE *receivedResponse, int maxLen, int *s } if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) { if(c < 512) { c++; } else { return FALSE; } - b = (BYTE)AT91C_BASE_SSC->SSC_RHR; + b = (uint8_t)AT91C_BASE_SSC->SSC_RHR; if(ManchesterDecoding((b & 0xf0) >> 4)) { *samples = ((c - 1) << 3) + 4; return TRUE; @@ -1542,7 +1543,7 @@ static BOOL GetIso14443aAnswerFromTag(BYTE *receivedResponse, int maxLen, int *s } } -void ReaderTransmitShort(const BYTE* bt) +void ReaderTransmitShort(const uint8_t* bt) { int wait = 0; int samples = 0; @@ -1556,13 +1557,13 @@ void ReaderTransmitShort(const BYTE* bt) if (tracing) LogTrace(bt,1,0,GetParity(bt,1),TRUE); } -void ReaderTransmitPar(BYTE* frame, int len, DWORD par) +void ReaderTransmitPar(uint8_t* frame, int len, uint32_t par) { int wait = 0; int samples = 0; // This is tied to other size changes - // BYTE* frame_addr = ((BYTE*)BigBuf) + 2024; + // uint8_t* frame_addr = ((uint8_t*)BigBuf) + 2024; CodeIso14443aAsReaderPar(frame,len,par); // Select the card @@ -1573,13 +1574,13 @@ void ReaderTransmitPar(BYTE* frame, int len, DWORD par) } -void ReaderTransmit(BYTE* frame, int len) +void ReaderTransmit(uint8_t* frame, int len) { // Generate parity and redirect ReaderTransmitPar(frame,len,GetParity(frame,len)); } -BOOL ReaderReceive(BYTE* receivedAnswer) +int ReaderReceive(uint8_t* receivedAnswer) { int samples = 0; if (!GetIso14443aAnswerFromTag(receivedAnswer,100,&samples,0)) return FALSE; @@ -1591,20 +1592,20 @@ BOOL ReaderReceive(BYTE* receivedAnswer) // Read an ISO 14443a tag. Send out commands and store answers. // //----------------------------------------------------------------------------- -void ReaderIso14443a(DWORD parameter) +void ReaderIso14443a(uint32_t parameter) { // Anticollision - BYTE wupa[] = { 0x52 }; - BYTE sel_all[] = { 0x93,0x20 }; - BYTE sel_uid[] = { 0x93,0x70,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }; - BYTE sel_all_c2[] = { 0x95,0x20 }; - BYTE sel_uid_c2[] = { 0x95,0x70,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }; + uint8_t wupa[] = { 0x52 }; + uint8_t sel_all[] = { 0x93,0x20 }; + uint8_t sel_uid[] = { 0x93,0x70,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }; + uint8_t sel_all_c2[] = { 0x95,0x20 }; + uint8_t sel_uid_c2[] = { 0x95,0x70,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }; // Mifare AUTH - BYTE mf_auth[] = { 0x60,0x00,0xf5,0x7b }; -// BYTE mf_nr_ar[] = { 0x00,0x00,0x00,0x00 }; + uint8_t mf_auth[] = { 0x60,0x00,0xf5,0x7b }; +// uint8_t mf_nr_ar[] = { 0x00,0x00,0x00,0x00 }; - BYTE* receivedAnswer = (((BYTE *)BigBuf) + 3560); // was 3560 - tied to other size changes + uint8_t* receivedAnswer = (((uint8_t *)BigBuf) + 3560); // was 3560 - tied to other size changes traceLen = 0; // Setup SSC @@ -1701,19 +1702,19 @@ void ReaderIso14443a(DWORD parameter) // Read an ISO 14443a tag. Send out commands and store answers. // //----------------------------------------------------------------------------- -void ReaderMifare(DWORD parameter) +void ReaderMifare(uint32_t parameter) { // Anticollision - BYTE wupa[] = { 0x52 }; - BYTE sel_all[] = { 0x93,0x20 }; - BYTE sel_uid[] = { 0x93,0x70,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }; + uint8_t wupa[] = { 0x52 }; + uint8_t sel_all[] = { 0x93,0x20 }; + uint8_t sel_uid[] = { 0x93,0x70,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }; // Mifare AUTH - BYTE mf_auth[] = { 0x60,0x00,0xf5,0x7b }; - BYTE mf_nr_ar[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }; + uint8_t mf_auth[] = { 0x60,0x00,0xf5,0x7b }; + uint8_t mf_nr_ar[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }; - BYTE* receivedAnswer = (((BYTE *)BigBuf) + 3560); // was 3560 - tied to other size changes + uint8_t* receivedAnswer = (((uint8_t *)BigBuf) + 3560); // was 3560 - tied to other size changes traceLen = 0; tracing = false; @@ -1758,7 +1759,7 @@ void ReaderMifare(DWORD parameter) byte_t par = 0; byte_t par_mask = 0xff; byte_t par_low = 0; - BOOL led_on = TRUE; + int led_on = TRUE; tracing = FALSE; byte_t nt[4]; diff --git a/armsrc/iso15693.c b/armsrc/iso15693.c index efb9386f..30a631e8 100644 --- a/armsrc/iso15693.c +++ b/armsrc/iso15693.c @@ -9,6 +9,7 @@ //----------------------------------------------------------------------------- #include "proxmark3.h" +#include "util.h" #include "apps.h" // FROM winsrc\prox.h ////////////////////////////////// @@ -65,7 +66,7 @@ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }; -static void CodeIso15693AsReader(BYTE *cmd, int n) +static void CodeIso15693AsReader(uint8_t *cmd, int n) { int i, j; @@ -145,14 +146,14 @@ static void CodeIso15693AsReader(BYTE *cmd, int n) //----------------------------------------------------------------------------- // The CRC used by ISO 15693. //----------------------------------------------------------------------------- -static WORD Crc(BYTE *v, int n) +static uint16_t Crc(uint8_t *v, int n) { - DWORD reg; + uint32_t reg; int i, j; reg = 0xffff; for(i = 0; i < n; i++) { - reg = reg ^ ((DWORD)v[i]); + reg = reg ^ ((uint32_t)v[i]); for (j = 0; j < 8; j++) { if (reg & 0x0001) { reg = (reg >> 1) ^ 0x8408; @@ -216,9 +217,9 @@ void itoa(int n, char s[]) //----------------------------------------------------------------------------- static void BuildIdentifyRequest(void) { - BYTE cmd[5]; + uint8_t cmd[5]; - WORD crc; + uint16_t crc; // one sub-carrier, inventory, 1 slot, fast rate // AFI is at bit 5 (1<<4) when doing an INVENTORY cmd[0] = (1 << 2) | (1 << 5) | (1 << 1); @@ -234,11 +235,11 @@ static void BuildIdentifyRequest(void) CodeIso15693AsReader(cmd, sizeof(cmd)); } -static void __attribute__((unused)) BuildSysInfoRequest(BYTE *uid) +static void __attribute__((unused)) BuildSysInfoRequest(uint8_t *uid) { - BYTE cmd[12]; + uint8_t cmd[12]; - WORD crc; + uint16_t crc; // If we set the Option_Flag in this request, the VICC will respond with the secuirty status of the block // followed by teh block data // one sub-carrier, inventory, 1 slot, fast rate @@ -263,13 +264,13 @@ static void __attribute__((unused)) BuildSysInfoRequest(BYTE *uid) CodeIso15693AsReader(cmd, sizeof(cmd)); } -static void BuildSelectRequest( BYTE uid[]) +static void BuildSelectRequest( uint8_t uid[]) { // uid[6]=0x31; // this is getting ignored - the uid array is not happening... - BYTE cmd[12]; + uint8_t cmd[12]; - WORD crc; + uint16_t crc; // one sub-carrier, inventory, 1 slot, fast rate //cmd[0] = (1 << 2) | (1 << 5) | (1 << 1); // INVENTROY FLAGS cmd[0] = (1 << 4) | (1 << 5) | (1 << 1); // Select and addressed FLAGS @@ -302,11 +303,11 @@ static void BuildSelectRequest( BYTE uid[]) CodeIso15693AsReader(cmd, sizeof(cmd)); } -static void __attribute__((unused)) BuildReadBlockRequest(BYTE *uid, BYTE blockNumber ) +static void __attribute__((unused)) BuildReadBlockRequest(uint8_t *uid, uint8_t blockNumber ) { - BYTE cmd[13]; + uint8_t cmd[13]; - WORD crc; + uint16_t crc; // If we set the Option_Flag in this request, the VICC will respond with the secuirty status of the block // followed by teh block data // one sub-carrier, inventory, 1 slot, fast rate @@ -333,11 +334,11 @@ static void __attribute__((unused)) BuildReadBlockRequest(BYTE *uid, BYTE blockN CodeIso15693AsReader(cmd, sizeof(cmd)); } -static void __attribute__((unused)) BuildReadMultiBlockRequest(BYTE *uid) +static void __attribute__((unused)) BuildReadMultiBlockRequest(uint8_t *uid) { - BYTE cmd[14]; + uint8_t cmd[14]; - WORD crc; + uint16_t crc; // If we set the Option_Flag in this request, the VICC will respond with the secuirty status of the block // followed by teh block data // one sub-carrier, inventory, 1 slot, fast rate @@ -366,11 +367,11 @@ static void __attribute__((unused)) BuildReadMultiBlockRequest(BYTE *uid) CodeIso15693AsReader(cmd, sizeof(cmd)); } -static void __attribute__((unused)) BuildArbitraryRequest(BYTE *uid,BYTE CmdCode) +static void __attribute__((unused)) BuildArbitraryRequest(uint8_t *uid,uint8_t CmdCode) { - BYTE cmd[14]; + uint8_t cmd[14]; - WORD crc; + uint16_t crc; // If we set the Option_Flag in this request, the VICC will respond with the secuirty status of the block // followed by teh block data // one sub-carrier, inventory, 1 slot, fast rate @@ -400,11 +401,11 @@ static void __attribute__((unused)) BuildArbitraryRequest(BYTE *uid,BYTE CmdCode CodeIso15693AsReader(cmd, sizeof(cmd)); } -static void __attribute__((unused)) BuildArbitraryCustomRequest(BYTE uid[], BYTE CmdCode) +static void __attribute__((unused)) BuildArbitraryCustomRequest(uint8_t uid[], uint8_t CmdCode) { - BYTE cmd[14]; + uint8_t cmd[14]; - WORD crc; + uint16_t crc; // If we set the Option_Flag in this request, the VICC will respond with the secuirty status of the block // followed by teh block data // one sub-carrier, inventory, 1 slot, fast rate @@ -440,9 +441,9 @@ static void __attribute__((unused)) BuildArbitraryCustomRequest(BYTE uid[], BYTE static void BuildInventoryResponse(void) { - BYTE cmd[12]; + uint8_t cmd[12]; - WORD crc; + uint16_t crc; // one sub-carrier, inventory, 1 slot, fast rate // AFI is at bit 5 (1<<4) when doing an INVENTORY cmd[0] = 0; //(1 << 2) | (1 << 5) | (1 << 1); @@ -467,7 +468,7 @@ static void __attribute__((unused)) BuildArbitraryCustomRequest(BYTE uid[], BYTE //----------------------------------------------------------------------------- // Transmit the command (to the tag) that was placed in ToSend[]. //----------------------------------------------------------------------------- -static void TransmitTo15693Tag(const BYTE *cmd, int len, int *samples, int *wait) +static void TransmitTo15693Tag(const uint8_t *cmd, int len, int *samples, int *wait) { int c; @@ -481,7 +482,7 @@ static void TransmitTo15693Tag(const BYTE *cmd, int len, int *samples, int *wait // c++; // } // if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) { -// volatile DWORD r = AT91C_BASE_SSC->SSC_RHR; +// volatile uint32_t r = AT91C_BASE_SSC->SSC_RHR; // (void)r; // } // WDT_HIT(); @@ -497,7 +498,7 @@ static void TransmitTo15693Tag(const BYTE *cmd, int len, int *samples, int *wait } } if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) { - volatile DWORD r = AT91C_BASE_SSC->SSC_RHR; + volatile uint32_t r = AT91C_BASE_SSC->SSC_RHR; (void)r; } WDT_HIT(); @@ -508,7 +509,7 @@ static void TransmitTo15693Tag(const BYTE *cmd, int len, int *samples, int *wait //----------------------------------------------------------------------------- // Transmit the command (to the reader) that was placed in ToSend[]. //----------------------------------------------------------------------------- -static void TransmitTo15693Reader(const BYTE *cmd, int len, int *samples, int *wait) +static void TransmitTo15693Reader(const uint8_t *cmd, int len, int *samples, int *wait) { int c; @@ -526,7 +527,7 @@ static void TransmitTo15693Reader(const BYTE *cmd, int len, int *samples, int *w } } if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) { - volatile DWORD r = AT91C_BASE_SSC->SSC_RHR; + volatile uint32_t r = AT91C_BASE_SSC->SSC_RHR; (void)r; } WDT_HIT(); @@ -534,13 +535,13 @@ static void TransmitTo15693Reader(const BYTE *cmd, int len, int *samples, int *w *samples = (c + *wait) << 3; } -static int GetIso15693AnswerFromTag(BYTE *receivedResponse, int maxLen, int *samples, int *elapsed) +static int GetIso15693AnswerFromTag(uint8_t *receivedResponse, int maxLen, int *samples, int *elapsed) { int c = 0; - BYTE *dest = (BYTE *)BigBuf; + uint8_t *dest = (uint8_t *)BigBuf; int getNext = 0; - SBYTE prev = 0; + int8_t prev = 0; // NOW READ RESPONSE FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR); @@ -552,15 +553,15 @@ static int GetIso15693AnswerFromTag(BYTE *receivedResponse, int maxLen, int *sam AT91C_BASE_SSC->SSC_THR = 0x43; } if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) { - SBYTE b; - b = (SBYTE)AT91C_BASE_SSC->SSC_RHR; + int8_t b; + b = (int8_t)AT91C_BASE_SSC->SSC_RHR; // The samples are correlations against I and Q versions of the // tone that the tag AM-modulates, so every other sample is I, // every other is Q. We just want power, so abs(I) + abs(Q) is // close to what we want. if(getNext) { - SBYTE r; + int8_t r; if(b < 0) { r = -b; @@ -573,7 +574,7 @@ static int GetIso15693AnswerFromTag(BYTE *receivedResponse, int maxLen, int *sam r += prev; } - dest[c++] = (BYTE)r; + dest[c++] = (uint8_t)r; if(c >= 2000) { break; @@ -618,9 +619,9 @@ static int GetIso15693AnswerFromTag(BYTE *receivedResponse, int maxLen, int *sam i = maxPos + arraylen(FrameSOF)/skip; - BYTE outBuf[20]; + uint8_t outBuf[20]; memset(outBuf, 0, sizeof(outBuf)); - BYTE mask = 0x01; + uint8_t mask = 0x01; for(;;) { int corr0 = 0, corr1 = 0, corrEOF = 0; for(j = 0; j < arraylen(Logic0); j += skip) { @@ -659,7 +660,7 @@ static int GetIso15693AnswerFromTag(BYTE *receivedResponse, int maxLen, int *sam DbpString("error, uneven octet! (discard extra bits!)"); /// DbpString(" mask=%02x", mask); } -// BYTE str1 [8]; +// uint8_t str1 [8]; // itoa(k,str1); // strcat(str1," octets read"); @@ -681,13 +682,13 @@ static int GetIso15693AnswerFromTag(BYTE *receivedResponse, int maxLen, int *sam } // Now the GetISO15693 message from sniffing command -static int GetIso15693AnswerFromSniff(BYTE *receivedResponse, int maxLen, int *samples, int *elapsed) +static int GetIso15693AnswerFromSniff(uint8_t *receivedResponse, int maxLen, int *samples, int *elapsed) { int c = 0; - BYTE *dest = (BYTE *)BigBuf; + uint8_t *dest = (uint8_t *)BigBuf; int getNext = 0; - SBYTE prev = 0; + int8_t prev = 0; // NOW READ RESPONSE FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR); @@ -699,15 +700,15 @@ static int GetIso15693AnswerFromSniff(BYTE *receivedResponse, int maxLen, int *s AT91C_BASE_SSC->SSC_THR = 0x43; } if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) { - SBYTE b; - b = (SBYTE)AT91C_BASE_SSC->SSC_RHR; + int8_t b; + b = (int8_t)AT91C_BASE_SSC->SSC_RHR; // The samples are correlations against I and Q versions of the // tone that the tag AM-modulates, so every other sample is I, // every other is Q. We just want power, so abs(I) + abs(Q) is // close to what we want. if(getNext) { - SBYTE r; + int8_t r; if(b < 0) { r = -b; @@ -720,7 +721,7 @@ static int GetIso15693AnswerFromSniff(BYTE *receivedResponse, int maxLen, int *s r += prev; } - dest[c++] = (BYTE)r; + dest[c++] = (uint8_t)r; if(c >= 20000) { break; @@ -765,9 +766,9 @@ static int GetIso15693AnswerFromSniff(BYTE *receivedResponse, int maxLen, int *s i = maxPos + arraylen(FrameSOF)/skip; - BYTE outBuf[20]; + uint8_t outBuf[20]; memset(outBuf, 0, sizeof(outBuf)); - BYTE mask = 0x01; + uint8_t mask = 0x01; for(;;) { int corr0 = 0, corr1 = 0, corrEOF = 0; for(j = 0; j < arraylen(Logic0); j += skip) { @@ -806,7 +807,7 @@ static int GetIso15693AnswerFromSniff(BYTE *receivedResponse, int maxLen, int *s DbpString("error, uneven octet! (discard extra bits!)"); /// DbpString(" mask=%02x", mask); } -// BYTE str1 [8]; +// uint8_t str1 [8]; // itoa(k,str1); // strcat(str1," octets read"); @@ -834,10 +835,10 @@ static int GetIso15693AnswerFromSniff(BYTE *receivedResponse, int maxLen, int *s void AcquireRawAdcSamplesIso15693(void) { int c = 0; - BYTE *dest = (BYTE *)BigBuf; + uint8_t *dest = (uint8_t *)BigBuf; int getNext = 0; - SBYTE prev = 0; + int8_t prev = 0; BuildIdentifyRequest(); @@ -861,7 +862,7 @@ void AcquireRawAdcSamplesIso15693(void) } } if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) { - volatile DWORD r = AT91C_BASE_SSC->SSC_RHR; + volatile uint32_t r = AT91C_BASE_SSC->SSC_RHR; (void)r; } WDT_HIT(); @@ -876,15 +877,15 @@ void AcquireRawAdcSamplesIso15693(void) AT91C_BASE_SSC->SSC_THR = 0x43; } if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) { - SBYTE b; - b = (SBYTE)AT91C_BASE_SSC->SSC_RHR; + int8_t b; + b = (int8_t)AT91C_BASE_SSC->SSC_RHR; // The samples are correlations against I and Q versions of the // tone that the tag AM-modulates, so every other sample is I, // every other is Q. We just want power, so abs(I) + abs(Q) is // close to what we want. if(getNext) { - SBYTE r; + int8_t r; if(b < 0) { r = -b; @@ -897,7 +898,7 @@ void AcquireRawAdcSamplesIso15693(void) r += prev; } - dest[c++] = (BYTE)r; + dest[c++] = (uint8_t)r; if(c >= 2000) { break; @@ -915,7 +916,7 @@ void AcquireRawAdcSamplesIso15693(void) // Simulate an ISO15693 reader, perform anti-collision and then attempt to read a sector // all demodulation performed in arm rather than host. - greg //----------------------------------------------------------------------------- -void ReaderIso15693(DWORD parameter) +void ReaderIso15693(uint32_t parameter) { LED_A_ON(); LED_B_ON(); @@ -924,11 +925,11 @@ void ReaderIso15693(DWORD parameter) //DbpString(parameter); - //BYTE *answer0 = (((BYTE *)BigBuf) + 3560); // allow 100 bytes per reponse (way too much) - BYTE *answer1 = (((BYTE *)BigBuf) + 3660); // - BYTE *answer2 = (((BYTE *)BigBuf) + 3760); - BYTE *answer3 = (((BYTE *)BigBuf) + 3860); - //BYTE *TagUID= (((BYTE *)BigBuf) + 3960); // where we hold the uid for hi15reader + //uint8_t *answer0 = (((uint8_t *)BigBuf) + 3560); // allow 100 bytes per reponse (way too much) + uint8_t *answer1 = (((uint8_t *)BigBuf) + 3660); // + uint8_t *answer2 = (((uint8_t *)BigBuf) + 3760); + uint8_t *answer3 = (((uint8_t *)BigBuf) + 3860); + //uint8_t *TagUID= (((uint8_t *)BigBuf) + 3960); // where we hold the uid for hi15reader // int answerLen0 = 0; int answerLen1 = 0; int answerLen2 = 0; @@ -963,7 +964,7 @@ void ReaderIso15693(DWORD parameter) // FIRST WE RUN AN INVENTORY TO GET THE TAG UID // THIS MEANS WE CAN PRE-BUILD REQUESTS TO SAVE CPU TIME - BYTE TagUID[7]; // where we hold the uid for hi15reader + uint8_t TagUID[7]; // where we hold the uid for hi15reader // BuildIdentifyRequest(); // //TransmitTo15693Tag(ToSend,ToSendMax+3,&tsamples, &wait); @@ -1053,14 +1054,14 @@ void ReaderIso15693(DWORD parameter) // Simulate an ISO15693 TAG, perform anti-collision and then print any reader commands // all demodulation performed in arm rather than host. - greg //----------------------------------------------------------------------------- -void SimTagIso15693(DWORD parameter) +void SimTagIso15693(uint32_t parameter) { LED_A_ON(); LED_B_ON(); LED_C_OFF(); LED_D_OFF(); - BYTE *answer1 = (((BYTE *)BigBuf) + 3660); // + uint8_t *answer1 = (((uint8_t *)BigBuf) + 3660); // int answerLen1 = 0; // Blank arrays diff --git a/armsrc/legicrf.c b/armsrc/legicrf.c index 86a4d5de..eb38939d 100644 --- a/armsrc/legicrf.c +++ b/armsrc/legicrf.c @@ -5,11 +5,10 @@ */ #include "proxmark3.h" - #include "apps.h" -#include "legicrf.h" -#include +#include "util.h" +#include "legicrf.h" #include "legic_prng.h" #include "crc.h" diff --git a/armsrc/lfops.c b/armsrc/lfops.c index e4ebacb0..93c0cbde 100644 --- a/armsrc/lfops.c +++ b/armsrc/lfops.c @@ -6,10 +6,11 @@ //----------------------------------------------------------------------------- #include "proxmark3.h" #include "apps.h" +#include "util.h" #include "hitag2.h" #include "crc16.h" -void AcquireRawAdcSamples125k(BOOL at134khz) +void AcquireRawAdcSamples125k(int at134khz) { if (at134khz) FpgaSendCommand(FPGA_CMD_SET_DIVISOR, 88); //134.8Khz @@ -34,7 +35,7 @@ void AcquireRawAdcSamples125k(BOOL at134khz) // split into two routines so we can avoid timing issues after sending commands // void DoAcquisition125k(void) { - BYTE *dest = (BYTE *)BigBuf; + uint8_t *dest = (uint8_t *)BigBuf; int n = sizeof(BigBuf); int i; @@ -46,7 +47,7 @@ void DoAcquisition125k(void) LED_D_ON(); } if (AT91C_BASE_SSC->SSC_SR & AT91C_SSC_RXRDY) { - dest[i] = (BYTE)AT91C_BASE_SSC->SSC_RHR; + dest[i] = (uint8_t)AT91C_BASE_SSC->SSC_RHR; i++; LED_D_OFF(); if (i >= n) break; @@ -56,9 +57,9 @@ void DoAcquisition125k(void) dest[0], dest[1], dest[2], dest[3], dest[4], dest[5], dest[6], dest[7]); } -void ModThenAcquireRawAdcSamples125k(int delay_off, int period_0, int period_1, BYTE *command) +void ModThenAcquireRawAdcSamples125k(int delay_off, int period_0, int period_1, uint8_t *command) { - BOOL at134khz; + int at134khz; /* Make sure the tag is reset */ FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF); @@ -141,13 +142,13 @@ void ReadTItag(void) // int n = GraphTraceLen; // 128 bit shift register [shift3:shift2:shift1:shift0] - DWORD shift3 = 0, shift2 = 0, shift1 = 0, shift0 = 0; + uint32_t shift3 = 0, shift2 = 0, shift1 = 0, shift0 = 0; int i, cycles=0, samples=0; // how many sample points fit in 16 cycles of each frequency - DWORD sampleslo = (FSAMPLE<<4)/FREQLO, sampleshi = (FSAMPLE<<4)/FREQHI; + uint32_t sampleslo = (FSAMPLE<<4)/FREQLO, sampleshi = (FSAMPLE<<4)/FREQHI; // when to tell if we're close enough to one freq or another - DWORD threshold = (sampleslo - sampleshi + 1)>>1; + uint32_t threshold = (sampleslo - sampleshi + 1)>>1; // TI tags charge at 134.2Khz FpgaSendCommand(FPGA_CMD_SET_DIVISOR, 88); //134.8Khz @@ -236,7 +237,7 @@ void ReadTItag(void) // i'm 99% sure the crc algorithm is correct, but it may need to eat the // bytes in reverse or something // calculate CRC - DWORD crc=0; + uint32_t crc=0; crc = update_crc16(crc, (shift0)&0xff); crc = update_crc16(crc, (shift0>>8)&0xff); @@ -257,7 +258,7 @@ void ReadTItag(void) } } -void WriteTIbyte(BYTE b) +void WriteTIbyte(uint8_t b) { int i = 0; @@ -286,7 +287,7 @@ void AcquireTiType(void) { int i, j, n; // tag transmission is <20ms, sampling at 2M gives us 40K samples max - // each sample is 1 bit stuffed into a DWORD so we need 1250 DWORDS + // each sample is 1 bit stuffed into a uint32_t so we need 1250 uint32_t #define TIBUFLEN 1250 // clear buffer @@ -355,7 +356,7 @@ void AcquireTiType(void) // arguments: 64bit data split into 32bit idhi:idlo and optional 16bit crc // if crc provided, it will be written with the data verbatim (even if bogus) // if not provided a valid crc will be computed from the data and written. -void WriteTItag(DWORD idhi, DWORD idlo, WORD crc) +void WriteTItag(uint32_t idhi, uint32_t idlo, uint16_t crc) { if(crc == 0) { crc = update_crc16(crc, (idlo)&0xff); @@ -426,7 +427,7 @@ void WriteTItag(DWORD idhi, DWORD idlo, WORD crc) void SimulateTagLowFrequency(int period, int gap, int ledcontrol) { int i; - BYTE *tab = (BYTE *)BigBuf; + uint8_t *tab = (uint8_t *)BigBuf; FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_SIMULATOR); @@ -673,7 +674,7 @@ static void hitag_handle_frame(int t0, int frame_len, char *frame) // compose fc/8 fc/10 waveform static void fc(int c, int *n) { - BYTE *dest = (BYTE *)BigBuf; + uint8_t *dest = (uint8_t *)BigBuf; int idx; // for when we want an fc8 pattern every 4 logical bits @@ -778,9 +779,9 @@ void CmdHIDsimTAG(int hi, int lo, int ledcontrol) // loop to capture raw HID waveform then FSK demodulate the TAG ID from it void CmdHIDdemodFSK(int findone, int *high, int *low, int ledcontrol) { - BYTE *dest = (BYTE *)BigBuf; + uint8_t *dest = (uint8_t *)BigBuf; int m=0, n=0, i=0, idx=0, found=0, lastval=0; - DWORD hi=0, lo=0; + uint32_t hi=0, lo=0; FpgaSendCommand(FPGA_CMD_SET_DIVISOR, 95); //125Khz FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER); @@ -815,7 +816,7 @@ void CmdHIDdemodFSK(int findone, int *high, int *low, int ledcontrol) LED_D_ON(); } if(AT91C_BASE_SSC->SSC_SR & (AT91C_SSC_RXRDY)) { - dest[i] = (BYTE)AT91C_BASE_SSC->SSC_RHR; + dest[i] = (uint8_t)AT91C_BASE_SSC->SSC_RHR; // we don't care about actual value, only if it's more or less than a // threshold essentially we capture zero crossings for later analysis if(dest[i] < 127) dest[i] = 0; else dest[i] = 1; diff --git a/armsrc/printf.c b/armsrc/printf.c index 6060870d..49537c64 100644 --- a/armsrc/printf.c +++ b/armsrc/printf.c @@ -34,40 +34,28 @@ * @(#)subr_prf.c 8.3 (Berkeley) 1/21/94 */ -typedef unsigned long size_t; -typedef long ssize_t; -#ifdef __64BIT__ -typedef unsigned long long uintmax_t; -typedef long long intmax_t; -#else -typedef unsigned int uintmax_t; -typedef int intmax_t; -#endif +#include +#include +#include "printf.h" +#include "util.h" + +typedef uint32_t uintmax_t; +typedef int32_t intmax_t; + typedef unsigned char u_char; typedef unsigned int u_int; typedef unsigned long u_long; typedef unsigned short u_short; typedef unsigned long long u_quad_t; typedef long long quad_t; -typedef unsigned long uintptr_t; -typedef long ptrdiff_t; -#define NULL ((void*)0) + +typedef int ssize_t; + #define NBBY 8 /* number of bits in a byte */ + char const hex2ascii_data[] = "0123456789abcdefghijklmnopqrstuvwxyz"; #define hex2ascii(hex) (hex2ascii_data[hex]) -#define va_list __builtin_va_list -#define va_start __builtin_va_start -#define va_arg __builtin_va_arg -#define va_end __builtin_va_end #define toupper(c) ((c) - 0x20 * (((c) >= 'a') && ((c) <= 'z'))) -static size_t -strlen(const char *s) -{ - size_t l = 0; - while (*s++) - l++; - return l; -} /* Max number conversion buffer length: a u_quad_t in base 2, plus NUL byte. */ #define MAXNBUF (sizeof(intmax_t) * NBBY + 1) @@ -431,6 +419,11 @@ number: #undef PCHAR } +int vsprintf(char *dest, const char *fmt, va_list ap) +{ + return kvsprintf(fmt, dest, 10, ap); +} + int sprintf(char *dest, const char *fmt, ...) { diff --git a/armsrc/printf.h b/armsrc/printf.h new file mode 100644 index 00000000..5c2ade8c --- /dev/null +++ b/armsrc/printf.h @@ -0,0 +1,12 @@ +#ifndef __PRINTF_H +#define __PRINTF_H + +#include + +int kvsprintf(const char *format, void *arg, int radix, va_list ap) __attribute__ ((format (printf, 1, 0))); +int vsprintf(char *str, const char *format, va_list ap) __attribute__ ((format (printf, 2, 0))); +int sprintf(char *str, const char *format, ...) __attribute__ ((format (printf, 2, 3))); + + + +#endif \ No newline at end of file diff --git a/armsrc/util.c b/armsrc/util.c index ae2e7247..ae6408cf 100644 --- a/armsrc/util.c +++ b/armsrc/util.c @@ -3,12 +3,12 @@ // Jonathan Westhues, Sept 2005 //----------------------------------------------------------------------------- #include "proxmark3.h" -#include "apps.h" +#include "util.h" void *memcpy(void *dest, const void *src, int len) { - BYTE *d = dest; - const BYTE *s = src; + uint8_t *d = dest; + const uint8_t *s = src; while((len--) > 0) { *d = *s; d++; @@ -19,7 +19,7 @@ void *memcpy(void *dest, const void *src, int len) void *memset(void *dest, int c, int len) { - BYTE *d = dest; + uint8_t *d = dest; while((len--) > 0) { *d = c; d++; @@ -29,8 +29,8 @@ void *memset(void *dest, int c, int len) int memcmp(const void *av, const void *bv, int len) { - const BYTE *a = av; - const BYTE *b = bv; + const uint8_t *a = av; + const uint8_t *b = bv; while((len--) > 0) { if(*a != *b) { @@ -42,7 +42,7 @@ int memcmp(const void *av, const void *bv, int len) return 0; } -int strlen(char *str) +int strlen(const char *str) { int l = 0; while(*str) { @@ -64,15 +64,15 @@ char* strncat(char *dest, const char *src, unsigned int n) return dest; } -void num_to_bytes(uint64_t n, size_t len, byte_t* dest) +void num_to_bytes(uint64_t n, size_t len, uint8_t* dest) { while (len--) { - dest[len] = (byte_t) n; + dest[len] = (uint8_t) n; n >>= 8; } } -uint64_t bytes_to_num(byte_t* src, size_t len) +uint64_t bytes_to_num(uint8_t* src, size_t len) { uint64_t num = 0; while (len--) @@ -139,12 +139,12 @@ int BUTTON_CLICKED(int ms) AT91C_BASE_PWMC_CH0->PWMC_CDTYR = 0; AT91C_BASE_PWMC_CH0->PWMC_CPRDR = 0xffff; - WORD start = AT91C_BASE_PWMC_CH0->PWMC_CCNTR; + uint16_t start = AT91C_BASE_PWMC_CH0->PWMC_CCNTR; int letoff = 0; for(;;) { - WORD now = AT91C_BASE_PWMC_CH0->PWMC_CCNTR; + uint16_t now = AT91C_BASE_PWMC_CH0->PWMC_CCNTR; // We haven't let off the button yet if (!letoff) @@ -162,7 +162,7 @@ int BUTTON_CLICKED(int ms) // Still haven't let it off else // Have we held down a full second? - if (now == (WORD)(start + ticks)) + if (now == (uint16_t)(start + ticks)) return BUTTON_HOLD; } @@ -174,7 +174,7 @@ int BUTTON_CLICKED(int ms) // Have we ran out of time to double click? else - if (now == (WORD)(start + ticks)) + if (now == (uint16_t)(start + ticks)) // At least we did a single click return BUTTON_SINGLE_CLICK; @@ -202,11 +202,11 @@ int BUTTON_HELD(int ms) AT91C_BASE_PWMC_CH0->PWMC_CDTYR = 0; AT91C_BASE_PWMC_CH0->PWMC_CPRDR = 0xffff; - WORD start = AT91C_BASE_PWMC_CH0->PWMC_CCNTR; + uint16_t start = AT91C_BASE_PWMC_CH0->PWMC_CCNTR; for(;;) { - WORD now = AT91C_BASE_PWMC_CH0->PWMC_CCNTR; + uint16_t now = AT91C_BASE_PWMC_CH0->PWMC_CCNTR; // As soon as our button let go, we didn't hold long enough if (!BUTTON_PRESS()) @@ -214,7 +214,7 @@ int BUTTON_HELD(int ms) // Have we waited the full second? else - if (now == (WORD)(start + ticks)) + if (now == (uint16_t)(start + ticks)) return BUTTON_HOLD; WDT_HIT(); @@ -237,11 +237,11 @@ void SpinDelayUs(int us) AT91C_BASE_PWMC_CH0->PWMC_CDTYR = 0; AT91C_BASE_PWMC_CH0->PWMC_CPRDR = 0xffff; - WORD start = AT91C_BASE_PWMC_CH0->PWMC_CCNTR; + uint16_t start = AT91C_BASE_PWMC_CH0->PWMC_CCNTR; for(;;) { - WORD now = AT91C_BASE_PWMC_CH0->PWMC_CCNTR; - if (now == (WORD)(start + ticks)) + uint16_t now = AT91C_BASE_PWMC_CH0->PWMC_CCNTR; + if (now == (uint16_t)(start + ticks)) return; WDT_HIT(); diff --git a/armsrc/util.h b/armsrc/util.h new file mode 100644 index 00000000..621f3fe5 --- /dev/null +++ b/armsrc/util.h @@ -0,0 +1,32 @@ +#ifndef __UTIL_H +#define __UTIL_H + +#include +#include + +#define LED_RED 1 +#define LED_ORANGE 2 +#define LED_GREEN 4 +#define LED_RED2 8 +#define BUTTON_HOLD 1 +#define BUTTON_NO_CLICK 0 +#define BUTTON_SINGLE_CLICK -1 +#define BUTTON_DOUBLE_CLICK -2 +#define BUTTON_ERROR -99 +int strlen(const char *str); +void *memcpy(void *dest, const void *src, int len); +void *memset(void *dest, int c, int len); +int memcmp(const void *av, const void *bv, int len); +char *strncat(char *dest, const char *src, unsigned int n); +void num_to_bytes(uint64_t n, size_t len, uint8_t* dest); +uint64_t bytes_to_num(uint8_t* src, size_t len); + +void SpinDelay(int ms); +void SpinDelayUs(int us); +void LED(int led, int ms); +void LEDsoff(); +int BUTTON_CLICKED(int ms); +int BUTTON_HELD(int ms); +void FormatVersionInformation(char *dst, int len, const char *prefix, void *version_information); + +#endif \ No newline at end of file diff --git a/bootrom/bootrom.c b/bootrom/bootrom.c index da0de9c7..04bc14be 100644 --- a/bootrom/bootrom.c +++ b/bootrom/bootrom.c @@ -69,11 +69,11 @@ static void Fatal(void) for(;;); } -void UsbPacketReceived(BYTE *packet, int len) +void UsbPacketReceived(uint8_t *packet, int len) { int i, dont_ack=0; UsbCommand *c = (UsbCommand *)packet; - volatile DWORD *p; + volatile uint32_t *p; if(len != sizeof(*c)) { Fatal(); @@ -93,14 +93,14 @@ void UsbPacketReceived(BYTE *packet, int len) /* The temporary write buffer of the embedded flash controller is mapped to the * whole memory region, only the last 8 bits are decoded. */ - p = (volatile DWORD *)&_flash_start; + p = (volatile uint32_t *)&_flash_start; for(i = 0; i < 12; i++) { p[i+c->arg[0]] = c->d.asDwords[i]; } break; case CMD_FINISH_WRITE: - p = (volatile DWORD *)&_flash_start; + p = (volatile uint32_t *)&_flash_start; for(i = 0; i < 4; i++) { p[i+60] = c->d.asDwords[i]; } diff --git a/bootrom/fromflash.c b/bootrom/fromflash.c index 80403e5f..cf754544 100644 --- a/bootrom/fromflash.c +++ b/bootrom/fromflash.c @@ -5,9 +5,9 @@ void __attribute__((section(".bootphase1"))) CopyBootToRAM(void) { int i; - volatile DWORD *s = (volatile DWORD *)&__bootphase2_src_start__; - volatile DWORD *d = (volatile DWORD *)&__bootphase2_start__; + volatile uint32_t *s = (volatile uint32_t *)&__bootphase2_src_start__; + volatile uint32_t *d = (volatile uint32_t *)&__bootphase2_start__; unsigned int l = (int)&__bootphase2_end__ - (int)&__bootphase2_start__; - for(i = 0; i < l/sizeof(DWORD); i++) *d++ = *s++; + for(i = 0; i < l/sizeof(uint32_t); i++) *d++ = *s++; } diff --git a/common/usb.c b/common/usb.c index cf324f19..6c5207d6 100644 --- a/common/usb.c +++ b/common/usb.c @@ -10,11 +10,11 @@ #define USB_REPORT_PACKET_SIZE 64 typedef struct PACKED { - BYTE bmRequestType; - BYTE bRequest; - WORD wValue; - WORD wIndex; - WORD wLength; + uint8_t bmRequestType; + uint8_t bRequest; + uint16_t wValue; + uint16_t wIndex; + uint16_t wLength; } UsbSetupData; #define USB_REQUEST_GET_STATUS 0 @@ -42,7 +42,7 @@ typedef struct PACKED { #define USB_DEVICE_CLASS_HID 0x03 -static const BYTE HidReportDescriptor[] = { +static const uint8_t HidReportDescriptor[] = { 0x06,0xA0,0xFF, // Usage Page (vendor defined) FFA0 0x09,0x01, // Usage (vendor defined) 0xA1,0x01, // Collection (Application) @@ -77,7 +77,7 @@ static const BYTE HidReportDescriptor[] = { 0xC0, // End Collection }; -static const BYTE DeviceDescriptor[] = { +static const uint8_t DeviceDescriptor[] = { 0x12, // Descriptor length (18 bytes) 0x01, // Descriptor type (Device) 0x10,0x01, // Complies with USB Spec. Release (0110h = release 1.10) @@ -94,7 +94,7 @@ static const BYTE DeviceDescriptor[] = { 0x01, // Number of possible configurations (1) }; -static const BYTE ConfigurationDescriptor[] = { +static const uint8_t ConfigurationDescriptor[] = { 0x09, // Descriptor length (9 bytes) 0x02, // Descriptor type (Configuration) 0x29,0x00, // Total data length (41 bytes) @@ -142,14 +142,14 @@ static const BYTE ConfigurationDescriptor[] = { 0x01, // Polling interval (1 ms) }; -static const BYTE StringDescriptor0[] = { +static const uint8_t StringDescriptor0[] = { 0x04, // Length 0x03, // Type is string 0x09, // English 0x04, // US }; -static const BYTE StringDescriptor1[] = { +static const uint8_t StringDescriptor1[] = { 24, // Length 0x03, // Type is string 'J', 0x00, @@ -165,7 +165,7 @@ static const BYTE StringDescriptor1[] = { 's', 0x00, }; -static const BYTE StringDescriptor2[] = { +static const uint8_t StringDescriptor2[] = { 54, // Length 0x03, // Type is string 'P', 0x00, @@ -196,19 +196,19 @@ static const BYTE StringDescriptor2[] = { 't', 0x00, }; -static const BYTE * const StringDescriptors[] = { +static const uint8_t * const StringDescriptors[] = { StringDescriptor0, StringDescriptor1, StringDescriptor2, }; -static BYTE UsbBuffer[64]; +static uint8_t UsbBuffer[64]; static int UsbSoFarCount; -static BYTE CurrentConfiguration; +static uint8_t CurrentConfiguration; -static void UsbSendEp0(const BYTE *data, int len) +static void UsbSendEp0(const uint8_t *data, int len) { int thisTime, i; @@ -278,7 +278,7 @@ static void HandleRxdSetupData(void) UsbSetupData usd; for(i = 0; i < sizeof(usd); i++) { - ((BYTE *)&usd)[i] = AT91C_BASE_UDP->UDP_FDR[0]; + ((uint8_t *)&usd)[i] = AT91C_BASE_UDP->UDP_FDR[0]; } if(usd.bmRequestType & 0x80) { @@ -294,19 +294,19 @@ static void HandleRxdSetupData(void) switch(usd.bRequest) { case USB_REQUEST_GET_DESCRIPTOR: if((usd.wValue >> 8) == USB_DESCRIPTOR_TYPE_DEVICE) { - UsbSendEp0((BYTE *)&DeviceDescriptor, + UsbSendEp0((uint8_t *)&DeviceDescriptor, min(sizeof(DeviceDescriptor), usd.wLength)); } else if((usd.wValue >> 8) == USB_DESCRIPTOR_TYPE_CONFIGURATION) { - UsbSendEp0((BYTE *)&ConfigurationDescriptor, + UsbSendEp0((uint8_t *)&ConfigurationDescriptor, min(sizeof(ConfigurationDescriptor), usd.wLength)); } else if((usd.wValue >> 8) == USB_DESCRIPTOR_TYPE_STRING) { - const BYTE *s = StringDescriptors[usd.wValue & 0xff]; + const uint8_t *s = StringDescriptors[usd.wValue & 0xff]; UsbSendEp0(s, min(s[0], usd.wLength)); } else if((usd.wValue >> 8) == USB_DESCRIPTOR_TYPE_HID_REPORT) { - UsbSendEp0((BYTE *)&HidReportDescriptor, + UsbSendEp0((uint8_t *)&HidReportDescriptor, min(sizeof(HidReportDescriptor), usd.wLength)); } else { - *((DWORD *)0x00200000) = usd.wValue; + *((uint32_t *)0x00200000) = usd.wValue; } break; @@ -326,8 +326,8 @@ static void HandleRxdSetupData(void) case USB_REQUEST_GET_STATUS: { if(usd.bmRequestType & 0x80) { - WORD w = 0; - UsbSendEp0((BYTE *)&w, sizeof(w)); + uint16_t w = 0; + UsbSendEp0((uint8_t *)&w, sizeof(w)); } break; } @@ -348,7 +348,7 @@ static void HandleRxdSetupData(void) break; case USB_REQUEST_GET_INTERFACE: { - BYTE b = 0; + uint8_t b = 0; UsbSendEp0(&b, sizeof(b)); break; } @@ -368,7 +368,7 @@ static void HandleRxdSetupData(void) } } -void UsbSendPacket(BYTE *packet, int len) +void UsbSendPacket(uint8_t *packet, int len) { int i, thisTime; @@ -451,7 +451,7 @@ void UsbStart(void) } } -BOOL UsbConnected() +int UsbConnected() { if (AT91C_BASE_UDP->UDP_GLBSTATE & AT91C_UDP_CONFG) return TRUE; @@ -459,9 +459,9 @@ BOOL UsbConnected() return FALSE; } -BOOL UsbPoll(BOOL blinkLeds) +int UsbPoll(int blinkLeds) { - BOOL ret = FALSE; + int ret = FALSE; if(AT91C_BASE_UDP->UDP_ISR & AT91C_UDP_ENDBUSRES) { AT91C_BASE_UDP->UDP_ICR = AT91C_UDP_ENDBUSRES; diff --git a/include/proxmark3.h b/include/proxmark3.h index c9fbdab6..fb1c6db1 100644 --- a/include/proxmark3.h +++ b/include/proxmark3.h @@ -7,8 +7,8 @@ #define __PROXMARK3_H // Might as well have the hardware-specific defines everywhere. -#include -#include +#include "at91sam7s512.h" +#include "config_gpio.h" #define WDT_HIT() AT91C_BASE_WDTC->WDTC_WDCR = 0xa5000001 @@ -67,14 +67,6 @@ #define SPI_FPGA_MODE 0 #define SPI_LCD_MODE 1 -typedef unsigned long DWORD; -typedef signed long SDWORD; -typedef unsigned long long QWORD; -typedef int BOOL; -typedef unsigned char BYTE; -typedef signed char SBYTE; -typedef unsigned short WORD; -typedef signed short SWORD; #define TRUE 1 #define FALSE 0 @@ -102,14 +94,14 @@ typedef signed short SWORD; //-------------------------------- // USB declarations -void UsbSendPacket(BYTE *packet, int len); -BOOL UsbConnected(); -BOOL UsbPoll(BOOL blinkLeds); +void UsbSendPacket(uint8_t *packet, int len); +int UsbConnected(); +int UsbPoll(int blinkLeds); void UsbStart(void); // This function is provided by the apps/bootrom, and called from UsbPoll // if data are available. -void UsbPacketReceived(BYTE *packet, int len); +void UsbPacketReceived(uint8_t *packet, int len); #define VERSION_INFORMATION_MAGIC 0x56334d50 struct version_information { -- 2.39.2