[Pkg-electronics-devel] Bug#1153568: openocd: jtagspi silently skips sector erase on 3-byte-address flashes (uninitialized always_4byte)

Vampire Star 2353187 at gmail.com
Tue Nov 10 05:09:09 GMT 2026


Package: openocd
Version: 0.12.0-4
Severity: important
Tags: patch upstream
X-Debbugs-CC: noodles at earth.li, pkg-electronics-devel at lists.alioth.debian.org

Dear maintainers,
the jtagspi driver shipped in openocd 0.12.0 silently skips sector erase
on 3-byte-address SPI flash (W25Q64 and similar) instead of reporting an
error. This corrupts data on re-flash while every command returns
success.
Root cause: struct jtagspi_flash_bank is allocated with malloc() and the
field always_4byte is only initialized inside "if (info->probed)"; on the
first probe that block is skipped and the field keeps heap garbage. When
it reads as true, jtagspi_sector_erase() sends the erase command with a
4-byte address; a 3-byte-address device ignores that command, BUSY is
never raised, jtagspi_wait() returns immediately, and openocd reports the
erase as successful in 1-3 ms instead of ~200 ms. A subsequent page
program then executes normally, and since NOR flash can only clear bits,
the stored content becomes "new AND old". On a blank chip this is
invisible (0xFF AND data == data), which is why factory flashing passes
and only re-flashing breaks.
Upstream fixed this in commit fe5ed48f40 (merged 2023-09-23), which
switched the allocation to calloc(), but there is no openocd release
after 0.12.0, so the Debian package still carries the defect. I checked
the patch series of 0.12.0-1, 0.12.0-3 and 0.12.0-4: only build fixes
(jimtcl, libgpiod, udev), no jtagspi change.
Suggested fix - one-line quilt patch, identical to the upstream commit
in isolation:
Description: jtagspi: use calloc() so always_4byte is initialized
Upstream commit fe5ed48f40. struct jtagspi_flash_bank is malloc()ed and
always_4byte is only set under "if (info->probed)", so on the first
probe it keeps heap garbage; jtagspi_sector_erase() then sends the erase
command with a 4-byte address to 3-byte-address flash, which ignores it
and openocd silently reports success.
Origin: upstream, commit fe5ed48f40
Bug-Debian: this report
Author: qiansr <2353187 at gmail.com>
Last-Update: 2026-11-10
--- a/src/flash/nor/jtagspi.c
+++ b/src/flash/nor/jtagspi.c
@@ -33,7 +33,7 @@ FLASH_BANK_COMMAND_HANDLER(jtagspi_flash_bank_command)
if (CMD_ARGC < 7)
return ERROR_COMMAND_SYNTAX_ERROR;
- info = malloc(sizeof(struct jtagspi_flash_bank));
+ info = calloc(1, sizeof(struct jtagspi_flash_bank));
if (!info) {
LOG_ERROR("no memory for flash bank info");
return ERROR_FAIL;
(That is the whole change; nothing else in the file needs touching. As
openocd 0.12.0-4 is currently marked for autoremoval due to #1152644,
this report is unrelated to that RC bug.)
Workaround for users, no rebuild needed: run
jtagspi always_4byte 0 off
after "flash probe" and before any erase, and verify every erase by
reading the sector back (expect all-FF). Exit code 0 proves nothing in
this driver.
Evidence from real hardware (CaptainDMA 75T, CH347 JTAG + BSCAN proxy +
W25Q64): with always_4byte forced off, three erase cycles took
207-228 ms and a full 2.19 MB write plus readback matched sha256; with
it on, a 1 ms fake-success and the "new AND old" corruption reproduced
deterministically: old 0xBB 0x11 0x22 0x44, wrote 0xA5, read back
0xA1 0x01 0x20 0x04 (0xBB & 0xA5 = 0xA1 etc.).
The same defect affects other distributions that build the 0.12.0
tarball (Ubuntu 24.04/26.04, Arch, Alpine, openSUSE, Homebrew, MSYS2,
xpack) and vendor forks that snapshot 0.12.x sources; Fedora 44+
already builds git snapshots and is fixed. There is also an upstream
change on Gerrit to make a dropped erase command visible
(https://review.openocd.org/c/openocd/+/10232), so that this failure
mode is not silent anymore in future releases.
Kind regards,
qiansr
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://alioth-lists.debian.net/pipermail/pkg-electronics-devel/attachments/20261110/23654b21/attachment-0001.htm>


More information about the Pkg-electronics-devel mailing list