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