image: add support for overriding kernel/rootfs images in sysupgrade-tar template
[openwrt/openwrt.git] / include / image-commands.mk
1 # Build commands that can be called from Device/* templates
2
3 define Build/uImage
4 mkimage -A $(LINUX_KARCH) \
5 -O linux -T kernel \
6 -C $(1) -a $(KERNEL_LOADADDR) -e $(if $(KERNEL_ENTRY),$(KERNEL_ENTRY),$(KERNEL_LOADADDR)) \
7 -n '$(call toupper,$(LINUX_KARCH)) LEDE Linux-$(LINUX_VERSION)' -d $@ $@.new
8 @mv $@.new $@
9 endef
10
11 define Build/netgear-chk
12 $(STAGING_DIR_HOST)/bin/mkchkimg \
13 -o $@.new \
14 -k $@ \
15 -b $(NETGEAR_BOARD_ID) \
16 -r $(NETGEAR_REGION)
17 mv $@.new $@
18 endef
19
20 define Build/netgear-dni
21 $(STAGING_DIR_HOST)/bin/mkdniimg \
22 -B $(NETGEAR_BOARD_ID) -v LEDE.$(REVISION) \
23 $(if $(NETGEAR_HW_ID),-H $(NETGEAR_HW_ID)) \
24 -r "$(1)" \
25 -i $@ -o $@.new
26 mv $@.new $@
27 endef
28
29 define Build/tplink-safeloader
30 -$(STAGING_DIR_HOST)/bin/tplink-safeloader \
31 -B $(TPLINK_BOARD_NAME) \
32 -V $(REVISION) \
33 -k $(word 1,$^) \
34 -r $@ \
35 -o $@.new \
36 -j \
37 $(wordlist 2,$(words $(1)),$(1)) \
38 $(if $(findstring sysupgrade,$(word 1,$(1))),-S) && mv $@.new $@ || rm -f $@
39 endef
40
41 define Build/append-dtb
42 $(call Image/BuildDTB,$(if $(DEVICE_DTS_DIR),$(DEVICE_DTS_DIR),$(DTS_DIR))/$(DEVICE_DTS).dts,$@.dtb)
43 cat $@.dtb >> $@
44 endef
45
46 define Build/fit
47 $(TOPDIR)/scripts/mkits.sh \
48 -D $(DEVICE_NAME) -o $@.its -k $@ \
49 $(if $(word 2,$(1)),-d $(word 2,$(1))) -C $(word 1,$(1)) \
50 -a $(KERNEL_LOADADDR) -e $(if $(KERNEL_ENTRY),$(KERNEL_ENTRY),$(KERNEL_LOADADDR)) \
51 -A $(ARCH) -v $(LINUX_VERSION)
52 PATH=$(LINUX_DIR)/scripts/dtc:$(PATH) mkimage -f $@.its $@.new
53 @mv $@.new $@
54 endef
55
56 define Build/lzma
57 $(call Build/lzma-no-dict,-lc1 -lp2 -pb2 $(1))
58 endef
59
60 define Build/lzma-no-dict
61 $(STAGING_DIR_HOST)/bin/lzma e $@ $(1) $@.new
62 @mv $@.new $@
63 endef
64
65 define Build/gzip
66 gzip -9n -c $@ $(1) > $@.new
67 @mv $@.new $@
68 endef
69
70 define Build/jffs2
71 rm -rf $(KDIR_TMP)/$(DEVICE_NAME)/jffs2 && \
72 mkdir -p $(KDIR_TMP)/$(DEVICE_NAME)/jffs2/$$(dirname $(1)) && \
73 cp $@ $(KDIR_TMP)/$(DEVICE_NAME)/jffs2/$(1) && \
74 $(STAGING_DIR_HOST)/bin/mkfs.jffs2 --pad \
75 $(if $(CONFIG_BIG_ENDIAN),--big-endian,--little-endian) \
76 --squash-uids -v -e $(patsubst %k,%KiB,$(BLOCKSIZE)) \
77 -o $@.new \
78 -d $(KDIR_TMP)/$(DEVICE_NAME)/jffs2 \
79 2>&1 1>/dev/null | awk '/^.+$$$$/' && \
80 $(STAGING_DIR_HOST)/bin/padjffs2 $@.new -J $(patsubst %k,,$(BLOCKSIZE))
81 -rm -rf $(KDIR_TMP)/$(DEVICE_NAME)/jffs2/
82 @mv $@.new $@
83 endef
84
85 define Build/kernel-bin
86 rm -f $@
87 cp $< $@
88 endef
89
90 define Build/patch-cmdline
91 $(STAGING_DIR_HOST)/bin/patch-cmdline $@ '$(CMDLINE)'
92 endef
93
94 define Build/append-kernel
95 dd if=$(word 1,$^) $(if $(1),bs=$(1) conv=sync) >> $@
96 endef
97
98 define Build/append-rootfs
99 dd if=$(word 2,$^) $(if $(1),bs=$(1) conv=sync) >> $@
100 endef
101
102 define Build/append-ubi
103 sh $(TOPDIR)/scripts/ubinize-image.sh \
104 $(if $(UBOOTENV_IN_UBI),--uboot-env) \
105 $(if $(KERNEL_IN_UBI),--kernel $(word 1,$^)) \
106 $(word 2,$^) \
107 $@.tmp \
108 -p $(BLOCKSIZE) -m $(PAGESIZE) \
109 $(if $(SUBPAGESIZE),-s $(SUBPAGESIZE)) \
110 $(if $(VID_HDR_OFFSET),-O $(VID_HDR_OFFSET)) \
111 $(UBINIZE_OPTS)
112 cat $@.tmp >> $@
113 rm $@.tmp
114 endef
115
116 define Build/pad-to
117 dd if=$@ of=$@.new bs=$(1) conv=sync
118 mv $@.new $@
119 endef
120
121 define Build/pad-rootfs
122 $(STAGING_DIR_HOST)/bin/padjffs2 $@ $(1) 4 8 16 64 128 256
123 endef
124
125 define Build/pad-offset
126 let \
127 size="$$(stat -c%s $@)" \
128 pad="$(word 1, $(1))" \
129 offset="$(word 2, $(1))" \
130 pad="(pad - ((size + offset) % pad)) % pad" \
131 newsize='size + pad'; \
132 dd if=$@ of=$@.new bs=$$newsize count=1 conv=sync
133 mv $@.new $@
134 endef
135
136 define Build/check-size
137 @[ $$(($(subst k,* 1024,$(subst m, * 1024k,$(1))))) -ge "$$(stat -c%s $@)" ] || { \
138 echo "WARNING: Image file $@ is too big" >&2; \
139 rm -f $@; \
140 }
141 endef
142
143 define Build/combined-image
144 -sh $(TOPDIR)/scripts/combined-image.sh \
145 "$(word 1,$^)" \
146 "$@" \
147 "$@.new"
148 @mv $@.new $@
149 endef
150
151 define Build/sysupgrade-tar
152 sh $(TOPDIR)/scripts/sysupgrade-tar.sh \
153 --board $(if $(BOARD_NAME),$(BOARD_NAME),$(DEVICE_NAME)) \
154 --kernel $(call param_get_default,kernel,$(1),$(word 1,$^)) \
155 --rootfs $(call param_get_default,rootfs,$(1),$(word 2,$^)) \
156 $@
157 endef