You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by ja...@apache.org on 2026/10/27 09:33:33 UTC

(mynewt-core) 07/08: hw/usb/tinyusb/msc_flash: Don't write erased value to flash

This is an automated email from the ASF dual-hosted git repository.

sjanc pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-core.git

commit 2b6c89ad313bc53c7eddb72289efd2c9209f1aa9
Author: Szymon Janc <sz...@codecoup.pl>
AuthorDate: Mon Oct 26 17:13:06 2026 +0100

    hw/usb/tinyusb/msc_flash: Don't write erased value to flash
    
    Some flash types, e.g. with ECC, do not allow writing again to
    location that was already written, even if it was written with erased
    value. Writing blocks containing erased value prevented later writes
    to those locations without erasing whole sector.
    
    Skip parts of written data that contain only erased value. This is
    also needed to write back sector content after erase, when only part
    of the sector is modified.
    
    Assisted-by: Claude:claude-opus-5-5
---
 hw/usb/tinyusb/msc_flash/src/msc_flash.c | 45 ++++++++++++++++++++++++++++++--
 1 file changed, 43 insertions(+), 2 deletions(-)

diff --git a/hw/usb/tinyusb/msc_flash/src/msc_flash.c b/hw/usb/tinyusb/msc_flash/src/msc_flash.c
index a4bf312c0..2726d399c 100644
--- a/hw/usb/tinyusb/msc_flash/src/msc_flash.c
+++ b/hw/usb/tinyusb/msc_flash/src/msc_flash.c
@@ -93,12 +93,53 @@ flash_sector_find(uint8_t flash_id, uint32_t addr, uint32_t *sector_start,
     return -1;
 }
 
+static bool
+buf_is_erased(const uint8_t *buf, uint32_t len, uint8_t erased_val)
+{
+    while (len--) {
+        if (*buf++ != erased_val) {
+            return false;
+        }
+    }
+
+    return true;
+}
+
+/*
+ * Writes data skipping parts that contain only erased value. Some flash types
+ * do not allow writing again to location that was written with erased value.
+ */
+static int
+flash_write_non_erased(struct lun_data *lun_data, uint32_t addr,
+                       const uint8_t *buffer, uint32_t len)
+{
+    uint8_t erased_val = hal_flash_erased_val(lun_data->flash_id);
+    uint32_t chunk;
+    uint32_t off;
+    uint32_t n;
+    int rc;
+
+    chunk = max(32, hal_flash_align(lun_data->flash_id));
+    for (off = 0; off < len; off += n) {
+        n = min(chunk, len - off);
+        if (buf_is_erased(buffer + off, n, erased_val)) {
+            continue;
+        }
+        rc = hal_flash_write(lun_data->flash_id, addr + off, buffer + off, n);
+        if (rc) {
+            return rc;
+        }
+    }
+
+    return 0;
+}
+
 static int
 flash_write(struct lun_data *lun_data, uint32_t addr, const uint8_t *buffer,
             uint32_t bufsize)
 {
-    return hal_flash_write(lun_data->flash_id, lun_data->flash_start + addr,
-                           buffer, bufsize);
+    return flash_write_non_erased(lun_data, lun_data->flash_start + addr,
+                                  buffer, bufsize);
 }
 
 static int