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