X-Git-Url: http://cvs.zerfleddert.de/cgi-bin/gitweb.cgi/proxmark3-svn/blobdiff_plain/aaa1a9a2dcb8b9f2b6acd937f84464daa50ecad6..34c30823382649d3c66502bd7cc643cf51778b1b:/armsrc/appmain.c

diff --git a/armsrc/appmain.c b/armsrc/appmain.c
index 2ba3d75c..a2c12803 100644
--- a/armsrc/appmain.c
+++ b/armsrc/appmain.c
@@ -9,18 +9,14 @@
 // The main application code. This is the first thing called after start.c
 // executes.
 //-----------------------------------------------------------------------------
-
 #include "usb_cdc.h"
-#include "cmd.h"
-
+//#include "cmd.h"
 #include "proxmark3.h"
 #include "apps.h"
 #include "util.h"
 #include "printf.h"
 #include "string.h"
-
 #include <stdarg.h>
-
 #include "legicrf.h"
 #include "hitag2.h"
 #include "hitagS.h"
@@ -28,6 +24,7 @@
 #include "BigBuf.h"
 #include "mifareutil.h"
 #include "pcf7931.h"
+
 #ifdef WITH_LCD
  #include "LCD.h"
 #endif
@@ -38,8 +35,6 @@
  #include "protocols.h"
 #endif
 
-#define abs(x) ( ((x)<0) ? -(x) : (x) )
-
 //=============================================================================
 // 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
@@ -76,6 +71,11 @@ void ToSendStuffBit(int b) {
 	}
 }
 
+void PrintToSendBuffer(void){
+	DbpString("Printing ToSendBuffer:");
+	Dbhexdump(ToSendMax, ToSend, 0);
+}
+
 //=============================================================================
 // Debug print functions, to go out over USB, to the usual PC-side client.
 //=============================================================================
@@ -182,20 +182,21 @@ static int ReadAdc(int ch)
 
 int AvgAdc(int ch) // was static - merlok
 {
-	int i;
-	int a = 0;
-
+	int i, a = 0;
 	for(i = 0; i < 32; ++i)
 		a += ReadAdc(ch);
 
 	return (a + 15) >> 5;
 }
 
+
 void MeasureAntennaTuning(void) {
+
 	uint8_t LF_Results[256];
-	int i, adcval = 0, peak = 0, peakv = 0, peakf = 0; //ptr = 0 
+	int i, adcval = 0, peak = 0, peakv = 0, peakf = 0;
 	int vLf125 = 0, vLf134 = 0, vHf = 0;	// in mV
 
+	memset(LF_Results, 0, sizeof(LF_Results));
 	LED_B_ON();
 
 /*
@@ -209,8 +210,8 @@ void MeasureAntennaTuning(void) {
   
   	FpgaDownloadAndGo(FPGA_BITSTREAM_LF);
 	FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_ADC | FPGA_LF_ADC_READER_FIELD);
-
-	for (i=255; i>=19; i--) {
+		
+	for  (i = 255; i >= 19; i--) {
 		WDT_HIT();
 		FpgaSendCommand(FPGA_CMD_SET_DIVISOR, i);
 		SpinDelay(20);
@@ -218,17 +219,14 @@ void MeasureAntennaTuning(void) {
 		if (i==95) 	vLf125 = adcval; // voltage at 125Khz
 		if (i==89) 	vLf134 = adcval; // voltage at 134Khz
 
-		LF_Results[i] = adcval>>8; // scale int to fit in byte for graphing purposes
+		LF_Results[i] = adcval >> 8; // scale int to fit in byte for graphing purposes
 		if(LF_Results[i] > peak) {
 			peakv = adcval;
 			peak = LF_Results[i];
 			peakf = i;
-			//ptr = i;
 		}
 	}
 
-	for (i=18; i >= 0; i--) LF_Results[i] = 0;
-	
 	LED_A_ON();
 	// Let the FPGA drive the high-frequency antenna around 13.56 MHz.
 	FpgaDownloadAndGo(FPGA_BITSTREAM_HF);
@@ -236,9 +234,8 @@ void MeasureAntennaTuning(void) {
 	SpinDelay(20);
 	vHf = (MAX_ADC_HF_VOLTAGE * AvgAdc(ADC_CHAN_HF)) >> 10;
 
-	cmd_send(CMD_MEASURED_ANTENNA_TUNING, vLf125 | (vLf134<<16), vHf, peakf | (peakv<<16), LF_Results, 256);
+	cmd_send(CMD_MEASURED_ANTENNA_TUNING, vLf125 | (vLf134 << 16), vHf, peakf | (peakv << 16), LF_Results, 256);
 	FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
-
 	LEDsoff();
 }
 
@@ -832,7 +829,7 @@ void ListenReaderField(int limit) {
 
 		if (limit != HF_ONLY) {
 			if(mode == 1) {
-				if (abs(lf_av - lf_baseline) > REPORT_CHANGE) 
+				if (ABS(lf_av - lf_baseline) > REPORT_CHANGE) 
 					LED_D_ON();
 				else
 					LED_D_OFF();
@@ -840,7 +837,7 @@ void ListenReaderField(int limit) {
 
 			lf_av_new = AvgAdc(ADC_CHAN_LF);
 			// see if there's a significant change
-			if(abs(lf_av - lf_av_new) > REPORT_CHANGE) {
+			if(ABS(lf_av - lf_av_new) > REPORT_CHANGE) {
 				Dbprintf("LF 125/134kHz Field Change: %5dmV", (MAX_ADC_LF_VOLTAGE * lf_av_new) >> 10);
 				lf_av = lf_av_new;
 				if (lf_av > lf_max)
@@ -850,7 +847,7 @@ void ListenReaderField(int limit) {
 
 		if (limit != LF_ONLY) {
 			if (mode == 1){
-				if (abs(hf_av - hf_baseline) > REPORT_CHANGE) 	
+				if (ABS(hf_av - hf_baseline) > REPORT_CHANGE) 	
 					LED_B_ON();
 				else
 					LED_B_OFF();
@@ -858,7 +855,7 @@ void ListenReaderField(int limit) {
 
 			hf_av_new = AvgAdc(ADC_CHAN_HF);
 			// see if there's a significant change
-			if(abs(hf_av - hf_av_new) > REPORT_CHANGE) {
+			if(ABS(hf_av - hf_av_new) > REPORT_CHANGE) {
 				Dbprintf("HF 13.56MHz Field Change: %5dmV", (MAX_ADC_HF_VOLTAGE * hf_av_new) >> 10);
 				hf_av = hf_av_new;
 				if (hf_av > hf_max)
@@ -899,7 +896,7 @@ void UsbPacketReceived(uint8_t *packet, int len)
 {
 	UsbCommand *c = (UsbCommand *)packet;
 
-  //Dbprintf("received %d bytes, with command: 0x%04x and args: %d %d %d",len,c->cmd,c->arg[0],c->arg[1],c->arg[2]);
+	//Dbprintf("received %d bytes, with command: 0x%04x and args: %d %d %d",len,c->cmd,c->arg[0],c->arg[1],c->arg[2]);
   
 	switch(c->cmd) {
 #ifdef WITH_LF
@@ -910,7 +907,7 @@ void UsbPacketReceived(uint8_t *packet, int len)
 			cmd_send(CMD_ACK, SampleLF(c->arg[0]),0,0,0,0);
 			break;
 		case CMD_MOD_THEN_ACQUIRE_RAW_ADC_SAMPLES_125K:
-			ModThenAcquireRawAdcSamples125k(c->arg[0],c->arg[1],c->arg[2],c->d.asBytes);
+			ModThenAcquireRawAdcSamples125k(c->arg[0], c->arg[1], c->arg[2], c->d.asBytes);
 			break;
 		case CMD_LF_SNOOP_RAW_ADC_SAMPLES:
 			cmd_send(CMD_ACK,SnoopLF(),0,0,0,0);
@@ -952,7 +949,7 @@ void UsbPacketReceived(uint8_t *packet, int len)
 			WriteTItag(c->arg[0],c->arg[1],c->arg[2]);
 			break;
 		case CMD_SIMULATE_TAG_125K:
-			LED_A_ON();
+			LED_A_ON();		
 			SimulateTagLowFrequency(c->arg[0], c->arg[1], 1);
 			LED_A_OFF();
 			break;
@@ -1013,10 +1010,10 @@ void UsbPacketReceived(uint8_t *packet, int len)
 		case CMD_TEST_HITAGS_TRACES:// Tests every challenge within the given file
 			check_challenges((bool)c->arg[0],(byte_t*)c->d.asBytes);
 			break;
-		case CMD_READ_HITAG_S://Reader for only Hitag S tags, args = key or challenge
+		case CMD_READ_HITAG_S: //Reader for only Hitag S tags, args = key or challenge
 			ReadHitagS((hitag_function)c->arg[0],(hitag_data*)c->d.asBytes);
 			break;
-		case CMD_WR_HITAG_S://writer for Hitag tags args=data to write,page and key or challenge
+		case CMD_WR_HITAG_S: //writer for Hitag tags args=data to write,page and key or challenge
 			WritePageHitagS((hitag_function)c->arg[0],(hitag_data*)c->d.asBytes,c->arg[2]);
 			break;
 #endif
@@ -1053,31 +1050,33 @@ void UsbPacketReceived(uint8_t *packet, int len)
 		case CMD_SIMULATE_TAG_LEGIC_RF:
 			LegicRfSimulate(c->arg[0], c->arg[1], c->arg[2]);
 			break;
-
 		case CMD_WRITER_LEGIC_RF:
-			LegicRfWriter(c->arg[1], c->arg[0]);
+			LegicRfWriter( c->arg[0], c->arg[1], c->arg[2], c->d.asBytes);
 			break;
-
 		case CMD_READER_LEGIC_RF:
-			LegicRfReader(c->arg[0], c->arg[1]);
+			LegicRfReader(c->arg[0], c->arg[1], c->arg[2]);
+			break;			
+		case CMD_LEGIC_INFO:
+			LegicRfInfo();
+			break;
+		case CMD_LEGIC_ESET:
+			LegicEMemSet(c->arg[0], c->arg[1], c->d.asBytes);
 			break;
 #endif
 
 #ifdef WITH_ISO14443b
-		case CMD_READ_SRI512_TAG:
-			ReadSTMemoryIso14443b(0x0F);
-			break;
-		case CMD_READ_SRIX4K_TAG:
-			ReadSTMemoryIso14443b(0x7F);
+		case CMD_READ_SRI_TAG:
+			ReadSTMemoryIso14443b(c->arg[0]);
 			break;
 		case CMD_SNOOP_ISO_14443B:
 			SnoopIso14443b();
 			break;
 		case CMD_SIMULATE_TAG_ISO_14443B:
-			SimulateIso14443bTag();
+			SimulateIso14443bTag(c->arg[0]);
 			break;
 		case CMD_ISO_14443B_COMMAND:
-			SendRawCommand14443B(c->arg[0],c->arg[1],c->arg[2],c->d.asBytes);
+			//SendRawCommand14443B(c->arg[0],c->arg[1],c->arg[2],c->d.asBytes);
+			SendRawCommand14443B_Ex(c);
 			break;
 #endif
 
@@ -1091,16 +1090,14 @@ void UsbPacketReceived(uint8_t *packet, int len)
 		case CMD_SIMULATE_TAG_ISO_14443a:
 			SimulateIso14443aTag(c->arg[0], c->arg[1], c->d.asBytes);  // ## Simulate iso14443a tag - pass tag type & UID
 			break;
-			
 		case CMD_EPA_PACE_COLLECT_NONCE:
 			EPA_PACE_Collect_Nonce(c);
 			break;
 		case CMD_EPA_PACE_REPLAY:
 			EPA_PACE_Replay(c);
 			break;
-			
 		case CMD_READER_MIFARE:
-            ReaderMifare(c->arg[0], c->arg[1]);
+            ReaderMifare(c->arg[0], c->arg[1], c->arg[2]);
 			break;
 		case CMD_MIFARE_READBL:
 			MifareReadBlock(c->arg[0], c->arg[1], c->arg[2], c->d.asBytes);
@@ -1193,7 +1190,6 @@ void UsbPacketReceived(uint8_t *packet, int len)
 		case CMD_MIFARE_DESFIRE:
 			MifareSendCommand(c->arg[0], c->arg[1], c->d.asBytes);
 			break;
-
 		case CMD_MIFARE_COLLECT_NONCES:
 			break;
 #endif
@@ -1224,7 +1220,7 @@ void UsbPacketReceived(uint8_t *packet, int len)
 		case CMD_READER_ICLASS_REPLAY:
 		    ReaderIClass_Replay(c->arg[0], c->d.asBytes);
 			break;
-	case CMD_ICLASS_EML_MEMSET:
+		case CMD_ICLASS_EML_MEMSET:
 			emlSet(c->d.asBytes,c->arg[0], c->arg[1]);
 			break;
 		case CMD_ICLASS_WRITEBLOCK:
@@ -1288,6 +1284,12 @@ void UsbPacketReceived(uint8_t *packet, int len)
 			break;
 		}
 		case CMD_DOWNLOADED_SIM_SAMPLES_125K: {
+			// iceman; since changing fpga_bitstreams clears bigbuff, Its better to call it before.
+			// to be able to use this one for uploading data to device not only for LF, I use c->arg[1] 
+			if ( c->arg[1] == 0 )
+				FpgaDownloadAndGo(FPGA_BITSTREAM_LF);
+			else 
+				FpgaDownloadAndGo(FPGA_BITSTREAM_HF);
 			uint8_t *b = BigBuf_get_addr();
 			memcpy( b + c->arg[0], c->d.asBytes, USB_CMD_DATA_SIZE);
 			cmd_send(CMD_ACK,0,0,0,0,0);
@@ -1385,10 +1387,7 @@ void  __attribute__((noreturn)) AppMain(void)
 	}
 	common_area.flags.osimage_present = 1;
 
-	LED_D_OFF();
-	LED_C_OFF();
-	LED_B_OFF();
-	LED_A_OFF();
+	LEDsoff();
 
 	// Init USB device
 	usb_enable();
@@ -1398,8 +1397,7 @@ void  __attribute__((noreturn)) AppMain(void)
 	AT91C_BASE_PIOA->PIO_PDR = GPIO_PCK0;
 	AT91C_BASE_PMC->PMC_SCER = AT91C_PMC_PCK0;
 	// PCK0 is PLL clock / 4 = 96Mhz / 4 = 24Mhz
-	AT91C_BASE_PMC->PMC_PCKR[0] = AT91C_PMC_CSS_PLL_CLK |
-		AT91C_PMC_PRES_CLK_4; //  4 for 24Mhz pck0, 2 for 48 MHZ pck0
+	AT91C_BASE_PMC->PMC_PCKR[0] = AT91C_PMC_CSS_PLL_CLK | AT91C_PMC_PRES_CLK_4; //  4 for 24Mhz pck0, 2 for 48 MHZ pck0
 	AT91C_BASE_PIOA->PIO_OER = GPIO_PCK0;
 
 	// Reset SPI
@@ -1421,10 +1419,11 @@ void  __attribute__((noreturn)) AppMain(void)
 	size_t rx_len;
   
 	for(;;) {
-		if (usb_poll()) {
-			rx_len = usb_read(rx,sizeof(UsbCommand));
+		if ( usb_poll_validate_length() ) {
+			rx_len = usb_read(rx, sizeof(UsbCommand));
+			
 			if (rx_len)
-				UsbPacketReceived(rx,rx_len);
+				UsbPacketReceived(rx, rx_len);
 		}
 		WDT_HIT();