From 5ea8f735472f4f9be1b8403f3c4ea2e554a43900 Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Thu, 11 Jan 2018 21:47:27 +0100 Subject: [PATCH] FIX: cmd_send has wrong varible definitions, leading to loss of values. --- armsrc/appmain.c | 41 ++++++++++++---------- armsrc/apps.h | 2 +- client/cmddata.c | 88 ++++++++++++++++++++++++++++-------------------- common/cmd.c | 2 +- common/cmd.h | 2 +- 5 files changed, 78 insertions(+), 57 deletions(-) diff --git a/armsrc/appmain.c b/armsrc/appmain.c index e65509f7c..33feb430a 100644 --- a/armsrc/appmain.c +++ b/armsrc/appmain.c @@ -110,7 +110,6 @@ void DbprintfEx(uint32_t cmd, const char *fmt, ...) { // should probably limit size here; oh well, let's just use a big buffer char output_string[128] = {0x00}; va_list ap; - va_start(ap, fmt); kvsprintf(fmt, output_string, 10, ap); va_end(ap); @@ -167,21 +166,19 @@ void Dbhexdump(int len, uint8_t *d, bool bAsci) { static int ReadAdc(int ch) { // Note: ADC_MODE_PRESCALE and ADC_MODE_SAMPLE_HOLD_TIME are set to the maximum allowed value. - // Both AMPL_LO and AMPL_HI are very high impedance (10MOhm) outputs, the input capacitance of the ADC is 12pF (typical). This results in a time constant - // of RC = 10MOhm * 12pF = 120us. Even after the maximum configurable sample&hold time of 40us the input capacitor will not be fully charged. + // AMPL_HI is are high impedance (10MOhm || 1MOhm) output, the input capacitance of the ADC is 12pF (typical). This results in a time constant + // of RC = (0.91MOhm) * 12pF = 10.9us. Even after the maximum configurable sample&hold time of 40us the input capacitor will not be fully charged. // // The maths are: // If there is a voltage v_in at the input, the voltage v_cap at the capacitor (this is what we are measuring) will be // - // v_cap = v_in * (1 - exp(-RC/SHTIM)) = v_in * (1 - exp(-3)) = v_in * 0,95 (i.e. an error of 5%) - // - // Note: with the "historic" values in the comments above, the error was 34% !!! + // v_cap = v_in * (1 - exp(-SHTIM/RC)) = v_in * (1 - exp(-40us/10.9us)) = v_in * 0,97 (i.e. an error of 3%) AT91C_BASE_ADC->ADC_CR = AT91C_ADC_SWRST; AT91C_BASE_ADC->ADC_MR = - ADC_MODE_PRESCALE(63) // [was 32] ADC_CLK = MCK / ((63+1) * 2) = 48MHz / 128 = 375kHz - | ADC_MODE_STARTUP_TIME(1) // [was 16] Startup Time = (1+1) * 8 / ADC_CLK = 16 / 375kHz = 42,7us Note: must be > 20us - | ADC_MODE_SAMPLE_HOLD_TIME(15); // [was 8] Sample & Hold Time SHTIM = 15 / ADC_CLK = 15 / 375kHz = 40us + ADC_MODE_PRESCALE(63) // ADC_CLK = MCK / ((63+1) * 2) = 48MHz / 128 = 375kHz + | ADC_MODE_STARTUP_TIME(1) // Startup Time = (1+1) * 8 / ADC_CLK = 16 / 375kHz = 42,7us Note: must be > 20us + | ADC_MODE_SAMPLE_HOLD_TIME(15); // Sample & Hold Time SHTIM = 15 / ADC_CLK = 15 / 375kHz = 40us AT91C_BASE_ADC->ADC_CHER = ADC_CHANNEL(ch); AT91C_BASE_ADC->ADC_CR = AT91C_ADC_START; @@ -205,7 +202,7 @@ void MeasureAntennaTuning(void) { uint8_t LF_Results[256]; uint32_t i, adcval = 0, peak = 0, peakv = 0, peakf = 0; - uint32_t vLf125 = 0, vLf134 = 0, vHf = 0; // in mV + uint32_t v_lf125 = 0, v_lf134 = 0, v_hf = 0; // in mV memset(LF_Results, 0, sizeof(LF_Results)); LED_B_ON(); @@ -229,26 +226,34 @@ void MeasureAntennaTuning(void) { SpinDelay(20); adcval = ((MAX_ADC_LF_VOLTAGE * AvgAdc(ADC_CHAN_LF)) >> 10); if (i == 95) - vLf125 = adcval; // voltage at 125Khz + v_lf125 = adcval; // voltage at 125Khz if (i == 89) - vLf134 = adcval; // voltage at 134Khz + v_lf134 = adcval; // voltage at 134Khz LF_Results[i] = adcval >> 9; // scale int to fit in byte for graphing purposes if(LF_Results[i] > peak) { peakv = adcval; - peak = LF_Results[i]; peakf = i; + peak = LF_Results[i]; } - } - + } + LED_A_ON(); // Let the FPGA drive the high-frequency antenna around 13.56 MHz. FpgaDownloadAndGo(FPGA_BITSTREAM_HF); FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER_RX_XCORR); SpinDelay(20); - vHf = (MAX_ADC_HF_VOLTAGE * AvgAdc(ADC_CHAN_HF)) >> 10; + v_hf = (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); + uint64_t arg0 = v_lf134; + arg0 <<= 32; + arg0 |= v_lf125; + + uint64_t arg2 = peakv; + arg2 <<= 32; + arg2 |= peakf; + + cmd_send(CMD_MEASURED_ANTENNA_TUNING, arg0, v_hf, arg2, LF_Results, 256); FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF); LEDsoff(); } @@ -262,7 +267,7 @@ void MeasureAntennaTuningHf(void) { while( !BUTTON_PRESS() ){ SpinDelay(20); vHf = (MAX_ADC_HF_VOLTAGE * AvgAdc(ADC_CHAN_HF)) >> 10; - DbprintfEx(CMD_MEASURE_ANTENNA_TUNING_HF, "%u mV",vHf); + DbprintfEx(CMD_MEASURE_ANTENNA_TUNING_HF, "%u mV / %5.2f V", vHf, vHf/1000.0); } FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF); DbpString("cancelled"); diff --git a/armsrc/apps.h b/armsrc/apps.h index 56f87d1ba..a966f4bcf 100644 --- a/armsrc/apps.h +++ b/armsrc/apps.h @@ -230,7 +230,7 @@ void check_challenges(bool file_given, byte_t* data); // cmd.h bool cmd_receive(UsbCommand* cmd); -bool cmd_send(uint32_t cmd, uint32_t arg0, uint32_t arg1, uint32_t arg2, void* data, size_t len); +bool cmd_send(uint64_t cmd, uint64_t arg0, uint64_t arg1, uint64_t arg2, void* data, size_t len); // util.h void HfSnoop(int , int); diff --git a/client/cmddata.c b/client/cmddata.c index 70cc741c8..a07015b7a 100644 --- a/client/cmddata.c +++ b/client/cmddata.c @@ -1397,11 +1397,11 @@ int CmdSamples(const char *Cmd) } int CmdTuneSamples(const char *Cmd) { -#define NON_VOLTAGE 999 -#define LF_UNUSABLE_V 2948 // was 2000. Changed due to bugfix in voltage measurements. LF results are now 47% higher. -#define LF_MARGINAL_V 14739 // was 10000. Changed due to bugfix bug in voltage measurements. LF results are now 47% higher. -#define HF_UNUSABLE_V 3167 // was 2000. Changed due to bugfix in voltage measurements. HF results are now 58% higher. -#define HF_MARGINAL_V 7917 // was 5000. Changed due to bugfix in voltage measurements. HF results are now 58% higher. +#define NON_VOLTAGE 1000 +#define LF_UNUSABLE_V 2000 +#define LF_MARGINAL_V 10000 +#define HF_UNUSABLE_V 3000 +#define HF_MARGINAL_V 5000 int timeout = 0; printf("\n[+] measuring antenna characteristics, please wait..."); @@ -1410,7 +1410,7 @@ int CmdTuneSamples(const char *Cmd) { clearCommandBuffer(); SendCommand(&c); UsbCommand resp; - while(!WaitForResponseTimeout(CMD_MEASURED_ANTENNA_TUNING, &resp, 2000)) { + while (!WaitForResponseTimeout(CMD_MEASURED_ANTENNA_TUNING, &resp, 2000)) { timeout++; printf("."); fflush(stdout); if (timeout > 7) { @@ -1418,46 +1418,62 @@ int CmdTuneSamples(const char *Cmd) { return 1; } } - - uint32_t vLf125 = resp.arg[0] & 0xffff; - uint32_t vLf134 = resp.arg[0] >> 16; + printf("\n"); - uint32_t vHf = resp.arg[1] & 0xffff;; - uint32_t peakf = resp.arg[2] & 0xffff; - uint32_t peakv = resp.arg[2] >> 16; + uint32_t v_lf125 = resp.arg[0]; + uint32_t v_lf134 = resp.arg[0] >> 32; - PrintAndLog("\n"); + uint32_t v_hf = resp.arg[1]; + uint32_t peakf = resp.arg[2]; + uint32_t peakv = resp.arg[2] >> 32; - if ( vLf125 > NON_VOLTAGE ) - PrintAndLog("[+] LF antenna: %5.2f V - 125.00 kHz", vLf125/1000.0); - if ( vLf134 > NON_VOLTAGE ) - PrintAndLog("[+] LF antenna: %5.2f V - 134.00 kHz", vLf134/1000.0); + if ( v_lf125 > NON_VOLTAGE ) + PrintAndLog("[+] LF antenna: %5.2f V - 125.00 kHz", v_lf125/1000.0); + if ( v_lf134 > NON_VOLTAGE ) + PrintAndLog("[+] LF antenna: %5.2f V - 134.00 kHz", v_lf134/1000.0); if ( peakv > NON_VOLTAGE && peakf > 0 ) PrintAndLog("[+] LF optimal: %5.2f V - %6.2f kHz", peakv/1000.0, 12000.0/(peakf+1)); - // LF judgement - if (peakv < LF_UNUSABLE_V) PrintAndLog("[!] LF antenna is unusable"); - else if (peakv < LF_MARGINAL_V) PrintAndLog("[!] LF antenna is marginal"); - else PrintAndLog("[+] LF antenna is ok"); + char judgement[10]; + memset(judgement, 0, sizeof(judgement)); + // LF evaluation + if (peakv < LF_UNUSABLE_V) + sprintf(judgement, "UNUSABLE"); + else if (peakv < LF_MARGINAL_V) + sprintf(judgement, "MARGINAL"); + else + sprintf(judgement, "OK"); - PrintAndLog(""); - if ( vHf > NON_VOLTAGE ) - PrintAndLog("[+] HF antenna: %5.2f V - 13.56 MHz", vHf/1000.0); + PrintAndLog("[%c] LF antenna is %s \n" + , (peakv < LF_UNUSABLE_V) ? '!' : '+' + , judgement + ); + + // HF evaluation + if ( v_hf > NON_VOLTAGE ) + PrintAndLog("[+] HF antenna: %5.2f V - 13.56 MHz %s", v_hf/1000.0, judgement); - // HF judgement - if (vHf < HF_UNUSABLE_V) PrintAndLog("[!] HF antenna is unusable"); - else if (vHf < HF_MARGINAL_V) PrintAndLog("[!] HF antenna is marginal"); - else PrintAndLog("[+] HF antenna is ok"); + memset(judgement, 0, sizeof(judgement)); + + if (v_hf < HF_UNUSABLE_V) + sprintf(judgement, "UNUSABLE"); + else if (v_hf < HF_MARGINAL_V) + sprintf(judgement, "MARGINAL"); + else + sprintf(judgement, "OK"); + PrintAndLog("[%c] HF antenna is %s" + , (v_hf < HF_UNUSABLE_V) ? '!' : '+' + , judgement + ); - if (peakv >= LF_UNUSABLE_V) { - for (int i = 0; i < 256; i++) { - GraphBuffer[i] = resp.d.asBytes[i] - 128; - } - PrintAndLog("\n[+] Displaying LF tuning graph. Divisor 89 is 134khz, 95 is 125khz.\n\n"); - GraphTraceLen = 256; - ShowGraphWindow(); - RepaintGraphWindow(); + // graph LF measurements + for (int i = 0; i < 256; i++) { + GraphBuffer[i] = resp.d.asBytes[i] - 128; } + PrintAndLog("\n[+] Displaying LF tuning graph. Divisor 89 is 134khz, 95 is 125khz.\n\n"); + GraphTraceLen = 256; + ShowGraphWindow(); + RepaintGraphWindow(); return 0; } diff --git a/common/cmd.c b/common/cmd.c index a66a1f66e..85dd97f9d 100644 --- a/common/cmd.c +++ b/common/cmd.c @@ -50,7 +50,7 @@ bool cmd_receive(UsbCommand* cmd) { return (rxlen); } -bool cmd_send(uint32_t cmd, uint32_t arg0, uint32_t arg1, uint32_t arg2, void* data, size_t len) { +bool cmd_send(uint64_t cmd, uint64_t arg0, uint64_t arg1, uint64_t arg2, void* data, size_t len) { UsbCommand txcmd; diff --git a/common/cmd.h b/common/cmd.h index 2d25c5b24..2caa71964 100644 --- a/common/cmd.h +++ b/common/cmd.h @@ -40,7 +40,7 @@ #include "string.h" bool cmd_receive(UsbCommand* cmd); -bool cmd_send(uint32_t cmd, uint32_t arg0, uint32_t arg1, uint32_t arg2, void* data, size_t len); +bool cmd_send(uint64_t cmd, uint64_t arg0, uint64_t arg1, uint64_t arg2, void* data, size_t len); #endif // _PROXMARK_CMD_H_