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