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:32 UTC
(mynewt-core) 06/08: hw/usb/tinyusb/msc_flash: Refuse flash not aligned to sectors
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, §or_start, §or_size) ||
+ sector_start != start_address ||
+ flash_sector_find(flash_id, end_address - 1, §or_start, §or_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;