image.mk: clean up and parallelize mkfs calls
[openwrt/openwrt.git] / include / image.mk
1 #
2 # Copyright (C) 2006-2010 OpenWrt.org
3 #
4 # This is free software, licensed under the GNU General Public License v2.
5 # See /LICENSE for more information.
6 #
7
8 override TARGET_BUILD=
9 include $(INCLUDE_DIR)/prereq.mk
10 include $(INCLUDE_DIR)/kernel.mk
11 include $(INCLUDE_DIR)/host.mk
12
13 override MAKE:=$(_SINGLE)$(SUBMAKE)
14 override NO_TRACE_MAKE:=$(_SINGLE)$(NO_TRACE_MAKE)
15
16 KDIR=$(KERNEL_BUILD_DIR)
17 DTS_DIR:=$(LINUX_DIR)/arch/$(ARCH)/boot/dts/
18
19 IMG_PREFIX:=openwrt-$(BOARD)$(if $(SUBTARGET),-$(SUBTARGET))
20
21 MKFS_DEVTABLE_OPT := -D $(INCLUDE_DIR)/device_table.txt
22
23 ifneq ($(CONFIG_BIG_ENDIAN),)
24 JFFS2OPTS := --big-endian --squash-uids -v
25 else
26 JFFS2OPTS := --little-endian --squash-uids -v
27 endif
28
29 ifeq ($(CONFIG_JFFS2_RTIME),y)
30 JFFS2OPTS += -X rtime
31 endif
32 ifeq ($(CONFIG_JFFS2_ZLIB),y)
33 JFFS2OPTS += -X zlib
34 endif
35 ifeq ($(CONFIG_JFFS2_LZMA),y)
36 JFFS2OPTS += -X lzma --compression-mode=size
37 endif
38 ifneq ($(CONFIG_JFFS2_RTIME),y)
39 JFFS2OPTS += -x rtime
40 endif
41 ifneq ($(CONFIG_JFFS2_ZLIB),y)
42 JFFS2OPTS += -x zlib
43 endif
44 ifneq ($(CONFIG_JFFS2_LZMA),y)
45 JFFS2OPTS += -x lzma
46 endif
47
48 JFFS2OPTS += $(MKFS_DEVTABLE_OPT)
49
50 SQUASHFS_BLOCKSIZE := $(CONFIG_TARGET_SQUASHFS_BLOCK_SIZE)k
51 SQUASHFSOPT := -b $(SQUASHFS_BLOCKSIZE)
52 SQUASHFSOPT += -p '/dev d 755 0 0' -p '/dev/console c 600 0 0 5 1'
53 SQUASHFSCOMP := gzip
54 LZMA_XZ_OPTIONS := -Xpreset 9 -Xe -Xlc 0 -Xlp 2 -Xpb 2
55 ifeq ($(CONFIG_SQUASHFS_LZMA),y)
56 SQUASHFSCOMP := lzma $(LZMA_XZ_OPTIONS)
57 endif
58 ifeq ($(CONFIG_SQUASHFS_XZ),y)
59 ifneq ($(filter arm x86 powerpc sparc,$(LINUX_KARCH)),)
60 BCJ_FILTER:=-Xbcj $(LINUX_KARCH)
61 endif
62 SQUASHFSCOMP := xz $(LZMA_XZ_OPTIONS) $(BCJ_FILTER)
63 endif
64
65 JFFS2_BLOCKSIZE ?= 64k 128k
66
67 fs-types-$(CONFIG_TARGET_ROOTFS_SQUASHFS) += squashfs
68 fs-types-$(CONFIG_TARGET_ROOTFS_JFFS2) += $(addprefix jffs2-,$(JFFS2_BLOCKSIZE))
69 fs-types-$(CONFIG_TARGET_ROOTFS_JFFS2_NAND) += $(addprefix jffs2-nand-,$(NAND_BLOCKSIZE))
70 fs-types-$(CONFIG_TARGET_ROOTFS_EXT4FS) += ext4
71 fs-types-$(CONFIG_TARGET_ROOTFS_ISO) += iso
72 TARGET_FILESYSTEMS := $(fs-types-y)
73
74 define add_jffs2_mark
75 echo -ne '\xde\xad\xc0\xde' >> $(1)
76 endef
77
78 define toupper
79 $(shell echo $(1) | tr '[:lower:]' '[:upper:]')
80 endef
81
82 # pad to 4k, 8k, 16k, 64k, 128k, 256k and add jffs2 end-of-filesystem mark
83 define prepare_generic_squashfs
84 $(STAGING_DIR_HOST)/bin/padjffs2 $(1) 4 8 16 64 128 256
85 endef
86
87 define Image/BuildKernel/Initramfs
88 cp $(KDIR)/vmlinux-initramfs.elf $(BIN_DIR)/$(IMG_PREFIX)-vmlinux-initramfs.elf
89 $(call Image/Build/Initramfs)
90 endef
91
92 define Image/BuildKernel/MkuImage
93 mkimage -A $(ARCH) -O linux -T kernel -C $(1) -a $(2) -e $(3) \
94 -n '$(call toupper,$(ARCH)) OpenWrt Linux-$(LINUX_VERSION)' -d $(4) $(5)
95 endef
96
97 define Image/BuildKernel/MkFIT
98 $(TOPDIR)/scripts/mkits.sh \
99 -D $(1) -o $(KDIR)/fit-$(1).its -k $(2) -d $(3) -C $(4) -a $(5) -e $(6) \
100 -A $(ARCH) -v $(LINUX_VERSION)
101 PATH=$(LINUX_DIR)/scripts/dtc:$(PATH) mkimage -f $(KDIR)/fit-$(1).its $(KDIR)/fit-$(1)$(7).itb
102 endef
103
104 define Image/mkfs/jffs2/sub
105 # FIXME: removing this line will cause strange behaviour in the foreach loop below
106 $(STAGING_DIR_HOST)/bin/mkfs.jffs2 $(3) --pad -e $(patsubst %k,%KiB,$(1)) -o $(KDIR)/root.jffs2-$(2) -d $(TARGET_DIR) -v 2>&1 1>/dev/null | awk '/^.+$$$$/'
107 $(STAGING_DIR_HOST)/bin/mkfs.jffs2 $(3) -e $(patsubst %k,%KiB,$(1)) -o $(KDIR)/root.jffs2-$(2)-raw -d $(TARGET_DIR) -v 2>&1 1>/dev/null | awk '/^.+$$$$/'
108 $(call add_jffs2_mark,$(KDIR)/root.jffs2-$(2))
109 endef
110
111 define Image/mkfs/jffs2/template
112 Image/mkfs/jffs2-$(1) = $$(call Image/mkfs/jffs2/sub,$(1),$(1),$(JFFS2OPTS))
113
114 endef
115
116 define Image/mkfs/jffs2-nand/template
117 Image/mkfs/jffs2-nand-$(1) = \
118 $$(call Image/mkfs/jffs2/sub, \
119 $(word 2,$(subst -, ,$(1))),nand-$(1), \
120 $(JFFS2OPTS) --no-cleanmarkers --pagesize=$(word 1,$(subst -, ,$(1))))
121
122 endef
123
124 $(eval $(foreach S,$(JFFS2_BLOCKSIZE),$(call Image/mkfs/jffs2/template,$(S))))
125 $(eval $(foreach S,$(NAND_BLOCKSIZE),$(call Image/mkfs/jffs2-nand/template,$(S))))
126
127 define Image/mkfs/squashfs
128 $(STAGING_DIR_HOST)/bin/mksquashfs4 $(TARGET_DIR) $(KDIR)/root.squashfs -nopad -noappend -root-owned -comp $(SQUASHFSCOMP) $(SQUASHFSOPT) -processors $(if $(CONFIG_PKG_BUILD_JOBS),$(CONFIG_PKG_BUILD_JOBS),1)
129 endef
130
131 # $(1): board name
132 # $(2): rootfs type
133 # $(3): kernel image
134 ifneq ($(CONFIG_NAND_SUPPORT),)
135 define Image/Build/SysupgradeNAND
136 mkdir -p "$(KDIR_TMP)/sysupgrade-$(1)/"
137 echo "BOARD=$(1)" > "$(KDIR_TMP)/sysupgrade-$(1)/CONTROL"
138 [ -z "$(2)" ] || $(CP) "$(KDIR)/root.$(2)" "$(KDIR_TMP)/sysupgrade-$(1)/root"
139 [ -z "$(3)" ] || $(CP) "$(3)" "$(KDIR_TMP)/sysupgrade-$(1)/kernel"
140 (cd "$(KDIR_TMP)"; $(TAR) cvf \
141 "$(BIN_DIR)/$(IMG_PREFIX)-$(1)-$(2)-sysupgrade.tar" sysupgrade-$(1))
142 endef
143 # $(1) board name
144 # $(2) ubinize-image options (e.g. --uboot-env and/or --kernel kernelimage)
145 # $(3) rootfstype (e.g. squashfs or ubifs)
146 # $(4) options to pass-through to ubinize (i.e. $($(PROFILE)_UBI_OPTS)))
147 define Image/Build/UbinizeImage
148 sh $(TOPDIR)/scripts/ubinize-image.sh $(2) \
149 "$(KDIR)/root.$(3)" \
150 "$(BIN_DIR)/$(IMG_PREFIX)-$(1)-$(3)-ubinized.bin" \
151 $(4)
152 endef
153
154 endif
155
156 ifneq ($(CONFIG_TARGET_ROOTFS_UBIFS),)
157 define Image/mkfs/ubifs/generate
158 $(CP) ./ubinize$(1).cfg $(KDIR)
159 ( cd $(KDIR); \
160 $(STAGING_DIR_HOST)/bin/ubinize \
161 $(if $($(PROFILE)_UBI_OPTS), \
162 $(shell echo $($(PROFILE)_UBI_OPTS)), \
163 $(shell echo $(UBI_OPTS)) \
164 ) \
165 -o $(KDIR)/root$(1).ubi \
166 ubinize$(1).cfg \
167 )
168 endef
169
170 define Image/mkfs/ubifs
171
172 ifneq ($($(PROFILE)_UBIFS_OPTS)$(UBIFS_OPTS),)
173 $(STAGING_DIR_HOST)/bin/mkfs.ubifs \
174 $(if $($(PROFILE)_UBIFS_OPTS), \
175 $(shell echo $($(PROFILE)_UBIFS_OPTS)), \
176 $(shell echo $(UBIFS_OPTS)) \
177 ) \
178 $(if $(CONFIG_TARGET_UBIFS_FREE_SPACE_FIXUP),--space-fixup) \
179 $(if $(CONFIG_TARGET_UBIFS_COMPRESSION_NONE),--force-compr=none) \
180 $(if $(CONFIG_TARGET_UBIFS_COMPRESSION_LZO),--force-compr=lzo) \
181 $(if $(CONFIG_TARGET_UBIFS_COMPRESSION_ZLIB),--force-compr=zlib) \
182 $(if $(shell echo $(CONFIG_TARGET_UBIFS_JOURNAL_SIZE)),--jrn-size=$(CONFIG_TARGET_UBIFS_JOURNAL_SIZE)) \
183 --squash-uids \
184 -o $(KDIR)/root.ubifs \
185 -d $(TARGET_DIR)
186 endif
187 $(call Image/Build,ubifs)
188
189 ifneq ($($(PROFILE)_UBI_OPTS)$(UBI_OPTS),)
190 $(call Image/mkfs/ubifs/generate,)
191 $(if $(wildcard ./ubinize-overlay.cfg),$(call Image/mkfs/ubifs/generate,-overlay))
192 endif
193 $(call Image/Build,ubi)
194 endef
195 endif
196
197 ifneq ($(CONFIG_TARGET_ROOTFS_CPIOGZ),)
198 define Image/mkfs/cpiogz
199 ( cd $(TARGET_DIR); find . | cpio -o -H newc | gzip -9 >$(BIN_DIR)/$(IMG_PREFIX)-rootfs.cpio.gz )
200 endef
201 endif
202
203 ifneq ($(CONFIG_TARGET_ROOTFS_TARGZ),)
204 define Image/mkfs/targz
205 # Preserve permissions (-p) when building as non-root user
206 $(TAR) -czpf $(BIN_DIR)/$(IMG_PREFIX)$(if $(PROFILE),-$(PROFILE))-rootfs.tar.gz --numeric-owner --owner=0 --group=0 -C $(TARGET_DIR)/ .
207 endef
208 endif
209
210 E2SIZE=$(shell echo $$(($(CONFIG_TARGET_ROOTFS_PARTSIZE)*1024*1024/$(CONFIG_TARGET_EXT4_BLOCKSIZE))))
211
212 define Image/mkfs/ext4
213 # generate an ext2 fs
214 $(STAGING_DIR_HOST)/bin/genext2fs -U -B $(CONFIG_TARGET_EXT4_BLOCKSIZE) -b $(E2SIZE) -N $(CONFIG_TARGET_EXT4_MAXINODE) -d $(TARGET_DIR)/ $(KDIR)/root.ext4 -m $(CONFIG_TARGET_EXT4_RESERVED_PCT) $(MKFS_DEVTABLE_OPT)
215 # convert it to ext4
216 $(STAGING_DIR_HOST)/bin/tune2fs $(if $(CONFIG_TARGET_EXT4_JOURNAL),-j) -O extents,uninit_bg,dir_index $(KDIR)/root.ext4
217 # fix it up
218 $(STAGING_DIR_HOST)/bin/e2fsck -fy $(KDIR)/root.ext4
219 endef
220
221 define Image/mkfs/prepare/default
222 # Use symbolic permissions to avoid clobbering SUID/SGID/sticky bits
223 - $(FIND) $(TARGET_DIR) -type f -not -perm +0100 -not -name 'ssh_host*' -not -name 'shadow' -print0 | $(XARGS) -0 chmod u+rw,g+r,o+r
224 - $(FIND) $(TARGET_DIR) -type f -perm +0100 -print0 | $(XARGS) -0 chmod u+rwx,g+rx,o+rx
225 - $(FIND) $(TARGET_DIR) -type d -print0 | $(XARGS) -0 chmod u+rwx,g+rx,o+rx
226 $(INSTALL_DIR) $(TARGET_DIR)/tmp $(TARGET_DIR)/overlay
227 chmod 1777 $(TARGET_DIR)/tmp
228 endef
229
230 define Image/mkfs/prepare
231 $(call Image/mkfs/prepare/default)
232 endef
233
234
235 define Image/Checksum
236 ( cd ${BIN_DIR} ; \
237 $(FIND) -maxdepth 1 -type f \! -name 'md5sums' -printf "%P\n" | sort | xargs \
238 md5sum --binary > md5sums \
239 )
240 endef
241
242 define BuildImage/mkfs
243 install: mkfs-$(1)
244 .PHONY: mkfs-$(1)
245 mkfs-$(1): mkfs_prepare
246 $(Image/mkfs/$(1))
247
248 endef
249
250 define BuildImage
251
252 download:
253 prepare:
254 compile:
255 clean:
256 image_prepare:
257
258 ifeq ($(IB),)
259 .PHONY: download prepare compile clean image_prepare mkfs_prepare kernel_prepare install
260 compile: compile-targets
261 $(call Build/Compile)
262
263 clean: clean-targets
264 $(call Build/Clean)
265
266 image_prepare: compile
267 $(call Image/Prepare)
268 endif
269
270 mkfs_prepare: image_prepare
271 $(call Image/mkfs/prepare)
272
273 kernel_prepare: mkfs_prepare
274 $(call Image/BuildKernel)
275 $(if $(CONFIG_TARGET_ROOTFS_INITRAMFS),$(call Image/BuildKernel/Initramfs))
276 $(call Image/InstallKernel)
277
278 $(foreach fs,$(TARGET_FILESYSTEMS),$(call BuildImage/mkfs,$(fs)))
279
280 install: kernel_prepare install-targets
281 $(call Image/mkfs/cpiogz)
282 $(call Image/mkfs/targz)
283 $(foreach fs,$(TARGET_FILESYSTEMS),
284 $(call Image/Build,$(fs))
285 )
286 $(call Image/mkfs/ubifs)
287 $(call Image/Checksum)
288
289 compile-targets:
290 install-targets:
291 clean-targets:
292
293 endef