Skip to content

boards: fix mtd_partition() argument units in flash partition loops - #20014

Open
rongbc wants to merge 1 commit into
apache:masterfrom
rongbc:rongbc_0831
Open

boards: fix mtd_partition() argument units in flash partition loops#20014
rongbc wants to merge 1 commit into
apache:masterfrom
rongbc:rongbc_0831

Conversation

@rongbc

@rongbc rongbc commented Aug 30, 2026

Copy link
Copy Markdown
Contributor

Summary

mtd_partition(mtd, firstblock, nblocks) takes the partition offset and size in units of the underlying device "blocks" (geo.blocksize), not erase blocks. The implementation is authoritative:

  • drivers/mtd/mtd_partition.c:875 computes blkpererase = erasesize / blocksize;
  • drivers/mtd/mtd_partition.c:885-886 divides the incoming firstblock/nblocks by blkpererase to derive erase-block boundaries (erasestart/eraseend), i.e. the inputs are treated as blocksize units;
  • part_bread/part_bwrite (drivers/mtd/mtd_partition.c:274/:296) use priv->firstblock directly as a block index into the parent device;
  • the doc comment at include/nuttx/mtd/mtd.h:286 ("The offset in bytes to the first block") is stale — the implementation is what callers must match.

Four board drivers instead accumulated partoffset and computed the partition size in erase-block units and passed them straight to mtd_partition(). On devices where blocksize != erasesize — W25/SST25 SPI NOR (256B vs 4KB) and SAMD5E5 program memory (512B vs 8KB cluster) — every partition came out erasesize/blocksize (16x) too small, and partitions after the first were misaligned/overlapping. On the W25 this is hardware-confirmed: a 512KB partition is reported as 32KB (see Testing).

This change converts partoffset and partszbytes to geo.blocksize units at the mtd_partition() call site while keeping the loop's erase-block accumulation:

blkpererase = geo.blocksize > 0 ? erasesize / geo.blocksize : 1;
mtd_part = mtd_partition(mtd, partoffset * blkpererase,
                         partszbytes / geo.blocksize);
partoffset += partszbytes / erasesize;

Affected boards:

  • stm32f103-minimum (W25Q32FV on SPI1)
  • at32f437-mini (W25, same code as upstream stm32f103-minimum)
  • stm32f429i-disco (SST25F064; the buggy path is enabled in the shipped extflash defconfig)
  • metro-m4 (SAMD5E5 program memory via mtd_progmem)

The remaining mtd_partition() call sites were audited and are correct: b-l475e-iot01a/stm32l476vg-disco already use geo.blocksize; mikroe-stm32f4 hardcodes the 256B conversion; the progmem OTA boards (imxrt/nrf5x/samv7/stm32h7) pass page units that equal geo.blocksize; the ESP32/ESP32C3/ESP32S3, BL602, TLSR82, RTL8720C and fs/partition paths are internally consistent.

Impact

  • Users: partition sizes on the affected boards are now correct. With the buggy code, a 512KB partition was reported (and usable) as 32KB only; partitions already formatted with the buggy code occupy only blocksize/erasesize of the intended region and should be re-created/reformatted after this change.
  • Build: no new configuration options, no new dependencies, no API/ABI change (mtd_partition() signature untouched). Pure board-level code change.
  • Hardware: only boards that enable the affected partition paths and have blocksize != erasesize are affected; devices with blocksize == erasesize (e.g. CONFIG_W25_SECT512) behave unchanged.
  • Documentation: none (the stale comment at include/nuttx/mtd/mtd.h:286 could be fixed separately; out of scope here).
  • Security: none.

Testing

Hardware (bug reproduction, before fix) — environment:

Item Value
NuttX baseline master @ 15678acf
Board stm32f103-minimum code, reproduced on its derived board stm32f103-mini-v2 (STM32F103RCT6)
External FLASH W25Q16 (2MB, SPI1, CS=PA2)
Config CONFIG_STM32F103MINIMUM_FLASH=y, CONFIG_STM32F103MINIMUM_FLASH_PART=y, CONFIG_MTD_SMART=y, CONFIG_FS_SMARTFS=y; partition list 512,512,512,512 (4×512KB)
Toolchain / build arm-none-eabi-gcc 10.3-2021.10; ./tools/configure.sh -l stm32f103-minimum:xxx && make

Test steps and result:

  1. Flash the build, boot, then mksmartfs /dev/smart0p1 and mount it.
  2. The partition reports 32KB instead of the expected 512KB (512KB ÷ 16 = 32KB; 16 = erasesize/blocksize = 4096/256). Partitions after the first are also misaligned.
  3. 32KB happens to be a multiple of the 4KB erase block, so the partition is created without error — it is simply the wrong size.

After the fix (this workspace, compile-level):

  • Host: Linux; toolchain arm-none-eabi-gcc 10.3-2021.10; NuttX master @ 15678acf + this change.
  • stm32f103-minimum/src/stm32_w25.c and at32f437-mini/src/at32_w25.c: -fsyntax-only with the partition path emulated (CONFIG_STM32_SPI1/CONFIG_AT32_SPI1, CONFIG_MTD_W25, CONFIG_*_FLASH_PART, CONFIG_FS_SMARTFS, CONFIG_MTD_SMART, ...) → exit 0, no errors.
  • stm32f429i-disco/src/stm32_bringup.c and samd5e5/metro-m4/src/sam_smartfs.c: full-file syntax check is not possible in this workspace (generated include/nuttx/config.h/include/arch/chip point at a stale, different board build); the exact edited statements were extracted from disk into a type-stub harness reproducing the real signatures and compiled with -Wall -Wextraexit 0, no errors.
  • Tree-wide grep confirms no remaining mtd_partition(..., partszbytes / erasesize) call sites.

Expected post-fix hardware result (to be confirmed on board): repeating the same steps above, /dev/smart0p1 should report 512KB, and /dev/mtd0p* offsets should be contiguous and non-overlapping.

@github-actions github-actions Bot added Size: S The size of the change in this PR is small Board: arm labels Aug 30, 2026
@github-actions

github-actions Bot commented Aug 30, 2026

Copy link
Copy Markdown

MemBrowse Memory Report

No memory changes detected for:

@Abhishekmishra2808

Abhishekmishra2808 commented Aug 30, 2026

Copy link
Copy Markdown
Contributor

@rongbc Please run checkpatch (see more at https://nuttx.apache.org/docs/latest/components/tools/checkpatch.html)

Logs:

Error: /home/runner/work/nuttx/nuttx/nuttx/boards/arm/at32/at32f437-mini/src/at32_w25.c:132:2: error: Missing blank line after declarations
Used config files:
    1: .codespellrc
Error: /home/runner/work/nuttx/nuttx/nuttx/boards/arm/samd5e5/metro-m4/src/sam_smartfs.c:97:4: error: Bad left brace alignment
Error: /home/runner/work/nuttx/nuttx/nuttx/boards/arm/samd5e5/metro-m4/src/sam_smartfs.c:115:10: error: Bad alignment
Error: /home/runner/work/nuttx/nuttx/nuttx/boards/arm/samd5e5/metro-m4/src/sam_smartfs.c:117:10: error: Bad alignment
Error: /home/runner/work/nuttx/nuttx/nuttx/boards/arm/samd5e5/metro-m4/src/sam_smartfs.c:119:10: error: Bad alignment
Error: /home/runner/work/nuttx/nuttx/nuttx/boards/arm/samd5e5/metro-m4/src/sam_smartfs.c:168:16: error: Bad alignment
Error: /home/runner/work/nuttx/nuttx/nuttx/boards/arm/samd5e5/metro-m4/src/sam_smartfs.c:170:16: error: Bad left brace alignment
Error: /home/runner/work/nuttx/nuttx/nuttx/boards/arm/samd5e5/metro-m4/src/sam_smartfs.c:171:18: error: Bad comment alignment
Error: /home/runner/work/nuttx/nuttx/nuttx/boards/arm/samd5e5/metro-m4/src/sam_smartfs.c:172:19: error: Bad comment block alignment
Error: /home/runner/work/nuttx/nuttx/nuttx/boards/arm/samd5e5/metro-m4/src/sam_smartfs.c:173:19: error: Bad comment block alignment
Error: /home/runner/work/nuttx/nuttx/nuttx/boards/arm/samd5e5/metro-m4/src/sam_smartfs.c:176:22: error: Bad alignment
Error: /home/runner/work/nuttx/nuttx/nuttx/boards/arm/samd5e5/metro-m4/src/sam_smartfs.c:177:22: error: Bad alignment
Error: /home/runner/work/nuttx/nuttx/nuttx/boards/arm/samd5e5/metro-m4/src/sam_smartfs.c:179:16: error: Bad right brace alignment
Error: /home/runner/work/nuttx/nuttx/nuttx/boards/arm/samd5e5/metro-m4/src/sam_smartfs.c:181:18: error: Bad alignment
Error: /home/runner/work/nuttx/nuttx/nuttx/boards/arm/samd5e5/metro-m4/src/sam_smartfs.c:184:18: error: Bad alignment
Error: /home/runner/work/nuttx/nuttx/nuttx/boards/arm/samd5e5/metro-m4/src/sam_smartfs.c:192:18: error: Bad alignment
Error: /home/runner/work/nuttx/nuttx/nuttx/boards/arm/samd5e5/metro-m4/src/sam_smartfs.c:194:18: error: Bad comment alignment
Error: /home/runner/work/nuttx/nuttx/nuttx/boards/arm/samd5e5/metro-m4/src/sam_smartfs.c:195:19: error: Bad comment block alignment
Error: /home/runner/work/nuttx/nuttx/nuttx/boards/arm/samd5e5/metro-m4/src/sam_smartfs.c:196:19: error: Bad comment block alignment
Error: /home/runner/work/nuttx/nuttx/nuttx/boards/arm/samd5e5/metro-m4/src/sam_smartfs.c:197:19: error: Bad comment block alignment
Error: /home/runner/work/nuttx/nuttx/nuttx/boards/arm/samd5e5/metro-m4/src/sam_smartfs.c:198:19: error: Bad comment block alignment
Error: /home/runner/work/nuttx/nuttx/nuttx/boards/arm/samd5e5/metro-m4/src/sam_smartfs.c:199:19: error: Bad comment block alignment
Error: /home/runner/work/nuttx/nuttx/nuttx/boards/arm/samd5e5/metro-m4/src/sam_smartfs.c:200:19: error: Bad comment block alignment
Error: /home/runner/work/nuttx/nuttx/nuttx/boards/arm/samd5e5/metro-m4/src/sam_smartfs.c:202:18: error: Bad alignment
Error: /home/runner/work/nuttx/nuttx/nuttx/boards/arm/samd5e5/metro-m4/src/sam_smartfs.c:209:18: error: Bad alignment
Error: /home/runner/work/nuttx/nuttx/nuttx/boards/arm/samd5e5/metro-m4/src/sam_smartfs.c:215:10: error: Bad alignment
Error: /home/runner/work/nuttx/nuttx/nuttx/boards/arm/samd5e5/metro-m4/src/sam_smartfs.c:217:10: error: Bad alignment
Error: /home/runner/work/nuttx/nuttx/nuttx/boards/arm/samd5e5/metro-m4/src/sam_smartfs.c:222:10: error: Bad alignment
Error: /home/runner/work/nuttx/nuttx/nuttx/boards/arm/samd5e5/metro-m4/src/sam_smartfs.c:227:10: error: Bad alignment
Error: /home/runner/work/nuttx/nuttx/nuttx/boards/arm/samd5e5/metro-m4/src/sam_smartfs.c:229:10: error: Bad alignment
Error: /home/runner/work/nuttx/nuttx/nuttx/boards/arm/samd5e5/metro-m4/src/sam_smartfs.c:230:8: error: Bad right brace alignment
Error: /home/runner/work/nuttx/nuttx/nuttx/boards/arm/stm32f1/stm32f103-minimum/src/stm32_w25.c:126:2: error: Missing blank line after declarations
Error: /home/runner/work/nuttx/nuttx/nuttx/boards/arm/stm32f1/stm32f103-minimum/src/stm32_w25.c:148:4: error: Bad left brace alignment
Error: /home/runner/work/nuttx/nuttx/nuttx/boards/arm/stm32f4/stm32f429i-disco/src/stm32_bringup.c:183:8: error: Bad left brace alignment
Error: /home/runner/work/nuttx/nuttx/nuttx/boards/arm/stm32f4/stm32f429i-disco/src/stm32_bringup.c:338:4: error: Bad left brace alignment
Error: /home/runner/work/nuttx/nuttx/nuttx/boards/arm/stm32f4/stm32f429i-disco/src/stm32_bringup.c:341:6: error: Missing blank line after declarations
Some checks failed. For contributing guidelines, see:
  https://github.com/apache/nuttx/blob/master/CONTRIBUTING.md
Error: Process completed with exit code 1.

@github-actions github-actions Bot added Size: L The size of the change in this PR is large and removed Size: S The size of the change in this PR is small labels Aug 30, 2026
Comment thread boards/arm/stm32f4/stm32f429i-disco/src/stm32_bringup.c
Comment thread boards/arm/stm32f1/stm32f103-minimum/src/stm32_w25.c
Comment thread boards/arm/samd5e5/metro-m4/src/sam_smartfs.c Outdated
mtd_partition(mtd, firstblock, nblocks) takes the partition offset and
size in units of the underlying device "blocks" (geo.blocksize), not
erase blocks.  Several board drivers accumulated partoffset and computed
the partition size in erase-block units and passed them straight to
mtd_partition(), so on devices where blocksize != erasesize (W25/SST25:
256B vs 4KB, SAMD5E5 progmem: 512B vs 8KB) every partition came out
erasesize/blocksize (16x) too small and misaligned.

Convert partoffset and partszbytes to geo.blocksize units at the
mtd_partition() call site while keeping the erase-block accumulation.

Affected boards:
- stm32f103-minimum (W25)
- at32f437-mini (W25)
- stm32f429i-disco (SST25F064, enabled in the extflash defconfig)
- metro-m4 (SAMD5E5 progmem)

Also fix pre-existing nxstyle violations in the touched files so the
change passes checkpatch (see CONTRIBUTING.md).

Signed-off-by: rongbaichuan <rongbaichuan1027@163.com>
@github-actions github-actions Bot added Size: M The size of the change in this PR is medium and removed Size: L The size of the change in this PR is large labels Aug 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Board: arm Size: M The size of the change in this PR is medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants