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