diff --git a/armsrc/cmd.c b/armsrc/cmd.c index b5774eab2..8f92009ce 100644 --- a/armsrc/cmd.c +++ b/armsrc/cmd.c @@ -44,9 +44,9 @@ int reply_old(uint64_t cmd, uint64_t arg0, uint64_t arg1, uint64_t arg2, const v txcmd.arg[1] = arg1; txcmd.arg[2] = arg2; - // Add the (optional) content to the frame, with a maximum size of PM3_CMD_DATA_SIZE + // Add the (optional) content to the frame, with a maximum size of PM3_CMD_DATA_SIZE_OLD if (data && len) { - len = MIN(len, PM3_CMD_DATA_SIZE); + len = MIN(len, PM3_CMD_DATA_SIZE_OLD); for (size_t i = 0; i < len; i++) { txcmd.d.asBytes[i] = ((const uint8_t *)data)[i]; } @@ -245,7 +245,7 @@ static int receive_ng_internal(PacketCommandNG *rx, uint32_t read_ng(uint8_t *da rx->oldarg[0] = rx_old.arg[0]; rx->oldarg[1] = rx_old.arg[1]; rx->oldarg[2] = rx_old.arg[2]; - rx->length = PM3_CMD_DATA_SIZE; + rx->length = PM3_CMD_DATA_SIZE_OLD; memcpy(&rx->data, &rx_old.d.asBytes, rx->length); } return PM3_SUCCESS; diff --git a/bootrom/bootrom.c b/bootrom/bootrom.c index 3583adbd9..620302677 100644 --- a/bootrom/bootrom.c +++ b/bootrom/bootrom.c @@ -65,9 +65,9 @@ static int reply_old(uint64_t cmd, uint64_t arg0, uint64_t arg1, uint64_t arg2, txcmd.arg[1] = arg1; txcmd.arg[2] = arg2; - // Add the (optional) content to the frame, with a maximum size of PM3_CMD_DATA_SIZE + // Add the (optional) content to the frame, with a maximum size of PM3_CMD_DATA_SIZE_OLD if (data && len) { - len = MIN(len, PM3_CMD_DATA_SIZE); + len = MIN(len, PM3_CMD_DATA_SIZE_OLD); for (size_t i = 0; i < len; i++) { txcmd.d.asBytes[i] = ((uint8_t *)data)[i]; } @@ -188,8 +188,8 @@ static void UsbPacketReceived(uint8_t *packet) { } if (isok) { - for (size_t pos = 0; pos < count; pos += PM3_CMD_DATA_SIZE) { - size_t len = MIN((count - pos), PM3_CMD_DATA_SIZE); + for (size_t pos = 0; pos < count; pos += PM3_CMD_DATA_SIZE_OLD) { + size_t len = MIN((count - pos), PM3_CMD_DATA_SIZE_OLD); isok = (0 == reply_old(CMD_READ_MEM_DOWNLOADED, pos, len, 0, &base[offset + pos], len)); if (!isok) { break; diff --git a/client/src/comms.c b/client/src/comms.c index c96ecf369..33cf760ff 100644 --- a/client/src/comms.c +++ b/client/src/comms.c @@ -115,7 +115,7 @@ void SendCommandOLD(uint64_t cmd, uint64_t arg0, uint64_t arg1, uint64_t arg2, c PacketCommandOLD c = {CMD_UNKNOWN, {0, 0, 0}, {{0}}}; - if (len > PM3_CMD_DATA_SIZE) { + if (len > PM3_CMD_DATA_SIZE_OLD) { PrintAndLogEx(WARNING, "Sending " _RED_("%zu") " bytes of payload is too much for OLD frames, abort", len); return; // return PM3_EOUTOFBOUND; @@ -622,7 +622,7 @@ __attribute__((force_align_arg_pointer)) rx.oldarg[0] = rx_old.arg[0]; rx.oldarg[1] = rx_old.arg[1]; rx.oldarg[2] = rx_old.arg[2]; - rx.length = PM3_CMD_DATA_SIZE; + rx.length = PM3_CMD_DATA_SIZE_OLD; memcpy(&rx.data, &rx_old.d, rx.length); PacketResponseReceived(&rx); if (rx.cmd == CMD_ACK) { diff --git a/client/src/flash.c b/client/src/flash.c index 7b2baf00e..abfb93106 100644 --- a/client/src/flash.c +++ b/client/src/flash.c @@ -833,17 +833,17 @@ static void flash_write_err_software(int pm3_err) { } // Send finish write cmd and waiting for response. -// The send_buf length is always 512byte(PM3_CMD_DATA_SIZE) +// The send_buf length is always 512byte(PM3_CMD_DATA_SIZE_OLD) static int send_finish_write_cmd(uint32_t address, int magic, uint8_t *send_buf, PacketResponseNG *resp) { - // The sending length is always PM3_CMD_DATA_SIZE, which is 512 bytes, because of the limitation of the old frame. - const int send_len = PM3_CMD_DATA_SIZE; + // The sending length is always PM3_CMD_DATA_SIZE_OLD, which is 512 bytes, because of the limitation of the old frame. + const int send_len = PM3_CMD_DATA_SIZE_OLD; #if defined ICOPYX // To prevent users from flashing unsupported firmware, icopyx checks arg1 and arg2 in this command. // Therefore, when sending magic to the device, we should not choose a value that happens to be the same as icopyx. // In fact, neither PM3V nor PM5V will be 0xff or 0x1fd, so this should have strong robustness. SendCommandBL(CMD_FINISH_WRITE, address, 0xff, 0x1fd, send_buf, send_len); #else - // If it's an older version of the flashher or a flashher specific to icopyx, then arg1 should be 0x00 or 0xff, + // If it's an older version of the flasher or a flasher specific to icopyx, then arg1 should be 0x00 or 0xff, // not a valid magic value. The client is specifically designed for icopyx. // --- // For devices with older firmware, it doesn't care about arg1, @@ -867,14 +867,14 @@ static int send_finish_write_cmd(uint32_t address, int magic, uint8_t *send_buf, // Write a block of data to flash, padding to the block size if needed. The bootloader will read the entire block, // so we need to make sure to pad it with 0xFF if the data is smaller than the block size. static int write_block(uint32_t address, int magic, uint8_t *data, uint32_t length, flash_dev_t *flash_dev) { - // Align length to PM3_CMD_DATA_SIZE or block_size + // Align length to PM3_CMD_DATA_SIZE_OLD or block_size // It is necessary to align with the minimum write unit of the target chip, // otherwise it may cause the device to lose the data or offset errors. - uint32_t padded_len = length % MAX(PM3_CMD_DATA_SIZE, flash_dev->block_size); + uint32_t padded_len = length % MAX(PM3_CMD_DATA_SIZE_OLD, flash_dev->block_size); if (padded_len) { - padded_len = MAX(PM3_CMD_DATA_SIZE, flash_dev->block_size) - padded_len; + padded_len = MAX(PM3_CMD_DATA_SIZE_OLD, flash_dev->block_size) - padded_len; } - // After aligning PM3_CMD_DATA_SIZE, allocate a new buffer, copy the data, and pad the end with 0xFF. + // After aligning PM3_CMD_DATA_SIZE_OLD, allocate a new buffer, copy the data, and pad the end with 0xFF. uint32_t aligned_len = length + padded_len; uint8_t *block_buf = malloc(aligned_len); if (block_buf == NULL) { @@ -904,7 +904,7 @@ static int write_block(uint32_t address, int magic, uint8_t *data, uint32_t leng } break; } - sent += PM3_CMD_DATA_SIZE; + sent += PM3_CMD_DATA_SIZE_OLD; } free(block_buf); // remember to free buffer return ret; diff --git a/include/pm3_cmd.h b/include/pm3_cmd.h index a60d30689..84484394c 100644 --- a/include/pm3_cmd.h +++ b/include/pm3_cmd.h @@ -26,13 +26,16 @@ #define USART_SLOW_LINK #define PM3_CMD_DATA_SIZE 512 +// OLD frames are pinned at 512 independently of PM3_CMD_DATA_SIZE: +// the bootloader only speaks OLD +#define PM3_CMD_DATA_SIZE_OLD 512 typedef struct { uint64_t cmd; uint64_t arg[3]; union { - uint8_t asBytes[PM3_CMD_DATA_SIZE]; - uint32_t asDwords[PM3_CMD_DATA_SIZE / 4]; + uint8_t asBytes[PM3_CMD_DATA_SIZE_OLD]; + uint32_t asDwords[PM3_CMD_DATA_SIZE_OLD / 4]; } d; } PACKED PacketCommandOLD; @@ -75,8 +78,8 @@ typedef struct { uint64_t cmd; uint64_t arg[3]; union { - uint8_t asBytes[PM3_CMD_DATA_SIZE]; - uint32_t asDwords[PM3_CMD_DATA_SIZE / 4]; + uint8_t asBytes[PM3_CMD_DATA_SIZE_OLD]; + uint32_t asDwords[PM3_CMD_DATA_SIZE_OLD / 4]; } d; } PACKED PacketResponseOLD;