fix spiffs downloads >= 64K, and stop losing flash errors

CMD_SPIFFS_DOWNLOAD read the whole file into BigBuf first.  BigBuf_calloc()
takes a uint16_t while the size is a uint32_t, so a 64K file allocated zero
bytes and anything larger wrapped to a short buffer the file was then read
straight past the end of.  rdv40_spiffs_read_stream() opens the file once and
hands out one frame at a time, so the size never reaches the allocator, and the
SPIFFS path now honours start_index.  Verified with an 80K round trip: upload,
download, byte identical.

The same BigBuf_calloc(filesize) wrap was in copy_in_spiffs(); it now copies in
SPIFFS_WRITE_CHUNK_SIZE chunks.

Separately, every flash failure was invisible.  SPIFFS_CHECK_RES() only treats a
negative result as an error and the HAL hooks returned 128/129/130, so SPIFFS
saw success.  The erase path was worst: Flash_Erase4k() only reports that the
command was sent, both Flash_CheckBusy() results were discarded, and
'return (SPIFFS_OK == erased)' yields 1 on failure.  A flash dump from a device
that hit this showed one block written without being erased - its object index
header held '0x14000 & 0x10000', and the filename ANDed away to empty.  Erases
are now read back and retried, and the HAL returns real SPIFFS error codes.

Flash_Write() returned len unconditionally even after a rejected page, which
also defeated the 'res == payload->len' check in CMD_FLASHMEM_WRITE.

Feedback, so none of this is silent again: CMD_SPIFFS_WRITE answers with the
SPIFFS result, the client aborts on the first refusal and names the byte it
stopped at, dl_it() checks the terminator status, and both transfer directions
print inline progress.

Co-Authored-By: Claude Opus 5 (1M context)
This commit is contained in:
iceman1001andClaude Opus 5 (1M context) committed 2026-09-12 14:58:08 +02:00
1 parent a967560335
commit 9a8e179266
6 files changed
+345 -50

No files matched your search

+38 -4
View File
@@ -22,6 +22,7 @@
#include "pmflash.h"
#include "fileutils.h" //saveFile
#include "comms.h" //getfromdevice
#include "util_posix.h" // msclock
#include "cliparser.h"
static int CmdHelp(const char *Cmd);
@@ -41,6 +42,13 @@ int flashmem_spiffs_load(const char *destfn, const uint8_t *data, size_t datalen
// fast push mode
g_conn.block_after_ACK = true;
// inline feedback, an upload is one packet per FLASH_MEM_BLOCK_SIZE bytes.
// Time based, not byte based: SPIFFS garbage collection can make a single
// packet take seconds on a full filesystem, and a counter that only moves
// every 16 packets is indistinguishable from a hang
const bool show_progress = (datalen > FLASH_MEM_BLOCK_SIZE);
uint64_t last_update = 0;
while (bytes_remaining > 0) {
uint32_t bytes_in_packet = MIN(FLASH_MEM_BLOCK_SIZE, bytes_remaining);
@@ -69,9 +77,6 @@ int flashmem_spiffs_load(const char *destfn, const uint8_t *data, size_t datalen
free(payload);
bytes_remaining -= bytes_in_packet;
bytes_sent += bytes_in_packet;
uint8_t retry = 3;
while (WaitForResponseTimeout(CMD_SPIFFS_WRITE, &resp, 2000) == false) {
PrintAndLogEx(WARNING, "timeout while waiting for reply");
@@ -81,9 +86,38 @@ int flashmem_spiffs_load(const char *destfn, const uint8_t *data, size_t datalen
goto out;
}
}
// the device only counts a packet once SPIFFS took it
if (resp.status != PM3_SUCCESS) {
int32_t serr = (resp.length >= sizeof(int32_t)) ? (int32_t)resp.data.asDwords[0] : 0;
if (show_progress) {
PrintAndLogEx(NORMAL, "");
}
PrintAndLogEx(FAILED, "device refused the write at " _YELLOW_("%u") " / " _YELLOW_("%zu") " bytes (spiffs errno %d)", bytes_sent, datalen, serr);
if (serr == SPIFFS_ERR_FULL_RDV) {
PrintAndLogEx(HINT, "Hint: filesystem is full, try `" _YELLOW_("mem spiffs tree") "` and remove what you don't need");
}
ret_val = PM3_EFLASH;
goto out;
}
bytes_remaining -= bytes_in_packet;
bytes_sent += bytes_in_packet;
if (show_progress && ((msclock() - last_update > 100) || (bytes_remaining == 0))) {
PrintAndLogEx(INPLACE, "Sent " _YELLOW_("%u") " / " _YELLOW_("%zu") " bytes (" _YELLOW_("%u") "%%)"
, bytes_sent
, datalen
, (uint32_t)((bytes_sent * 100) / datalen)
);
last_update = msclock();
}
}
out:
if (show_progress) {
PrintAndLogEx(NORMAL, "");
}
clearCommandBuffer();
// turn off fast push mode
@@ -532,9 +566,9 @@ static int CmdFlashMemSpiFFSUpload(const char *Cmd) {
if (res == PM3_SUCCESS) {
PrintAndLogEx(SUCCESS, "Wrote "_GREEN_("%zu") " bytes to file "_GREEN_("%s"), datalen, dst);
PrintAndLogEx(HINT, "Hint: Try `" _YELLOW_("mem spiffs tree") "` to verify");
}
PrintAndLogEx(HINT, "Hint: Try `" _YELLOW_("mem spiffs tree") "` to verify");
return res;
}
+29 -5
View File
@@ -1275,6 +1275,11 @@ static bool dl_it(uint8_t *dest, uint32_t bytes, PacketResponseNG *response, siz
uint32_t bytes_completed = 0;
__atomic_store_n(&timeout_start_time, msclock(), __ATOMIC_SEQ_CST);
// inline feedback. Small transfers finish before a line could be read, so
// only the ones that actually take time say anything
const bool show_progress = (bytes > 4096);
uint64_t last_update = 0;
// Add delay depending on the communication channel & speed
if (ms_timeout != (size_t) - 1)
ms_timeout += communication_delay();
@@ -1283,17 +1288,22 @@ static bool dl_it(uint8_t *dest, uint32_t bytes, PacketResponseNG *response, siz
if (getReply(response)) {
// the terminator is answered on the request opcode itself
// the terminator is answered on the request opcode itself, and carries
// the status of the transfer, ie: PM3_EMALLOC / PM3_EFILE
if (response->cmd == req_cmd) {
if (response->status == PM3_EMALLOC) {
return false;
if (show_progress) {
PrintAndLogEx(NORMAL, "");
}
return true;
return (response->status == PM3_SUCCESS);
}
// 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) {
if (show_progress) {
PrintAndLogEx(NORMAL, "");
}
return true;
}
if (response->cmd == rec_cmd) {
@@ -1334,6 +1344,15 @@ static bool dl_it(uint8_t *dest, uint32_t bytes, PacketResponseNG *response, siz
memcpy(dest + offset, src, copy_bytes);
bytes_completed += copy_bytes;
if (show_progress && ((msclock() - last_update > 100) || (bytes_completed == bytes))) {
PrintAndLogEx(INPLACE, "Received " _YELLOW_("%u") " / " _YELLOW_("%u") " bytes (" _YELLOW_("%u") "%%)"
, bytes_completed
, bytes
, (bytes_completed * 100) / bytes
);
last_update = msclock();
}
} else if (response->cmd == CMD_WTX && response->length == sizeof(uint16_t)) {
uint16_t wtx = response->data.asDwords[0] & 0xFFFF;
PrintAndLogEx(DEBUG, "Got Waiting Time eXtension request %i ms", wtx);
@@ -1348,6 +1367,7 @@ static bool dl_it(uint8_t *dest, uint32_t bytes, PacketResponseNG *response, siz
break;
}
if (msclock() - tmp_clk > 3000 && show_warning) {
// 3 seconds elapsed (but this doesn't mean the timeout was exceeded)
PrintAndLogEx(INFO, "Waiting for a response from the Proxmark3...");
@@ -1355,5 +1375,9 @@ static bool dl_it(uint8_t *dest, uint32_t bytes, PacketResponseNG *response, siz
show_warning = false;
}
}
if (show_progress) {
PrintAndLogEx(NORMAL, "");
}
return false;
}