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_PERSONALIZE_UID
: {
1574 uint8_t pers_option
;
1577 struct p
*payload
= (struct p
*) packet
->data
.asBytes
;
1578 uint64_t authkey
= bytes_to_num(payload
->key
, 6);
1579 MifarePersonalizeUID(payload
->keytype
, payload
->pers_option
, authkey
);
1582 case CMD_HF_MIFARE_SETMOD
: {
1583 MifareSetMod(packet
->data
.asBytes
);
1587 case CMD_HF_DESFIRE_READBL
: {
1590 case CMD_HF_DESFIRE_WRITEBL
: {
1593 case CMD_HF_DESFIRE_AUTH1
: {
1594 MifareDES_Auth1(packet
->data
.asBytes
);
1597 case CMD_HF_DESFIRE_AUTH2
: {
1598 //MifareDES_Auth2(packet->oldarg[0],packet->data.asBytes);
1601 case CMD_HF_DESFIRE_READER
: {
1602 //readermifaredes(packet->oldarg[0], packet->oldarg[1], packet->data.asBytes);
1605 case CMD_HF_DESFIRE_INFO
: {
1606 MifareDesfireGetInformation();
1609 case CMD_HF_DESFIRE_COMMAND
: {
1610 MifareSendCommand(packet
->data
.asBytes
);
1613 case CMD_HF_MIFARE_NACK_DETECT
: {
1617 case CMD_HF_MFU_OTP_TEAROFF
: {
1618 MifareU_Otp_Tearoff(packet
->oldarg
[0], packet
->oldarg
[1], packet
->data
.asBytes
);
1621 case CMD_HF_MFU_COUNTER_TEAROFF
: {
1624 uint32_t tearoff_time
;
1627 struct p
*payload
= (struct p
*) packet
->data
.asBytes
;
1628 MifareU_Counter_Tearoff(payload
->counter
, payload
->tearoff_time
, payload
->value
);
1631 case CMD_HF_MIFARE_STATIC_NONCE
: {
1632 MifareHasStaticNonce();
1637 #ifdef WITH_NFCBARCODE
1638 case CMD_HF_THINFILM_READ
: {
1642 case CMD_HF_THINFILM_SIMULATE
: {
1643 SimulateThinFilm(packet
->data
.asBytes
, packet
->length
);
1649 // Makes use of ISO14443a FPGA Firmware
1650 case CMD_HF_ICLASS_SNIFF
: {
1652 uint8_t jam_search_len
;
1653 uint8_t jam_search_string
[];
1655 struct p
*payload
= (struct p
*) packet
->data
.asBytes
;
1656 SniffIClass(payload
->jam_search_len
, payload
->jam_search_string
);
1657 reply_ng(CMD_HF_ICLASS_SNIFF
, PM3_SUCCESS
, NULL
, 0);
1660 case CMD_HF_ICLASS_SIMULATE
: {
1666 struct p *payload = (struct p *) packet->data.asBytes;
1669 SimulateIClass(packet
->oldarg
[0], packet
->oldarg
[1], packet
->oldarg
[2], packet
->data
.asBytes
);
1672 case CMD_HF_ICLASS_READER
: {
1673 ReaderIClass(packet
->oldarg
[0]);
1676 case CMD_HF_ICLASS_EML_MEMSET
: {
1677 //iceman, should call FPGADOWNLOAD before, since it corrupts BigBuf
1678 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
1684 struct p
*payload
= (struct p
*) packet
->data
.asBytes
;
1685 emlSet(payload
->data
, payload
->offset
, payload
->len
);
1688 case CMD_HF_ICLASS_WRITEBL
: {
1689 iClass_WriteBlock(packet
->data
.asBytes
);
1692 case CMD_HF_ICLASS_READBL
: {
1693 iClass_ReadBlock(packet
->data
.asBytes
);
1696 case CMD_HF_ICLASS_CHKKEYS
: {
1697 iClass_Authentication_fast((iclass_chk_t
*)packet
->data
.asBytes
);
1700 case CMD_HF_ICLASS_DUMP
: {
1701 iClass_Dump(packet
->data
.asBytes
);
1704 case CMD_HF_ICLASS_RESTORE
: {
1705 iClass_Restore((iclass_restore_req_t
*)packet
->data
.asBytes
);
1711 case CMD_HF_SNIFF
: {
1713 uint32_t samplesToSkip
;
1714 uint32_t triggersToSkip
;
1716 struct p
*payload
= (struct p
*)packet
->data
.asBytes
;
1719 int res
= HfSniff(payload
->samplesToSkip
, payload
->triggersToSkip
, &len
);
1725 reply_ng(CMD_HF_SNIFF
, res
, (uint8_t *)&retval
, sizeof(retval
));
1731 case CMD_FPGAMEM_DOWNLOAD
: {
1737 #ifdef WITH_SMARTCARD
1738 case CMD_SMART_ATR
: {
1742 case CMD_SMART_SETBAUD
: {
1743 SmartCardSetBaud(packet
->oldarg
[0]);
1746 case CMD_SMART_SETCLOCK
: {
1750 struct p
*payload
= (struct p
*)packet
->data
.asBytes
;
1751 SmartCardSetClock(payload
->new_clk
);
1754 case CMD_SMART_RAW
: {
1755 SmartCardRaw((smart_card_raw_t
*)packet
->data
.asBytes
);
1758 case CMD_SMART_UPLOAD
: {
1759 // upload file from client
1762 uint32_t bytes_in_packet
;
1766 struct p
*payload
= (struct p
*)packet
->data
.asBytes
;
1767 uint8_t *mem
= BigBuf_get_addr();
1768 memcpy(mem
+ payload
->idx
, payload
->data
, payload
->bytes_in_packet
);
1770 uint8_t a
= 0, b
= 0;
1771 compute_crc(CRC_14443_A
, mem
+ payload
->idx
, payload
->bytes_in_packet
, &a
, &b
);
1772 int res
= PM3_SUCCESS
;
1773 if (payload
->crc
!= (a
<< 8 | b
)) {
1774 DbpString("CRC Failed");
1777 reply_ng(CMD_SMART_UPLOAD
, res
, NULL
, 0);
1780 case CMD_SMART_UPGRADE
: {
1785 struct p
*payload
= (struct p
*)packet
->data
.asBytes
;
1787 uint8_t *fwdata
= BigBuf_get_addr();
1788 uint8_t a
= 0, b
= 0;
1789 compute_crc(CRC_14443_A
, fwdata
, payload
->fw_size
, &a
, &b
);
1791 if (payload
->crc
!= (a
<< 8 | b
)) {
1792 Dbprintf("CRC Failed, 0x[%04x] != 0x[%02x%02x]", payload
->crc
, a
, b
);
1793 reply_ng(CMD_SMART_UPGRADE
, PM3_ESOFT
, NULL
, 0);
1795 SmartCardUpgrade(payload
->fw_size
);
1802 #ifdef WITH_FPC_USART
1803 case CMD_USART_TX
: {
1805 usart_writebuffer_sync(packet
->data
.asBytes
, packet
->length
);
1806 reply_ng(CMD_USART_TX
, PM3_SUCCESS
, NULL
, 0);
1810 case CMD_USART_RX
: {
1815 struct p
*payload
= (struct p
*) &packet
->data
.asBytes
;
1817 uint16_t pre_available
= 0;
1818 uint8_t *dest
= BigBuf_malloc(USART_FIFOLEN
);
1819 uint32_t wait
= payload
->waittime
;
1820 uint32_t ti
= GetTickCount();
1823 available
= usart_rxdata_available();
1824 if (available
> pre_available
) {
1825 // When receiving data, reset timer and shorten timeout
1826 ti
= GetTickCount();
1828 pre_available
= available
;
1831 // We stop either after waittime if no data or 50ms after last data received
1832 if (GetTickCountDelta(ti
) > wait
)
1835 if (available
> 0) {
1836 uint16_t len
= usart_read_ng(dest
, available
);
1837 reply_ng(CMD_USART_RX
, PM3_SUCCESS
, dest
, len
);
1839 reply_ng(CMD_USART_RX
, PM3_ENODATA
, NULL
, 0);
1845 case CMD_USART_TXRX
: {
1851 struct p
*payload
= (struct p
*) &packet
->data
.asBytes
;
1852 usart_writebuffer_sync(payload
->data
, packet
->length
- sizeof(payload
));
1854 uint16_t pre_available
= 0;
1855 uint8_t *dest
= BigBuf_malloc(USART_FIFOLEN
);
1856 uint32_t wait
= payload
->waittime
;
1857 uint32_t ti
= GetTickCount();
1860 available
= usart_rxdata_available();
1861 if (available
> pre_available
) {
1862 // When receiving data, reset timer and shorten timeout
1863 ti
= GetTickCount();
1865 pre_available
= available
;
1868 // We stop either after waittime if no data or 50ms after last data received
1869 if (GetTickCountDelta(ti
) > wait
)
1872 if (available
> 0) {
1873 uint16_t len
= usart_read_ng(dest
, available
);
1874 reply_ng(CMD_USART_TXRX
, PM3_SUCCESS
, dest
, len
);
1876 reply_ng(CMD_USART_TXRX
, PM3_ENODATA
, NULL
, 0);
1882 case CMD_USART_CONFIG
: {
1887 struct p
*payload
= (struct p
*) &packet
->data
.asBytes
;
1888 usart_init(payload
->baudrate
, payload
->parity
);
1889 reply_ng(CMD_USART_CONFIG
, PM3_SUCCESS
, NULL
, 0);
1893 case CMD_BUFF_CLEAR
: {
1898 case CMD_MEASURE_ANTENNA_TUNING
: {
1899 MeasureAntennaTuning();
1902 case CMD_MEASURE_ANTENNA_TUNING_HF
: {
1903 if (packet
->length
!= 1)
1904 reply_ng(CMD_MEASURE_ANTENNA_TUNING_HF
, PM3_EINVARG
, NULL
, 0);
1906 switch (packet
->data
.asBytes
[0]) {
1907 case 1: // MEASURE_ANTENNA_TUNING_HF_START
1908 // Let the FPGA drive the high-frequency antenna around 13.56 MHz.
1909 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
1910 FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER
);
1911 reply_ng(CMD_MEASURE_ANTENNA_TUNING_HF
, PM3_SUCCESS
, NULL
, 0);
1914 if (button_status
== BUTTON_SINGLE_CLICK
)
1915 reply_ng(CMD_MEASURE_ANTENNA_TUNING_HF
, PM3_EOPABORTED
, NULL
, 0);
1916 uint16_t volt
= MeasureAntennaTuningHfData();
1917 reply_ng(CMD_MEASURE_ANTENNA_TUNING_HF
, PM3_SUCCESS
, (uint8_t *)&volt
, sizeof(volt
));
1920 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1921 reply_ng(CMD_MEASURE_ANTENNA_TUNING_HF
, PM3_SUCCESS
, NULL
, 0);
1924 reply_ng(CMD_MEASURE_ANTENNA_TUNING_HF
, PM3_EINVARG
, NULL
, 0);
1929 case CMD_MEASURE_ANTENNA_TUNING_LF
: {
1930 if (packet
->length
!= 2)
1931 reply_ng(CMD_MEASURE_ANTENNA_TUNING_LF
, PM3_EINVARG
, NULL
, 0);
1933 switch (packet
->data
.asBytes
[0]) {
1934 case 1: // MEASURE_ANTENNA_TUNING_LF_START
1935 // Let the FPGA drive the low-frequency antenna around 125kHz
1936 FpgaDownloadAndGo(FPGA_BITSTREAM_LF
);
1937 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_READER
| FPGA_LF_ADC_READER_FIELD
);
1938 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, packet
->data
.asBytes
[1]);
1939 reply_ng(CMD_MEASURE_ANTENNA_TUNING_LF
, PM3_SUCCESS
, NULL
, 0);
1942 if (button_status
== BUTTON_SINGLE_CLICK
)
1943 reply_ng(CMD_MEASURE_ANTENNA_TUNING_LF
, PM3_EOPABORTED
, NULL
, 0);
1945 uint32_t volt
= MeasureAntennaTuningLfData();
1946 reply_ng(CMD_MEASURE_ANTENNA_TUNING_LF
, PM3_SUCCESS
, (uint8_t *)&volt
, sizeof(volt
));
1949 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1950 reply_ng(CMD_MEASURE_ANTENNA_TUNING_LF
, PM3_SUCCESS
, NULL
, 0);
1953 reply_ng(CMD_MEASURE_ANTENNA_TUNING_LF
, PM3_EINVARG
, NULL
, 0);
1958 case CMD_LISTEN_READER_FIELD
: {
1959 if (packet
->length
!= sizeof(uint8_t))
1961 ListenReaderField(packet
->data
.asBytes
[0]);
1964 case CMD_FPGA_MAJOR_MODE_OFF
: { // ## FPGA Control
1965 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF
);
1967 LED_D_OFF(); // LED D indicates field ON or OFF
1970 case CMD_DOWNLOAD_BIGBUF
: {
1972 uint8_t *mem
= BigBuf_get_addr();
1973 uint32_t startidx
= packet
->oldarg
[0];
1974 uint32_t numofbytes
= packet
->oldarg
[1];
1976 // arg0 = startindex
1977 // arg1 = length bytes to transfer
1978 // arg2 = BigBuf tracelen
1979 //Dbprintf("transfer to client parameters: %" PRIu32 " | %" PRIu32 " | %" PRIu32, startidx, numofbytes, packet->oldarg[2]);
1981 for (size_t i
= 0; i
< numofbytes
; i
+= PM3_CMD_DATA_SIZE
) {
1982 size_t len
= MIN((numofbytes
- i
), PM3_CMD_DATA_SIZE
);
1983 int result
= reply_old(CMD_DOWNLOADED_BIGBUF
, i
, len
, BigBuf_get_traceLen(), mem
+ startidx
+ i
, len
);
1984 if (result
!= PM3_SUCCESS
)
1985 Dbprintf("transfer to client failed :: | bytes between %d - %d (%d) | result: %d", i
, i
+ len
, len
, result
);
1987 // Trigger a finish downloading signal with an ACK frame
1988 // iceman, when did sending samplingconfig array got attached here?!?
1989 // arg0 = status of download transfer
1992 // asbytes = samplingconfig array
1993 reply_mix(CMD_ACK
, 1, 0, BigBuf_get_traceLen(), getSamplingConfig(), sizeof(sample_config
));
1998 case CMD_LF_UPLOAD_SIM_SAMPLES
: {
1999 // iceman; since changing fpga_bitstreams clears bigbuff, Its better to call it before.
2000 // to be able to use this one for uploading data to device
2007 uint8_t data
[PM3_CMD_DATA_SIZE
- sizeof(uint8_t) - sizeof(uint16_t)];
2009 struct p
*payload
= (struct p
*)packet
->data
.asBytes
;
2011 FpgaDownloadAndGo(FPGA_BITSTREAM_LF
);
2013 if ((payload
->flag
& 0x1) == 0x1) {
2014 BigBuf_Clear_ext(false);
2018 // offset should not be over buffer
2019 if (payload
->offset
>= BigBuf_get_size()) {
2020 reply_ng(CMD_LF_UPLOAD_SIM_SAMPLES
, PM3_EOVFLOW
, NULL
, 0);
2023 // ensure len bytes copied wont go past end of bigbuf
2024 uint16_t len
= MIN(BigBuf_get_size() - payload
->offset
, sizeof(payload
->data
));
2026 uint8_t *mem
= BigBuf_get_addr();
2028 memcpy(mem
+ payload
->offset
, &payload
->data
, len
);
2029 reply_ng(CMD_LF_UPLOAD_SIM_SAMPLES
, PM3_SUCCESS
, NULL
, 0);
2033 case CMD_DOWNLOAD_EML_BIGBUF
: {
2035 uint8_t *mem
= BigBuf_get_EM_addr();
2036 uint32_t startidx
= packet
->oldarg
[0];
2037 uint32_t numofbytes
= packet
->oldarg
[1];
2039 // arg0 = startindex
2040 // arg1 = length bytes to transfer
2043 for (size_t i
= 0; i
< numofbytes
; i
+= PM3_CMD_DATA_SIZE
) {
2044 size_t len
= MIN((numofbytes
- i
), PM3_CMD_DATA_SIZE
);
2045 int result
= reply_old(CMD_DOWNLOADED_EML_BIGBUF
, i
, len
, 0, mem
+ startidx
+ i
, len
);
2046 if (result
!= PM3_SUCCESS
)
2047 Dbprintf("transfer to client failed :: | bytes between %d - %d (%d) | result: %d", i
, i
+ len
, len
, result
);
2049 // Trigger a finish downloading signal with an ACK frame
2050 reply_mix(CMD_ACK
, 1, 0, 0, 0, 0);
2054 case CMD_READ_MEM
: {
2055 if (packet
->length
!= sizeof(uint32_t))
2057 ReadMem(packet
->data
.asDwords
[0]);
2061 case CMD_SPIFFS_TEST
: {
2065 case CMD_SPIFFS_CHECK
: {
2066 rdv40_spiffs_check();
2069 case CMD_SPIFFS_MOUNT
: {
2070 rdv40_spiffs_lazy_mount();
2073 case CMD_SPIFFS_UNMOUNT
: {
2074 rdv40_spiffs_lazy_unmount();
2077 case CMD_SPIFFS_PRINT_TREE
: {
2078 rdv40_spiffs_safe_print_tree();
2081 case CMD_SPIFFS_PRINT_FSINFO
: {
2082 rdv40_spiffs_safe_print_fsinfo();
2085 case CMD_SPIFFS_DOWNLOAD
: {
2087 uint8_t filename
[32];
2088 uint8_t *pfilename
= packet
->data
.asBytes
;
2089 memcpy(filename
, pfilename
, SPIFFS_OBJ_NAME_LEN
);
2090 if (DBGLEVEL
>= DBG_DEBUG
) Dbprintf("Filename received for spiffs dump : %s", filename
);
2092 uint32_t size
= packet
->oldarg
[1];
2094 uint8_t *buff
= BigBuf_malloc(size
);
2095 rdv40_spiffs_read_as_filetype((char *)filename
, (uint8_t *)buff
, size
, RDV40_SPIFFS_SAFETY_SAFE
);
2101 for (size_t i
= 0; i
< size
; i
+= PM3_CMD_DATA_SIZE
) {
2102 size_t len
= MIN((size
- i
), PM3_CMD_DATA_SIZE
);
2103 int result
= reply_old(CMD_SPIFFS_DOWNLOADED
, i
, len
, 0, buff
+ i
, len
);
2104 if (result
!= PM3_SUCCESS
)
2105 Dbprintf("transfer to client failed :: | bytes between %d - %d (%d) | result: %d", i
, i
+ len
, len
, result
);
2107 // Trigger a finish downloading signal with an ACK frame
2108 reply_ng(CMD_SPIFFS_DOWNLOAD
, PM3_SUCCESS
, NULL
, 0);
2112 case CMD_SPIFFS_STAT
: {
2114 uint8_t filename
[32];
2115 uint8_t *pfilename
= packet
->data
.asBytes
;
2116 memcpy(filename
, pfilename
, SPIFFS_OBJ_NAME_LEN
);
2117 if (DBGLEVEL
>= DBG_DEBUG
) {
2118 Dbprintf("Filename received for spiffs STAT : %s", filename
);
2121 int changed
= rdv40_spiffs_lazy_mount();
2122 uint32_t size
= size_in_spiffs((char *)filename
);
2124 rdv40_spiffs_lazy_unmount();
2127 reply_ng(CMD_SPIFFS_STAT
, PM3_SUCCESS
, (uint8_t *)&size
, sizeof(uint32_t));
2131 case CMD_SPIFFS_REMOVE
: {
2138 struct p
*payload
= (struct p
*) packet
->data
.asBytes
;
2140 if (DBGLEVEL
>= DBG_DEBUG
) {
2141 Dbprintf("Filename received for spiffs REMOVE : %s", payload
->fn
);
2144 rdv40_spiffs_remove((char *)payload
->fn
, RDV40_SPIFFS_SAFETY_SAFE
);
2145 reply_ng(CMD_SPIFFS_REMOVE
, PM3_SUCCESS
, NULL
, 0);
2149 case CMD_SPIFFS_RENAME
: {
2157 struct p
*payload
= (struct p
*) packet
->data
.asBytes
;
2159 if (DBGLEVEL
>= DBG_DEBUG
) {
2160 Dbprintf("SPIFFS RENAME");
2161 Dbprintf("Source........ %s", payload
->src
);
2162 Dbprintf("Destination... %s", payload
->dest
);
2164 rdv40_spiffs_rename((char *)payload
->src
, (char *)payload
->dest
, RDV40_SPIFFS_SAFETY_SAFE
);
2165 reply_ng(CMD_SPIFFS_RENAME
, PM3_SUCCESS
, NULL
, 0);
2169 case CMD_SPIFFS_COPY
: {
2177 struct p
*payload
= (struct p
*) packet
->data
.asBytes
;
2179 if (DBGLEVEL
>= DBG_DEBUG
) {
2180 Dbprintf("SPIFFS COPY");
2181 Dbprintf("Source........ %s", payload
->src
);
2182 Dbprintf("Destination... %s", payload
->dest
);
2184 rdv40_spiffs_copy((char *)payload
->src
, (char *)payload
->dest
, RDV40_SPIFFS_SAFETY_SAFE
);
2185 reply_ng(CMD_SPIFFS_COPY
, PM3_SUCCESS
, NULL
, 0);
2189 case CMD_SPIFFS_WRITE
: {
2192 flashmem_write_t
*payload
= (flashmem_write_t
*)packet
->data
.asBytes
;
2194 if (DBGLEVEL
>= DBG_DEBUG
) {
2195 Dbprintf("SPIFFS WRITE, dest `%s` with APPEND set to: %c", payload
->fn
, payload
->append
? 'Y' : 'N');
2198 if (payload
->append
) {
2199 rdv40_spiffs_append((char *) payload
->fn
, payload
->data
, payload
->bytes_in_packet
, RDV40_SPIFFS_SAFETY_SAFE
);
2201 rdv40_spiffs_write((char *) payload
->fn
, payload
->data
, payload
->bytes_in_packet
, RDV40_SPIFFS_SAFETY_SAFE
);
2204 reply_ng(CMD_SPIFFS_WRITE
, PM3_SUCCESS
, NULL
, 0);
2208 case CMD_SPIFFS_WIPE
: {
2210 rdv40_spiffs_safe_wipe();
2211 reply_ng(CMD_SPIFFS_WIPE
, PM3_SUCCESS
, NULL
, 0);
2215 case CMD_FLASHMEM_SET_SPIBAUDRATE
: {
2216 if (packet
->length
!= sizeof(uint32_t))
2218 FlashmemSetSpiBaudrate(packet
->data
.asDwords
[0]);
2221 case CMD_FLASHMEM_WRITE
: {
2224 flashmem_old_write_t
*payload
= (flashmem_old_write_t
*)packet
->data
.asBytes
;
2226 if (FlashInit() == false) {
2227 reply_ng(CMD_FLASHMEM_WRITE
, PM3_EIO
, NULL
, 0);
2232 if (payload
->startidx
== DEFAULT_T55XX_KEYS_OFFSET
) {
2233 Flash_CheckBusy(BUSY_TIMEOUT
);
2234 Flash_WriteEnable();
2235 Flash_Erase4k(3, 0xC);
2236 } else if (payload
->startidx
== DEFAULT_MF_KEYS_OFFSET
) {
2237 Flash_CheckBusy(BUSY_TIMEOUT
);
2238 Flash_WriteEnable();
2239 Flash_Erase4k(3, 0x9);
2240 Flash_CheckBusy(BUSY_TIMEOUT
);
2241 Flash_WriteEnable();
2242 Flash_Erase4k(3, 0xA);
2243 } else if (payload
->startidx
== DEFAULT_ICLASS_KEYS_OFFSET
) {
2244 Flash_CheckBusy(BUSY_TIMEOUT
);
2245 Flash_WriteEnable();
2246 Flash_Erase4k(3, 0xB);
2247 } else if (payload
->startidx
== FLASH_MEM_SIGNATURE_OFFSET
) {
2248 Flash_CheckBusy(BUSY_TIMEOUT
);
2249 Flash_WriteEnable();
2250 Flash_Erase4k(3, 0xF);
2253 uint16_t res
= Flash_Write(payload
->startidx
, payload
->data
, payload
->len
);
2255 reply_ng(CMD_FLASHMEM_WRITE
, (res
== payload
->len
) ? PM3_SUCCESS
: PM3_ESOFT
, NULL
, 0);
2259 case CMD_FLASHMEM_WIPE
: {
2261 uint8_t page
= packet
->oldarg
[0];
2262 uint8_t initalwipe
= packet
->oldarg
[1];
2265 isok
= Flash_WipeMemory();
2266 reply_mix(CMD_ACK
, isok
, 0, 0, 0, 0);
2271 isok
= Flash_WipeMemoryPage(page
);
2273 reply_mix(CMD_ACK
, isok
, 0, 0, 0, 0);
2277 case CMD_FLASHMEM_DOWNLOAD
: {
2280 uint8_t *mem
= BigBuf_malloc(PM3_CMD_DATA_SIZE
);
2281 uint32_t startidx
= packet
->oldarg
[0];
2282 uint32_t numofbytes
= packet
->oldarg
[1];
2283 // arg0 = startindex
2284 // arg1 = length bytes to transfer
2287 if (FlashInit() == false) {
2291 for (size_t i
= 0; i
< numofbytes
; i
+= PM3_CMD_DATA_SIZE
) {
2292 size_t len
= MIN((numofbytes
- i
), PM3_CMD_DATA_SIZE
);
2293 Flash_CheckBusy(BUSY_TIMEOUT
);
2294 bool isok
= Flash_ReadDataCont(startidx
+ i
, mem
, len
);
2296 Dbprintf("reading flash memory failed :: | bytes between %d - %d", i
, len
);
2298 isok
= reply_old(CMD_FLASHMEM_DOWNLOADED
, i
, len
, 0, mem
, len
);
2300 Dbprintf("transfer to client failed :: | bytes between %d - %d", i
, len
);
2304 reply_mix(CMD_ACK
, 1, 0, 0, 0, 0);
2309 case CMD_FLASHMEM_INFO
: {
2312 rdv40_validation_t
*info
= (rdv40_validation_t
*)BigBuf_malloc(sizeof(rdv40_validation_t
));
2314 bool isok
= Flash_ReadData(FLASH_MEM_SIGNATURE_OFFSET
, info
->signature
, FLASH_MEM_SIGNATURE_LEN
);
2317 Flash_UniqueID(info
->flashid
);
2320 reply_mix(CMD_ACK
, isok
, 0, 0, info
, sizeof(rdv40_validation_t
));
2327 case CMD_LF_SET_DIVISOR
: {
2328 FpgaDownloadAndGo(FPGA_BITSTREAM_LF
);
2329 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, packet
->data
.asBytes
[0]);
2332 case CMD_SET_ADC_MUX
: {
2333 switch (packet
->data
.asBytes
[0]) {
2335 SetAdcMuxFor(GPIO_MUXSEL_LOPKD
);
2338 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
2340 #ifndef WITH_FPC_USART
2342 SetAdcMuxFor(GPIO_MUXSEL_LORAW
);
2345 SetAdcMuxFor(GPIO_MUXSEL_HIRAW
);
2361 while ((AT91C_BASE_PMC
->PMC_MCFR
& AT91C_CKGR_MAINRDY
) == 0); // Wait for MAINF value to become available...
2362 uint16_t mainf
= AT91C_BASE_PMC
->PMC_MCFR
& AT91C_CKGR_MAINF
;
2363 Dbprintf(" Slow clock old measured value:.........%d Hz", (16 * MAINCK
) / mainf
);
2364 TimingIntervalAcquisition();
2366 while ((AT91C_BASE_PMC
->PMC_MCFR
& AT91C_CKGR_MAINRDY
) == 0); // Wait for MAINF value to become available...
2367 mainf
= AT91C_BASE_PMC
->PMC_MCFR
& AT91C_CKGR_MAINF
;
2368 Dbprintf(""); // first message gets lost
2369 Dbprintf(" Slow clock new measured value:.........%d Hz", (16 * MAINCK
) / mainf
);
2370 reply_ng(CMD_TIA
, PM3_SUCCESS
, NULL
, 0);
2373 case CMD_STANDALONE
: {
2374 uint8_t *bb
= BigBuf_get_EM_addr();
2375 bb
[0] = packet
->data
.asBytes
[0];
2379 case CMD_CAPABILITIES
: {
2384 reply_ng(CMD_PING
, PM3_SUCCESS
, packet
->data
.asBytes
, packet
->length
);
2388 case CMD_LCD_RESET
: {
2393 LCDSend(packet
->oldarg
[0]);
2397 case CMD_FINISH_WRITE
:
2398 case CMD_HARDWARE_RESET
: {
2401 // (iceman) why this wait?
2403 AT91C_BASE_RSTC
->RSTC_RCR
= RST_CONTROL_KEY
| AT91C_RSTC_PROCRST
;
2404 // We're going to reset, and the bootrom will take control.
2408 case CMD_START_FLASH
: {
2409 if (common_area
.flags
.bootrom_present
) {
2410 common_area
.command
= COMMON_AREA_COMMAND_ENTER_FLASH_MODE
;
2413 AT91C_BASE_RSTC
->RSTC_RCR
= RST_CONTROL_KEY
| AT91C_RSTC_PROCRST
;
2414 // We're going to flash, and the bootrom will take control.
2418 case CMD_DEVICE_INFO
: {
2419 uint32_t dev_info
= DEVICE_INFO_FLAG_OSIMAGE_PRESENT
| DEVICE_INFO_FLAG_CURRENT_MODE_OS
;
2420 if (common_area
.flags
.bootrom_present
) {
2421 dev_info
|= DEVICE_INFO_FLAG_BOOTROM_PRESENT
;
2423 reply_old(CMD_DEVICE_INFO
, dev_info
, 0, 0, 0, 0);
2427 Dbprintf("%s: 0x%04x", "unknown command:", packet
->cmd
);
2433 void __attribute__((noreturn
)) AppMain(void) {
2436 BigBuf_initialize();
2438 for (uint32_t *p
= _stack_start
; p
< _stack_end
- 0x200; ++p
) {
2444 // The FPGA gets its clock from us from PCK0 output, so set that up.
2445 AT91C_BASE_PIOA
->PIO_BSR
= GPIO_PCK0
;
2446 AT91C_BASE_PIOA
->PIO_PDR
= GPIO_PCK0
;
2447 AT91C_BASE_PMC
->PMC_SCER
|= AT91C_PMC_PCK0
;
2448 // PCK0 is PLL clock / 4 = 96MHz / 4 = 24MHz
2449 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
2450 AT91C_BASE_PIOA
->PIO_OER
= GPIO_PCK0
;
2453 AT91C_BASE_SPI
->SPI_CR
= AT91C_SPI_SWRST
;
2454 AT91C_BASE_SPI
->SPI_CR
= AT91C_SPI_SWRST
; // errata says it needs twice to be correctly set.
2457 AT91C_BASE_SSC
->SSC_CR
= AT91C_SSC_SWRST
;
2460 SetAdcMuxFor(GPIO_MUXSEL_HIPKD
);
2462 // Load the FPGA image, which we have stored in our flash.
2463 // (the HF version by default)
2464 FpgaDownloadAndGo(FPGA_BITSTREAM_HF
);
2472 #ifdef WITH_SMARTCARD
2476 #ifdef WITH_FPC_USART
2477 usart_init(USART_BAUD_RATE
, USART_PARITY
);
2480 // This is made as late as possible to ensure enumeration without timeout
2481 // against device such as http://www.hobbytronics.co.uk/usb-host-board-v2
2484 allow_send_wtx
= true;
2487 // If flash is not present, BUSY_TIMEOUT kicks in, let's do it after USB
2491 // Enforce a spiffs check/garbage collection at boot so we are likely to never
2492 // fall under the 2 contigous free blocks availables
2493 rdv40_spiffs_check();
2499 if (*_stack_start
!= 0xdeadbeef) {
2500 Dbprintf("Stack overflow detected! Please increase stack size, currently %d bytes", (uint32_t)_stack_end
- (uint32_t)_stack_start
);
2501 Dbprintf("Unplug your device now.");
2505 // Check if there is a packet available
2507 memset(&rx
.data
, 0, sizeof(rx
.data
));
2509 int ret
= receive_ng(&rx
);
2510 if (ret
== PM3_SUCCESS
) {
2511 PacketReceived(&rx
);
2512 } else if (ret
!= PM3_ENODATA
) {
2514 Dbprintf("Error in frame reception: %d %s", ret
, (ret
== PM3_EIO
) ? "PM3_EIO" : "");
2515 // TODO if error, shall we resync ?
2518 // Press button for one second to enter a possible standalone mode
2519 button_status
= BUTTON_HELD(1000);
2520 if (button_status
== BUTTON_HOLD
) {
2522 * So this is the trigger to execute a standalone mod. Generic entrypoint by following the standalone/standalone.h headerfile
2523 * All standalone mod "main loop" should be the RunMod() function.
2525 allow_send_wtx
= false;
2527 allow_send_wtx
= true;