ramips: remove factory image for TP-Link Archer C20 v1
[openwrt/openwrt.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 rootfs_align
7 $(patsubst %-256k,0x40000,$(patsubst %-128k,0x20000,$(patsubst %-64k,0x10000,$(patsubst squashfs%,0x4,$(patsubst root.%,%,$(1))))))
8 endef
9
10
11 define Build/append-dtb
12 cat $(KDIR)/image-$(firstword $(DEVICE_DTS)).dtb >> $@
13 endef
14
15 define Build/append-dtb-elf
16 $(TARGET_CROSS)objcopy \
17 --set-section-flags=.appended_dtb=alloc,contents \
18 --update-section \
19 .appended_dtb=$(KDIR)/image-$(firstword $(DEVICE_DTS)).dtb $@
20 endef
21
22 define Build/append-kernel
23 dd if=$(IMAGE_KERNEL) >> $@
24 endef
25
26 compat_version=$(if $(DEVICE_COMPAT_VERSION),$(DEVICE_COMPAT_VERSION),1.0)
27 json_quote=$(subst ','\'',$(subst ",\",$(1)))
28 #")')
29
30 legacy_supported_message=$(SUPPORTED_DEVICES) - Image version mismatch: image $(compat_version), \
31 device 1.0. Please wipe config during upgrade (force required) or reinstall. \
32 $(if $(DEVICE_COMPAT_MESSAGE),Reason: $(DEVICE_COMPAT_MESSAGE),Please check documentation ...)
33
34 metadata_devices=$(if $(1),$(subst "$(space)","$(comma)",$(strip $(foreach v,$(1),"$(call json_quote,$(v))"))))
35 metadata_json = \
36 '{ $(if $(IMAGE_METADATA),$(IMAGE_METADATA)$(comma)) \
37 "metadata_version": "1.1", \
38 "compat_version": "$(call json_quote,$(compat_version))", \
39 $(if $(DEVICE_COMPAT_MESSAGE),"compat_message": "$(call json_quote,$(DEVICE_COMPAT_MESSAGE))"$(comma)) \
40 $(if $(filter-out 1.0,$(compat_version)),"new_supported_devices": \
41 [$(call metadata_devices,$(SUPPORTED_DEVICES))]$(comma) \
42 "supported_devices": ["$(call json_quote,$(legacy_supported_message))"]$(comma)) \
43 $(if $(filter 1.0,$(compat_version)),"supported_devices":[$(call metadata_devices,$(SUPPORTED_DEVICES))]$(comma)) \
44 "version": { \
45 "dist": "$(call json_quote,$(VERSION_DIST))", \
46 "version": "$(call json_quote,$(VERSION_NUMBER))", \
47 "revision": "$(call json_quote,$(REVISION))", \
48 "target": "$(call json_quote,$(TARGETID))", \
49 "board": "$(call json_quote,$(if $(BOARD_NAME),$(BOARD_NAME),$(DEVICE_NAME)))" \
50 } \
51 }'
52
53 define Build/append-metadata
54 $(if $(SUPPORTED_DEVICES),-echo $(call metadata_json) | fwtool -I - $@)
55 [ ! -s "$(BUILD_KEY)" -o ! -s "$(BUILD_KEY).ucert" -o ! -s "$@" ] || { \
56 cp "$(BUILD_KEY).ucert" "$@.ucert" ;\
57 usign -S -m "$@" -s "$(BUILD_KEY)" -x "$@.sig" ;\
58 ucert -A -c "$@.ucert" -x "$@.sig" ;\
59 fwtool -S "$@.ucert" "$@" ;\
60 }
61 endef
62
63 define Build/append-rootfs
64 dd if=$(IMAGE_ROOTFS) >> $@
65 endef
66
67 define Build/append-squashfs-fakeroot-be
68 rm -rf $@.fakefs $@.fakesquashfs
69 mkdir $@.fakefs
70 $(STAGING_DIR_HOST)/bin/mksquashfs-lzma \
71 $@.fakefs $@.fakesquashfs \
72 -noappend -root-owned -be -nopad -b 65536 \
73 $(if $(SOURCE_DATE_EPOCH),-fixed-time $(SOURCE_DATE_EPOCH))
74 cat $@.fakesquashfs >> $@
75 endef
76
77 define Build/append-string
78 echo -n $(1) >> $@
79 endef
80
81 define Build/append-ubi
82 sh $(TOPDIR)/scripts/ubinize-image.sh \
83 $(if $(UBOOTENV_IN_UBI),--uboot-env) \
84 $(if $(KERNEL_IN_UBI),--kernel $(IMAGE_KERNEL)) \
85 $(foreach part,$(UBINIZE_PARTS),--part $(part)) \
86 $(IMAGE_ROOTFS) \
87 $@.tmp \
88 -p $(BLOCKSIZE:%k=%KiB) -m $(PAGESIZE) \
89 $(if $(SUBPAGESIZE),-s $(SUBPAGESIZE)) \
90 $(if $(VID_HDR_OFFSET),-O $(VID_HDR_OFFSET)) \
91 $(UBINIZE_OPTS)
92 cat $@.tmp >> $@
93 rm $@.tmp
94 endef
95
96 define Build/append-uboot
97 dd if=$(UBOOT_PATH) >> $@
98 endef
99
100 # append a fake/empty uImage header, to fool bootloaders rootfs integrity check
101 # for example
102 define Build/append-uImage-fakehdr
103 $(eval type=$(word 1,$(1)))
104 $(eval magic=$(word 2,$(1)))
105 touch $@.fakehdr
106 $(STAGING_DIR_HOST)/bin/mkimage \
107 -A $(LINUX_KARCH) -O linux -T $(type) -C none \
108 -n '$(VERSION_DIST) fake $(type)' \
109 $(if $(magic),-M $(magic)) \
110 -d $@.fakehdr \
111 -s \
112 $@.fakehdr
113 cat $@.fakehdr >> $@
114 endef
115
116 define Build/buffalo-dhp-image
117 $(STAGING_DIR_HOST)/bin/mkdhpimg $@ $@.new
118 mv $@.new $@
119 endef
120
121 define Build/buffalo-enc
122 $(eval product=$(word 1,$(1)))
123 $(eval version=$(word 2,$(1)))
124 $(eval args=$(wordlist 3,$(words $(1)),$(1)))
125 $(STAGING_DIR_HOST)/bin/buffalo-enc \
126 -p $(product) -v $(version) $(args) \
127 -i $@ -o $@.new
128 mv $@.new $@
129 endef
130
131 define Build/buffalo-enc-tag
132 $(call Build/buffalo-enc,'' '' -S 152 $(1))
133 endef
134
135 define Build/buffalo-tag-dhp
136 $(eval product=$(word 1,$(1)))
137 $(eval region=$(word 2,$(1)))
138 $(eval language=$(word 3,$(1)))
139 $(STAGING_DIR_HOST)/bin/buffalo-tag \
140 -d 0x01000000 -w 1 \
141 -a $(BUFFALO_TAG_PLATFORM) \
142 -v $(BUFFALO_TAG_VERSION) -m $(BUFFALO_TAG_MINOR) \
143 -b $(product) -p $(product) \
144 -r $(region) -r $(region) -l $(language) \
145 -I $@ -o $@.new
146 mv $@.new $@
147 endef
148
149 define Build/check-size
150 @imagesize="$$(stat -c%s $@)"; \
151 limitsize="$$(($(subst k,* 1024,$(subst m, * 1024k,$(if $(1),$(1),$(IMAGE_SIZE))))))"; \
152 [ $$limitsize -ge $$imagesize ] || { \
153 echo "WARNING: Image file $@ is too big: $$imagesize > $$limitsize" >&2; \
154 rm -f $@; \
155 }
156 endef
157
158 define Build/elecom-product-header
159 $(eval product=$(word 1,$(1)))
160 $(eval fw=$(if $(word 2,$(1)),$(word 2,$(1)),$@))
161
162 ( \
163 echo -n -e "ELECOM\x00\x00$(product)" | dd bs=40 count=1 conv=sync; \
164 echo -n "0.00" | dd bs=16 count=1 conv=sync; \
165 dd if=$(fw); \
166 ) > $(fw).new
167 mv $(fw).new $(fw)
168 endef
169
170 define Build/elx-header
171 $(eval hw_id=$(word 1,$(1)))
172 $(eval xor_pattern=$(word 2,$(1)))
173 ( \
174 echo -ne "\x00\x00\x00\x00\x00\x00\x00\x03" | \
175 dd bs=42 count=1 conv=sync; \
176 hw_id="$(hw_id)"; \
177 echo -ne "\x$${hw_id:0:2}\x$${hw_id:2:2}\x$${hw_id:4:2}\x$${hw_id:6:2}" | \
178 dd bs=20 count=1 conv=sync; \
179 echo -ne "$$(printf '%08x' $$(stat -c%s $@) | fold -s2 | xargs -I {} echo \\x{} | tr -d '\n')" | \
180 dd bs=8 count=1 conv=sync; \
181 echo -ne "$$($(STAGING_DIR_HOST)/bin/mkhash md5 $@ | fold -s2 | xargs -I {} echo \\x{} | tr -d '\n')" | \
182 dd bs=58 count=1 conv=sync; \
183 ) > $(KDIR)/tmp/$(DEVICE_NAME).header
184 $(call Build/xor-image,-p $(xor_pattern) -x)
185 cat $(KDIR)/tmp/$(DEVICE_NAME).header $@ > $@.new
186 mv $@.new $@
187 rm -rf $(KDIR)/tmp/$(DEVICE_NAME).header
188 endef
189
190 define Build/eva-image
191 $(STAGING_DIR_HOST)/bin/lzma2eva $(KERNEL_LOADADDR) $(KERNEL_LOADADDR) $@ $@.new
192 mv $@.new $@
193 endef
194
195 define Build/fit
196 $(TOPDIR)/scripts/mkits.sh \
197 -D $(DEVICE_NAME) -o $@.its -k $@ \
198 $(if $(word 2,$(1)),-d $(word 2,$(1))) -C $(word 1,$(1)) \
199 -a $(KERNEL_LOADADDR) -e $(if $(KERNEL_ENTRY),$(KERNEL_ENTRY),$(KERNEL_LOADADDR)) \
200 $(if $(DEVICE_FDT_NUM),-n $(DEVICE_FDT_NUM)) \
201 -c $(if $(DEVICE_DTS_CONFIG),$(DEVICE_DTS_CONFIG),"config@1") \
202 -A $(LINUX_KARCH) -v $(LINUX_VERSION)
203 PATH=$(LINUX_DIR)/scripts/dtc:$(PATH) mkimage -f $@.its $@.new
204 @mv $@.new $@
205 endef
206
207 define Build/gzip
208 gzip -f -9n -c $@ $(1) > $@.new
209 @mv $@.new $@
210 endef
211
212 define Build/install-dtb
213 $(call locked, \
214 $(foreach dts,$(DEVICE_DTS), \
215 $(CP) \
216 $(DTS_DIR)/$(dts).dtb \
217 $(BIN_DIR)/$(IMG_PREFIX)-$(dts).dtb; \
218 ), \
219 install-dtb-$(IMG_PREFIX) \
220 )
221 endef
222
223 define Build/jffs2
224 rm -rf $(KDIR_TMP)/$(DEVICE_NAME)/jffs2 && \
225 mkdir -p $(KDIR_TMP)/$(DEVICE_NAME)/jffs2/$$(dirname $(1)) && \
226 cp $@ $(KDIR_TMP)/$(DEVICE_NAME)/jffs2/$(1) && \
227 $(STAGING_DIR_HOST)/bin/mkfs.jffs2 --pad \
228 $(if $(CONFIG_BIG_ENDIAN),--big-endian,--little-endian) \
229 --squash-uids -v -e $(patsubst %k,%KiB,$(BLOCKSIZE)) \
230 -o $@.new \
231 -d $(KDIR_TMP)/$(DEVICE_NAME)/jffs2 \
232 2>&1 1>/dev/null | awk '/^.+$$$$/' && \
233 $(STAGING_DIR_HOST)/bin/padjffs2 $@.new -J $(patsubst %k,,$(BLOCKSIZE))
234 -rm -rf $(KDIR_TMP)/$(DEVICE_NAME)/jffs2/
235 @mv $@.new $@
236 endef
237
238 define Build/kernel2minor
239 kernel2minor -k $@ -r $@.new $(1)
240 mv $@.new $@
241 endef
242
243 define Build/kernel-bin
244 rm -f $@
245 cp $< $@
246 endef
247
248 define Build/linksys-image
249 $(TOPDIR)/scripts/linksys-image.sh \
250 "$(call param_get_default,type,$(1),$(DEVICE_NAME))" \
251 $@ $@.new
252 mv $@.new $@
253 endef
254
255 define Build/lzma
256 $(call Build/lzma-no-dict,-lc1 -lp2 -pb2 $(1))
257 endef
258
259 define Build/lzma-no-dict
260 $(STAGING_DIR_HOST)/bin/lzma e $@ $(1) $@.new
261 @mv $@.new $@
262 endef
263
264 define Build/netgear-chk
265 $(STAGING_DIR_HOST)/bin/mkchkimg \
266 -o $@.new \
267 -k $@ \
268 -b $(NETGEAR_BOARD_ID) \
269 $(if $(NETGEAR_REGION),-r $(NETGEAR_REGION),)
270 mv $@.new $@
271 endef
272
273 define Build/netgear-dni
274 $(STAGING_DIR_HOST)/bin/mkdniimg \
275 -B $(NETGEAR_BOARD_ID) -v $(VERSION_DIST).$(firstword $(subst -, ,$(REVISION))) \
276 $(if $(NETGEAR_HW_ID),-H $(NETGEAR_HW_ID)) \
277 -r "$(1)" \
278 -i $@ -o $@.new
279 mv $@.new $@
280 endef
281
282 define Build/openmesh-image
283 $(TOPDIR)/scripts/om-fwupgradecfg-gen.sh \
284 "$(call param_get_default,ce_type,$(1),$(DEVICE_NAME))" \
285 "$@-fwupgrade.cfg" \
286 "$(call param_get_default,kernel,$(1),$(IMAGE_KERNEL))" \
287 "$(call param_get_default,rootfs,$(1),$@)"
288 $(TOPDIR)/scripts/combined-ext-image.sh \
289 "$(call param_get_default,ce_type,$(1),$(DEVICE_NAME))" "$@" \
290 "$@-fwupgrade.cfg" "fwupgrade.cfg" \
291 "$(call param_get_default,kernel,$(1),$(IMAGE_KERNEL))" "kernel" \
292 "$(call param_get_default,rootfs,$(1),$@)" "rootfs"
293 endef
294
295 define Build/pad-extra
296 dd if=/dev/zero bs=$(1) count=1 >> $@
297 endef
298
299 define Build/pad-offset
300 let \
301 size="$$(stat -c%s $@)" \
302 pad="$(subst k,* 1024,$(word 1, $(1)))" \
303 offset="$(subst k,* 1024,$(word 2, $(1)))" \
304 pad="(pad - ((size + offset) % pad)) % pad" \
305 newsize='size + pad'; \
306 dd if=$@ of=$@.new bs=$$newsize count=1 conv=sync
307 mv $@.new $@
308 endef
309
310 define Build/pad-rootfs
311 $(STAGING_DIR_HOST)/bin/padjffs2 $@ $(1) \
312 $(if $(BLOCKSIZE),$(BLOCKSIZE:%k=%),4 8 16 64 128 256)
313 endef
314
315 define Build/pad-to
316 $(call Image/pad-to,$@,$(1))
317 endef
318
319 define Build/patch-cmdline
320 $(STAGING_DIR_HOST)/bin/patch-cmdline $@ '$(CMDLINE)'
321 endef
322
323 # Convert a raw image into a $1 type image.
324 # E.g. | qemu-image vdi
325 define Build/qemu-image
326 if command -v qemu-img; then \
327 qemu-img convert -f raw -O $1 $@ $@.new; \
328 mv $@.new $@; \
329 else \
330 echo "WARNING: Install qemu-img to create VDI/VMDK images" >&2; exit 1; \
331 fi
332 endef
333
334 define Build/qsdk-ipq-factory-nand
335 $(TOPDIR)/scripts/mkits-qsdk-ipq-image.sh \
336 $@.its ubi $@
337 PATH=$(LINUX_DIR)/scripts/dtc:$(PATH) mkimage -f $@.its $@.new
338 @mv $@.new $@
339 endef
340
341 define Build/qsdk-ipq-factory-nor
342 $(TOPDIR)/scripts/mkits-qsdk-ipq-image.sh \
343 $@.its hlos $(IMAGE_KERNEL) rootfs $(IMAGE_ROOTFS)
344 PATH=$(LINUX_DIR)/scripts/dtc:$(PATH) mkimage -f $@.its $@.new
345 @mv $@.new $@
346 endef
347
348 define Build/seama
349 $(STAGING_DIR_HOST)/bin/seama -i $@ \
350 -m "dev=/dev/mtdblock/$(SEAMA_MTDBLOCK)" -m "type=firmware"
351 mv $@.seama $@
352 endef
353
354 define Build/seama-seal
355 $(STAGING_DIR_HOST)/bin/seama -i $@ -s $@.seama \
356 -m "signature=$(SEAMA_SIGNATURE)"
357 mv $@.seama $@
358 endef
359
360 define Build/senao-header
361 $(STAGING_DIR_HOST)/bin/mksenaofw $(1) -e $@ -o $@.new
362 mv $@.new $@
363 endef
364
365 define Build/sysupgrade-tar
366 sh $(TOPDIR)/scripts/sysupgrade-tar.sh \
367 --board $(if $(BOARD_NAME),$(BOARD_NAME),$(DEVICE_NAME)) \
368 --kernel $(call param_get_default,kernel,$(1),$(IMAGE_KERNEL)) \
369 --rootfs $(call param_get_default,rootfs,$(1),$(IMAGE_ROOTFS)) \
370 $@
371 endef
372
373 define Build/tplink-safeloader
374 -$(STAGING_DIR_HOST)/bin/tplink-safeloader \
375 -B $(TPLINK_BOARD_ID) \
376 -V $(REVISION) \
377 -k $(IMAGE_KERNEL) \
378 -r $@ \
379 -o $@.new \
380 -j \
381 $(wordlist 2,$(words $(1)),$(1)) \
382 $(if $(findstring sysupgrade,$(word 1,$(1))),-S) && mv $@.new $@ || rm -f $@
383 endef
384
385 define Build/tplink-v1-header
386 $(STAGING_DIR_HOST)/bin/mktplinkfw \
387 -c -H $(TPLINK_HWID) -W $(TPLINK_HWREV) -L $(KERNEL_LOADADDR) \
388 -E $(if $(KERNEL_ENTRY),$(KERNEL_ENTRY),$(KERNEL_LOADADDR)) \
389 -m $(TPLINK_HEADER_VERSION) -N "$(VERSION_DIST)" -V $(REVISION) \
390 -k $@ -o $@.new $(1)
391 @mv $@.new $@
392 endef
393
394 # combine kernel and rootfs into one image
395 # mktplinkfw <type> <optional extra arguments to mktplinkfw binary>
396 # <type> is "sysupgrade" or "factory"
397 #
398 # -a align the rootfs start on an <align> bytes boundary
399 # -j add jffs2 end-of-filesystem markers
400 # -s strip padding from end of the image
401 # -X reserve <size> bytes in the firmware image (hexval prefixed with 0x)
402 define Build/tplink-v1-image
403 -$(STAGING_DIR_HOST)/bin/mktplinkfw \
404 -H $(TPLINK_HWID) -W $(TPLINK_HWREV) -F $(TPLINK_FLASHLAYOUT) \
405 -N "$(VERSION_DIST)" -V $(REVISION) -m $(TPLINK_HEADER_VERSION) \
406 -k $(IMAGE_KERNEL) -r $(IMAGE_ROOTFS) -o $@.new -j -X 0x40000 \
407 -a $(call rootfs_align,$(FILESYSTEM)) \
408 $(wordlist 2,$(words $(1)),$(1)) \
409 $(if $(findstring sysupgrade,$(word 1,$(1))),-s) && mv $@.new $@ || rm -f $@
410 endef
411
412 define Build/tplink-v2-header
413 $(STAGING_DIR_HOST)/bin/mktplinkfw2 \
414 -c -H $(TPLINK_HWID) -W $(TPLINK_HWREV) -L $(KERNEL_LOADADDR) \
415 -E $(if $(KERNEL_ENTRY),$(KERNEL_ENTRY),$(KERNEL_LOADADDR)) \
416 -w $(TPLINK_HWREVADD) -F "$(TPLINK_FLASHLAYOUT)" \
417 -T $(TPLINK_HVERSION) -V "ver. 2.0" \
418 -k $@ -o $@.new $(1)
419 @mv $@.new $@
420 endef
421
422 define Build/tplink-v2-image
423 $(STAGING_DIR_HOST)/bin/mktplinkfw2 \
424 -H $(TPLINK_HWID) -W $(TPLINK_HWREV) \
425 -w $(TPLINK_HWREVADD) -F "$(TPLINK_FLASHLAYOUT)" \
426 -T $(TPLINK_HVERSION) -V "ver. 2.0" -a 0x4 -j \
427 -k $(IMAGE_KERNEL) -r $(IMAGE_ROOTFS) -o $@.new $(1)
428 cat $@.new >> $@
429 rm -rf $@.new
430 endef
431
432 define Build/uImage
433 mkimage \
434 -A $(LINUX_KARCH) \
435 -O linux \
436 -T kernel \
437 -C $(word 1,$(1)) \
438 -a $(KERNEL_LOADADDR) \
439 -e $(if $(KERNEL_ENTRY),$(KERNEL_ENTRY),$(KERNEL_LOADADDR)) \
440 -n '$(if $(UIMAGE_NAME),$(UIMAGE_NAME),$(call toupper,$(LINUX_KARCH)) $(VERSION_DIST) Linux-$(LINUX_VERSION))' \
441 $(if $(UIMAGE_MAGIC),-M $(UIMAGE_MAGIC)) \
442 $(wordlist 2,$(words $(1)),$(1)) \
443 -d $@ $@.new
444 mv $@.new $@
445 endef
446
447 define Build/xor-image
448 $(STAGING_DIR_HOST)/bin/xorimage -i $@ -o $@.xor $(1)
449 mv $@.xor $@
450 endef
451
452 define Build/zip
453 mkdir $@.tmp
454 mv $@ $@.tmp/$(1)
455
456 zip -j -X \
457 $(if $(SOURCE_DATE_EPOCH),--mtime="$(SOURCE_DATE_EPOCH)") \
458 $@ $@.tmp/$(if $(1),$(1),$@)
459 rm -rf $@.tmp
460 endef
461
462 define Build/zyxel-ras-image
463 let \
464 newsize="$(subst k,* 1024,$(RAS_ROOTFS_SIZE))"; \
465 $(STAGING_DIR_HOST)/bin/mkrasimage \
466 -b $(RAS_BOARD) \
467 -v $(RAS_VERSION) \
468 -r $@ \
469 -s $$newsize \
470 -o $@.new \
471 $(if $(findstring separate-kernel,$(word 1,$(1))),-k $(IMAGE_KERNEL)) \
472 && mv $@.new $@
473 endef