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