OLD/MIX convert to NG: Phase C10

This commit is contained in:
iceman1001 committed 2026-08-29 15:50:36 +02:00
1 parent 72ff6a5b80
commit 7b7ec48bd9
4 files changed
+147 -50

No files matched your search

+63 -24
View File
@@ -1195,6 +1195,28 @@ out:
#endif #endif
// static, not on the stack: this is 512 bytes and PacketReceived is already deep
// in the call chain on a device with a small stack
static uint8_t g_dl_chunkbuf[sizeof(download_chunk_t) + DOWNLOAD_CHUNK_MAX];
static int reply_download_chunk(uint16_t cmd, uint32_t offset, const uint8_t *data, uint16_t len) {
uint8_t *buf = g_dl_chunkbuf;
download_chunk_t *chunk = (download_chunk_t *)buf;
chunk->offset = offset;
if (len && data) {
memcpy(chunk->data, data, len);
}
return reply_ng(cmd, PM3_SUCCESS, buf, sizeof(download_chunk_t) + len);
}
static void reply_download_done(uint16_t cmd, uint32_t bytes_sent, uint32_t extra) {
download_done_t done = {
.bytes_sent = bytes_sent,
.extra = extra,
};
reply_ng(cmd, PM3_SUCCESS, (uint8_t *)&done, sizeof(done));
}
static void PacketReceived(PacketCommandNG *packet) { static void PacketReceived(PacketCommandNG *packet) {
/* /*
if (packet->ng) { if (packet->ng) {
@@ -3078,23 +3100,27 @@ static void PacketReceived(PacketCommandNG *packet) {
case CMD_DOWNLOAD_BIGBUF: { case CMD_DOWNLOAD_BIGBUF: {
LED_B_ON(); LED_B_ON();
uint8_t *mem = BigBuf_get_addr(); uint8_t *mem = BigBuf_get_addr();
uint32_t startidx = packet->oldarg[0]; if (packet->length < sizeof(download_req_t)) {
uint32_t numofbytes = packet->oldarg[1]; break;
}
const download_req_t *dreq = (const download_req_t *)packet->data.asBytes;
uint32_t startidx = dreq->start_index;
uint32_t numofbytes = dreq->bytes;
// arg0 = startindex // arg0 = startindex
// arg1 = length bytes to transfer // arg1 = length bytes to transfer
// arg2 = BigBuf tracelen // arg2 = BigBuf tracelen
//Dbprintf("transfer to client parameters: %" PRIu32 " | %" PRIu32 " | %" PRIu32, startidx, numofbytes, packet->oldarg[2]); //Dbprintf("transfer to client parameters: %" PRIu32 " | %" PRIu32 " | %" PRIu32, startidx, numofbytes, packet->oldarg[2]);
for (size_t offset = 0; offset < numofbytes; offset += PM3_CMD_DATA_SIZE) { for (size_t offset = 0; offset < numofbytes; offset += DOWNLOAD_CHUNK_MAX) {
size_t len = MIN((numofbytes - offset), PM3_CMD_DATA_SIZE); size_t len = MIN((numofbytes - offset), DOWNLOAD_CHUNK_MAX);
int result = reply_old(CMD_DOWNLOADED_BIGBUF, offset, len, BigBuf_get_traceLen(), &mem[startidx + offset], len); int result = reply_download_chunk(CMD_DOWNLOADED_BIGBUF, offset, &mem[startidx + offset], len);
if (result != PM3_SUCCESS) if (result != PM3_SUCCESS)
Dbprintf("transfer to client failed :: | bytes between %d - %d (%d) | result: %d", offset, offset + len, len, result); Dbprintf("transfer to client failed :: | bytes between %d - %d (%d) | result: %d", offset, offset + len, len, result);
} }
// Trigger a finish downloading signal with an ACK frame // Trigger a finish downloading signal with an ACK frame
// arg0 = status of download transfer // arg0 = status of download transfer
reply_mix(CMD_ACK, 1, 0, BigBuf_get_traceLen(), NULL, 0); reply_download_done(CMD_DOWNLOAD_BIGBUF, numofbytes, BigBuf_get_traceLen());
LED_B_OFF(); LED_B_OFF();
break; break;
} }
@@ -3137,21 +3163,25 @@ static void PacketReceived(PacketCommandNG *packet) {
case CMD_DOWNLOAD_EML_BIGBUF: { case CMD_DOWNLOAD_EML_BIGBUF: {
LED_B_ON(); LED_B_ON();
uint8_t *mem = BigBuf_get_EM_addr(); uint8_t *mem = BigBuf_get_EM_addr();
uint32_t startidx = packet->oldarg[0]; if (packet->length < sizeof(download_req_t)) {
uint32_t numofbytes = packet->oldarg[1]; break;
}
const download_req_t *dreq = (const download_req_t *)packet->data.asBytes;
uint32_t startidx = dreq->start_index;
uint32_t numofbytes = dreq->bytes;
// arg0 = startindex // arg0 = startindex
// arg1 = length bytes to transfer // arg1 = length bytes to transfer
// arg2 = RFU // arg2 = RFU
for (size_t i = 0; i < numofbytes; i += PM3_CMD_DATA_SIZE) { for (size_t i = 0; i < numofbytes; i += DOWNLOAD_CHUNK_MAX) {
size_t len = MIN((numofbytes - i), PM3_CMD_DATA_SIZE); size_t len = MIN((numofbytes - i), DOWNLOAD_CHUNK_MAX);
int result = reply_old(CMD_DOWNLOADED_EML_BIGBUF, i, len, 0, mem + startidx + i, len); int result = reply_download_chunk(CMD_DOWNLOADED_EML_BIGBUF, i, mem + startidx + i, len);
if (result != PM3_SUCCESS) if (result != PM3_SUCCESS)
Dbprintf("transfer to client failed :: | bytes between %d - %d (%d) | result: %d", i, i + len, len, result); Dbprintf("transfer to client failed :: | bytes between %d - %d (%d) | result: %d", i, i + len, len, result);
} }
// Trigger a finish downloading signal with an ACK frame // Trigger a finish downloading signal with an ACK frame
reply_mix(CMD_ACK, 1, 0, 0, 0, 0); reply_download_done(CMD_DOWNLOAD_EML_BIGBUF, numofbytes, 0);
LED_B_OFF(); LED_B_OFF();
break; break;
} }
@@ -3239,11 +3269,16 @@ static void PacketReceived(PacketCommandNG *packet) {
case CMD_SPIFFS_DOWNLOAD: { case CMD_SPIFFS_DOWNLOAD: {
LED_B_ON(); LED_B_ON();
uint8_t filename[32]; uint8_t filename[32];
uint8_t *pfilename = packet->data.asBytes; if (packet->length < sizeof(download_req_t)) {
memcpy(filename, pfilename, SPIFFS_OBJ_NAME_LEN); reply_ng(CMD_SPIFFS_DOWNLOAD, PM3_EINVARG, NULL, 0);
break;
}
const download_req_t *dreq = (const download_req_t *)packet->data.asBytes;
uint16_t fnlen = MIN((uint16_t)(packet->length - sizeof(download_req_t)), (uint16_t)SPIFFS_OBJ_NAME_LEN);
memcpy(filename, dreq->data, fnlen);
if (g_dbglevel >= DBG_DEBUG) Dbprintf("Filename received for spiffs dump : %s", filename); if (g_dbglevel >= DBG_DEBUG) Dbprintf("Filename received for spiffs dump : %s", filename);
uint32_t size = packet->oldarg[1]; uint32_t size = dreq->bytes;
uint8_t *buff = BigBuf_calloc(size); uint8_t *buff = BigBuf_calloc(size);
if (buff == NULL) { if (buff == NULL) {
@@ -3256,9 +3291,9 @@ static void PacketReceived(PacketCommandNG *packet) {
// arg1 = size // arg1 = size
// arg2 = RFU // arg2 = RFU
for (size_t i = 0; i < size; i += PM3_CMD_DATA_SIZE) { for (size_t i = 0; i < size; i += DOWNLOAD_CHUNK_MAX) {
size_t len = MIN((size - i), PM3_CMD_DATA_SIZE); size_t len = MIN((size - i), DOWNLOAD_CHUNK_MAX);
int result = reply_old(CMD_SPIFFS_DOWNLOADED, i, len, 0, buff + i, len); int result = reply_download_chunk(CMD_SPIFFS_DOWNLOADED, i, buff + i, len);
if (result != PM3_SUCCESS) if (result != PM3_SUCCESS)
Dbprintf("transfer to client failed :: | bytes between %d - %d (%d) | result: %d", i, i + len, len, result); Dbprintf("transfer to client failed :: | bytes between %d - %d (%d) | result: %d", i, i + len, len, result);
} }
@@ -3459,8 +3494,12 @@ static void PacketReceived(PacketCommandNG *packet) {
LED_B_ON(); LED_B_ON();
uint8_t *mem = BigBuf_calloc(PM3_CMD_DATA_SIZE); uint8_t *mem = BigBuf_calloc(PM3_CMD_DATA_SIZE);
uint32_t startidx = packet->oldarg[0]; if (packet->length < sizeof(download_req_t)) {
uint32_t numofbytes = packet->oldarg[1]; break;
}
const download_req_t *dreq = (const download_req_t *)packet->data.asBytes;
uint32_t startidx = dreq->start_index;
uint32_t numofbytes = dreq->bytes;
// arg0 = startindex // arg0 = startindex
// arg1 = length bytes to transfer // arg1 = length bytes to transfer
// arg2 = RFU // arg2 = RFU
@@ -3469,14 +3508,14 @@ static void PacketReceived(PacketCommandNG *packet) {
break; break;
} }
for (size_t i = 0; i < numofbytes; i += PM3_CMD_DATA_SIZE) { for (size_t i = 0; i < numofbytes; i += DOWNLOAD_CHUNK_MAX) {
size_t len = MIN((numofbytes - i), PM3_CMD_DATA_SIZE); size_t len = MIN((numofbytes - i), DOWNLOAD_CHUNK_MAX);
Flash_CheckBusy(BUSY_TIMEOUT); Flash_CheckBusy(BUSY_TIMEOUT);
uint16_t isok = Flash_ReadDataCont(startidx + i, mem, len); uint16_t isok = Flash_ReadDataCont(startidx + i, mem, len);
if (isok == false) { if (isok == false) {
Dbprintf("reading flash memory failed with bytes between %d - %d", i, len); Dbprintf("reading flash memory failed with bytes between %d - %d", i, len);
} }
isok = reply_old(CMD_FLASHMEM_DOWNLOADED, i, len, 0, mem, len); isok = reply_download_chunk(CMD_FLASHMEM_DOWNLOADED, i, mem, len);
if (isok != PM3_SUCCESS) { if (isok != PM3_SUCCESS) {
Dbprintf("transfer to client failed with bytes between %d - %d", i, len); Dbprintf("transfer to client failed with bytes between %d - %d", i, len);
@@ -3484,7 +3523,7 @@ static void PacketReceived(PacketCommandNG *packet) {
} }
FlashStop(); FlashStop();
reply_mix(CMD_ACK, 1, 0, 0, 0, 0); reply_download_done(CMD_FLASHMEM_DOWNLOAD, numofbytes, 0);
BigBuf_free(); BigBuf_free();
LED_B_OFF(); LED_B_OFF();
break; break;
+1 -1
View File
@@ -1241,8 +1241,8 @@ int ExchangeRAW14a(uint8_t *datain, int datainlen, bool activateField, bool leav
uint8_t *recv; uint8_t *recv;
PacketResponseNG resp; PacketResponseNG resp;
resend:
uint16_t rlen_1245 = 0; uint16_t rlen_1245 = 0;
resend:
if (WaitForIso14aReply(&resp, 7000, &rlen_1245, NULL)) { if (WaitForIso14aReply(&resp, 7000, &rlen_1245, NULL)) {
recv = resp.data.asBytes; recv = resp.data.asBytes;
int iLen = rlen_1245; int iLen = rlen_1245;
+53 -25
View File
@@ -78,7 +78,7 @@ static uint64_t timeout_start_time;
static uint64_t last_packet_time; static uint64_t last_packet_time;
static bool dl_it(uint8_t *dest, uint32_t bytes, PacketResponseNG *response, size_t ms_timeout, bool show_warning, uint32_t rec_cmd); static bool dl_it(uint8_t *dest, uint32_t bytes, PacketResponseNG *response, size_t ms_timeout, bool show_warning, uint32_t rec_cmd, uint32_t req_cmd);
// Wait until the comm thread has actually put a queued command on the wire. // Wait until the comm thread has actually put a queued command on the wire.
// Callers used to sleep a fixed guess instead, which was reasonable when a // Callers used to sleep a fixed guess instead, which was reasonable when a
@@ -1222,20 +1222,30 @@ bool GetFromDevice(DeviceMemType_t memtype, uint8_t *dest, uint32_t bytes, uint3
switch (memtype) { switch (memtype) {
case BIG_BUF: { case BIG_BUF: {
SendCommandMIX(CMD_DOWNLOAD_BIGBUF, start_index, bytes, 0, NULL, 0); download_req_t req = { .start_index = start_index, .bytes = bytes };
return dl_it(dest, bytes, response, ms_timeout, show_warning, CMD_DOWNLOADED_BIGBUF); SendCommandNG(CMD_DOWNLOAD_BIGBUF, (uint8_t *)&req, sizeof(req));
return dl_it(dest, bytes, response, ms_timeout, show_warning, CMD_DOWNLOADED_BIGBUF, CMD_DOWNLOAD_BIGBUF);
} }
case BIG_BUF_EML: { case BIG_BUF_EML: {
SendCommandMIX(CMD_DOWNLOAD_EML_BIGBUF, start_index, bytes, 0, NULL, 0); download_req_t req = { .start_index = start_index, .bytes = bytes };
return dl_it(dest, bytes, response, ms_timeout, show_warning, CMD_DOWNLOADED_EML_BIGBUF); SendCommandNG(CMD_DOWNLOAD_EML_BIGBUF, (uint8_t *)&req, sizeof(req));
return dl_it(dest, bytes, response, ms_timeout, show_warning, CMD_DOWNLOADED_EML_BIGBUF, CMD_DOWNLOAD_EML_BIGBUF);
} }
case SPIFFS: { case SPIFFS: {
SendCommandMIX(CMD_SPIFFS_DOWNLOAD, start_index, bytes, 0, data, datalen); uint8_t sbuf[PM3_CMD_DATA_SIZE] = {0};
return dl_it(dest, bytes, response, ms_timeout, show_warning, CMD_SPIFFS_DOWNLOADED); download_req_t *sreq = (download_req_t *)sbuf;
sreq->start_index = start_index;
sreq->bytes = bytes;
if (datalen && data) {
memcpy(sreq->data, data, datalen);
}
SendCommandNG(CMD_SPIFFS_DOWNLOAD, sbuf, sizeof(download_req_t) + datalen);
return dl_it(dest, bytes, response, ms_timeout, show_warning, CMD_SPIFFS_DOWNLOADED, CMD_SPIFFS_DOWNLOAD);
} }
case FLASH_MEM: { case FLASH_MEM: {
SendCommandMIX(CMD_FLASHMEM_DOWNLOAD, start_index, bytes, 0, NULL, 0); download_req_t req = { .start_index = start_index, .bytes = bytes };
return dl_it(dest, bytes, response, ms_timeout, show_warning, CMD_FLASHMEM_DOWNLOADED); SendCommandNG(CMD_FLASHMEM_DOWNLOAD, (uint8_t *)&req, sizeof(req));
return dl_it(dest, bytes, response, ms_timeout, show_warning, CMD_FLASHMEM_DOWNLOADED, CMD_FLASHMEM_DOWNLOAD);
} }
case SIM_MEM: { case SIM_MEM: {
//SendCommandMIX(CMD_DOWNLOAD_SIM_MEM, start_index, bytes, 0, NULL, 0); //SendCommandMIX(CMD_DOWNLOAD_SIM_MEM, start_index, bytes, 0, NULL, 0);
@@ -1244,19 +1254,19 @@ bool GetFromDevice(DeviceMemType_t memtype, uint8_t *dest, uint32_t bytes, uint3
} }
case FPGA_MEM: { case FPGA_MEM: {
SendCommandNG(CMD_FPGAMEM_DOWNLOAD, NULL, 0); SendCommandNG(CMD_FPGAMEM_DOWNLOAD, NULL, 0);
return dl_it(dest, bytes, response, ms_timeout, show_warning, CMD_FPGAMEM_DOWNLOADED); return dl_it(dest, bytes, response, ms_timeout, show_warning, CMD_FPGAMEM_DOWNLOADED, CMD_FPGAMEM_DOWNLOAD);
} }
case MCU_FLASH: case MCU_FLASH:
case MCU_MEM: { case MCU_MEM: {
uint32_t flags = (memtype == MCU_MEM) ? READ_MEM_DOWNLOAD_FLAG_RAW : 0; uint32_t flags = (memtype == MCU_MEM) ? READ_MEM_DOWNLOAD_FLAG_RAW : 0;
SendCommandBL(CMD_READ_MEM_DOWNLOAD, start_index, bytes, flags, NULL, 0); SendCommandBL(CMD_READ_MEM_DOWNLOAD, start_index, bytes, flags, NULL, 0);
return dl_it(dest, bytes, response, ms_timeout, show_warning, CMD_READ_MEM_DOWNLOADED); return dl_it(dest, bytes, response, ms_timeout, show_warning, CMD_READ_MEM_DOWNLOADED, CMD_READ_MEM_DOWNLOAD);
} }
} }
return false; return false;
} }
static bool dl_it(uint8_t *dest, uint32_t bytes, PacketResponseNG *response, size_t ms_timeout, bool show_warning, uint32_t rec_cmd) { static bool dl_it(uint8_t *dest, uint32_t bytes, PacketResponseNG *response, size_t ms_timeout, bool show_warning, uint32_t rec_cmd, uint32_t req_cmd) {
uint32_t bytes_completed = 0; uint32_t bytes_completed = 0;
__atomic_store_n(&timeout_start_time, msclock(), __ATOMIC_SEQ_CST); __atomic_store_n(&timeout_start_time, msclock(), __ATOMIC_SEQ_CST);
@@ -1269,23 +1279,41 @@ static bool dl_it(uint8_t *dest, uint32_t bytes, PacketResponseNG *response, siz
if (getReply(response)) { if (getReply(response)) {
// the terminator is answered on the request opcode itself
if (response->cmd == req_cmd) {
if (response->status == PM3_EMALLOC) {
return false;
}
return true;
}
// CMD_READ_MEM_DOWNLOAD is served by the bootrom, which only speaks
// OLD frames and still finishes with an anonymous ACK
if (response->cmd == CMD_ACK) if (response->cmd == CMD_ACK)
return true; return true;
if (response->cmd == CMD_SPIFFS_DOWNLOAD && response->status == PM3_EMALLOC)
return false;
// Spiffs // fpgamem-plot download is converted to NG,
if (response->cmd == CMD_SPIFFS_DOWNLOAD || response->cmd == CMD_FPGAMEM_DOWNLOAD)
return true;
// sample_buf is a array pointer, located in data.c
// arg0 = offset in transfer. Startindex of this chunk
// arg1 = length bytes to transfer
// arg2 = bigbuff tracelength (?)
if (response->cmd == rec_cmd) { if (response->cmd == rec_cmd) {
uint32_t offset = response->oldarg[0]; uint32_t offset, copy_bytes;
uint32_t copy_bytes = MIN(bytes - bytes_completed, response->oldarg[1]); const uint8_t *src;
//uint32_t tracelen = response->oldarg[2];
if (response->ng) {
// NG chunk: offset in the header, size from the frame length
if (response->length < sizeof(download_chunk_t)) {
PrintAndLogEx(FAILED, "ERROR: short download chunk from device");
break;
}
const download_chunk_t *chunk = (const download_chunk_t *)response->data.asBytes;
offset = chunk->offset;
copy_bytes = response->length - sizeof(download_chunk_t);
src = chunk->data;
} else {
// OLD chunk, bootrom path
offset = response->oldarg[0];
copy_bytes = response->oldarg[1];
src = response->data.asBytes;
}
copy_bytes = MIN(bytes - bytes_completed, copy_bytes);
// extended bounds check1. upper limit is PM3_CMD_DATA_SIZE // extended bounds check1. upper limit is PM3_CMD_DATA_SIZE
// shouldn't happen // shouldn't happen
@@ -1297,7 +1325,7 @@ static bool dl_it(uint8_t *dest, uint32_t bytes, PacketResponseNG *response, siz
break; break;
} }
memcpy(dest + offset, response->data.asBytes, copy_bytes); memcpy(dest + offset, src, copy_bytes);
bytes_completed += copy_bytes; bytes_completed += copy_bytes;
} else if (response->cmd == CMD_WTX && response->length == sizeof(uint16_t)) { } else if (response->cmd == CMD_WTX && response->length == sizeof(uint16_t)) {
uint16_t wtx = response->data.asDwords[0] & 0xFFFF; uint16_t wtx = response->data.asDwords[0] & 0xFFFF;
+30
View File
@@ -417,6 +417,36 @@ typedef struct {
uint32_t timings[]; uint32_t timings[];
} epa_result_t; } epa_result_t;
// Bulk download protocol, used by CMD_DOWNLOAD_BIGBUF, CMD_DOWNLOAD_EML_BIGBUF,
// CMD_SPIFFS_DOWNLOAD and CMD_FLASHMEM_DOWNLOAD.
//
// Request: download_req_t on the CMD_DOWNLOAD_* opcode
// Chunks: download_chunk_t on the matching CMD_DOWNLOADED_* opcode, one per
// frame; the payload length gives the chunk size, so no length field
// Done: download_done_t answered on the original CMD_DOWNLOAD_* opcode,
// which is what tells the client the transfer finished
//
// CMD_READ_MEM_DOWNLOAD deliberately keeps the old format: the bootrom serves it
// and only speaks OLD frames. dl_it() handles both.
typedef struct {
uint32_t start_index;
uint32_t bytes;
uint8_t data[]; // SPIFFS: the filename
} PACKED download_req_t;
typedef struct {
uint32_t offset;
uint8_t data[];
} PACKED download_chunk_t;
typedef struct {
uint32_t bytes_sent;
uint32_t extra; // BigBuf trace length, otherwise 0
} PACKED download_done_t;
// payload bytes carried by one chunk frame
#define DOWNLOAD_CHUNK_MAX (PM3_CMD_DATA_SIZE - sizeof(download_chunk_t))
// CMD_LF_PCF7931_WRITE payload. // CMD_LF_PCF7931_WRITE payload.
// Replaces a uint32_t buf[10] that the device read as BYTES: the client wrote // Replaces a uint32_t buf[10] that the device read as BYTES: the client wrote
// OffsetWidth/OffsetPosition/InitDelay at byte offsets 28/32/36 while the device // OffsetWidth/OffsetPosition/InitDelay at byte offsets 28/32/36 while the device