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