summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ports/stm32/mboot/main.c33
1 files changed, 32 insertions, 1 deletions
diff --git a/ports/stm32/mboot/main.c b/ports/stm32/mboot/main.c
index 2424b37da..39492b4e5 100644
--- a/ports/stm32/mboot/main.c
+++ b/ports/stm32/mboot/main.c
@@ -435,7 +435,7 @@ void mp_hal_pin_config_speed(uint32_t port_pin, uint32_t speed) {
#elif defined(STM32G0)
#define FLASH_LAYOUT_STR "@Internal Flash /0x08000000/256*02Kg" MBOOT_SPIFLASH_LAYOUT MBOOT_SPIFLASH2_LAYOUT
#elif defined(STM32H5)
-#define FLASH_LAYOUT_STR "@Internal Flash /0x08000000/256*08Kg" MBOOT_SPIFLASH_LAYOUT MBOOT_SPIFLASH2_LAYOUT
+#define FLASH_LAYOUT_TEMPLATE "@Internal Flash /0x08000000/???*08Kg" MBOOT_SPIFLASH_LAYOUT MBOOT_SPIFLASH2_LAYOUT
#elif defined(STM32H743xx)
#define FLASH_LAYOUT_STR "@Internal Flash /0x08000000/16*128Kg" MBOOT_SPIFLASH_LAYOUT MBOOT_SPIFLASH2_LAYOUT
#elif defined(STM32H750xx)
@@ -444,6 +444,25 @@ void mp_hal_pin_config_speed(uint32_t port_pin, uint32_t speed) {
#define FLASH_LAYOUT_STR "@Internal Flash /0x08000000/256*04Kg" MBOOT_SPIFLASH_LAYOUT MBOOT_SPIFLASH2_LAYOUT
#endif
+#if !defined(FLASH_LAYOUT_STR)
+
+#define FLASH_LAYOUT_STR_ALLOC (sizeof(FLASH_LAYOUT_TEMPLATE))
+
+// Build the flash layout string from a template with total flash size inserted.
+static size_t build_flash_layout_str(char *buf) {
+ size_t len = FLASH_LAYOUT_STR_ALLOC - 1;
+ memcpy(buf, FLASH_LAYOUT_TEMPLATE, len + 1);
+ unsigned int num_sectors = FLASH_SIZE / FLASH_SECTOR_SIZE;
+ buf += 31; // location of "???" in FLASH_LAYOUT_TEMPLATE
+ for (unsigned int i = 0; i < 3; ++i) {
+ *buf-- = '0' + num_sectors % 10;
+ num_sectors /= 10;
+ }
+ return len;
+}
+
+#endif
+
static bool flash_is_modifiable_addr_range(uint32_t addr, uint32_t len) {
return addr + len < (uint32_t)&_mboot_protected_flash_start
|| addr >= (uint32_t)&_mboot_protected_flash_end_exclusive;
@@ -756,8 +775,12 @@ void i2c_slave_process_rx_end(i2c_slave_t *i2c) {
} else if (buf[0] == I2C_CMD_RESET && len == 0) {
dfu_context.leave_dfu = true;
} else if (buf[0] == I2C_CMD_GETLAYOUT && len == 0) {
+ #if defined(FLASH_LAYOUT_STR)
len = strlen(FLASH_LAYOUT_STR);
memcpy(buf, FLASH_LAYOUT_STR, len);
+ #else
+ len = build_flash_layout_str(buf);
+ #endif
} else if (buf[0] == I2C_CMD_MASSERASE && len == 0) {
len = do_mass_erase();
} else if (buf[0] == I2C_CMD_PAGEERASE && len == 4) {
@@ -1161,7 +1184,15 @@ static uint8_t *pyb_usbdd_StrDescriptor(USBD_HandleTypeDef *pdev, uint8_t idx, u
}
case USBD_IDX_CONFIG_STR:
+ #if defined(FLASH_LAYOUT_STR)
USBD_GetString((uint8_t *)FLASH_LAYOUT_STR, str_desc, length);
+ #else
+ {
+ char buf[FLASH_LAYOUT_STR_ALLOC];
+ build_flash_layout_str(buf);
+ USBD_GetString((uint8_t *)buf, str_desc, length);
+ }
+ #endif
return str_desc;
case MBOOT_ERROR_STR_OVERWRITE_BOOTLOADER_IDX: