Repository navigation
fix(stm32f4): erase every sector in range; give each region its own sectors #166
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,15 +15,25 @@ | |
| /* | ||
| * STM32F407 Flash Memory Map (1MB total) | ||
| * | ||
| * 0x08000000 - 0x08003FFF (16K) Stage-0 (eBootloader) | ||
| * 0x08004000 - 0x0800FFFF (48K) Stage-1 (E-Boot) | ||
| * 0x08010000 - 0x0807FFFF (448K) Slot A (application firmware) | ||
| * 0x08080000 - 0x080EFFFF (448K) Slot B (candidate firmware) | ||
| * 0x080F0000 - 0x080F3FFF (16K) Recovery image (golden) | ||
| * 0x080F4000 - 0x080F5FFF (8K) Boot control (primary) | ||
| * 0x080F6000 - 0x080F7FFF (8K) Boot control (backup) | ||
| * 0x080F8000 - 0x080F9FFF (8K) Boot log | ||
| * 0x080FA000 - 0x080FFFFF (24K) Device config / reserved | ||
| * The F407 erases whole sectors only, and they are not uniform: | ||
| * sectors 0-3 are 16K, sector 4 is 64K, sectors 5-11 are 128K. Every | ||
| * region therefore owns whole sectors -- erasing one region (a slot | ||
| * update, a boot control save, a log clear) must never erase another. | ||
| * Slot B, the recovery image, both boot control copies and the boot log | ||
| * used to share sector 11, so saving the boot control block wiped the | ||
| * other four. | ||
| * | ||
| * Sector Address range Size Region | ||
| * 0 0x08000000 - 0x08003FFF 16K Stage-0 (eBootloader) | ||
| * 1-3 0x08004000 - 0x0800FFFF 48K Stage-1 (E-Boot) | ||
| * 4 0x08010000 - 0x0801FFFF 64K Boot log | ||
| * 5-6 0x08020000 - 0x0805FFFF 256K Slot A (application firmware) | ||
| * 7-8 0x08060000 - 0x0809FFFF 256K Slot B (candidate firmware) | ||
| * 9 0x080A0000 - 0x080BFFFF 128K Recovery image (golden) | ||
| * 10 0x080C0000 - 0x080DFFFF 128K Boot control (primary) | ||
| * 11 0x080E0000 - 0x080FFFFF 128K Boot control (backup) | ||
| * | ||
| * Stage-0 and stage-1 match stm32f4_stage0.ld and stm32f4_stage1.ld. | ||
| */ | ||
|
|
||
| #define STM32F4_FLASH_BASE 0x08000000 | ||
|
|
@@ -35,22 +45,73 @@ | |
| #define STM32F4_STAGE1_ADDR 0x08004000 | ||
| #define STM32F4_STAGE1_SIZE (48 * 1024) | ||
|
|
||
| #define STM32F4_SLOT_A_ADDR 0x08010000 | ||
| #define STM32F4_SLOT_A_SIZE (448 * 1024) | ||
| #define STM32F4_LOG_ADDR 0x08010000 | ||
| #define STM32F4_LOG_SIZE (64 * 1024) | ||
|
|
||
| #define STM32F4_SLOT_B_ADDR 0x08080000 | ||
| #define STM32F4_SLOT_B_SIZE (448 * 1024) | ||
| #define STM32F4_SLOT_A_ADDR 0x08020000 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟠 High · Default images target the old slot When an image is packed without an explicit Update the packer’s default address and its default entry-address behavior to match the new Slot A base. Prompt for AI agentsIn `boards/stm32f4/board_stm32f4.h` at line 51:
**Default images target the old slot**
When an image is packed without an explicit `--load-addr`, `imgpack.py` still defaults `--load-addr` to `0x08010000`, so callers that omit it produce images whose load and entry addresses point into the new boot-log region instead of Slot A; those images do not boot correctly on the new layout.
Update the packer’s default address and its default entry-address behavior to match the new Slot A base.
Comment on lines
+48
to
+51
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 2. The packing example targets the boot log The usage example in tools/imgpack.py still supplies 0x08010000 as the image load and entry address, although this PR assigns that address to the boot log. Someone following the example can pack an image for the relocated slot that passes the header's entry-range check but jumps to the boot-log address instead of the application. Agent Prompt
|
||
| #define STM32F4_SLOT_A_SIZE (256 * 1024) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟠 High · Packaging guidance points into log region When a user follows the STM32F4 packaging example or layout guidance, Update the packer example and the book's STM32F4 layout guidance to use and describe the new slot addresses. Also at Prompt for AI agentsIn `boards/stm32f4/board_stm32f4.h` at line 52:
**Packaging guidance points into log region**
When a user follows the STM32F4 packaging example or layout guidance, `tools/imgpack.py` still gives `0x08010000` as the load and entry address, which the new layout assigns to the boot log, and the book still describes the old slot map, so users produce images for the wrong address or follow contradictory layout instructions.
Update the packer example and the book's STM32F4 layout guidance to use and describe the new slot addresses.
Also at `docs/quickstart.md:105`, `docs/installation_usage.md:164`, `docs/architecture.md:38`.
Comment on lines
+51
to
+52
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 3. Existing boards cannot boot their images The relocated slot and boot-control constants are used at startup without recognizing or migrating the previous flash layout. When new bootloader code runs on a board provisioned with that layout, it looks for an image partway through old Slot A and reads boot control from addresses formerly occupied by Slot B. Agent Prompt
|
||
|
|
||
| #define STM32F4_RECOVERY_ADDR 0x080F0000 | ||
| #define STM32F4_RECOVERY_SIZE (16 * 1024) | ||
| #define STM32F4_SLOT_B_ADDR 0x08060000 | ||
| #define STM32F4_SLOT_B_SIZE (256 * 1024) | ||
|
Comment on lines
+51
to
+55
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 4. Generated flash layouts conflict with eboot The new STM32F4 slot definitions place Slot A at 0x08020000 and Slot B at 0x08060000, but ebuild’s generate_boot_yaml() calculates different offsets and sizes for a 1 MiB device. When ebuild generates boot inputs for STM32F4, its flash-layout header and linker fragment therefore describe slots that do not match this eBoot board’s runtime layout. Agent Prompt
|
||
|
|
||
| #define STM32F4_BOOTCTL_ADDR 0x080F4000 | ||
| #define STM32F4_BOOTCTL_BACKUP_ADDR 0x080F6000 | ||
| #define STM32F4_RECOVERY_ADDR 0x080A0000 | ||
| #define STM32F4_RECOVERY_SIZE (128 * 1024) | ||
|
|
||
| #define STM32F4_LOG_ADDR 0x080F8000 | ||
| #define STM32F4_BOOTCTL_ADDR 0x080C0000 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| #define STM32F4_BOOTCTL_SIZE (128 * 1024) | ||
|
|
||
| #define STM32F4_BOOTCTL_BACKUP_ADDR 0x080E0000 | ||
| #define STM32F4_BOOTCTL_BACKUP_SIZE (128 * 1024) | ||
|
|
||
| #define STM32F4_VECTOR_OFFSET (STM32F4_STAGE1_ADDR - STM32F4_FLASH_BASE) | ||
|
|
||
| /* ---- Flash sector geometry ---- */ | ||
|
|
||
| #define STM32F4_FLASH_SECTORS 12 | ||
|
|
||
| /** | ||
| * @brief Sector number holding @p addr, or -1 outside the 1MB array. | ||
| */ | ||
| static inline int stm32f4_flash_sector_of(uint32_t addr) | ||
| { | ||
| if (addr < STM32F4_FLASH_BASE) | ||
| return -1; | ||
| uint32_t off = addr - STM32F4_FLASH_BASE; | ||
| if (off < 0x10000) /* sectors 0-3: 16K each */ | ||
| return (int)(off / 0x4000); | ||
| if (off < 0x20000) /* sector 4: 64K */ | ||
| return 4; | ||
| if (off < STM32F4_FLASH_SIZE) /* sectors 5-11: 128K each */ | ||
| return 4 + (int)(off / 0x20000); | ||
| return -1; | ||
| } | ||
|
|
||
| /** | ||
| * @brief First and last sector an erase of [addr, addr + len) must clear. | ||
| * | ||
| * Every sector the range touches is included. A sector is the smallest | ||
| * unit the F407 can erase, so the erase may clear bytes past either end of | ||
| * the range; the memory map above keeps each region sector-aligned so | ||
| * those bytes never belong to another region. | ||
| * | ||
| * @return 0 on success; -1 for len == 0, or a range that wraps or leaves | ||
| * the flash array (nothing is to be erased then). | ||
| */ | ||
| static inline int stm32f4_flash_sector_range(uint32_t addr, size_t len, | ||
| int *first, int *last) | ||
| { | ||
| if (len == 0 || (uint64_t)addr + len > (uint64_t)STM32F4_FLASH_BASE + | ||
| STM32F4_FLASH_SIZE) | ||
| return -1; | ||
|
Comment on lines
+103
to
+105
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reject wrapped lengths on 64-bit hosts. When
|
||
| int f = stm32f4_flash_sector_of(addr); | ||
| int l = stm32f4_flash_sector_of((uint32_t)(addr + len - 1)); | ||
| if (f < 0 || l < 0) | ||
| return -1; | ||
| *first = f; | ||
| *last = l; | ||
| return 0; | ||
| } | ||
|
|
||
| /* UART for recovery */ | ||
| #define STM32F4_UART_INSTANCE 2 /* USART2 on PA2/PA3 */ | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -161,7 +161,7 @@ python3 -m pip install -r requirements.txt | |
| # Pack firmware with eboot image header. The input limits (size, load and | ||
| # entry addresses) are listed in quickstart.md and in tools/imgpack.py. | ||
| python tools/imgpack.py --input firmware.bin --output firmware.packed.bin \ | ||
| --version 1.0.0 --load-addr 0x08010000 --entry-addr 0x08010100 | ||
| --version 1.0.0 --load-addr 0x08020000 --entry-addr 0x08020100 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Update the The preceding
|
||
|
|
||
| # Sign the image | ||
| python tools/sign_image.py firmware.packed.bin --key signing_key.pem | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1. Slot erases can reset the device
🐞 Bug☼ ReliabilityAgent Prompt
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation toolsDismiss ↗ | View ↗