proxmark3/armsrc/em4x50.c

1816 lines
49 KiB
C
Raw Normal View History

2020-06-15 20:32:51 +08:00
//-----------------------------------------------------------------------------
// Copyright (C) 2020 tharexde
//
// This code is licensed to you under the terms of the GNU GPL, version 2 or,
// at your option, any later version. See the LICENSE.txt file for the text of
// the license.
//-----------------------------------------------------------------------------
// Low frequency EM4x50 commands
//-----------------------------------------------------------------------------
#include "fpgaloader.h"
#include "ticks.h"
#include "dbprint.h"
2021-01-04 05:50:27 +08:00
#include "lfsampling.h"
2020-06-15 20:32:51 +08:00
#include "lfadc.h"
#include "lfdemod.h"
#include "commonutil.h"
2020-06-15 20:32:51 +08:00
#include "em4x50.h"
2020-10-25 23:40:39 +08:00
#include "BigBuf.h"
#include "spiffs.h"
#include "appmain.h" // tear
2020-06-15 20:32:51 +08:00
// Sam7s has several timers, we will use the source TIMER_CLOCK1 (aka AT91C_TC_CLKS_TIMER_DIV1_CLOCK)
// TIMER_CLOCK1 = MCK/2, MCK is running at 48 MHz, Timer is running at 48/2 = 24 MHz
// EM4x50 units (T0) have duration of 8 microseconds (us), which is 1/125000 per second (carrier)
// T0 = TIMER_CLOCK1 / 125000 = 192
#ifndef T0
#define T0 192
#endif
#define EM4X50_T_TAG_QUARTER_PERIOD 16
#define EM4X50_T_TAG_HALF_PERIOD 32
#define EM4X50_T_TAG_THREE_QUARTER_PERIOD 48
#define EM4X50_T_TAG_FULL_PERIOD 64
#define EM4X50_T_TAG_TPP 64
#define EM4X50_T_TAG_TWA 64
2021-01-04 05:50:27 +08:00
#define EM4X50_T_TAG_TINIT 2112
#define EM4X50_T_TAG_TWEE 3200
#define EM4X50_T_TAG_WAITING_FOR_SIGNAL 75
#define EM4X50_T_WAITING_FOR_DBLLIW 1550
2021-01-04 05:50:27 +08:00
#define EM4X50_T_WAITING_FOR_ACK 4
// the following value seems to be critical; if it's too low (e.g. < 120)
// some cards are no longer readable although they're ok
#define EM4X50_T_WAITING_FOR_SNGLLIW 140
2020-06-15 20:32:51 +08:00
#define EM4X50_TAG_TOLERANCE 8
#define EM4X50_TAG_WORD 45
2020-12-17 03:44:04 +08:00
#define EM4X50_TAG_MAX_NO_BYTES 136
2020-06-15 20:32:51 +08:00
#define EM4X50_COMMAND_LOGIN 0x01
#define EM4X50_COMMAND_RESET 0x80
2020-06-15 20:32:51 +08:00
#define EM4X50_COMMAND_WRITE 0x12
#define EM4X50_COMMAND_WRITE_PASSWORD 0x11
2020-06-15 20:32:51 +08:00
#define EM4X50_COMMAND_SELECTIVE_READ 0x0A
int gHigh = 190;
int gLow = 60;
2021-01-04 05:50:27 +08:00
int gcount = 0;
int gcycles = 0;
int rm = 0;
static bool em4x50_sim_send_listen_window(void);
static void em4x50_sim_handle_command(uint8_t command);
void catch_samples(void);
2020-12-09 19:18:01 +08:00
// do nothing for <period> using timer0
static void wait_timer(uint32_t period) {
2020-10-22 06:42:18 +08:00
AT91C_BASE_TC0->TC_CCR = AT91C_TC_SWTRG;
while (AT91C_BASE_TC0->TC_CV < period);
2020-06-15 20:32:51 +08:00
}
2021-01-04 05:50:27 +08:00
void catch_samples(void) {
2020-12-30 00:40:18 +08:00
uint8_t sample = 0;
if (EM4X50_MAX_NO_SAMPLES > CARD_MEMORY_SIZE) {
Dbprintf("exeeded emulator memory size");
return;
}
uint8_t *em4x50_sample_buffer = BigBuf_get_addr();
memcpy(em4x50_sample_buffer, &gHigh, 1);
memcpy(em4x50_sample_buffer + 1, &gLow, 1);
for (int i = 2; i < EM4X50_MAX_NO_SAMPLES + 2; i++) {
sample = AT91C_BASE_SSC->SSC_RHR;
memcpy(em4x50_sample_buffer + i, &sample, 1);
wait_timer(T0); // 8µs delay
}
}
2020-12-09 19:18:01 +08:00
// extract and check parities
// return result of parity check and extracted plain data
2020-11-20 06:18:04 +08:00
static bool extract_parities(uint64_t word, uint32_t *data) {
2020-12-12 21:46:40 +08:00
2020-11-20 06:18:04 +08:00
uint8_t row_parities = 0x0, col_parities = 0x0;
uint8_t row_parities_calculated = 0x0, col_parities_calculated = 0x0;
2020-12-12 21:46:40 +08:00
2020-11-20 06:18:04 +08:00
*data = 0x0;
2020-12-12 21:46:40 +08:00
2020-11-20 06:18:04 +08:00
// extract plain data (32 bits) from raw word (45 bits)
for (int i = 0; i < 4; i++) {
*data <<= 8;
*data |= (word >> ((4 - i) * 9 + 1)) & 0xFF;
}
2020-12-12 21:46:40 +08:00
2020-11-20 06:18:04 +08:00
// extract row parities (4 bits + stop bit) from raw word (45 bits)
for (int i = 0; i < 5; i++) {
row_parities <<= 1;
row_parities |= (word >> ((4 - i) * 9)) & 0x1;
}
// extract col_parities (8 bits, no stop bit) from raw word (45 bits)
col_parities = (word >> 1) & 0xFF;
// check extracted parities against extracted data
// calculate row parities from data
for (int i = 0; i < 4; i++) {
row_parities_calculated <<= 1;
for (int j = 0; j < 8; j++) {
row_parities_calculated ^= (*data >> ((3 - i) * 8 + (7 - j))) & 0x1;
}
}
// add stop bit (always zero)
row_parities_calculated <<= 1;
// calculate column parities from data
for (int i = 0; i < 8; i++) {
col_parities_calculated <<= 1;
for (int j = 0; j < 4; j++) {
col_parities_calculated ^= (*data >> ((3 - j) * 8 + (7 - i))) & 0x1;
}
}
2020-12-12 21:46:40 +08:00
2020-11-20 06:18:04 +08:00
if ((row_parities == row_parities_calculated) && (col_parities == col_parities_calculated))
return true;
return false;
}
2020-06-15 20:32:51 +08:00
static void em4x50_setup_read(void) {
2020-07-12 02:53:07 +08:00
2020-06-15 20:32:51 +08:00
FpgaDownloadAndGo(FPGA_BITSTREAM_LF);
FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_ADC | FPGA_LF_ADC_READER_FIELD);
// 50ms for the resonant antenna to settle.
SpinDelay(50);
2020-09-06 05:58:21 +08:00
2020-06-15 20:32:51 +08:00
// Now set up the SSC to get the ADC samples that are now streaming at us.
2020-07-02 18:33:53 +08:00
FpgaSetupSsc(FPGA_MAJOR_MODE_LF_READER);
2020-07-12 02:53:07 +08:00
2020-06-15 20:32:51 +08:00
FpgaSendCommand(FPGA_CMD_SET_DIVISOR, LF_DIVISOR_125);
// Connect the A/D to the peak-detected low-frequency path.
SetAdcMuxFor(GPIO_MUXSEL_LOPKD);
2020-07-12 02:53:07 +08:00
2020-06-15 20:32:51 +08:00
// Steal this pin from the SSP (SPI communication channel with fpga) and
// use it to control the modulation
AT91C_BASE_PIOA->PIO_PER = GPIO_SSC_DOUT;
AT91C_BASE_PIOA->PIO_OER = GPIO_SSC_DOUT;
// Disable modulation at default, which means enable the field
LOW(GPIO_SSC_DOUT);
2020-07-12 02:53:07 +08:00
2020-06-15 20:32:51 +08:00
// Enable Peripheral Clock for
// TIMER_CLOCK0, used to measure exact timing before answering
2021-01-04 05:50:27 +08:00
AT91C_BASE_PMC->PMC_PCER |= (1 << AT91C_ID_TC0) | (1 << AT91C_ID_TC1);
2020-06-18 05:08:02 +08:00
AT91C_BASE_PIOA->PIO_BSR = GPIO_SSC_FRAME;
2020-06-15 20:32:51 +08:00
// Disable timer during configuration
AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKDIS;
2021-01-04 05:50:27 +08:00
AT91C_BASE_TC1->TC_CCR = AT91C_TC_CLKDIS;
2020-06-15 20:32:51 +08:00
// TC0: Capture mode, default timer source = MCK/2 (TIMER_CLOCK1), no triggers
AT91C_BASE_TC0->TC_CMR = AT91C_TC_CLKS_TIMER_DIV1_CLOCK;
2021-01-04 05:50:27 +08:00
AT91C_BASE_TC1->TC_CMR = AT91C_TC_CLKS_TIMER_DIV1_CLOCK;
2020-07-12 02:53:07 +08:00
2020-06-15 20:32:51 +08:00
// TC1: Capture mode, default timer source = MCK/2 (TIMER_CLOCK1), no triggers
2020-07-12 02:53:07 +08:00
2020-06-15 20:32:51 +08:00
// Enable and reset counters
AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKEN | AT91C_TC_SWTRG;
2021-01-04 05:50:27 +08:00
AT91C_BASE_TC1->TC_CCR = AT91C_TC_CLKEN | AT91C_TC_SWTRG;
2020-07-12 02:53:07 +08:00
2020-06-15 20:32:51 +08:00
// synchronized startup procedure
while (AT91C_BASE_TC0->TC_CV > 0) {}; // wait until TC1 returned to zero
2020-07-12 02:53:07 +08:00
2020-06-15 20:32:51 +08:00
// Watchdog hit
WDT_HIT();
}
static void em4x50_setup_sim(void) {
2021-01-04 05:50:27 +08:00
StopTicks();
FpgaDownloadAndGo(FPGA_BITSTREAM_LF);
2021-01-04 05:50:27 +08:00
sample_config *sc = getSamplingConfig();
sc->decimation = 1;
sc->averaging = 0;
sc->divisor = LF_DIVISOR_125;
FpgaSendCommand(FPGA_CMD_SET_DIVISOR, sc->divisor);
FpgaWriteConfWord(FPGA_MAJOR_MODE_LF_EDGE_DETECT);
AT91C_BASE_PIOA->PIO_PER = GPIO_SSC_DOUT | GPIO_SSC_CLK;
AT91C_BASE_PIOA->PIO_OER = GPIO_SSC_DOUT;
AT91C_BASE_PIOA->PIO_ODR = GPIO_SSC_CLK;
2021-01-04 05:50:27 +08:00
AT91C_BASE_PMC->PMC_PCER |= (1 << AT91C_ID_TC0);
AT91C_BASE_PIOA->PIO_BSR = GPIO_SSC_FRAME;
AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKDIS;
AT91C_BASE_TC0->TC_CMR = AT91C_TC_CLKS_TIMER_DIV1_CLOCK;
AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKEN | AT91C_TC_SWTRG;
}
2020-12-09 19:18:01 +08:00
// calculate signal properties (mean amplitudes) from measured data:
// 32 amplitudes (maximum values) -> mean amplitude value -> gHigh -> gLow
static bool get_signalproperties(void) {
2020-07-12 02:53:07 +08:00
bool signal_found = false;
int no_periods = 32, pct = 75, noise = 140;
2020-07-12 02:53:07 +08:00
uint8_t sample_ref = 127;
uint8_t sample_max_mean = 0;
uint8_t sample_max[no_periods];
uint32_t sample_max_sum = 0;
memset(sample_max, 0x00, sizeof(sample_max));
LED_A_ON();
// wait until signal/noise > 1 (max. 32 periods)
for (int i = 0; i < EM4X50_T_TAG_WAITING_FOR_SIGNAL; i++) {
2020-07-12 02:53:07 +08:00
2020-09-05 19:25:57 +08:00
if (BUTTON_PRESS()) return false;
2020-09-24 05:45:43 +08:00
// about 2 samples per bit period
wait_timer(T0 * EM4X50_T_TAG_HALF_PERIOD);
2020-12-12 21:46:40 +08:00
// ignore first samples
if ((i > SIGNAL_IGNORE_FIRST_SAMPLES) && (AT91C_BASE_SSC->SSC_RHR > noise)) {
signal_found = true;
break;
}
}
2020-07-12 02:53:07 +08:00
if (signal_found == false) {
LED_A_OFF();
return false;
}
// calculate mean maximum value of 32 periods, each period has a length of
// 3 single "full periods" to eliminate the influence of a listen window
for (int i = 0; i < no_periods; i++) {
AT91C_BASE_TC0->TC_CCR = AT91C_TC_SWTRG;
while (AT91C_BASE_TC0->TC_CV < T0 * 3 * EM4X50_T_TAG_FULL_PERIOD) {
2020-07-12 02:53:07 +08:00
2020-09-05 19:25:57 +08:00
if (BUTTON_PRESS()) return false;
2020-07-12 02:53:07 +08:00
2020-09-24 05:45:43 +08:00
volatile uint8_t sample = (uint8_t)AT91C_BASE_SSC->SSC_RHR;
if (sample > sample_max[i])
sample_max[i] = sample;
2020-07-12 02:53:07 +08:00
}
2020-07-12 02:53:07 +08:00
sample_max_sum += sample_max[i];
}
2020-07-12 02:53:07 +08:00
sample_max_mean = sample_max_sum / no_periods;
2020-07-12 02:53:07 +08:00
// set global envelope variables
gHigh = sample_ref + pct * (sample_max_mean - sample_ref) / 100;
gLow = sample_ref - pct * (sample_max_mean - sample_ref) / 100;
LED_A_OFF();
2020-12-12 21:46:40 +08:00
return true;
}
2020-12-09 19:18:01 +08:00
// returns true if bit is undefined by evaluating a single sample within
// a bit period (given there is no LIW, ACK or NAK)
// This function is used for identifying a listen window in functions
// "find_double_listen_window" and "check_ack"
2020-10-22 06:55:29 +08:00
static bool invalid_bit(void) {
2020-07-12 02:53:07 +08:00
2020-06-15 20:32:51 +08:00
// get sample at 3/4 of bit period
wait_timer(T0 * EM4X50_T_TAG_THREE_QUARTER_PERIOD);
2020-12-30 00:40:18 +08:00
//wait_timer(T0 * EM4X50_T_TAG_HALF_PERIOD);
2020-07-12 02:53:07 +08:00
uint8_t sample = (uint8_t)AT91C_BASE_SSC->SSC_RHR;
2020-06-15 20:32:51 +08:00
// wait until end of bit period
wait_timer(T0 * EM4X50_T_TAG_QUARTER_PERIOD);
2020-12-30 00:40:18 +08:00
//wait_timer(T0 * EM4X50_T_TAG_HALF_PERIOD);
2020-06-15 20:32:51 +08:00
2020-10-22 06:42:18 +08:00
// bit in "undefined" state?
if (sample <= gHigh && sample >= gLow)
return true;
2020-07-12 02:53:07 +08:00
2020-10-22 06:42:18 +08:00
return false;
2020-06-15 20:32:51 +08:00
}
static uint32_t get_pulse_length(void) {
2020-08-13 18:25:04 +08:00
2020-09-24 05:45:43 +08:00
int32_t timeout = (T0 * 3 * EM4X50_T_TAG_FULL_PERIOD);
2020-08-13 18:25:04 +08:00
2020-09-05 18:50:30 +08:00
// iterates pulse length (low -> high -> low)
2020-09-24 05:45:43 +08:00
2020-09-05 18:50:30 +08:00
volatile uint8_t sample = (uint8_t)AT91C_BASE_SSC->SSC_RHR;
2020-08-13 18:25:04 +08:00
2020-10-22 06:42:18 +08:00
while (sample > gLow && (timeout--))
2020-06-15 20:32:51 +08:00
sample = (uint8_t)AT91C_BASE_SSC->SSC_RHR;
2020-09-24 05:45:43 +08:00
2020-09-05 18:50:30 +08:00
if (timeout == 0)
return 0;
2020-06-15 20:32:51 +08:00
2020-11-16 04:59:25 +08:00
AT91C_BASE_TC0->TC_CCR = AT91C_TC_SWTRG;
timeout = (T0 * 3 * EM4X50_T_TAG_FULL_PERIOD);
2020-09-24 05:45:43 +08:00
2020-10-22 06:42:18 +08:00
while (sample < gHigh && (timeout--))
2020-06-15 20:32:51 +08:00
sample = (uint8_t)AT91C_BASE_SSC->SSC_RHR;
2020-09-05 18:50:30 +08:00
if (timeout == 0)
return 0;
2020-06-15 20:32:51 +08:00
2020-09-05 18:50:30 +08:00
timeout = (T0 * 3 * EM4X50_T_TAG_FULL_PERIOD);
2020-10-22 06:42:18 +08:00
while (sample > gLow && (timeout--))
2020-06-15 20:32:51 +08:00
sample = (uint8_t)AT91C_BASE_SSC->SSC_RHR;
2020-09-05 18:50:30 +08:00
if (timeout == 0)
return 0;
2020-11-16 04:59:25 +08:00
return (uint32_t)AT91C_BASE_TC0->TC_CV;
2020-09-06 05:58:21 +08:00
2020-06-15 20:32:51 +08:00
}
2020-12-09 19:18:01 +08:00
// check if pulse length <pl> corresponds to given length <length>
2020-06-15 20:32:51 +08:00
static bool check_pulse_length(uint32_t pl, int length) {
2020-12-12 21:46:40 +08:00
return ((pl >= T0 * (length - EM4X50_TAG_TOLERANCE)) && (pl <= T0 * (length + EM4X50_TAG_TOLERANCE)));
2020-06-15 20:32:51 +08:00
}
2020-12-09 19:18:01 +08:00
// send single bit according to EM4x50 application note and datasheet
2020-09-05 19:25:57 +08:00
static void em4x50_reader_send_bit(int bit) {
2020-06-15 20:32:51 +08:00
// reset clock for the next bit
AT91C_BASE_TC0->TC_CCR = AT91C_TC_SWTRG;
if (bit == 0) {
2021-01-04 05:50:27 +08:00
// disable modulation (activate the field) for 7 cycles of carrier
2020-06-15 20:32:51 +08:00
// period (Opt64)
LOW(GPIO_SSC_DOUT);
while (AT91C_BASE_TC0->TC_CV < T0 * 7);
2021-01-04 05:50:27 +08:00
// enable modulation (drop the field) for remaining first
2020-06-15 20:32:51 +08:00
// half of bit period
HIGH(GPIO_SSC_DOUT);
while (AT91C_BASE_TC0->TC_CV < T0 * EM4X50_T_TAG_HALF_PERIOD);
2020-08-13 18:25:04 +08:00
2020-06-15 20:32:51 +08:00
// disable modulation for second half of bit period
LOW(GPIO_SSC_DOUT);
while (AT91C_BASE_TC0->TC_CV < T0 * EM4X50_T_TAG_FULL_PERIOD);
} else {
2020-08-13 18:25:04 +08:00
2020-06-15 20:32:51 +08:00
// bit = "1" means disable modulation for full bit period
LOW(GPIO_SSC_DOUT);
while (AT91C_BASE_TC0->TC_CV < T0 * EM4X50_T_TAG_FULL_PERIOD);
}
}
2020-09-05 19:25:57 +08:00
2020-12-09 19:18:01 +08:00
// send byte (without parity)
2020-09-05 19:25:57 +08:00
static void em4x50_reader_send_byte(uint8_t byte) {
2020-12-09 19:18:01 +08:00
for (int i = 0; i < 8; i++) {
2020-09-24 05:45:43 +08:00
em4x50_reader_send_bit((byte >> (7 - i)) & 1);
2020-12-09 19:18:01 +08:00
}
2020-09-05 19:25:57 +08:00
}
2020-06-15 20:32:51 +08:00
2020-12-09 19:18:01 +08:00
// send byte followed by its (equal) parity bit
2020-09-05 19:25:57 +08:00
static void em4x50_reader_send_byte_with_parity(uint8_t byte) {
int parity = 0, bit = 0;
2020-09-24 05:45:43 +08:00
2020-09-05 19:25:57 +08:00
for (int i = 0; i < 8; i++) {
2020-09-24 05:45:43 +08:00
bit = (byte >> (7 - i)) & 1;
2020-09-05 19:25:57 +08:00
em4x50_reader_send_bit(bit);
parity ^= bit;
}
em4x50_reader_send_bit(parity);
}
2020-06-15 20:32:51 +08:00
2020-12-09 19:18:01 +08:00
// send 32 bit word with parity bits according to EM4x50 datasheet
// word hast be sent in msb notation
2020-10-22 06:42:18 +08:00
static void em4x50_reader_send_word(const uint32_t word) {
uint8_t bytes[4] = {0x0, 0x0, 0x0, 0x0};
2020-12-12 21:46:40 +08:00
2020-10-22 06:42:18 +08:00
for (int i = 0; i < 4; i++) {
bytes[i] = (word >> (24 - (8 * i))) & 0xFF;
2020-09-05 19:25:57 +08:00
em4x50_reader_send_byte_with_parity(bytes[i]);
2020-10-22 06:42:18 +08:00
}
2020-12-12 21:46:40 +08:00
2020-09-05 19:25:57 +08:00
// send column parities
em4x50_reader_send_byte(bytes[0] ^ bytes[1] ^ bytes[2] ^ bytes[3]);
2020-06-15 20:32:51 +08:00
2020-09-05 19:25:57 +08:00
// send final stop bit (always "0")
em4x50_reader_send_bit(0);
}
2020-06-15 20:32:51 +08:00
2020-12-09 19:18:01 +08:00
// find single listen window
2020-09-05 18:50:30 +08:00
static bool find_single_listen_window(void) {
2020-07-01 06:45:46 +08:00
int cnt_pulses = 0;
2020-12-12 21:46:40 +08:00
LED_B_ON();
2020-08-13 18:25:04 +08:00
2020-09-05 18:50:30 +08:00
while (cnt_pulses < EM4X50_T_WAITING_FOR_SNGLLIW) {
2020-08-13 18:25:04 +08:00
2020-07-01 06:45:46 +08:00
// identification of listen window is done via evaluation of
// pulse lengths
if (check_pulse_length(get_pulse_length(), 3 * EM4X50_T_TAG_FULL_PERIOD)) {
2020-08-13 18:25:04 +08:00
2020-07-01 06:45:46 +08:00
if (check_pulse_length(get_pulse_length(), 2 * EM4X50_T_TAG_FULL_PERIOD)) {
2020-08-13 18:25:04 +08:00
// found listen window
LED_B_OFF();
2020-07-01 06:45:46 +08:00
return true;
}
}
2020-09-06 05:58:21 +08:00
cnt_pulses++;
2020-07-01 06:45:46 +08:00
}
2020-08-13 18:25:04 +08:00
LED_B_OFF();
2020-07-01 06:45:46 +08:00
return false;
}
2020-12-09 19:18:01 +08:00
// find two successive listen windows that indicate the beginning of
// data transmission
// double listen window to be detected within 1600 pulses -> worst case
// reason: first detectable double listen window after 34 words
// -> 34 words + 34 single listen windows -> about 1600 pulses
2020-10-04 04:59:21 +08:00
static int find_double_listen_window(bool bcommand) {
int cnt_pulses = 0;
2020-12-12 21:46:40 +08:00
LED_B_ON();
2020-08-13 18:25:04 +08:00
2020-09-05 18:50:30 +08:00
while (cnt_pulses < EM4X50_T_WAITING_FOR_DBLLIW) {
2020-08-13 18:25:04 +08:00
if (BUTTON_PRESS())
return PM3_EOPABORTED;
2020-06-15 20:32:51 +08:00
// identification of listen window is done via evaluation of
// pulse lengths
if (check_pulse_length(get_pulse_length(), 3 * EM4X50_T_TAG_FULL_PERIOD)) {
2020-08-13 18:25:04 +08:00
2020-06-15 20:32:51 +08:00
if (check_pulse_length(get_pulse_length(), 2 * EM4X50_T_TAG_FULL_PERIOD)) {
2020-08-13 18:25:04 +08:00
2020-06-15 20:32:51 +08:00
// first listen window found
2020-08-13 18:25:04 +08:00
2020-06-15 20:32:51 +08:00
if (bcommand) {
2021-01-04 05:50:27 +08:00
2020-06-15 20:32:51 +08:00
// data transmission from card has to be stopped, because
// a commamd shall be issued
2020-08-13 18:25:04 +08:00
2020-10-24 07:18:06 +08:00
// unfortunately the position in listen window (where
2020-06-15 20:32:51 +08:00
// command request has to be sent) has gone, so if a
// second window follows - sync on this to issue a command
2020-08-13 18:25:04 +08:00
2020-06-15 20:32:51 +08:00
// skip the next bit...
wait_timer(T0 * EM4X50_T_TAG_FULL_PERIOD);
2020-08-13 18:25:04 +08:00
2020-06-15 20:32:51 +08:00
// ...and check if the following bit does make sense
// (if not it is the correct position within the second
// listen window)
2020-10-22 06:55:29 +08:00
if (invalid_bit()) {
2020-08-13 18:25:04 +08:00
2020-06-15 20:32:51 +08:00
// send RM for request mode
2020-09-05 19:25:57 +08:00
em4x50_reader_send_bit(0);
em4x50_reader_send_bit(0);
LED_B_OFF();
return PM3_SUCCESS;
2020-06-15 20:32:51 +08:00
}
}
if (check_pulse_length(get_pulse_length(), 3 * EM4X50_T_TAG_FULL_PERIOD)) {
LED_B_OFF();
2020-06-15 20:32:51 +08:00
// return although second listen window consists of one
// more bit period but this period is necessary for
// evaluating further pulse lengths
return PM3_SUCCESS;
2020-06-15 20:32:51 +08:00
}
}
}
2020-10-04 04:59:21 +08:00
cnt_pulses++;
2020-06-15 20:32:51 +08:00
}
2020-08-13 18:25:04 +08:00
2020-12-12 21:46:40 +08:00
LED_B_OFF();
return PM3_EFAILED;
2020-06-15 20:32:51 +08:00
}
2020-12-09 19:18:01 +08:00
// function is used to check wether a tag on the proxmark is an
// EM4x50 tag or not -> speed up "lf search" process
2020-07-01 06:45:46 +08:00
static bool find_em4x50_tag(void) {
2020-09-06 05:58:21 +08:00
return find_single_listen_window();
2020-07-01 06:45:46 +08:00
}
2020-12-09 19:18:01 +08:00
// To issue a command we have to find a listen window first.
// Because identification and synchronization at the same time is not
// possible when using pulse lengths a double listen window is used.
2020-10-04 04:59:21 +08:00
static int request_receive_mode(void) {
2020-10-12 02:06:03 +08:00
return find_double_listen_window(true);
2020-06-15 20:32:51 +08:00
}
2020-12-09 19:18:01 +08:00
// returns true if signal structue corresponds to ACK, anything else is
// counted as NAK (-> false)
// Only relevant for pasword writing function:
// If <bliw> is true then within the single listen window right after the
// ack signal a RM request has to be sent.
2020-06-15 20:32:51 +08:00
static bool check_ack(bool bliw) {
2021-01-04 05:50:27 +08:00
int cnt_pulses = 0;
2020-06-15 20:32:51 +08:00
AT91C_BASE_TC0->TC_CCR = AT91C_TC_SWTRG;
2021-01-04 05:50:27 +08:00
//while (cnt_pulses < EM4X50_T_WAITING_FOR_ACK) {
2020-06-15 20:32:51 +08:00
while (AT91C_BASE_TC0->TC_CV < T0 * 4 * EM4X50_T_TAG_FULL_PERIOD) {
2020-08-13 18:25:04 +08:00
if (BUTTON_PRESS())
return false;
2021-01-04 05:50:27 +08:00
2020-06-15 20:32:51 +08:00
if (check_pulse_length(get_pulse_length(), 2 * EM4X50_T_TAG_FULL_PERIOD)) {
2020-08-13 18:25:04 +08:00
2021-01-04 05:50:27 +08:00
catch_samples();
2020-06-15 20:32:51 +08:00
// The received signal is either ACK or NAK.
2020-08-13 18:25:04 +08:00
2020-06-15 20:32:51 +08:00
if (check_pulse_length(get_pulse_length(), 2 * EM4X50_T_TAG_FULL_PERIOD)) {
2020-08-13 18:25:04 +08:00
2020-06-15 20:32:51 +08:00
// Now the signal must be ACK.
if (!bliw) {
return true;
} else {
2020-08-13 18:25:04 +08:00
2020-06-15 20:32:51 +08:00
// send RM request after ack signal
// wait for 2 bits (remaining "bit" of ACK signal + first
// "bit" of listen window)
wait_timer(T0 * 2 * EM4X50_T_TAG_FULL_PERIOD);
2020-08-13 18:25:04 +08:00
2020-10-24 07:18:06 +08:00
// check for listen window (if first bit cannot be interpreted
2020-06-15 20:32:51 +08:00
// as a valid bit it must belong to a listen window)
2020-10-22 06:55:29 +08:00
if (invalid_bit()) {
2020-08-13 18:25:04 +08:00
2020-06-15 20:32:51 +08:00
// send RM for request mode
2020-09-05 19:25:57 +08:00
em4x50_reader_send_bit(0);
em4x50_reader_send_bit(0);
2020-06-15 20:32:51 +08:00
return true;
}
}
} else {
2020-08-13 18:25:04 +08:00
// It's NAK -> stop searching
break;
2020-06-15 20:32:51 +08:00
}
}
2021-01-04 05:50:27 +08:00
cnt_pulses++;
2020-06-15 20:32:51 +08:00
}
return false;
}
2020-12-09 19:18:01 +08:00
// decodes one word by evaluating pulse lengths and previous bit;
// word must have 45 bits in total:
// 32 data bits + 4 row parity bits + 8 column parity bits + 1 stop bit
2020-10-12 02:06:03 +08:00
static int get_word_from_bitstream(uint32_t *data) {
bool bitchange = false;
int cnt = 0;
2020-06-15 20:32:51 +08:00
uint32_t pl = 0;
2020-10-12 02:06:03 +08:00
uint64_t word = 0x0;
2020-12-12 21:46:40 +08:00
LED_C_ON();
2020-12-12 21:46:40 +08:00
2020-10-12 02:06:03 +08:00
*data = 0x0;
2020-06-15 20:32:51 +08:00
// initial bit value depends on last pulse length of listen window
pl = get_pulse_length();
if (check_pulse_length(pl, 3 * EM4X50_T_TAG_HALF_PERIOD)) {
// pulse length = 1.5
2020-10-12 02:06:03 +08:00
word = 0x1;
2020-06-15 20:32:51 +08:00
} else if (check_pulse_length(pl, 2 * EM4X50_T_TAG_FULL_PERIOD)) {
// pulse length = 2
2020-10-12 02:06:03 +08:00
bitchange = true;
2020-06-15 20:32:51 +08:00
} else {
2020-08-13 18:25:04 +08:00
2020-06-15 20:32:51 +08:00
// pulse length = 2.5
2020-10-12 02:06:03 +08:00
word = 0x1;
cnt++;
2020-06-15 20:32:51 +08:00
}
// identify remaining bits based on pulse lengths
// between two listen windows only pulse lengths of 1, 1.5 and 2 are possible
while (BUTTON_PRESS() == false) {
2020-09-24 05:45:43 +08:00
2020-10-12 02:06:03 +08:00
cnt++;
word <<= 1;
2020-12-12 21:46:40 +08:00
2020-06-15 20:32:51 +08:00
pl = get_pulse_length();
2020-08-13 18:25:04 +08:00
2020-06-15 20:32:51 +08:00
if (check_pulse_length(pl, EM4X50_T_TAG_FULL_PERIOD)) {
// pulse length = 1 -> keep former bit value
2020-10-12 02:06:03 +08:00
word |= (word >> 1) & 0x1;
2020-06-15 20:32:51 +08:00
} else if (check_pulse_length(pl, 3 * EM4X50_T_TAG_HALF_PERIOD)) {
// pulse length = 1.5 -> decision on bit change
2020-08-13 18:25:04 +08:00
2020-10-12 02:06:03 +08:00
if (bitchange) {
2020-06-15 20:32:51 +08:00
// if number of pulse lengths with 1.5 periods is even -> add bit
2020-10-12 02:06:03 +08:00
word |= (word >> 1) & 0x1;
word <<= 1;
2020-06-15 20:32:51 +08:00
// pulse length of 1.5 changes bit value
2020-10-12 02:06:03 +08:00
word |= ((word >> 1) & 0x1) ^ 0x1;
cnt++;
2020-08-13 18:25:04 +08:00
2020-06-15 20:32:51 +08:00
// next time add only one bit
2020-10-12 02:06:03 +08:00
bitchange = false;
2020-08-13 18:25:04 +08:00
2020-06-15 20:32:51 +08:00
} else {
2020-10-12 02:06:03 +08:00
word |= ((word >> 1) & 0x1) ^ 0x1;
2020-08-13 18:25:04 +08:00
2020-06-15 20:32:51 +08:00
// next time two bits have to be added
2020-10-12 02:06:03 +08:00
bitchange = true;
2020-06-15 20:32:51 +08:00
}
} else if (check_pulse_length(pl, 2 * EM4X50_T_TAG_FULL_PERIOD)) {
// pulse length of 2 means: adding 2 bits "01"
2020-10-12 02:06:03 +08:00
cnt++;
word <<= 1;
word |= 0x1;
2020-06-15 20:32:51 +08:00
} else if (check_pulse_length(pl, 3 * EM4X50_T_TAG_FULL_PERIOD)) {
LED_C_OFF();
2020-06-15 20:32:51 +08:00
// pulse length of 3 indicates listen window -> clear last
2020-10-12 02:06:03 +08:00
// bit (= 0) and return (without parities)
word >>= 2;
return (extract_parities(word, data)) ? --cnt : 0;
2020-06-15 20:32:51 +08:00
}
}
2020-12-12 21:46:40 +08:00
LED_C_OFF();
2020-12-12 21:46:40 +08:00
return PM3_EOPABORTED;
2020-06-15 20:32:51 +08:00
}
2020-11-20 06:18:04 +08:00
static bool em4x50_sim_send_bit(uint8_t bit) {
uint16_t check = 0;
for (int t = 0; t < EM4X50_T_TAG_FULL_PERIOD; t++) {
// wait until SSC_CLK goes HIGH
// used as a simple detection of a reader field?
while (!(AT91C_BASE_PIOA->PIO_PDSR & GPIO_SSC_CLK)) {
WDT_HIT();
if (check == 1000) {
if (BUTTON_PRESS())
return false;
check = 0;
}
++check;
}
if (bit)
OPEN_COIL();
else
SHORT_COIL();
check = 0;
//wait until SSC_CLK goes LOW
while (AT91C_BASE_PIOA->PIO_PDSR & GPIO_SSC_CLK) {
WDT_HIT();
if (check == 1000) {
if (BUTTON_PRESS())
return false;
check = 0;
}
++check;
}
if (t == EM4X50_T_TAG_HALF_PERIOD)
bit ^= 1;
}
return true;
}
static bool em4x50_sim_send_byte(uint8_t byte) {
// send byte
for (int i = 0; i < 8; i++)
if (!em4x50_sim_send_bit((byte >> (7 - i)) & 1))
return false;
return true;
}
static bool em4x50_sim_send_byte_with_parity(uint8_t byte) {
uint8_t parity = 0x0;
// send byte with parity (even)
for (int i = 0; i < 8; i++)
parity ^= (byte >> i) & 1;
2020-12-09 19:18:01 +08:00
if (em4x50_sim_send_byte(byte) == false)
2020-11-20 06:18:04 +08:00
return false;;
2020-12-09 19:18:01 +08:00
if (em4x50_sim_send_bit(parity) == false)
2020-11-20 06:18:04 +08:00
return false;
return true;
}
2020-12-18 03:55:01 +08:00
static bool em4x50_sim_send_word(uint32_t word) {
2020-11-20 06:18:04 +08:00
uint8_t cparity = 0x00;
// word has tobe sent in msb, not lsb
word = reflect32(word);
2020-12-12 21:46:40 +08:00
2020-11-20 06:18:04 +08:00
// 4 bytes each with even row parity bit
2020-12-09 19:18:01 +08:00
for (int i = 0; i < 4; i++) {
if (em4x50_sim_send_byte_with_parity((word >> ((3 - i) * 8)) & 0xFF) == false) {
2020-11-20 06:18:04 +08:00
return false;
2020-12-09 19:18:01 +08:00
}
}
2020-11-20 06:18:04 +08:00
// column parity
for (int i = 0; i < 8; i++) {
cparity <<= 1;
for (int j = 0; j < 4; j++) {
cparity ^= (((word >> ((3 - j) * 8)) & 0xFF) >> (7 - i)) & 1;
}
}
2020-12-09 19:18:01 +08:00
if (em4x50_sim_send_byte(cparity) == false)
2020-11-20 06:18:04 +08:00
return false;
// stop bit
2020-12-09 19:18:01 +08:00
if (em4x50_sim_send_bit(0) == false)
2020-11-20 06:18:04 +08:00
return false;
return true;
}
2021-01-04 05:50:27 +08:00
static int wait_cycles(int maxperiods) {
int period = 0;
while (period < maxperiods) {
while (!(AT91C_BASE_PIOA->PIO_PDSR & GPIO_SSC_CLK));
while (AT91C_BASE_PIOA->PIO_PDSR & GPIO_SSC_CLK);
period++;
}
return PM3_SUCCESS;
}
static int get_cycles(void) {
int cycles = 0;
AT91C_BASE_TC0->TC_CCR = AT91C_TC_SWTRG;
while (AT91C_BASE_TC0->TC_CV < T0 * EM4X50_T_TAG_FULL_PERIOD) {
while (!(AT91C_BASE_PIOA->PIO_PDSR & GPIO_SSC_CLK));
// check again to minimize desynchronization
if (AT91C_BASE_TC0->TC_CV >= T0 * EM4X50_T_TAG_FULL_PERIOD)
break;
while (AT91C_BASE_PIOA->PIO_PDSR & GPIO_SSC_CLK);
cycles++;
}
return cycles;
}
static uint8_t em4x50_sim_read_bit(void) {
int cond = EM4X50_T_TAG_FULL_PERIOD - EM4X50_TAG_TOLERANCE;
return (get_cycles() < cond) ? 0 : 1;
}
static uint8_t em4x50_sim_read_byte(void) {
uint8_t byte = 0;
for (int i = 0; i < 8; i++) {
byte <<= 1;
byte |= em4x50_sim_read_bit();
}
return byte;
}
static uint8_t em4x50_sim_read_byte_with_parity_check(void) {
uint8_t byte = 0, parity = 0, pval = 0;
for (int i = 0; i < 8; i++) {
byte <<= 1;
byte |= em4x50_sim_read_bit();
parity ^= (byte & 1);
}
pval = em4x50_sim_read_bit();
return (parity == pval) ? byte : 0;
}
static uint32_t em4x50_sim_read_word(void) {
uint8_t parities = 0, parities_calculated = 0, stop_bit = 0;
uint8_t bytes[4] = {0};
// read plain data
for (int i = 0; i < 4; i++) {
bytes[i] = em4x50_sim_read_byte_with_parity_check();
}
// read column parities and stop bit
parities = em4x50_sim_read_byte();
stop_bit = em4x50_sim_read_bit();
// calculate column parities from data
for (int i = 0; i < 8; i++) {
parities_calculated <<= 1;
for (int j = 0; j < 4; j++) {
parities_calculated ^= (bytes[j] >> (7 - i)) & 1;
}
}
// check parities
if ((parities == parities_calculated) && (stop_bit == 0)) {
return BYTES2UINT32(bytes);
}
return 0;
}
static void em4x50_sim_send_ack(void) {
SHORT_COIL();
wait_cycles(EM4X50_T_TAG_HALF_PERIOD);
OPEN_COIL();
wait_cycles(EM4X50_T_TAG_HALF_PERIOD);
SHORT_COIL();
wait_cycles(3 * EM4X50_T_TAG_HALF_PERIOD);
OPEN_COIL();
wait_cycles(EM4X50_T_TAG_HALF_PERIOD);
SHORT_COIL();
wait_cycles(3 * EM4X50_T_TAG_HALF_PERIOD);
OPEN_COIL();
wait_cycles(EM4X50_T_TAG_HALF_PERIOD);
SHORT_COIL();
}
static void em4x50_sim_send_nak(void) {
SHORT_COIL();
wait_cycles(EM4X50_T_TAG_HALF_PERIOD);
OPEN_COIL();
wait_cycles(EM4X50_T_TAG_HALF_PERIOD);
SHORT_COIL();
wait_cycles(3 * EM4X50_T_TAG_HALF_PERIOD);
OPEN_COIL();
wait_cycles(EM4X50_T_TAG_HALF_PERIOD);
SHORT_COIL();
wait_cycles(EM4X50_T_TAG_FULL_PERIOD);
OPEN_COIL();
wait_cycles(EM4X50_T_TAG_HALF_PERIOD);
SHORT_COIL();
wait_cycles(EM4X50_T_TAG_HALF_PERIOD);
}
/*
2020-12-18 03:55:01 +08:00
static bool em4x50_sim_send_listen_window(void) {
2020-11-20 06:18:04 +08:00
uint16_t check = 0;
for (int t = 0; t < 5 * EM4X50_T_TAG_FULL_PERIOD; t++) {
// wait until SSC_CLK goes HIGH
while (!(AT91C_BASE_PIOA->PIO_PDSR & GPIO_SSC_CLK)) {
WDT_HIT();
2020-12-28 05:22:44 +08:00
2020-11-20 06:18:04 +08:00
if (check == 1000) {
if (BUTTON_PRESS())
return false;
check = 0;
}
++check;
}
if (t >= 4 * EM4X50_T_TAG_FULL_PERIOD)
SHORT_COIL();
else if (t >= 3 * EM4X50_T_TAG_FULL_PERIOD)
OPEN_COIL();
else if (t >= EM4X50_T_TAG_FULL_PERIOD)
SHORT_COIL();
else if (t >= EM4X50_T_TAG_HALF_PERIOD)
OPEN_COIL();
else
SHORT_COIL();
check = 0;
// wait until SSC_CLK goes LOW
while (AT91C_BASE_PIOA->PIO_PDSR & GPIO_SSC_CLK) {
WDT_HIT();
if (check == 1000) {
if (BUTTON_PRESS())
return false;
check = 0;
}
++check;
}
2020-12-28 05:22:44 +08:00
}
2020-11-20 06:18:04 +08:00
return true;
}
2021-01-04 05:50:27 +08:00
*/
static void em4x50_sim_handle_login_command(void) {
//uint8_t *em4x50_mem = BigBuf_get_EM_addr();
uint32_t password = 0;
//uint32_t tag[EM4X50_NO_WORDS] = {0x0};
// read password
password = em4x50_sim_read_word();
//for (int i = 0; i < EM4X50_NO_WORDS; i++)
// tag[i] = reflect32(bytes_to_num(em4x50_mem + (i * 4), 4));
// processing pause time (corresponds to a "1" bit)
em4x50_sim_send_bit(1);
em4x50_sim_send_ack();
/*
if (password == tag[0]) {
em4x50_sim_send_ack();
} else {
em4x50_sim_send_ack();
}
*/
// continue with standard read mode
em4x50_sim_handle_command(0);
Dbprintf("password = %08x", password);
BigBuf_free();
}
static void em4x50_sim_handle_reset_command(void) {
// processing pause time (corresponds to a "1" bit)
em4x50_sim_send_bit(1);
// send ACK
em4x50_sim_send_ack();
// wait for tinit
wait_timer(T0 * EM4X50_T_TAG_TINIT);
// continue with standard read mode
em4x50_sim_handle_command(0);
}
static void em4x50_sim_handle_write_command(void) {
uint8_t *em4x50_mem = BigBuf_get_EM_addr();
uint8_t address = 0;
uint32_t data = 0;
uint32_t tag[EM4X50_NO_WORDS] = {0x0};
// read address
address = em4x50_sim_read_byte_with_parity_check();
// read data
data = em4x50_sim_read_word();
for (int i = 0; i < EM4X50_NO_WORDS; i++) {
tag[i] = reflect32(bytes_to_num(em4x50_mem + (i * 4), 4));
}
// extract necessary control data
bool raw = (tag[EM4X50_CONTROL] >> CONFIG_BLOCK) & READ_AFTER_WRITE;
// extract protection data
int fwrp = tag[EM4X50_PROTECTION] & 0xFF; // first word read protected
int lwrp = (tag[EM4X50_PROTECTION] >> 8) & 0xFF; // last word read protected
// save data to tag
tag[address] = data;
// write access time
wait_timer(T0 * EM4X50_T_TAG_TWA);
if ((address >= fwrp) && (address <= lwrp)) {
em4x50_sim_send_nak();
} else if ((address == EM4X50_DEVICE_SERIAL)
&& (address == EM4X50_DEVICE_ID)
&& (address == 0)
) {
em4x50_sim_send_nak();
} else {
em4x50_sim_send_ack();
}
// EEPROM write time
//wait_timer(T0 * EM4X50_T_TAG_TWEE);
em4x50_sim_send_ack();
// if "read after write" (raw) bit is set, repeat written data once
if (raw) {
em4x50_sim_send_listen_window();
em4x50_sim_send_listen_window();
em4x50_sim_send_word(tag[address]);
}
BigBuf_free();
// continue with standard read mode
em4x50_sim_handle_command(0);
}
static void em4x50_sim_handle_selective_read_command(void) {
uint8_t *em4x50_mem = BigBuf_get_EM_addr();
uint32_t address = 0;
uint32_t tag[EM4X50_NO_WORDS] = {0x0};
// read password
address = em4x50_sim_read_word();
// extract control data
int fwr = address & 0xFF; // first word read
int lwr = (address >> 8) & 0xFF; // last word read
for (int i = 0; i < EM4X50_NO_WORDS; i++) {
tag[i] = reflect32(bytes_to_num(em4x50_mem + (i * 4), 4));
}
// extract protection data
int fwrp = tag[EM4X50_PROTECTION] & 0xFF; // first word read protected
int lwrp = (tag[EM4X50_PROTECTION] >> 8) & 0xFF; // last word read protected
// processing pause time (corresponds to a "1" bit)
em4x50_sim_send_bit(1);
em4x50_sim_send_ack();
while (BUTTON_PRESS() == false) {
WDT_HIT();
em4x50_sim_send_listen_window();
for (int i = fwr; i <= lwr; i++) {
em4x50_sim_send_listen_window();
if ((i >= fwrp) && (i <= lwrp)) {
em4x50_sim_send_word(0x00);
} else {
em4x50_sim_send_word(tag[i]);
}
}
}
BigBuf_free();
}
static void em4x50_sim_handle_standard_read_command(void) {
uint8_t *em4x50_mem = BigBuf_get_EM_addr();
uint32_t tag[EM4X50_NO_WORDS] = {0x0};
for (int i = 0; i < EM4X50_NO_WORDS; i++) {
tag[i] = reflect32(bytes_to_num(em4x50_mem + (i * 4), 4));
}
// extract control data
int fwr = tag[CONFIG_BLOCK] & 0xFF; // first word read
int lwr = (tag[CONFIG_BLOCK] >> 8) & 0xFF; // last word read
// extract protection data
int fwrp = tag[EM4X50_PROTECTION] & 0xFF; // first word read protected
int lwrp = (tag[EM4X50_PROTECTION] >> 8) & 0xFF; // last word read protected
while (BUTTON_PRESS() == false) {
WDT_HIT();
em4x50_sim_send_listen_window();
for (int i = fwr; i <= lwr; i++) {
em4x50_sim_send_listen_window();
if ((i >= fwrp) && (i <= lwrp)) {
em4x50_sim_send_word(0x00);
} else {
em4x50_sim_send_word(tag[i]);
}
}
}
BigBuf_free();
}
static void em4x50_sim_handle_command(uint8_t command) {
switch (command) {
case EM4X50_COMMAND_LOGIN:
em4x50_sim_handle_login_command();
break;
case EM4X50_COMMAND_RESET:
em4x50_sim_handle_reset_command();
break;
case EM4X50_COMMAND_WRITE:
em4x50_sim_handle_write_command();
break;
case EM4X50_COMMAND_WRITE_PASSWORD:
Dbprintf("Command = write_password");
break;
case EM4X50_COMMAND_SELECTIVE_READ:
em4x50_sim_handle_selective_read_command();
break;
default:
em4x50_sim_handle_standard_read_command();
break;
}
}
// reader requests receive mode (rm) by sending two zeros
static void check_rm_request(void) {
int cond = EM4X50_T_TAG_FULL_PERIOD - EM4X50_TAG_TOLERANCE;
// look for first zero
if (get_cycles() < cond) {
// look for second zero
if (get_cycles() < cond) {
// read mode request detected, get command from reader
uint8_t command = em4x50_sim_read_byte_with_parity_check();
em4x50_sim_handle_command(command);
}
}
}
static bool em4x50_sim_send_listen_window(void) {
SHORT_COIL();
wait_cycles(EM4X50_T_TAG_HALF_PERIOD);
OPEN_COIL();
wait_cycles(EM4X50_T_TAG_HALF_PERIOD);
SHORT_COIL();
wait_cycles(2 * EM4X50_T_TAG_FULL_PERIOD);
OPEN_COIL();
check_rm_request();
SHORT_COIL();
wait_cycles(EM4X50_T_TAG_FULL_PERIOD);
return true;
}
2020-11-20 06:18:04 +08:00
2020-12-09 19:18:01 +08:00
// simple login to EM4x50,
// used in operations that require authentication
2020-11-20 06:18:04 +08:00
static bool login(uint32_t password) {
if (request_receive_mode() == PM3_SUCCESS) {
2020-06-15 20:32:51 +08:00
// send login command
2020-09-05 19:25:57 +08:00
em4x50_reader_send_byte_with_parity(EM4X50_COMMAND_LOGIN);
2020-06-15 20:32:51 +08:00
// send password
2020-09-05 19:25:57 +08:00
em4x50_reader_send_word(password);
2020-12-12 21:46:40 +08:00
wait_timer(T0 * EM4X50_T_TAG_TPP);
2020-06-15 20:32:51 +08:00
// check if ACK is returned
2021-01-04 05:50:27 +08:00
if (check_ack(false)) {
return PM3_SUCCESS;
2021-01-04 05:50:27 +08:00
}
2020-08-13 18:25:04 +08:00
2020-06-15 20:32:51 +08:00
} else {
2020-08-13 18:25:04 +08:00
if (DBGLEVEL >= DBG_DEBUG)
Dbprintf("error in command request");
2020-06-15 20:32:51 +08:00
}
2020-08-13 18:25:04 +08:00
return PM3_EFAILED;
2020-06-15 20:32:51 +08:00
}
2020-12-09 19:18:01 +08:00
// searching for password in given range
2020-11-20 06:18:04 +08:00
static bool brute(uint32_t start, uint32_t stop, uint32_t *pwd) {
bool pwd_found = false;
int cnt = 0;
for (*pwd = start; *pwd <= stop; (*pwd)++) {
if (login(*pwd) == PM3_SUCCESS) {
2020-12-12 21:46:40 +08:00
2020-11-20 06:18:04 +08:00
pwd_found = true;
// to be safe login 5 more times
for (int i = 0; i < 5; i++) {
if (login(*pwd) != PM3_SUCCESS) {
pwd_found = false;
break;
}
}
2020-12-12 21:46:40 +08:00
if (pwd_found)
break;
2020-11-20 06:18:04 +08:00
}
2020-12-12 21:46:40 +08:00
2020-11-20 06:18:04 +08:00
// print password every 500 iterations
if ((++cnt % 500) == 0) {
// print header
if (cnt == 500) {
Dbprintf("|---------+------------+------------|");
Dbprintf("| no. | pwd (msb) | pwd (lsb) |");
Dbprintf("|---------+------------+------------|");
}
// print data
Dbprintf("|%8i | 0x%08x | 0x%08x |", cnt, reflect32(*pwd), *pwd);
}
2020-12-12 21:46:40 +08:00
2020-11-20 06:18:04 +08:00
if (BUTTON_PRESS())
break;
2020-12-12 21:46:40 +08:00
2020-11-20 06:18:04 +08:00
}
// print footer
if (cnt >= 500)
Dbprintf("|---------+------------+------------|");
return pwd_found;
}
2020-12-09 19:18:01 +08:00
// login into EM4x50
2020-11-20 06:18:04 +08:00
void em4x50_login(uint32_t *password) {
em4x50_setup_read();
2020-12-09 19:18:01 +08:00
uint8_t status = PM3_EFAILED;
2020-11-20 06:18:04 +08:00
if (get_signalproperties() && find_em4x50_tag())
status = login(*password);
lf_finalize();
2020-12-09 19:18:01 +08:00
reply_ng(CMD_LF_EM4X50_LOGIN, status, NULL, 0);
2020-11-20 06:18:04 +08:00
}
2020-12-12 21:46:40 +08:00
// envoke password search
2020-11-20 06:18:04 +08:00
void em4x50_brute(em4x50_data_t *etd) {
2020-12-09 19:18:01 +08:00
em4x50_setup_read();
2020-11-20 06:18:04 +08:00
bool bsuccess = false;
uint32_t pwd = 0x0;
if (get_signalproperties() && find_em4x50_tag())
bsuccess = brute(etd->password1, etd->password2, &pwd);
lf_finalize();
2020-12-09 19:18:01 +08:00
reply_ng(CMD_LF_EM4X50_BRUTE, bsuccess ? PM3_SUCCESS : PM3_EFAILED, (uint8_t *)(&pwd), sizeof(pwd));
2020-11-20 06:18:04 +08:00
}
2020-12-09 19:18:01 +08:00
// check passwords from dictionary content in flash memory
void em4x50_chk(uint8_t *filename) {
2020-11-20 06:18:04 +08:00
int status = PM3_EFAILED;
uint32_t pwd = 0x0;
#ifdef WITH_FLASH
2020-11-20 06:18:04 +08:00
BigBuf_free();
2020-11-20 06:18:04 +08:00
int changed = rdv40_spiffs_lazy_mount();
uint16_t pwd_count = 0;
uint32_t size = size_in_spiffs((char *)filename);
pwd_count = size / 4;
uint8_t *pwds = BigBuf_malloc(size);
2020-11-20 06:18:04 +08:00
rdv40_spiffs_read_as_filetype((char *)filename, pwds, size, RDV40_SPIFFS_SAFETY_SAFE);
if (changed)
rdv40_spiffs_lazy_unmount();
2020-11-20 06:18:04 +08:00
em4x50_setup_read();
// set gHigh and gLow
if (get_signalproperties() && find_em4x50_tag()) {
// try to login with current password
for (int i = 0; i < pwd_count; i++) {
// manual interruption
if (BUTTON_PRESS()) {
status = PM3_EOPABORTED;
2020-11-20 06:18:04 +08:00
break;
}
// get next password
pwd = 0x0;
for (int j = 0; j < 4; j++)
pwd |= (*(pwds + 4 * i + j)) << ((3 - j) * 8);
2020-12-12 21:46:40 +08:00
2020-11-20 06:18:04 +08:00
if ((status = login(pwd)) == PM3_SUCCESS)
break;
}
}
BigBuf_free();
#endif
2020-11-20 06:18:04 +08:00
lf_finalize();
2020-12-09 19:18:01 +08:00
reply_ng(CMD_LF_EM4X50_CHK, status, (uint8_t *)&pwd, sizeof(pwd));
2020-11-20 06:18:04 +08:00
}
2020-12-09 19:18:01 +08:00
// resets EM4x50 tag (used by write function)
static int reset(void) {
if (request_receive_mode() == PM3_SUCCESS) {
// send reset command
2020-09-05 19:25:57 +08:00
em4x50_reader_send_byte_with_parity(EM4X50_COMMAND_RESET);
2020-09-24 05:45:43 +08:00
if (check_ack(false))
return PM3_SUCCESS;
2020-11-20 06:18:04 +08:00
} else {
if (DBGLEVEL >= DBG_DEBUG)
Dbprintf("error in command request");
}
return PM3_EFAILED;
}
2020-12-09 19:18:01 +08:00
// reads data that tag transmits when exposed to reader field
// (standard read mode); number of read words is saved in <now>
static int standard_read(int *now, uint32_t *words) {
2020-08-13 18:25:04 +08:00
int fwr = *now, res = PM3_EFAILED;
2020-06-15 20:32:51 +08:00
2020-10-24 07:18:06 +08:00
// start with the identification of two successive listening windows
if ((res = find_double_listen_window(false)) == PM3_SUCCESS) {
2020-06-15 20:32:51 +08:00
// read and save words until following double listen window is detected
while ((res = get_word_from_bitstream(&words[*now])) == EM4X50_TAG_WORD)
(*now)++;
2020-06-15 20:32:51 +08:00
// number of detected words
*now -= fwr;
2020-08-13 18:25:04 +08:00
2020-06-15 20:32:51 +08:00
} else {
2020-08-13 18:25:04 +08:00
if (DBGLEVEL >= DBG_DEBUG)
Dbprintf("didn't find a listen window");
2020-06-15 20:32:51 +08:00
}
return res;
2020-06-15 20:32:51 +08:00
}
2020-12-09 19:18:01 +08:00
// reads from "first word read" (fwr) to "last word read" (lwr)
// result is verified by "standard read mode"
static int selective_read(uint32_t addresses, uint32_t *words) {
2020-08-13 18:25:04 +08:00
int status = PM3_EFAILED;
2020-10-22 06:42:18 +08:00
uint8_t fwr = addresses & 0xFF; // first word read (first byte)
uint8_t lwr = (addresses >> 8) & 0xFF; // last word read (second byte)
int now = fwr; // number of words
2020-06-15 20:32:51 +08:00
if (request_receive_mode() == PM3_SUCCESS) {
2020-06-15 20:32:51 +08:00
// send selective read command
2020-09-05 19:25:57 +08:00
em4x50_reader_send_byte_with_parity(EM4X50_COMMAND_SELECTIVE_READ);
2020-06-15 20:32:51 +08:00
// send address data
2020-09-05 19:25:57 +08:00
em4x50_reader_send_word(addresses);
2020-06-15 20:32:51 +08:00
2020-09-24 05:45:43 +08:00
// look for ACK sequence
2020-06-15 20:32:51 +08:00
if (check_ack(false))
2020-09-24 05:45:43 +08:00
// save and verify via standard read mode (compare number of words)
if ((status = standard_read(&now, words)) == PM3_SUCCESS)
2020-06-15 20:32:51 +08:00
if (now == (lwr - fwr + 1))
return status;
2020-08-13 18:25:04 +08:00
2020-06-15 20:32:51 +08:00
} else {
2020-08-13 18:25:04 +08:00
if (DBGLEVEL >= DBG_DEBUG)
Dbprintf("error in command request");
2020-06-15 20:32:51 +08:00
}
return status;
2020-06-15 20:32:51 +08:00
}
2020-12-09 19:18:01 +08:00
// reads by using "selective read mode" -> bidirectional communication
void em4x50_read(em4x50_data_t *etd) {
2020-11-20 06:18:04 +08:00
bool blogin = true;
int status = PM3_EFAILED;
2020-10-28 01:18:02 +08:00
uint32_t words[EM4X50_NO_WORDS] = {0x0};
2020-06-26 20:19:41 +08:00
em4x50_setup_read();
2020-08-13 18:25:04 +08:00
2020-06-26 20:19:41 +08:00
// set gHigh and gLow
2020-09-24 05:45:43 +08:00
if (get_signalproperties() && find_em4x50_tag()) {
// try to login with given password
if (etd->pwd_given)
blogin = (login(etd->password1) == PM3_SUCCESS);
2020-08-13 18:25:04 +08:00
// only one word has to be read -> first word read = last word read
2020-11-20 06:18:04 +08:00
if (blogin)
status = selective_read(etd->addresses, words);
2020-06-26 20:19:41 +08:00
}
2020-08-13 18:25:04 +08:00
2020-09-05 19:25:57 +08:00
LOW(GPIO_SSC_DOUT);
2020-06-26 20:19:41 +08:00
lf_finalize();
2020-12-09 19:18:01 +08:00
2020-12-17 03:44:04 +08:00
reply_ng(CMD_LF_EM4X50_READ, status, (uint8_t *)words, EM4X50_TAG_MAX_NO_BYTES);
2020-06-26 20:19:41 +08:00
}
2020-12-09 19:18:01 +08:00
// collects as much information as possible via selective read mode
2020-11-20 06:18:04 +08:00
void em4x50_info(em4x50_data_t *etd) {
2020-10-04 04:59:21 +08:00
2020-11-20 06:18:04 +08:00
bool blogin = true;
int status = PM3_EFAILED;
uint32_t addresses = 0x00002100; // read from fwr = 0 to lwr = 33 (0x21)
uint32_t words[EM4X50_NO_WORDS] = {0x0};
2020-10-04 04:59:21 +08:00
2021-01-04 05:50:27 +08:00
gcount = 0;
2020-09-28 05:39:04 +08:00
em4x50_setup_read();
2020-11-20 06:18:04 +08:00
if (get_signalproperties() && find_em4x50_tag()) {
// login with given password
if (etd->pwd_given)
blogin = (login(etd->password1) == PM3_SUCCESS);
if (blogin)
status = selective_read(addresses, words);
}
2020-09-28 05:39:04 +08:00
2020-10-04 04:59:21 +08:00
lf_finalize();
2021-01-04 05:50:27 +08:00
2020-12-17 03:44:04 +08:00
reply_ng(CMD_LF_EM4X50_INFO, status, (uint8_t *)words, EM4X50_TAG_MAX_NO_BYTES);
2020-10-04 04:59:21 +08:00
}
2020-12-09 19:18:01 +08:00
// reads data that tag transmits "voluntarily" -> standard read mode
void em4x50_reader(void) {
int now = 0;
uint32_t words[EM4X50_NO_WORDS] = {0x0};
em4x50_setup_read();
if (get_signalproperties() && find_em4x50_tag())
standard_read(&now, words);
LOW(GPIO_SSC_DOUT);
lf_finalize();
2021-01-04 05:50:27 +08:00
reply_ng(CMD_LF_EM4X50_READER, now, (uint8_t *)words, 4 * now);
}
2020-12-09 19:18:01 +08:00
// writes <word> to specified <addresses>
2020-11-20 06:18:04 +08:00
static int write(uint32_t word, uint32_t addresses) {
2020-12-12 21:46:40 +08:00
if (request_receive_mode() == PM3_SUCCESS) {
2020-10-22 06:42:18 +08:00
2020-11-20 06:18:04 +08:00
// send write command
em4x50_reader_send_byte_with_parity(EM4X50_COMMAND_WRITE);
// send address data
em4x50_reader_send_byte_with_parity(addresses & 0xFF);
// send data
em4x50_reader_send_word(word);
if (tearoff_hook() == PM3_ETEAROFF) { // tearoff occurred
reply_ng(CMD_LF_EM4X50_WRITE, PM3_ETEAROFF, NULL, 0);
return PM3_ETEAROFF;
} else {
2020-12-12 21:46:40 +08:00
2020-11-20 06:18:04 +08:00
// wait for T0 * EM4X50_T_TAG_TWA (write access time)
wait_timer(T0 * EM4X50_T_TAG_TWA);
// look for ACK sequence
if (check_ack(false)) {
// now EM4x50 needs T0 * EM4X50_T_TAG_TWEE (EEPROM write time)
// for saving data and should return with ACK
if (check_ack(false))
return PM3_SUCCESS;
}
}
} else {
if (DBGLEVEL >= DBG_DEBUG)
Dbprintf("error in command request");
}
return PM3_EFAILED;
}
2020-12-09 19:18:01 +08:00
// changes password from <password> to <new_password>
2020-11-20 06:18:04 +08:00
static int write_password(uint32_t password, uint32_t new_password) {
if (request_receive_mode() == PM3_SUCCESS) {
2020-11-20 06:18:04 +08:00
// send write password command
em4x50_reader_send_byte_with_parity(EM4X50_COMMAND_WRITE_PASSWORD);
// send address data
em4x50_reader_send_word(password);
if (tearoff_hook() == PM3_ETEAROFF) { // tearoff occurred
reply_ng(CMD_LF_EM4X50_WRITE, PM3_ETEAROFF, NULL, 0);
return PM3_ETEAROFF;
} else {
// wait for T0 * EM4x50_T_TAG_TPP (processing pause time)
wait_timer(T0 * EM4X50_T_TAG_TPP);
// look for ACK sequence and send rm request
// during following listen window
if (check_ack(true)) {
// send new password
em4x50_reader_send_word(new_password);
// wait for T0 * EM4X50_T_TAG_TWA (write access time)
wait_timer(T0 * EM4X50_T_TAG_TWA);
if (check_ack(false))
if (check_ack(false))
return PM3_SUCCESS;
}
}
} else {
if (DBGLEVEL >= DBG_DEBUG)
Dbprintf("error in command request");
}
2020-11-21 05:03:19 +08:00
return PM3_EFAILED;
2020-11-20 06:18:04 +08:00
}
2020-12-09 19:18:01 +08:00
// write operation process for EM4x50 tag,
// single word is written to given address, verified by selective read operation
// wrong password -> return with PM3_EFAILED
2020-11-20 06:18:04 +08:00
void em4x50_write(em4x50_data_t *etd) {
int status = PM3_EFAILED;
uint32_t words[EM4X50_NO_WORDS] = {0x0};
2020-10-22 06:42:18 +08:00
em4x50_setup_read();
2020-11-20 06:18:04 +08:00
if (get_signalproperties() && find_em4x50_tag()) {
2020-10-22 06:42:18 +08:00
2020-11-20 06:18:04 +08:00
// if password is given try to login first
status = PM3_SUCCESS;
if (etd->pwd_given)
status = login(etd->password1);
2020-10-22 06:42:18 +08:00
2020-11-20 06:18:04 +08:00
if (status == PM3_SUCCESS) {
// write word to given address
status = write(etd->word, etd->addresses);
if (status == PM3_ETEAROFF) {
lf_finalize();
return;
}
if (status == PM3_SUCCESS) {
// to verify result reset EM4x50
status = reset();
if (status == PM3_SUCCESS) {
// if password is given renew login after reset
if (etd->pwd_given)
status = login(etd->password1);
2020-12-12 21:46:40 +08:00
2020-11-20 06:18:04 +08:00
if (status == PM3_SUCCESS) {
// call a selective read
status = selective_read(etd->addresses, words);
if (status == PM3_SUCCESS) {
// compare result with given word
if (words[etd->addresses & 0xFF] != reflect32(etd->word))
status = PM3_EFAILED;
}
}
}
}
}
}
lf_finalize();
2020-12-17 03:44:04 +08:00
reply_ng(CMD_LF_EM4X50_WRITE, status, (uint8_t *)words, EM4X50_TAG_MAX_NO_BYTES);
2020-10-22 06:42:18 +08:00
}
2020-12-09 19:18:01 +08:00
// simple change of password
2020-11-20 06:18:04 +08:00
void em4x50_writepwd(em4x50_data_t *etd) {
int status = PM3_EFAILED;
2020-10-22 06:42:18 +08:00
em4x50_setup_read();
2020-11-20 06:18:04 +08:00
if (get_signalproperties() && find_em4x50_tag()) {
2020-10-22 06:42:18 +08:00
2020-11-20 06:18:04 +08:00
// login and change password
if (login(etd->password1) == PM3_SUCCESS) {
status = write_password(etd->password1, etd->password2);
if (status == PM3_ETEAROFF) {
lf_finalize();
return;
}
}
}
lf_finalize();
2020-12-09 19:18:01 +08:00
reply_ng(CMD_LF_EM4X50_WRITEPWD, status, NULL, 0);
2020-10-22 06:42:18 +08:00
}
2020-10-25 23:40:39 +08:00
2020-12-09 19:18:01 +08:00
// simulate uploaded data in emulator memory
// (currently simulation allows only a one-way communication)
2021-01-04 05:50:27 +08:00
void em4x50_sim() {
int status = PM3_SUCCESS;
uint8_t *em4x50_mem = BigBuf_get_EM_addr();
2020-10-27 07:53:50 +08:00
uint32_t words[EM4X50_NO_WORDS] = {0x0};
2020-12-12 21:46:40 +08:00
2020-10-31 21:53:52 +08:00
for (int i = 0; i < EM4X50_NO_WORDS; i++)
words[i] = reflect32(bytes_to_num(em4x50_mem + (i * 4), 4));
2020-12-12 21:46:40 +08:00
// only if valid em4x50 data (e.g. uid == serial)
if (words[EM4X50_DEVICE_SERIAL] != words[EM4X50_DEVICE_ID]) {
2020-10-27 07:53:50 +08:00
2021-01-04 05:50:27 +08:00
em4x50_setup_sim();
em4x50_sim_handle_command(0);
/*
// extract control data
int fwr = words[CONFIG_BLOCK] & 0xFF; // first word read
int lwr = (words[CONFIG_BLOCK] >> 8) & 0xFF; // last word read
// extract protection data
int fwrp = words[EM4X50_PROTECTION] & 0xFF; // first word read protected
int lwrp = (words[EM4X50_PROTECTION] >> 8) & 0xFF; // last word read protected
2020-10-27 07:53:50 +08:00
2020-12-09 19:18:01 +08:00
// iceman, will need a usb cmd check to break as well
while (BUTTON_PRESS() == false) {
2020-12-12 21:46:40 +08:00
2021-01-04 05:50:27 +08:00
rm = 0;
WDT_HIT();
2020-10-27 07:53:50 +08:00
em4x50_sim_send_listen_window();
for (int i = fwr; i <= lwr; i++) {
2020-10-27 07:53:50 +08:00
em4x50_sim_send_listen_window();
if ((i >= fwrp) && (i <= lwrp))
em4x50_sim_send_word(0x00);
else
em4x50_sim_send_word(words[i]);
}
2020-10-27 07:53:50 +08:00
}
2021-01-04 05:50:27 +08:00
*/
} else {
status = PM3_ENODATA;
}
2020-12-12 21:46:40 +08:00
BigBuf_free();
lf_finalize();
2021-01-04 05:50:27 +08:00
2020-12-09 19:18:01 +08:00
reply_ng(CMD_LF_EM4X50_SIM, status, NULL, 0);
}
2020-12-28 05:22:44 +08:00
void em4x50_test(em4x50_test_t *ett) {
int status = PM3_EFAILED;
2020-12-30 00:40:18 +08:00
// set field on or off
if (ett->field != -1) {
em4x50_setup_read();
if (ett->field == 1) {
LED_A_ON();
} else {
HIGH(GPIO_SSC_DOUT);
LED_A_OFF();
}
status = ett->field;
}
2020-12-28 05:22:44 +08:00
2020-12-30 00:40:18 +08:00
// check field status
if (ett->check_field) {
em4x50_setup_sim();
bool field_on = false;
while (BUTTON_PRESS() == false) {
2020-12-28 05:22:44 +08:00
2020-12-30 00:40:18 +08:00
if (!(AT91C_BASE_PIOA->PIO_PDSR & GPIO_SSC_CLK)) {
if (field_on == false) {
Dbprintf("field on");
field_on = true;
}
} else if (AT91C_BASE_PIOA->PIO_PDSR & GPIO_SSC_CLK){
if (field_on == true) {
Dbprintf("field off");
field_on = false;
}
}
}
2020-12-28 05:22:44 +08:00
status = 1;
}
2020-12-30 00:40:18 +08:00
// timing values
if (ett->cycles != 0) {
uint32_t tval = 0;
uint32_t tvalhigh[ett->cycles];
uint32_t tvallow[ett->cycles];
em4x50_setup_sim();
while (AT91C_BASE_TC0->TC_CV > 0);
for (int t = 0; t < ett->cycles; t++) {
// field on -> high value
AT91C_BASE_TC0->TC_CCR = AT91C_TC_SWTRG;
tval = AT91C_BASE_TC0->TC_CV;
while (!(AT91C_BASE_PIOA->PIO_PDSR & GPIO_SSC_CLK));
tvalhigh[t] = AT91C_BASE_TC0->TC_CV - tval;
2020-12-28 05:22:44 +08:00
2020-12-30 00:40:18 +08:00
// filed off -> zero value
AT91C_BASE_TC0->TC_CCR = AT91C_TC_SWTRG;
tval = AT91C_BASE_TC0->TC_CV;
while (AT91C_BASE_PIOA->PIO_PDSR & GPIO_SSC_CLK);
tvallow[t] = AT91C_BASE_TC0->TC_CV - tval;
2020-12-28 05:22:44 +08:00
}
2020-12-30 00:40:18 +08:00
for (int t = 0; t < ett->cycles; t++) {
Dbprintf("%03i %li %li", t, tvallow[t], tvalhigh[t]);
}
2020-12-28 05:22:44 +08:00
}
2021-01-04 05:50:27 +08:00
em4x50_setup_sim();
for (;;) {
em4x50_sim_send_listen_window();
em4x50_sim_send_listen_window();
em4x50_sim_send_bit(0);
em4x50_sim_send_bit(0);
em4x50_sim_send_bit(0);
em4x50_sim_send_ack();
em4x50_sim_send_ack();
em4x50_sim_send_bit(0);
em4x50_sim_send_bit(0);
em4x50_sim_send_bit(0);
em4x50_sim_send_nak();
em4x50_sim_send_nak();
em4x50_sim_send_bit(0);
em4x50_sim_send_bit(0);
em4x50_sim_send_bit(0);
}
2020-12-28 05:22:44 +08:00
reply_ng(CMD_LF_EM4X50_TEST, status, NULL, 0);
}