X-Git-Url: http://cvs.zerfleddert.de/cgi-bin/gitweb.cgi/proxmark3-svn/blobdiff_plain/9bea179a71188589f8e642f615177a108cea8d55..ac86b656816e0368353921991e59226f38e5b698:/armsrc/appmain.c diff --git a/armsrc/appmain.c b/armsrc/appmain.c index 7dba687f..23046b4d 100644 --- a/armsrc/appmain.c +++ b/armsrc/appmain.c @@ -13,8 +13,6 @@ #include "LCD.h" #endif -int usbattached = 0; - //============================================================================= // 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 @@ -24,6 +22,7 @@ int usbattached = 0; BYTE ToSend[256]; int ToSendMax; static int ToSendBit; +struct common_area common_area __attribute__((section(".commonarea"))); void BufferClear(void) { @@ -64,8 +63,8 @@ void ToSendStuffBit(int b) void DbpString(char *str) { /* this holds up stuff unless we're connected to usb */ -// if (!usbattached) -// return; + if (!UsbConnected()) + return; UsbCommand c; c.cmd = CMD_DEBUG_PRINT_STRING; @@ -80,8 +79,8 @@ void DbpString(char *str) void DbpIntegers(int x1, int x2, int x3) { /* this holds up stuff unless we're connected to usb */ -// if (!usbattached) -// return; + if (!UsbConnected()) + return; UsbCommand c; c.cmd = CMD_DEBUG_PRINT_INTEGERS; @@ -237,6 +236,34 @@ void ReadMem(int addr) DbpIntegers(0, data[i], data[i+1]); } +/* osimage version information is linked in */ +extern struct version_information version_information; +/* bootrom version information is pointed to from _bootphase1_version_pointer */ +extern char *_bootphase1_version_pointer, _flash_start, _flash_end; +void SendVersion(void) +{ + char temp[48]; /* Limited data payload in USB packets */ + DbpString("Prox/RFID mark3 RFID instrument"); + + /* Try to find the bootrom version information. Expect to find a pointer at + * symbol _bootphase1_version_pointer, perform slight sanity checks on the + * pointer, then use it. + */ + char *bootrom_version = *(char**)&_bootphase1_version_pointer; + if( bootrom_version < &_flash_start || bootrom_version >= &_flash_end ) { + DbpString("bootrom version information appears invalid"); + } else { + FormatVersionInformation(temp, sizeof(temp), "bootrom: ", bootrom_version); + DbpString(temp); + } + + FormatVersionInformation(temp, sizeof(temp), "os: ", &version_information); + DbpString(temp); + + FpgaGatherVersion(temp, sizeof(temp)); + DbpString(temp); +} + // samy's sniff and repeat routine void SamyRun() { @@ -266,7 +293,7 @@ void SamyRun() for (;;) { - usbattached = UsbPoll(FALSE); + UsbPoll(FALSE); WDT_HIT(); // Was our button held down or pressed? @@ -373,56 +400,47 @@ your antenna. You will probably not get some good results if there is a LF and a at the same place! :-) LIGHT SCHEME USED: - -Light scheme | Descriptiong ----------------------------------------------------- - ---- | No field detected - X--- | 14% of maximum current detected - -X-- | 29% of maximum current detected - --X- | 43% of maximum current detected - ---X | 57% of maximum current detected - --XX | 71% of maximum current detected - -XXX | 86% of maximum current detected - XXXX | 100% of maximum current detected - -TODO: -Add the LF part for MODE 2 - */ +static const char LIGHT_SCHEME[] = { + 0x0, /* ---- | No field detected */ + 0x1, /* X--- | 14% of maximum current detected */ + 0x2, /* -X-- | 29% of maximum current detected */ + 0x4, /* --X- | 43% of maximum current detected */ + 0x8, /* ---X | 57% of maximum current detected */ + 0xC, /* --XX | 71% of maximum current detected */ + 0xE, /* -XXX | 86% of maximum current detected */ + 0xF, /* XXXX | 100% of maximum current detected */ +}; +static const int LIGHT_LEN = sizeof(LIGHT_SCHEME)/sizeof(LIGHT_SCHEME[0]); + void ListenReaderField(int limit) { - int lf_av, lf_av_new, lf_baseline= 0, lf_count= 0; + int lf_av, lf_av_new, lf_baseline= 0, lf_count= 0, lf_max; int hf_av, hf_av_new, hf_baseline= 0, hf_count= 0, hf_max; - int mode=1; + int mode=1, display_val, display_max, i; #define LF_ONLY 1 #define HF_ONLY 2 - LED_A_OFF(); - LED_B_OFF(); - LED_C_OFF(); - LED_D_OFF(); + LEDsoff(); - lf_av= ReadAdc(ADC_CHAN_LF); + lf_av=lf_max=ReadAdc(ADC_CHAN_LF); - if(limit != HF_ONLY) - { + if(limit != HF_ONLY) { DbpString("LF 125/134 Baseline:"); DbpIntegers(lf_av,0,0); lf_baseline= lf_av; - } + } hf_av=hf_max=ReadAdc(ADC_CHAN_HF); - if (limit != LF_ONLY) - { + if (limit != LF_ONLY) { DbpString("HF 13.56 Baseline:"); DbpIntegers(hf_av,0,0); hf_baseline= hf_av; - } + } - for(;;) - { + for(;;) { if (BUTTON_PRESS()) { SpinDelay(500); switch (mode) { @@ -433,85 +451,78 @@ void ListenReaderField(int limit) case 2: default: DbpString("Stopped"); - LED_A_OFF(); - LED_B_OFF(); - LED_C_OFF(); - LED_D_OFF(); + LEDsoff(); return; break; } } WDT_HIT(); - if (limit != HF_ONLY) - { - if (abs(lf_av - lf_baseline) > 10) - LED_D_ON(); - else - LED_D_OFF(); + if (limit != HF_ONLY) { + if(mode==1) { + if (abs(lf_av - lf_baseline) > 10) LED_D_ON(); + else LED_D_OFF(); + } + ++lf_count; lf_av_new= ReadAdc(ADC_CHAN_LF); // see if there's a significant change - if(abs(lf_av - lf_av_new) > 10) - { + if(abs(lf_av - lf_av_new) > 10) { DbpString("LF 125/134 Field Change:"); DbpIntegers(lf_av,lf_av_new,lf_count); lf_av= lf_av_new; + if (lf_av > lf_max) + lf_max = lf_av; lf_count= 0; - } } + } - if (limit != LF_ONLY) - { - if (abs(hf_av - hf_baseline) > 10) { - if (mode == 1) - LED_B_ON(); - if (mode == 2) { - if ( hf_av>(hf_max/7)*6) { - LED_A_ON(); LED_B_ON(); LED_C_ON(); LED_D_ON(); - } - if ( (hf_av>(hf_max/7)*5) && (hf_av<=(hf_max/7)*6) ) { - LED_A_ON(); LED_B_ON(); LED_C_OFF(); LED_D_ON(); - } - if ( (hf_av>(hf_max/7)*4) && (hf_av<=(hf_max/7)*5) ) { - LED_A_OFF(); LED_B_ON(); LED_C_OFF(); LED_D_ON(); - } - if ( (hf_av>(hf_max/7)*3) && (hf_av<=(hf_max/7)*4) ) { - LED_A_OFF(); LED_B_OFF(); LED_C_OFF(); LED_D_ON(); - } - if ( (hf_av>(hf_max/7)*2) && (hf_av<=(hf_max/7)*3) ) { - LED_A_OFF(); LED_B_ON(); LED_C_OFF(); LED_D_OFF(); - } - if ( (hf_av>(hf_max/7)*1) && (hf_av<=(hf_max/7)*2) ) { - LED_A_ON(); LED_B_OFF(); LED_C_OFF(); LED_D_OFF(); - } - if ( (hf_av>(hf_max/7)*0) && (hf_av<=(hf_max/7)*1) ) { - LED_A_OFF(); LED_B_OFF(); LED_C_ON(); LED_D_OFF(); - } - } - } else { - if (mode == 1) { - LED_B_OFF(); - } - if (mode == 2) { - LED_A_OFF(); LED_B_OFF(); LED_C_OFF(); LED_D_OFF(); - } + if (limit != LF_ONLY) { + if (mode == 1){ + if (abs(hf_av - hf_baseline) > 10) LED_B_ON(); + else LED_B_OFF(); } - + ++hf_count; hf_av_new= ReadAdc(ADC_CHAN_HF); // see if there's a significant change - if(abs(hf_av - hf_av_new) > 10) - { + if(abs(hf_av - hf_av_new) > 10) { DbpString("HF 13.56 Field Change:"); DbpIntegers(hf_av,hf_av_new,hf_count); hf_av= hf_av_new; if (hf_av > hf_max) hf_max = hf_av; hf_count= 0; + } + } + + if(mode == 2) { + if (limit == LF_ONLY) { + display_val = lf_av; + display_max = lf_max; + } else if (limit == HF_ONLY) { + display_val = hf_av; + display_max = hf_max; + } else { /* Pick one at random */ + if( (hf_max - hf_baseline) > (lf_max - lf_baseline) ) { + display_val = hf_av; + display_max = hf_max; + } else { + display_val = lf_av; + display_max = lf_max; + } + } + for (i=0; i= ((display_max/LIGHT_LEN)*i) && display_val <= ((display_max/LIGHT_LEN)*(i+1))) { + if (LIGHT_SCHEME[i] & 0x1) LED_C_ON(); else LED_C_OFF(); + if (LIGHT_SCHEME[i] & 0x2) LED_A_ON(); else LED_A_OFF(); + if (LIGHT_SCHEME[i] & 0x4) LED_B_ON(); else LED_B_OFF(); + if (LIGHT_SCHEME[i] & 0x8) LED_D_ON(); else LED_D_OFF(); + break; } } } + } } void UsbPacketReceived(BYTE *packet, int len) @@ -597,10 +608,6 @@ void UsbPacketReceived(BYTE *packet, int len) LED_D_OFF(); // LED D indicates field ON or OFF break; - case CMD_ACQUIRE_RAW_BITS_TI_TYPE: - AcquireRawBitsTI(); - break; - case CMD_READ_TI_TYPE: ReadTItag(); break; @@ -609,8 +616,7 @@ void UsbPacketReceived(BYTE *packet, int len) WriteTItag(c->ext1,c->ext2,c->ext3); break; - case CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K: - case CMD_DOWNLOAD_RAW_BITS_TI_TYPE: { + case CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K: { UsbCommand n; if(c->cmd == CMD_DOWNLOAD_RAW_ADC_SAMPLES_125K) { n.cmd = CMD_DOWNLOADED_RAW_ADC_SAMPLES_125K; @@ -632,18 +638,22 @@ void UsbPacketReceived(BYTE *packet, int len) SimulateTagLowFrequency(c->ext1, 1); LED_A_OFF(); break; -#ifdef WITH_LCD - case CMD_LCD_RESET: - LCDReset(); - break; -#endif case CMD_READ_MEM: ReadMem(c->ext1); break; case CMD_SET_LF_DIVISOR: FpgaSendCommand(FPGA_CMD_SET_DIVISOR, c->ext1); break; + case CMD_VERSION: + SendVersion(); + break; + case CMD_LF_SIMULATE_BIDIR: + SimulateTagLowFrequencyBidir(c->ext1, c->ext2); + break; #ifdef WITH_LCD + case CMD_LCD_RESET: + LCDReset(); + break; case CMD_LCD: LCDSend(c->ext1); break; @@ -659,18 +669,40 @@ void UsbPacketReceived(BYTE *packet, int len) // We're going to reset, and the bootrom will take control. } break; - - + case CMD_START_FLASH: + if(common_area.flags.bootrom_present) { + common_area.command = COMMON_AREA_COMMAND_ENTER_FLASH_MODE; + } + USB_D_PLUS_PULLUP_OFF(); + RSTC_CONTROL = RST_CONTROL_KEY | RST_CONTROL_PROCESSOR_RESET; + for(;;); + break; + + case CMD_DEVICE_INFO: { + UsbCommand c; + c.cmd = CMD_DEVICE_INFO; + c.ext1 = DEVICE_INFO_FLAG_OSIMAGE_PRESENT | DEVICE_INFO_FLAG_CURRENT_MODE_OS; + if(common_area.flags.bootrom_present) c.ext1 |= DEVICE_INFO_FLAG_BOOTROM_PRESENT; + UsbSendPacket((BYTE*)&c, sizeof(c)); + } + break; default: DbpString("unknown command"); break; } } -void AppMain(void) +void __attribute__((noreturn)) AppMain(void) { - memset(BigBuf,0,sizeof(BigBuf)); SpinDelay(100); + + if(common_area.magic != COMMON_AREA_MAGIC || common_area.version != 1) { + /* Initialize common area */ + memset(&common_area, 0, sizeof(common_area)); + common_area.magic = COMMON_AREA_MAGIC; + common_area.version = 1; + } + common_area.flags.osimage_present = 1; LED_D_OFF(); LED_C_OFF(); @@ -701,14 +733,14 @@ void AppMain(void) LCDInit(); // test text on different colored backgrounds - LCDString(" The quick brown fox ", &FONT6x8,1,1+8*0,WHITE ,BLACK ); - LCDString(" jumped over the ", &FONT6x8,1,1+8*1,BLACK ,WHITE ); - LCDString(" lazy dog. ", &FONT6x8,1,1+8*2,YELLOW ,RED ); - LCDString(" AaBbCcDdEeFfGgHhIiJj ", &FONT6x8,1,1+8*3,RED ,GREEN ); - LCDString(" KkLlMmNnOoPpQqRrSsTt ", &FONT6x8,1,1+8*4,MAGENTA,BLUE ); - LCDString("UuVvWwXxYyZz0123456789", &FONT6x8,1,1+8*5,BLUE ,YELLOW); - LCDString("`-=[]_;',./~!@#$%^&*()", &FONT6x8,1,1+8*6,BLACK ,CYAN ); - LCDString(" _+{}|:\\\"<>? ",&FONT6x8,1,1+8*7,BLUE ,MAGENTA); + LCDString(" The quick brown fox ", (char *)&FONT6x8,1,1+8*0,WHITE ,BLACK ); + LCDString(" jumped over the ", (char *)&FONT6x8,1,1+8*1,BLACK ,WHITE ); + LCDString(" lazy dog. ", (char *)&FONT6x8,1,1+8*2,YELLOW ,RED ); + LCDString(" AaBbCcDdEeFfGgHhIiJj ", (char *)&FONT6x8,1,1+8*3,RED ,GREEN ); + LCDString(" KkLlMmNnOoPpQqRrSsTt ", (char *)&FONT6x8,1,1+8*4,MAGENTA,BLUE ); + LCDString("UuVvWwXxYyZz0123456789", (char *)&FONT6x8,1,1+8*5,BLUE ,YELLOW); + LCDString("`-=[]_;',./~!@#$%^&*()", (char *)&FONT6x8,1,1+8*6,BLACK ,CYAN ); + LCDString(" _+{}|:\\\"<>? ",(char *)&FONT6x8,1,1+8*7,BLUE ,MAGENTA); // color bands LCDFill(0, 1+8* 8, 132, 8, BLACK); @@ -723,7 +755,7 @@ void AppMain(void) #endif for(;;) { - usbattached = UsbPoll(FALSE); + UsbPoll(FALSE); WDT_HIT(); if (BUTTON_HELD(1000) > 0)