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:26 UTC

(mynewt-core) branch master updated (f3f59649b -> c82d3ba3e)

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

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


    from f3f59649b net/oic: Drop partially received packet when TCP4 connection closes
     new d5746e99b hw/usb/tinyusb/msc_flash: Check LBA range in READ10 and WRITE10
     new 807b3c6a0 hw/usb/tinyusb/msc_flash: Handle flash access errors
     new 53c8224fd hw/usb/tinyusb/msc_flash: Fix handling of invalid LUN
     new f5175a7b6 hw/usb/tinyusb/msc_flash: Fix MBR read with partial block transfers
     new 4334c30ef hw/usb/tinyusb/msc_flash: Use flash device for flash area variant
     new 49a4e49ab hw/usb/tinyusb/msc_flash: Refuse flash not aligned to sectors
     new 2b6c89ad3 hw/usb/tinyusb/msc_flash: Don't write erased value to flash
     new c82d3ba3e hw/usb/tinyusb/msc_flash: Preserve flash content on block write

The 8 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 hw/usb/tinyusb/msc_flash/src/msc_flash.c | 356 +++++++++++++++++++++++--------
 hw/usb/tinyusb/msc_flash/syscfg.yml      |   6 +
 2 files changed, 269 insertions(+), 93 deletions(-)


(mynewt-core) 04/08: hw/usb/tinyusb/msc_flash: Fix MBR read with partial block transfers

Posted by ja...@apache.org.
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 f5175a7b69c6637f2ebc61f4340976cacd109a66
Author: Szymon Janc <sz...@codecoup.pl>
AuthorDate: Sun Oct 25 22:37:21 2026 +0100

    hw/usb/tinyusb/msc_flash: Fix MBR read with partial block transfers
    
    MBR block was always built as full 512 bytes block regardless of
    transfer size and offset, writing past the buffer if it was smaller
    than 512 bytes, and returning MBR content also for data past the
    first block.
    
    Fill only the requested part of MBR block and return at most the
    remaining part of it. Also reject offsets that are not within block,
    which are possible as TinyUSB computes block size from values
    provided by the host.
    
    Assisted-by: Claude:claude-opus-5-5
---
 hw/usb/tinyusb/msc_flash/src/msc_flash.c | 43 +++++++++++++++++++++++++++-----
 1 file changed, 37 insertions(+), 6 deletions(-)

diff --git a/hw/usb/tinyusb/msc_flash/src/msc_flash.c b/hw/usb/tinyusb/msc_flash/src/msc_flash.c
index 5e2f9969f..c4f772656 100644
--- a/hw/usb/tinyusb/msc_flash/src/msc_flash.c
+++ b/hw/usb/tinyusb/msc_flash/src/msc_flash.c
@@ -132,6 +132,37 @@ get_lun_data(uint8_t lun)
     return IN_ARRAY(luns, lun) ? luns + lun : NULL;
 }
 
+/*
+ * Fills buffer with part of MBR starting at offset. Returns number of bytes
+ * filled, MBR occupies single block.
+ */
+static uint32_t
+mbr_read(struct lun_data *lun_data, uint32_t offset, uint8_t *buf, uint32_t len)
+{
+    static const struct {
+        uint16_t off;
+        uint8_t val;
+    } mbr[] = {
+        /* Partition type */
+        { 450, 0x81 },
+        /* Boot signature */
+        { 510, 0x55 },
+        { 511, 0xAA },
+    };
+    int i;
+
+    len = min(len, lun_data->block_size - offset);
+    memset(buf, 0, len);
+
+    for (i = 0; i < ARRAY_SIZE(mbr); i++) {
+        if (mbr[i].off >= offset && mbr[i].off < offset + len) {
+            buf[mbr[i].off - offset] = mbr[i].val;
+        }
+    }
+
+    return len;
+}
+
 /*
  * Converts LBA and offset to flash address. Returns false if access of size
  * bytes is not fully contained within flash exported by LUN.
@@ -259,14 +290,14 @@ tud_msc_read10_cb(uint8_t lun, uint32_t lba, uint32_t offset, void *buffer,
         return -1;
     }
 
+    /* Offset within block is derived from block size chosen by host */
+    if (offset >= lun_data->block_size) {
+        return -1;
+    }
+
     /* If MBR was selected add marker and partition type */
     if (MYNEWT_VAL_MSC_FLASH_MBR == 1 && lba == 0) {
-        uint8_t *buf = buffer;
-
-        memset(buffer, 0, bufsize);
-        buf[450] = 0x81;
-        buf[510] = 0x55;
-        buf[511] = 0xAA;
+        bufsize = mbr_read(lun_data, offset, buffer, bufsize);
     } else {
         if (!lba_to_flash_addr(lun_data, lba, offset, bufsize, &flash_addr)) {
             return -1;


(mynewt-core) 03/08: hw/usb/tinyusb/msc_flash: Fix handling of invalid LUN

Posted by ja...@apache.org.
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 53c8224fd92e4b758c44c9e2d7da08714e62d993
Author: Szymon Janc <sz...@codecoup.pl>
AuthorDate: Sun Oct 25 22:37:21 2026 +0100

    hw/usb/tinyusb/msc_flash: Fix handling of invalid LUN
    
    LUN number comes from USB host and is not validated by TinyUSB.
    START STOP UNIT command for non-existent LUN wrote medium state through
    NULL pointer. READ10 for non-existent LUN, or after medium was ejected,
    returned 0 which TinyUSB treats as busy, so the callback was retried
    forever.
    
    Fail START STOP UNIT for non-existent LUN and return error from READ10.
    
    Assisted-by: Claude:claude-opus-5-5
---
 hw/usb/tinyusb/msc_flash/src/msc_flash.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/hw/usb/tinyusb/msc_flash/src/msc_flash.c b/hw/usb/tinyusb/msc_flash/src/msc_flash.c
index 2b6df4f2d..5e2f9969f 100644
--- a/hw/usb/tinyusb/msc_flash/src/msc_flash.c
+++ b/hw/usb/tinyusb/msc_flash/src/msc_flash.c
@@ -227,6 +227,10 @@ tud_msc_start_stop_cb(uint8_t lun, uint8_t power_condition, bool start, bool loa
 
     MSC_FLASH_LOG_DEBUG("Start: %d eject: %d\n", start, load_eject);
 
+    if (lun_data == NULL) {
+        return false;
+    }
+
     if (load_eject) {
         if (start) {
         } else {
@@ -252,7 +256,7 @@ tud_msc_read10_cb(uint8_t lun, uint32_t lba, uint32_t offset, void *buffer,
 
     /* Check if disk is even present */
     if (lun_data == NULL || lun_data->medium_state != MEDIUM_PRESENT) {
-        return 0;
+        return -1;
     }
 
     /* If MBR was selected add marker and partition type */


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

Posted by ja...@apache.org.
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


(mynewt-core) 05/08: hw/usb/tinyusb/msc_flash: Use flash device for flash area variant

Posted by ja...@apache.org.
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 4334c30ef6fabd58ceeec5a0e607c83326632541
Author: Szymon Janc <sz...@codecoup.pl>
AuthorDate: Sun Oct 25 22:51:47 2026 +0100

    hw/usb/tinyusb/msc_flash: Use flash device for flash area variant
    
    With MSC_FLASH_FLASH_AREA_ID flash was accessed through flash area API
    with flash area id stored in place of flash id. Resolve flash device
    id and offset of flash area during initialization and access flash
    using the same HAL flash calls as with MSC_FLASH_FLASH_ID. Access
    range is already validated against the size of exported flash.
    
    This is needed for handling of flash sectors which are properties of
    flash device. No functional changes.
    
    Assisted-by: Claude:claude-opus-5-5
---
 hw/usb/tinyusb/msc_flash/src/msc_flash.c | 106 ++++++++++---------------------
 1 file changed, 33 insertions(+), 73 deletions(-)

diff --git a/hw/usb/tinyusb/msc_flash/src/msc_flash.c b/hw/usb/tinyusb/msc_flash/src/msc_flash.c
index c4f772656..7393d8e46 100644
--- a/hw/usb/tinyusb/msc_flash/src/msc_flash.c
+++ b/hw/usb/tinyusb/msc_flash/src/msc_flash.c
@@ -39,9 +39,10 @@ struct lun_data {
     medium_state_t medium_state;
     uint32_t block_count;
     uint16_t block_size;
-    /* Flash Id or Flash area id */
+    /* Flash Id */
     uint8_t flash_id;
 
+    /* Address of exported part of flash */
     uint32_t flash_start;
     uint32_t flash_end;
 
@@ -60,38 +61,16 @@ struct lun_data luns[1] = {
 static int
 flash_read(struct lun_data *lun_data, uint32_t addr, uint8_t *buffer, uint32_t bufsize)
 {
-    int rc = -1;
-#if defined(MYNEWT_VAL_MSC_FLASH_FLASH_ID)
-    rc = hal_flash_read(lun_data->flash_id, lun_data->flash_start + addr,
-                        buffer, bufsize);
-#elif defined(MYNEWT_VAL_MSC_FLASH_FLASH_AREA_ID)
-    const struct flash_area *fa;
-
-    if (flash_area_open(MYNEWT_VAL_MSC_FLASH_FLASH_AREA_ID, &fa) == 0) {
-        rc = flash_area_read(fa, addr, buffer, bufsize);
-        flash_area_close(fa);
-    }
-#endif
-    return rc;
+    return hal_flash_read(lun_data->flash_id, lun_data->flash_start + addr,
+                          buffer, bufsize);
 }
 
 static int
 flash_write(struct lun_data *lun_data, uint32_t addr, const uint8_t *buffer,
             uint32_t bufsize)
 {
-    int rc = -1;
-#if defined(MYNEWT_VAL_MSC_FLASH_FLASH_ID)
-    rc = hal_flash_write(lun_data->flash_id, lun_data->flash_start + addr,
-                         buffer, bufsize);
-#elif defined(MYNEWT_VAL_MSC_FLASH_FLASH_AREA_ID)
-    const struct flash_area *fa;
-
-    if (flash_area_open(MYNEWT_VAL_MSC_FLASH_FLASH_AREA_ID, &fa) == 0) {
-        rc = flash_area_write(fa, addr, buffer, bufsize);
-        flash_area_close(fa);
-    }
-#endif
-    return rc;
+    return hal_flash_write(lun_data->flash_id, lun_data->flash_start + addr,
+                           buffer, bufsize);
 }
 
 static int
@@ -101,24 +80,11 @@ flash_erase_if_needed(struct lun_data *lun_data, uint32_t addr, uint32_t size)
     int rc = 0;
 
     while (left > 0 && rc == 0) {
-#if defined(MYNEWT_VAL_MSC_FLASH_FLASH_ID)
         if (!hal_flash_isempty(lun_data->flash_id, lun_data->flash_start + addr,
                                lun_data->flash_sector_buffer, 512)) {
             rc = hal_flash_erase(lun_data->flash_id,
                                  lun_data->flash_start + addr, 512);
         }
-#elif defined(MYNEWT_VAL_MSC_FLASH_FLASH_AREA_ID)
-        const struct flash_area *fa;
-
-        rc = flash_area_open(MYNEWT_VAL_MSC_FLASH_FLASH_AREA_ID, &fa);
-        if (rc == 0) {
-            if (!flash_area_read_is_empty(fa, addr,
-                                          lun_data->flash_sector_buffer, 512)) {
-                rc = flash_area_erase(fa, addr, 512);
-            }
-            flash_area_close(fa);
-        }
-#endif
         left -= 512;
     }
 
@@ -400,45 +366,39 @@ boot_preboot(void)
 void
 msc_flash_init(void)
 {
+    uint32_t start_address;
+    uint32_t end_address;
+    uint8_t flash_id;
 #if defined(MYNEWT_VAL_MSC_FLASH_FLASH_ID)
     /* Flash ID as defined in bsp.yml */
-    int flash_id = MYNEWT_VAL_MSC_FLASH_FLASH_ID;
-    uint32_t start_address;
+    const struct hal_flash *hf;
     uint32_t sector_size;
-    uint32_t end_address;
-    uint32_t flash_size;
-    const struct hal_flash *hf = hal_bsp_flash_dev(flash_id);
-
-    if (hf) {
-        hal_flash_sector_info(flash_id, 0, &start_address, &sector_size);
-        start_address += MYNEWT_VAL_MSC_FLASH_FLASH_OFFSET;
-        hal_flash_sector_info(flash_id, hf->hf_sector_cnt - 1, &end_address,
-                              &sector_size);
-        end_address += sector_size;
-        flash_size = end_address - start_address;
-        luns[0].flash_id = MYNEWT_VAL_MSC_FLASH_FLASH_ID;
-        luns[0].block_count = flash_size / 512 + MYNEWT_VAL_MSC_FLASH_MBR;
-        luns[0].block_size = 512;
-        luns[0].flash_start = start_address;
-        luns[0].flash_end = end_address;
+
+    flash_id = MYNEWT_VAL_MSC_FLASH_FLASH_ID;
+    hf = hal_bsp_flash_dev(flash_id);
+    if (!hf) {
+        return;
     }
+    hal_flash_sector_info(flash_id, 0, &start_address, &sector_size);
+    start_address += MYNEWT_VAL_MSC_FLASH_FLASH_OFFSET;
+    hal_flash_sector_info(flash_id, hf->hf_sector_cnt - 1, &end_address, &sector_size);
+    end_address += sector_size;
 #elif defined(MYNEWT_VAL_MSC_FLASH_FLASH_AREA_ID)
-    int flash_area_id = MYNEWT_VAL_MSC_FLASH_FLASH_AREA_ID;
-    /* Flash ID as defined in bsp.yml */
     const struct flash_area *fa;
-    uint32_t start_address;
-    uint32_t end_address;
-    uint32_t flash_size;
-
-    if (flash_area_open(flash_area_id, &fa) == 0) {
-        start_address = fa->fa_off;
-        end_address = start_address + fa->fa_size;
-        flash_size = end_address - start_address;
-        luns[0].flash_id = MYNEWT_VAL_MSC_FLASH_FLASH_AREA_ID;
-        luns[0].block_count = flash_size / 512 + MYNEWT_VAL_MSC_FLASH_MBR;
-        luns[0].block_size = 512;
-        luns[0].flash_start = start_address;
-        luns[0].flash_end = end_address;
+
+    if (flash_area_open(MYNEWT_VAL_MSC_FLASH_FLASH_AREA_ID, &fa)) {
+        return;
     }
+    flash_id = fa->fa_device_id;
+    start_address = fa->fa_off;
+    end_address = start_address + fa->fa_size;
+    flash_area_close(fa);
 #endif
+
+    luns[0].flash_id = flash_id;
+    luns[0].block_count =
+        (end_address - start_address) / 512 + MYNEWT_VAL_MSC_FLASH_MBR;
+    luns[0].block_size = 512;
+    luns[0].flash_start = start_address;
+    luns[0].flash_end = end_address;
 }


(mynewt-core) 08/08: hw/usb/tinyusb/msc_flash: Preserve flash content on block write

Posted by ja...@apache.org.
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 c82d3ba3e2a56f88e44cf7a128ec878b539fe88d
Author: Szymon Janc <sz...@codecoup.pl>
AuthorDate: Mon Oct 26 17:13:17 2026 +0100

    hw/usb/tinyusb/msc_flash: Preserve flash content on block write
    
    Before writing a 512 bytes block that was not erased, the whole flash
    sector containing it was erased. With sectors larger than block, e.g.
    4 KB on nRF52, this destroyed content of other blocks in the sector,
    corrupting file system on every overwrite.
    
    When written block is not erased, read the sector into buffer, erase
    it and write it back with new block content. Size of the buffer is set
    with MSC_FLASH_SECTOR_BUF_SIZE, writes to flash with larger sectors
    fail unless written block is erased. The buffer is moved out of
    lun_data, which is initialized data, so it is not stored in flash
    image, and it is shared by all LUNs as writes are serialized.
    
    Assisted-by: Claude:claude-opus-5-5
---
 hw/usb/tinyusb/msc_flash/src/msc_flash.c | 87 ++++++++++++++++++++++++--------
 hw/usb/tinyusb/msc_flash/syscfg.yml      |  6 +++
 2 files changed, 73 insertions(+), 20 deletions(-)

diff --git a/hw/usb/tinyusb/msc_flash/src/msc_flash.c b/hw/usb/tinyusb/msc_flash/src/msc_flash.c
index 2726d399c..e67554885 100644
--- a/hw/usb/tinyusb/msc_flash/src/msc_flash.c
+++ b/hw/usb/tinyusb/msc_flash/src/msc_flash.c
@@ -45,11 +45,11 @@ struct lun_data {
     /* Address of exported part of flash */
     uint32_t flash_start;
     uint32_t flash_end;
-
-    /* buffer for checking if flash is empty */
-    uint8_t flash_sector_buffer[512];
 };
 
+/* Buffer for read-modify-write of flash sector */
+static uint8_t flash_sector_buffer[MYNEWT_VAL(MSC_FLASH_SECTOR_BUF_SIZE)];
+
 struct lun_data luns[1] = {
     {
         .medium_state = MEDIUM_PRESENT,
@@ -134,31 +134,79 @@ flash_write_non_erased(struct lun_data *lun_data, uint32_t addr,
     return 0;
 }
 
+/*
+ * Writes data that does not cross sector boundary. If flash is not erased,
+ * sector content is preserved by reading it to buffer, erasing sector and
+ * writing it back with new data.
+ */
 static int
-flash_write(struct lun_data *lun_data, uint32_t addr, const uint8_t *buffer,
-            uint32_t bufsize)
+flash_write_in_sector(struct lun_data *lun_data, uint32_t sector_start,
+                      uint32_t sector_size, uint32_t addr,
+                      const uint8_t *buffer, uint32_t len)
 {
-    return flash_write_non_erased(lun_data, lun_data->flash_start + addr,
-                                  buffer, bufsize);
+    int rc;
+
+    rc = hal_flash_isempty(lun_data->flash_id, addr, flash_sector_buffer, len);
+    if (rc == 1) {
+        return flash_write_non_erased(lun_data, addr, buffer, len);
+    } else if (rc < 0) {
+        return rc;
+    }
+
+    if (sector_size > sizeof(flash_sector_buffer)) {
+        MSC_FLASH_LOG_ERROR("Sector size %u exceeds buffer size\n",
+                            (unsigned)sector_size);
+        return -1;
+    }
+
+    rc = hal_flash_read(lun_data->flash_id, sector_start, flash_sector_buffer,
+                        sector_size);
+    if (rc) {
+        return rc;
+    }
+    memcpy(flash_sector_buffer + (addr - sector_start), buffer, len);
+
+    rc = hal_flash_erase_sector(lun_data->flash_id, sector_start);
+    if (rc) {
+        return rc;
+    }
+
+    return flash_write_non_erased(lun_data, sector_start, flash_sector_buffer,
+                                  sector_size);
 }
 
 static int
-flash_erase_if_needed(struct lun_data *lun_data, uint32_t addr, uint32_t size)
+flash_write(struct lun_data *lun_data, uint32_t addr, const uint8_t *buffer,
+            uint32_t bufsize)
 {
-    int left = size;
-    int rc = 0;
-
-    while (left > 0 && rc == 0) {
-        if (!hal_flash_isempty(lun_data->flash_id, lun_data->flash_start + addr,
-                               lun_data->flash_sector_buffer, 512)) {
-            rc = hal_flash_erase(lun_data->flash_id,
-                                 lun_data->flash_start + addr, 512);
+    uint32_t sector_start;
+    uint32_t sector_size;
+    uint32_t n;
+    int rc;
+
+    addr += lun_data->flash_start;
+
+    while (bufsize > 0) {
+        rc = flash_sector_find(lun_data->flash_id, addr, &sector_start, &sector_size);
+        if (rc) {
+            return rc;
+        }
+
+        n = min(bufsize, sector_start + sector_size - addr);
+        rc = flash_write_in_sector(lun_data, sector_start, sector_size, addr,
+                                   buffer, n);
+        if (rc) {
+            return rc;
         }
-        left -= 512;
+
+        addr += n;
+        buffer += n;
+        bufsize -= n;
     }
 
-    return rc;
+    return 0;
 }
+
 #define IN_ARRAY(arr, ix) ((ix) < ARRAY_SIZE(arr))
 
 static struct lun_data *
@@ -368,8 +416,7 @@ tud_msc_write10_cb(uint8_t lun, uint32_t lba, uint32_t offset, uint8_t *buffer,
         if (!lba_to_flash_addr(lun_data, lba, offset, bufsize, &flash_addr)) {
             return -1;
         }
-        if (flash_erase_if_needed(lun_data, flash_addr, bufsize) != 0 ||
-            flash_write(lun_data, flash_addr, buffer, bufsize) != 0) {
+        if (flash_write(lun_data, flash_addr, buffer, bufsize) != 0) {
             return -1;
         }
     }
diff --git a/hw/usb/tinyusb/msc_flash/syscfg.yml b/hw/usb/tinyusb/msc_flash/syscfg.yml
index 87cb6000d..687829c47 100644
--- a/hw/usb/tinyusb/msc_flash/syscfg.yml
+++ b/hw/usb/tinyusb/msc_flash/syscfg.yml
@@ -27,6 +27,12 @@ syscfg.defs:
     MSC_FLASH_FLASH_AREA_ID:
         description: 'Target flash area'
         value:
+    MSC_FLASH_SECTOR_BUF_SIZE:
+        description: >
+            Size of buffer used to preserve content of flash sector when
+            block within it is written. Writes to flash with sectors larger
+            than this value fail unless written area is already erased.
+        value: 4096
     MSC_FLASH_MBR:
         description: >
             Add partition table. It can prevent Windows to popup request


(mynewt-core) 01/08: hw/usb/tinyusb/msc_flash: Check LBA range in READ10 and WRITE10

Posted by ja...@apache.org.
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 d5746e99b239619bf734edc3ed063e200a207fff
Author: Szymon Janc <sz...@codecoup.pl>
AuthorDate: Sun Oct 25 22:33:42 2026 +0100

    hw/usb/tinyusb/msc_flash: Check LBA range in READ10 and WRITE10
    
    LBA received from USB host was not checked against the number of
    blocks exported by LUN, and was used to compute flash address with
    32-bit arithmetic. With MSC_FLASH_FLASH_ID and non-zero
    MSC_FLASH_FLASH_OFFSET this allowed the host to read, erase and write
    any part of the flash, including bootloader and images, and not only
    the exported part.
    
    Reject accesses that are not fully contained within the exported
    blocks. Offset within block is now also taken into account for reads.
    
    Assisted-by: Claude:claude-opus-5-5
---
 hw/usb/tinyusb/msc_flash/src/msc_flash.c | 37 ++++++++++++++++++++++++++++++--
 1 file changed, 35 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 357b98b8c..77c1b1aee 100644
--- a/hw/usb/tinyusb/msc_flash/src/msc_flash.c
+++ b/hw/usb/tinyusb/msc_flash/src/msc_flash.c
@@ -122,6 +122,33 @@ get_lun_data(uint8_t lun)
     return IN_ARRAY(luns, lun) ? luns + lun : NULL;
 }
 
+/*
+ * Converts LBA and offset to flash address. Returns false if access of size
+ * bytes is not fully contained within flash exported by LUN.
+ */
+static bool
+lba_to_flash_addr(struct lun_data *lun_data, uint32_t lba, uint32_t offset,
+                  uint32_t size, uint32_t *addr)
+{
+    uint64_t start;
+    uint64_t end;
+
+    if (lba < MYNEWT_VAL_MSC_FLASH_MBR || lba >= lun_data->block_count) {
+        return false;
+    }
+
+    start = (uint64_t)(lba - MYNEWT_VAL_MSC_FLASH_MBR) * lun_data->block_size + offset;
+    end = (uint64_t)(lun_data->block_count - MYNEWT_VAL_MSC_FLASH_MBR) *
+          lun_data->block_size;
+    if (start + size > end) {
+        return false;
+    }
+
+    *addr = start;
+
+    return true;
+}
+
 /*
  * Invoked when received SCSI_CMD_INQUIRY
  * Application fill vendor id, product id and revision with string up to 8, 16, 4 characters respectively
@@ -209,6 +236,7 @@ tud_msc_read10_cb(uint8_t lun, uint32_t lba, uint32_t offset, void *buffer,
                   uint32_t bufsize)
 {
     struct lun_data *lun_data = get_lun_data(lun);
+    uint32_t flash_addr;
 
     MSC_FLASH_LOG_DEBUG("read10(%d, %d, buf, %d)", lba, offset, bufsize);
 
@@ -226,7 +254,10 @@ tud_msc_read10_cb(uint8_t lun, uint32_t lba, uint32_t offset, void *buffer,
         buf[510] = 0x55;
         buf[511] = 0xAA;
     } else {
-        flash_read(lun_data, (lba - MYNEWT_VAL_MSC_FLASH_MBR) * 512, buffer, bufsize);
+        if (!lba_to_flash_addr(lun_data, lba, offset, bufsize, &flash_addr)) {
+            return -1;
+        }
+        flash_read(lun_data, flash_addr, buffer, bufsize);
     }
 
     return bufsize;
@@ -252,7 +283,9 @@ tud_msc_write10_cb(uint8_t lun, uint32_t lba, uint32_t offset, uint8_t *buffer,
     }
 
     if (lba >= MYNEWT_VAL_MSC_FLASH_MBR) {
-        flash_addr = (lba - MYNEWT_VAL_MSC_FLASH_MBR) * 512;
+        if (!lba_to_flash_addr(lun_data, lba, offset, bufsize, &flash_addr)) {
+            return -1;
+        }
         flash_erase_if_needed(lun_data, flash_addr, bufsize);
         flash_write(lun_data, flash_addr, buffer, bufsize);
     }


(mynewt-core) 02/08: hw/usb/tinyusb/msc_flash: Handle flash access errors

Posted by ja...@apache.org.
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 807b3c6a0fb3ed60b9350fb654d2853bacbb1dc8
Author: Szymon Janc <sz...@codecoup.pl>
AuthorDate: Sun Oct 25 22:33:42 2026 +0100

    hw/usb/tinyusb/msc_flash: Handle flash access errors
    
    Flash read, write and erase errors were ignored and READ10 and WRITE10
    reported success, so the host got stale buffer content on read errors
    and was not informed about failed writes. flash_area_open() result
    was not checked either, and flash area pointer was used uninitialized
    if it failed.
    
    Check flash_area_open() result and fail READ10 and WRITE10 on flash
    errors.
    
    Assisted-by: Claude:claude-opus-5-5
---
 hw/usb/tinyusb/msc_flash/src/msc_flash.c | 60 +++++++++++++++++++-------------
 1 file changed, 36 insertions(+), 24 deletions(-)

diff --git a/hw/usb/tinyusb/msc_flash/src/msc_flash.c b/hw/usb/tinyusb/msc_flash/src/msc_flash.c
index 77c1b1aee..2b6df4f2d 100644
--- a/hw/usb/tinyusb/msc_flash/src/msc_flash.c
+++ b/hw/usb/tinyusb/msc_flash/src/msc_flash.c
@@ -57,62 +57,72 @@ struct lun_data luns[1] = {
     }
 };
 
-static uint32_t
+static int
 flash_read(struct lun_data *lun_data, uint32_t addr, uint8_t *buffer, uint32_t bufsize)
 {
+    int rc = -1;
 #if defined(MYNEWT_VAL_MSC_FLASH_FLASH_ID)
-    hal_flash_read(lun_data->flash_id, lun_data->flash_start + addr, buffer, bufsize);
+    rc = hal_flash_read(lun_data->flash_id, lun_data->flash_start + addr,
+                        buffer, bufsize);
 #elif defined(MYNEWT_VAL_MSC_FLASH_FLASH_AREA_ID)
     const struct flash_area *fa;
-    flash_area_open(MYNEWT_VAL_MSC_FLASH_FLASH_AREA_ID, &fa);
-    if (fa) {
-        flash_area_read(fa, addr, buffer, bufsize);
+
+    if (flash_area_open(MYNEWT_VAL_MSC_FLASH_FLASH_AREA_ID, &fa) == 0) {
+        rc = flash_area_read(fa, addr, buffer, bufsize);
         flash_area_close(fa);
     }
 #endif
-    return bufsize;
+    return rc;
 }
 
-static uint32_t
+static int
 flash_write(struct lun_data *lun_data, uint32_t addr, const uint8_t *buffer,
             uint32_t bufsize)
 {
+    int rc = -1;
 #if defined(MYNEWT_VAL_MSC_FLASH_FLASH_ID)
-    hal_flash_write(lun_data->flash_id, lun_data->flash_start + addr, buffer, bufsize);
+    rc = hal_flash_write(lun_data->flash_id, lun_data->flash_start + addr,
+                         buffer, bufsize);
 #elif defined(MYNEWT_VAL_MSC_FLASH_FLASH_AREA_ID)
     const struct flash_area *fa;
-    flash_area_open(MYNEWT_VAL_MSC_FLASH_FLASH_AREA_ID, &fa);
-    if (fa) {
-        flash_area_write(fa, addr, buffer, bufsize);
+
+    if (flash_area_open(MYNEWT_VAL_MSC_FLASH_FLASH_AREA_ID, &fa) == 0) {
+        rc = flash_area_write(fa, addr, buffer, bufsize);
         flash_area_close(fa);
     }
 #endif
-    return bufsize;
+    return rc;
 }
 
-static void
+static int
 flash_erase_if_needed(struct lun_data *lun_data, uint32_t addr, uint32_t size)
 {
     int left = size;
-    while (left > 0) {
+    int rc = 0;
+
+    while (left > 0 && rc == 0) {
 #if defined(MYNEWT_VAL_MSC_FLASH_FLASH_ID)
         if (!hal_flash_isempty(lun_data->flash_id, lun_data->flash_start + addr,
                                lun_data->flash_sector_buffer, 512)) {
-            hal_flash_erase(lun_data->flash_id, lun_data->flash_start + addr, 512);
+            rc = hal_flash_erase(lun_data->flash_id,
+                                 lun_data->flash_start + addr, 512);
         }
 #elif defined(MYNEWT_VAL_MSC_FLASH_FLASH_AREA_ID)
         const struct flash_area *fa;
-        flash_area_open(MYNEWT_VAL_MSC_FLASH_FLASH_AREA_ID, &fa);
-        if (fa) {
+
+        rc = flash_area_open(MYNEWT_VAL_MSC_FLASH_FLASH_AREA_ID, &fa);
+        if (rc == 0) {
             if (!flash_area_read_is_empty(fa, addr,
                                           lun_data->flash_sector_buffer, 512)) {
-                flash_area_erase(fa, addr, 512);
+                rc = flash_area_erase(fa, addr, 512);
             }
             flash_area_close(fa);
         }
 #endif
         left -= 512;
     }
+
+    return rc;
 }
 #define IN_ARRAY(arr, ix) ((ix) < ARRAY_SIZE(arr))
 
@@ -257,7 +267,9 @@ tud_msc_read10_cb(uint8_t lun, uint32_t lba, uint32_t offset, void *buffer,
         if (!lba_to_flash_addr(lun_data, lba, offset, bufsize, &flash_addr)) {
             return -1;
         }
-        flash_read(lun_data, flash_addr, buffer, bufsize);
+        if (flash_read(lun_data, flash_addr, buffer, bufsize) != 0) {
+            return -1;
+        }
     }
 
     return bufsize;
@@ -286,8 +298,10 @@ tud_msc_write10_cb(uint8_t lun, uint32_t lba, uint32_t offset, uint8_t *buffer,
         if (!lba_to_flash_addr(lun_data, lba, offset, bufsize, &flash_addr)) {
             return -1;
         }
-        flash_erase_if_needed(lun_data, flash_addr, bufsize);
-        flash_write(lun_data, flash_addr, buffer, bufsize);
+        if (flash_erase_if_needed(lun_data, flash_addr, bufsize) != 0 ||
+            flash_write(lun_data, flash_addr, buffer, bufsize) != 0) {
+            return -1;
+        }
     }
 
     return bufsize;
@@ -381,9 +395,7 @@ msc_flash_init(void)
     uint32_t end_address;
     uint32_t flash_size;
 
-    flash_area_open(flash_area_id, &fa);
-
-    if (fa) {
+    if (flash_area_open(flash_area_id, &fa) == 0) {
         start_address = fa->fa_off;
         end_address = start_address + fa->fa_size;
         flash_size = end_address - start_address;


(mynewt-core) 06/08: hw/usb/tinyusb/msc_flash: Refuse flash not aligned to sectors

Posted by ja...@apache.org.
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 49a4e49ab774bc135a26bdd8993afc181f14d89b
Author: Szymon Janc <sz...@codecoup.pl>
AuthorDate: Mon Oct 26 17:12:56 2026 +0100

    hw/usb/tinyusb/msc_flash: Refuse flash not aligned to sectors
    
    Flash is erased in whole sectors. If exported flash did not start or
    end on sector boundary, writing first or last block erased also flash
    outside of exported part, e.g. bootloader or image, which could brick
    the device.
    
    Refuse to export flash that does not consist of whole sectors.
    
    Assisted-by: Claude:claude-opus-5-5
---
 hw/usb/tinyusb/msc_flash/src/msc_flash.c | 44 +++++++++++++++++++++++++++++++-
 1 file changed, 43 insertions(+), 1 deletion(-)

diff --git a/hw/usb/tinyusb/msc_flash/src/msc_flash.c b/hw/usb/tinyusb/msc_flash/src/msc_flash.c
index 7393d8e46..a4bf312c0 100644
--- a/hw/usb/tinyusb/msc_flash/src/msc_flash.c
+++ b/hw/usb/tinyusb/msc_flash/src/msc_flash.c
@@ -65,6 +65,34 @@ flash_read(struct lun_data *lun_data, uint32_t addr, uint8_t *buffer, uint32_t b
                           buffer, bufsize);
 }
 
+/* Finds sector containing address */
+static int
+flash_sector_find(uint8_t flash_id, uint32_t addr, uint32_t *sector_start,
+                  uint32_t *sector_size)
+{
+    const struct hal_flash *hf = hal_bsp_flash_dev(flash_id);
+    uint32_t start;
+    uint32_t size;
+    int i;
+
+    if (!hf) {
+        return -1;
+    }
+
+    for (i = 0; i < hf->hf_sector_cnt; i++) {
+        if (hal_flash_sector_info(flash_id, i, &start, &size)) {
+            return -1;
+        }
+        if (addr >= start && addr - start < size) {
+            *sector_start = start;
+            *sector_size = size;
+            return 0;
+        }
+    }
+
+    return -1;
+}
+
 static int
 flash_write(struct lun_data *lun_data, uint32_t addr, const uint8_t *buffer,
             uint32_t bufsize)
@@ -366,13 +394,14 @@ boot_preboot(void)
 void
 msc_flash_init(void)
 {
+    uint32_t sector_start;
+    uint32_t sector_size;
     uint32_t start_address;
     uint32_t end_address;
     uint8_t flash_id;
 #if defined(MYNEWT_VAL_MSC_FLASH_FLASH_ID)
     /* Flash ID as defined in bsp.yml */
     const struct hal_flash *hf;
-    uint32_t sector_size;
 
     flash_id = MYNEWT_VAL_MSC_FLASH_FLASH_ID;
     hf = hal_bsp_flash_dev(flash_id);
@@ -395,6 +424,19 @@ msc_flash_init(void)
     flash_area_close(fa);
 #endif
 
+    /*
+     * Exported flash must consist of whole sectors, otherwise writing to it
+     * would require erasing flash outside of it.
+     */
+    if (flash_sector_find(flash_id, start_address, &sector_start, &sector_size) ||
+        sector_start != start_address ||
+        flash_sector_find(flash_id, end_address - 1, &sector_start, &sector_size) ||
+        sector_start + sector_size != end_address) {
+        MSC_FLASH_LOG_ERROR("Flash 0x%x-0x%x is not sector aligned\n",
+                            (unsigned)start_address, (unsigned)end_address);
+        return;
+    }
+
     luns[0].flash_id = flash_id;
     luns[0].block_count =
         (end_address - start_address) / 512 + MYNEWT_VAL_MSC_FLASH_MBR;