Repository navigation
zephyr-cp: fix flash scratch page bugs; use 4 KB NOR flash layout pages; fix RP2 layout - #11613
Merged
Merged
Conversation
When the RAM cache can't be allocated, flash.c stages writes in the scratch page. A block written there twice, or written after a flush filled the whole scratch page, was programmed over flash that had not been erased. FAT and directory sectors are rewritten often, so the filesystem was corrupted. Start a fresh scratch page when a block is written again, and unload the page after flushing it, as supervisor/shared/external_flash does. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Zephyr's default nRF QSPI layout page is the 64 KB erase block. flash.c reserves one page as its scratch page and caches one page in RAM while writing, so the filesystem lost 64 KB and the RAM cache needed 64 KB. Use the 4 KB sector instead. The filesystem now has 4088 blocks, like ports/nordic, so a filesystem formatted there fits. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Fill the heap so flash.c can't allocate its RAM cache, then repeatedly append to and flush files so the same directory and FAT blocks are written again while their page is staged in the scratch page. The flash simulator rejects writes to flash that isn't erased, so this fails without the scratch page fix. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
flash.c used the page holding the partition's last block as its scratch page, but it addresses pages relative to the start of the partition. When the partition doesn't start and end on page boundaries, that scratch page either runs past the end of the partition, so erasing it fails (native_sim with 64 KB erase blocks), or overlaps the last data page, which a scratch-page flush then corrupts (the nRF54LM20 DK: a 0x62b000-byte partition at 0x1d5000, with 64 KB SPI NOR layout pages). Use the last whole page instead, and leave the partial page unused. Update the test littlefs geometry to match, and add a native_sim test with a partial last page. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Like the nRF QSPI driver, the SPI NOR and MSPI NOR drivers default to 64 KB layout pages. The nRF54LM20 DK's filesystem partition (0x62b000 bytes at 0x1d5000) isn't 64 KB-aligned, so part of it was unusable, and the RAM cache needed 64 KB. Default both to their 4 KB sectors. Boards that set the size themselves (the nRF54L15 DK, nRF54H20 DK) keep it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…d time flash.c addresses erase pages relative to the start of the filesystem partition. For the NOR flash drivers, whose layout page size is a Kconfig setting, fail the build if the partition doesn't start and end on a layout page boundary. This would have caught the nRF54LM20 DK's partition with 64 KB SPI NOR layout pages. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Collaborator
Author
|
I did not mean to ask for a Copilot review. That was a mouse slip. But I'll look at it anyway. EDIT: both critiques were specious. |
Copilot stopped reviewing on behalf of
dhalbert due to an error
October 15, 2026 19:04
ports/raspberrypi caches one 4 KB flash sector in a static buffer and reserves no scratch page, so its filesystem was one page larger than zephyr-cp's on the same board, and a filesystem it formatted didn't fit. Add CIRCUITPY_FLASH_SCRATCH_PAGE, off by default on RP2040 and RP2350. With it off, flash.c allocates the RAM cache once at startup and keeps it, and the filesystem gets every whole page of the partition. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR note by @dhalbert.
Code and tests by Claude, at the direction of @dhalbert.
ports/zephyr-cp/supervisor/flash.c. This was not a problem in the non-zephyrsupervisor/shared/external_flash/external_flash.c.flash.cnow uses the last whole page as the scratch page, and leaves any partial page unused.CIRCUITPY_FLASH_SCRATCH_PAGE, which is on by default and off for RP2.This does not break existing filesystems, and they will get larger when reformatted.
Extensive testing was done on live boards and native_sim. The native_sim tests were augmented to check for the problems mentioned here.