changed legic sim to have a different loop and exit message. Added some colors in output and the return codes on deviceside for legisim now uses the same PM3_E* styled

This commit is contained in:
iceman1001 2023-07-17 19:31:37 +02:00
commit c70e5beeac
3 changed files with 36 additions and 41 deletions

View file

@ -3,6 +3,7 @@ All notable changes to this project will be documented in this file.
This project uses the changelog in accordance with [keepchangelog](http://keepachangelog.com/). Please use this to write notable changes, which is not the same as git commit log... This project uses the changelog in accordance with [keepchangelog](http://keepachangelog.com/). Please use this to write notable changes, which is not the same as git commit log...
## [unreleased][unreleased] ## [unreleased][unreleased]
- Changed `hf legic sim` - loop and return codes on deviceside updated to DEFINES (@iceman1001)
- Changed `hf legic einfo` - now accepts the three different cardsizes as params (@iceman1001) - Changed `hf legic einfo` - now accepts the three different cardsizes as params (@iceman1001)
- Fix `lf cotag reader -1` - now doesn't fail (@iceman1001) - Fix `lf cotag reader -1` - now doesn't fail (@iceman1001)
- Added support for LZ4 compressed hadnested tables (@doegox) - Added support for LZ4 compressed hadnested tables (@doegox)

View file

@ -96,13 +96,13 @@ static int8_t rx_bit(void) {
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) == false) {
return -1; return PM3_ERFTRANS;
} }
// 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) == false) {
return -1; return PM3_ERFTRANS;
} }
// update bit and frame end // update bit and frame end
@ -110,7 +110,7 @@ static int8_t rx_bit(void) {
// 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 PM3_ERFTRANS;
} }
// apply threshold (average of RWD_TIME_0 and ) // apply threshold (average of RWD_TIME_0 and )
@ -235,7 +235,7 @@ static int32_t rx_frame(uint8_t *len) {
// check for code violation // check for code violation
if (i > RWD_CMD_TIMEOUT) { if (i > RWD_CMD_TIMEOUT) {
return -1; return PM3_ETIMEOUT;
} }
} }
@ -251,7 +251,7 @@ static int32_t rx_frame(uint8_t *len) {
// 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 PM3_ERFTRANS;
} }
// check for code violation caused by end of frame // check for code violation caused by end of frame
@ -353,7 +353,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 PM3_ERFTRANS;
} }
// configure prng // configure prng
@ -375,19 +375,19 @@ 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 PM3_ERFTRANS;
} }
// 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 PM3_ERFTRANS;
break; break;
case 1: case 1:
if (ack != 0x39) return -1; if (ack != 0x39) return PM3_ERFTRANS;
break; break;
case 2: case 2:
if (ack != 0x39) return -1; if (ack != 0x39) return PM3_ERFTRANS;
break; break;
} }
@ -399,7 +399,7 @@ static int32_t setup_phase(legic_card_select_t *p_card) {
// the gap by one period. // the gap by one period.
last_frame_end += TAG_BIT_PERIOD; last_frame_end += TAG_BIT_PERIOD;
return 0; return PM3_SUCCESS;
} }
static uint8_t calc_crc4(uint16_t cmd, uint8_t cmd_sz, uint8_t value) { static uint8_t calc_crc4(uint16_t cmd, uint8_t cmd_sz, uint8_t value) {
@ -414,7 +414,7 @@ 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 PM3_ETIMEOUT;
} }
// check if command is LEGIC_READ // check if command is LEGIC_READ
@ -425,8 +425,7 @@ static int32_t connected_phase(legic_card_select_t *p_card) {
// transmit data // transmit data
tx_frame((crc << 8) | byte, 12); tx_frame((crc << 8) | byte, 12);
return PM3_SUCCESS;
return 0;
} }
// check if command is LEGIC_WRITE // check if command is LEGIC_WRITE
@ -441,7 +440,7 @@ static int32_t connected_phase(legic_card_select_t *p_card) {
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 PM3_ECRC;
} }
// store data // store data
@ -449,11 +448,10 @@ static int32_t connected_phase(legic_card_select_t *p_card) {
// transmit ack // transmit ack
tx_ack(); tx_ack();
return PM3_SUCCESS;
return 0;
} }
return -1; return PM3_ERFTRANS;
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@ -474,35 +472,30 @@ void LegicRfSimulate(uint8_t tagtype, bool send_reply) {
goto OUT; goto OUT;
} }
uint16_t counter = 0;
LED_A_ON(); LED_A_ON();
Dbprintf("Legic Prime, simulating uid: %02X%02X%02X%02X", legic_mem[0], legic_mem[1], legic_mem[2], legic_mem[3]); Dbprintf("Legic Prime, simulating uid... " _YELLOW_("%02X%02X%02X%02X"), legic_mem[0], legic_mem[1], legic_mem[2], legic_mem[3]);
while (BUTTON_PRESS() == false) { while (BUTTON_PRESS() == false) {
WDT_HIT(); WDT_HIT();
if (counter >= 2000) { if (data_available()) {
if (data_available()) { res = PM3_EOPABORTED;
res = PM3_EOPABORTED; goto OUT;
break;
}
counter = 0;
} }
counter++;
// wait for carrier, restart after timeout // wait for carrier, restart after timeout
if (wait_for(RWD_PULSE, GetCountSspClk() + TAG_BIT_PERIOD) == false) { if (wait_for(RWD_PULSE, GetCountSspClk() + TAG_BIT_PERIOD) == false) {
continue; continue;
} }
// wait for connection, restart on error // wait for connection, restart on error
if (setup_phase(&card)) { if (setup_phase(&card) != PM3_SUCCESS) {
continue; continue;
} }
// connection is established, process commands until one fails // connection is established, process commands until one fails
while (connected_phase(&card) == false) { while (connected_phase(&card) != PM3_SUCCESS) {
WDT_HIT(); WDT_HIT();
} }
} }
@ -510,11 +503,11 @@ void LegicRfSimulate(uint8_t tagtype, bool send_reply) {
OUT: OUT:
if (g_dbglevel >= DBG_ERROR) { if (g_dbglevel >= DBG_ERROR) {
Dbprintf("Emulator stopped. Tracing: %d trace length: %d ", get_tracing(), BigBuf_get_traceLen()); Dbprintf("Emulator stopped. Trace length... " _YELLOW_("%d"), BigBuf_get_traceLen());
} }
if (res == PM3_EOPABORTED) if (res == PM3_EOPABORTED)
DbpString("aborted by user"); DbpString("Aborted by user");
switch_off(); switch_off();
StopTicks(); StopTicks();

View file

@ -536,20 +536,21 @@ static int CmdLegicSim(const char *Cmd) {
SendCommandNG(CMD_HF_LEGIC_SIMULATE, (uint8_t *)&payload, sizeof(payload)); SendCommandNG(CMD_HF_LEGIC_SIMULATE, (uint8_t *)&payload, sizeof(payload));
PacketResponseNG resp; PacketResponseNG resp;
PrintAndLogEx(INFO, "Press pm3-button to abort simulation"); PrintAndLogEx(INFO, "Press " _GREEN_("<Enter>") " or pm3-button to abort simulation");
bool keypress = kbd_enter_pressed(); for (;;) {
while (keypress == false) { if (kbd_enter_pressed()) {
keypress = kbd_enter_pressed(); SendCommandNG(CMD_BREAK_LOOP, NULL, 0);
PrintAndLogEx(DEBUG, "User aborted");
break;
}
if (WaitForResponseTimeout(CMD_HF_LEGIC_SIMULATE, &resp, 1500)) { if (WaitForResponseTimeout(CMD_HF_LEGIC_SIMULATE, &resp, 1500)) {
break; break;
} }
} }
if (keypress)
SendCommandNG(CMD_BREAK_LOOP, NULL, 0);
PrintAndLogEx(INFO, "Done"); PrintAndLogEx(INFO, "Done");
PrintAndLogEx(HINT, "Try `" _YELLOW_("hf legic list") "` to view trace log");
return PM3_SUCCESS; return PM3_SUCCESS;
} }