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