diff --git a/armsrc/em4x50.c b/armsrc/em4x50.c index 3f0ad9b6b..bce422908 100644 --- a/armsrc/em4x50.c +++ b/armsrc/em4x50.c @@ -14,6 +14,7 @@ #include "lfadc.h" #include "commonutil.h" #include "em4x50.h" +#include "appmain.h" // tear // 4 data bytes // + byte with row parities @@ -470,6 +471,8 @@ static bool find_double_listen_window(bool bcommand) { if (bcommand) { +// SpinDelay(10); + // data transmission from card has to be stopped, because // a commamd shall be issued @@ -816,7 +819,7 @@ void em4x50_info(em4x50_data_t *etd) { status = (bsuccess << 1) + blogin; lf_finalize(); - reply_ng(CMD_ACK, status, (uint8_t *)tag.sectors, 238); + reply_ng(CMD_LF_EM4X50_INFO, status, (uint8_t *)tag.sectors, 238); } void em4x50_read(em4x50_data_t *etd) { @@ -860,14 +863,13 @@ void em4x50_read(em4x50_data_t *etd) { status = (now << 2) + (bsuccess << 1) + blogin; lf_finalize(); - reply_ng(CMD_ACK, status, (uint8_t *)tag.sectors, 238); + reply_ng(CMD_LF_EM4X50_READ, status, (uint8_t *)tag.sectors, 238); } //============================================================================== // write functions //============================================================================== - -static bool write(uint8_t word[4], uint8_t address) { +static int write(uint8_t word[4], uint8_t address) { // writes to specified
@@ -882,17 +884,23 @@ static bool write(uint8_t word[4], uint8_t address) { // send data em4x50_send_word(word); - // wait for T0 * EM4X50_T_TAG_TWA (write access time) - wait_timer(FPGA_TIMER_0, T0 * EM4X50_T_TAG_TWA); + if (tearoff_hook() == PM3_ETEAROFF) { // tearoff occured + reply_ng(CMD_LF_EM4X50_WRITE, PM3_ETEAROFF, NULL, 0); + return PM3_ETEAROFF; + } else { - // look for ACK sequence - if (check_ack(false)) { + // wait for T0 * EM4X50_T_TAG_TWA (write access time) + wait_timer(FPGA_TIMER_0, T0 * EM4X50_T_TAG_TWA); - // now EM4x50 needs T0 * EM4X50_T_TAG_TWEE (EEPROM write time) - // for saving data and should return with ACK - if (check_ack(false)) - return true; + // 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 { @@ -900,10 +908,10 @@ static bool write(uint8_t word[4], uint8_t address) { Dbprintf("error in command request"); } - return false; + return PM3_ESOFT; } -static bool write_password(uint8_t password[4], uint8_t new_password[4]) { +static int write_password(uint8_t password[4], uint8_t new_password[4]) { // changes password from to @@ -915,23 +923,29 @@ static bool write_password(uint8_t password[4], uint8_t new_password[4]) { // send address data em4x50_send_word(password); - // wait for T0 * EM4x50_T_TAG_TPP (processing pause time) - wait_timer(FPGA_TIMER_0, T0 * EM4X50_T_TAG_TPP); + if (tearoff_hook() == PM3_ETEAROFF) { // tearoff occured + reply_ng(CMD_LF_EM4X50_WRITE, PM3_ETEAROFF, NULL, 0); + return PM3_ETEAROFF; + } else { - // look for ACK sequence and send rm request - // during following listen window - if (check_ack(true)) { + // wait for T0 * EM4x50_T_TAG_TPP (processing pause time) + wait_timer(FPGA_TIMER_0, T0 * EM4X50_T_TAG_TPP); - // send new password - em4x50_send_word(new_password); + // look for ACK sequence and send rm request + // during following listen window + if (check_ack(true)) { - // wait for T0 * EM4X50_T_TAG_TWA (write access time) - wait_timer(FPGA_TIMER_0, T0 * EM4X50_T_TAG_TWA); + // send new password + em4x50_send_word(new_password); + + // wait for T0 * EM4X50_T_TAG_TWA (write access time) + wait_timer(FPGA_TIMER_0, T0 * EM4X50_T_TAG_TWA); - if (check_ack(false)) if (check_ack(false)) - return true; + if (check_ack(false)) + return PM3_SUCCESS; + } } } else { @@ -939,7 +953,7 @@ static bool write_password(uint8_t password[4], uint8_t new_password[4]) { Dbprintf("error in command request"); } - return false; + return PM3_ESOFT; } void em4x50_write(em4x50_data_t *etd) { @@ -966,8 +980,13 @@ void em4x50_write(em4x50_data_t *etd) { blogin = login(etd->password); // write word to given address - if (write(etd->word, etd->address)) { - + int res = write(etd->word, etd->address); + if (res == PM3_ETEAROFF) { + lf_finalize(); + return; + } + + if (res == PM3_SUCCESS) { // to verify result reset EM4x50 if (reset()) { @@ -996,9 +1015,8 @@ void em4x50_write(em4x50_data_t *etd) { } status = (bsuccess << 1) + blogin; - lf_finalize(); - reply_ng(CMD_ACK, status, (uint8_t *)tag.sectors, 238); + reply_ng(CMD_LF_EM4X50_WRITE, status, (uint8_t *)tag.sectors, 238); } void em4x50_write_password(em4x50_data_t *etd) { @@ -1015,12 +1033,18 @@ void em4x50_write_password(em4x50_data_t *etd) { // login and change password if (login(etd->password)) { - bsuccess = write_password(etd->password, etd->new_password); + + int res = write_password(etd->password, etd->new_password); + if (res == PM3_ETEAROFF) { + lf_finalize(); + return; + } + bsuccess = (res == PM3_SUCCESS); } } lf_finalize(); - reply_ng(CMD_ACK, bsuccess, 0, 0); + reply_ng(CMD_LF_EM4X50_WRITE_PASSWORD, bsuccess, 0, 0); } void em4x50_wipe(em4x50_data_t *etd) { @@ -1078,5 +1102,5 @@ void em4x50_wipe(em4x50_data_t *etd) { } lf_finalize(); - reply_ng(CMD_ACK, bsuccess, (uint8_t *)tag.sectors, 238); + reply_ng(CMD_LF_EM4X50_WIPE, bsuccess, (uint8_t *)tag.sectors, 238); } diff --git a/client/src/cmdlfem4x50.c b/client/src/cmdlfem4x50.c index fa4e2f765..3bddc2113 100644 --- a/client/src/cmdlfem4x50.c +++ b/client/src/cmdlfem4x50.c @@ -315,7 +315,7 @@ int CmdEM4x50Info(const char *Cmd) { SendCommandNG(CMD_LF_EM4X50_INFO, (uint8_t *)&etd, sizeof(etd)); PacketResponseNG resp; - if (!WaitForResponseTimeout(CMD_ACK, &resp, TIMEOUT)) { + if (!WaitForResponseTimeout(CMD_LF_EM4X50_INFO, &resp, TIMEOUT)) { PrintAndLogEx(WARNING, "timeout while waiting for reply."); return PM3_ETIMEOUT; } @@ -388,7 +388,7 @@ int CmdEM4x50Write(const char *Cmd) { clearCommandBuffer(); SendCommandNG(CMD_LF_EM4X50_WRITE, (uint8_t *)&etd, sizeof(etd)); PacketResponseNG resp; - if (!WaitForResponseTimeout(CMD_ACK, &resp, TIMEOUT)) { + if (!WaitForResponseTimeout(CMD_LF_EM4X50_WRITE, &resp, TIMEOUT)) { PrintAndLogEx(WARNING, "timeout while waiting for reply."); return PM3_ETIMEOUT; } @@ -483,7 +483,7 @@ int CmdEM4x50WritePassword(const char *Cmd) { clearCommandBuffer(); SendCommandNG(CMD_LF_EM4X50_WRITE_PASSWORD, (uint8_t *)&etd, sizeof(etd)); - if (!WaitForResponseTimeout(CMD_ACK, &resp, TIMEOUT)) { + if (!WaitForResponseTimeout(CMD_LF_EM4X50_WRITE_PASSWORD, &resp, TIMEOUT)) { PrintAndLogEx(WARNING, "timeout while waiting for reply."); return PM3_ETIMEOUT; } @@ -515,7 +515,7 @@ int em4x50_read(em4x50_data_t *etd, em4x50_word_t *out, bool verbose) { SendCommandNG(CMD_LF_EM4X50_READ, (uint8_t *)&edata, sizeof(edata)); PacketResponseNG resp; - if (!WaitForResponseTimeout(CMD_ACK, &resp, TIMEOUT)) { + if (!WaitForResponseTimeout(CMD_LF_EM4X50_READ, &resp, TIMEOUT)) { PrintAndLogEx(WARNING, "(em4x50) timeout while waiting for reply."); return PM3_ETIMEOUT; } @@ -651,7 +651,7 @@ int CmdEM4x50Dump(const char *Cmd) { clearCommandBuffer(); SendCommandNG(CMD_LF_EM4X50_INFO, (uint8_t *)&etd, sizeof(etd)); PacketResponseNG resp; - if (!WaitForResponseTimeout(CMD_ACK, &resp, TIMEOUT)) { + if (!WaitForResponseTimeout(CMD_LF_EM4X50_INFO, &resp, TIMEOUT)) { PrintAndLogEx(WARNING, "timeout while waiting for reply."); return PM3_ETIMEOUT; } @@ -726,7 +726,7 @@ int CmdEM4x50Wipe(const char *Cmd) { clearCommandBuffer(); SendCommandNG(CMD_LF_EM4X50_WIPE, (uint8_t *)&etd, sizeof(etd)); - if (!WaitForResponseTimeout(CMD_ACK, &resp, 2 * TIMEOUT)) { + if (!WaitForResponseTimeout(CMD_LF_EM4X50_WIPE, &resp, 2 * TIMEOUT)) { PrintAndLogEx(WARNING, "\ntimeout while waiting for reply.\n"); return PM3_ETIMEOUT; }