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