]> git.zerfleddert.de Git - proxmark3-svn/blobdiff - armsrc/appmain.c
alphabet ;)
[proxmark3-svn] / armsrc / appmain.c
index 5313202abb4ac8e30ff3b41f31edd7dd8b566ab5..b231cae1e27b1623eb734b886aed489efe5986e7 100644 (file)
@@ -8,18 +8,25 @@
 #include <proxmark3.h>\r
 #include <stdlib.h>\r
 #include "apps.h"\r
+#include "legicrf.h"\r
 #ifdef WITH_LCD\r
 #include "fonts.h"\r
 #include "LCD.h"\r
 #endif\r
 \r
+#define va_list __builtin_va_list\r
+#define va_start __builtin_va_start\r
+#define va_arg __builtin_va_arg\r
+#define va_end __builtin_va_end\r
+int kvsprintf(char const *fmt, void *arg, int radix, va_list ap);\r
+       \r
 //=============================================================================\r
 // A buffer where we can queue things up to be sent through the FPGA, for\r
 // any purpose (fake tag, as reader, whatever). We go MSB first, since that\r
 // is the order in which they go out on the wire.\r
 //=============================================================================\r
 \r
-BYTE ToSend[256];\r
+BYTE ToSend[512];\r
 int ToSendMax;\r
 static int ToSendBit;\r
 struct common_area common_area __attribute__((section(".commonarea")));\r
@@ -27,7 +34,7 @@ struct common_area common_area __attribute__((section(".commonarea")));
 void BufferClear(void)\r
 {\r
        memset(BigBuf,0,sizeof(BigBuf));\r
-       DbpString("Buffer cleared");\r
+       Dbprintf("Buffer cleared (%i bytes)",sizeof(BigBuf));\r
 }\r
 \r
 void ToSendReset(void)\r
@@ -68,14 +75,18 @@ void DbpString(char *str)
 \r
        UsbCommand c;\r
        c.cmd = CMD_DEBUG_PRINT_STRING;\r
-       c.ext1 = strlen(str);\r
-       memcpy(c.d.asBytes, str, c.ext1);\r
+       c.arg[0] = strlen(str);\r
+       if(c.arg[0] > sizeof(c.d.asBytes)) {\r
+               c.arg[0] = sizeof(c.d.asBytes);\r
+       }\r
+       memcpy(c.d.asBytes, str, c.arg[0]);\r
 \r
        UsbSendPacket((BYTE *)&c, sizeof(c));\r
        // TODO fix USB so stupid things like this aren't req'd\r
        SpinDelay(50);\r
 }\r
 \r
+#if 0\r
 void DbpIntegers(int x1, int x2, int x3)\r
 {\r
        /* this holds up stuff unless we're connected to usb */\r
@@ -84,14 +95,27 @@ void DbpIntegers(int x1, int x2, int x3)
 \r
        UsbCommand c;\r
        c.cmd = CMD_DEBUG_PRINT_INTEGERS;\r
-       c.ext1 = x1;\r
-       c.ext2 = x2;\r
-       c.ext3 = x3;\r
+       c.arg[0] = x1;\r
+       c.arg[1] = x2;\r
+       c.arg[2] = x3;\r
 \r
        UsbSendPacket((BYTE *)&c, sizeof(c));\r
        // XXX\r
        SpinDelay(50);\r
 }\r
+#endif\r
+\r
+void Dbprintf(const char *fmt, ...) {\r
+// should probably limit size here; oh well, let's just use a big buffer\r
+       char output_string[128];\r
+       va_list ap;\r
+\r
+       va_start(ap, fmt);\r
+       kvsprintf(fmt, output_string, 10, ap);\r
+       va_end(ap);\r
\r
+       DbpString(output_string);\r
+}\r
 \r
 //-----------------------------------------------------------------------------\r
 // Read an ADC channel and block till it completes, then return the result\r
@@ -175,12 +199,33 @@ void MeasureAntennaTuning(void)
        vHf = (33000 * AvgAdc(ADC_CHAN_HF)) >> 10;\r
 \r
        c.cmd = CMD_MEASURED_ANTENNA_TUNING;\r
-       c.ext1 = (vLf125 << 0) | (vLf134 << 16);\r
-       c.ext2 = vHf;\r
-       c.ext3 = peakf | (peakv << 16);\r
+       c.arg[0] = (vLf125 << 0) | (vLf134 << 16);\r
+       c.arg[1] = vHf;\r
+       c.arg[2] = peakf | (peakv << 16);\r
        UsbSendPacket((BYTE *)&c, sizeof(c));\r
 }\r
 \r
+void MeasureAntennaTuningHf(void)\r
+{\r
+       int vHf = 0;    // in mV\r
+\r
+       DbpString("Measuring HF antenna, press button to exit");\r
+\r
+       for (;;) {\r
+               // Let the FPGA drive the high-frequency antenna around 13.56 MHz.\r
+               FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR);\r
+               SpinDelay(20);\r
+               // Vref = 3300mV, and an 10:1 voltage divider on the input\r
+               // can measure voltages up to 33000 mV\r
+               vHf = (33000 * AvgAdc(ADC_CHAN_HF)) >> 10;\r
+       \r
+               Dbprintf("%d mV",vHf);\r
+               if (BUTTON_PRESS()) break;\r
+       }\r
+       DbpString("cancelled");\r
+}\r
+\r
+\r
 void SimulateTagHfListen(void)\r
 {\r
        BYTE *dest = (BYTE *)BigBuf;\r
@@ -229,13 +274,10 @@ void SimulateTagHfListen(void)
 \r
 void ReadMem(int addr)\r
 {\r
-       const DWORD *data = ((DWORD *)addr);\r
-       int i;\r
+       const BYTE *data = ((BYTE *)addr);\r
 \r
-       DbpString("Reading memory at address");\r
-       DbpIntegers(0, 0, addr);\r
-       for (i = 0; i < 8; i+= 2)\r
-               DbpIntegers(0, data[i], data[i+1]);\r
+       Dbprintf("%x: %02x %02x %02x %02x %02x %02x %02x %02x",\r
+               addr, data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7]);\r
 }\r
 \r
 /* osimage version information is linked in */\r
@@ -266,6 +308,7 @@ void SendVersion(void)
        DbpString(temp);\r
 }\r
 \r
+#ifdef WITH_LF\r
 // samy's sniff and repeat routine\r
 void SamyRun()\r
 {\r
@@ -320,8 +363,7 @@ void SamyRun()
                        SpinDelay(500);\r
 \r
                        CmdHIDdemodFSK(1, &high[selected], &low[selected], 0);\r
-                       DbpString("Recorded");\r
-                       DbpIntegers(selected, high[selected], low[selected]);\r
+                       Dbprintf("Recorded %x %x %x", selected, high[selected], low[selected]);\r
 \r
                        LEDsoff();\r
                        LED(selected + 1, 0);\r
@@ -351,7 +393,7 @@ void SamyRun()
                                // wait for button to be released\r
                                while(BUTTON_PRESS())\r
                                        WDT_HIT();\r
-                               DbpIntegers(selected, high[selected], low[selected]);\r
+                               Dbprintf("%x %x %x", selected, high[selected], low[selected]);\r
                                CmdHIDsimTAG(high[selected], low[selected], 0);\r
                                DbpString("Done playing");\r
                                if (BUTTON_HELD(1000) > 0)\r
@@ -376,7 +418,7 @@ void SamyRun()
                }\r
        }\r
 }\r
-\r
+#endif\r
 \r
 /*\r
 OBJECTIVE\r
@@ -429,17 +471,15 @@ void ListenReaderField(int limit)
        lf_av=lf_max=ReadAdc(ADC_CHAN_LF);\r
 \r
        if(limit != HF_ONLY) {\r
-               DbpString("LF 125/134 Baseline:");\r
-               DbpIntegers(lf_av,0,0);\r
-               lf_baseline= lf_av;\r
+               Dbprintf("LF 125/134 Baseline: %d", lf_av);\r
+               lf_baseline = lf_av;\r
        }\r
 \r
        hf_av=hf_max=ReadAdc(ADC_CHAN_HF);\r
 \r
        if (limit != LF_ONLY) {\r
-               DbpString("HF 13.56 Baseline:");\r
-               DbpIntegers(hf_av,0,0);\r
-               hf_baseline= hf_av;\r
+               Dbprintf("HF 13.56 Baseline: %d", hf_av);\r
+               hf_baseline = hf_av;\r
        }\r
 \r
        for(;;) {\r
@@ -470,9 +510,8 @@ void ListenReaderField(int limit)
                        lf_av_new= ReadAdc(ADC_CHAN_LF);\r
                        // see if there's a significant change\r
                        if(abs(lf_av - lf_av_new) > 10) {\r
-                               DbpString("LF 125/134 Field Change:");\r
-                               DbpIntegers(lf_av,lf_av_new,lf_count);\r
-                               lf_av= lf_av_new;\r
+                               Dbprintf("LF 125/134 Field Change: %x %x %x", lf_av, lf_av_new, lf_count);\r
+                               lf_av = lf_av_new;\r
                                if (lf_av > lf_max)\r
                                        lf_max = lf_av;\r
                                lf_count= 0;\r
@@ -489,9 +528,8 @@ void ListenReaderField(int limit)
                        hf_av_new= ReadAdc(ADC_CHAN_HF);\r
                        // see if there's a significant change\r
                        if(abs(hf_av - hf_av_new) > 10) {\r
-                               DbpString("HF 13.56 Field Change:");\r
-                               DbpIntegers(hf_av,hf_av_new,hf_count);\r
-                               hf_av= hf_av_new;\r
+                               Dbprintf("HF 13.56 Field Change: %x %x %x", hf_av, hf_av_new, hf_count);\r
+                               hf_av = hf_av_new;\r
                                if (hf_av > hf_max)\r
                                        hf_max = hf_av;\r
                                hf_count= 0;\r
@@ -530,79 +568,127 @@ void ListenReaderField(int limit)
 void UsbPacketReceived(BYTE *packet, int len)\r
 {\r
        UsbCommand *c = (UsbCommand *)packet;\r
+       UsbCommand ack;\r
+       ack.cmd = CMD_ACK;\r
 \r
        switch(c->cmd) {\r
+#ifdef WITH_LF\r
                case CMD_ACQUIRE_RAW_ADC_SAMPLES_125K:\r
-                       AcquireRawAdcSamples125k(c->ext1);\r
+                       AcquireRawAdcSamples125k(c->arg[0]);\r
+                       UsbSendPacket((BYTE*)&ack, sizeof(ack));\r
                        break;\r
+#endif\r
 \r
+#ifdef WITH_LF\r
                case CMD_MOD_THEN_ACQUIRE_RAW_ADC_SAMPLES_125K:\r
-                       ModThenAcquireRawAdcSamples125k(c->ext1,c->ext2,c->ext3,c->d.asBytes);\r
+                       ModThenAcquireRawAdcSamples125k(c->arg[0],c->arg[1],c->arg[2],c->d.asBytes);\r
                        break;\r
+#endif\r
 \r
+#ifdef WITH_ISO15693\r
                case CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_15693:\r
                        AcquireRawAdcSamplesIso15693();\r
                        break;\r
+#endif\r
 \r
                case CMD_BUFF_CLEAR:\r
                        BufferClear();\r
                        break;\r
 \r
+#ifdef WITH_ISO15693\r
                case CMD_READER_ISO_15693:\r
-                       ReaderIso15693(c->ext1);\r
+                       ReaderIso15693(c->arg[0]);\r
                        break;\r
+#endif\r
+\r
+               case CMD_READER_LEGIC_RF:\r
+                       LegicRfReader(c->arg[0], c->arg[1]);\r
+                       break;
 \r
+#ifdef WITH_ISO15693\r
                case CMD_SIMTAG_ISO_15693:\r
-                       SimTagIso15693(c->ext1);\r
+                       SimTagIso15693(c->arg[0]);\r
                        break;\r
+#endif\r
 \r
+#ifdef WITH_ISO14443b\r
                case CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_14443:\r
-                       AcquireRawAdcSamplesIso14443(c->ext1);\r
+                       AcquireRawAdcSamplesIso14443(c->arg[0]);\r
                        break;\r
+#endif\r
 \r
+#ifdef WITH_ISO14443b\r
                case CMD_READ_SRI512_TAG:\r
-                       ReadSRI512Iso14443(c->ext1);\r
+                       ReadSRI512Iso14443(c->arg[0]);\r
                        break;\r
+               case CMD_READ_SRIX4K_TAG:\r
+                       ReadSRIX4KIso14443(c->arg[0]);\r
+                       break;\r
+#endif\r
 \r
+#ifdef WITH_ISO14443a\r
                case CMD_READER_ISO_14443a:\r
-                       ReaderIso14443a(c->ext1);\r
+                       ReaderIso14443a(c->arg[0]);\r
                        break;\r
+#endif\r
 \r
+#ifdef WITH_ISO14443a\r
+               case CMD_READER_MIFARE:\r
+                       ReaderMifare(c->arg[0]);\r
+                       break;\r
+#endif\r
+      \r
+#ifdef WITH_ISO14443b\r
                case CMD_SNOOP_ISO_14443:\r
                        SnoopIso14443();\r
                        break;\r
+#endif\r
 \r
+#ifdef WITH_ISO14443a\r
                case CMD_SNOOP_ISO_14443a:\r
                        SnoopIso14443a();\r
                        break;\r
+#endif\r
 \r
                case CMD_SIMULATE_TAG_HF_LISTEN:\r
                        SimulateTagHfListen();\r
                        break;\r
 \r
+#ifdef WITH_ISO14443b\r
                case CMD_SIMULATE_TAG_ISO_14443:\r
                        SimulateIso14443Tag();\r
                        break;\r
-\r
+#endif\r
+               \r
+#ifdef WITH_ISO14443a\r
                case CMD_SIMULATE_TAG_ISO_14443a:\r
-                       SimulateIso14443aTag(c->ext1, c->ext2);  // ## Simulate iso14443a tag - pass tag type & UID\r
+                       SimulateIso14443aTag(c->arg[0], c->arg[1]);  // ## Simulate iso14443a tag - pass tag type & UID\r
                        break;\r
+#endif\r
 \r
                case CMD_MEASURE_ANTENNA_TUNING:\r
                        MeasureAntennaTuning();\r
                        break;\r
 \r
+               case CMD_MEASURE_ANTENNA_TUNING_HF:\r
+                       MeasureAntennaTuningHf();\r
+                       break;\r
+\r
                case CMD_LISTEN_READER_FIELD:\r
-                       ListenReaderField(c->ext1);\r
+                       ListenReaderField(c->arg[0]);\r
                        break;\r
 \r
+#ifdef WITH_LF\r
                case CMD_HID_DEMOD_FSK:\r
                        CmdHIDdemodFSK(0, 0, 0, 1);                             // Demodulate HID tag\r
                        break;\r
+#endif\r
 \r
+#ifdef WITH_LF\r
                case CMD_HID_SIM_TAG:\r
-                       CmdHIDsimTAG(c->ext1, c->ext2, 1);                                      // Simulate HID tag by ID\r
+                       CmdHIDsimTAG(c->arg[0], c->arg[1], 1);                                  // Simulate HID tag by ID\r
                        break;\r
+#endif\r
 \r
                case CMD_FPGA_MAJOR_MODE_OFF:           // ## FPGA Control\r
                        FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
@@ -610,13 +696,17 @@ void UsbPacketReceived(BYTE *packet, int len)
                        LED_D_OFF(); // LED D indicates field ON or OFF\r
                        break;\r
 \r
+#ifdef WITH_LF\r
                case CMD_READ_TI_TYPE:\r
                        ReadTItag();\r
                        break;\r
+#endif\r
 \r
+#ifdef WITH_LF\r
                case CMD_WRITE_TI_TYPE:\r
-                       WriteTItag(c->ext1,c->ext2,c->ext3);\r
+                       WriteTItag(c->arg[0],c->arg[1],c->arg[2]);\r
                        break;\r
+#endif\r
 \r
                case CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K: {\r
                        UsbCommand n;\r
@@ -625,47 +715,61 @@ void UsbPacketReceived(BYTE *packet, int len)
                        } else {\r
                                n.cmd = CMD_DOWNLOADED_RAW_BITS_TI_TYPE;\r
                        }\r
-                       n.ext1 = c->ext1;\r
-                       memcpy(n.d.asDwords, BigBuf+c->ext1, 12*sizeof(DWORD));\r
+                       n.arg[0] = c->arg[0];\r
+                       memcpy(n.d.asDwords, BigBuf+c->arg[0], 12*sizeof(DWORD));\r
                        UsbSendPacket((BYTE *)&n, sizeof(n));\r
                        break;\r
                }\r
+\r
                case CMD_DOWNLOADED_SIM_SAMPLES_125K: {\r
                        BYTE *b = (BYTE *)BigBuf;\r
-                       memcpy(b+c->ext1, c->d.asBytes, 48);\r
+                       memcpy(b+c->arg[0], c->d.asBytes, 48);\r
+                       //Dbprintf("copied 48 bytes to %i",b+c->arg[0]);\r
+                       UsbSendPacket((BYTE*)&ack, sizeof(ack));\r
                        break;\r
                }\r
+\r
+#ifdef WITH_LF\r
                case CMD_SIMULATE_TAG_125K:\r
                        LED_A_ON();\r
-                       SimulateTagLowFrequency(c->ext1, 1);\r
+                       SimulateTagLowFrequency(c->arg[0], c->arg[1], 1);\r
                        LED_A_OFF();\r
                        break;\r
+#endif\r
+\r
                case CMD_READ_MEM:\r
-                       ReadMem(c->ext1);\r
+                       ReadMem(c->arg[0]);\r
                        break;\r
+\r
                case CMD_SET_LF_DIVISOR:\r
-                       FpgaSendCommand(FPGA_CMD_SET_DIVISOR, c->ext1);\r
+                       FpgaSendCommand(FPGA_CMD_SET_DIVISOR, c->arg[0]);\r
                        break;\r
+\r
                case CMD_SET_ADC_MUX:\r
-                       switch(c->ext1) {\r
-                       case 0: SetAdcMuxFor(GPIO_MUXSEL_LOPKD); break;\r
-                       case 1: SetAdcMuxFor(GPIO_MUXSEL_LORAW); break;\r
-                       case 2: SetAdcMuxFor(GPIO_MUXSEL_HIPKD); break;\r
-                       case 3: SetAdcMuxFor(GPIO_MUXSEL_HIRAW); break;\r
+                       switch(c->arg[0]) {\r
+                               case 0: SetAdcMuxFor(GPIO_MUXSEL_LOPKD); break;\r
+                               case 1: SetAdcMuxFor(GPIO_MUXSEL_LORAW); break;\r
+                               case 2: SetAdcMuxFor(GPIO_MUXSEL_HIPKD); break;\r
+                               case 3: SetAdcMuxFor(GPIO_MUXSEL_HIRAW); break;\r
                        }\r
                        break;\r
+\r
                case CMD_VERSION:\r
                        SendVersion();\r
                        break;\r
+\r
+#ifdef WITH_LF\r
                case CMD_LF_SIMULATE_BIDIR:\r
-                       SimulateTagLowFrequencyBidir(c->ext1, c->ext2);\r
+                       SimulateTagLowFrequencyBidir(c->arg[0], c->arg[1]);\r
                        break;\r
+#endif\r
+\r
 #ifdef WITH_LCD\r
                case CMD_LCD_RESET:\r
                        LCDReset();\r
                        break;\r
                case CMD_LCD:\r
-                       LCDSend(c->ext1);\r
+                       LCDSend(c->arg[0]);\r
                        break;\r
 #endif\r
                case CMD_SETUP_WRITE:\r
@@ -679,6 +783,7 @@ void UsbPacketReceived(BYTE *packet, int len)
                                // We're going to reset, and the bootrom will take control.\r
                        }\r
                        break;\r
+\r
                case CMD_START_FLASH:\r
                        if(common_area.flags.bootrom_present) {\r
                                common_area.command = COMMON_AREA_COMMAND_ENTER_FLASH_MODE;\r
@@ -691,13 +796,13 @@ void UsbPacketReceived(BYTE *packet, int len)
                case CMD_DEVICE_INFO: {\r
                        UsbCommand c;\r
                        c.cmd = CMD_DEVICE_INFO;\r
-                       c.ext1 = DEVICE_INFO_FLAG_OSIMAGE_PRESENT | DEVICE_INFO_FLAG_CURRENT_MODE_OS;\r
-                       if(common_area.flags.bootrom_present) c.ext1 |= DEVICE_INFO_FLAG_BOOTROM_PRESENT;\r
+                       c.arg[0] = DEVICE_INFO_FLAG_OSIMAGE_PRESENT | DEVICE_INFO_FLAG_CURRENT_MODE_OS;\r
+                       if(common_area.flags.bootrom_present) c.arg[0] |= DEVICE_INFO_FLAG_BOOTROM_PRESENT;\r
                        UsbSendPacket((BYTE*)&c, sizeof(c));\r
                }\r
                        break;\r
                default:\r
-                       DbpString("unknown command");\r
+                       Dbprintf("%s: 0x%04x","unknown command:",c->cmd);\r
                        break;\r
        }\r
 }\r
@@ -768,7 +873,9 @@ void  __attribute__((noreturn)) AppMain(void)
                UsbPoll(FALSE);\r
                WDT_HIT();\r
 \r
+#ifdef WITH_LF\r
                if (BUTTON_HELD(1000) > 0)\r
                        SamyRun();\r
+#endif\r
        }\r
 }\r
Impressum, Datenschutz