mediatek: fix IPv4-only corner case and commit in 99-net-ps
[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 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                 -c $(if $(DEVICE_DTS_CONFIG),$(DEVICE_DTS_CONFIG),"config@1") \
162                 -A $(LINUX_KARCH) -v $(LINUX_VERSION)
163         PATH=$(LINUX_DIR)/scripts/dtc:$(PATH) mkimage -f $@.its $@.new
164         @mv $@.new $@
165 endef
166
167 define Build/lzma
168         $(call Build/lzma-no-dict,-lc1 -lp2 -pb2 $(1))
169 endef
170
171 define Build/lzma-no-dict
172         $(STAGING_DIR_HOST)/bin/lzma e $@ $(1) $@.new
173         @mv $@.new $@
174 endef
175
176 define Build/gzip
177         gzip -f -9n -c $@ $(1) > $@.new
178         @mv $@.new $@
179 endef
180
181 define Build/zip
182         mkdir $@.tmp
183         mv $@ $@.tmp/$(1)
184
185         zip -j -X \
186                 $(if $(SOURCE_DATE_EPOCH),--mtime="$(SOURCE_DATE_EPOCH)") \
187                 $@ $@.tmp/$(if $(1),$(1),$@)
188         rm -rf $@.tmp
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/kernel-bin
207         rm -f $@
208         cp $< $@
209 endef
210
211 define Build/patch-cmdline
212         $(STAGING_DIR_HOST)/bin/patch-cmdline $@ '$(CMDLINE)'
213 endef
214
215 define Build/append-kernel
216         dd if=$(IMAGE_KERNEL) >> $@
217 endef
218
219 define Build/append-rootfs
220         dd if=$(IMAGE_ROOTFS) >> $@
221 endef
222
223 define Build/append-ubi
224         sh $(TOPDIR)/scripts/ubinize-image.sh \
225                 $(if $(UBOOTENV_IN_UBI),--uboot-env) \
226                 $(if $(KERNEL_IN_UBI),--kernel $(IMAGE_KERNEL)) \
227                 $(foreach part,$(UBINIZE_PARTS),--part $(part)) \
228                 $(IMAGE_ROOTFS) \
229                 $@.tmp \
230                 -p $(BLOCKSIZE:%k=%KiB) -m $(PAGESIZE) \
231                 $(if $(SUBPAGESIZE),-s $(SUBPAGESIZE)) \
232                 $(if $(VID_HDR_OFFSET),-O $(VID_HDR_OFFSET)) \
233                 $(UBINIZE_OPTS)
234         cat $@.tmp >> $@
235         rm $@.tmp
236 endef
237
238 define Build/append-uboot
239         dd if=$(UBOOT_PATH) >> $@
240 endef
241
242 define Build/pad-to
243         $(call Image/pad-to,$@,$(1))
244 endef
245
246 define Build/pad-extra
247         dd if=/dev/zero bs=$(1) count=1 >> $@
248 endef
249
250 define Build/pad-rootfs
251         $(STAGING_DIR_HOST)/bin/padjffs2 $@ $(1) \
252                 $(if $(BLOCKSIZE),$(BLOCKSIZE:%k=%),4 8 16 64 128 256)
253 endef
254
255 define Build/pad-offset
256         let \
257                 size="$$(stat -c%s $@)" \
258                 pad="$(subst k,* 1024,$(word 1, $(1)))" \
259                 offset="$(subst k,* 1024,$(word 2, $(1)))" \
260                 pad="(pad - ((size + offset) % pad)) % pad" \
261                 newsize='size + pad'; \
262                 dd if=$@ of=$@.new bs=$$newsize count=1 conv=sync
263         mv $@.new $@
264 endef
265
266 define Build/xor-image
267         $(STAGING_DIR_HOST)/bin/xorimage -i $@ -o $@.xor $(1)
268         mv $@.xor $@
269 endef
270
271 define Build/check-size
272         @[ $$(($(subst k,* 1024,$(subst m, * 1024k,$(if $(1),$(1),$(IMAGE_SIZE)))))) -ge "$$(stat -c%s $@)" ] || { \
273                 echo "WARNING: Image file $@ is too big" >&2; \
274                 rm -f $@; \
275         }
276 endef
277
278 define Build/check-kernel-size
279         @[ $$(($(subst k,* 1024,$(subst m, * 1024k,$(1))))) -ge "$$(stat -c%s $(IMAGE_KERNEL))" ] || { \
280                 echo "WARNING: Kernel for $@ is too big > $(1)" >&2; \
281                 rm -f $@; \
282         }
283 endef
284
285 define Build/combined-image
286         -sh $(TOPDIR)/scripts/combined-image.sh \
287                 "$(IMAGE_KERNEL)" \
288                 "$@" \
289                 "$@.new"
290         @mv $@.new $@
291 endef
292
293 define Build/linksys-image
294         $(TOPDIR)/scripts/linksys-image.sh \
295                 "$(call param_get_default,type,$(1),$(DEVICE_NAME))" \
296                 $@ $@.new
297                 mv $@.new $@
298 endef
299
300 define Build/openmesh-image
301         $(TOPDIR)/scripts/om-fwupgradecfg-gen.sh \
302                 "$(call param_get_default,ce_type,$(1),$(DEVICE_NAME))" \
303                 "$@-fwupgrade.cfg" \
304                 "$(call param_get_default,kernel,$(1),$(IMAGE_KERNEL))" \
305                 "$(call param_get_default,rootfs,$(1),$@)"
306         $(TOPDIR)/scripts/combined-ext-image.sh \
307                 "$(call param_get_default,ce_type,$(1),$(DEVICE_NAME))" "$@" \
308                 "$@-fwupgrade.cfg" "fwupgrade.cfg" \
309                 "$(call param_get_default,kernel,$(1),$(IMAGE_KERNEL))" "kernel" \
310                 "$(call param_get_default,rootfs,$(1),$@)" "rootfs"
311 endef
312
313 define Build/qsdk-ipq-factory-nand
314         $(TOPDIR)/scripts/mkits-qsdk-ipq-image.sh \
315                 $@.its ubi $@
316         PATH=$(LINUX_DIR)/scripts/dtc:$(PATH) mkimage -f $@.its $@.new
317         @mv $@.new $@
318 endef
319
320 define Build/qsdk-ipq-factory-nor
321         $(TOPDIR)/scripts/mkits-qsdk-ipq-image.sh \
322                 $@.its hlos $(IMAGE_KERNEL) rootfs $(IMAGE_ROOTFS)
323         PATH=$(LINUX_DIR)/scripts/dtc:$(PATH) mkimage -f $@.its $@.new
324         @mv $@.new $@
325 endef
326
327 define Build/senao-header
328         $(STAGING_DIR_HOST)/bin/mksenaofw $(1) -e $@ -o $@.new
329         mv $@.new $@
330 endef
331
332 define Build/sysupgrade-tar
333         sh $(TOPDIR)/scripts/sysupgrade-tar.sh \
334                 --board $(if $(BOARD_NAME),$(BOARD_NAME),$(DEVICE_NAME)) \
335                 --kernel $(call param_get_default,kernel,$(1),$(IMAGE_KERNEL)) \
336                 --rootfs $(call param_get_default,rootfs,$(1),$(IMAGE_ROOTFS)) \
337                 $@
338 endef
339
340 define Build/tplink-v1-header
341         $(STAGING_DIR_HOST)/bin/mktplinkfw \
342                 -c -H $(TPLINK_HWID) -W $(TPLINK_HWREV) -L $(KERNEL_LOADADDR) \
343                 -E $(if $(KERNEL_ENTRY),$(KERNEL_ENTRY),$(KERNEL_LOADADDR)) \
344                 -m $(TPLINK_HEADER_VERSION) -N "$(VERSION_DIST)" -V $(REVISION) \
345                 -k $@ -o $@.new $(1)
346         @mv $@.new $@
347 endef
348
349 # combine kernel and rootfs into one image
350 # mktplinkfw <type> <optional extra arguments to mktplinkfw binary>
351 # <type> is "sysupgrade" or "factory"
352 #
353 # -a align the rootfs start on an <align> bytes boundary
354 # -j add jffs2 end-of-filesystem markers
355 # -s strip padding from end of the image
356 # -X reserve <size> bytes in the firmware image (hexval prefixed with 0x)
357 define Build/tplink-v1-image
358         -$(STAGING_DIR_HOST)/bin/mktplinkfw \
359                 -H $(TPLINK_HWID) -W $(TPLINK_HWREV) -F $(TPLINK_FLASHLAYOUT) \
360                 -N "$(VERSION_DIST)" -V $(REVISION) -m $(TPLINK_HEADER_VERSION) \
361                 -k $(IMAGE_KERNEL) -r $(IMAGE_ROOTFS) -o $@.new -j -X 0x40000 \
362                 -a $(call rootfs_align,$(FILESYSTEM)) \
363                 $(wordlist 2,$(words $(1)),$(1)) \
364                 $(if $(findstring sysupgrade,$(word 1,$(1))),-s) && mv $@.new $@ || rm -f $@
365 endef
366
367 define Build/tplink-v2-header
368         $(STAGING_DIR_HOST)/bin/mktplinkfw2 \
369                 -c -H $(TPLINK_HWID) -W $(TPLINK_HWREV) -L $(KERNEL_LOADADDR) \
370                 -E $(if $(KERNEL_ENTRY),$(KERNEL_ENTRY),$(KERNEL_LOADADDR))  \
371                 -w $(TPLINK_HWREVADD) -F "$(TPLINK_FLASHLAYOUT)" \
372                 -T $(TPLINK_HVERSION) -V "ver. 2.0" \
373                 -k $@ -o $@.new $(1)
374         @mv $@.new $@
375 endef
376
377 define Build/tplink-v2-image
378         $(STAGING_DIR_HOST)/bin/mktplinkfw2 \
379                 -H $(TPLINK_HWID) -W $(TPLINK_HWREV) \
380                 -w $(TPLINK_HWREVADD) -F "$(TPLINK_FLASHLAYOUT)" \
381                 -T $(TPLINK_HVERSION) -V "ver. 2.0" -a 0x4 -j \
382                 -k $(IMAGE_KERNEL) -r $(IMAGE_ROOTFS) -o $@.new $(1)
383         cat $@.new >> $@
384         rm -rf $@.new
385 endef
386
387 json_quote=$(subst ','\'',$(subst ",\",$(1)))
388 #")')
389 metadata_devices=$(if $(1),$(subst "$(space)","$(comma)",$(strip $(foreach v,$(1),"$(call json_quote,$(v))"))))
390 metadata_json = \
391         '{ $(if $(IMAGE_METADATA),$(IMAGE_METADATA)$(comma)) \
392                 "metadata_version": "1.0", \
393                 "supported_devices":[$(call metadata_devices,$(1))], \
394                 "version": { \
395                         "dist": "$(call json_quote,$(VERSION_DIST))", \
396                         "version": "$(call json_quote,$(VERSION_NUMBER))", \
397                         "revision": "$(call json_quote,$(REVISION))", \
398                         "target": "$(call json_quote,$(TARGETID))", \
399                         "board": "$(call json_quote,$(if $(BOARD_NAME),$(BOARD_NAME),$(DEVICE_NAME)))" \
400                 } \
401         }'
402
403 define Build/append-metadata
404         $(if $(SUPPORTED_DEVICES),-echo $(call metadata_json,$(SUPPORTED_DEVICES)) | fwtool -I - $@)
405         [ ! -s "$(BUILD_KEY)" -o ! -s "$(BUILD_KEY).ucert" -o ! -s "$@" ] || { \
406                 cp "$(BUILD_KEY).ucert" "$@.ucert" ;\
407                 usign -S -m "$@" -s "$(BUILD_KEY)" -x "$@.sig" ;\
408                 ucert -A -c "$@.ucert" -x "$@.sig" ;\
409                 fwtool -S "$@.ucert" "$@" ;\
410         }
411 endef
412
413 define Build/kernel2minor
414         kernel2minor -k $@ -r $@.new $(1)
415         mv $@.new $@
416 endef
417
418 # Convert a raw image into a $1 type image.
419 # E.g. | qemu-image vdi
420 define Build/qemu-image
421         qemu-img convert -f raw -O $1 $@ $@.new
422         @mv $@.new $@
423 endef