1 //-----------------------------------------------------------------------------
2 // Jonathan Westhues, Mar 2006
3 // Edits by Gerhard de Koning Gans, Sep 2007 (##)
5 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
6 // at your option, any later version. See the LICENSE.txt file for the text of
8 //-----------------------------------------------------------------------------
9 // The main application code. This is the first thing called after start.c
11 //-----------------------------------------------------------------------------
16 #include "proxmark3_arm.h"
21 #include "fpgaloader.h"
26 #include "iso14443a.h"
27 #include "iso14443b.h"
36 #include "legicrfsim.h"
37 //#include "cryptorfsim.h"
41 #include "lfsampling.h"
42 #include "mifarecmd.h"
43 #include "mifaredesfire.h"
44 #include "mifaresim.h"
46 #include "Standalone/standalone.h"
49 #include "commonutil.h"
54 #include "LCD_disabled.h"
70 int DBGLEVEL
= DBG_ERROR
;
71 uint8_t g_trigger
= 0;
72 bool g_hf_field_active
= false;
73 extern uint32_t _stack_start
[], _stack_end
[];
74 struct common_area common_area
__attribute__((section(".commonarea")));
75 static int button_status
= BUTTON_NO_CLICK
;
76 static bool allow_send_wtx
= false;
77 uint16_t tearoff_delay_us
= 0;
78 bool tearoff_enabled
= false;
80 int tearoff_hook(void) {
81 if (tearoff_enabled
) {
82 if (tearoff_delay_us
== 0) {
83 Dbprintf(_RED_("No tear-off delay configured!"));
84 return PM3_SUCCESS
; // SUCCESS = the hook didn't do anything
86 SpinDelayUsPrecision(tearoff_delay_us
);
87 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
88 tearoff_enabled
= false;
89 Dbprintf(_YELLOW_("Tear-off triggered!"));
92 return PM3_SUCCESS
; // SUCCESS = the hook didn't do anything
96 void hf_field_off(void) {
97 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
99 g_hf_field_active
= false;
102 void send_wtx(uint16_t wtx
) {
103 if (allow_send_wtx
) {
104 reply_ng(CMD_WTX
, PM3_SUCCESS
, (uint8_t *)&wtx
, sizeof(wtx
));
108 //-----------------------------------------------------------------------------
109 // Read an ADC channel and block till it completes, then return the result
110 // in ADC units (0 to 1023). Also a routine to sum up a number of samples and
112 //-----------------------------------------------------------------------------
113 static uint16_t ReadAdc(int ch
) {
115 // Note: ADC_MODE_PRESCALE and ADC_MODE_SAMPLE_HOLD_TIME are set to the maximum allowed value.
116 // AMPL_HI is are high impedance (10MOhm || 1MOhm) output, the input capacitance of the ADC is 12pF (typical). This results in a time constant
117 // 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.
120 // 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
122 // 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%)
124 AT91C_BASE_ADC
->ADC_CR
= AT91C_ADC_SWRST
;
125 AT91C_BASE_ADC
->ADC_MR
=
126 ADC_MODE_PRESCALE(63) // ADC_CLK = MCK / ((63+1) * 2) = 48MHz / 128 = 375kHz
127 | ADC_MODE_STARTUP_TIME(1) // Startup Time = (1+1) * 8 / ADC_CLK = 16 / 375kHz = 42,7us Note: must be > 20us
128 | ADC_MODE_SAMPLE_HOLD_TIME(15); // Sample & Hold Time SHTIM = 15 / ADC_CLK = 15 / 375kHz = 40us
130 AT91C_BASE_ADC
->ADC_CHER
= ADC_CHANNEL(ch
);
131 AT91C_BASE_ADC
->ADC_CR
= AT91C_ADC_START
;
133 while (!(AT91C_BASE_ADC
->ADC_SR
& ADC_END_OF_CONVERSION(ch
))) {};
135 return (AT91C_BASE_ADC
->ADC_CDR
[ch
] & 0x3FF);
138 // was static - merlok
139 uint16_t AvgAdc(int ch
) {
140 return SumAdc(ch
, 32) >> 5;
143 uint16_t SumAdc(int ch
, int NbSamples
) {
145 for (uint8_t i
= 0; i
< NbSamples
; i
++)
147 return (a
+ (NbSamples
>> 1) - 1);
150 static void MeasureAntennaTuning(void) {
163 uint8_t results
[256];
166 memset(payload
.results
, 0, sizeof(payload
.results
));
168 sample_config
*sc
= getSamplingConfig();
169 payload
.divisor
= sc
->divisor
;
174 * Sweeps the useful LF range of the proxmark from
175 * 46.8kHz (divisor=255) to 600kHz (divisor=19) and
176 * read the voltage in the antenna, the result left
177 * in the buffer is a graph which should clearly show
178 * the resonating frequency of your LF antenna
179 * ( hopefully around 95 if it is tuned to 125kHz!)
182 FpgaDownloadAndGo(FPGA_BITSTREAM_LF
);
183 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
| FPGA_LF_ADC_READER_FIELD
);
186 for (uint8_t i
= 255; i
>= 19; i
--) {
188 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, i
);
190 uint32_t adcval
= ((MAX_ADC_LF_VOLTAGE
* (SumAdc(ADC_CHAN_LF
, 32) >> 1)) >> 14);
191 if (i
== LF_DIVISOR_125
)
192 payload
.v_lf125
= adcval
; // voltage at 125kHz
194 if (i
== LF_DIVISOR_134
)
195 payload
.v_lf134
= adcval
; // voltage at 134kHz
197 if (i
== sc
->divisor
)
198 payload
.v_lfconf
= adcval
; // voltage at `lf config --divisor`
200 payload
.results
[i
] = adcval
>> 9; // scale int to fit in byte for graphing purposes
202 if (payload
.results
[i
] > peak
) {
203 payload
.peak_v
= adcval
;
205 peak
= payload
.results
[i
];
210 // Let the FPGA drive the high-frequency antenna around 13.56 MHz.
211 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
212 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER
);
216 payload
.v_hf
= (MAX_ADC_HF_VOLTAGE_RDV40
* SumAdc(ADC_CHAN_HF_RDV40
, 32)) >> 15;
218 payload
.v_hf
= (MAX_ADC_HF_VOLTAGE
* SumAdc(ADC_CHAN_HF
, 32)) >> 15;
221 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
222 reply_ng(CMD_MEASURE_ANTENNA_TUNING
, PM3_SUCCESS
, (uint8_t *)&payload
, sizeof(payload
));
226 // Measure HF in milliVolt
227 static uint16_t MeasureAntennaTuningHfData(void) {
230 return (MAX_ADC_HF_VOLTAGE_RDV40
* SumAdc(ADC_CHAN_HF_RDV40
, 32)) >> 15;
232 return (MAX_ADC_HF_VOLTAGE
* SumAdc(ADC_CHAN_HF
, 32)) >> 15;
237 // Measure LF in milliVolt
238 static uint32_t MeasureAntennaTuningLfData(void) {
239 return (MAX_ADC_LF_VOLTAGE
* (SumAdc(ADC_CHAN_LF
, 32) >> 1)) >> 14;
242 void print_stack_usage(void) {
243 for (uint32_t *p
= _stack_start
; ; ++p
) {
244 if (*p
!= 0xdeadbeef) {
245 Dbprintf(" Max stack usage......... %d / %d bytes", (uint32_t)_stack_end
- (uint32_t)p
, (uint32_t)_stack_end
- (uint32_t)_stack_start
);
251 void ReadMem(int addr
) {
252 const uint8_t *data
= ((uint8_t *)addr
);
254 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]);
257 /* osimage version information is linked in, cf commonutil.h */
258 /* bootrom version information is pointed to from _bootphase1_version_pointer */
259 extern uint32_t _bootphase1_version_pointer
[], _flash_start
[], _flash_end
[], __data_src_start__
[];
260 #ifdef WITH_NO_COMPRESSION
261 extern uint32_t _bootrom_end
[], _bootrom_start
[], __os_size__
[];
263 static void SendVersion(void) {
264 char temp
[PM3_CMD_DATA_SIZE
- 12]; /* Limited data payload in USB packets */
265 char VersionString
[PM3_CMD_DATA_SIZE
- 12] = { '\0' };
267 /* Try to find the bootrom version information. Expect to find a pointer at
268 * symbol _bootphase1_version_pointer, perform slight sanity checks on the
269 * pointer, then use it.
271 // dummy casting to avoid "dereferencing type-punned pointer breaking strict-aliasing rules" errors
272 uint32_t bootrom_version_ptr
= (uint32_t)_bootphase1_version_pointer
;
273 char *bootrom_version
= *(char **)(bootrom_version_ptr
);
275 strncat(VersionString
, " [ "_YELLOW_("ARM")" ]\n", sizeof(VersionString
) - strlen(VersionString
) - 1);
277 if ((uint32_t)bootrom_version
< (uint32_t)_flash_start
|| (uint32_t)bootrom_version
>= (uint32_t)_flash_end
) {
278 strcat(VersionString
, "bootrom version information appears invalid\n");
280 FormatVersionInformation(temp
, sizeof(temp
), " bootrom: ", bootrom_version
);
281 strncat(VersionString
, temp
, sizeof(VersionString
) - strlen(VersionString
) - 1);
282 strncat(VersionString
, "\n", sizeof(VersionString
) - strlen(VersionString
) - 1);
285 FormatVersionInformation(temp
, sizeof(temp
), " os: ", &version_information
);
286 strncat(VersionString
, temp
, sizeof(VersionString
) - strlen(VersionString
) - 1);
287 strncat(VersionString
, "\n", sizeof(VersionString
) - strlen(VersionString
) - 1);
289 #if defined(__clang__)
290 strncat(VersionString
, " compiled with Clang/LLVM "__VERSION__
"\n", sizeof(VersionString
) - strlen(VersionString
) - 1);
291 #elif defined(__GNUC__) || defined(__GNUG__)
292 strncat(VersionString
, " compiled with GCC "__VERSION__
"\n", sizeof(VersionString
) - strlen(VersionString
) - 1);
295 strncat(VersionString
, "\n [ "_YELLOW_("FPGA")" ] \n ", sizeof(VersionString
) - strlen(VersionString
) - 1);
297 for (int i
= 0; i
< g_fpga_bitstream_num
; i
++) {
298 strncat(VersionString
, g_fpga_version_information
[i
], sizeof(VersionString
) - strlen(VersionString
) - 1);
299 if (i
< g_fpga_bitstream_num
- 1) {
300 strncat(VersionString
, "\n ", sizeof(VersionString
) - strlen(VersionString
) - 1);
303 #ifndef WITH_NO_COMPRESSION
304 // Send Chip ID and used flash memory
305 uint32_t text_and_rodata_section_size
= (uint32_t)__data_src_start__
- (uint32_t)_flash_start
;
306 uint32_t compressed_data_section_size
= common_area
.arg1
;
311 uint32_t section_size
;
312 uint32_t versionstr_len
;
313 char versionstr
[PM3_CMD_DATA_SIZE
- 12];
317 payload
.id
= *(AT91C_DBGU_CIDR
);
318 #ifdef WITH_NO_COMPRESSION
319 payload
.section_size
= (uint32_t)_bootrom_end
- (uint32_t)_bootrom_start
+ (uint32_t)__os_size__
;
321 payload
.section_size
= text_and_rodata_section_size
+ compressed_data_section_size
;
323 payload
.versionstr_len
= strlen(VersionString
) + 1;
324 memcpy(payload
.versionstr
, VersionString
, payload
.versionstr_len
);
326 reply_ng(CMD_VERSION
, PM3_SUCCESS
, (uint8_t *)&payload
, 12 + payload
.versionstr_len
);
329 static void TimingIntervalAcquisition(void) {
330 // trigger new acquisition by turning main oscillator off and on
331 mck_from_pll_to_slck();
332 mck_from_slck_to_pll();
333 // wait for MCFR and recompute RTMR scaler
337 static void print_debug_level(void) {
338 char dbglvlstr
[20] = {0};
341 sprintf(dbglvlstr
, "none");
344 sprintf(dbglvlstr
, "error");
347 sprintf(dbglvlstr
, "info");
350 sprintf(dbglvlstr
, "debug");
353 sprintf(dbglvlstr
, "extended");
356 Dbprintf(" Debug log level......... %d ( " _YELLOW_("%s")" )", DBGLEVEL
, dbglvlstr
);
359 // measure the Connection Speed by sending SpeedTestBufferSize bytes to client and measuring the elapsed time.
360 // Note: this mimics GetFromBigbuf(), i.e. we have the overhead of the PacketCommandNG structure included.
361 static void printConnSpeed(void) {
362 DbpString(_CYAN_("Transfer Speed"));
363 Dbprintf(" Sending packets to client...");
365 #define CONN_SPEED_TEST_MIN_TIME 500 // in milliseconds
366 uint8_t *test_data
= BigBuf_get_addr();
367 uint32_t start_time
= GetTickCount();
368 uint32_t delta_time
= 0;
369 uint32_t bytes_transferred
= 0;
373 while (delta_time
< CONN_SPEED_TEST_MIN_TIME
) {
374 reply_ng(CMD_DOWNLOADED_BIGBUF
, PM3_SUCCESS
, test_data
, PM3_CMD_DATA_SIZE
);
375 bytes_transferred
+= PM3_CMD_DATA_SIZE
;
376 delta_time
= GetTickCountDelta(start_time
);
380 Dbprintf(" Time elapsed................... %dms", delta_time
);
381 Dbprintf(" Bytes transferred.............. %d", bytes_transferred
);
382 Dbprintf(" Transfer Speed PM3 -> Client... " _YELLOW_("%d") " bytes/s", 1000 * bytes_transferred
/ delta_time
);
386 * Prints runtime information about the PM3.
388 static void SendStatus(void) {
389 BigBuf_print_status();
392 Flashmem_print_status();
394 #ifdef WITH_SMARTCARD
398 printLFConfig(); // LF Sampling config
399 printT55xxConfig(); // LF T55XX Config
401 #ifdef WITH_ISO14443a
402 printHf14aConfig(); // HF 14a config
405 DbpString(_CYAN_("Various"));
410 tosend_t
*ts
= get_tosend();
411 Dbprintf(" ToSendMax............... %d", ts
->max
);
412 Dbprintf(" ToSend BUFFERSIZE....... %d", TOSEND_BUFFER_SIZE
);
413 while ((AT91C_BASE_PMC
->PMC_MCFR
& AT91C_CKGR_MAINRDY
) == 0); // Wait for MAINF value to become available...
414 uint16_t mainf
= AT91C_BASE_PMC
->PMC_MCFR
& AT91C_CKGR_MAINF
; // Get # main clocks within 16 slow clocks
415 Dbprintf(" Slow clock.............. %d Hz", (16 * MAINCK
) / mainf
);
416 uint32_t delta_time
= 0;
417 uint32_t start_time
= GetTickCount();
418 #define SLCK_CHECK_MS 50
419 SpinDelay(SLCK_CHECK_MS
);
420 delta_time
= GetTickCountDelta(start_time
);
421 if ((delta_time
< SLCK_CHECK_MS
- 1) || (delta_time
> SLCK_CHECK_MS
+ 1)) {
422 // error > 2% with SLCK_CHECK_MS=50
423 Dbprintf(_RED_(" Slow Clock speed change detected, TIA needed"));
424 Dbprintf(_YELLOW_(" Slow Clock actual speed seems closer to %d kHz"),
425 (16 * MAINCK
/ 1000) / mainf
* delta_time
/ SLCK_CHECK_MS
);
427 DbpString(_CYAN_("Installed StandAlone Mode"));
431 Flashmem_print_info();
434 reply_ng(CMD_STATUS
, PM3_SUCCESS
, NULL
, 0);
437 static void SendCapabilities(void) {
438 capabilities_t capabilities
;
439 capabilities
.version
= CAPABILITIES_VERSION
;
440 capabilities
.via_fpc
= g_reply_via_fpc
;
441 capabilities
.via_usb
= g_reply_via_usb
;
442 capabilities
.bigbuf_size
= BigBuf_get_size();
443 capabilities
.baudrate
= 0; // no real baudrate for USB-CDC
444 #ifdef WITH_FPC_USART
446 capabilities
.baudrate
= g_usart_baudrate
;
450 capabilities
.compiled_with_flash
= true;
451 capabilities
.hw_available_flash
= FlashInit();
453 capabilities
.compiled_with_flash
= false;
454 capabilities
.hw_available_flash
= false;
456 #ifdef WITH_SMARTCARD
457 capabilities
.compiled_with_smartcard
= true;
459 capabilities
.hw_available_smartcard
= I2C_get_version(&maj
, &min
) == PM3_SUCCESS
;
461 capabilities
.compiled_with_smartcard
= false;
462 capabilities
.hw_available_smartcard
= false;
464 #ifdef WITH_FPC_USART
465 capabilities
.compiled_with_fpc_usart
= true;
467 capabilities
.compiled_with_fpc_usart
= false;
469 #ifdef WITH_FPC_USART_DEV
470 capabilities
.compiled_with_fpc_usart_dev
= true;
472 capabilities
.compiled_with_fpc_usart_dev
= false;
474 #ifdef WITH_FPC_USART_HOST
475 capabilities
.compiled_with_fpc_usart_host
= true;
477 capabilities
.compiled_with_fpc_usart_host
= false;
480 capabilities
.compiled_with_lf
= true;
482 capabilities
.compiled_with_lf
= false;
485 capabilities
.compiled_with_hitag
= true;
487 capabilities
.compiled_with_hitag
= false;
490 capabilities
.compiled_with_em4x50
= true;
492 capabilities
.compiled_with_em4x50
= false;
495 capabilities
.compiled_with_em4x70
= true;
497 capabilities
.compiled_with_em4x70
= false;
501 capabilities
.compiled_with_hfsniff
= true;
503 capabilities
.compiled_with_hfsniff
= false;
506 capabilities
.compiled_with_hfplot
= true;
508 capabilities
.compiled_with_hfplot
= false;
510 #ifdef WITH_ISO14443a
511 capabilities
.compiled_with_iso14443a
= true;
513 capabilities
.compiled_with_iso14443a
= false;
515 #ifdef WITH_ISO14443b
516 capabilities
.compiled_with_iso14443b
= true;
518 capabilities
.compiled_with_iso14443b
= false;
521 capabilities
.compiled_with_iso15693
= true;
523 capabilities
.compiled_with_iso15693
= false;
526 capabilities
.compiled_with_felica
= true;
528 capabilities
.compiled_with_felica
= false;
531 capabilities
.compiled_with_legicrf
= true;
533 capabilities
.compiled_with_legicrf
= false;
536 capabilities
.compiled_with_iclass
= true;
538 capabilities
.compiled_with_iclass
= false;
540 #ifdef WITH_NFCBARCODE
541 capabilities
.compiled_with_nfcbarcode
= true;
543 capabilities
.compiled_with_nfcbarcode
= false;
546 capabilities
.compiled_with_lcd
= true;
548 capabilities
.compiled_with_lcd
= false;
550 reply_ng(CMD_CAPABILITIES
, PM3_SUCCESS
, (uint8_t *)&capabilities
, sizeof(capabilities
));
553 // Show some leds in a pattern to identify StandAlone mod is running
554 void StandAloneMode(void) {
556 DbpString("Stand-alone mode, no computer necessary");
566 Listen and detect an external reader. Determine the best location
570 Inside the ListenReaderField() function, there is two mode.
571 By default, when you call the function, you will enter mode 1.
572 If you press the PM3 button one time, you will enter mode 2.
573 If you press the PM3 button a second time, you will exit the function.
575 DESCRIPTION OF MODE 1:
576 This mode just listens for an external reader field and lights up green
577 for HF and/or red for LF. This is the original mode of the detectreader
580 DESCRIPTION OF MODE 2:
581 This mode will visually represent, using the LEDs, the actual strength of the
582 current compared to the maximum current detected. Basically, once you know
583 what kind of external reader is present, it will help you spot the best location to place
584 your antenna. You will probably not get some good results if there is a LF and a HF reader
585 at the same place! :-)
587 #define LIGHT_LEVELS 20
589 void ListenReaderField(uint8_t limit
) {
592 #define REPORT_CHANGE 1000 // report new values only if they have changed at least by REPORT_CHANGE mV
594 uint16_t lf_av
= 0, lf_av_new
, lf_baseline
= 0, lf_max
= 0;
595 uint16_t hf_av
= 0, hf_av_new
, hf_baseline
= 0, hf_max
= 0;
596 uint16_t mode
= 1, display_val
, display_max
;
598 // switch off FPGA - we don't want to measure our own signal
599 // 20180315 - iceman, why load this before and then turn off?
600 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
601 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
605 if (limit
== LF_ONLY
) {
606 lf_av
= lf_max
= (MAX_ADC_LF_VOLTAGE
* SumAdc(ADC_CHAN_LF
, 32)) >> 15;
607 Dbprintf("LF 125/134kHz Baseline: %dmV", lf_av
);
611 if (limit
== HF_ONLY
) {
614 // iceman, useless, since we are measuring readerfield, not our field. My tests shows a max of 20v from a reader.
615 hf_av
= hf_max
= (MAX_ADC_HF_VOLTAGE_RDV40
* SumAdc(ADC_CHAN_HF_RDV40
, 32)) >> 15;
617 hf_av
= hf_max
= (MAX_ADC_HF_VOLTAGE
* SumAdc(ADC_CHAN_HF
, 32)) >> 15;
619 Dbprintf("HF 13.56MHz Baseline: %dmV", hf_av
);
625 // Switch modes with button
626 if (BUTTON_PRESS()) {
631 DbpString("Signal Strength Mode");
635 DbpString("Stopped");
636 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
643 if (limit
== LF_ONLY
) {
645 if (ABS(lf_av
- lf_baseline
) > REPORT_CHANGE
)
651 lf_av_new
= (MAX_ADC_LF_VOLTAGE
* SumAdc(ADC_CHAN_LF
, 32)) >> 15;
652 // see if there's a significant change
653 if (ABS(lf_av
- lf_av_new
) > REPORT_CHANGE
) {
654 Dbprintf("LF 125/134kHz Field Change: %5dmV", lf_av_new
);
661 if (limit
== HF_ONLY
) {
663 if (ABS(hf_av
- hf_baseline
) > REPORT_CHANGE
)
670 hf_av_new
= (MAX_ADC_HF_VOLTAGE_RDV40
* SumAdc(ADC_CHAN_HF_RDV40
, 32)) >> 15;
672 hf_av_new
= (MAX_ADC_HF_VOLTAGE
* SumAdc(ADC_CHAN_HF
, 32)) >> 15;
674 // see if there's a significant change
675 if (ABS(hf_av
- hf_av_new
) > REPORT_CHANGE
) {
676 Dbprintf("HF 13.56MHz Field Change: %5dmV", hf_av_new
);
684 if (limit
== LF_ONLY
) {
686 display_max
= lf_max
;
687 } else if (limit
== HF_ONLY
) {
689 display_max
= hf_max
;
690 } else { /* Pick one at random */
691 if ((hf_max
- hf_baseline
) > (lf_max
- lf_baseline
)) {
693 display_max
= hf_max
;
696 display_max
= lf_max
;
700 display_val
= display_val
* (4 * LIGHT_LEVELS
) / MAX(1, display_max
);
701 uint32_t duty_a
= MIN(MAX(display_val
, 0 * LIGHT_LEVELS
), 1 * LIGHT_LEVELS
) - 0 * LIGHT_LEVELS
;
702 uint32_t duty_b
= MIN(MAX(display_val
, 1 * LIGHT_LEVELS
), 2 * LIGHT_LEVELS
) - 1 * LIGHT_LEVELS
;
703 uint32_t duty_c
= MIN(MAX(display_val
, 2 * LIGHT_LEVELS
), 3 * LIGHT_LEVELS
) - 2 * LIGHT_LEVELS
;
704 uint32_t duty_d
= MIN(MAX(display_val
, 3 * LIGHT_LEVELS
), 4 * LIGHT_LEVELS
) - 3 * LIGHT_LEVELS
;
709 } else if (duty_a
== LIGHT_LEVELS
) {
715 SpinDelay(LIGHT_LEVELS
- duty_a
);
721 } else if (duty_b
== LIGHT_LEVELS
) {
727 SpinDelay(LIGHT_LEVELS
- duty_b
);
733 } else if (duty_c
== LIGHT_LEVELS
) {
739 SpinDelay(LIGHT_LEVELS
- duty_c
);
745 } else if (duty_d
== LIGHT_LEVELS
) {
751 SpinDelay(LIGHT_LEVELS
- duty_d
);
756 static void PacketReceived(PacketCommandNG
*packet
) {
759 Dbprintf("received NG frame with %d bytes payload, with command: 0x%04x", packet->length, cmd);
761 Dbprintf("received OLD frame of %d bytes, with command: 0x%04x and args: %d %d %d", packet->length, packet->cmd, packet->oldarg[0], packet->oldarg[1], packet->oldarg[2]);
765 switch (packet
->cmd
) {
768 case CMD_QUIT_SESSION
: {
769 g_reply_via_fpc
= false;
770 g_reply_via_usb
= false;
774 case CMD_SET_DBGMODE
: {
775 DBGLEVEL
= packet
->data
.asBytes
[0];
777 reply_ng(CMD_SET_DBGMODE
, PM3_SUCCESS
, NULL
, 0);
780 case CMD_SET_TEAROFF
: {
786 struct p
*payload
= (struct p
*)packet
->data
.asBytes
;
787 if (payload
->on
&& payload
->off
)
788 reply_ng(CMD_SET_TEAROFF
, PM3_EINVARG
, NULL
, 0);
790 tearoff_enabled
= true;
792 tearoff_enabled
= false;
793 if (payload
->delay_us
> 0)
794 tearoff_delay_us
= payload
->delay_us
;
795 reply_ng(CMD_SET_TEAROFF
, PM3_SUCCESS
, NULL
, 0);
799 case CMD_HF_DROPFIELD
: {
804 case CMD_LF_T55XX_SET_CONFIG
: {
805 setT55xxConfig(packet
->oldarg
[0], (t55xx_configurations_t
*) packet
->data
.asBytes
);
808 case CMD_LF_SAMPLING_PRINT_CONFIG
: {
812 case CMD_LF_SAMPLING_GET_CONFIG
: {
813 sample_config
*config
= getSamplingConfig();
814 reply_ng(CMD_LF_SAMPLING_GET_CONFIG
, PM3_SUCCESS
, (uint8_t *)config
, sizeof(sample_config
));
817 case CMD_LF_SAMPLING_SET_CONFIG
: {
819 memcpy(&c
, packet
->data
.asBytes
, sizeof(sample_config
));
820 setSamplingConfig(&c
);
821 // setSamplingConfig((sample_config *) packet->data.asBytes);
824 case CMD_LF_ACQ_RAW_ADC
: {
826 uint32_t samples
: 31;
829 struct p
*payload
= (struct p
*)packet
->data
.asBytes
;
830 uint32_t bits
= SampleLF(payload
->verbose
, payload
->samples
);
831 reply_ng(CMD_LF_ACQ_RAW_ADC
, PM3_SUCCESS
, (uint8_t *)&bits
, sizeof(bits
));
834 case CMD_LF_MOD_THEN_ACQ_RAW_ADC
: {
839 uint8_t symbol_extra
[LF_CMDREAD_MAX_EXTRA_SYMBOLS
];
840 uint16_t period_extra
[LF_CMDREAD_MAX_EXTRA_SYMBOLS
];
841 uint32_t samples
: 31;
844 struct p
*payload
= (struct p
*)packet
->data
.asBytes
;
845 uint8_t symbol_extra
[LF_CMDREAD_MAX_EXTRA_SYMBOLS
];
846 uint16_t period_extra
[LF_CMDREAD_MAX_EXTRA_SYMBOLS
];
847 memcpy(symbol_extra
, payload
->symbol_extra
, sizeof(symbol_extra
));
848 memcpy(period_extra
, payload
->period_extra
, sizeof(period_extra
));
849 ModThenAcquireRawAdcSamples125k(payload
->delay
, payload
->period_0
, payload
->period_1
, symbol_extra
, period_extra
, packet
->data
.asBytes
+ sizeof(struct p
), payload
->verbose
, payload
->samples
);
852 case CMD_LF_SNIFF_RAW_ADC
: {
854 uint32_t samples
: 31;
857 struct p
*payload
= (struct p
*)packet
->data
.asBytes
;
858 uint32_t bits
= SniffLF(payload
->verbose
, payload
->samples
);
859 reply_ng(CMD_LF_SNIFF_RAW_ADC
, PM3_SUCCESS
, (uint8_t *)&bits
, sizeof(bits
));
862 case CMD_LF_HID_WATCH
: {
864 int res
= lf_hid_watch(0, &high
, &low
);
865 reply_ng(CMD_LF_HID_WATCH
, res
, NULL
, 0);
868 case CMD_LF_HID_SIMULATE
: {
869 lf_hidsim_t
*payload
= (lf_hidsim_t
*)packet
->data
.asBytes
;
870 CmdHIDsimTAG(payload
->hi2
, payload
->hi
, payload
->lo
, payload
->longFMT
, 1);
873 case CMD_LF_FSK_SIMULATE
: {
874 lf_fsksim_t
*payload
= (lf_fsksim_t
*)packet
->data
.asBytes
;
875 CmdFSKsimTAG(payload
->fchigh
, payload
->fclow
, payload
->separator
, payload
->clock
, packet
->length
- sizeof(lf_fsksim_t
), payload
->data
, true);
878 case CMD_LF_ASK_SIMULATE
: {
879 lf_asksim_t
*payload
= (lf_asksim_t
*)packet
->data
.asBytes
;
880 CmdASKsimTAG(payload
->encoding
, payload
->invert
, payload
->separator
, payload
->clock
, packet
->length
- sizeof(lf_asksim_t
), payload
->data
, true);
883 case CMD_LF_PSK_SIMULATE
: {
884 lf_psksim_t
*payload
= (lf_psksim_t
*)packet
->data
.asBytes
;
885 CmdPSKsimTAG(payload
->carrier
, payload
->invert
, payload
->clock
, packet
->length
- sizeof(lf_psksim_t
), payload
->data
, true);
888 case CMD_LF_NRZ_SIMULATE
: {
889 lf_nrzsim_t
*payload
= (lf_nrzsim_t
*)packet
->data
.asBytes
;
890 CmdNRZsimTAG(payload
->invert
, payload
->separator
, payload
->clock
, packet
->length
- sizeof(lf_nrzsim_t
), payload
->data
, true);
893 case CMD_LF_HID_CLONE
: {
894 lf_hidsim_t
*payload
= (lf_hidsim_t
*)packet
->data
.asBytes
;
895 CopyHIDtoT55x7(payload
->hi2
, payload
->hi
, payload
->lo
, payload
->longFMT
, payload
->Q5
, payload
->EM
);
898 case CMD_LF_IO_WATCH
: {
900 int res
= lf_io_watch(0, &high
, &low
);
901 reply_ng(CMD_LF_IO_WATCH
, res
, NULL
, 0);
904 case CMD_LF_EM410X_WATCH
: {
907 int res
= lf_em410x_watch(0, &high
, &low
);
908 reply_ng(CMD_LF_EM410X_WATCH
, res
, NULL
, 0);
911 case CMD_LF_EM410X_WRITE
: {
918 struct p
*payload
= (struct p
*)packet
->data
.asBytes
;
919 int res
= copy_em410x_to_t55xx(payload
->card
, payload
->clock
, payload
->high
, payload
->low
);
920 reply_ng(CMD_LF_EM410X_WRITE
, res
, NULL
, 0);
923 case CMD_LF_TI_READ
: {
927 case CMD_LF_TI_WRITE
: {
933 struct p
*payload
= (struct p
*)packet
->data
.asBytes
;
934 WriteTItag(payload
->high
, payload
->low
, packet
->crc
);
937 case CMD_LF_SIMULATE
: {
943 struct p
*payload
= (struct p
*)packet
->data
.asBytes
;
944 // length, start gap, led control
945 SimulateTagLowFrequency(payload
->len
, payload
->gap
, true);
946 reply_ng(CMD_LF_SIMULATE
, PM3_EOPABORTED
, NULL
, 0);
950 case CMD_LF_SIMULATE_BIDIR
: {
951 SimulateTagLowFrequencyBidir(packet
->oldarg
[0], packet
->oldarg
[1]);
954 case CMD_LF_T55XX_READBL
: {
960 uint8_t downlink_mode
;
962 struct p
*payload
= (struct p
*) packet
->data
.asBytes
;
963 T55xxReadBlock(payload
->page
, payload
->pwdmode
, false, payload
->blockno
, payload
->password
, payload
->downlink_mode
);
966 case CMD_LF_T55XX_WRITEBL
: {
968 T55xxWriteBlock(packet
->data
.asBytes
);
971 case CMD_LF_T55XX_DANGERRAW
: {
972 T55xxDangerousRawTest(packet
->data
.asBytes
);
975 case CMD_LF_T55XX_WAKEUP
: {
980 struct p
*payload
= (struct p
*) packet
->data
.asBytes
;
981 T55xxWakeUp(payload
->password
, payload
->flags
);
984 case CMD_LF_T55XX_RESET_READ
: {
985 T55xxResetRead(packet
->data
.asBytes
[0] & 0xff);
988 case CMD_LF_T55XX_CHK_PWDS
: {
989 T55xx_ChkPwds(packet
->data
.asBytes
[0] & 0xff);
992 case CMD_LF_PCF7931_READ
: {
996 case CMD_LF_PCF7931_WRITE
: {
998 packet
->data
.asBytes
[0], packet
->data
.asBytes
[1], packet
->data
.asBytes
[2], packet
->data
.asBytes
[3],
999 packet
->data
.asBytes
[4], packet
->data
.asBytes
[5], packet
->data
.asBytes
[6], packet
->data
.asBytes
[9],
1000 packet
->data
.asBytes
[7] - 128, packet
->data
.asBytes
[8] - 128,
1007 case CMD_LF_EM4X_LOGIN
: {
1011 struct p
*payload
= (struct p
*) packet
->data
.asBytes
;
1012 EM4xLogin(payload
->password
);
1015 case CMD_LF_EM4X_BF
: {
1020 struct p
*payload
= (struct p
*) packet
->data
.asBytes
;
1021 EM4xBruteforce(payload
->start_pwd
, payload
->n
);
1024 case CMD_LF_EM4X_READWORD
: {
1030 struct p
*payload
= (struct p
*) packet
->data
.asBytes
;
1031 EM4xReadWord(payload
->address
, payload
->password
, payload
->usepwd
);
1034 case CMD_LF_EM4X_WRITEWORD
: {
1041 struct p
*payload
= (struct p
*) packet
->data
.asBytes
;
1042 EM4xWriteWord(payload
->address
, payload
->data
, payload
->password
, payload
->usepwd
);
1045 case CMD_LF_EM4X_PROTECTWORD
: {
1051 struct p
*payload
= (struct p
*) packet
->data
.asBytes
;
1052 EM4xProtectWord(payload
->data
, payload
->password
, payload
->usepwd
);
1055 case CMD_LF_AWID_WATCH
: {
1057 int res
= lf_awid_watch(0, &high
, &low
);
1058 reply_ng(CMD_LF_AWID_WATCH
, res
, NULL
, 0);
1061 case CMD_LF_VIKING_CLONE
: {
1067 struct p
*payload
= (struct p
*)packet
->data
.asBytes
;
1068 CopyVikingtoT55xx(payload
->blocks
, payload
->Q5
, payload
->EM
);
1071 case CMD_LF_COTAG_READ
: {
1075 struct p
*payload
= (struct p
*)packet
->data
.asBytes
;
1076 Cotag(payload
->mode
);
1082 case CMD_LF_HITAG_SNIFF
: { // Eavesdrop Hitag tag, args = type
1084 // SniffHitag2(packet->oldarg[0]);
1085 reply_ng(CMD_LF_HITAG_SNIFF
, PM3_SUCCESS
, NULL
, 0);
1088 case CMD_LF_HITAG_SIMULATE
: { // Simulate Hitag tag, args = memory content
1092 case CMD_LF_HITAG_READER
: { // Reader for Hitag tags, args = type and function
1093 ReaderHitag((hitag_function
)packet
->oldarg
[0], (hitag_data
*)packet
->data
.asBytes
);
1096 case CMD_LF_HITAGS_SIMULATE
: { // Simulate Hitag s tag, args = memory content
1097 SimulateHitagSTag((bool)packet
->oldarg
[0], packet
->data
.asBytes
);
1100 case CMD_LF_HITAGS_TEST_TRACES
: { // Tests every challenge within the given file
1101 check_challenges((bool)packet
->oldarg
[0], packet
->data
.asBytes
);
1104 case CMD_LF_HITAGS_READ
: { //Reader for only Hitag S tags, args = key or challenge
1105 ReadHitagS((hitag_function
)packet
->oldarg
[0], (hitag_data
*)packet
->data
.asBytes
);
1108 case CMD_LF_HITAGS_WRITE
: { //writer for Hitag tags args=data to write,page and key or challenge
1109 if ((hitag_function
)packet
->oldarg
[0] < 10) {
1110 WritePageHitagS((hitag_function
)packet
->oldarg
[0], (hitag_data
*)packet
->data
.asBytes
, packet
->oldarg
[2]);
1112 WriterHitag((hitag_function
)packet
->oldarg
[0], (hitag_data
*)packet
->data
.asBytes
, packet
->oldarg
[2]);
1116 case CMD_LF_HITAG_ELOAD
: {
1117 lf_hitag_t
*payload
= (lf_hitag_t
*) packet
->data
.asBytes
;
1118 uint8_t *mem
= BigBuf_get_EM_addr();
1119 memcpy((uint8_t *)mem
, payload
->data
, payload
->len
);
1125 case CMD_LF_EM4X50_INFO
: {
1126 em4x50_info((em4x50_data_t
*)packet
->data
.asBytes
);
1129 case CMD_LF_EM4X50_WRITE
: {
1130 em4x50_write((em4x50_data_t
*)packet
->data
.asBytes
);
1133 case CMD_LF_EM4X50_WRITEPWD
: {
1134 em4x50_writepwd((em4x50_data_t
*)packet
->data
.asBytes
);
1137 case CMD_LF_EM4X50_READ
: {
1138 em4x50_read((em4x50_data_t
*)packet
->data
.asBytes
);
1141 case CMD_LF_EM4X50_BRUTE
: {
1142 em4x50_brute((em4x50_data_t
*)packet
->data
.asBytes
);
1145 case CMD_LF_EM4X50_LOGIN
: {
1146 em4x50_login((uint32_t *)packet
->data
.asBytes
);
1149 case CMD_LF_EM4X50_SIM
: {
1150 //-----------------------------------------------------------------------------
1151 // Note: we call FpgaDownloadAndGo(FPGA_BITSTREAM_LF) here although FPGA is not
1152 // involved in dealing with emulator memory. But if it is called later, it might
1153 // destroy the Emulator Memory.
1154 //-----------------------------------------------------------------------------
1155 FpgaDownloadAndGo(FPGA_BITSTREAM_LF
);
1156 em4x50_sim((uint32_t *)packet
->data
.asBytes
);
1159 case CMD_LF_EM4X50_READER
: {
1163 case CMD_LF_EM4X50_ESET
: {
1164 //-----------------------------------------------------------------------------
1165 // Note: we call FpgaDownloadAndGo(FPGA_BITSTREAM_LF) here although FPGA is not
1166 // involved in dealing with emulator memory. But if it is called later, it might
1167 // destroy the Emulator Memory.
1168 //-----------------------------------------------------------------------------
1169 FpgaDownloadAndGo(FPGA_BITSTREAM_LF
);
1170 emlSet(packet
->data
.asBytes
, packet
->oldarg
[0], packet
->oldarg
[1]);
1173 case CMD_LF_EM4X50_CHK
: {
1174 //-----------------------------------------------------------------------------
1175 // Note: we call FpgaDownloadAndGo(FPGA_BITSTREAM_LF) here although FPGA is not
1176 // involved in dealing with emulator memory. But if it is called later, it might
1177 // destroy the Emulator Memory.
1178 //-----------------------------------------------------------------------------
1179 FpgaDownloadAndGo(FPGA_BITSTREAM_LF
);
1180 em4x50_chk((uint8_t *)packet
->data
.asBytes
);
1186 case CMD_LF_EM4X70_INFO
: {
1187 em4x70_info((em4x70_data_t
*)packet
->data
.asBytes
);
1190 case CMD_LF_EM4X70_WRITE
: {
1191 em4x70_write((em4x70_data_t
*)packet
->data
.asBytes
);
1194 case CMD_LF_EM4X70_UNLOCK
: {
1195 em4x70_unlock((em4x70_data_t
*)packet
->data
.asBytes
);
1198 case CMD_LF_EM4X70_AUTH
: {
1199 em4x70_auth((em4x70_data_t
*)packet
->data
.asBytes
);
1202 case CMD_LF_EM4X70_WRITEPIN
: {
1203 em4x70_write_pin((em4x70_data_t
*)packet
->data
.asBytes
);
1206 case CMD_LF_EM4X70_WRITEKEY
: {
1207 em4x70_write_key((em4x70_data_t
*)packet
->data
.asBytes
);
1212 #ifdef WITH_ISO15693
1213 case CMD_HF_ISO15693_ACQ_RAW_ADC
: {
1214 AcquireRawAdcSamplesIso15693();
1217 case CMD_HF_ISO15693_SNIFF
: {
1218 SniffIso15693(0, NULL
);
1219 reply_ng(CMD_HF_ISO15693_SNIFF
, PM3_SUCCESS
, NULL
, 0);
1222 case CMD_HF_ISO15693_COMMAND
: {
1223 DirectTag15693Command(packet
->oldarg
[0], packet
->oldarg
[1], packet
->oldarg
[2], packet
->data
.asBytes
);
1226 case CMD_HF_ISO15693_FINDAFI
: {
1227 BruteforceIso15693Afi(packet
->oldarg
[0]);
1230 case CMD_HF_ISO15693_READER
: {
1231 ReaderIso15693(packet
->oldarg
[0], NULL
);
1234 case CMD_HF_ISO15693_SIMULATE
: {
1238 struct p
*payload
= (struct p
*) packet
->data
.asBytes
;
1239 SimTagIso15693(payload
->uid
);
1242 case CMD_HF_ISO15693_CSETUID
: {
1246 struct p
*payload
= (struct p
*) packet
->data
.asBytes
;
1247 SetTag15693Uid(payload
->uid
);
1250 case CMD_HF_ISO15693_SLIX_L_DISABLE_PRIVACY
: {
1254 struct p
*payload
= (struct p
*) packet
->data
.asBytes
;
1255 DisablePrivacySlixLIso15693(payload
->pwd
);
1262 case CMD_HF_LEGIC_SIMULATE
: {
1267 struct p
*payload
= (struct p
*) packet
->data
.asBytes
;
1268 LegicRfSimulate(payload
->tagtype
, payload
->send_reply
);
1271 case CMD_HF_LEGIC_WRITER
: {
1272 LegicRfWriter(packet
->oldarg
[0], packet
->oldarg
[1], packet
->oldarg
[2], packet
->data
.asBytes
);
1275 case CMD_HF_LEGIC_READER
: {
1276 LegicRfReader(packet
->oldarg
[0], packet
->oldarg
[1], packet
->oldarg
[2]);
1279 case CMD_HF_LEGIC_INFO
: {
1283 case CMD_HF_LEGIC_ESET
: {
1284 //-----------------------------------------------------------------------------
1285 // Note: we call FpgaDownloadAndGo(FPGA_BITSTREAM_HF) here although FPGA is not
1286 // involved in dealing with emulator memory. But if it is called later, it might
1287 // destroy the Emulator Memory.
1288 //-----------------------------------------------------------------------------
1290 // arg1 = num of bytes
1291 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
1292 emlSet(packet
->data
.asBytes
, packet
->oldarg
[0], packet
->oldarg
[1]);
1297 #ifdef WITH_ISO14443b
1298 case CMD_HF_SRI_READ
: {
1302 struct p
*payload
= (struct p
*) packet
->data
.asBytes
;
1303 ReadSTBlock(payload
->blockno
);
1306 case CMD_HF_ISO14443B_SNIFF
: {
1308 reply_ng(CMD_HF_ISO14443B_SNIFF
, PM3_SUCCESS
, NULL
, 0);
1311 case CMD_HF_ISO14443B_SIMULATE
: {
1312 SimulateIso14443bTag(packet
->data
.asBytes
);
1315 case CMD_HF_ISO14443B_COMMAND
: {
1316 iso14b_raw_cmd_t
*payload
= (iso14b_raw_cmd_t
*)packet
->data
.asBytes
;
1317 SendRawCommand14443B_Ex(payload
);
1320 case CMD_HF_CRYPTORF_SIM
: {
1321 // simulate_crf_tag();
1327 case CMD_HF_FELICA_COMMAND
: {
1328 felica_sendraw(packet
);
1331 case CMD_HF_FELICALITE_SIMULATE
: {
1335 struct p
*payload
= (struct p
*) packet
->data
.asBytes
;
1336 felica_sim_lite(payload
->uid
);
1339 case CMD_HF_FELICA_SNIFF
: {
1344 struct p
*payload
= (struct p
*) packet
->data
.asBytes
;
1345 felica_sniff(payload
->samples
, payload
->triggers
);
1348 case CMD_HF_FELICALITE_DUMP
: {
1349 felica_dump_lite_s();
1354 #ifdef WITH_ISO14443a
1355 case CMD_HF_ISO14443A_PRINT_CONFIG
: {
1359 case CMD_HF_ISO14443A_GET_CONFIG
: {
1360 hf14a_config
*hf14aconfig
= getHf14aConfig();
1361 reply_ng(CMD_HF_ISO14443A_GET_CONFIG
, PM3_SUCCESS
, (uint8_t *)hf14aconfig
, sizeof(hf14a_config
));
1364 case CMD_HF_ISO14443A_SET_CONFIG
: {
1366 memcpy(&c
, packet
->data
.asBytes
, sizeof(hf14a_config
));
1370 case CMD_HF_ISO14443A_SNIFF
: {
1371 SniffIso14443a(packet
->data
.asBytes
[0]);
1372 reply_ng(CMD_HF_ISO14443A_SNIFF
, PM3_SUCCESS
, NULL
, 0);
1375 case CMD_HF_ISO14443A_READER
: {
1376 ReaderIso14443a(packet
);
1379 case CMD_HF_ISO14443A_SIMULATE
: {
1386 struct p
*payload
= (struct p
*) packet
->data
.asBytes
;
1387 SimulateIso14443aTag(payload
->tagtype
, payload
->flags
, payload
->uid
, payload
->exitAfter
); // ## Simulate iso14443a tag - pass tag type & UID
1390 case CMD_HF_ISO14443A_ANTIFUZZ
: {
1394 struct p
*payload
= (struct p
*) packet
->data
.asBytes
;
1395 iso14443a_antifuzz(payload
->flag
);
1398 case CMD_HF_EPA_COLLECT_NONCE
: {
1399 EPA_PACE_Collect_Nonce(packet
);
1402 case CMD_HF_EPA_REPLAY
: {
1403 EPA_PACE_Replay(packet
);
1406 case CMD_HF_MIFARE_READER
: {
1412 struct p
*payload
= (struct p
*) packet
->data
.asBytes
;
1413 ReaderMifare(payload
->first_run
, payload
->blockno
, payload
->key_type
);
1416 case CMD_HF_MIFARE_READBL
: {
1417 mf_readblock_t
*payload
= (mf_readblock_t
*)packet
->data
.asBytes
;
1418 MifareReadBlock(payload
->blockno
, payload
->keytype
, payload
->key
);
1421 case CMD_HF_MIFAREU_READBL
: {
1422 MifareUReadBlock(packet
->oldarg
[0], packet
->oldarg
[1], packet
->data
.asBytes
);
1425 case CMD_HF_MIFAREUC_AUTH
: {
1426 MifareUC_Auth(packet
->oldarg
[0], packet
->data
.asBytes
);
1429 case CMD_HF_MIFAREU_READCARD
: {
1430 MifareUReadCard(packet
->oldarg
[0], packet
->oldarg
[1], packet
->oldarg
[2], packet
->data
.asBytes
);
1433 case CMD_HF_MIFAREUC_SETPWD
: {
1434 MifareUSetPwd(packet
->oldarg
[0], packet
->data
.asBytes
);
1437 case CMD_HF_MIFARE_READSC
: {
1438 MifareReadSector(packet
->oldarg
[0], packet
->oldarg
[1], packet
->data
.asBytes
);
1441 case CMD_HF_MIFARE_WRITEBL
: {
1442 MifareWriteBlock(packet
->oldarg
[0], packet
->oldarg
[1], packet
->data
.asBytes
);
1445 case CMD_HF_MIFAREU_WRITEBL
: {
1446 MifareUWriteBlock(packet
->oldarg
[0], packet
->oldarg
[1], packet
->data
.asBytes
);
1449 case CMD_HF_MIFAREU_WRITEBL_COMPAT
: {
1450 MifareUWriteBlockCompat(packet
->oldarg
[0], packet
->oldarg
[1], packet
->data
.asBytes
);
1453 case CMD_HF_MIFARE_ACQ_ENCRYPTED_NONCES
: {
1454 MifareAcquireEncryptedNonces(packet
->oldarg
[0], packet
->oldarg
[1], packet
->oldarg
[2], packet
->data
.asBytes
);
1457 case CMD_HF_MIFARE_ACQ_NONCES
: {
1458 MifareAcquireNonces(packet
->oldarg
[0], packet
->oldarg
[2]);
1461 case CMD_HF_MIFARE_NESTED
: {
1465 uint8_t target_block
;
1466 uint8_t target_keytype
;
1470 struct p
*payload
= (struct p
*) packet
->data
.asBytes
;
1471 MifareNested(payload
->block
, payload
->keytype
, payload
->target_block
, payload
->target_keytype
, payload
->calibrate
, payload
->key
);
1474 case CMD_HF_MIFARE_STATIC_NESTED
: {
1478 uint8_t target_block
;
1479 uint8_t target_keytype
;
1482 struct p
*payload
= (struct p
*) packet
->data
.asBytes
;
1483 MifareStaticNested(payload
->block
, payload
->keytype
, payload
->target_block
, payload
->target_keytype
, payload
->key
);
1486 case CMD_HF_MIFARE_CHKKEYS
: {
1487 MifareChkKeys(packet
->data
.asBytes
, false);
1490 case CMD_HF_MIFARE_CHKKEYS_FAST
: {
1491 MifareChkKeys_fast(packet
->oldarg
[0], packet
->oldarg
[1], packet
->oldarg
[2], packet
->data
.asBytes
);
1494 case CMD_HF_MIFARE_CHKKEYS_FILE
: {
1496 uint8_t filename
[32];
1498 struct p
*payload
= (struct p
*) packet
->data
.asBytes
;
1499 MifareChkKeys_file(payload
->filename
);
1502 case CMD_HF_MIFARE_SIMULATE
: {
1510 struct p
*payload
= (struct p
*) packet
->data
.asBytes
;
1511 Mifare1ksim(payload
->flags
, payload
->exitAfter
, payload
->uid
, payload
->atqa
, payload
->sak
);
1514 case CMD_HF_MIFARE_EML_MEMCLR
: {
1516 reply_ng(CMD_HF_MIFARE_EML_MEMCLR
, PM3_SUCCESS
, NULL
, 0);
1519 case CMD_HF_MIFARE_EML_MEMSET
: {
1526 struct p
*payload
= (struct p
*) packet
->data
.asBytes
;
1527 MifareEMemSet(payload
->blockno
, payload
->blockcnt
, payload
->blockwidth
, payload
->data
);
1530 case CMD_HF_MIFARE_EML_MEMGET
: {
1535 struct p
*payload
= (struct p
*) packet
->data
.asBytes
;
1536 MifareEMemGet(payload
->blockno
, payload
->blockcnt
);
1539 case CMD_HF_MIFARE_EML_LOAD
: {
1540 mfc_eload_t
*payload
= (mfc_eload_t
*) packet
->data
.asBytes
;
1541 MifareECardLoadExt(payload
->sectorcnt
, payload
->keytype
);
1544 // Work with "magic Chinese" card
1545 case CMD_HF_MIFARE_CSETBL
: {
1546 MifareCSetBlock(packet
->oldarg
[0], packet
->oldarg
[1], packet
->data
.asBytes
);
1549 case CMD_HF_MIFARE_CGETBL
: {
1550 MifareCGetBlock(packet
->oldarg
[0], packet
->oldarg
[1], packet
->data
.asBytes
);
1553 case CMD_HF_MIFARE_CIDENT
: {
1554 bool is_mfc
= packet
->data
.asBytes
[0];
1555 MifareCIdent(is_mfc
);
1558 // Gen 3 magic cards
1559 case CMD_HF_MIFARE_GEN3UID
: {
1560 MifareGen3UID(packet
->oldarg
[0], packet
->data
.asBytes
);
1563 case CMD_HF_MIFARE_GEN3BLK
: {
1564 MifareGen3Blk(packet
->oldarg
[0], packet
->data
.asBytes
);
1567 case CMD_HF_MIFARE_GEN3FREEZ
: {
1571 case CMD_HF_MIFARE_G3_RDBL
: {
1575 struct p
*payload
= (struct p
*) packet
->data
.asBytes
;
1576 MifareG3ReadBlk(payload
->blockno
);
1579 case CMD_HF_MIFARE_PERSONALIZE_UID
: {
1582 uint8_t pers_option
;
1585 struct p
*payload
= (struct p
*) packet
->data
.asBytes
;
1586 uint64_t authkey
= bytes_to_num(payload
->key
, 6);
1587 MifarePersonalizeUID(payload
->keytype
, payload
->pers_option
, authkey
);
1590 case CMD_HF_MIFARE_SETMOD
: {
1591 MifareSetMod(packet
->data
.asBytes
);
1595 case CMD_HF_DESFIRE_READBL
: {
1598 case CMD_HF_DESFIRE_WRITEBL
: {
1601 case CMD_HF_DESFIRE_AUTH1
: {
1602 MifareDES_Auth1(packet
->data
.asBytes
);
1605 case CMD_HF_DESFIRE_AUTH2
: {
1606 //MifareDES_Auth2(packet->oldarg[0],packet->data.asBytes);
1609 case CMD_HF_DESFIRE_READER
: {
1610 //readermifaredes(packet->oldarg[0], packet->oldarg[1], packet->data.asBytes);
1613 case CMD_HF_DESFIRE_INFO
: {
1614 MifareDesfireGetInformation();
1617 case CMD_HF_DESFIRE_COMMAND
: {
1618 MifareSendCommand(packet
->data
.asBytes
);
1621 case CMD_HF_MIFARE_NACK_DETECT
: {
1625 case CMD_HF_MFU_OTP_TEAROFF
: {
1626 MifareU_Otp_Tearoff(packet
->oldarg
[0], packet
->oldarg
[1], packet
->data
.asBytes
);
1629 case CMD_HF_MFU_COUNTER_TEAROFF
: {
1632 uint32_t tearoff_time
;
1635 struct p
*payload
= (struct p
*) packet
->data
.asBytes
;
1636 MifareU_Counter_Tearoff(payload
->counter
, payload
->tearoff_time
, payload
->value
);
1639 case CMD_HF_MIFARE_STATIC_NONCE
: {
1640 MifareHasStaticNonce();
1645 #ifdef WITH_NFCBARCODE
1646 case CMD_HF_THINFILM_READ
: {
1650 case CMD_HF_THINFILM_SIMULATE
: {
1651 SimulateThinFilm(packet
->data
.asBytes
, packet
->length
);
1657 // Makes use of ISO14443a FPGA Firmware
1658 case CMD_HF_ICLASS_SNIFF
: {
1660 uint8_t jam_search_len
;
1661 uint8_t jam_search_string
[];
1663 struct p
*payload
= (struct p
*) packet
->data
.asBytes
;
1664 SniffIClass(payload
->jam_search_len
, payload
->jam_search_string
);
1665 reply_ng(CMD_HF_ICLASS_SNIFF
, PM3_SUCCESS
, NULL
, 0);
1668 case CMD_HF_ICLASS_SIMULATE
: {
1674 struct p *payload = (struct p *) packet->data.asBytes;
1677 SimulateIClass(packet
->oldarg
[0], packet
->oldarg
[1], packet
->oldarg
[2], packet
->data
.asBytes
);
1680 case CMD_HF_ICLASS_READER
: {
1681 ReaderIClass(packet
->oldarg
[0]);
1684 case CMD_HF_ICLASS_EML_MEMSET
: {
1685 //iceman, should call FPGADOWNLOAD before, since it corrupts BigBuf
1686 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
1692 struct p
*payload
= (struct p
*) packet
->data
.asBytes
;
1693 emlSet(payload
->data
, payload
->offset
, payload
->len
);
1696 case CMD_HF_ICLASS_WRITEBL
: {
1697 iClass_WriteBlock(packet
->data
.asBytes
);
1700 case CMD_HF_ICLASS_READBL
: {
1701 iClass_ReadBlock(packet
->data
.asBytes
);
1704 case CMD_HF_ICLASS_CHKKEYS
: {
1705 iClass_Authentication_fast((iclass_chk_t
*)packet
->data
.asBytes
);
1708 case CMD_HF_ICLASS_DUMP
: {
1709 iClass_Dump(packet
->data
.asBytes
);
1712 case CMD_HF_ICLASS_RESTORE
: {
1713 iClass_Restore((iclass_restore_req_t
*)packet
->data
.asBytes
);
1719 case CMD_HF_SNIFF
: {
1721 uint32_t samplesToSkip
;
1722 uint32_t triggersToSkip
;
1724 struct p
*payload
= (struct p
*)packet
->data
.asBytes
;
1727 int res
= HfSniff(payload
->samplesToSkip
, payload
->triggersToSkip
, &len
);
1733 reply_ng(CMD_HF_SNIFF
, res
, (uint8_t *)&retval
, sizeof(retval
));
1739 case CMD_FPGAMEM_DOWNLOAD
: {
1745 #ifdef WITH_SMARTCARD
1746 case CMD_SMART_ATR
: {
1750 case CMD_SMART_SETBAUD
: {
1751 SmartCardSetBaud(packet
->oldarg
[0]);
1754 case CMD_SMART_SETCLOCK
: {
1758 struct p
*payload
= (struct p
*)packet
->data
.asBytes
;
1759 SmartCardSetClock(payload
->new_clk
);
1762 case CMD_SMART_RAW
: {
1763 SmartCardRaw((smart_card_raw_t
*)packet
->data
.asBytes
);
1766 case CMD_SMART_UPLOAD
: {
1767 // upload file from client
1770 uint32_t bytes_in_packet
;
1774 struct p
*payload
= (struct p
*)packet
->data
.asBytes
;
1775 uint8_t *mem
= BigBuf_get_addr();
1776 memcpy(mem
+ payload
->idx
, payload
->data
, payload
->bytes_in_packet
);
1778 uint8_t a
= 0, b
= 0;
1779 compute_crc(CRC_14443_A
, mem
+ payload
->idx
, payload
->bytes_in_packet
, &a
, &b
);
1780 int res
= PM3_SUCCESS
;
1781 if (payload
->crc
!= (a
<< 8 | b
)) {
1782 DbpString("CRC Failed");
1785 reply_ng(CMD_SMART_UPLOAD
, res
, NULL
, 0);
1788 case CMD_SMART_UPGRADE
: {
1793 struct p
*payload
= (struct p
*)packet
->data
.asBytes
;
1795 uint8_t *fwdata
= BigBuf_get_addr();
1796 uint8_t a
= 0, b
= 0;
1797 compute_crc(CRC_14443_A
, fwdata
, payload
->fw_size
, &a
, &b
);
1799 if (payload
->crc
!= (a
<< 8 | b
)) {
1800 Dbprintf("CRC Failed, 0x[%04x] != 0x[%02x%02x]", payload
->crc
, a
, b
);
1801 reply_ng(CMD_SMART_UPGRADE
, PM3_ESOFT
, NULL
, 0);
1803 SmartCardUpgrade(payload
->fw_size
);
1810 #ifdef WITH_FPC_USART
1811 case CMD_USART_TX
: {
1813 usart_writebuffer_sync(packet
->data
.asBytes
, packet
->length
);
1814 reply_ng(CMD_USART_TX
, PM3_SUCCESS
, NULL
, 0);
1818 case CMD_USART_RX
: {
1823 struct p
*payload
= (struct p
*) &packet
->data
.asBytes
;
1825 uint16_t pre_available
= 0;
1826 uint8_t *dest
= BigBuf_malloc(USART_FIFOLEN
);
1827 uint32_t wait
= payload
->waittime
;
1828 uint32_t ti
= GetTickCount();
1831 available
= usart_rxdata_available();
1832 if (available
> pre_available
) {
1833 // When receiving data, reset timer and shorten timeout
1834 ti
= GetTickCount();
1836 pre_available
= available
;
1839 // We stop either after waittime if no data or 50ms after last data received
1840 if (GetTickCountDelta(ti
) > wait
)
1843 if (available
> 0) {
1844 uint16_t len
= usart_read_ng(dest
, available
);
1845 reply_ng(CMD_USART_RX
, PM3_SUCCESS
, dest
, len
);
1847 reply_ng(CMD_USART_RX
, PM3_ENODATA
, NULL
, 0);
1853 case CMD_USART_TXRX
: {
1859 struct p
*payload
= (struct p
*) &packet
->data
.asBytes
;
1860 usart_writebuffer_sync(payload
->data
, packet
->length
- sizeof(payload
));
1862 uint16_t pre_available
= 0;
1863 uint8_t *dest
= BigBuf_malloc(USART_FIFOLEN
);
1864 uint32_t wait
= payload
->waittime
;
1865 uint32_t ti
= GetTickCount();
1868 available
= usart_rxdata_available();
1869 if (available
> pre_available
) {
1870 // When receiving data, reset timer and shorten timeout
1871 ti
= GetTickCount();
1873 pre_available
= available
;
1876 // We stop either after waittime if no data or 50ms after last data received
1877 if (GetTickCountDelta(ti
) > wait
)
1880 if (available
> 0) {
1881 uint16_t len
= usart_read_ng(dest
, available
);
1882 reply_ng(CMD_USART_TXRX
, PM3_SUCCESS
, dest
, len
);
1884 reply_ng(CMD_USART_TXRX
, PM3_ENODATA
, NULL
, 0);
1890 case CMD_USART_CONFIG
: {
1895 struct p
*payload
= (struct p
*) &packet
->data
.asBytes
;
1896 usart_init(payload
->baudrate
, payload
->parity
);
1897 reply_ng(CMD_USART_CONFIG
, PM3_SUCCESS
, NULL
, 0);
1901 case CMD_BUFF_CLEAR
: {
1906 case CMD_MEASURE_ANTENNA_TUNING
: {
1907 MeasureAntennaTuning();
1910 case CMD_MEASURE_ANTENNA_TUNING_HF
: {
1911 if (packet
->length
!= 1)
1912 reply_ng(CMD_MEASURE_ANTENNA_TUNING_HF
, PM3_EINVARG
, NULL
, 0);
1914 switch (packet
->data
.asBytes
[0]) {
1915 case 1: // MEASURE_ANTENNA_TUNING_HF_START
1916 // Let the FPGA drive the high-frequency antenna around 13.56 MHz.
1917 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
1918 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER
);
1919 reply_ng(CMD_MEASURE_ANTENNA_TUNING_HF
, PM3_SUCCESS
, NULL
, 0);
1922 if (button_status
== BUTTON_SINGLE_CLICK
)
1923 reply_ng(CMD_MEASURE_ANTENNA_TUNING_HF
, PM3_EOPABORTED
, NULL
, 0);
1924 uint16_t volt
= MeasureAntennaTuningHfData();
1925 reply_ng(CMD_MEASURE_ANTENNA_TUNING_HF
, PM3_SUCCESS
, (uint8_t *)&volt
, sizeof(volt
));
1928 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1929 reply_ng(CMD_MEASURE_ANTENNA_TUNING_HF
, PM3_SUCCESS
, NULL
, 0);
1932 reply_ng(CMD_MEASURE_ANTENNA_TUNING_HF
, PM3_EINVARG
, NULL
, 0);
1937 case CMD_MEASURE_ANTENNA_TUNING_LF
: {
1938 if (packet
->length
!= 2)
1939 reply_ng(CMD_MEASURE_ANTENNA_TUNING_LF
, PM3_EINVARG
, NULL
, 0);
1941 switch (packet
->data
.asBytes
[0]) {
1942 case 1: // MEASURE_ANTENNA_TUNING_LF_START
1943 // Let the FPGA drive the low-frequency antenna around 125kHz
1944 FpgaDownloadAndGo(FPGA_BITSTREAM_LF
);
1945 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
| FPGA_LF_ADC_READER_FIELD
);
1946 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, packet
->data
.asBytes
[1]);
1947 reply_ng(CMD_MEASURE_ANTENNA_TUNING_LF
, PM3_SUCCESS
, NULL
, 0);
1950 if (button_status
== BUTTON_SINGLE_CLICK
)
1951 reply_ng(CMD_MEASURE_ANTENNA_TUNING_LF
, PM3_EOPABORTED
, NULL
, 0);
1953 uint32_t volt
= MeasureAntennaTuningLfData();
1954 reply_ng(CMD_MEASURE_ANTENNA_TUNING_LF
, PM3_SUCCESS
, (uint8_t *)&volt
, sizeof(volt
));
1957 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1958 reply_ng(CMD_MEASURE_ANTENNA_TUNING_LF
, PM3_SUCCESS
, NULL
, 0);
1961 reply_ng(CMD_MEASURE_ANTENNA_TUNING_LF
, PM3_EINVARG
, NULL
, 0);
1966 case CMD_LISTEN_READER_FIELD
: {
1967 if (packet
->length
!= sizeof(uint8_t))
1969 ListenReaderField(packet
->data
.asBytes
[0]);
1972 case CMD_FPGA_MAJOR_MODE_OFF
: { // ## FPGA Control
1973 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1975 LED_D_OFF(); // LED D indicates field ON or OFF
1978 case CMD_DOWNLOAD_BIGBUF
: {
1980 uint8_t *mem
= BigBuf_get_addr();
1981 uint32_t startidx
= packet
->oldarg
[0];
1982 uint32_t numofbytes
= packet
->oldarg
[1];
1984 // arg0 = startindex
1985 // arg1 = length bytes to transfer
1986 // arg2 = BigBuf tracelen
1987 //Dbprintf("transfer to client parameters: %" PRIu32 " | %" PRIu32 " | %" PRIu32, startidx, numofbytes, packet->oldarg[2]);
1989 for (size_t i
= 0; i
< numofbytes
; i
+= PM3_CMD_DATA_SIZE
) {
1990 size_t len
= MIN((numofbytes
- i
), PM3_CMD_DATA_SIZE
);
1991 int result
= reply_old(CMD_DOWNLOADED_BIGBUF
, i
, len
, BigBuf_get_traceLen(), mem
+ startidx
+ i
, len
);
1992 if (result
!= PM3_SUCCESS
)
1993 Dbprintf("transfer to client failed :: | bytes between %d - %d (%d) | result: %d", i
, i
+ len
, len
, result
);
1995 // Trigger a finish downloading signal with an ACK frame
1996 // iceman, when did sending samplingconfig array got attached here?!?
1997 // arg0 = status of download transfer
2000 // asbytes = samplingconfig array
2001 reply_mix(CMD_ACK
, 1, 0, BigBuf_get_traceLen(), getSamplingConfig(), sizeof(sample_config
));
2006 case CMD_LF_UPLOAD_SIM_SAMPLES
: {
2007 // iceman; since changing fpga_bitstreams clears bigbuff, Its better to call it before.
2008 // to be able to use this one for uploading data to device
2015 uint8_t data
[PM3_CMD_DATA_SIZE
- sizeof(uint8_t) - sizeof(uint16_t)];
2017 struct p
*payload
= (struct p
*)packet
->data
.asBytes
;
2019 FpgaDownloadAndGo(FPGA_BITSTREAM_LF
);
2021 if ((payload
->flag
& 0x1) == 0x1) {
2022 BigBuf_Clear_ext(false);
2026 // offset should not be over buffer
2027 if (payload
->offset
>= BigBuf_get_size()) {
2028 reply_ng(CMD_LF_UPLOAD_SIM_SAMPLES
, PM3_EOVFLOW
, NULL
, 0);
2031 // ensure len bytes copied wont go past end of bigbuf
2032 uint16_t len
= MIN(BigBuf_get_size() - payload
->offset
, sizeof(payload
->data
));
2034 uint8_t *mem
= BigBuf_get_addr();
2036 memcpy(mem
+ payload
->offset
, &payload
->data
, len
);
2037 reply_ng(CMD_LF_UPLOAD_SIM_SAMPLES
, PM3_SUCCESS
, NULL
, 0);
2041 case CMD_DOWNLOAD_EML_BIGBUF
: {
2043 uint8_t *mem
= BigBuf_get_EM_addr();
2044 uint32_t startidx
= packet
->oldarg
[0];
2045 uint32_t numofbytes
= packet
->oldarg
[1];
2047 // arg0 = startindex
2048 // arg1 = length bytes to transfer
2051 for (size_t i
= 0; i
< numofbytes
; i
+= PM3_CMD_DATA_SIZE
) {
2052 size_t len
= MIN((numofbytes
- i
), PM3_CMD_DATA_SIZE
);
2053 int result
= reply_old(CMD_DOWNLOADED_EML_BIGBUF
, i
, len
, 0, mem
+ startidx
+ i
, len
);
2054 if (result
!= PM3_SUCCESS
)
2055 Dbprintf("transfer to client failed :: | bytes between %d - %d (%d) | result: %d", i
, i
+ len
, len
, result
);
2057 // Trigger a finish downloading signal with an ACK frame
2058 reply_mix(CMD_ACK
, 1, 0, 0, 0, 0);
2062 case CMD_READ_MEM
: {
2063 if (packet
->length
!= sizeof(uint32_t))
2065 ReadMem(packet
->data
.asDwords
[0]);
2069 case CMD_SPIFFS_TEST
: {
2073 case CMD_SPIFFS_CHECK
: {
2074 rdv40_spiffs_check();
2077 case CMD_SPIFFS_MOUNT
: {
2078 rdv40_spiffs_lazy_mount();
2081 case CMD_SPIFFS_UNMOUNT
: {
2082 rdv40_spiffs_lazy_unmount();
2085 case CMD_SPIFFS_PRINT_TREE
: {
2086 rdv40_spiffs_safe_print_tree();
2089 case CMD_SPIFFS_PRINT_FSINFO
: {
2090 rdv40_spiffs_safe_print_fsinfo();
2093 case CMD_SPIFFS_DOWNLOAD
: {
2095 uint8_t filename
[32];
2096 uint8_t *pfilename
= packet
->data
.asBytes
;
2097 memcpy(filename
, pfilename
, SPIFFS_OBJ_NAME_LEN
);
2098 if (DBGLEVEL
>= DBG_DEBUG
) Dbprintf("Filename received for spiffs dump : %s", filename
);
2100 uint32_t size
= packet
->oldarg
[1];
2102 uint8_t *buff
= BigBuf_malloc(size
);
2103 rdv40_spiffs_read_as_filetype((char *)filename
, (uint8_t *)buff
, size
, RDV40_SPIFFS_SAFETY_SAFE
);
2109 for (size_t i
= 0; i
< size
; i
+= PM3_CMD_DATA_SIZE
) {
2110 size_t len
= MIN((size
- i
), PM3_CMD_DATA_SIZE
);
2111 int result
= reply_old(CMD_SPIFFS_DOWNLOADED
, i
, len
, 0, buff
+ i
, len
);
2112 if (result
!= PM3_SUCCESS
)
2113 Dbprintf("transfer to client failed :: | bytes between %d - %d (%d) | result: %d", i
, i
+ len
, len
, result
);
2115 // Trigger a finish downloading signal with an ACK frame
2116 reply_ng(CMD_SPIFFS_DOWNLOAD
, PM3_SUCCESS
, NULL
, 0);
2120 case CMD_SPIFFS_STAT
: {
2122 uint8_t filename
[32];
2123 uint8_t *pfilename
= packet
->data
.asBytes
;
2124 memcpy(filename
, pfilename
, SPIFFS_OBJ_NAME_LEN
);
2125 if (DBGLEVEL
>= DBG_DEBUG
) {
2126 Dbprintf("Filename received for spiffs STAT : %s", filename
);
2129 int changed
= rdv40_spiffs_lazy_mount();
2130 uint32_t size
= size_in_spiffs((char *)filename
);
2132 rdv40_spiffs_lazy_unmount();
2135 reply_ng(CMD_SPIFFS_STAT
, PM3_SUCCESS
, (uint8_t *)&size
, sizeof(uint32_t));
2139 case CMD_SPIFFS_REMOVE
: {
2146 struct p
*payload
= (struct p
*) packet
->data
.asBytes
;
2148 if (DBGLEVEL
>= DBG_DEBUG
) {
2149 Dbprintf("Filename received for spiffs REMOVE : %s", payload
->fn
);
2152 rdv40_spiffs_remove((char *)payload
->fn
, RDV40_SPIFFS_SAFETY_SAFE
);
2153 reply_ng(CMD_SPIFFS_REMOVE
, PM3_SUCCESS
, NULL
, 0);
2157 case CMD_SPIFFS_RENAME
: {
2165 struct p
*payload
= (struct p
*) packet
->data
.asBytes
;
2167 if (DBGLEVEL
>= DBG_DEBUG
) {
2168 Dbprintf("SPIFFS RENAME");
2169 Dbprintf("Source........ %s", payload
->src
);
2170 Dbprintf("Destination... %s", payload
->dest
);
2172 rdv40_spiffs_rename((char *)payload
->src
, (char *)payload
->dest
, RDV40_SPIFFS_SAFETY_SAFE
);
2173 reply_ng(CMD_SPIFFS_RENAME
, PM3_SUCCESS
, NULL
, 0);
2177 case CMD_SPIFFS_COPY
: {
2185 struct p
*payload
= (struct p
*) packet
->data
.asBytes
;
2187 if (DBGLEVEL
>= DBG_DEBUG
) {
2188 Dbprintf("SPIFFS COPY");
2189 Dbprintf("Source........ %s", payload
->src
);
2190 Dbprintf("Destination... %s", payload
->dest
);
2192 rdv40_spiffs_copy((char *)payload
->src
, (char *)payload
->dest
, RDV40_SPIFFS_SAFETY_SAFE
);
2193 reply_ng(CMD_SPIFFS_COPY
, PM3_SUCCESS
, NULL
, 0);
2197 case CMD_SPIFFS_WRITE
: {
2200 flashmem_write_t
*payload
= (flashmem_write_t
*)packet
->data
.asBytes
;
2202 if (DBGLEVEL
>= DBG_DEBUG
) {
2203 Dbprintf("SPIFFS WRITE, dest `%s` with APPEND set to: %c", payload
->fn
, payload
->append
? 'Y' : 'N');
2206 if (payload
->append
) {
2207 rdv40_spiffs_append((char *) payload
->fn
, payload
->data
, payload
->bytes_in_packet
, RDV40_SPIFFS_SAFETY_SAFE
);
2209 rdv40_spiffs_write((char *) payload
->fn
, payload
->data
, payload
->bytes_in_packet
, RDV40_SPIFFS_SAFETY_SAFE
);
2212 reply_ng(CMD_SPIFFS_WRITE
, PM3_SUCCESS
, NULL
, 0);
2216 case CMD_SPIFFS_WIPE
: {
2218 rdv40_spiffs_safe_wipe();
2219 reply_ng(CMD_SPIFFS_WIPE
, PM3_SUCCESS
, NULL
, 0);
2223 case CMD_FLASHMEM_SET_SPIBAUDRATE
: {
2224 if (packet
->length
!= sizeof(uint32_t))
2226 FlashmemSetSpiBaudrate(packet
->data
.asDwords
[0]);
2229 case CMD_FLASHMEM_WRITE
: {
2232 flashmem_old_write_t
*payload
= (flashmem_old_write_t
*)packet
->data
.asBytes
;
2234 if (FlashInit() == false) {
2235 reply_ng(CMD_FLASHMEM_WRITE
, PM3_EIO
, NULL
, 0);
2240 if (payload
->startidx
== DEFAULT_T55XX_KEYS_OFFSET
) {
2241 Flash_CheckBusy(BUSY_TIMEOUT
);
2242 Flash_WriteEnable();
2243 Flash_Erase4k(3, 0xC);
2244 } else if (payload
->startidx
== DEFAULT_MF_KEYS_OFFSET
) {
2245 Flash_CheckBusy(BUSY_TIMEOUT
);
2246 Flash_WriteEnable();
2247 Flash_Erase4k(3, 0x9);
2248 Flash_CheckBusy(BUSY_TIMEOUT
);
2249 Flash_WriteEnable();
2250 Flash_Erase4k(3, 0xA);
2251 } else if (payload
->startidx
== DEFAULT_ICLASS_KEYS_OFFSET
) {
2252 Flash_CheckBusy(BUSY_TIMEOUT
);
2253 Flash_WriteEnable();
2254 Flash_Erase4k(3, 0xB);
2255 } else if (payload
->startidx
== FLASH_MEM_SIGNATURE_OFFSET
) {
2256 Flash_CheckBusy(BUSY_TIMEOUT
);
2257 Flash_WriteEnable();
2258 Flash_Erase4k(3, 0xF);
2261 uint16_t res
= Flash_Write(payload
->startidx
, payload
->data
, payload
->len
);
2263 reply_ng(CMD_FLASHMEM_WRITE
, (res
== payload
->len
) ? PM3_SUCCESS
: PM3_ESOFT
, NULL
, 0);
2267 case CMD_FLASHMEM_WIPE
: {
2269 uint8_t page
= packet
->oldarg
[0];
2270 uint8_t initalwipe
= packet
->oldarg
[1];
2273 isok
= Flash_WipeMemory();
2274 reply_mix(CMD_ACK
, isok
, 0, 0, 0, 0);
2279 isok
= Flash_WipeMemoryPage(page
);
2281 reply_mix(CMD_ACK
, isok
, 0, 0, 0, 0);
2285 case CMD_FLASHMEM_DOWNLOAD
: {
2288 uint8_t *mem
= BigBuf_malloc(PM3_CMD_DATA_SIZE
);
2289 uint32_t startidx
= packet
->oldarg
[0];
2290 uint32_t numofbytes
= packet
->oldarg
[1];
2291 // arg0 = startindex
2292 // arg1 = length bytes to transfer
2295 if (FlashInit() == false) {
2299 for (size_t i
= 0; i
< numofbytes
; i
+= PM3_CMD_DATA_SIZE
) {
2300 size_t len
= MIN((numofbytes
- i
), PM3_CMD_DATA_SIZE
);
2301 Flash_CheckBusy(BUSY_TIMEOUT
);
2302 bool isok
= Flash_ReadDataCont(startidx
+ i
, mem
, len
);
2304 Dbprintf("reading flash memory failed :: | bytes between %d - %d", i
, len
);
2306 isok
= reply_old(CMD_FLASHMEM_DOWNLOADED
, i
, len
, 0, mem
, len
);
2308 Dbprintf("transfer to client failed :: | bytes between %d - %d", i
, len
);
2312 reply_mix(CMD_ACK
, 1, 0, 0, 0, 0);
2317 case CMD_FLASHMEM_INFO
: {
2320 rdv40_validation_t
*info
= (rdv40_validation_t
*)BigBuf_malloc(sizeof(rdv40_validation_t
));
2322 bool isok
= Flash_ReadData(FLASH_MEM_SIGNATURE_OFFSET
, info
->signature
, FLASH_MEM_SIGNATURE_LEN
);
2325 Flash_UniqueID(info
->flashid
);
2328 reply_mix(CMD_ACK
, isok
, 0, 0, info
, sizeof(rdv40_validation_t
));
2335 case CMD_LF_SET_DIVISOR
: {
2336 FpgaDownloadAndGo(FPGA_BITSTREAM_LF
);
2337 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, packet
->data
.asBytes
[0]);
2340 case CMD_SET_ADC_MUX
: {
2341 switch (packet
->data
.asBytes
[0]) {
2343 SetAdcMuxFor(GPIO_MUXSEL_LOPKD
);
2346 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
2348 #ifndef WITH_FPC_USART
2350 SetAdcMuxFor(GPIO_MUXSEL_LORAW
);
2353 SetAdcMuxFor(GPIO_MUXSEL_HIRAW
);
2369 while ((AT91C_BASE_PMC
->PMC_MCFR
& AT91C_CKGR_MAINRDY
) == 0); // Wait for MAINF value to become available...
2370 uint16_t mainf
= AT91C_BASE_PMC
->PMC_MCFR
& AT91C_CKGR_MAINF
;
2371 Dbprintf(" Slow clock old measured value:.........%d Hz", (16 * MAINCK
) / mainf
);
2372 TimingIntervalAcquisition();
2374 while ((AT91C_BASE_PMC
->PMC_MCFR
& AT91C_CKGR_MAINRDY
) == 0); // Wait for MAINF value to become available...
2375 mainf
= AT91C_BASE_PMC
->PMC_MCFR
& AT91C_CKGR_MAINF
;
2376 Dbprintf(""); // first message gets lost
2377 Dbprintf(" Slow clock new measured value:.........%d Hz", (16 * MAINCK
) / mainf
);
2378 reply_ng(CMD_TIA
, PM3_SUCCESS
, NULL
, 0);
2381 case CMD_STANDALONE
: {
2382 uint8_t *bb
= BigBuf_get_EM_addr();
2383 bb
[0] = packet
->data
.asBytes
[0];
2387 case CMD_CAPABILITIES
: {
2392 reply_ng(CMD_PING
, PM3_SUCCESS
, packet
->data
.asBytes
, packet
->length
);
2396 case CMD_LCD_RESET
: {
2401 LCDSend(packet
->oldarg
[0]);
2405 case CMD_FINISH_WRITE
:
2406 case CMD_HARDWARE_RESET
: {
2409 // (iceman) why this wait?
2411 AT91C_BASE_RSTC
->RSTC_RCR
= RST_CONTROL_KEY
| AT91C_RSTC_PROCRST
;
2412 // We're going to reset, and the bootrom will take control.
2416 case CMD_START_FLASH
: {
2417 if (common_area
.flags
.bootrom_present
) {
2418 common_area
.command
= COMMON_AREA_COMMAND_ENTER_FLASH_MODE
;
2421 AT91C_BASE_RSTC
->RSTC_RCR
= RST_CONTROL_KEY
| AT91C_RSTC_PROCRST
;
2422 // We're going to flash, and the bootrom will take control.
2426 case CMD_DEVICE_INFO
: {
2427 uint32_t dev_info
= DEVICE_INFO_FLAG_OSIMAGE_PRESENT
| DEVICE_INFO_FLAG_CURRENT_MODE_OS
;
2428 if (common_area
.flags
.bootrom_present
) {
2429 dev_info
|= DEVICE_INFO_FLAG_BOOTROM_PRESENT
;
2431 reply_old(CMD_DEVICE_INFO
, dev_info
, 0, 0, 0, 0);
2435 Dbprintf("%s: 0x%04x", "unknown command:", packet
->cmd
);
2441 void __attribute__((noreturn
)) AppMain(void) {
2444 BigBuf_initialize();
2446 for (uint32_t *p
= _stack_start
; p
< _stack_end
- 0x200; ++p
) {
2452 // The FPGA gets its clock from us from PCK0 output, so set that up.
2453 AT91C_BASE_PIOA
->PIO_BSR
= GPIO_PCK0
;
2454 AT91C_BASE_PIOA
->PIO_PDR
= GPIO_PCK0
;
2455 AT91C_BASE_PMC
->PMC_SCER
|= AT91C_PMC_PCK0
;
2456 // PCK0 is PLL clock / 4 = 96MHz / 4 = 24MHz
2457 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
2458 AT91C_BASE_PIOA
->PIO_OER
= GPIO_PCK0
;
2461 AT91C_BASE_SPI
->SPI_CR
= AT91C_SPI_SWRST
;
2462 AT91C_BASE_SPI
->SPI_CR
= AT91C_SPI_SWRST
; // errata says it needs twice to be correctly set.
2465 AT91C_BASE_SSC
->SSC_CR
= AT91C_SSC_SWRST
;
2468 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
2470 // Load the FPGA image, which we have stored in our flash.
2471 // (the HF version by default)
2472 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
2480 #ifdef WITH_SMARTCARD
2484 #ifdef WITH_FPC_USART
2485 usart_init(USART_BAUD_RATE
, USART_PARITY
);
2488 // This is made as late as possible to ensure enumeration without timeout
2489 // against device such as http://www.hobbytronics.co.uk/usb-host-board-v2
2492 allow_send_wtx
= true;
2495 // If flash is not present, BUSY_TIMEOUT kicks in, let's do it after USB
2499 // Enforce a spiffs check/garbage collection at boot so we are likely to never
2500 // fall under the 2 contigous free blocks availables
2501 rdv40_spiffs_check();
2507 if (*_stack_start
!= 0xdeadbeef) {
2508 Dbprintf("Stack overflow detected! Please increase stack size, currently %d bytes", (uint32_t)_stack_end
- (uint32_t)_stack_start
);
2509 Dbprintf("Unplug your device now.");
2513 // Check if there is a packet available
2515 memset(&rx
.data
, 0, sizeof(rx
.data
));
2517 int ret
= receive_ng(&rx
);
2518 if (ret
== PM3_SUCCESS
) {
2519 PacketReceived(&rx
);
2520 } else if (ret
!= PM3_ENODATA
) {
2522 Dbprintf("Error in frame reception: %d %s", ret
, (ret
== PM3_EIO
) ? "PM3_EIO" : "");
2523 // TODO if error, shall we resync ?
2526 // Press button for one second to enter a possible standalone mode
2527 button_status
= BUTTON_HELD(1000);
2528 if (button_status
== BUTTON_HOLD
) {
2530 * So this is the trigger to execute a standalone mod. Generic entrypoint by following the standalone/standalone.h headerfile
2531 * All standalone mod "main loop" should be the RunMod() function.
2533 allow_send_wtx
= false;
2535 allow_send_wtx
= true;