build: fix image headers silently truncated with uutils coreutils

Build/elx-header and Build/znet-header convert a hex string into \xHH
escape sequences using `fold -s2`. This relies on the obsolete -WIDTH
option syntax of GNU fold, which parses "-s2" as "-s -2". uutils
coreutils does not support this syntax and fails with:

  error: unexpected argument '-2' found

    tip: to pass '-2' as a value, use '-- -2'

Since fold writes nothing to stdout in this case, the command
substitution expands to an empty string. The following
`dd bs=N count=1 conv=sync` then writes zero bytes, because conv=sync
only pads partial input blocks and never pads empty input. The header
field is dropped entirely, and all subsequent fields move to lower
offsets.

The build still succeeds, because the failure happens inside the
subshell which is then handed to echo.
The malformed images are only rejected when a device tries to
boot them.

For example, on the Zyxel WSM20, the missing payload size field in the
znet-header moves the 0x12345678 magic and the payload to unexpected
offsets. The bootloader then rejects the image, even though sysupgrade
accepted it.

This affects all builds on Ubuntu 25.10 and later, because these
releases use uutils coreutils by default.

Replace `fold -s2` with `fold -w2`, which is the documented way to set
the line width and produces identical output with GNU fold.

Affected devices:
- Build/znet-header is used in all images of the Zyxel WSM20 and the
  ELECOM WRC-X1800GS
- Build/elx-header is used in the factory images of the ELECOM
  WAB-I1750-PS, WAB-S1167-PS, WAB-S600-PS, WRC-2533GHBK-I and
  WRC-2533GHBK2-T, the I-O DATA WN-AC1167GR, WN-AC733GR3 and WNPR2600G,
  the Edimax RA21S and RG21S, and the D-Link DAP-X1860 A1

Signed-off-by: Grische <github@grische.xyz>
Assisted-by: Claude:claude-opus-5
Link: https://github.com/openwrt/openwrt/pull/25410
Signed-off-by: Hauke Mehrtens <hauke@hauke-m.de>
This commit is contained in:
Grische authored and Hauke Mehrtens committed 2026-10-04 16:34:06 +02:00
1 parent db9616b9af
commit 195c2ce324
2 files changed
+3 -3

No files matched your search

+2 -2
View File
@@ -407,9 +407,9 @@ define Build/elx-header
hw_id="$(hw_id)"; \
echo -ne "\x$${hw_id:0:2}\x$${hw_id:2:2}\x$${hw_id:4:2}\x$${hw_id:6:2}" | \
dd bs=20 count=1 conv=sync; \
echo -ne "$$(printf '%08x' $$(stat -c%s $@) | fold -s2 | xargs -I {} echo \\x{} | tr -d '\n')" | \
echo -ne "$$(printf '%08x' $$(stat -c%s $@) | fold -w2 | xargs -I {} echo \\x{} | tr -d '\n')" | \
dd bs=8 count=1 conv=sync; \
echo -ne "$$($(MKHASH) md5 $@ | fold -s2 | xargs -I {} echo \\x{} | tr -d '\n')" | \
echo -ne "$$($(MKHASH) md5 $@ | fold -w2 | xargs -I {} echo \\x{} | tr -d '\n')" | \
dd bs=58 count=1 conv=sync; \
) > $(KDIR)/tmp/$(DEVICE_NAME).header
-$(call Build/xor-image,-p $(xor_pattern) -x) \