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