Skip to content

fix(flasher): pass detected flash size to SPI_SET_PARAMS (#41)#42

Open
dsbaars wants to merge 1 commit intotinygo-org:mainfrom
dsbaars:fix-spi-set-params-flash-size
Open

fix(flasher): pass detected flash size to SPI_SET_PARAMS (#41)#42
dsbaars wants to merge 1 commit intotinygo-org:mainfrom
dsbaars:fix-spi-set-params-flash-size

Conversation

@dsbaars
Copy link
Copy Markdown

@dsbaars dsbaars commented May 2, 2026

Fixes #41.

What's wrong

attachFlash hardcodes 4*1024*1024 in spiSetParams regardless of FlasherOptions.FlashSize or what detectFlashSize() finds on the chip. The stub uses this value as a write bound — any write past 4 MB is rejected with RESPONSE_FAILED_SPI_OP (status 0xC4).

Reproducible on ESP32-S3 with 8 MB flash by writing a partition at 0x6F0000: the first compressed block fails with command 0x11 failed: status=0xC4 error=0x00. esptool.py and esptool-js both pass the real size to SPI_SET_PARAMS for this exact reason (esptool/cmds.py:1852"the flash chip should be configured with the real size").

What this PR does

  • Adds flashSizeStringToBytes for the existing 1MB..128MB size names.
  • Splits attachFlash so the size step becomes a reusable configureSPIForSize.
  • Re-runs configureSPIForSize after detectFlashSize in FlashImages so the stub gets the real chip size before the first write.
  • Falls back to 4 MB when FlashSize is empty / keep / unrecognised — preserves prior behaviour for the unconfigured case.

Test plan

  • go test ./pkg/espflasher/ — all existing tests still pass.
  • Verified end-to-end on ESP32-S3 / 8 MB hardware that previously failed at 0x6F0000 with status=0xC4. With this patch the storage partition flashes through to MD5 verify.
  • Would appreciate a second pair of eyes for ESP8266 (the new SPI re-config is gated behind ChipType != ChipESP8266, matching the ESP8266 short-circuit at the top of attachFlash).

…ucceed

attachFlash hardcoded 4 MB in spiSetParams regardless of FlashSize / detected
chip size. The stub uses this value as a write bound, so any write past 4 MB
on a larger chip is rejected with RESPONSE_FAILED_SPI_OP (status 0xC4),
even though detectFlashSize correctly identifies the chip as 8 MB / 16 MB.

Reproducible on ESP32-S3 with an 8 MB flash by writing at offset 0x6F0000:
the first compressed block fails with `command 0x11 failed: status=0xC4`.
esptool and esptool-js both pass the actual size to SPI_SET_PARAMS for this
exact reason ("the flash chip should be configured with the real size").

This change:
  - adds flashSizeStringToBytes helper for the existing 1MB..128MB names
  - splits attachFlash so the size step is a reusable configureSPIForSize
  - re-runs configureSPIForSize after detectFlashSize in FlashImages so the
    stub gets the real chip size before the first write

Falls back to 4 MB when FlashSize is empty / "keep" / unrecognised, matching
the previous behaviour for the unconfigured case.

Fixes tinygo-org#41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

attachFlash hardcodes 4 MB, breaking writes past 4 MB on chips with larger flash

1 participant