ubus: update to latest git HEAD
[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)) LEDE 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 LEDE.$(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)) LEDE 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/install-zImage
117 $(CP) $(KDIR)/zImage \
118 $(BIN_DIR)/$(IMG_PREFIX)-$(PROFILE_SANITIZED)-zImage
119 endef
120
121 define Build/fit
122 $(TOPDIR)/scripts/mkits.sh \
123 -D $(DEVICE_NAME) -o $@.its -k $@ \
124 $(if $(word 2,$(1)),-d $(word 2,$(1))) -C $(word 1,$(1)) \
125 -a $(KERNEL_LOADADDR) -e $(if $(KERNEL_ENTRY),$(KERNEL_ENTRY),$(KERNEL_LOADADDR)) \
126 -A $(LINUX_KARCH) -v $(LINUX_VERSION)
127 PATH=$(LINUX_DIR)/scripts/dtc:$(PATH) mkimage -f $@.its $@.new
128 @mv $@.new $@
129 endef
130
131 define Build/lzma
132 $(call Build/lzma-no-dict,-lc1 -lp2 -pb2 $(1))
133 endef
134
135 define Build/lzma-no-dict
136 $(STAGING_DIR_HOST)/bin/lzma e $@ $(1) $@.new
137 @mv $@.new $@
138 endef
139
140 define Build/gzip
141 gzip -9n -c $@ $(1) > $@.new
142 @mv $@.new $@
143 endef
144
145 define Build/jffs2
146 rm -rf $(KDIR_TMP)/$(DEVICE_NAME)/jffs2 && \
147 mkdir -p $(KDIR_TMP)/$(DEVICE_NAME)/jffs2/$$(dirname $(1)) && \
148 cp $@ $(KDIR_TMP)/$(DEVICE_NAME)/jffs2/$(1) && \
149 $(STAGING_DIR_HOST)/bin/mkfs.jffs2 --pad \
150 $(if $(CONFIG_BIG_ENDIAN),--big-endian,--little-endian) \
151 --squash-uids -v -e $(patsubst %k,%KiB,$(BLOCKSIZE)) \
152 -o $@.new \
153 -d $(KDIR_TMP)/$(DEVICE_NAME)/jffs2 \
154 2>&1 1>/dev/null | awk '/^.+$$$$/' && \
155 $(STAGING_DIR_HOST)/bin/padjffs2 $@.new -J $(patsubst %k,,$(BLOCKSIZE))
156 -rm -rf $(KDIR_TMP)/$(DEVICE_NAME)/jffs2/
157 @mv $@.new $@
158 endef
159
160 define Build/kernel-bin
161 rm -f $@
162 cp $< $@
163 endef
164
165 define Build/patch-cmdline
166 $(STAGING_DIR_HOST)/bin/patch-cmdline $@ '$(CMDLINE)'
167 endef
168
169 define Build/append-kernel
170 dd if=$(IMAGE_KERNEL) >> $@
171 endef
172
173 define Build/append-rootfs
174 dd if=$(IMAGE_ROOTFS) >> $@
175 endef
176
177 define Build/append-ubi
178 sh $(TOPDIR)/scripts/ubinize-image.sh \
179 $(if $(UBOOTENV_IN_UBI),--uboot-env) \
180 $(if $(KERNEL_IN_UBI),--kernel $(IMAGE_KERNEL)) \
181 $(foreach part,$(UBINIZE_PARTS),--part $(part)) \
182 $(IMAGE_ROOTFS) \
183 $@.tmp \
184 -p $(BLOCKSIZE:%k=%KiB) -m $(PAGESIZE) \
185 $(if $(SUBPAGESIZE),-s $(SUBPAGESIZE)) \
186 $(if $(VID_HDR_OFFSET),-O $(VID_HDR_OFFSET)) \
187 $(UBINIZE_OPTS)
188 cat $@.tmp >> $@
189 rm $@.tmp
190 endef
191
192 define Build/pad-to
193 dd if=$@ of=$@.new bs=$(1) conv=sync
194 mv $@.new $@
195 endef
196
197 define Build/pad-extra
198 dd if=/dev/zero bs=$(1) count=1 >> $@
199 endef
200
201 define Build/pad-rootfs
202 $(STAGING_DIR_HOST)/bin/padjffs2 $@ $(1) \
203 $(if $(BLOCKSIZE),$(BLOCKSIZE:%k=%),4 8 16 64 128 256)
204 endef
205
206 define Build/pad-offset
207 let \
208 size="$$(stat -c%s $@)" \
209 pad="$(subst k,* 1024,$(word 1, $(1)))" \
210 offset="$(subst k,* 1024,$(word 2, $(1)))" \
211 pad="(pad - ((size + offset) % pad)) % pad" \
212 newsize='size + pad'; \
213 dd if=$@ of=$@.new bs=$$newsize count=1 conv=sync
214 mv $@.new $@
215 endef
216
217 define Build/check-size
218 @[ $$(($(subst k,* 1024,$(subst m, * 1024k,$(1))))) -ge "$$(stat -c%s $@)" ] || { \
219 echo "WARNING: Image file $@ is too big" >&2; \
220 rm -f $@; \
221 }
222 endef
223
224 define Build/combined-image
225 -sh $(TOPDIR)/scripts/combined-image.sh \
226 "$(IMAGE_KERNEL)" \
227 "$@" \
228 "$@.new"
229 @mv $@.new $@
230 endef
231
232 define Build/sysupgrade-tar
233 sh $(TOPDIR)/scripts/sysupgrade-tar.sh \
234 --board $(if $(BOARD_NAME),$(BOARD_NAME),$(DEVICE_NAME)) \
235 --kernel $(call param_get_default,kernel,$(1),$(IMAGE_KERNEL)) \
236 --rootfs $(call param_get_default,rootfs,$(1),$(IMAGE_ROOTFS)) \
237 $@
238 endef
239
240 define Build/tplink-v1-header
241 $(STAGING_DIR_HOST)/bin/mktplinkfw \
242 -c -H $(TPLINK_HWID) -W $(TPLINK_HWREV) -L $(KERNEL_LOADADDR) \
243 -E $(if $(KERNEL_ENTRY),$(KERNEL_ENTRY),$(KERNEL_LOADADDR)) \
244 -m $(TPLINK_HEADER_VERSION) -N "$(VERSION_DIST)" -V $(REVISION) \
245 -k $@ -o $@.new $(1)
246 @mv $@.new $@
247 endef
248
249 define Build/tplink-v2-header
250 $(STAGING_DIR_HOST)/bin/mktplinkfw2 \
251 -c -H $(TPLINK_HWID) -W $(TPLINK_HWREV) -L $(KERNEL_LOADADDR) \
252 -E $(if $(KERNEL_ENTRY),$(KERNEL_ENTRY),$(KERNEL_LOADADDR)) \
253 -w $(TPLINK_HWREVADD) -F "$(TPLINK_FLASHLAYOUT)" \
254 -T $(TPLINK_HVERSION) -V "ver. 2.0" \
255 -k $@ -o $@.new $(1)
256 @mv $@.new $@
257 endef
258
259 define Build/tplink-v2-image
260 $(STAGING_DIR_HOST)/bin/mktplinkfw2 \
261 -H $(TPLINK_HWID) -W $(TPLINK_HWREV) \
262 -w $(TPLINK_HWREVADD) -F "$(TPLINK_FLASHLAYOUT)" \
263 -T $(TPLINK_HVERSION) -V "ver. 2.0" -a 0x4 -j \
264 -k $(IMAGE_KERNEL) -r $(IMAGE_ROOTFS) -o $@.new $(1)
265 cat $@.new >> $@
266 rm -rf $@.new
267 endef
268
269 json_quote=$(subst ','\'',$(subst ",\",$(1)))
270 #")')
271 metadata_devices=$(if $(1),$(subst "$(space)","$(comma)",$(strip $(foreach v,$(1),"$(call json_quote,$(v))"))))
272 metadata_json = \
273 '{ $(if $(IMAGE_METADATA),$(IMAGE_METADATA)$(comma)) \
274 "supported_devices":[$(call metadata_devices,$(1))], \
275 "version": { \
276 "dist": "$(call json_quote,$(VERSION_DIST))", \
277 "version": "$(call json_quote,$(VERSION_NUMBER))", \
278 "revision": "$(call json_quote,$(REVISION))", \
279 "board": "$(call json_quote,$(BOARD))" \
280 } \
281 }'
282
283 define Build/append-metadata
284 $(if $(SUPPORTED_DEVICES),echo $(call metadata_json,$(SUPPORTED_DEVICES)) | fwtool -I - $@)
285 endef
286
287 define Build/kernel2minor
288 kernel2minor -k $@ -r $@.new $(1)
289 mv $@.new $@
290 endef