Skip to content

Commit de08190

Browse files
committed
stm32/mboot: Allow USB strings to be dynamic.
Signed-off-by: Damien George <[email protected]>
1 parent aa09456 commit de08190

File tree

1 file changed

+60
-16
lines changed

1 file changed

+60
-16
lines changed

ports/stm32/mboot/main.c

Lines changed: 60 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -429,38 +429,82 @@ void mp_hal_pin_config_speed(uint32_t port_pin, uint32_t speed) {
429429
|| defined(STM32F723xx) \
430430
|| defined(STM32F732xx) \
431431
|| defined(STM32F733xx)
432-
#define FLASH_LAYOUT_STR "@Internal Flash /0x08000000/04*016Kg,01*064Kg,07*128Kg" MBOOT_SPIFLASH_LAYOUT MBOOT_SPIFLASH2_LAYOUT
432+
#define INTERNAL_FLASH_LAYOUT "@Internal Flash /0x08000000/04*016Kg,01*064Kg,07*128Kg"
433433
#elif defined(STM32F765xx) || defined(STM32F767xx) || defined(STM32F769xx)
434-
#define FLASH_LAYOUT_STR "@Internal Flash /0x08000000/04*032Kg,01*128Kg,07*256Kg" MBOOT_SPIFLASH_LAYOUT MBOOT_SPIFLASH2_LAYOUT
434+
#define INTERNAL_FLASH_LAYOUT "@Internal Flash /0x08000000/04*032Kg,01*128Kg,07*256Kg"
435435
#elif defined(STM32G0)
436-
#define FLASH_LAYOUT_STR "@Internal Flash /0x08000000/256*02Kg" MBOOT_SPIFLASH_LAYOUT MBOOT_SPIFLASH2_LAYOUT
436+
#define INTERNAL_FLASH_LAYOUT "@Internal Flash /0x08000000/256*02Kg"
437437
#elif defined(STM32H5)
438-
#define FLASH_LAYOUT_TEMPLATE "@Internal Flash /0x08000000/???*08Kg" MBOOT_SPIFLASH_LAYOUT MBOOT_SPIFLASH2_LAYOUT
438+
#define INTERNAL_FLASH_LAYOUT "@Internal Flash /0x08000000/???*08Kg"
439+
#define INTERNAL_FLASH_LAYOUT_HAS_TEMPLATE (1)
439440
#elif defined(STM32H743xx)
440-
#define FLASH_LAYOUT_STR "@Internal Flash /0x08000000/16*128Kg" MBOOT_SPIFLASH_LAYOUT MBOOT_SPIFLASH2_LAYOUT
441+
#define INTERNAL_FLASH_LAYOUT "@Internal Flash /0x08000000/16*128Kg"
441442
#elif defined(STM32H750xx)
442-
#define FLASH_LAYOUT_STR "@Internal Flash /0x08000000/01*128Kg" MBOOT_SPIFLASH_LAYOUT MBOOT_SPIFLASH2_LAYOUT
443+
#define INTERNAL_FLASH_LAYOUT "@Internal Flash /0x08000000/01*128Kg"
443444
#elif defined(STM32WB)
444-
#define FLASH_LAYOUT_STR "@Internal Flash /0x08000000/256*04Kg" MBOOT_SPIFLASH_LAYOUT MBOOT_SPIFLASH2_LAYOUT
445+
#define INTERNAL_FLASH_LAYOUT "@Internal Flash /0x08000000/256*04Kg"
445446
#endif
446447

447-
#if !defined(FLASH_LAYOUT_STR)
448+
#if INTERNAL_FLASH_LAYOUT_HAS_TEMPLATE \
449+
|| defined(MBOOT_SPIFLASH_LAYOUT_DYNAMIC_MAX_LEN) \
450+
|| defined(MBOOT_SPIFLASH2_LAYOUT_DYNAMIC_MAX_LEN)
448451

449-
#define FLASH_LAYOUT_STR_ALLOC (sizeof(FLASH_LAYOUT_TEMPLATE))
452+
#ifndef MBOOT_SPIFLASH_LAYOUT_DYNAMIC_MAX_LEN
453+
#define MBOOT_SPIFLASH_LAYOUT_DYNAMIC_MAX_LEN (sizeof(MBOOT_SPIFLASH_LAYOUT) - 1)
454+
#endif
455+
456+
#ifndef MBOOT_SPIFLASH2_LAYOUT_DYNAMIC_MAX_LEN
457+
#define MBOOT_SPIFLASH2_LAYOUT_DYNAMIC_MAX_LEN (sizeof(MBOOT_SPIFLASH2_LAYOUT) - 1)
458+
#endif
459+
460+
#define FLASH_LAYOUT_STR_ALLOC \
461+
( \
462+
(sizeof(INTERNAL_FLASH_LAYOUT) - 1) \
463+
+ MBOOT_SPIFLASH_LAYOUT_DYNAMIC_MAX_LEN \
464+
+ MBOOT_SPIFLASH2_LAYOUT_DYNAMIC_MAX_LEN \
465+
+ 1 \
466+
)
450467

451468
// Build the flash layout string from a template with total flash size inserted.
452-
static size_t build_flash_layout_str(char *buf) {
453-
size_t len = FLASH_LAYOUT_STR_ALLOC - 1;
454-
memcpy(buf, FLASH_LAYOUT_TEMPLATE, len + 1);
469+
static size_t build_flash_layout_str(uint8_t *buf) {
470+
const char *internal_layout = INTERNAL_FLASH_LAYOUT;
471+
size_t internal_layout_len = strlen(internal_layout);
472+
473+
const char *spiflash_layout = MBOOT_SPIFLASH_LAYOUT;
474+
size_t spiflash_layout_len = strlen(spiflash_layout);
475+
476+
const char *spiflash2_layout = MBOOT_SPIFLASH2_LAYOUT;
477+
size_t spiflash2_layout_len = strlen(spiflash2_layout);
478+
479+
uint8_t *buf_orig = buf;
480+
481+
memcpy(buf, internal_layout, internal_layout_len);
482+
buf += internal_layout_len;
483+
484+
#if INTERNAL_FLASH_LAYOUT_HAS_TEMPLATE
455485
unsigned int num_sectors = FLASH_SIZE / FLASH_SECTOR_SIZE;
456-
buf += 31; // location of "???" in FLASH_LAYOUT_TEMPLATE
486+
uint8_t *buf_size = buf_orig + 31; // location of "???" in FLASH_LAYOUT_TEMPLATE
457487
for (unsigned int i = 0; i < 3; ++i) {
458-
*buf-- = '0' + num_sectors % 10;
488+
*buf_size-- = '0' + num_sectors % 10;
459489
num_sectors /= 10;
460490
}
461-
return len;
491+
#endif
492+
493+
memcpy(buf, spiflash_layout, spiflash_layout_len);
494+
buf += spiflash_layout_len;
495+
496+
memcpy(buf, spiflash2_layout, spiflash2_layout_len);
497+
buf += spiflash2_layout_len;
498+
499+
*buf++ = '\0';
500+
501+
return buf - buf_orig;
462502
}
463503

504+
#else
505+
506+
#define FLASH_LAYOUT_STR INTERNAL_FLASH_LAYOUT MBOOT_SPIFLASH_LAYOUT MBOOT_SPIFLASH2_LAYOUT
507+
464508
#endif
465509

466510
static bool flash_is_modifiable_addr_range(uint32_t addr, uint32_t len) {
@@ -1188,7 +1232,7 @@ static uint8_t *pyb_usbdd_StrDescriptor(USBD_HandleTypeDef *pdev, uint8_t idx, u
11881232
USBD_GetString((uint8_t *)FLASH_LAYOUT_STR, str_desc, length);
11891233
#else
11901234
{
1191-
char buf[FLASH_LAYOUT_STR_ALLOC];
1235+
uint8_t buf[FLASH_LAYOUT_STR_ALLOC];
11921236
build_flash_layout_str(buf);
11931237
USBD_GetString((uint8_t *)buf, str_desc, length);
11941238
}

0 commit comments

Comments
 (0)