build: uImage: separate arguments into individual lines
[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/eva-image
159 $(STAGING_DIR_HOST)/bin/lzma2eva $(KERNEL_LOADADDR) $(KERNEL_LOADADDR) $@ $@.new
160 mv $@.new $@
161 endef
162
163 define Build/fit
164 $(TOPDIR)/scripts/mkits.sh \
165 -D $(DEVICE_NAME) -o $@.its -k $@ \
166 $(if $(word 2,$(1)),-d $(word 2,$(1))) -C $(word 1,$(1)) \
167 -a $(KERNEL_LOADADDR) -e $(if $(KERNEL_ENTRY),$(KERNEL_ENTRY),$(KERNEL_LOADADDR)) \
168 $(if $(DEVICE_FDT_NUM),-n $(DEVICE_FDT_NUM)) \
169 -c $(if $(DEVICE_DTS_CONFIG),$(DEVICE_DTS_CONFIG),"config@1") \
170 -A $(LINUX_KARCH) -v $(LINUX_VERSION)
171 PATH=$(LINUX_DIR)/scripts/dtc:$(PATH) mkimage -f $@.its $@.new
172 @mv $@.new $@
173 endef
174
175 define Build/gzip
176 gzip -f -9n -c $@ $(1) > $@.new
177 @mv $@.new $@
178 endef
179
180 define Build/install-dtb
181 $(call locked, \
182 $(foreach dts,$(DEVICE_DTS), \
183 $(CP) \
184 $(DTS_DIR)/$(dts).dtb \
185 $(BIN_DIR)/$(IMG_PREFIX)-$(dts).dtb; \
186 ), \
187 install-dtb-$(IMG_PREFIX) \
188 )
189 endef
190
191 define Build/jffs2
192 rm -rf $(KDIR_TMP)/$(DEVICE_NAME)/jffs2 && \
193 mkdir -p $(KDIR_TMP)/$(DEVICE_NAME)/jffs2/$$(dirname $(1)) && \
194 cp $@ $(KDIR_TMP)/$(DEVICE_NAME)/jffs2/$(1) && \
195 $(STAGING_DIR_HOST)/bin/mkfs.jffs2 --pad \
196 $(if $(CONFIG_BIG_ENDIAN),--big-endian,--little-endian) \
197 --squash-uids -v -e $(patsubst %k,%KiB,$(BLOCKSIZE)) \
198 -o $@.new \
199 -d $(KDIR_TMP)/$(DEVICE_NAME)/jffs2 \
200 2>&1 1>/dev/null | awk '/^.+$$$$/' && \
201 $(STAGING_DIR_HOST)/bin/padjffs2 $@.new -J $(patsubst %k,,$(BLOCKSIZE))
202 -rm -rf $(KDIR_TMP)/$(DEVICE_NAME)/jffs2/
203 @mv $@.new $@
204 endef
205
206 define Build/kernel2minor
207 kernel2minor -k $@ -r $@.new $(1)
208 mv $@.new $@
209 endef
210
211 define Build/kernel-bin
212 rm -f $@
213 cp $< $@
214 endef
215
216 define Build/linksys-image
217 $(TOPDIR)/scripts/linksys-image.sh \
218 "$(call param_get_default,type,$(1),$(DEVICE_NAME))" \
219 $@ $@.new
220 mv $@.new $@
221 endef
222
223 define Build/lzma
224 $(call Build/lzma-no-dict,-lc1 -lp2 -pb2 $(1))
225 endef
226
227 define Build/lzma-no-dict
228 $(STAGING_DIR_HOST)/bin/lzma e $@ $(1) $@.new
229 @mv $@.new $@
230 endef
231
232 define Build/netgear-chk
233 $(STAGING_DIR_HOST)/bin/mkchkimg \
234 -o $@.new \
235 -k $@ \
236 -b $(NETGEAR_BOARD_ID) \
237 $(if $(NETGEAR_REGION),-r $(NETGEAR_REGION),)
238 mv $@.new $@
239 endef
240
241 define Build/netgear-dni
242 $(STAGING_DIR_HOST)/bin/mkdniimg \
243 -B $(NETGEAR_BOARD_ID) -v $(VERSION_DIST).$(firstword $(subst -, ,$(REVISION))) \
244 $(if $(NETGEAR_HW_ID),-H $(NETGEAR_HW_ID)) \
245 -r "$(1)" \
246 -i $@ -o $@.new
247 mv $@.new $@
248 endef
249
250 define Build/openmesh-image
251 $(TOPDIR)/scripts/om-fwupgradecfg-gen.sh \
252 "$(call param_get_default,ce_type,$(1),$(DEVICE_NAME))" \
253 "$@-fwupgrade.cfg" \
254 "$(call param_get_default,kernel,$(1),$(IMAGE_KERNEL))" \
255 "$(call param_get_default,rootfs,$(1),$@)"
256 $(TOPDIR)/scripts/combined-ext-image.sh \
257 "$(call param_get_default,ce_type,$(1),$(DEVICE_NAME))" "$@" \
258 "$@-fwupgrade.cfg" "fwupgrade.cfg" \
259 "$(call param_get_default,kernel,$(1),$(IMAGE_KERNEL))" "kernel" \
260 "$(call param_get_default,rootfs,$(1),$@)" "rootfs"
261 endef
262
263 define Build/pad-extra
264 dd if=/dev/zero bs=$(1) count=1 >> $@
265 endef
266
267 define Build/pad-offset
268 let \
269 size="$$(stat -c%s $@)" \
270 pad="$(subst k,* 1024,$(word 1, $(1)))" \
271 offset="$(subst k,* 1024,$(word 2, $(1)))" \
272 pad="(pad - ((size + offset) % pad)) % pad" \
273 newsize='size + pad'; \
274 dd if=$@ of=$@.new bs=$$newsize count=1 conv=sync
275 mv $@.new $@
276 endef
277
278 define Build/pad-rootfs
279 $(STAGING_DIR_HOST)/bin/padjffs2 $@ $(1) \
280 $(if $(BLOCKSIZE),$(BLOCKSIZE:%k=%),4 8 16 64 128 256)
281 endef
282
283 define Build/pad-to
284 $(call Image/pad-to,$@,$(1))
285 endef
286
287 define Build/patch-cmdline
288 $(STAGING_DIR_HOST)/bin/patch-cmdline $@ '$(CMDLINE)'
289 endef
290
291 # Convert a raw image into a $1 type image.
292 # E.g. | qemu-image vdi
293 define Build/qemu-image
294 qemu-img convert -f raw -O $1 $@ $@.new
295 @mv $@.new $@
296 endef
297
298 define Build/qsdk-ipq-factory-nand
299 $(TOPDIR)/scripts/mkits-qsdk-ipq-image.sh \
300 $@.its ubi $@
301 PATH=$(LINUX_DIR)/scripts/dtc:$(PATH) mkimage -f $@.its $@.new
302 @mv $@.new $@
303 endef
304
305 define Build/qsdk-ipq-factory-nor
306 $(TOPDIR)/scripts/mkits-qsdk-ipq-image.sh \
307 $@.its hlos $(IMAGE_KERNEL) rootfs $(IMAGE_ROOTFS)
308 PATH=$(LINUX_DIR)/scripts/dtc:$(PATH) mkimage -f $@.its $@.new
309 @mv $@.new $@
310 endef
311
312 define Build/seama
313 $(STAGING_DIR_HOST)/bin/seama -i $@ \
314 -m "dev=/dev/mtdblock/$(SEAMA_MTDBLOCK)" -m "type=firmware"
315 mv $@.seama $@
316 endef
317
318 define Build/seama-seal
319 $(STAGING_DIR_HOST)/bin/seama -i $@ -s $@.seama \
320 -m "signature=$(SEAMA_SIGNATURE)"
321 mv $@.seama $@
322 endef
323
324 define Build/senao-header
325 $(STAGING_DIR_HOST)/bin/mksenaofw $(1) -e $@ -o $@.new
326 mv $@.new $@
327 endef
328
329 define Build/sysupgrade-tar
330 sh $(TOPDIR)/scripts/sysupgrade-tar.sh \
331 --board $(if $(BOARD_NAME),$(BOARD_NAME),$(DEVICE_NAME)) \
332 --kernel $(call param_get_default,kernel,$(1),$(IMAGE_KERNEL)) \
333 --rootfs $(call param_get_default,rootfs,$(1),$(IMAGE_ROOTFS)) \
334 $@
335 endef
336
337 define Build/tplink-safeloader
338 -$(STAGING_DIR_HOST)/bin/tplink-safeloader \
339 -B $(TPLINK_BOARD_ID) \
340 -V $(REVISION) \
341 -k $(IMAGE_KERNEL) \
342 -r $@ \
343 -o $@.new \
344 -j \
345 $(wordlist 2,$(words $(1)),$(1)) \
346 $(if $(findstring sysupgrade,$(word 1,$(1))),-S) && mv $@.new $@ || rm -f $@
347 endef
348
349 define Build/tplink-v1-header
350 $(STAGING_DIR_HOST)/bin/mktplinkfw \
351 -c -H $(TPLINK_HWID) -W $(TPLINK_HWREV) -L $(KERNEL_LOADADDR) \
352 -E $(if $(KERNEL_ENTRY),$(KERNEL_ENTRY),$(KERNEL_LOADADDR)) \
353 -m $(TPLINK_HEADER_VERSION) -N "$(VERSION_DIST)" -V $(REVISION) \
354 -k $@ -o $@.new $(1)
355 @mv $@.new $@
356 endef
357
358 # combine kernel and rootfs into one image
359 # mktplinkfw <type> <optional extra arguments to mktplinkfw binary>
360 # <type> is "sysupgrade" or "factory"
361 #
362 # -a align the rootfs start on an <align> bytes boundary
363 # -j add jffs2 end-of-filesystem markers
364 # -s strip padding from end of the image
365 # -X reserve <size> bytes in the firmware image (hexval prefixed with 0x)
366 define Build/tplink-v1-image
367 -$(STAGING_DIR_HOST)/bin/mktplinkfw \
368 -H $(TPLINK_HWID) -W $(TPLINK_HWREV) -F $(TPLINK_FLASHLAYOUT) \
369 -N "$(VERSION_DIST)" -V $(REVISION) -m $(TPLINK_HEADER_VERSION) \
370 -k $(IMAGE_KERNEL) -r $(IMAGE_ROOTFS) -o $@.new -j -X 0x40000 \
371 -a $(call rootfs_align,$(FILESYSTEM)) \
372 $(wordlist 2,$(words $(1)),$(1)) \
373 $(if $(findstring sysupgrade,$(word 1,$(1))),-s) && mv $@.new $@ || rm -f $@
374 endef
375
376 define Build/tplink-v2-header
377 $(STAGING_DIR_HOST)/bin/mktplinkfw2 \
378 -c -H $(TPLINK_HWID) -W $(TPLINK_HWREV) -L $(KERNEL_LOADADDR) \
379 -E $(if $(KERNEL_ENTRY),$(KERNEL_ENTRY),$(KERNEL_LOADADDR)) \
380 -w $(TPLINK_HWREVADD) -F "$(TPLINK_FLASHLAYOUT)" \
381 -T $(TPLINK_HVERSION) -V "ver. 2.0" \
382 -k $@ -o $@.new $(1)
383 @mv $@.new $@
384 endef
385
386 define Build/tplink-v2-image
387 $(STAGING_DIR_HOST)/bin/mktplinkfw2 \
388 -H $(TPLINK_HWID) -W $(TPLINK_HWREV) \
389 -w $(TPLINK_HWREVADD) -F "$(TPLINK_FLASHLAYOUT)" \
390 -T $(TPLINK_HVERSION) -V "ver. 2.0" -a 0x4 -j \
391 -k $(IMAGE_KERNEL) -r $(IMAGE_ROOTFS) -o $@.new $(1)
392 cat $@.new >> $@
393 rm -rf $@.new
394 endef
395
396 define Build/uImage
397 mkimage \
398 -A $(LINUX_KARCH) \
399 -O linux \
400 -T kernel \
401 -C $(word 1,$(1)) \
402 -a $(KERNEL_LOADADDR) \
403 -e $(if $(KERNEL_ENTRY),$(KERNEL_ENTRY),$(KERNEL_LOADADDR)) \
404 -n '$(if $(UIMAGE_NAME),$(UIMAGE_NAME),$(call toupper,$(LINUX_KARCH)) $(VERSION_DIST) Linux-$(LINUX_VERSION))' \
405 $(wordlist 2,$(words $(1)),$(1)) \
406 -d $@ $@.new
407 mv $@.new $@
408 endef
409
410 define Build/xor-image
411 $(STAGING_DIR_HOST)/bin/xorimage -i $@ -o $@.xor $(1)
412 mv $@.xor $@
413 endef
414
415 define Build/zip
416 mkdir $@.tmp
417 mv $@ $@.tmp/$(1)
418
419 zip -j -X \
420 $(if $(SOURCE_DATE_EPOCH),--mtime="$(SOURCE_DATE_EPOCH)") \
421 $@ $@.tmp/$(if $(1),$(1),$@)
422 rm -rf $@.tmp
423 endef
424
425 define Build/zyxel-ras-image
426 let \
427 newsize="$(subst k,* 1024,$(RAS_ROOTFS_SIZE))"; \
428 $(STAGING_DIR_HOST)/bin/mkrasimage \
429 -b $(RAS_BOARD) \
430 -v $(RAS_VERSION) \
431 -r $@ \
432 -s $$newsize \
433 -o $@.new \
434 $(if $(findstring separate-kernel,$(word 1,$(1))),-k $(IMAGE_KERNEL)) \
435 && mv $@.new $@
436 endef