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

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


The following commit(s) were added to refs/heads/master by this push:
     new e64e69609 tinyusb/msc_fat_view: Check image only when it is fully 
written
e64e69609 is described below

commit e64e6960967fc5b87664398d15804117f7c300b7
Author: Jerzy Kasenberg <[email protected]>
AuthorDate: Tue Oct 20 09:19:59 2026 +0200

    tinyusb/msc_fat_view: Check image only when it is fully written
    
    Some tools (e.g. cp from Git Bash/MSYS) do not set file size
    upfront. File grows with each write and host updates directory
    entry with intermediate sizes while data is still being written.
    Update handler treated first directory entry pointing to written
    sectors as finished file and checked the image right away:
    - if TLV area was empty image was rejected and rest of the file
      was not written to flash,
    - if slot contained old image of the same size, its TLVs made
      partially written image look valid, image was marked pending
      and device reset in the middle of the copy.
    
    Now image is checked only when data written so far covers whole
    image including TLV area (size taken from image header and TLV
    info). Earlier directory updates are ignored.
    
    Assisted-by: Claude:claude-opus-5-5
    Signed-off-by: Jerzy Kasenberg <[email protected]>
---
 hw/usb/tinyusb/msc_fat_view/src/update_handler.c | 44 ++++++++++++++++++++++++
 1 file changed, 44 insertions(+)

diff --git a/hw/usb/tinyusb/msc_fat_view/src/update_handler.c 
b/hw/usb/tinyusb/msc_fat_view/src/update_handler.c
index d2da32094..049366168 100644
--- a/hw/usb/tinyusb/msc_fat_view/src/update_handler.c
+++ b/hw/usb/tinyusb/msc_fat_view/src/update_handler.c
@@ -208,6 +208,45 @@ image_write_sector(struct msc_fat_view_write_handler 
*handler, uint32_t sector,
 #endif
 }
 
+/*
+ * Checks if whole image (including TLVs) was already written.
+ * Host can update directory entry before all file data is written
+ * (i.e. when file grows with each write), image must not be checked
+ * till then.
+ */
+static bool
+image_write_complete(void)
+{
+#ifdef FLASH_AREA_IMAGE
+    const struct flash_area *fa;
+    struct image_header hdr;
+    struct image_tlv_info tlv_info;
+    uint32_t written;
+    uint32_t tlv_off;
+    bool complete = false;
+
+    written = (unallocated_write.last_sector - unallocated_write.first_sector 
+ 1) *
+              SECTOR_SIZE;
+    if (flash_area_open(FLASH_AREA_IMAGE, &fa)) {
+        return false;
+    }
+    if (flash_area_read(fa, 0, &hdr, sizeof(hdr)) == 0) {
+        tlv_off = hdr.ih_hdr_size + hdr.ih_img_size + hdr.ih_protect_tlv_size;
+        if (tlv_off + sizeof(tlv_info) <= written &&
+            flash_area_read(fa, tlv_off, &tlv_info, sizeof(tlv_info)) == 0) {
+            /* Not a TLV area, image is broken, let image check reject it */
+            complete = tlv_info.it_magic != IMAGE_TLV_INFO_MAGIC ||
+                       tlv_off + tlv_info.it_tlv_tot <= written;
+        }
+    }
+    flash_area_close(fa);
+
+    return complete;
+#else
+    return true;
+#endif
+}
+
 static int
 image_file_written(struct msc_fat_view_write_handler *handler, uint32_t size,
                    uint32_t sector, bool first_sector)
@@ -216,6 +255,11 @@ image_file_written(struct msc_fat_view_write_handler 
*handler, uint32_t size,
     uint32_t flags;
 
     if (unallocated_write.write_status == WRITE_IN_PROGRESS) {
+        if (unallocated_write.first_sector == sector && first_sector &&
+            !image_write_complete()) {
+            MSC_FAT_VIEW_LOG_INFO("New file not complete yet (%d bytes)\n", 
(int)size);
+            return 0;
+        }
         if (unallocated_write.first_sector == sector && first_sector) {
             MSC_FAT_VIEW_LOG_INFO("New file detected\n");
             if (BOOT_LOADER) {

Reply via email to