1 //-----------------------------------------------------------------------------
2 // Copyright (C) Proxmark3 contributors. See AUTHORS.md for details.
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // See LICENSE.txt for the text of the license.
15 //-----------------------------------------------------------------------------
16 // Low frequency EM4x50 commands
17 //-----------------------------------------------------------------------------
19 #include "fpgaloader.h"
22 #include "lfsampling.h"
25 #include "commonutil.h"
29 #include "appmain.h" // tear
30 #include "bruteforce.h"
32 // Sam7s has several timers, we will use the source TIMER_CLOCK1 (aka AT91C_TC_CLKS_TIMER_DIV1_CLOCK)
33 // TIMER_CLOCK1 = MCK/2, MCK is running at 48 MHz, Timer is running at 48/2 = 24 MHz
34 // EM4x50 units (T0) have duration of 8 microseconds (us), which is 1/125000 per second (carrier)
35 // T0 = TIMER_CLOCK1 / 125000 = 192
39 // conversions (carrier frequency 125 kHz):
41 // 1 cycle = 1 period = 8 us = 12 ticks
42 // 1 bit = 64 cycles = 768 ticks = 512 us (for Opt64)
43 #define CYCLES2TICKS 12
44 #define CYCLES2MUSEC 8
46 // given in cycles/periods
47 #define EM4X50_T_TAG_QUARTER_PERIOD 16
48 #define EM4X50_T_TAG_HALF_PERIOD 32
49 #define EM4X50_T_TAG_THREE_QUARTER_PERIOD 48
50 #define EM4X50_T_TAG_FULL_PERIOD 64
51 #define EM4X50_T_TAG_TPP 64
52 #define EM4X50_T_TAG_TWA 64
53 #define EM4X50_T_TAG_TINIT 2112
54 #define EM4X50_T_TAG_TWEE 3200
55 #define EM4X50_T_TAG_WAITING_FOR_SIGNAL 75
56 #define EM4X50_T_WAITING_FOR_DBLLIW 1550
57 #define EM4X50_T_WAITING_FOR_ACK 4
58 #define EM4X50_T_TOLERANCE 8
59 #define EM4X50_T_ZERO_DETECTION 3
61 // timeout values (empirical) for simulation mode (may vary with regard to reader)
62 #define EM4X50_T_SIMULATION_TIMEOUT_READ 600
63 #define EM4X50_T_SIMULATION_TIMEOUT_WAIT 50
65 // the following value (pulses) seems to be critical; if it's too low
66 //(e.g. < 120) some cards are no longer readable although they're ok
67 #define EM4X50_T_WAITING_FOR_SNGLLIW 140
70 #define EM4X50_TAG_WORD 45
71 #define EM4X50_TAG_MAX_NO_BYTES 136
72 #define EM4X50_TIMEOUT_PULSE_EVAL 2500
77 // indication whether a previous login has been successful, so operations
78 // that require authentication can be handled
80 // WritePassword process in simulation mode is handled in a different way
81 // compared to operations like read, write, login, so it is necessary to
82 // to be able to identfiy it
83 bool g_WritePasswordProcess
= false;
84 // if reader sends a different password than "expected" -> save it
85 uint32_t g_Password
= 0;
87 // extract and check parities
88 // return result of parity check and extracted plain data
89 static bool extract_parities(uint64_t word
, uint32_t *data
) {
91 uint8_t row_parities
= 0x0, col_parities
= 0x0;
92 uint8_t row_parities_calculated
= 0x0, col_parities_calculated
= 0x0;
96 // extract plain data (32 bits) from raw word (45 bits)
97 for (int i
= 0; i
< 4; i
++) {
99 *data
|= (word
>> ((4 - i
) * 9 + 1)) & 0xFF;
102 // extract row parities (4 bits + stop bit) from raw word (45 bits)
103 for (int i
= 0; i
< 5; i
++) {
105 row_parities
|= (word
>> ((4 - i
) * 9)) & 0x1;
108 // extract col_parities (8 bits, no stop bit) from raw word (45 bits)
109 col_parities
= (word
>> 1) & 0xFF;
111 // check extracted parities against extracted data
113 // calculate row parities from data
114 for (int i
= 0; i
< 4; i
++) {
115 row_parities_calculated
<<= 1;
116 for (int j
= 0; j
< 8; j
++) {
117 row_parities_calculated
^= (*data
>> ((3 - i
) * 8 + (7 - j
))) & 0x1;
121 // add stop bit (always zero)
122 row_parities_calculated
<<= 1;
124 // calculate column parities from data
125 for (int i
= 0; i
< 8; i
++) {
126 col_parities_calculated
<<= 1;
127 for (int j
= 0; j
< 4; j
++) {
128 col_parities_calculated
^= (*data
>> ((3 - j
) * 8 + (7 - i
))) & 0x1;
132 if ((row_parities
== row_parities_calculated
) && (col_parities
== col_parities_calculated
)) {
138 void em4x50_setup_read(void) {
140 FpgaDownloadAndGo(FPGA_BITSTREAM_LF
);
141 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_ADC
| FPGA_LF_ADC_READER_FIELD
);
145 // 50ms for the resonant antenna to settle.
148 // Now set up the SSC to get the ADC samples that are now streaming at us.
149 FpgaSetupSsc(FPGA_MAJOR_MODE_LF_READER
);
151 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, LF_DIVISOR_125
);
153 // Connect the A/D to the peak-detected low-frequency path.
154 SetAdcMuxFor(GPIO_MUXSEL_LOPKD
);
156 // Steal this pin from the SSP (SPI communication channel with fpga) and
157 // use it to control the modulation
158 AT91C_BASE_PIOA
->PIO_PER
= GPIO_SSC_DOUT
;
159 AT91C_BASE_PIOA
->PIO_OER
= GPIO_SSC_DOUT
;
161 // Disable modulation at default, which means enable the field
168 void em4x50_setup_sim(void) {
169 FpgaDownloadAndGo(FPGA_BITSTREAM_LF
);
170 FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_EDGE_DETECT
);
171 FpgaSendCommand(FPGA_CMD_SET_DIVISOR
, LF_DIVISOR_125
);
173 AT91C_BASE_PIOA
->PIO_PER
= GPIO_SSC_DOUT
| GPIO_SSC_CLK
;
174 AT91C_BASE_PIOA
->PIO_OER
= GPIO_SSC_DOUT
;
175 AT91C_BASE_PIOA
->PIO_ODR
= GPIO_SSC_CLK
;
183 // calculate signal properties (mean amplitudes) from measured data:
184 // 32 amplitudes (maximum values) -> mean amplitude value -> g_High -> g_Low
185 static bool get_signalproperties(void) {
187 bool signal_found
= false;
188 int no_periods
= 32, pct
= 75, noise
= 140;
189 uint8_t sample_ref
= 127;
190 uint8_t sample_max_mean
= 0;
191 uint8_t sample_max
[no_periods
];
192 uint32_t sample_max_sum
= 0;
193 memset(sample_max
, 0x00, sizeof(sample_max
));
195 // wait until signal/noise > 1 (max. 32 periods)
196 for (int i
= 0; i
< EM4X50_T_TAG_WAITING_FOR_SIGNAL
; i
++) {
198 if (BUTTON_PRESS()) return false;
200 // about 2 samples per bit period
201 WaitUS(EM4X50_T_TAG_HALF_PERIOD
* CYCLES2MUSEC
);
203 // ignore first samples
204 if ((i
> SIGNAL_IGNORE_FIRST_SAMPLES
) && (AT91C_BASE_SSC
->SSC_RHR
> noise
)) {
210 if (signal_found
== false) {
214 // calculate mean maximum value of 32 periods, each period has a length of
215 // 3 single "full periods" to eliminate the influence of a listen window
216 for (int i
= 0; i
< no_periods
; i
++) {
218 uint32_t tval
= GetTicks();
219 while (GetTicks() - tval
< 12 * 3 * EM4X50_T_TAG_FULL_PERIOD
) {
221 if (BUTTON_PRESS()) return false;
223 volatile uint8_t sample
= (uint8_t)AT91C_BASE_SSC
->SSC_RHR
;
224 if (sample
> sample_max
[i
])
225 sample_max
[i
] = sample
;
229 sample_max_sum
+= sample_max
[i
];
232 sample_max_mean
= sample_max_sum
/ no_periods
;
234 // set global envelope variables
235 g_High
= sample_ref
+ pct
* (sample_max_mean
- sample_ref
) / 100;
236 g_Low
= sample_ref
- pct
* (sample_max_mean
- sample_ref
) / 100;
241 // returns true if bit is undefined by evaluating a single sample within
242 // a bit period (given there is no LIW, ACK or NAK)
243 // This function is used for identifying a listen window in functions
244 // "find_double_listen_window" and "check_ack"
245 static bool invalid_bit(void) {
247 // get sample at 3/4 of bit period
248 WaitUS(EM4X50_T_TAG_THREE_QUARTER_PERIOD
* CYCLES2MUSEC
);
250 uint8_t sample
= (uint8_t)AT91C_BASE_SSC
->SSC_RHR
;
252 // wait until end of bit period
253 WaitUS(EM4X50_T_TAG_QUARTER_PERIOD
* CYCLES2MUSEC
);
255 // bit in "undefined" state?
256 if (sample
<= g_High
&& sample
>= g_Low
)
262 static uint32_t get_pulse_length(void) {
264 int32_t timeout
= EM4X50_TIMEOUT_PULSE_EVAL
, tval
= 0;
266 // iterates pulse lengths (low -> high -> low)
268 volatile uint8_t sample
= (uint8_t)AT91C_BASE_SSC
->SSC_RHR
;
270 while (sample
> g_Low
&& (timeout
--))
271 sample
= (uint8_t)AT91C_BASE_SSC
->SSC_RHR
;
277 timeout
= EM4X50_TIMEOUT_PULSE_EVAL
;
279 while (sample
< g_High
&& (timeout
--))
280 sample
= (uint8_t)AT91C_BASE_SSC
->SSC_RHR
;
285 timeout
= EM4X50_TIMEOUT_PULSE_EVAL
;
286 while (sample
> g_Low
&& (timeout
--))
287 sample
= (uint8_t)AT91C_BASE_SSC
->SSC_RHR
;
292 return GetTicks() - tval
;
296 // check if pulse length <pl> corresponds to given length <length>
297 static bool check_pulse_length(uint32_t pl
, int length
) {
298 return ((pl
>= (length
- EM4X50_T_TOLERANCE
) * CYCLES2TICKS
) &&
299 (pl
<= (length
+ EM4X50_T_TOLERANCE
) * CYCLES2TICKS
));
302 // send single bit according to EM4x50 application note and datasheet
303 static void em4x50_reader_send_bit(int bit
) {
305 // reset clock for the next bit
306 uint32_t tval
= GetTicks();
310 // disable modulation (activate the field) for 7 cycles of carrier
313 while (GetTicks() - tval
< 7 * CYCLES2TICKS
);
315 // enable modulation (drop the field) for remaining first
316 // half of bit period
318 while (GetTicks() - tval
< EM4X50_T_TAG_HALF_PERIOD
* CYCLES2TICKS
);
320 // disable modulation for second half of bit period
322 while (GetTicks() - tval
< EM4X50_T_TAG_FULL_PERIOD
* CYCLES2TICKS
);
326 // bit = "1" means disable modulation for full bit period
328 while (GetTicks() - tval
< EM4X50_T_TAG_FULL_PERIOD
* CYCLES2TICKS
);
332 // send byte (without parity)
333 static void em4x50_reader_send_byte(uint8_t byte
) {
334 for (int i
= 0; i
< 8; i
++) {
335 em4x50_reader_send_bit((byte
>> (7 - i
)) & 1);
339 // send byte followed by its (even) parity bit
340 static void em4x50_reader_send_byte_with_parity(uint8_t byte
) {
343 for (int i
= 0; i
< 8; i
++) {
344 int bit
= (byte
>> (7 - i
)) & 1;
345 em4x50_reader_send_bit(bit
);
349 em4x50_reader_send_bit(parity
);
352 // send 32 bit word with parity bits according to EM4x50 datasheet
353 // word hast be sent in msb notation
354 static void em4x50_reader_send_word(const uint32_t word
) {
355 uint8_t bytes
[4] = {0x0, 0x0, 0x0, 0x0};
357 for (int i
= 0; i
< 4; i
++) {
358 bytes
[i
] = (word
>> (24 - (8 * i
))) & 0xFF;
359 em4x50_reader_send_byte_with_parity(bytes
[i
]);
362 // send column parities
363 em4x50_reader_send_byte(bytes
[0] ^ bytes
[1] ^ bytes
[2] ^ bytes
[3]);
365 // send final stop bit (always "0")
366 em4x50_reader_send_bit(0);
369 // find single listen window
370 static bool find_single_listen_window(void) {
373 while (cnt_pulses
< EM4X50_T_WAITING_FOR_SNGLLIW
) {
375 // identification of listen window is done via evaluation of
377 if (check_pulse_length(get_pulse_length(), 3 * EM4X50_T_TAG_FULL_PERIOD
)) {
379 if (check_pulse_length(get_pulse_length(), 2 * EM4X50_T_TAG_FULL_PERIOD
)) {
381 // found listen window
391 // find two successive listen windows that indicate the beginning of
393 // double listen window to be detected within 1600 pulses -> worst case
394 // reason: first detectable double listen window after 34 words
395 // -> 34 words + 34 single listen windows -> about 1600 pulses
396 static int find_double_listen_window(bool bcommand
) {
399 while (cnt_pulses
< EM4X50_T_WAITING_FOR_DBLLIW
) {
401 if (BUTTON_PRESS()) {
402 return PM3_EOPABORTED
;
405 // identification of listen window is done via evaluation of
407 if (check_pulse_length(get_pulse_length(), 3 * EM4X50_T_TAG_FULL_PERIOD
)) {
409 if (check_pulse_length(get_pulse_length(), 2 * EM4X50_T_TAG_FULL_PERIOD
)) {
411 // first listen window found
415 // data transmission from card has to be stopped, because
416 // a commamd shall be issued
418 // unfortunately the position in listen window (where
419 // command request has to be sent) has gone, so if a
420 // second window follows - sync on this to issue a command
422 // skip the next bit...
423 WaitUS(EM4X50_T_TAG_FULL_PERIOD
* CYCLES2MUSEC
);
425 // ...and check if the following bit does make sense
426 // (if not it is the correct position within the second
430 // send RM for request mode
431 em4x50_reader_send_bit(0);
432 em4x50_reader_send_bit(0);
439 if (check_pulse_length(get_pulse_length(), 3 * EM4X50_T_TAG_FULL_PERIOD
)) {
441 // return although second listen window consists of one
442 // more bit period but this period is necessary for
443 // evaluating further pulse lengths
454 // function is used to check whether a tag on the proxmark is an
455 // EM4x50 tag or not -> speed up "lf search" process
456 static bool find_em4x50_tag(void) {
457 return find_single_listen_window();
460 // To issue a command we have to find a listen window first.
461 // Because identification and synchronization at the same time is not
462 // possible when using pulse lengths a double listen window is used.
463 static int request_receive_mode(void) {
464 return find_double_listen_window(true);
467 // returns true if signal structue corresponds to ACK, anything else is
468 // counted as NAK (-> false)
469 // Only relevant for password writing function:
470 // If <bliw> is true then within the single listen window right after the
471 // ack signal a RM request has to be sent.
472 static bool check_ack(bool bliw
) {
473 int count_cycles
= 0;
474 while (count_cycles
< EM4X50_T_WAITING_FOR_ACK
) {
478 if (check_pulse_length(get_pulse_length(), 2 * EM4X50_T_TAG_FULL_PERIOD
)) {
480 // The received signal is either ACK or NAK.
482 if (check_pulse_length(get_pulse_length(), 2 * EM4X50_T_TAG_FULL_PERIOD
)) {
484 // Now the signal must be ACK.
492 // send RM request after ack signal
494 // wait for 2 bits (remaining "bit" of ACK signal + first
495 // "bit" of listen window)
496 WaitUS(2 * EM4X50_T_TAG_FULL_PERIOD
* CYCLES2MUSEC
);
498 // check for listen window (if first bit cannot be interpreted
499 // as a valid bit it must belong to a listen window)
502 // send RM for request mode
503 em4x50_reader_send_bit(0);
504 em4x50_reader_send_bit(0);
511 // It's NAK -> stop searching
521 // decodes one word by evaluating pulse lengths and previous bit;
522 // word must have 45 bits in total:
523 // 32 data bits + 4 row parity bits + 8 column parity bits + 1 stop bit
524 static int get_word_from_bitstream(uint32_t *data
) {
525 bool bitchange
= false;
532 // initial bit value depends on last pulse length of listen window
533 pl
= get_pulse_length();
534 if (check_pulse_length(pl
, 3 * EM4X50_T_TAG_HALF_PERIOD
)) {
536 // pulse length = 1.5
539 } else if (check_pulse_length(pl
, 2 * EM4X50_T_TAG_FULL_PERIOD
)) {
546 // pulse length = 2.5
551 // identify remaining bits based on pulse lengths
552 // between two listen windows only pulse lengths of 1, 1.5 and 2 are possible
553 while (BUTTON_PRESS() == false) {
558 pl
= get_pulse_length();
560 if (check_pulse_length(pl
, EM4X50_T_TAG_FULL_PERIOD
)) {
562 // pulse length = 1 -> keep former bit value
563 word
|= (word
>> 1) & 0x1;
565 } else if (check_pulse_length(pl
, 3 * EM4X50_T_TAG_HALF_PERIOD
)) {
567 // pulse length = 1.5 -> decision on bit change
571 // if number of pulse lengths with 1.5 periods is even -> add bit
572 word
|= (word
>> 1) & 0x1;
575 // pulse length of 1.5 changes bit value
576 word
|= ((word
>> 1) & 0x1) ^ 0x1;
579 // next time add only one bit
584 word
|= ((word
>> 1) & 0x1) ^ 0x1;
586 // next time two bits have to be added
590 } else if (check_pulse_length(pl
, 2 * EM4X50_T_TAG_FULL_PERIOD
)) {
592 // pulse length of 2 means: adding 2 bits "01"
598 } else if (check_pulse_length(pl
, 3 * EM4X50_T_TAG_FULL_PERIOD
)) {
600 // pulse length of 3 indicates listen window -> clear last
601 // bit (= 0) and return (without parities)
603 return (extract_parities(word
, data
)) ? --cnt
: 0;
607 return PM3_EOPABORTED
;
610 // simple login to EM4x50,
611 // used in operations that require authentication
612 static int login(uint32_t password
) {
613 if (request_receive_mode() == PM3_SUCCESS
) {
615 // send login command
616 em4x50_reader_send_byte_with_parity(EM4X50_COMMAND_LOGIN
);
619 em4x50_reader_send_word(password
);
621 WaitUS(EM4X50_T_TAG_TPP
* CYCLES2MUSEC
);
623 // check if ACK is returned
624 if (check_ack(false))
628 if (g_dbglevel
>= DBG_DEBUG
)
629 Dbprintf("error in command request");
635 // searching for password using chosen bruteforce algorithm
636 static bool brute(const em4x50_data_t
*etd
, uint32_t *pwd
) {
638 generator_context_t ctx
;
639 bool pwd_found
= false;
640 int generator_ret
= 0;
643 bf_generator_init(&ctx
, etd
->bruteforce_mode
, BF_KEY_SIZE_32
);
645 if (etd
->bruteforce_mode
== BF_MODE_CHARSET
) {
646 bf_generator_set_charset(&ctx
, etd
->bruteforce_charset
);
647 } else if (etd
->bruteforce_mode
== BF_MODE_RANGE
) {
648 ctx
.range_low
= etd
->password1
;
649 ctx
.range_high
= etd
->password2
;
652 while ((generator_ret
= bf_generate(&ctx
)) == BF_GENERATOR_NEXT
) {
653 *pwd
= bf_get_key32(&ctx
);
657 if (login(*pwd
) == PM3_SUCCESS
) {
661 // to be safe login 5 more times
662 for (int i
= 0; i
< 5; i
++) {
663 if (login(*pwd
) != PM3_SUCCESS
) {
673 // print password every 500 iterations
674 if ((++cnt
% 500) == 0) {
678 Dbprintf("|---------+------------+------------|");
679 Dbprintf("| no. | pwd (msb) | pwd (lsb) |");
680 Dbprintf("|---------+------------+------------|");
684 Dbprintf("|%8i | 0x%08x | 0x%08x |", cnt
, reflect32(*pwd
), *pwd
);
694 Dbprintf("|---------+------------+------------|");
700 void em4x50_login(const uint32_t *password
, bool ledcontrol
) {
703 int status
= PM3_EFAILED
;
704 if (ledcontrol
) LED_C_ON();
705 if (get_signalproperties() && find_em4x50_tag()) {
710 status
= login(*password
);
713 if (ledcontrol
) LEDsoff();
714 lf_finalize(ledcontrol
);
715 reply_ng(CMD_LF_EM4X50_LOGIN
, status
, NULL
, 0);
718 // invoke password search
719 void em4x50_brute(const em4x50_data_t
*etd
, bool ledcontrol
) {
722 bool bsuccess
= false;
724 if (ledcontrol
) LED_C_ON();
725 if (get_signalproperties() && find_em4x50_tag()) {
730 bsuccess
= brute(etd
, &pwd
);
733 if (ledcontrol
) LEDsoff();
734 lf_finalize(ledcontrol
);
735 reply_ng(CMD_LF_EM4X50_BRUTE
, bsuccess
? PM3_SUCCESS
: PM3_EFAILED
, (uint8_t *)(&pwd
), sizeof(pwd
));
738 // check passwords from dictionary content in flash memory
739 void em4x50_chk(const char *filename
, bool ledcontrol
) {
740 int status
= PM3_EFAILED
;
747 int changed
= rdv40_spiffs_lazy_mount();
748 uint16_t pwd_count
= 0;
749 uint32_t size
= size_in_spiffs(filename
);
750 pwd_count
= size
/ 4;
751 uint8_t *pwds
= BigBuf_malloc(size
);
753 rdv40_spiffs_read_as_filetype(filename
, pwds
, size
, RDV40_SPIFFS_SAFETY_SAFE
);
756 rdv40_spiffs_lazy_unmount();
760 // set g_High and g_Low
761 if (ledcontrol
) LED_C_ON();
762 if (get_signalproperties() && find_em4x50_tag()) {
769 // try to login with current password
770 for (int i
= 0; i
< pwd_count
; i
++) {
772 // manual interruption
773 if (BUTTON_PRESS()) {
774 status
= PM3_EOPABORTED
;
780 for (int j
= 0; j
< 4; j
++)
781 pwd
|= (*(pwds
+ 4 * i
+ j
)) << ((3 - j
) * 8);
783 if ((status
= login(pwd
)) == PM3_SUCCESS
) {
795 if (ledcontrol
) LEDsoff();
796 lf_finalize(ledcontrol
);
797 reply_ng(CMD_LF_EM4X50_CHK
, status
, (uint8_t *)&pwd
, sizeof(pwd
));
800 // resets EM4x50 tag (used by write function)
801 static int reset(void) {
802 if (request_receive_mode() == PM3_SUCCESS
) {
804 // send reset command
805 em4x50_reader_send_byte_with_parity(EM4X50_COMMAND_RESET
);
807 if (check_ack(false))
811 if (g_dbglevel
>= DBG_DEBUG
)
812 Dbprintf("error in command request");
818 // reads data that tag transmits when exposed to reader field
819 // (standard read mode); number of read words is saved in <now>
820 int standard_read(int *now
, uint32_t *words
) {
822 int fwr
= *now
, res
= PM3_EFAILED
;
824 // start with the identification of two successive listening windows
825 if ((res
= find_double_listen_window(false)) == PM3_SUCCESS
) {
827 // read and save words until following double listen window is detected
828 res
= get_word_from_bitstream(&words
[*now
]);
829 while (res
== EM4X50_TAG_WORD
) {
831 res
= get_word_from_bitstream(&words
[*now
]);
834 // number of detected words
838 if (g_dbglevel
>= DBG_DEBUG
)
839 Dbprintf("didn't find a listen window");
845 // reads from "first word read" (fwr) to "last word read" (lwr)
846 // result is verified by "standard read mode"
847 static int selective_read(uint32_t addresses
, uint32_t *words
) {
849 int status
= PM3_EFAILED
;
850 uint8_t fwr
= addresses
& 0xFF; // first word read (first byte)
851 uint8_t lwr
= (addresses
>> 8) & 0xFF; // last word read (second byte)
852 int now
= fwr
; // number of words
854 if (request_receive_mode() == PM3_SUCCESS
) {
856 // send selective read command
857 em4x50_reader_send_byte_with_parity(EM4X50_COMMAND_SELECTIVE_READ
);
860 em4x50_reader_send_word(addresses
);
862 // look for ACK sequence
863 if (check_ack(false))
865 // save and verify via standard read mode (compare number of words)
866 if ((status
= standard_read(&now
, words
)) == PM3_SUCCESS
)
867 if (now
== (lwr
- fwr
+ 1))
871 if (g_dbglevel
>= DBG_DEBUG
)
872 Dbprintf("error in command request");
878 // reads by using "selective read mode" -> bidirectional communication
879 void em4x50_read(const em4x50_data_t
*etd
, bool ledcontrol
) {
880 int status
= PM3_EFAILED
;
881 uint32_t words
[EM4X50_NO_WORDS
] = {0x0};
885 // set g_High and g_Low
886 if (ledcontrol
) LED_C_ON();
887 if (get_signalproperties() && find_em4x50_tag()) {
896 // try to login with given password
898 blogin
= (login(etd
->password1
) == PM3_SUCCESS
);
900 // only one word has to be read -> first word read = last word read
902 status
= selective_read(etd
->addresses
, words
);
905 if (ledcontrol
) LEDsoff();
907 lf_finalize(ledcontrol
);
908 reply_ng(CMD_LF_EM4X50_READ
, status
, (uint8_t *)words
, EM4X50_TAG_MAX_NO_BYTES
);
911 // collects as much information as possible via selective read mode
912 void em4x50_info(const em4x50_data_t
*etd
, bool ledcontrol
) {
913 int status
= PM3_EFAILED
;
914 uint32_t words
[EM4X50_NO_WORDS
] = {0x0};
918 if (ledcontrol
) LED_C_ON();
919 if (get_signalproperties() && find_em4x50_tag()) {
926 // login with given password
928 blogin
= (login(etd
->password1
) == PM3_SUCCESS
);
931 // read addresses from fwr = 0 to lwr = 33 (0x21)
932 status
= selective_read(0x00002100, words
);
936 if (ledcontrol
) LEDsoff();
937 lf_finalize(ledcontrol
);
938 reply_ng(CMD_LF_EM4X50_INFO
, status
, (uint8_t *)words
, EM4X50_TAG_MAX_NO_BYTES
);
941 // reads data that tag transmits "voluntarily" -> standard read mode
942 void em4x50_reader(bool ledcontrol
) {
945 uint32_t words
[EM4X50_NO_WORDS
] = {0x0};
949 if (ledcontrol
) LED_C_ON();
950 if (get_signalproperties() && find_em4x50_tag()) {
955 standard_read(&now
, words
);
958 if (ledcontrol
) LEDsoff();
960 lf_finalize(ledcontrol
);
961 reply_ng(CMD_LF_EM4X50_READER
, now
, (uint8_t *)words
, 4 * now
);
964 // writes <word> to specified <addresses>
965 static int write(uint32_t word
, uint32_t addresses
) {
967 if (request_receive_mode() == PM3_SUCCESS
) {
969 // send write command
970 em4x50_reader_send_byte_with_parity(EM4X50_COMMAND_WRITE
);
973 em4x50_reader_send_byte_with_parity(addresses
& 0xFF);
976 em4x50_reader_send_word(word
);
978 if (tearoff_hook() == PM3_ETEAROFF
) { // tearoff occurred
979 reply_ng(CMD_LF_EM4X50_WRITE
, PM3_ETEAROFF
, NULL
, 0);
983 // wait for T0 * EM4X50_T_TAG_TWA (write access time)
984 WaitUS(EM4X50_T_TAG_TWA
* CYCLES2MUSEC
);
986 // look for ACK sequence
987 if (check_ack(false)) {
989 // now EM4x50 needs T0 * EM4X50_T_TAG_TWEE (EEPROM write time = 3.2ms = 50 * 64 periods)
990 // for saving data and should return with ACK
991 for (int i
= 0; i
< 50; i
++) {
992 WaitUS(EM4X50_T_TAG_FULL_PERIOD
* CYCLES2MUSEC
);
995 if (check_ack(false))
1000 if (g_dbglevel
>= DBG_DEBUG
)
1001 Dbprintf("error in command request");
1007 // changes password from <password> to <new_password>
1008 static int write_password(uint32_t password
, uint32_t new_password
) {
1009 if (request_receive_mode() == PM3_SUCCESS
) {
1011 // send write password command
1012 em4x50_reader_send_byte_with_parity(EM4X50_COMMAND_WRITE_PASSWORD
);
1014 // send address data
1015 em4x50_reader_send_word(password
);
1017 if (tearoff_hook() == PM3_ETEAROFF
) { // tearoff occurred
1018 reply_ng(CMD_LF_EM4X50_WRITE
, PM3_ETEAROFF
, NULL
, 0);
1019 return PM3_ETEAROFF
;
1022 // wait for T0 * EM4x50_T_TAG_TPP (processing pause time)
1023 WaitUS(EM4X50_T_TAG_TPP
* CYCLES2MUSEC
);
1025 // look for ACK sequence and send rm request
1026 // during following listen window
1027 if (check_ack(true)) {
1029 // send new password
1030 em4x50_reader_send_word(new_password
);
1032 // wait for T0 * EM4X50_T_TAG_TWA (write access time)
1033 WaitUS(EM4X50_T_TAG_TWA
* CYCLES2MUSEC
);
1035 if (check_ack(false)) {
1037 // now EM4x50 needs T0 * EM4X50_T_TAG_TWEE (EEPROM write time = 3.2ms = 50 * 64 periods)
1038 // for saving data and should return with ACK
1039 for (int i
= 0; i
< 50; i
++) {
1040 WaitUS(EM4X50_T_TAG_FULL_PERIOD
* CYCLES2MUSEC
);
1043 if (check_ack(false))
1049 if (g_dbglevel
>= DBG_DEBUG
)
1050 Dbprintf("error in command request");
1056 // write operation process for EM4x50 tag,
1057 // single word is written to given address, verified by selective read operation
1058 // wrong password -> return with PM3_EFAILED
1059 void em4x50_write(const em4x50_data_t
*etd
, bool ledcontrol
) {
1060 int status
= PM3_EFAILED
;
1061 uint32_t words
[EM4X50_NO_WORDS
] = {0x0};
1063 em4x50_setup_read();
1065 if (ledcontrol
) LED_C_ON();
1066 if (get_signalproperties() && find_em4x50_tag()) {
1073 // if password is given try to login first
1074 status
= PM3_SUCCESS
;
1076 status
= login(etd
->password1
);
1078 if (status
== PM3_SUCCESS
) {
1080 // write word to given address
1081 status
= write(etd
->word
, etd
->addresses
);
1082 if (status
== PM3_ETEAROFF
) {
1083 lf_finalize(ledcontrol
);
1087 if (status
== PM3_SUCCESS
) {
1089 // to verify result reset EM4x50
1091 if (status
== PM3_SUCCESS
) {
1093 // if password is given renew login after reset
1095 status
= login(etd
->password1
);
1097 if (status
== PM3_SUCCESS
) {
1099 // call a selective read
1100 status
= selective_read(etd
->addresses
, words
);
1101 if (status
== PM3_SUCCESS
) {
1103 // compare result with given word
1104 if (words
[etd
->addresses
& 0xFF] != reflect32(etd
->word
))
1105 status
= PM3_EFAILED
;
1113 if (ledcontrol
) LEDsoff();
1114 lf_finalize(ledcontrol
);
1115 reply_ng(CMD_LF_EM4X50_WRITE
, status
, (uint8_t *)words
, EM4X50_TAG_MAX_NO_BYTES
);
1118 // simple change of password
1119 void em4x50_writepwd(const em4x50_data_t
*etd
, bool ledcontrol
) {
1120 int status
= PM3_EFAILED
;
1122 em4x50_setup_read();
1124 if (ledcontrol
) LED_C_ON();
1125 if (get_signalproperties() && find_em4x50_tag()) {
1132 // login and change password
1133 if (login(etd
->password1
) == PM3_SUCCESS
) {
1135 status
= write_password(etd
->password1
, etd
->password2
);
1136 if (status
== PM3_ETEAROFF
) {
1137 lf_finalize(ledcontrol
);
1143 if (ledcontrol
) LEDsoff();
1144 lf_finalize(ledcontrol
);
1145 reply_ng(CMD_LF_EM4X50_WRITEPWD
, status
, NULL
, 0);
1148 // send bit in receive mode by counting carrier cycles
1149 static void em4x50_sim_send_bit(uint8_t bit
) {
1151 uint16_t timeout
= EM4X50_T_SIMULATION_TIMEOUT_READ
;
1153 for (int t
= 0; t
< EM4X50_T_TAG_FULL_PERIOD
; t
++) {
1155 // wait until SSC_CLK goes HIGH
1156 // used as a simple detection of a reader field?
1157 while ((timeout
--) && !(AT91C_BASE_PIOA
->PIO_PDSR
& GPIO_SSC_CLK
));
1162 timeout
= EM4X50_T_SIMULATION_TIMEOUT_READ
;
1169 //wait until SSC_CLK goes LOW
1170 while ((timeout
--) && (AT91C_BASE_PIOA
->PIO_PDSR
& GPIO_SSC_CLK
));
1174 timeout
= EM4X50_T_SIMULATION_TIMEOUT_READ
;
1176 if (t
== EM4X50_T_TAG_HALF_PERIOD
)
1182 // send byte in receive mode either with or without parity check (even)
1183 static void em4x50_sim_send_byte(uint8_t byte
, bool paritycheck
) {
1186 for (int i
= 0; i
< 8; i
++) {
1187 em4x50_sim_send_bit((byte
>> (7 - i
)) & 1);
1192 uint8_t parity
= 0x0;
1194 for (int i
= 0; i
< 8; i
++) {
1195 parity
^= (byte
>> i
) & 1;
1198 em4x50_sim_send_bit(parity
);
1202 // send complete word in receive mode (including all parity checks)
1203 static void em4x50_sim_send_word(uint32_t word
) {
1205 uint8_t cparity
= 0x00;
1207 // word has tobe sent in msb, not lsb
1208 word
= reflect32(word
);
1210 // 4 bytes each with even row parity bit
1211 for (int i
= 0; i
< 4; i
++) {
1212 em4x50_sim_send_byte((word
>> ((3 - i
) * 8)) & 0xFF, true);
1216 for (int i
= 0; i
< 8; i
++) {
1218 for (int j
= 0; j
< 4; j
++) {
1219 cparity
^= (((word
>> ((3 - j
) * 8)) & 0xFF) >> (7 - i
)) & 1;
1222 em4x50_sim_send_byte(cparity
, false);
1225 em4x50_sim_send_bit(0);
1228 // wait for <maxperiods> pulses of carrier frequency
1229 static void wait_cycles(int maxperiods
) {
1231 int period
= 0, timeout
= EM4X50_T_SIMULATION_TIMEOUT_WAIT
;
1233 while (period
< maxperiods
) {
1235 while ((timeout
--) && !(AT91C_BASE_PIOA
->PIO_PDSR
& GPIO_SSC_CLK
));
1239 timeout
= EM4X50_T_SIMULATION_TIMEOUT_WAIT
;
1241 while ((timeout
--) && (AT91C_BASE_PIOA
->PIO_PDSR
& GPIO_SSC_CLK
));
1245 timeout
= EM4X50_T_SIMULATION_TIMEOUT_WAIT
;
1251 // read single bit in simulation mode
1252 static int em4x50_sim_read_bit(void) {
1255 int timeout
= EM4X50_T_SIMULATION_TIMEOUT_READ
;
1257 // wait 16 cycles to make sure there is no field when reading a "0" bit
1258 uint32_t waitval
= GetTicks();
1259 while (GetTicks() - waitval
< EM4X50_T_TAG_QUARTER_PERIOD
* CYCLES2TICKS
);
1261 while (cycles
< EM4X50_T_TAG_THREE_QUARTER_PERIOD
) {
1263 // wait until reader field disappears
1264 while ((timeout
--) && !(AT91C_BASE_PIOA
->PIO_PDSR
& GPIO_SSC_CLK
));
1266 return PM3_ETIMEOUT
;
1268 timeout
= EM4X50_T_SIMULATION_TIMEOUT_READ
;
1270 // now check until reader switches on carrier field
1271 uint32_t tval
= GetTicks();
1272 while ((timeout
--) && (AT91C_BASE_PIOA
->PIO_PDSR
& GPIO_SSC_CLK
)) {
1275 return PM3_ETIMEOUT
;
1278 // check if current cycle takes longer than "usual""
1279 if (GetTicks() - tval
> EM4X50_T_ZERO_DETECTION
* CYCLES2TICKS
) {
1281 // gap detected; wait until reader field is switched on again
1282 while ((timeout
--) && (AT91C_BASE_PIOA
->PIO_PDSR
& GPIO_SSC_CLK
));
1285 return PM3_ETIMEOUT
;
1288 // now we have a reference "position", from here it will take
1289 // slightly less than 32 cycles until the end of the bit period
1292 // end of bit period is reached; return with bit value "0"
1297 timeout
= EM4X50_T_SIMULATION_TIMEOUT_READ
;
1299 // no gap detected, i.e. reader field is still up;
1300 // continue with counting cycles
1304 // reached 64 cycles (= EM4X50_T_TAG_FULL_PERIOD) -> return bit value "1"
1308 // read byte in simulation mode either with or without parity check (even)
1309 static bool em4x50_sim_read_byte(uint8_t *byte
, bool paritycheck
) {
1311 for (int i
= 0; i
< 8; i
++) {
1313 *byte
|= em4x50_sim_read_bit();
1318 int pval
= em4x50_sim_read_bit();
1321 for (int i
= 0; i
< 8; i
++) {
1322 parity
^= ((*byte
) >> i
) & 1;
1325 if (parity
!= pval
) {
1333 // read complete word in simulation mode
1334 static bool em4x50_sim_read_word(uint32_t *word
) {
1336 uint8_t stop_bit
= 0;
1337 uint8_t parities
= 0, parities_calculated
= 0;
1338 uint8_t bytes
[4] = {0};
1341 for (int i
= 0; i
< 4; i
++) {
1342 em4x50_sim_read_byte(&bytes
[i
], true);
1345 // read column parities and stop bit
1346 em4x50_sim_read_byte(&parities
, false);
1347 stop_bit
= em4x50_sim_read_bit();
1349 // calculate column parities from data
1350 for (int i
= 0; i
< 8; i
++) {
1351 parities_calculated
<<= 1;
1352 for (int j
= 0; j
< 4; j
++) {
1353 parities_calculated
^= (bytes
[j
] >> (7 - i
)) & 1;
1357 *word
= BYTES2UINT32_BE(bytes
);
1360 if ((parities
== parities_calculated
) && (stop_bit
== 0)) {
1367 // check if reader requests receive mode (rm) by sending two zeros
1368 static int check_rm_request(const uint32_t *tag
, bool ledcontrol
) {
1370 // look for first zero
1371 int bit
= em4x50_sim_read_bit();
1374 // look for second zero
1375 bit
= em4x50_sim_read_bit();
1378 if (ledcontrol
) LED_C_ON();
1380 // if command before was EM4X50_COMMAND_WRITE_PASSWORD
1381 // switch to separate process
1382 if (g_WritePasswordProcess
) {
1383 return EM4X50_COMMAND_WRITE_PASSWORD
;
1385 // read mode request detected, get command from reader
1386 uint8_t command
= 0;
1387 em4x50_sim_read_byte(&command
, true);
1393 return (bit
!= PM3_ETIMEOUT
) ? PM3_SUCCESS
: PM3_ETIMEOUT
;
1396 // send single listen window in simulation mode
1397 static int em4x50_sim_send_listen_window(const uint32_t *tag
, bool ledcontrol
) {
1400 wait_cycles(EM4X50_T_TAG_HALF_PERIOD
);
1403 wait_cycles(EM4X50_T_TAG_HALF_PERIOD
);
1406 wait_cycles(2 * EM4X50_T_TAG_FULL_PERIOD
);
1409 int command
= check_rm_request(tag
, ledcontrol
);
1410 if (command
!= PM3_SUCCESS
) {
1415 wait_cycles(EM4X50_T_TAG_FULL_PERIOD
);
1421 static void em4x50_sim_send_ack(void) {
1424 wait_cycles(EM4X50_T_TAG_HALF_PERIOD
);
1427 wait_cycles(EM4X50_T_TAG_HALF_PERIOD
);
1430 wait_cycles(3 * EM4X50_T_TAG_HALF_PERIOD
);
1433 wait_cycles(EM4X50_T_TAG_HALF_PERIOD
);
1436 wait_cycles(3 * EM4X50_T_TAG_HALF_PERIOD
);
1439 wait_cycles(EM4X50_T_TAG_HALF_PERIOD
);
1445 static void em4x50_sim_send_nak(void) {
1448 wait_cycles(EM4X50_T_TAG_HALF_PERIOD
);
1451 wait_cycles(EM4X50_T_TAG_HALF_PERIOD
);
1454 wait_cycles(3 * EM4X50_T_TAG_HALF_PERIOD
);
1457 wait_cycles(EM4X50_T_TAG_HALF_PERIOD
);
1460 wait_cycles(EM4X50_T_TAG_FULL_PERIOD
);
1463 wait_cycles(EM4X50_T_TAG_HALF_PERIOD
);
1466 wait_cycles(EM4X50_T_TAG_HALF_PERIOD
);
1469 // standard read mode process (simulation mode)
1470 static int em4x50_sim_handle_standard_read_command(const uint32_t *tag
, bool ledcontrol
) {
1472 // extract control data
1473 int fwr
= reflect32(tag
[EM4X50_CONTROL
]) & 0xFF; // first word read
1474 int lwr
= (reflect32(tag
[EM4X50_CONTROL
]) >> 8) & 0xFF; // last word read
1475 // extract protection data:
1476 // first word read protected
1477 int fwrp
= reflect32(tag
[EM4X50_PROTECTION
]) & 0xFF;
1478 // last word read protected
1479 int lwrp
= (reflect32(tag
[EM4X50_PROTECTION
]) >> 8) & 0xFF;
1481 while ((BUTTON_PRESS() == false) && (data_available() == false)) {
1485 int res
= em4x50_sim_send_listen_window(tag
, ledcontrol
);
1487 if (res
!= PM3_SUCCESS
) {
1491 for (int i
= fwr
; i
<= lwr
; i
++) {
1493 res
= em4x50_sim_send_listen_window(tag
, ledcontrol
);
1494 if (res
!= PM3_SUCCESS
) {
1498 if ((g_Login
== false) && (i
>= fwrp
) && (i
<= lwrp
)) {
1499 em4x50_sim_send_word(0x00);
1501 em4x50_sim_send_word(reflect32(tag
[i
]));
1506 return PM3_EOPABORTED
;
1509 // selective read mode process (simulation mode)
1510 static int em4x50_sim_handle_selective_read_command(const uint32_t *tag
, bool ledcontrol
) {
1513 uint32_t address
= 0;
1514 bool addr
= em4x50_sim_read_word(&address
);
1516 // processing pause time (corresponds to a "1" bit)
1517 em4x50_sim_send_bit(1);
1520 em4x50_sim_send_ack();
1522 em4x50_sim_send_nak();
1523 return EM4X50_COMMAND_STANDARD_READ
;
1526 // extract control data
1527 int fwr
= address
& 0xFF; // first word read
1528 int lwr
= (address
>> 8) & 0xFF; // last word read
1530 // extract protection data:
1531 // first word read protected
1532 int fwrp
= reflect32(tag
[EM4X50_PROTECTION
]) & 0xFF;
1533 // last word read protected
1534 int lwrp
= (reflect32(tag
[EM4X50_PROTECTION
]) >> 8) & 0xFF;
1536 while ((BUTTON_PRESS() == false) && (data_available() == false)) {
1540 int command
= em4x50_sim_send_listen_window(tag
, ledcontrol
);
1541 if (command
!= PM3_SUCCESS
) {
1545 for (int i
= fwr
; i
<= lwr
; i
++) {
1547 command
= em4x50_sim_send_listen_window(tag
, ledcontrol
);
1548 if (command
!= PM3_SUCCESS
) {
1552 // if not authenticated do not send read protected words
1553 if ((g_Login
== false) && (i
>= fwrp
) && (i
<= lwrp
)) {
1554 em4x50_sim_send_word(0x00);
1556 em4x50_sim_send_word(reflect32(tag
[i
]));
1561 return PM3_EOPABORTED
;
1564 // login process (simulation mode)
1565 static int em4x50_sim_handle_login_command(const uint32_t *tag
, bool ledcontrol
) {
1568 uint32_t password
= 0;
1569 bool pwd
= em4x50_sim_read_word(&password
);
1571 // processing pause time (corresponds to a "1" bit)
1572 em4x50_sim_send_bit(1);
1574 if (pwd
&& (password
== reflect32(tag
[EM4X50_DEVICE_PASSWORD
]))) {
1575 em4x50_sim_send_ack();
1577 if (ledcontrol
) LED_D_ON();
1579 em4x50_sim_send_nak();
1581 if (ledcontrol
) LED_D_OFF();
1583 // save transmitted password (to be used in standalone mode)
1584 g_Password
= password
;
1586 // continue with standard read mode
1587 return EM4X50_COMMAND_STANDARD_READ
;
1590 // reset process (simulation mode)
1591 static int em4x50_sim_handle_reset_command(const uint32_t *tag
, bool ledcontrol
) {
1593 // processing pause time (corresponds to a "1" bit)
1594 em4x50_sim_send_bit(1);
1597 em4x50_sim_send_ack();
1599 if (ledcontrol
) LED_D_OFF();
1601 // wait for initialization (tinit)
1602 wait_cycles(EM4X50_T_TAG_TINIT
);
1604 // continue with standard read mode
1605 return EM4X50_COMMAND_STANDARD_READ
;
1608 // write process (simulation mode)
1609 static int em4x50_sim_handle_write_command(uint32_t *tag
, bool ledcontrol
) {
1612 uint8_t address
= 0;
1613 bool addr
= em4x50_sim_read_byte(&address
, true);
1616 bool word
= em4x50_sim_read_word(&data
);
1618 // write access time
1619 wait_cycles(EM4X50_T_TAG_TWA
);
1621 if ((addr
== false) || (word
== false)) {
1622 em4x50_sim_send_nak();
1623 return EM4X50_COMMAND_STANDARD_READ
;
1626 // extract necessary control data
1627 bool raw
= (tag
[EM4X50_CONTROL
] >> CONFIG_BLOCK
) & READ_AFTER_WRITE
;
1628 // extract protection data:
1629 // first word write protected
1630 int fwwp
= reflect8((tag
[EM4X50_PROTECTION
] >> 24) & 0xFF);
1631 // last word write protected
1632 int lwwp
= reflect8((tag
[EM4X50_PROTECTION
] >> 16) & 0xFF);
1636 case EM4X50_DEVICE_PASSWORD
:
1637 em4x50_sim_send_nak();
1638 return EM4X50_COMMAND_STANDARD_READ
;
1641 case EM4X50_PROTECTION
:
1643 tag
[address
] = reflect32(data
);
1644 em4x50_sim_send_ack();
1646 em4x50_sim_send_nak();
1647 return EM4X50_COMMAND_STANDARD_READ
;
1651 case EM4X50_CONTROL
:
1653 tag
[address
] = reflect32(data
);
1654 em4x50_sim_send_ack();
1656 em4x50_sim_send_nak();
1657 return EM4X50_COMMAND_STANDARD_READ
;
1661 case EM4X50_DEVICE_SERIAL
:
1662 em4x50_sim_send_nak();
1663 return EM4X50_COMMAND_STANDARD_READ
;
1666 case EM4X50_DEVICE_ID
:
1667 em4x50_sim_send_nak();
1668 return EM4X50_COMMAND_STANDARD_READ
;
1672 if ((address
>= fwwp
) && (address
<= lwwp
)) {
1674 tag
[address
] = reflect32(data
);
1675 em4x50_sim_send_ack();
1677 em4x50_sim_send_nak();
1678 return EM4X50_COMMAND_STANDARD_READ
;
1681 tag
[address
] = reflect32(data
);
1682 em4x50_sim_send_ack();
1687 // EEPROM write time
1688 // strange: need some sort of 'waveform correction', otherwise ack signal
1689 // will not be detected; sending a single "1" as last "bit" of Twee
1690 // seems to solve the problem
1691 wait_cycles(EM4X50_T_TAG_TWEE
- EM4X50_T_TAG_FULL_PERIOD
);
1692 em4x50_sim_send_bit(1);
1693 em4x50_sim_send_ack();
1695 // if "read after write" (raw) bit is set, send written data once
1697 int command
= em4x50_sim_send_listen_window(tag
, ledcontrol
);
1698 if (command
!= PM3_SUCCESS
) {
1702 command
= em4x50_sim_send_listen_window(tag
, ledcontrol
);
1703 if (command
!= PM3_SUCCESS
) {
1707 em4x50_sim_send_word(tag
[address
]);
1710 // continue with standard read mode
1711 return EM4X50_COMMAND_STANDARD_READ
;
1714 // write password process (simulation mode)
1715 static int em4x50_sim_handle_writepwd_command(uint32_t *tag
, bool ledcontrol
) {
1719 g_WritePasswordProcess
= true;
1722 uint32_t act_password
= 0;
1723 pwd
= em4x50_sim_read_word(&act_password
);
1725 // processing pause time tpp (corresponds to a "1" bit)
1726 em4x50_sim_send_bit(1);
1728 if (pwd
&& (act_password
== reflect32(tag
[EM4X50_DEVICE_PASSWORD
]))) {
1729 em4x50_sim_send_ack();
1732 em4x50_sim_send_nak();
1734 g_WritePasswordProcess
= false;
1736 // save transmitted password (to be used in standalone mode)
1737 g_Password
= act_password
;
1739 return EM4X50_COMMAND_STANDARD_READ
;
1742 int command
= em4x50_sim_send_listen_window(tag
, ledcontrol
);
1743 g_WritePasswordProcess
= false;
1744 if (command
!= EM4X50_COMMAND_WRITE_PASSWORD
) {
1748 // read new password
1749 uint32_t new_password
= 0;
1750 pwd
= em4x50_sim_read_word(&new_password
);
1752 // write access time twa
1753 wait_cycles(EM4X50_T_TAG_TWA
);
1756 em4x50_sim_send_ack();
1757 tag
[EM4X50_DEVICE_PASSWORD
] = reflect32(new_password
);
1758 g_Password
= new_password
;
1760 em4x50_sim_send_nak();
1761 return EM4X50_COMMAND_STANDARD_READ
;
1764 // EEPROM write time
1765 // strange: need some sort of 'waveform correction', otherwise ack signal
1766 // will not be detected; sending a single "1" as last part of Twee
1767 // seems to solve the problem
1768 wait_cycles(EM4X50_T_TAG_TWEE
- EM4X50_T_TAG_FULL_PERIOD
);
1769 em4x50_sim_send_bit(1);
1770 em4x50_sim_send_ack();
1772 // continue with standard read mode
1773 return EM4X50_COMMAND_STANDARD_READ
;
1776 void em4x50_handle_commands(int *command
, uint32_t *tag
, bool ledcontrol
) {
1780 case EM4X50_COMMAND_LOGIN
:
1781 *command
= em4x50_sim_handle_login_command(tag
, ledcontrol
);
1784 case EM4X50_COMMAND_RESET
:
1785 *command
= em4x50_sim_handle_reset_command(tag
, ledcontrol
);
1788 case EM4X50_COMMAND_WRITE
:
1789 *command
= em4x50_sim_handle_write_command(tag
, ledcontrol
);
1792 case EM4X50_COMMAND_WRITE_PASSWORD
:
1793 *command
= em4x50_sim_handle_writepwd_command(tag
, ledcontrol
);
1796 case EM4X50_COMMAND_SELECTIVE_READ
:
1797 *command
= em4x50_sim_handle_selective_read_command(tag
, ledcontrol
);
1800 case EM4X50_COMMAND_STANDARD_READ
:
1801 if (ledcontrol
) LED_C_OFF();
1802 *command
= em4x50_sim_handle_standard_read_command(tag
, ledcontrol
);
1805 // bit errors during reading may lead to unknown commands
1806 // -> continue with standard read mode
1808 *command
= EM4X50_COMMAND_STANDARD_READ
;
1813 // simulate uploaded data in emulator memory
1814 // LED C -> reader command has been detected
1815 // LED D -> operations that require authentication are possible
1816 void em4x50_sim(const uint32_t *password
, bool ledcontrol
) {
1818 int command
= PM3_ENODATA
;
1820 uint8_t *em4x50_mem
= BigBuf_get_EM_addr();
1821 uint32_t tag
[EM4X50_NO_WORDS
] = {0x0};
1823 for (int i
= 0; i
< EM4X50_NO_WORDS
; i
++)
1824 tag
[i
] = bytes_to_num(em4x50_mem
+ (i
* 4), 4);
1826 // via eload uploaded dump usually does not contain a password
1827 if (tag
[EM4X50_DEVICE_PASSWORD
] == 0) {
1828 tag
[EM4X50_DEVICE_PASSWORD
] = reflect32(*password
);
1831 // only if valid em4x50 data (e.g. uid == serial)
1832 if (tag
[EM4X50_DEVICE_SERIAL
] != tag
[EM4X50_DEVICE_ID
]) {
1835 if (ledcontrol
) LEDsoff();
1838 g_WritePasswordProcess
= false;
1840 // start with initial command = standard read mode
1841 command
= EM4X50_COMMAND_STANDARD_READ
;
1845 em4x50_handle_commands(&command
, tag
, ledcontrol
);
1847 // stop if key (pm3 button or enter key) has been pressed
1848 if (command
== PM3_EOPABORTED
) {
1852 // if timeout (e.g. no reader field) continue with standard read
1853 // mode and reset former authentication
1854 if (command
== PM3_ETIMEOUT
) {
1855 command
= EM4X50_COMMAND_STANDARD_READ
;
1857 if (ledcontrol
) LED_D_OFF();
1863 lf_finalize(ledcontrol
);
1864 reply_ng(CMD_LF_EM4X50_SIM
, command
, NULL
, 0);