mirror of
https://github.com/RfidResearchGroup/proxmark3.git
synced 2025-08-20 13:23:51 -07:00
FIX: 'hf legic sim' - needed even more timeout.
see https://github.com/RfidResearchGroup/proxmark3/issues/83
This commit is contained in:
parent
803aab7431
commit
730a7e8044
1 changed files with 30 additions and 31 deletions
|
@ -46,7 +46,7 @@ static uint32_t last_frame_end; /* ts of last bit of previews rx or tx frame */
|
||||||
#define RWD_TIME_PAUSE 4 /* 18.9us */
|
#define RWD_TIME_PAUSE 4 /* 18.9us */
|
||||||
#define RWD_TIME_1 21 /* RWD_TIME_PAUSE 18.9us off + 80.2us on = 99.1us */
|
#define RWD_TIME_1 21 /* RWD_TIME_PAUSE 18.9us off + 80.2us on = 99.1us */
|
||||||
#define RWD_TIME_0 13 /* RWD_TIME_PAUSE 18.9us off + 42.4us on = 61.3us */
|
#define RWD_TIME_0 13 /* RWD_TIME_PAUSE 18.9us off + 42.4us on = 61.3us */
|
||||||
#define RWD_CMD_TIMEOUT 80 /* 80 * 99.1us (arbitrary value) */
|
#define RWD_CMD_TIMEOUT 120 /* 120 * 99.1us (arbitrary value) */
|
||||||
#define RWD_MIN_FRAME_LEN 6 /* Shortest frame is 6 bits */
|
#define RWD_MIN_FRAME_LEN 6 /* Shortest frame is 6 bits */
|
||||||
#define RWD_MAX_FRAME_LEN 23 /* Longest frame is 23 bits */
|
#define RWD_MAX_FRAME_LEN 23 /* Longest frame is 23 bits */
|
||||||
|
|
||||||
|
@ -59,8 +59,8 @@ static uint32_t last_frame_end; /* ts of last bit of previews rx or tx frame */
|
||||||
|
|
||||||
// Returns true if a pulse/pause is received within timeout
|
// Returns true if a pulse/pause is received within timeout
|
||||||
static inline bool wait_for(bool value, const uint32_t timeout) {
|
static inline bool wait_for(bool value, const uint32_t timeout) {
|
||||||
while((bool)(AT91C_BASE_PIOA->PIO_PDSR & GPIO_SSC_DIN) != value) {
|
while ((bool)(AT91C_BASE_PIOA->PIO_PDSR & GPIO_SSC_DIN) != value) {
|
||||||
if(GetCountSspClk() > timeout) {
|
if (GetCountSspClk() > timeout) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -81,12 +81,12 @@ static inline int8_t rx_bit() {
|
||||||
uint32_t bit_start = last_frame_end;
|
uint32_t bit_start = last_frame_end;
|
||||||
|
|
||||||
// wait for pause to end
|
// wait for pause to end
|
||||||
if(!wait_for(RWD_PULSE, bit_start + RWD_TIME_1*3/2)) {
|
if (!wait_for(RWD_PULSE, bit_start + RWD_TIME_1*3/2)) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// wait for next pause
|
// wait for next pause
|
||||||
if(!wait_for(RWD_PAUSE, bit_start + RWD_TIME_1*3/2)) {
|
if (!wait_for(RWD_PAUSE, bit_start + RWD_TIME_1*3/2)) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -94,7 +94,7 @@ static inline int8_t rx_bit() {
|
||||||
last_frame_end = GetCountSspClk();
|
last_frame_end = GetCountSspClk();
|
||||||
|
|
||||||
// check for code violation (bit to short)
|
// check for code violation (bit to short)
|
||||||
if(last_frame_end - bit_start < RWD_TIME_PAUSE) {
|
if (last_frame_end - bit_start < RWD_TIME_PAUSE) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -122,7 +122,7 @@ static inline int8_t rx_bit() {
|
||||||
static inline void tx_bit(bool bit) {
|
static inline void tx_bit(bool bit) {
|
||||||
LED_C_ON();
|
LED_C_ON();
|
||||||
|
|
||||||
if(bit) {
|
if (bit) {
|
||||||
// modulate subcarrier
|
// modulate subcarrier
|
||||||
HIGH(GPIO_SSC_DOUT);
|
HIGH(GPIO_SSC_DOUT);
|
||||||
} else {
|
} else {
|
||||||
|
@ -132,7 +132,7 @@ static inline void tx_bit(bool bit) {
|
||||||
|
|
||||||
// wait for tx timeslot to end
|
// wait for tx timeslot to end
|
||||||
last_frame_end += TAG_BIT_PERIOD;
|
last_frame_end += TAG_BIT_PERIOD;
|
||||||
while(GetCountSspClk() < last_frame_end) { };
|
while (GetCountSspClk() < last_frame_end) { };
|
||||||
LED_C_OFF();
|
LED_C_OFF();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -150,13 +150,13 @@ static void tx_frame(uint32_t frame, uint8_t len) {
|
||||||
// wait for next tx timeslot
|
// wait for next tx timeslot
|
||||||
last_frame_end += TAG_FRAME_WAIT;
|
last_frame_end += TAG_FRAME_WAIT;
|
||||||
legic_prng_forward(TAG_FRAME_WAIT/TAG_BIT_PERIOD - 1);
|
legic_prng_forward(TAG_FRAME_WAIT/TAG_BIT_PERIOD - 1);
|
||||||
while(GetCountSspClk() < last_frame_end) { };
|
while (GetCountSspClk() < last_frame_end) { };
|
||||||
|
|
||||||
// backup ts for trace log
|
// backup ts for trace log
|
||||||
uint32_t last_frame_start = last_frame_end;
|
uint32_t last_frame_start = last_frame_end;
|
||||||
|
|
||||||
// transmit frame, MSB first
|
// transmit frame, MSB first
|
||||||
for(uint8_t i = 0; i < len; ++i) {
|
for (uint8_t i = 0; i < len; ++i) {
|
||||||
bool bit = (frame >> i) & 0x01;
|
bool bit = (frame >> i) & 0x01;
|
||||||
tx_bit(bit ^ legic_prng_get_bit());
|
tx_bit(bit ^ legic_prng_get_bit());
|
||||||
legic_prng_forward(1);
|
legic_prng_forward(1);
|
||||||
|
@ -174,7 +174,7 @@ static void tx_ack() {
|
||||||
// wait for ack timeslot
|
// wait for ack timeslot
|
||||||
last_frame_end += TAG_ACK_WAIT;
|
last_frame_end += TAG_ACK_WAIT;
|
||||||
legic_prng_forward(TAG_ACK_WAIT/TAG_BIT_PERIOD - 1);
|
legic_prng_forward(TAG_ACK_WAIT/TAG_BIT_PERIOD - 1);
|
||||||
while(GetCountSspClk() < last_frame_end) { };
|
while (GetCountSspClk() < last_frame_end) { };
|
||||||
|
|
||||||
// backup ts for trace log
|
// backup ts for trace log
|
||||||
uint32_t last_frame_start = last_frame_end;
|
uint32_t last_frame_start = last_frame_end;
|
||||||
|
@ -206,19 +206,19 @@ static int32_t rx_frame(uint8_t *len) {
|
||||||
last_frame_end -= 2;
|
last_frame_end -= 2;
|
||||||
|
|
||||||
// wait for first pause (start of frame)
|
// wait for first pause (start of frame)
|
||||||
for(uint8_t i = 0; true; ++i) {
|
for (uint8_t i = 0; true; ++i) {
|
||||||
// increment prng every TAG_BIT_PERIOD
|
// increment prng every TAG_BIT_PERIOD
|
||||||
last_frame_end += TAG_BIT_PERIOD;
|
last_frame_end += TAG_BIT_PERIOD;
|
||||||
legic_prng_forward(1);
|
legic_prng_forward(1);
|
||||||
|
|
||||||
// if start of frame was received exit delay loop
|
// if start of frame was received exit delay loop
|
||||||
if(wait_for(RWD_PAUSE, last_frame_end)) {
|
if (wait_for(RWD_PAUSE, last_frame_end)) {
|
||||||
last_frame_end = GetCountSspClk();
|
last_frame_end = GetCountSspClk();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// check for code violation
|
// check for code violation
|
||||||
if(i > RWD_CMD_TIMEOUT) {
|
if (i > RWD_CMD_TIMEOUT) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -227,19 +227,19 @@ static int32_t rx_frame(uint8_t *len) {
|
||||||
uint32_t last_frame_start = last_frame_end;
|
uint32_t last_frame_start = last_frame_end;
|
||||||
|
|
||||||
// receive frame
|
// receive frame
|
||||||
for(*len = 0; true; ++(*len)) {
|
for (*len = 0; true; ++(*len)) {
|
||||||
// receive next bit
|
// receive next bit
|
||||||
LED_B_ON();
|
LED_B_ON();
|
||||||
int8_t bit = rx_bit();
|
int8_t bit = rx_bit();
|
||||||
LED_B_OFF();
|
LED_B_OFF();
|
||||||
|
|
||||||
// check for code violation and to short / long frame
|
// check for code violation and to short / long frame
|
||||||
if((bit < 0) && ((*len < RWD_MIN_FRAME_LEN) || (*len > RWD_MAX_FRAME_LEN))) {
|
if ((bit < 0) && ((*len < RWD_MIN_FRAME_LEN) || (*len > RWD_MAX_FRAME_LEN))) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// check for code violation caused by end of frame
|
// check for code violation caused by end of frame
|
||||||
if(bit < 0) {
|
if (bit < 0) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -256,7 +256,6 @@ static int32_t rx_frame(uint8_t *len) {
|
||||||
// log
|
// log
|
||||||
uint8_t cmdbytes[] = {*len, BYTEx(frame, 0), BYTEx(frame, 1), BYTEx(frame, 2)};
|
uint8_t cmdbytes[] = {*len, BYTEx(frame, 0), BYTEx(frame, 1), BYTEx(frame, 2)};
|
||||||
LogTrace(cmdbytes, sizeof(cmdbytes), last_frame_start, last_frame_end, NULL, true);
|
LogTrace(cmdbytes, sizeof(cmdbytes), last_frame_start, last_frame_end, NULL, true);
|
||||||
|
|
||||||
return frame;
|
return frame;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -267,7 +266,7 @@ static int32_t rx_frame(uint8_t *len) {
|
||||||
static int32_t init_card(uint8_t cardtype, legic_card_select_t *p_card) {
|
static int32_t init_card(uint8_t cardtype, legic_card_select_t *p_card) {
|
||||||
p_card->tagtype = cardtype;
|
p_card->tagtype = cardtype;
|
||||||
|
|
||||||
switch(p_card->tagtype) {
|
switch (p_card->tagtype) {
|
||||||
case 0:
|
case 0:
|
||||||
p_card->cmdsize = 6;
|
p_card->cmdsize = 6;
|
||||||
p_card->addrsize = 5;
|
p_card->addrsize = 5;
|
||||||
|
@ -338,7 +337,7 @@ static int32_t setup_phase(legic_card_select_t *p_card) {
|
||||||
|
|
||||||
// wait for iv
|
// wait for iv
|
||||||
int32_t iv = rx_frame(&len);
|
int32_t iv = rx_frame(&len);
|
||||||
if((len != 7) || (iv < 0)) {
|
if ((len != 7) || (iv < 0)) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -346,7 +345,7 @@ static int32_t setup_phase(legic_card_select_t *p_card) {
|
||||||
legic_prng_init(iv);
|
legic_prng_init(iv);
|
||||||
|
|
||||||
// reply with card type
|
// reply with card type
|
||||||
switch(p_card->tagtype) {
|
switch (p_card->tagtype) {
|
||||||
case 0:
|
case 0:
|
||||||
tx_frame(0x0D, 6);
|
tx_frame(0x0D, 6);
|
||||||
break;
|
break;
|
||||||
|
@ -360,12 +359,12 @@ static int32_t setup_phase(legic_card_select_t *p_card) {
|
||||||
|
|
||||||
// wait for ack
|
// wait for ack
|
||||||
int32_t ack = rx_frame(&len);
|
int32_t ack = rx_frame(&len);
|
||||||
if((len != 6) || (ack < 0)) {
|
if ((len != 6) || (ack < 0)) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// validate data
|
// validate data
|
||||||
switch(p_card->tagtype) {
|
switch (p_card->tagtype) {
|
||||||
case 0:
|
case 0:
|
||||||
if(ack != 0x19) return -1;
|
if(ack != 0x19) return -1;
|
||||||
break;
|
break;
|
||||||
|
@ -399,12 +398,12 @@ static int32_t connected_phase(legic_card_select_t *p_card) {
|
||||||
|
|
||||||
// wait for command
|
// wait for command
|
||||||
int32_t cmd = rx_frame(&len);
|
int32_t cmd = rx_frame(&len);
|
||||||
if(cmd < 0) {
|
if (cmd < 0) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// check if command is LEGIC_READ
|
// check if command is LEGIC_READ
|
||||||
if(len == p_card->cmdsize) {
|
if (len == p_card->cmdsize) {
|
||||||
// prepare data
|
// prepare data
|
||||||
uint8_t byte = legic_mem[cmd >> 1];
|
uint8_t byte = legic_mem[cmd >> 1];
|
||||||
uint8_t crc = calc_crc4(cmd, p_card->cmdsize, byte);
|
uint8_t crc = calc_crc4(cmd, p_card->cmdsize, byte);
|
||||||
|
@ -416,7 +415,7 @@ static int32_t connected_phase(legic_card_select_t *p_card) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// check if command is LEGIC_WRITE
|
// check if command is LEGIC_WRITE
|
||||||
if(len == p_card->cmdsize + 8 + 4) {
|
if (len == p_card->cmdsize + 8 + 4) {
|
||||||
// decode data
|
// decode data
|
||||||
uint16_t mask = (1 << p_card->addrsize) - 1;
|
uint16_t mask = (1 << p_card->addrsize) - 1;
|
||||||
uint16_t addr = (cmd >> 1) & mask;
|
uint16_t addr = (cmd >> 1) & mask;
|
||||||
|
@ -425,7 +424,7 @@ static int32_t connected_phase(legic_card_select_t *p_card) {
|
||||||
|
|
||||||
// check received against calculated crc
|
// check received against calculated crc
|
||||||
uint8_t calc_crc = calc_crc4(addr << 1, p_card->cmdsize, byte);
|
uint8_t calc_crc = calc_crc4(addr << 1, p_card->cmdsize, byte);
|
||||||
if(calc_crc != crc) {
|
if (calc_crc != crc) {
|
||||||
Dbprintf("!!! crc mismatch: %x != %x !!!", calc_crc, crc);
|
Dbprintf("!!! crc mismatch: %x != %x !!!", calc_crc, crc);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
@ -453,7 +452,7 @@ void LegicRfSimulate(uint8_t cardtype) {
|
||||||
init_tag();
|
init_tag();
|
||||||
|
|
||||||
// verify command line input
|
// verify command line input
|
||||||
if(init_card(cardtype, &card) != 0) {
|
if (init_card(cardtype, &card) != 0) {
|
||||||
DbpString("Unknown tagtype.");
|
DbpString("Unknown tagtype.");
|
||||||
goto OUT;
|
goto OUT;
|
||||||
}
|
}
|
||||||
|
@ -464,17 +463,17 @@ void LegicRfSimulate(uint8_t cardtype) {
|
||||||
WDT_HIT();
|
WDT_HIT();
|
||||||
|
|
||||||
// wait for carrier, restart after timeout
|
// wait for carrier, restart after timeout
|
||||||
if(!wait_for(RWD_PULSE, GetCountSspClk() + TAG_BIT_PERIOD)) {
|
if (!wait_for(RWD_PULSE, GetCountSspClk() + TAG_BIT_PERIOD)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// wait for connection, restart on error
|
// wait for connection, restart on error
|
||||||
if(setup_phase(&card)) {
|
if (setup_phase(&card)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// conection is established, process commands until one fails
|
// conection is established, process commands until one fails
|
||||||
while(!connected_phase(&card)) {
|
while (!connected_phase(&card)) {
|
||||||
WDT_HIT();
|
WDT_HIT();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue