refactoring

This commit is contained in:
tharexde 2021-01-16 19:40:42 +01:00
commit cff48f84c6
2 changed files with 23 additions and 54 deletions

View file

@ -708,9 +708,9 @@ static int em4x50_sim_send_bit(uint8_t bit) {
static int em4x50_sim_send_byte(uint8_t byte) { static int em4x50_sim_send_byte(uint8_t byte) {
// send byte // send byte
for (int i = 0; i < 8; i++) for (int i = 0; i < 8; i++) {
if (em4x50_sim_send_bit((byte >> (7 - i)) & 1) == PM3_ETIMEOUT) em4x50_sim_send_bit((byte >> (7 - i)) & 1);
return PM3_ETIMEOUT; }
return PM3_SUCCESS; return PM3_SUCCESS;
@ -724,13 +724,8 @@ static int em4x50_sim_send_byte_with_parity(uint8_t byte) {
for (int i = 0; i < 8; i++) for (int i = 0; i < 8; i++)
parity ^= (byte >> i) & 1; parity ^= (byte >> i) & 1;
if (em4x50_sim_send_byte(byte) == PM3_ETIMEOUT) { em4x50_sim_send_byte(byte);
return PM3_ETIMEOUT; em4x50_sim_send_bit(parity);
}
if (em4x50_sim_send_bit(parity) == PM3_ETIMEOUT) {
return PM3_ETIMEOUT;
}
return PM3_SUCCESS; return PM3_SUCCESS;
} }
@ -744,9 +739,7 @@ static int em4x50_sim_send_word(uint32_t word) {
// 4 bytes each with even row parity bit // 4 bytes each with even row parity bit
for (int i = 0; i < 4; i++) { for (int i = 0; i < 4; i++) {
if (em4x50_sim_send_byte_with_parity((word >> ((3 - i) * 8)) & 0xFF) == PM3_ETIMEOUT) { em4x50_sim_send_byte_with_parity((word >> ((3 - i) * 8)) & 0xFF);
return PM3_ETIMEOUT;
}
} }
// column parity // column parity
@ -756,14 +749,10 @@ static int em4x50_sim_send_word(uint32_t word) {
cparity ^= (((word >> ((3 - j) * 8)) & 0xFF) >> (7 - i)) & 1; cparity ^= (((word >> ((3 - j) * 8)) & 0xFF) >> (7 - i)) & 1;
} }
} }
if (em4x50_sim_send_byte(cparity) == PM3_ETIMEOUT) { em4x50_sim_send_byte(cparity);
return PM3_ETIMEOUT;
}
// stop bit // stop bit
if (em4x50_sim_send_bit(0) == PM3_ETIMEOUT) { em4x50_sim_send_bit(0);
return PM3_ETIMEOUT;
}
return PM3_SUCCESS; return PM3_SUCCESS;
} }
@ -848,15 +837,11 @@ static int em4x50_sim_read_bit(void) {
static int em4x50_sim_read_byte(void) { static int em4x50_sim_read_byte(void) {
int bit = 0, byte = 0; int byte = 0;
for (int i = 0; i < 8; i++) { for (int i = 0; i < 8; i++) {
byte <<= 1; byte <<= 1;
bit = em4x50_sim_read_bit(); byte |= em4x50_sim_read_bit();
if (bit == PM3_ETIMEOUT) {
return PM3_ETIMEOUT;
}
byte |= bit;
} }
return byte; return byte;
@ -864,24 +849,16 @@ static int em4x50_sim_read_byte(void) {
static int em4x50_sim_read_byte_with_parity_check(uint8_t *byte) { static int em4x50_sim_read_byte_with_parity_check(uint8_t *byte) {
int bit = 0, pval = 0; int pval = 0;
uint8_t parity = 0; uint8_t parity = 0;
for (int i = 0; i < 8; i++) { for (int i = 0; i < 8; i++) {
*byte <<= 1; *byte <<= 1;
bit = em4x50_sim_read_bit(); *byte |= em4x50_sim_read_bit();
if (bit == PM3_ETIMEOUT) {
return PM3_ETIMEOUT;
}
*byte |= bit;
parity ^= ((*byte) & 1); parity ^= ((*byte) & 1);
} }
pval = em4x50_sim_read_bit(); pval = em4x50_sim_read_bit();
if (pval == PM3_ETIMEOUT) {
return PM3_ETIMEOUT;
}
return (parity == pval) ? PM3_SUCCESS : PM3_EFAILED; return (parity == pval) ? PM3_SUCCESS : PM3_EFAILED;
} }
@ -894,20 +871,12 @@ static int em4x50_sim_read_word(uint32_t *word) {
// read plain data // read plain data
for (int i = 0; i < 4; i++) { for (int i = 0; i < 4; i++) {
if (em4x50_sim_read_byte_with_parity_check(&bytes[i]) != PM3_SUCCESS) { em4x50_sim_read_byte_with_parity_check(&bytes[i]);
return PM3_ETIMEOUT;
}
} }
// read column parities and stop bit // read column parities and stop bit
parities = em4x50_sim_read_byte(); parities = em4x50_sim_read_byte();
if (parities == PM3_ETIMEOUT) {
return PM3_ETIMEOUT;
}
stop_bit = em4x50_sim_read_bit(); stop_bit = em4x50_sim_read_bit();
if (stop_bit == PM3_ETIMEOUT) {
return PM3_ETIMEOUT;
}
// calculate column parities from data // calculate column parities from data
for (int i = 0; i < 8; i++) { for (int i = 0; i < 8; i++) {
@ -1217,8 +1186,7 @@ static int em4x50_sim_handle_selective_read_command(uint32_t *tag) {
// last word read protected // last word read protected
int lwrp = (reflect32(tag[EM4X50_PROTECTION]) >> 8) & 0xFF; int lwrp = (reflect32(tag[EM4X50_PROTECTION]) >> 8) & 0xFF;
// iceman, will need a usb cmd check to break as well while ((BUTTON_PRESS() == false) && (data_available() == false)) {
while (BUTTON_PRESS() == false) {
WDT_HIT(); WDT_HIT();
@ -1260,7 +1228,7 @@ static int em4x50_sim_handle_standard_read_command(uint32_t *tag) {
int lwrp = (reflect32(tag[EM4X50_PROTECTION]) >> 8) & 0xFF; int lwrp = (reflect32(tag[EM4X50_PROTECTION]) >> 8) & 0xFF;
// iceman, will need a usb cmd check to break as well // iceman, will need a usb cmd check to break as well
while (BUTTON_PRESS() == false) { while ((BUTTON_PRESS() == false) && (data_available() == false)) {
WDT_HIT(); WDT_HIT();
@ -1832,10 +1800,6 @@ void em4x50_sim(uint32_t *password) {
for (;;) { for (;;) {
if (data_available()) {
command = PM3_EOPABORTED;
}
switch (command) { switch (command) {
case EM4X50_COMMAND_LOGIN: case EM4X50_COMMAND_LOGIN:
@ -1863,10 +1827,12 @@ void em4x50_sim(uint32_t *password) {
break; break;
} }
// stop if key (pm3 button or enter key) has been pressed
if (command == PM3_EOPABORTED) { if (command == PM3_EOPABORTED) {
break; break;
} }
// if timeout (e.g. no reader field) go on with standard read mode
if (command == PM3_ETIMEOUT) { if (command == PM3_ETIMEOUT) {
command = EM4X50_COMMAND_STANDARD_READ; command = EM4X50_COMMAND_STANDARD_READ;
} }

View file

@ -1212,6 +1212,7 @@ int CmdEM4x50Restore(const char *Cmd) {
int CmdEM4x50Sim(const char *Cmd) { int CmdEM4x50Sim(const char *Cmd) {
int status = PM3_EFAILED;
uint32_t password = 0; uint32_t password = 0;
CLIParserContext *ctx; CLIParserContext *ctx;
@ -1256,16 +1257,18 @@ int CmdEM4x50Sim(const char *Cmd) {
keypress = kbd_enter_pressed(); keypress = kbd_enter_pressed();
if (WaitForResponseTimeout(CMD_LF_EM4X50_SIM, &resp, 1500)) { if (WaitForResponseTimeout(CMD_LF_EM4X50_SIM, &resp, 1500)) {
status = resp.status;
break; break;
} }
} }
if (keypress) { if (keypress) {
SendCommandNG(CMD_BREAK_LOOP, NULL, 0); SendCommandNG(CMD_BREAK_LOOP, NULL, 0);
status = PM3_EOPABORTED;
} }
if ((resp.status == PM3_SUCCESS) || (resp.status == PM3_EOPABORTED)) if ((status == PM3_SUCCESS) || (status == PM3_EOPABORTED))
PrintAndLogEx(SUCCESS, "Done"); PrintAndLogEx(INFO, "Done");
else else
PrintAndLogEx(FAILED, "No valid em4x50 data in memory"); PrintAndLogEx(FAILED, "No valid em4x50 data in memory");