Skip to content

Commit de24245

Browse files
committed
build: read full blocks from pipes in image headers
Many image recipes write header fields with printf or echo piped into dd bs=N count=1 conv=sync. Bash writes the output of both line buffered, so a field holding a 0x0a byte reaches the pipe in two writes. If dd reads between them, it gets a short block, pads it with zeros due to conv=sync and stops due to count=1, so the rest of the field is lost. This depends on the scheduling of the builder, and the rebuilder has caught it in a ZNET header of the Zyxel WSM20: -00000000: 5a4e 4554 80cd 7900 530a 4132 530a 0000 ZNET..y.S.A2S... +00000000: 5a4e 4554 80cd 7900 530a 4132 530a 4132 ZNET..y.S.A2S.A2 Fields holding sizes, CRCs or MD5 sums hit this at random, e.g. in elx-header, iodata-mstc-header2, seil-header or nec-fw. Text fields only hit it if they contain a newline, but they use the same pattern. dongwon-header had the same problem and no longer uses dd since commit f6ef30a ("ath79: dongwon-header: don't read the CRC from a pipe with dd"). Pass iflag=fullblock to every dd that reads such a pipe with count= or conv=sync, so dd reads until the block is full or the input ends. dd with bs=1 is left alone, as a read always returns a full block there. iflag=fullblock is supported by GNU and uutils dd, like oflag=seek_bytes already used in ramips. Build dd in tools/coreutils as well, so hosts other than Linux get GNU dd next to the GNU date and stat the images already need. Link: openwrt/openwrt#25897 Signed-off-by: Paul Spooren <mail@aparcar.org>
1 parent b050ce7 commit de24245

10 files changed

Lines changed: 38 additions & 38 deletions

File tree

‎include/image-commands.mk‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -378,8 +378,8 @@ define Build/elecom-product-header
378378
$(eval fw=$(if $(word 2,$(1)),$(word 2,$(1)),$@))
379379

380380
-( \
381-
echo -n -e "ELECOM\x00\x00$(product)" | dd bs=40 count=1 conv=sync; \
382-
echo -n "0.00" | dd bs=16 count=1 conv=sync; \
381+
echo -n -e "ELECOM\x00\x00$(product)" | dd bs=40 count=1 conv=sync iflag=fullblock; \
382+
echo -n "0.00" | dd bs=16 count=1 conv=sync iflag=fullblock; \
383383
dd if=$(fw); \
384384
) > $(fw).new \
385385
&& mv $(fw).new $(fw) || rm -f $(fw)
@@ -392,7 +392,7 @@ define Build/elecom-wrc-gs-factory
392392
$(MKHASH) md5 $(hash_opt) $@ >> $@
393393
( \
394394
echo -n "ELECOM $(product) v$(version)" | \
395-
dd bs=32 count=1 conv=sync; \
395+
dd bs=32 count=1 conv=sync iflag=fullblock; \
396396
dd if=$@; \
397397
) > $@.new
398398
mv $@.new $@
@@ -403,14 +403,14 @@ define Build/elx-header
403403
$(eval xor_pattern=$(word 2,$(1)))
404404
( \
405405
echo -ne "\x00\x00\x00\x00\x00\x00\x00\x03" | \
406-
dd bs=42 count=1 conv=sync; \
406+
dd bs=42 count=1 conv=sync iflag=fullblock; \
407407
hw_id="$(hw_id)"; \
408408
echo -ne "\x$${hw_id:0:2}\x$${hw_id:2:2}\x$${hw_id:4:2}\x$${hw_id:6:2}" | \
409-
dd bs=20 count=1 conv=sync; \
409+
dd bs=20 count=1 conv=sync iflag=fullblock; \
410410
echo -ne "$$(printf '%08x' $$(stat -c%s $@) | fold -w2 | xargs -I {} echo \\x{} | tr -d '\n')" | \
411-
dd bs=8 count=1 conv=sync; \
411+
dd bs=8 count=1 conv=sync iflag=fullblock; \
412412
echo -ne "$$($(MKHASH) md5 $@ | fold -w2 | xargs -I {} echo \\x{} | tr -d '\n')" | \
413-
dd bs=58 count=1 conv=sync; \
413+
dd bs=58 count=1 conv=sync iflag=fullblock; \
414414
) > $(KDIR)/tmp/$(DEVICE_NAME).header
415415
-$(call Build/xor-image,-p $(xor_pattern) -x) \
416416
&& cat $(KDIR)/tmp/$(DEVICE_NAME).header $@ > $@.new \

‎target/linux/ath79/image/common-buffalo.mk‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ endef
1414

1515
define Build/buffalo-tftp-header
1616
( \
17-
echo -n -e "# Airstation Public Fmt1" | dd bs=32 count=1 conv=sync; \
17+
echo -n -e "# Airstation Public Fmt1" | dd bs=32 count=1 conv=sync iflag=fullblock; \
1818
dd if=$@; \
1919
) > $@.new
2020
mv $@.new $@

‎target/linux/ath79/image/common-yuncore.mk‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ define Build/yuncore-tftp-header-16m
55
"&& saveenv" \
66
"&& erase 0x9f050000 +0xfa0000" \
77
"&& cp.b 0x800600c0 0x9f050000 0xfa0000" |\
8-
dd bs=192 count=1 conv=sync; \
8+
dd bs=192 count=1 conv=sync iflag=fullblock; \
99
dd if=$@; \
1010
) > $@.new
1111
mv $@.new $@

‎target/linux/ath79/image/generic.mk‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,10 @@ define Build/nec-enc
113113
endef
114114

115115
define Build/nec-fw
116-
( stat -c%s $@ | tr -d "\n" | dd bs=16 count=1 conv=sync; ) >> $@
116+
( stat -c%s $@ | tr -d "\n" | dd bs=16 count=1 conv=sync iflag=fullblock; ) >> $@
117117
( \
118-
echo -n -e "$(1)" | dd bs=16 count=1 conv=sync; \
119-
echo -n "0.0.00" | dd bs=16 count=1 conv=sync; \
118+
echo -n -e "$(1)" | dd bs=16 count=1 conv=sync iflag=fullblock; \
119+
echo -n "0.0.00" | dd bs=16 count=1 conv=sync iflag=fullblock; \
120120
dd if=$@; \
121121
) > $@.new
122122
mv $@.new $@

‎target/linux/mediatek/image/filogic.mk‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ define Build/mstc-header
109109
printf "$(magic)"; \
110110
tail -c+5 $@.crclen; head -c4 $@.crclen; \
111111
dd if=/dev/zero bs=4 count=2; \
112-
printf "$(version)" | dd bs=56 count=1 conv=sync 2>/dev/null; \
112+
printf "$(version)" | dd bs=56 count=1 conv=sync iflag=fullblock 2>/dev/null; \
113113
dd if=/dev/zero bs=$$((0x20000 - 0x84)) count=1 conv=sync 2>/dev/null | \
114114
tr "\0" "\377"; \
115115
cat $@; \
@@ -129,9 +129,9 @@ define Build/cetron-header
129129
$(eval model=$(word 2,$(1)))
130130
( \
131131
dd if=/dev/zero bs=856 count=1 2>/dev/null; \
132-
printf "$(model)," | dd bs=128 count=1 conv=sync 2>/dev/null; \
133-
md5sum $@ | cut -f1 -d" " | dd bs=32 count=1 2>/dev/null; \
134-
printf "$(magic)" | dd bs=4 count=1 conv=sync 2>/dev/null; \
132+
printf "$(model)," | dd bs=128 count=1 conv=sync iflag=fullblock 2>/dev/null; \
133+
md5sum $@ | cut -f1 -d" " | dd bs=32 count=1 iflag=fullblock 2>/dev/null; \
134+
printf "$(magic)" | dd bs=4 count=1 conv=sync iflag=fullblock 2>/dev/null; \
135135
cat $@; \
136136
) > $@.tmp
137137
fw_crc=$$(gzip -c $@.tmp | tail -c 8 | od -An -N4 -tx4 --endian little | tr -d ' \n'); \

‎target/linux/mvebu/image/cortexa9.mk‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ define Build/seil-header
2424
od -An -tx8 --endian little | tr -d ' \n')"; \
2525
printf "SEIL2015"; \
2626
printf "$(call toupper,$(LINUX_KARCH)) $(VERSION_DIST) Linux-$(LINUX_VERSION)" | \
27-
dd bs=80 count=1 conv=sync 2>/dev/null; \
27+
dd bs=80 count=1 conv=sync iflag=fullblock 2>/dev/null; \
2828
printf "$$(echo $${data_size_crc:8:8} | sed 's/../\\x&/g')"; \
2929
printf "\x00\x00\x00\x01\x00\x00\x00\x09\x00\x00\x00\x63"; \
30-
printf "$(REVISION)" | dd bs=32 count=1 conv=sync 2>/dev/null; \
30+
printf "$(REVISION)" | dd bs=32 count=1 conv=sync iflag=fullblock 2>/dev/null; \
3131
printf "\x00\x00\x00\x00"; \
3232
printf "$$(echo $${data_size_crc:0:8} | sed 's/../\\x&/g')"; \
3333
cat $@; \

‎target/linux/qualcommax/image/ipq50xx.mk‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ define Build/mstc-header
99
printf "CMOC"; \
1010
tail -c+5 $@.crclen; head -c4 $@.crclen; \
1111
printf '$(call toupper,$(LINUX_KARCH)) $(VERSION_DIST) Linux-$(LINUX_VERSION)' | \
12-
dd bs=64 count=1 conv=sync 2>/dev/null; \
12+
dd bs=64 count=1 conv=sync iflag=fullblock 2>/dev/null; \
1313
printf "$(version)" | \
14-
dd bs=64 count=1 conv=sync 2>/dev/null; \
14+
dd bs=64 count=1 conv=sync iflag=fullblock 2>/dev/null; \
1515
dd if=/dev/zero bs=$$(($(hdrlen) - 0x8c)) count=1 2>/dev/null; \
1616
cat $@; \
1717
) > $@.new

‎target/linux/ramips/image/mt7621.mk‎

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,14 @@ define Build/iodata-mstc-header
136136
data_size_crc="$$(dd if=$@ ibs=64 skip=1 2>/dev/null | gzip -c | \
137137
tail -c 8 | od -An -tx8 --endian little | tr -d ' \n')"; \
138138
echo -ne "$$(echo $$data_size_crc | sed 's/../\\x&/g')" | \
139-
dd of=$@ bs=8 count=1 seek=7 conv=notrunc 2>/dev/null; \
139+
dd of=$@ bs=8 count=1 seek=7 conv=notrunc iflag=fullblock 2>/dev/null; \
140140
)
141141
dd if=/dev/zero of=$@ bs=4 count=1 seek=1 conv=notrunc 2>/dev/null
142142
( \
143143
header_crc="$$(dd if=$@ bs=64 count=1 2>/dev/null | gzip -c | \
144144
tail -c 8 | od -An -N4 -tx4 --endian little | tr -d ' \n')"; \
145145
echo -ne "$$(echo $$header_crc | sed 's/../\\x&/g')" | \
146-
dd of=$@ bs=4 count=1 seek=1 conv=notrunc 2>/dev/null; \
146+
dd of=$@ bs=4 count=1 seek=1 conv=notrunc iflag=fullblock 2>/dev/null; \
147147
)
148148
endef
149149

@@ -155,18 +155,18 @@ define Build/iodata-mstc-header2
155155
fw_len_crc=$$(gzip -c $@ | tail -c 8 | \
156156
od -An -tx8 --endian little); \
157157
printf "\x03\x1d\x61\x29\x07$(model)" | \
158-
dd bs=21 count=1 conv=sync 2>/dev/null; \
159-
printf "0.00.000" | dd bs=16 count=1 conv=sync 2>/dev/null; \
158+
dd bs=21 count=1 conv=sync iflag=fullblock 2>/dev/null; \
159+
printf "0.00.000" | dd bs=16 count=1 conv=sync iflag=fullblock 2>/dev/null; \
160160
printf "$$(echo $(REVISION) | cut -d- -f1 | head -c8)" | \
161-
dd bs=9 count=1 conv=sync 2>/dev/null; \
161+
dd bs=9 count=1 conv=sync iflag=fullblock 2>/dev/null; \
162162
printf "$(call toupper,$(LINUX_KARCH)) $(VERSION_DIST) Linux-$(LINUX_VERSION)" | \
163-
dd bs=33 count=1 conv=sync 2>/dev/null; \
163+
dd bs=33 count=1 conv=sync iflag=fullblock 2>/dev/null; \
164164
date -d "@$(SOURCE_DATE_EPOCH)" "+%F" | tr -d "\n" | \
165-
dd bs=15 count=1 conv=sync 2>/dev/null; \
165+
dd bs=15 count=1 conv=sync iflag=fullblock 2>/dev/null; \
166166
printf "$$(echo $(model_id) | sed 's/../\\x&/g')" | \
167-
dd bs=8 count=1 conv=sync 2>/dev/null; \
167+
dd bs=8 count=1 conv=sync iflag=fullblock 2>/dev/null; \
168168
printf "$$(echo $$fw_len_crc | sed 's/../\\x&/g')" | \
169-
dd bs=14 count=1 conv=sync 2>/dev/null; \
169+
dd bs=14 count=1 conv=sync iflag=fullblock 2>/dev/null; \
170170
cat $@; \
171171
) > $@.new
172172
( \
@@ -188,15 +188,15 @@ define Build/znet-header
188188
payload_len="$$(dd if=$@ bs=4 count=1 skip=1 2>/dev/null | od -An -tdI --endian big | tr -d ' \n')"; \
189189
payload_size_crc="$$(dd if=$@ ibs=1 count=$$payload_len 2>/dev/null | gzip -c | \
190190
tail -c 8 | od -An -N4 -tx4 --endian big | tr -d ' \n')"; \
191-
echo -ne "$(magic)" | dd bs=4 count=1 conv=sync 2>/dev/null; \
191+
echo -ne "$(magic)" | dd bs=4 count=1 conv=sync iflag=fullblock 2>/dev/null; \
192192
echo -ne "$$(printf '%08x' $$(stat -c%s $@) | fold -w2 | xargs -I {} echo \\x{} | tac | tr -d '\n')" | \
193-
dd bs=4 count=1 conv=sync 2>/dev/null; \
193+
dd bs=4 count=1 conv=sync iflag=fullblock 2>/dev/null; \
194194
echo -ne "$$(echo $$data_size_crc | sed 's/../\\x&/g')" | \
195-
dd bs=4 count=1 conv=sync 2>/dev/null; \
195+
dd bs=4 count=1 conv=sync iflag=fullblock 2>/dev/null; \
196196
echo -ne "$$(echo $$payload_size_crc | sed 's/../\\x&/g')" | \
197-
dd bs=4 count=1 conv=sync 2>/dev/null; \
198-
echo -ne "\x12\x34\x56\x78" | dd bs=4 count=1 conv=sync 2>/dev/null; \
199-
echo -ne "$(version)" | dd bs=28 count=1 conv=sync 2>/dev/null; \
197+
dd bs=4 count=1 conv=sync iflag=fullblock 2>/dev/null; \
198+
echo -ne "\x12\x34\x56\x78" | dd bs=4 count=1 conv=sync iflag=fullblock 2>/dev/null; \
199+
echo -ne "$(version)" | dd bs=28 count=1 conv=sync iflag=fullblock 2>/dev/null; \
200200
dd if=/dev/zero bs=$$((0x40000 - $(hdrlen))) count=1 conv=sync 2>/dev/null | tr "\000" "\377"; \
201201
cat $@; \
202202
) > $@.new
@@ -216,7 +216,7 @@ define Build/belkin-header
216216
fw_len_crc=$$(gzip -c $@ | tail -c 8 | od -An -tx8 | tr -d ' \n'); \
217217
fw_crc_len="$${fw_len_crc:8:8}$${fw_len_crc:0:8}"; \
218218
\
219-
printf "$(magic)" | dd bs=4 count=1 conv=sync 2>/dev/null; \
219+
printf "$(magic)" | dd bs=4 count=1 conv=sync iflag=fullblock 2>/dev/null; \
220220
printf "$$(echo $${type_fw_date}$${hw_fw_ver} | \
221221
sed 's/../\\x&/g')"; \
222222
printf "$$(echo $${fw_crc_len}$${fw_crc_len} | \

‎target/linux/ramips/image/rt305x.mk‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
define Build/buffalo-tftp-header
55
( \
66
echo -n -e "# Airstation FirmWare\nrun u_fw\nreset\n\n" | \
7-
dd bs=512 count=1 conv=sync; \
7+
dd bs=512 count=1 conv=sync iflag=fullblock; \
88
dd if=$@; \
99
) > $@.tmp && \
1010
$(STAGING_DIR_HOST)/bin/buffalo-tftp -i $@.tmp -o $@.new

‎tools/coreutils/Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ PKG_HASH:=2bec616375002c92c1ed5ead32a092b174fe44c14bc736d32e5961053b821d84
1616

1717
HOST_BUILD_PARALLEL := 1
1818

19-
PKG_PROGRAMS:=chown cp date ginstall ln readlink realpath rmdir seq stat touch
19+
PKG_PROGRAMS:=chown cp date dd ginstall ln readlink realpath rmdir seq stat touch
2020

2121
include $(INCLUDE_DIR)/host-build.mk
2222

0 commit comments

Comments
 (0)