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