9ce97b17cfa4ccd5adb979e41162733dc9949433
[openwrt/staging/stintel.git] / include / image-commands.mk
1 # Build commands that can be called from Device/* templates
2
3 IMAGE_KERNEL = $(word 1,$^)
4 IMAGE_ROOTFS = $(word 2,$^)
5
6 define ModelNameLimit16
7 $(shell printf %.16s "$(word 2, $(subst _, ,$(1)))")
8 endef
9
10 define rootfs_align
11 $(patsubst %-256k,0x40000,$(patsubst %-128k,0x20000,$(patsubst %-64k,0x10000,$(patsubst squashfs%,0x4,$(patsubst root.%,%,$(1))))))
12 endef
13
14
15 define Build/append-dtb
16 cat $(KDIR)/image-$(firstword $(DEVICE_DTS)).dtb >> $@
17 endef
18
19 define Build/append-dtb-elf
20 $(TARGET_CROSS)objcopy \
21 --set-section-flags=.appended_dtb=alloc,contents \
22 --update-section \
23 .appended_dtb=$(KDIR)/image-$(firstword $(DEVICE_DTS)).dtb $@
24 endef
25
26 define Build/append-kernel
27 dd if=$(IMAGE_KERNEL) >> $@
28 endef
29
30 define Build/package-kernel-ubifs
31 mkdir $@.kernelubifs
32 cp $@ $@.kernelubifs/kernel
33 $(STAGING_DIR_HOST)/bin/mkfs.ubifs \
34 $(KERNEL_UBIFS_OPTS) \
35 -r $@.kernelubifs $@
36 rm -r $@.kernelubifs
37 endef
38
39 define Build/append-image
40 cp "$(BIN_DIR)/$(DEVICE_IMG_PREFIX)-$(1)" "$@.stripmeta"
41 fwtool -s /dev/null -t "$@.stripmeta" || :
42 fwtool -i /dev/null -t "$@.stripmeta" || :
43 dd if="$@.stripmeta" >> "$@"
44 rm "$@.stripmeta"
45 endef
46
47 ifdef IB
48 define Build/append-image-stage
49 dd if=$(STAGING_DIR_IMAGE)/$(BOARD)$(if $(SUBTARGET),-$(SUBTARGET))-$(DEVICE_NAME)-$(1) >> $@
50 endef
51 else
52 define Build/append-image-stage
53 cp "$(BIN_DIR)/$(DEVICE_IMG_PREFIX)-$(1)" "$@.stripmeta"
54 fwtool -s /dev/null -t "$@.stripmeta" || :
55 fwtool -i /dev/null -t "$@.stripmeta" || :
56 mkdir -p "$(STAGING_DIR_IMAGE)"
57 dd if="$@.stripmeta" of="$(STAGING_DIR_IMAGE)/$(BOARD)$(if $(SUBTARGET),-$(SUBTARGET))-$(DEVICE_NAME)-$(1)"
58 dd if="$@.stripmeta" >> "$@"
59 rm "$@.stripmeta"
60 endef
61 endif
62
63
64 compat_version=$(if $(DEVICE_COMPAT_VERSION),$(DEVICE_COMPAT_VERSION),1.0)
65 json_quote=$(subst ','\'',$(subst ",\",$(1)))
66 #")')
67
68 legacy_supported_message=$(SUPPORTED_DEVICES) - Image version mismatch: image $(compat_version), \
69 device 1.0. Please wipe config during upgrade (force required) or reinstall. \
70 $(if $(DEVICE_COMPAT_MESSAGE),Reason: $(DEVICE_COMPAT_MESSAGE),Please check documentation ...)
71
72 metadata_devices=$(if $(1),$(subst "$(space)","$(comma)",$(strip $(foreach v,$(1),"$(call json_quote,$(v))"))))
73 metadata_json = \
74 '{ $(if $(IMAGE_METADATA),$(IMAGE_METADATA)$(comma)) \
75 "metadata_version": "1.1", \
76 "compat_version": "$(call json_quote,$(compat_version))", \
77 $(if $(DEVICE_COMPAT_MESSAGE),"compat_message": "$(call json_quote,$(DEVICE_COMPAT_MESSAGE))"$(comma)) \
78 $(if $(filter-out 1.0,$(compat_version)),"new_supported_devices": \
79 [$(call metadata_devices,$(SUPPORTED_DEVICES))]$(comma) \
80 "supported_devices": ["$(call json_quote,$(legacy_supported_message))"]$(comma)) \
81 $(if $(filter 1.0,$(compat_version)),"supported_devices":[$(call metadata_devices,$(SUPPORTED_DEVICES))]$(comma)) \
82 "version": { \
83 "dist": "$(call json_quote,$(VERSION_DIST))", \
84 "version": "$(call json_quote,$(VERSION_NUMBER))", \
85 "revision": "$(call json_quote,$(REVISION))", \
86 "target": "$(call json_quote,$(TARGETID))", \
87 "board": "$(call json_quote,$(if $(BOARD_NAME),$(BOARD_NAME),$(DEVICE_NAME)))" \
88 } \
89 }'
90
91 define Build/append-metadata
92 $(if $(SUPPORTED_DEVICES),-echo $(call metadata_json) | fwtool -I - $@)
93 sha256sum "$@" | cut -d" " -f1 > "$@.sha256sum"
94 [ ! -s "$(BUILD_KEY)" -o ! -s "$(BUILD_KEY).ucert" -o ! -s "$@" ] || { \
95 cp "$(BUILD_KEY).ucert" "$@.ucert" ;\
96 usign -S -m "$@" -s "$(BUILD_KEY)" -x "$@.sig" ;\
97 ucert -A -c "$@.ucert" -x "$@.sig" ;\
98 fwtool -S "$@.ucert" "$@" ;\
99 }
100 endef
101
102 define Build/append-rootfs
103 dd if=$(IMAGE_ROOTFS) >> $@
104 endef
105
106 define Build/append-squashfs-fakeroot-be
107 rm -rf $@.fakefs $@.fakesquashfs
108 mkdir $@.fakefs
109 $(STAGING_DIR_HOST)/bin/mksquashfs3-lzma \
110 $@.fakefs $@.fakesquashfs \
111 -noappend -root-owned -be -nopad -b 65536 \
112 $(if $(SOURCE_DATE_EPOCH),-fixed-time $(SOURCE_DATE_EPOCH))
113 cat $@.fakesquashfs >> $@
114 endef
115
116 define Build/append-squashfs4-fakeroot
117 rm -rf $@.fakefs $@.fakesquashfs
118 mkdir $@.fakefs
119 $(STAGING_DIR_HOST)/bin/mksquashfs4 \
120 $@.fakefs $@.fakesquashfs \
121 -nopad -noappend -root-owned
122 cat $@.fakesquashfs >> $@
123 endef
124
125 define Build/append-string
126 echo -n $(1) >> $@
127 endef
128
129 define Build/append-md5sum-ascii-salted
130 cp $@ $@.salted
131 echo -ne $(1) >> $@.salted
132 $(STAGING_DIR_HOST)/bin/mkhash md5 $@.salted | head -c32 >> $@
133 rm $@.salted
134 endef
135
136 UBI_NAND_SIZE_LIMIT = $(IMAGE_SIZE) - ($(NAND_SIZE)*20/1024 + 4*$(BLOCKSIZE))
137
138 define Build/append-ubi
139 sh $(TOPDIR)/scripts/ubinize-image.sh \
140 $(if $(UBOOTENV_IN_UBI),--uboot-env) \
141 $(foreach part,$(UBINIZE_PARTS),--part $(part)) \
142 $(if $(findstring fit,$(1)), \
143 $(if $(KERNEL_IN_UBI),--part fit=$(IMAGE_KERNEL)), \
144 $(if $(KERNEL_IN_UBI),--kernel $(IMAGE_KERNEL)) \
145 --rootfs $(IMAGE_ROOTFS)) \
146 $@.tmp \
147 -p $(BLOCKSIZE:%k=%KiB) -m $(PAGESIZE) \
148 $(if $(SUBPAGESIZE),-s $(SUBPAGESIZE)) \
149 $(if $(VID_HDR_OFFSET),-O $(VID_HDR_OFFSET)) \
150 $(UBINIZE_OPTS)
151 cat $@.tmp >> $@
152 rm $@.tmp
153 $(if $(and $(IMAGE_SIZE),$(NAND_SIZE)),\
154 $(call Build/check-size,$(UBI_NAND_SIZE_LIMIT)))
155 endef
156
157 define Build/ubinize-kernel
158 cp $@ $@.tmp
159 sh $(TOPDIR)/scripts/ubinize-image.sh \
160 --kernel $@.tmp \
161 $@ \
162 -p $(BLOCKSIZE:%k=%KiB) -m $(PAGESIZE) \
163 $(if $(SUBPAGESIZE),-s $(SUBPAGESIZE)) \
164 $(if $(VID_HDR_OFFSET),-O $(VID_HDR_OFFSET)) \
165 $(UBINIZE_OPTS)
166 rm $@.tmp
167 endef
168
169 define Build/append-uboot
170 dd if=$(UBOOT_PATH) >> $@
171 endef
172
173 # append a fake/empty uImage header, to fool bootloaders rootfs integrity check
174 # for example
175 define Build/append-uImage-fakehdr
176 $(eval type=$(word 1,$(1)))
177 $(eval magic=$(word 2,$(1)))
178 touch $@.fakehdr
179 $(STAGING_DIR_HOST)/bin/mkimage \
180 -A $(LINUX_KARCH) -O linux -T $(type) -C none \
181 -n '$(VERSION_DIST) fake $(type)' \
182 $(if $(magic),-M $(magic)) \
183 -d $@.fakehdr \
184 -s \
185 $@.fakehdr
186 cat $@.fakehdr >> $@
187 endef
188
189 define Build/buffalo-dhp-image
190 $(STAGING_DIR_HOST)/bin/mkdhpimg $@ $@.new
191 mv $@.new $@
192 endef
193
194 define Build/buffalo-enc
195 $(eval product=$(word 1,$(1)))
196 $(eval version=$(word 2,$(1)))
197 $(eval args=$(wordlist 3,$(words $(1)),$(1)))
198 $(STAGING_DIR_HOST)/bin/buffalo-enc \
199 -p $(product) -v $(version) $(args) \
200 -i $@ -o $@.new
201 mv $@.new $@
202 endef
203
204 define Build/buffalo-enc-tag
205 $(call Build/buffalo-enc,'' '' -S 152 $(1))
206 endef
207
208 define Build/buffalo-tag-dhp
209 $(eval product=$(word 1,$(1)))
210 $(eval region=$(word 2,$(1)))
211 $(eval language=$(word 3,$(1)))
212 $(STAGING_DIR_HOST)/bin/buffalo-tag \
213 -d 0x01000000 -w 1 \
214 -a $(BUFFALO_TAG_PLATFORM) \
215 -v $(BUFFALO_TAG_VERSION) -m $(BUFFALO_TAG_MINOR) \
216 -b $(product) -p $(product) \
217 -r $(region) -r $(region) -l $(language) \
218 -I $@ -o $@.new
219 mv $@.new $@
220 endef
221
222 define Build/check-size
223 @imagesize="$$(stat -c%s $@)"; \
224 limitsize="$$(($(call exp_units,$(if $(1),$(1),$(IMAGE_SIZE)))))"; \
225 [ $$limitsize -ge $$imagesize ] || { \
226 $(call ERROR_MESSAGE, WARNING: Image file $@ is too big: $$imagesize > $$limitsize); \
227 rm -f $@; \
228 }
229 endef
230
231 define Build/copy-file
232 cat "$(1)" > "$@"
233 endef
234
235 define Build/dlink-sge-image
236 $(STAGING_DIR_HOST)/bin/dlink-sge-image $(1) $@ $@.enc
237 mv $@.enc $@
238 endef
239
240 define Build/edimax-header
241 $(STAGING_DIR_HOST)/bin/mkedimaximg -i $@ -o $@.new $(1)
242 @mv $@.new $@
243 endef
244
245 define Build/elecom-product-header
246 $(eval product=$(word 1,$(1)))
247 $(eval fw=$(if $(word 2,$(1)),$(word 2,$(1)),$@))
248
249 ( \
250 echo -n -e "ELECOM\x00\x00$(product)" | dd bs=40 count=1 conv=sync; \
251 echo -n "0.00" | dd bs=16 count=1 conv=sync; \
252 dd if=$(fw); \
253 ) > $(fw).new
254 mv $(fw).new $(fw)
255 endef
256
257 define Build/elecom-wrc-gs-factory
258 $(eval product=$(word 1,$(1)))
259 $(eval version=$(word 2,$(1)))
260 $(eval hash_opt=$(word 3,$(1)))
261 $(MKHASH) md5 $(hash_opt) $@ >> $@
262 ( \
263 echo -n "ELECOM $(product) v$(version)" | \
264 dd bs=32 count=1 conv=sync; \
265 dd if=$@; \
266 ) > $@.new
267 mv $@.new $@
268 endef
269
270 define Build/elx-header
271 $(eval hw_id=$(word 1,$(1)))
272 $(eval xor_pattern=$(word 2,$(1)))
273 ( \
274 echo -ne "\x00\x00\x00\x00\x00\x00\x00\x03" | \
275 dd bs=42 count=1 conv=sync; \
276 hw_id="$(hw_id)"; \
277 echo -ne "\x$${hw_id:0:2}\x$${hw_id:2:2}\x$${hw_id:4:2}\x$${hw_id:6:2}" | \
278 dd bs=20 count=1 conv=sync; \
279 echo -ne "$$(printf '%08x' $$(stat -c%s $@) | fold -s2 | xargs -I {} echo \\x{} | tr -d '\n')" | \
280 dd bs=8 count=1 conv=sync; \
281 echo -ne "$$($(MKHASH) md5 $@ | fold -s2 | xargs -I {} echo \\x{} | tr -d '\n')" | \
282 dd bs=58 count=1 conv=sync; \
283 ) > $(KDIR)/tmp/$(DEVICE_NAME).header
284 $(call Build/xor-image,-p $(xor_pattern) -x)
285 cat $(KDIR)/tmp/$(DEVICE_NAME).header $@ > $@.new
286 mv $@.new $@
287 rm -rf $(KDIR)/tmp/$(DEVICE_NAME).header
288 endef
289
290 define Build/eva-image
291 $(STAGING_DIR_HOST)/bin/lzma2eva $(KERNEL_LOADADDR) $(KERNEL_LOADADDR) $@ $@.new
292 mv $@.new $@
293 endef
294
295 define Build/initrd_compression
296 $(if $(CONFIG_TARGET_INITRAMFS_COMPRESSION_BZIP2),.bzip2) \
297 $(if $(CONFIG_TARGET_INITRAMFS_COMPRESSION_GZIP),.gzip) \
298 $(if $(CONFIG_TARGET_INITRAMFS_COMPRESSION_LZ4),.lz4) \
299 $(if $(CONFIG_TARGET_INITRAMFS_COMPRESSION_LZMA),.lzma) \
300 $(if $(CONFIG_TARGET_INITRAMFS_COMPRESSION_LZO),.lzo) \
301 $(if $(CONFIG_TARGET_INITRAMFS_COMPRESSION_XZ),.xz) \
302 $(if $(CONFIG_TARGET_INITRAMFS_COMPRESSION_ZSTD),.zstd)
303 endef
304
305 define Build/fit
306 $(TOPDIR)/scripts/mkits.sh \
307 -D $(DEVICE_NAME) -o $@.its -k $@ \
308 -C $(word 1,$(1)) \
309 $(if $(word 2,$(1)),\
310 $(if $(findstring 11,$(if $(DEVICE_DTS_OVERLAY),1)$(if $(findstring $(KERNEL_BUILD_DIR)/image-,$(word 2,$(1))),,1)), \
311 -d $(KERNEL_BUILD_DIR)/image-$$(basename $(word 2,$(1))), \
312 -d $(word 2,$(1)))) \
313 $(if $(findstring with-rootfs,$(word 3,$(1))),-r $(IMAGE_ROOTFS)) \
314 $(if $(findstring with-initrd,$(word 3,$(1))), \
315 $(if $(CONFIG_TARGET_ROOTFS_INITRAMFS_SEPARATE), \
316 -i $(KERNEL_BUILD_DIR)/initrd.cpio$(strip $(call Build/initrd_compression)))) \
317 -a $(KERNEL_LOADADDR) -e $(if $(KERNEL_ENTRY),$(KERNEL_ENTRY),$(KERNEL_LOADADDR)) \
318 $(if $(DEVICE_FDT_NUM),-n $(DEVICE_FDT_NUM)) \
319 $(if $(DEVICE_DTS_DELIMITER),-l $(DEVICE_DTS_DELIMITER)) \
320 $(if $(DEVICE_DTS_LOADADDR),-s $(DEVICE_DTS_LOADADDR)) \
321 $(if $(DEVICE_DTS_OVERLAY),$(foreach dtso,$(DEVICE_DTS_OVERLAY), -O $(dtso):$(KERNEL_BUILD_DIR)/image-$(dtso).dtbo)) \
322 -c $(if $(DEVICE_DTS_CONFIG),$(DEVICE_DTS_CONFIG),"config-1") \
323 -A $(LINUX_KARCH) -v $(LINUX_VERSION)
324 PATH=$(LINUX_DIR)/scripts/dtc:$(PATH) mkimage $(if $(findstring external,$(word 3,$(1))),\
325 -E -B 0x1000 $(if $(findstring static,$(word 3,$(1))),-p 0x1000)) -f $@.its $@.new
326 @mv $@.new $@
327 endef
328
329 define Build/libdeflate-gzip
330 $(STAGING_DIR_HOST)/bin/libdeflate-gzip -f -12 -c $@ $(1) > $@.new
331 @mv $@.new $@
332 endef
333
334 define Build/gzip
335 $(STAGING_DIR_HOST)/bin/gzip -f -9n -c $@ $(1) > $@.new
336 @mv $@.new $@
337 endef
338
339 define Build/gzip-filename
340 @mkdir -p $@.tmp
341 @cp $@ $@.tmp/$(word 1,$(1))
342 $(if $(SOURCE_DATE_EPOCH),touch -hcd "@$(SOURCE_DATE_EPOCH)" $@.tmp/$(word 1,$(1)) $(word 2,$(1)))
343 $(STAGING_DIR_HOST)/bin/gzip -f -9 -N -c $@.tmp/$(word 1,$(1)) $(word 2,$(1)) > $@.new
344 @mv $@.new $@
345 @rm -rf $@.tmp
346 endef
347
348 define Build/install-dtb
349 $(call locked, \
350 $(foreach dts,$(DEVICE_DTS), \
351 $(CP) \
352 $(DTS_DIR)/$(dts).dtb \
353 $(BIN_DIR)/$(IMG_PREFIX)-$(dts).dtb; \
354 ), \
355 install-dtb-$(IMG_PREFIX) \
356 )
357 endef
358
359 define Build/iptime-crc32
360 $(STAGING_DIR_HOST)/bin/iptime-crc32 $(1) $@ $@.new
361 mv $@.new $@
362 endef
363
364 define Build/iptime-naspkg
365 $(STAGING_DIR_HOST)/bin/iptime-naspkg $(1) $@ $@.new
366 mv $@.new $@
367 endef
368
369 define Build/jffs2
370 rm -rf $(KDIR_TMP)/$(DEVICE_NAME)/jffs2 && \
371 mkdir -p $(KDIR_TMP)/$(DEVICE_NAME)/jffs2/$$(dirname $(1)) && \
372 cp $@ $(KDIR_TMP)/$(DEVICE_NAME)/jffs2/$(1) && \
373 $(STAGING_DIR_HOST)/bin/mkfs.jffs2 --pad \
374 $(if $(CONFIG_BIG_ENDIAN),--big-endian,--little-endian) \
375 --squash-uids -v -e $(patsubst %k,%KiB,$(BLOCKSIZE)) \
376 -o $@.new \
377 -d $(KDIR_TMP)/$(DEVICE_NAME)/jffs2 \
378 2>&1 1>/dev/null | awk '/^.+$$$$/' && \
379 $(STAGING_DIR_HOST)/bin/padjffs2 $@.new -J $(patsubst %k,,$(BLOCKSIZE))
380 -rm -rf $(KDIR_TMP)/$(DEVICE_NAME)/jffs2/
381 @mv $@.new $@
382 endef
383
384 define Build/kernel2minor
385 $(eval temp_file := $(shell mktemp))
386 cp $@ $(temp_file)
387 kernel2minor -k $(temp_file) -r $(temp_file).new $(1)
388 mv $(temp_file).new $@
389 rm -f $(temp_file)
390 endef
391
392 define Build/kernel-bin
393 rm -f $@
394 cp $< $@
395 endef
396
397 define Build/linksys-image
398 let \
399 size="$$(stat -c%s $@)" \
400 pad="$(call exp_units,$(PAGESIZE))" \
401 offset="256" \
402 pad="(pad - ((size + offset) % pad)) % pad"; \
403 dd if=/dev/zero bs=$$pad count=1 | tr '\000' '\377' >> $@
404 printf ".LINKSYS.01000409%-15s%08X%-8s%-16s" \
405 "$(call param_get_default,type,$(1),$(DEVICE_NAME))" \
406 "$$(cksum $@ | cut -d ' ' -f1)" \
407 "0" "K0000000F0246434" >> $@
408 dd if=/dev/zero bs=192 count=1 >> $@
409 endef
410
411 define Build/lzma
412 $(call Build/lzma-no-dict,-lc1 -lp2 -pb2 $(1))
413 endef
414
415 define Build/lzma-no-dict
416 $(STAGING_DIR_HOST)/bin/lzma e $@ $(1) $@.new
417 @mv $@.new $@
418 endef
419
420 define Build/moxa-encode-fw
421 $(TOPDIR)/scripts/moxa-encode-fw.py \
422 --input $@ \
423 --output $@ \
424 --magic $(MOXA_MAGIC) \
425 --hwid $(MOXA_HWID) \
426 --buildid 00000000
427 endef
428
429 define Build/netgear-chk
430 $(STAGING_DIR_HOST)/bin/mkchkimg \
431 -o $@.new \
432 -k $@ \
433 -b $(NETGEAR_BOARD_ID) \
434 $(if $(NETGEAR_REGION),-r $(NETGEAR_REGION),)
435 mv $@.new $@
436 endef
437
438 define Build/netgear-dni
439 $(STAGING_DIR_HOST)/bin/mkdniimg \
440 -B $(NETGEAR_BOARD_ID) -v $(shell cat $(VERSION_DIST)| sed -e 's/[[:space:]]/-/g').$(firstword $(subst -, ,$(REVISION))) \
441 $(if $(NETGEAR_HW_ID),-H $(NETGEAR_HW_ID)) \
442 -r "$(1)" \
443 -i $@ -o $@.new
444 mv $@.new $@
445 endef
446
447 define Build/netgear-encrypted-factory
448 $(TOPDIR)/scripts/netgear-encrypted-factory.py \
449 --input-file $@ \
450 --output-file $@ \
451 --model $(NETGEAR_ENC_MODEL) \
452 --region $(NETGEAR_ENC_REGION) \
453 $(if $(NETGEAR_ENC_HW_ID_LIST),--hw-id-list "$(NETGEAR_ENC_HW_ID_LIST)") \
454 $(if $(NETGEAR_ENC_MODEL_LIST),--model-list "$(NETGEAR_ENC_MODEL_LIST)") \
455 --version V1.0.0.0.$(shell cat $(VERSION_DIST)| sed -e 's/[[:space:]]/-/g').$(firstword $(subst -, ,$(REVISION))) \
456 --encryption-block-size 0x20000 \
457 --openssl-bin "$(STAGING_DIR_HOST)/bin/openssl" \
458 --key 6865392d342b4d212964363d6d7e7765312c7132613364316e26322a5a5e2538 \
459 --iv 4a253169516c38243d6c6d2d3b384145
460 endef
461
462 define Build/openmesh-image
463 $(TOPDIR)/scripts/om-fwupgradecfg-gen.sh \
464 "$(call param_get_default,ce_type,$(1),$(DEVICE_NAME))" \
465 "$@-fwupgrade.cfg" \
466 "$(call param_get_default,kernel,$(1),$(IMAGE_KERNEL))" \
467 "$(call param_get_default,rootfs,$(1),$@)"
468 $(TOPDIR)/scripts/combined-ext-image.sh \
469 "$(call param_get_default,ce_type,$(1),$(DEVICE_NAME))" "$@" \
470 "$@-fwupgrade.cfg" "fwupgrade.cfg" \
471 "$(call param_get_default,kernel,$(1),$(IMAGE_KERNEL))" "kernel" \
472 "$(call param_get_default,rootfs,$(1),$@)" "rootfs"
473 endef
474
475 define Build/pad-extra
476 dd if=/dev/zero bs=$(1) count=1 >> $@
477 endef
478
479 define Build/pad-offset
480 let \
481 size="$$(stat -c%s $@)" \
482 pad="$(call exp_units,$(word 1, $(1)))" \
483 offset="$(call exp_units,$(word 2, $(1)))" \
484 pad="(pad - ((size + offset) % pad)) % pad" \
485 newsize='size + pad'; \
486 dd if=$@ of=$@.new bs=$$newsize count=1 conv=sync
487 mv $@.new $@
488 endef
489
490 define Build/pad-rootfs
491 $(STAGING_DIR_HOST)/bin/padjffs2 $@ $(1) \
492 $(if $(BLOCKSIZE),$(BLOCKSIZE:%k=%),4 8 16 64 128 256)
493 endef
494
495 define Build/pad-to
496 $(call Image/pad-to,$@,$(1))
497 endef
498
499 define Build/patch-cmdline
500 $(STAGING_DIR_HOST)/bin/patch-cmdline $@ '$(CMDLINE)'
501 endef
502
503 # Convert a raw image into a $1 type image.
504 # E.g. | qemu-image vdi <optional extra arguments to qemu-img binary>
505 define Build/qemu-image
506 if command -v qemu-img; then \
507 qemu-img convert -f raw -O $1 $@ $@.new; \
508 mv $@.new $@; \
509 else \
510 echo "WARNING: Install qemu-img to create VDI/VMDK images" >&2; exit 1; \
511 fi
512 endef
513
514 define Build/qsdk-ipq-factory-nand
515 $(TOPDIR)/scripts/mkits-qsdk-ipq-image.sh \
516 $@.its ubi $@
517 PATH=$(LINUX_DIR)/scripts/dtc:$(PATH) mkimage -f $@.its $@.new
518 @mv $@.new $@
519 endef
520
521 define Build/qsdk-ipq-factory-nor
522 $(TOPDIR)/scripts/mkits-qsdk-ipq-image.sh \
523 $@.its hlos $(IMAGE_KERNEL) rootfs $(IMAGE_ROOTFS)
524 PATH=$(LINUX_DIR)/scripts/dtc:$(PATH) mkimage -f $@.its $@.new
525 @mv $@.new $@
526 endef
527
528 define Build/seama
529 $(STAGING_DIR_HOST)/bin/seama -i $@ \
530 -m "dev=/dev/mtdblock/$(SEAMA_MTDBLOCK)" -m "type=firmware"
531 mv $@.seama $@
532 endef
533
534 define Build/seama-seal
535 $(STAGING_DIR_HOST)/bin/seama -i $@ -s $@.seama \
536 -m "signature=$(SEAMA_SIGNATURE)"
537 mv $@.seama $@
538 endef
539
540 define Build/senao-header
541 $(STAGING_DIR_HOST)/bin/mksenaofw $(1) -e $@ -o $@.new
542 mv $@.new $@
543 endef
544
545 define Build/sysupgrade-tar
546 sh $(TOPDIR)/scripts/sysupgrade-tar.sh \
547 --board $(if $(BOARD_NAME),$(BOARD_NAME),$(DEVICE_NAME)) \
548 --kernel $(call param_get_default,kernel,$(1),$(IMAGE_KERNEL)) \
549 --rootfs $(call param_get_default,rootfs,$(1),$(IMAGE_ROOTFS)) \
550 $@
551 endef
552
553 define Build/tplink-safeloader
554 -$(STAGING_DIR_HOST)/bin/tplink-safeloader \
555 -B $(TPLINK_BOARD_ID) \
556 -V $(REVISION) \
557 -k $(IMAGE_KERNEL) \
558 -r $@ \
559 -o $@.new \
560 -j \
561 $(wordlist 2,$(words $(1)),$(1)) \
562 $(if $(findstring sysupgrade,$(word 1,$(1))),-S) && mv $@.new $@ || rm -f $@
563 endef
564
565 define Build/tplink-v1-header
566 $(STAGING_DIR_HOST)/bin/mktplinkfw \
567 -c -H $(TPLINK_HWID) -W $(TPLINK_HWREV) -L $(KERNEL_LOADADDR) \
568 -E $(if $(KERNEL_ENTRY),$(KERNEL_ENTRY),$(KERNEL_LOADADDR)) \
569 -m $(TPLINK_HEADER_VERSION) -N "$(VERSION_DIST)" -V $(REVISION) \
570 -k $@ -o $@.new $(1)
571 @mv $@.new $@
572 endef
573
574 # combine kernel and rootfs into one image
575 # mktplinkfw <type> <optional extra arguments to mktplinkfw binary>
576 # <type> is "sysupgrade" or "factory"
577 #
578 # -a align the rootfs start on an <align> bytes boundary
579 # -j add jffs2 end-of-filesystem markers
580 # -s strip padding from end of the image
581 # -X reserve <size> bytes in the firmware image (hexval prefixed with 0x)
582 define Build/tplink-v1-image
583 -$(STAGING_DIR_HOST)/bin/mktplinkfw \
584 -H $(TPLINK_HWID) -W $(TPLINK_HWREV) -F $(TPLINK_FLASHLAYOUT) \
585 -N "$(VERSION_DIST)" -V $(REVISION) -m $(TPLINK_HEADER_VERSION) \
586 -k $(IMAGE_KERNEL) -r $(IMAGE_ROOTFS) -o $@.new -j -X 0x40000 \
587 -a $(call rootfs_align,$(FILESYSTEM)) \
588 $(wordlist 2,$(words $(1)),$(1)) \
589 $(if $(findstring sysupgrade,$(word 1,$(1))),-s) && mv $@.new $@ || rm -f $@
590 endef
591
592 define Build/tplink-v2-header
593 $(STAGING_DIR_HOST)/bin/mktplinkfw2 \
594 -c -H $(TPLINK_HWID) -W $(TPLINK_HWREV) -L $(KERNEL_LOADADDR) \
595 -E $(if $(KERNEL_ENTRY),$(KERNEL_ENTRY),$(KERNEL_LOADADDR)) \
596 -w $(TPLINK_HWREVADD) -F "$(TPLINK_FLASHLAYOUT)" \
597 -T $(TPLINK_HVERSION) -V "ver. 2.0" \
598 -k $@ -o $@.new $(1)
599 @mv $@.new $@
600 endef
601
602 define Build/tplink-v2-image
603 $(STAGING_DIR_HOST)/bin/mktplinkfw2 \
604 -H $(TPLINK_HWID) -W $(TPLINK_HWREV) \
605 -w $(TPLINK_HWREVADD) -F "$(TPLINK_FLASHLAYOUT)" \
606 -T $(TPLINK_HVERSION) -V "ver. 2.0" -a 0x4 -j \
607 -k $(IMAGE_KERNEL) -r $(IMAGE_ROOTFS) -o $@.new $(1)
608 cat $@.new >> $@
609 rm -rf $@.new
610 endef
611
612 define Build/uImage
613 $(if $(UIMAGE_TIME),SOURCE_DATE_EPOCH="$(UIMAGE_TIME)") \
614 mkimage \
615 -A $(LINUX_KARCH) \
616 -O linux \
617 -T kernel \
618 -C $(word 1,$(1)) \
619 -a $(KERNEL_LOADADDR) \
620 -e $(if $(KERNEL_ENTRY),$(KERNEL_ENTRY),$(KERNEL_LOADADDR)) \
621 -n '$(if $(UIMAGE_NAME),$(UIMAGE_NAME),$(call toupper,$(LINUX_KARCH)) $(VERSION_DIST) Linux-$(LINUX_VERSION))' \
622 $(if $(UIMAGE_MAGIC),-M $(UIMAGE_MAGIC)) \
623 $(wordlist 2,$(words $(1)),$(1)) \
624 -d $@ $@.new
625 mv $@.new $@
626 endef
627
628 define Build/xor-image
629 $(STAGING_DIR_HOST)/bin/xorimage -i $@ -o $@.xor $(1)
630 mv $@.xor $@
631 endef
632
633 define Build/zip
634 rm -rf $@.tmp
635 mkdir $@.tmp
636 mv $@ $@.tmp/$(word 1,$(1))
637 TZ=UTC $(STAGING_DIR_HOST)/bin/zip -j -X \
638 $(wordlist 2,$(words $(1)),$(1)) \
639 $@ $@.tmp/$(if $(word 1,$(1)),$(word 1,$(1)),$$(basename $@))
640 rm -rf $@.tmp
641 endef
642
643 define Build/zyxel-ras-image
644 let \
645 newsize="$(call exp_units,$(RAS_ROOTFS_SIZE))"; \
646 $(STAGING_DIR_HOST)/bin/mkrasimage \
647 -b $(RAS_BOARD) \
648 -v $(RAS_VERSION) \
649 -r $@ \
650 -s $$newsize \
651 -o $@.new \
652 $(if $(findstring separate-kernel,$(word 1,$(1))),-k $(IMAGE_KERNEL)) \
653 && mv $@.new $@
654 endef