From 9a8e17926661b8e0b27bc41685542d3de9ed7993 Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Sat, 12 Sep 2026 14:58:08 +0200 Subject: [PATCH] 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) --- CHANGELOG.md | 3 + armsrc/spiffs.c | 284 +++++++++++++++++++++++--- armsrc/spiffs.h | 8 + client/src/cmdflashmemspiffs.c | 42 +++- client/src/comms.c | 34 ++- common_arm/flash_data/flashmem_core.c | 24 ++- 6 files changed, 345 insertions(+), 50 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 24b6e0cb3..82966d484 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,9 @@ All notable changes to this project will be documented in this file. This project uses the changelog in accordance with [keepchangelog](http://keepachangelog.com/). Please use this to write notable changes, which is not the same as git commit log... ## [unreleased][unreleased] +- Changed `mem spiffs upload`/`mem spiffs dump` - both now print inline progress, and a refused write is reported with the byte it stopped at instead of claiming success (@iceman1001) +- Fixed `mem spiffs dump` - a file of 64K or more wrapped the uint16_t `BigBuf_calloc()` takes, the download now streams one frame at a time (@iceman1001) +- Fixed SPIFFS flash layer - a failed erase was reported to SPIFFS as success, letting it write into an un-erased sector where NOR AND-semantics silently mangled file data (@iceman1001) - Fixed `hf thinfilm sim` - a 32 sample field read between every frame cost 3.8ms and nearly doubled the frame repeat period (@iceman1001) - Added `hf thinfilm sim` - it now traces, so `trace list -t thinfilm` works on the sim side (@iceman1001) - Fixed `hf texkom reader -v` - now no ovewflow in the general decoder (@iceman1001) diff --git a/armsrc/spiffs.c b/armsrc/spiffs.c index 0b2799c5a..95e34ac36 100644 --- a/armsrc/spiffs.c +++ b/armsrc/spiffs.c @@ -56,12 +56,20 @@ #include "spiffs.h" #include "BigBuf.h" #include "dbprint.h" +#include "pm3_cmd.h" +#include "string.h" ///// FLASH LEVEL R/W/E operations for feeding SPIFFS Driver///////////////// +// +// SPIFFS_CHECK_RES() only treats a NEGATIVE result as an error, so the 128 / 129 / 130 +// these used to return meant every flash failure was reported to SPIFFS as success. +// A failed erase that reads back as success is the worst of them: SPIFFS then writes +// into a sector that still holds the old content, and since a NOR write can only clear +// bits, page headers and file data silently AND together instead of being replaced. static s32_t rdv40_spiffs_llread(u32_t addr, u32_t size, u8_t *dst) { if (!Flash_ReadData(addr, dst, size)) { - return 128; + return SPIFFS_ERR_NOT_READABLE; } return SPIFFS_OK; } @@ -69,19 +77,50 @@ static s32_t rdv40_spiffs_llread(u32_t addr, u32_t size, u8_t *dst) { static s32_t rdv40_spiffs_llwrite(u32_t addr, u32_t size, u8_t *src) { if (FlashInit() == false) { - return 129; + return SPIFFS_ERR_NOT_WRITABLE; + } + + if (Flash_Write(addr, src, size) != size) { + return SPIFFS_ERR_NOT_WRITABLE; } - Flash_Write(addr, src, size); return SPIFFS_OK; } +// A sector erase that did not take - the chip was still busy, or the write enable +// latch was gone by the time the command arrived - leaves the old content in place. +// Flash_Erase4k() only reports that the command was sent, so read the sector back. +static bool flash_sector_is_erased(uint32_t addr) { + + static uint8_t buf[64]; + + for (uint32_t offset = 0; offset < SPIFFS_CFG_LOG_BLOCK_SZ; offset += sizeof(buf)) { + + if (Flash_ReadDataCont(addr + offset, buf, sizeof(buf)) != sizeof(buf)) { + return false; + } + + for (uint32_t i = 0; i < sizeof(buf); i++) { + if (buf[i] != 0xFF) { + if (g_dbglevel >= DBG_DEBUG) { + Dbprintf("SPIFFS erase verify failed at 0x%05x, read %02x", addr + offset + i, buf[i]); + } + return false; + } + } + } + return true; +} + static s32_t rdv40_spiffs_llerase(u32_t addr, u32_t size) { + (void)size; // always one SPIFFS_CFG_LOG_BLOCK_SZ sector + if (FlashInit() == false) { - return 130; + return SPIFFS_ERR_ERASE_FAIL; } if (g_dbglevel >= DBG_DEBUG) Dbprintf("LLERASEDBG : Orig addr : %d\n", addr); + uint32_t sector_addr = addr; uint8_t block, sector = 0; block = addr / RDV40_LLERASE_BLOCKSIZE; if (block) { @@ -91,18 +130,40 @@ static s32_t rdv40_spiffs_llerase(u32_t addr, u32_t size) { if (g_dbglevel >= DBG_DEBUG) Dbprintf("LLERASEDBG : Result addr : %d\n", addr); sector = addr / SPIFFS_CFG_LOG_BLOCK_SZ; - Flash_CheckBusy(BUSY_TIMEOUT); - Flash_WriteEnable(); if (g_dbglevel >= DBG_DEBUG) Dbprintf("LLERASEDBG : block : %d, sector : %d \n", block, sector); - uint8_t erased = Flash_Erase4k(block, sector); - Flash_CheckBusy(BUSY_TIMEOUT); - FlashStop(); + for (uint8_t attempt = 0; attempt < 2; attempt++) { - // iceman: SPIFFS_OK expands to 0, erased is bool from Flash_Erase4k, which returns TRUE if ok. - // so this return logic looks wrong. - return (SPIFFS_OK == erased); + // true == still busy when the timeout ran out + if (Flash_CheckBusy(BUSY_TIMEOUT)) { + Dbprintf("SPIFFS erase: flash still busy before erasing sector 0x%05x", sector_addr); + continue; + } + + Flash_WriteEnable(); + + if (Flash_Erase4k(block, sector) == false) { + Dbprintf("SPIFFS erase: bad sector address, block %u sector %u", block, sector); + break; + } + + if (Flash_CheckBusy(BUSY_TIMEOUT)) { + Dbprintf("SPIFFS erase: sector 0x%05x did not finish in %u us", sector_addr, BUSY_TIMEOUT); + continue; + } + + if (flash_sector_is_erased(sector_addr)) { + FlashStop(); + return SPIFFS_OK; + } + + Dbprintf("SPIFFS erase: sector 0x%05x did not erase, retrying", sector_addr); + } + + FlashStop(); + Dbprintf(_RED_("SPIFFS erase of sector 0x%05x failed, filesystem not written"), sector_addr); + return SPIFFS_ERR_ERASE_FAIL; } //////////////////////////////////////////////////////////////////////////////// @@ -195,20 +256,44 @@ int rdv40_spiffs_check(void) { ///// Base RDV40_SPIFFS_SAFETY_NORMAL operations//////////////////////////////// +// RDV40_SPIFFS_SAFE_FUNCTION() returns the mount-change flag, not a result, so a +// failed write had no way of reaching the caller - the client happily reported +// "Wrote N bytes" for a file the device never stored. The last write result is +// kept here instead, see rdv40_spiffs_write_status() +static int g_spiffs_write_res = SPIFFS_OK; + +int rdv40_spiffs_write_status(void) { + return g_spiffs_write_res; +} + void write_to_spiffs(const char *filename, const uint8_t *src, uint32_t size) { + g_spiffs_write_res = SPIFFS_OK; spiffs_file fd = SPIFFS_open(&fs, filename, SPIFFS_CREAT | SPIFFS_TRUNC | SPIFFS_RDWR, 0); + if (fd < 0) { + g_spiffs_write_res = SPIFFS_errno(&fs); + Dbprintf("open errno %i\n", g_spiffs_write_res); + return; + } // Note: SPIFFS_write() doesn't declare third parameter as const (but should) if (SPIFFS_write(&fs, fd, (void *)src, size) < 0) { - Dbprintf("wr errno %i\n", SPIFFS_errno(&fs)); + g_spiffs_write_res = SPIFFS_errno(&fs); + Dbprintf("wr errno %i\n", g_spiffs_write_res); } SPIFFS_close(&fs, fd); } void append_to_spiffs(const char *filename, const uint8_t *src, uint32_t size) { + g_spiffs_write_res = SPIFFS_OK; spiffs_file fd = SPIFFS_open(&fs, filename, SPIFFS_APPEND | SPIFFS_RDWR, 0); + if (fd < 0) { + g_spiffs_write_res = SPIFFS_errno(&fs); + Dbprintf("open errno %i\n", g_spiffs_write_res); + return; + } // Note: SPIFFS_write() doesn't declare third parameter as const (but should) if (SPIFFS_write(&fs, fd, (void *)src, size) < 0) { - Dbprintf("errno %i\n", SPIFFS_errno(&fs)); + g_spiffs_write_res = SPIFFS_errno(&fs); + Dbprintf("errno %i\n", g_spiffs_write_res); } SPIFFS_close(&fs, fd); } @@ -268,7 +353,8 @@ int exists_in_spiffs(const char *filename) { static RDV40SpiFFSFileType filetype_in_spiffs(const char *filename) { RDV40SpiFFSFileType filetype = RDV40_SPIFFS_FILETYPE_UNKNOWN; - char symlinked[SPIFFS_OBJ_NAME_LEN]; + // SPIFFS_OBJ_NAME_LEN + 4 : a 31 char name plus ".lnk" and the terminator + char symlinked[SPIFFS_OBJ_NAME_LEN + 4]; sprintf(symlinked, "%s.lnk", filename); if (exists_in_spiffs(filename)) { @@ -301,20 +387,47 @@ static RDV40SpiFFSFileType filetype_in_spiffs(const char *filename) { } return filetype; } -/* -static int is_valid_filename(const char *filename) { - if (filename == NULL) { - return false; - } - uint32_t len = strlen(filename); - return len > 0 && len < SPIFFS_OBJ_NAME_LEN; -} -*/ + static void copy_in_spiffs(const char *src, const char *dst) { - uint32_t size = size_in_spiffs(src); - uint8_t *mem = BigBuf_calloc(size); - read_from_spiffs(src, (uint8_t *)mem, size); - write_to_spiffs(dst, (uint8_t *)mem, size); + + // no BigBuf_calloc(filesize) here: a file can be bigger than BigBuf, and bigger + // than the uint16_t BigBuf_calloc() takes. Copy it in SPIFFS_WRITE_CHUNK_SIZE + // chunks instead, which is also what rdv40_spiffs_write() writes in + uint8_t *mem = BigBuf_calloc(SPIFFS_WRITE_CHUNK_SIZE); + if (mem == NULL) { + Dbprintf("error, cannot allocate copy buffer"); + return; + } + + spiffs_file fd = SPIFFS_open(&fs, src, SPIFFS_RDONLY, 0); + if (fd < 0) { + Dbprintf("errno %i\n", SPIFFS_errno(&fs)); + return; + } + + bool first = true; + while (true) { + + s32_t len = SPIFFS_read(&fs, fd, mem, SPIFFS_WRITE_CHUNK_SIZE); + if (len < 0) { + Dbprintf("errno %i\n", SPIFFS_errno(&fs)); + break; + } + + if (len == 0) { + break; + } + + // the first chunk creates / truncates the destination, the rest append to it + if (first) { + write_to_spiffs(dst, mem, (uint32_t)len); + first = false; + } else { + append_to_spiffs(dst, mem, (uint32_t)len); + } + } + + SPIFFS_close(&fs, fd); } //////////////////////////////////////////////////////////////////////////////// @@ -525,7 +638,7 @@ int rdv40_spiffs_is_symlink(const char *s) { return ret; } -// since FILENAME can't be longer than 32Bytes as of hard configuration, we're +// since FILENAME can't be longer than 32 Bytes as of hard configuration, we're // safe with Such maximum. So the "size" variable is actually the known/intended // size of DESTINATION file, may it be known (may we provide a "stat from // symlink ?") @@ -535,7 +648,7 @@ int rdv40_spiffs_read_as_symlink(const char *filename, uint8_t *dst, uint32_t si RDV40_SPIFFS_SAFE_FUNCTION( char linkdest[SPIFFS_OBJ_NAME_LEN]; - char linkfilename[SPIFFS_OBJ_NAME_LEN]; + char linkfilename[SPIFFS_OBJ_NAME_LEN + 4]; sprintf(linkfilename, "%s.lnk", filename); if (g_dbglevel >= DBG_DEBUG) @@ -564,7 +677,7 @@ int rdv40_spiffs_read_as_symlink(const char *filename, uint8_t *dst, uint32_t si // TODO : FORBID creating a symlink with a basename (before.lnk) which already exists as a file ! int rdv40_spiffs_make_symlink(const char *linkdest, const char *filename, RDV40SpiFFSSafetyLevel level) { RDV40_SPIFFS_SAFE_FUNCTION( - char linkfilename[SPIFFS_OBJ_NAME_LEN]; + char linkfilename[SPIFFS_OBJ_NAME_LEN + 4]; sprintf(linkfilename, "%s.lnk", filename); write_to_spiffs(linkfilename, (const uint8_t *)linkdest, SPIFFS_OBJ_NAME_LEN); ) @@ -600,6 +713,115 @@ int rdv40_spiffs_read_as_filetype(const char *filename, uint8_t *dst, uint32_t s ) } +// Resolve a filename to the real file it names, following a .lnk symlink the same +// way rdv40_spiffs_read_as_filetype() does. Expects the filesystem to be mounted. +static int resolve_in_spiffs(const char *filename, char *dst, uint16_t dstlen) { + + switch (filetype_in_spiffs(filename)) { + case RDV40_SPIFFS_FILETYPE_REAL: { + strncpy(dst, filename, dstlen - 1); + dst[dstlen - 1] = 0; + return PM3_SUCCESS; + } + case RDV40_SPIFFS_FILETYPE_SYMLINK: { + char linkfilename[SPIFFS_OBJ_NAME_LEN + 4]; + sprintf(linkfilename, "%s.lnk", filename); + read_from_spiffs(linkfilename, (uint8_t *)dst, MIN(dstlen, (uint16_t)SPIFFS_OBJ_NAME_LEN)); + dst[dstlen - 1] = 0; + return PM3_SUCCESS; + } + case RDV40_SPIFFS_FILETYPE_BOTH: + case RDV40_SPIFFS_FILETYPE_UNKNOWN: + default: { + break; + } + } + return PM3_EFILE; +} + +// Read a file out in chunks, handing each one to `cb` as it arrives. +// Expects the filesystem to be mounted. Returns bytes streamed, or a negative PM3_E* +static int stream_from_spiffs(const char *filename, uint32_t offset, uint32_t size, uint8_t *chunkbuf, uint16_t chunklen, spiffs_chunk_cb_t cb) { + + char target[SPIFFS_OBJ_NAME_LEN] = {0}; + int res = resolve_in_spiffs(filename, target, sizeof(target)); + if (res != PM3_SUCCESS) { + return res; + } + + spiffs_file fd = SPIFFS_open(&fs, target, SPIFFS_RDONLY, 0); + if (fd < 0) { + Dbprintf("errno %i\n", SPIFFS_errno(&fs)); + return PM3_EFILE; + } + + if (offset && (SPIFFS_lseek(&fs, fd, offset, SPIFFS_SEEK_SET) < 0)) { + Dbprintf("errno %i\n", SPIFFS_errno(&fs)); + SPIFFS_close(&fs, fd); + return PM3_EFILE; + } + + res = PM3_SUCCESS; + uint32_t sent = 0; + + while (sent < size) { + + // SPIFFS_read clamps to the end of the file and returns 0 once there + s32_t len = SPIFFS_read(&fs, fd, chunkbuf, MIN(size - sent, (uint32_t)chunklen)); + if (len < 0) { + Dbprintf("errno %i\n", SPIFFS_errno(&fs)); + res = PM3_EFILE; + break; + } + + if (len == 0) { + break; + } + + // offsets are relative to the start of the transfer, not to the file + res = cb(sent, chunkbuf, (uint16_t)len); + if (res != PM3_SUCCESS) { + break; + } + + sent += (uint32_t)len; + } + + SPIFFS_close(&fs, fd); + return (res == PM3_SUCCESS) ? (int)sent : res; +} + +// Stream `size` bytes from `offset` of a file, one `chunklen` chunk at a time. +// +// The download path used to read the whole file into BigBuf first. That caps a +// download at what BigBuf holds and, since BigBuf_calloc() takes a uint16_t, the +// request wrapped for files >= 64KB: a short buffer, then a full size read run +// straight past the end of it. Here the file is opened once and only one chunk +// is ever buffered, so file size no longer enters into it. +// +// Returns the number of bytes streamed, or a negative PM3_E* on failure. +// +// Not RDV40_SPIFFS_SAFE_FUNCTION() : that macro returns the mount-change flag, +// and the caller needs to know how much of the file actually went out. +int rdv40_spiffs_read_stream(const char *filename, uint32_t offset, uint32_t size, uint8_t *chunkbuf, uint16_t chunklen, spiffs_chunk_cb_t cb, RDV40SpiFFSSafetyLevel level) { + + if ((filename == NULL) || (chunkbuf == NULL) || (chunklen == 0) || (cb == NULL)) { + return PM3_EINVARG; + } + + int changed = 0; + if ((level == RDV40_SPIFFS_SAFETY_LAZY) || (level == RDV40_SPIFFS_SAFETY_SAFE)) { + changed = rdv40_spiffs_lazy_mount(); + } + + int res = stream_from_spiffs(filename, offset, size, chunkbuf, chunklen, cb); + + if (level == RDV40_SPIFFS_SAFETY_SAFE) { + rdv40_spiffs_lazy_mount_rollback(changed); + } + return res; +} + // TODO regarding reads/write and symlinks : // Provide a higher level readFile function which // - don't need a size to be provided, getting it from STAT call and using bigbuff malloc diff --git a/armsrc/spiffs.h b/armsrc/spiffs.h index e0b5868da..9a5a1bee1 100644 --- a/armsrc/spiffs.h +++ b/armsrc/spiffs.h @@ -48,6 +48,11 @@ typedef struct rdv40_spiffs_fsinfo { int rdv40_spiffs_read_as_filetype(const char *filename, uint8_t *dst, uint32_t size, RDV40SpiFFSSafetyLevel level); +// one call per chunk read by rdv40_spiffs_read_stream(), `offset` is relative to +// the start of the transfer. Return PM3_SUCCESS to carry on, anything else stops +typedef int (*spiffs_chunk_cb_t)(uint32_t offset, const uint8_t *data, uint16_t len); +int rdv40_spiffs_read_stream(const char *filename, uint32_t offset, uint32_t size, uint8_t *chunkbuf, uint16_t chunklen, spiffs_chunk_cb_t cb, RDV40SpiFFSSafetyLevel level); + int rdv40_spiffs_check(void); int rdv40_spiffs_lazy_unmount(void); int rdv40_spiffs_lazy_mount(void); @@ -58,6 +63,9 @@ int rdv40_spiffs_rename(const char *old_filename, const char *new_filename, RDV4 int rdv40_spiffs_remove(const char *filename, RDV40SpiFFSSafetyLevel level); int rdv40_spiffs_read_as_symlink(const char *filename, uint8_t *dst, uint32_t size, RDV40SpiFFSSafetyLevel level); void write_to_spiffs(const char *filename, const uint8_t *src, uint32_t size); + +// SPIFFS_OK, or the SPIFFS errno of the last write_to_spiffs()/append_to_spiffs() +int rdv40_spiffs_write_status(void); void read_from_spiffs(const char *filename, uint8_t *dst, uint32_t size); void test_spiffs(void); void rdv40_spiffs_safe_print_tree(void); diff --git a/client/src/cmdflashmemspiffs.c b/client/src/cmdflashmemspiffs.c index 2176913aa..f51e3abf1 100644 --- a/client/src/cmdflashmemspiffs.c +++ b/client/src/cmdflashmemspiffs.c @@ -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; } diff --git a/client/src/comms.c b/client/src/comms.c index 3d602cb63..59755cff0 100644 --- a/client/src/comms.c +++ b/client/src/comms.c @@ -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; } diff --git a/common_arm/flash_data/flashmem_core.c b/common_arm/flash_data/flashmem_core.c index beb1e90e6..b563925cb 100644 --- a/common_arm/flash_data/flashmem_core.c +++ b/common_arm/flash_data/flashmem_core.c @@ -106,12 +106,15 @@ uint16_t Flash_WriteData(uint32_t address, uint8_t *in, uint16_t len) { // assumes valid start 256 based 00 address // Start writing flash from the specified location. -// Write FLASH_MEM_BLOCK_SIZE bytes at most each time. If the writing is nearly complete, write it as bytes_remaining bytes. +// Write FLASH_MEM_BLOCK_SIZE bytes at most each time. +// If the writing is nearly complete, write it as bytes_remaining bytes. uint16_t Flash_Write(uint32_t address, uint8_t *in, uint16_t len) { - bool isok; - uint16_t res, bytes_sent = 0, bytes_remaining = len; - uint8_t buf[FLASH_MEM_BLOCK_SIZE]; + uint16_t res; + uint16_t bytes_sent = 0; + uint16_t bytes_remaining = len; + uint8_t buf[FLASH_MEM_BLOCK_SIZE] = {0}; + while (bytes_remaining > 0) { Flash_CheckBusy(BUSY_TIMEOUT); @@ -123,18 +126,19 @@ uint16_t Flash_Write(uint32_t address, uint8_t *in, uint16_t len) { res = Flash_WriteDataCont(address + bytes_sent, buf, bytes_in_packet); + // a rejected page stops here, so bytes_sent stays at what really landed + if (res != bytes_in_packet) { + goto out; + } + bytes_remaining -= bytes_in_packet; bytes_sent += bytes_in_packet; - - isok = (res == bytes_in_packet); - - if (!isok) - goto out; } out: FlashStop(); - return len; + // bytes actually programmed, callers compare this against len + return bytes_sent; } void Flashmem_print_status(void) {