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