Better Command structs

This commit is contained in:
Philippe Teuwen
2019-04-18 00:12:52 +02:00
parent 533667ea6d
commit 4e31c6110b
8 changed files with 219 additions and 206 deletions
+161 -154
View File
File diff suppressed because it is too large Load Diff
+9 -9
View File
@@ -313,7 +313,7 @@ void EPA_PACE_Collect_Nonce(UsbCommandNG *c) {
// now get the nonce
uint8_t nonce[256] = {0};
uint8_t requested_size = (uint8_t)c->core.old.arg[0];
uint8_t requested_size = (uint8_t)c->oldarg[0];
func_return = EPA_PACE_Get_Nonce(requested_size, nonce);
// check if the command succeeded
if (func_return < 0) {
@@ -434,19 +434,19 @@ void EPA_PACE_Replay(UsbCommandNG *c) {
uint32_t timings[sizeof(apdu_lengths_replay) / sizeof(apdu_lengths_replay[0])] = {0};
// if an APDU has been passed, save it
if (c->core.old.arg[0] != 0) {
if (c->oldarg[0] != 0) {
// make sure it's not too big
if (c->core.old.arg[2] > apdus_replay[c->core.old.arg[0] - 1].len) {
if (c->oldarg[2] > apdus_replay[c->oldarg[0] - 1].len) {
cmd_send(CMD_ACK, 1, 0, 0, NULL, 0);
}
memcpy(apdus_replay[c->core.old.arg[0] - 1].data + c->core.old.arg[1],
c->core.old.d.asBytes,
c->core.old.arg[2]);
memcpy(apdus_replay[c->oldarg[0] - 1].data + c->oldarg[1],
c->data.asBytes,
c->oldarg[2]);
// save/update APDU length
if (c->core.old.arg[1] == 0) {
apdu_lengths_replay[c->core.old.arg[0] - 1] = c->core.old.arg[2];
if (c->oldarg[1] == 0) {
apdu_lengths_replay[c->oldarg[0] - 1] = c->oldarg[2];
} else {
apdu_lengths_replay[c->core.old.arg[0] - 1] += c->core.old.arg[2];
apdu_lengths_replay[c->oldarg[0] - 1] += c->oldarg[2];
}
cmd_send(CMD_ACK, 0, 0, 0, NULL, 0);
return;
+3 -3
View File
@@ -479,9 +479,9 @@ void felica_sendraw(UsbCommandNG *c) {
if (MF_DBGLEVEL > 3) Dbprintf("FeliCa_sendraw Enter");
felica_command_t param = c->core.old.arg[0];
size_t len = c->core.old.arg[1] & 0xffff;
uint8_t *cmd = c->core.old.d.asBytes;
felica_command_t param = c->oldarg[0];
size_t len = c->oldarg[1] & 0xffff;
uint8_t *cmd = c->data.asBytes;
uint32_t arg0;
felica_card_select_t card;
+5 -5
View File
@@ -2351,11 +2351,11 @@ int iso14_apdu(uint8_t *cmd, uint16_t cmd_len, bool send_chaining, void *data, u
// arg2 timeout
// d.asBytes command bytes to send
void ReaderIso14443a(UsbCommandNG *c) {
iso14a_command_t param = c->core.old.arg[0];
size_t len = c->core.old.arg[1] & 0xffff;
size_t lenbits = c->core.old.arg[1] >> 16;
uint32_t timeout = c->core.old.arg[2];
uint8_t *cmd = c->core.old.d.asBytes;
iso14a_command_t param = c->oldarg[0];
size_t len = c->oldarg[1] & 0xffff;
size_t lenbits = c->oldarg[1] >> 16;
uint32_t timeout = c->oldarg[2];
uint8_t *cmd = c->data.asBytes;
uint32_t arg0;
uint8_t buf[USB_CMD_DATA_SIZE] = {0x00};
uint8_t par[MAX_PARITY_SIZE] = {0x00};
+4 -4
View File
@@ -1567,10 +1567,10 @@ void iso14b_set_trigger(bool enable) {
*
*/
void SendRawCommand14443B_Ex(UsbCommandNG *c) {
iso14b_command_t param = c->core.old.arg[0];
size_t len = c->core.old.arg[1] & 0xffff;
uint32_t timeout = c->core.old.arg[2];
uint8_t *cmd = c->core.old.d.asBytes;
iso14b_command_t param = c->oldarg[0];
size_t len = c->oldarg[1] & 0xffff;
uint32_t timeout = c->oldarg[2];
uint8_t *cmd = c->data.asBytes;
uint8_t status;
uint32_t sendlen = sizeof(iso14b_card_select_t);
uint8_t buf[USB_CMD_DATA_SIZE] = {0x00};
+7 -7
View File
@@ -90,9 +90,9 @@ void UsbPacketReceived(UsbCommandNG *packet) {
//if ( len != sizeof(UsbCommand)) Fatal();
uint32_t arg0 = (uint32_t)packet->core.old.arg[0];
uint32_t arg0 = (uint32_t)packet->oldarg[0];
switch (packet->core.old.cmd) {
switch (packet->cmd) {
case CMD_DEVICE_INFO: {
dont_ack = 1;
arg0 = DEVICE_INFO_FLAG_BOOTROM_PRESENT | DEVICE_INFO_FLAG_CURRENT_MODE_BOOTROM |
@@ -110,7 +110,7 @@ void UsbPacketReceived(UsbCommandNG *packet) {
*/
p = (volatile uint32_t *)&_flash_start;
for (i = 0; i < 12; i++)
p[i + arg0] = packet->core.old.d.asDwords[i];
p[i + arg0] = packet->data.asDwords[i];
}
break;
@@ -118,7 +118,7 @@ void UsbPacketReceived(UsbCommandNG *packet) {
uint32_t *flash_mem = (uint32_t *)(&_flash_start);
for (int j = 0; j < 2; j++) {
for (i = 0 + (64 * j); i < 64 + (64 * j); i++) {
flash_mem[i] = packet->core.old.d.asDwords[i];
flash_mem[i] = packet->data.asDwords[i];
}
uint32_t flash_address = arg0 + (0x100 * j);
@@ -154,7 +154,7 @@ void UsbPacketReceived(UsbCommandNG *packet) {
break;
case CMD_START_FLASH: {
if (packet->core.old.arg[2] == START_FLASH_MAGIC)
if (packet->oldarg[2] == START_FLASH_MAGIC)
bootrom_unlocked = 1;
else
bootrom_unlocked = 0;
@@ -163,8 +163,8 @@ void UsbPacketReceived(UsbCommandNG *packet) {
int prot_end = (int)&_bootrom_end;
int allow_start = (int)&_flash_start;
int allow_end = (int)&_flash_end;
int cmd_start = packet->core.old.arg[0];
int cmd_end = packet->core.old.arg[1];
int cmd_start = packet->oldarg[0];
int cmd_end = packet->oldarg[1];
/* Only allow command if the bootrom is unlocked, or the parameters are outside of the protected
* bootrom area. In any case they must be within the flash area.
+10 -10
View File
@@ -26,7 +26,7 @@ static pthread_t USB_communication_thread;
// Transmit buffer.
static UsbCommandOLD txBuffer;
static UsbCommandNG txBufferNG;
static UsbCommandNGRaw txBufferNG;
size_t txBufferNGLen;
static bool txBuffer_pending = false;
static pthread_mutex_t txBufferMutex = PTHREAD_MUTEX_INITIALIZER;
@@ -104,7 +104,7 @@ void SendCommandNG(uint16_t cmd, uint8_t *data, size_t len) {
return;
}
UsbCommandNGPostamble *tx_post = (UsbCommandNGPostamble *)((uint8_t *)&txBufferNG + sizeof(UsbCommandNGPreamble) + sizeof(UsbPacketNGCore) - USB_CMD_DATA_SIZE + len);
UsbCommandNGPostamble *tx_post = (UsbCommandNGPostamble *)((uint8_t *)&txBufferNG + sizeof(UsbCommandNGPreamble) + len);
pthread_mutex_lock(&txBufferMutex);
/**
@@ -116,14 +116,14 @@ void SendCommandNG(uint16_t cmd, uint8_t *data, size_t len) {
pthread_cond_wait(&txBufferSig, &txBufferMutex);
}
txBufferNG.magic = USB_COMMANDNG_PREAMBLE_MAGIC;
txBufferNG.length = len;
txBufferNG.core.ng.cmd = cmd;
memcpy(&txBufferNG.core.ng.data, data, len);
txBufferNG.pre.magic = USB_COMMANDNG_PREAMBLE_MAGIC;
txBufferNG.pre.length = len;
txBufferNG.pre.cmd = cmd;
memcpy(&txBufferNG.data, data, len);
uint8_t first, second;
compute_crc(CRC_14443_A, (uint8_t *)&txBufferNG, sizeof(UsbCommandNGPreamble) + sizeof(UsbPacketNGCore) - USB_CMD_DATA_SIZE + len, &first, &second);
compute_crc(CRC_14443_A, (uint8_t *)&txBufferNG, sizeof(UsbCommandNGPreamble) + len, &first, &second);
tx_post->crc = (first << 8) + second;
txBufferNGLen = sizeof(UsbCommandNGPreamble) + sizeof(UsbPacketNGCore) - USB_CMD_DATA_SIZE + len + sizeof(UsbCommandNGPostamble);
txBufferNGLen = sizeof(UsbCommandNGPreamble) + len + sizeof(UsbCommandNGPostamble);
txBuffer_pending = true;
// tell communication thread that a new command can be send
@@ -311,13 +311,13 @@ __attribute__((force_align_arg_pointer))
}
}
if (!error) { // Get the postamble
if ((!uart_receive(sp, (uint8_t *)&rx_raw.post, sizeof(UsbReplyNGPostamble), &rxlen)) || (rxlen != sizeof(UsbReplyNGPostamble))) {
if ((!uart_receive(sp, (uint8_t *)&rx_raw.foopost, sizeof(UsbReplyNGPostamble), &rxlen)) || (rxlen != sizeof(UsbReplyNGPostamble))) {
PrintAndLogEx(WARNING, "Received packet frame error fetching postamble");
error = true;
}
}
if (!error) { // Check CRC
rx.crc = rx_raw.post.crc;
rx.crc = rx_raw.foopost.crc;
uint8_t first, second;
compute_crc(CRC_14443_A, (uint8_t *)&rx_raw, sizeof(UsbReplyNGPreamble) + rx.length, &first, &second);
if ((first << 8) + second != rx.crc) {
+20 -14
View File
@@ -35,14 +35,10 @@ typedef struct {
} d;
} PACKED UsbCommandOLD;
typedef struct {
uint16_t cmd;
uint8_t data[USB_CMD_DATA_SIZE];
} PACKED UsbPacketNGCore;
typedef struct {
uint32_t magic;
uint16_t length; // length of the variable part, 0 if none.
uint16_t cmd;
} PACKED UsbCommandNGPreamble;
#define USB_COMMANDNG_PREAMBLE_MAGIC 0x61334d50 // PM3a
@@ -51,17 +47,27 @@ typedef struct {
uint16_t crc;
} PACKED UsbCommandNGPostamble;
// For internal usage
typedef struct {
uint32_t magic;
uint16_t length; // length of the variable part, 0 if none.
union { // we can simplify it once we get rid of old format compatibility
UsbPacketNGCore ng;
UsbCommandOLD old;
} core;
uint16_t crc;
bool ng;
uint16_t cmd;
uint16_t length;
uint32_t magic; // NG
uint16_t crc; // NG
uint64_t oldarg[3]; // OLD
union {
uint8_t asBytes[USB_CMD_DATA_SIZE];
uint32_t asDwords[USB_CMD_DATA_SIZE / 4];
} data;
bool ng; // does it store NG data or OLD data?
} PACKED UsbCommandNG;
// For reception and CRC check
typedef struct {
UsbCommandNGPreamble pre;
uint8_t data[USB_CMD_DATA_SIZE];
UsbCommandNGPostamble foopost; // Probably not at that offset!
} PACKED UsbCommandNGRaw;
typedef struct {
uint32_t magic;
uint16_t length; // length of the variable part, 0 if none.
@@ -94,7 +100,7 @@ typedef struct {
typedef struct {
UsbReplyNGPreamble pre;
uint8_t data[USB_CMD_DATA_SIZE];
UsbReplyNGPostamble post;
UsbReplyNGPostamble foopost; // Probably not at that offset!
} PACKED UsbReplyNGRaw;
#ifdef WITH_FPC_HOST