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;
}