2 # Copyright (C) 2006-2010 OpenWrt.org
4 # This is free software, licensed under the GNU General Public License v2.
5 # See /LICENSE for more information.
9 include $(INCLUDE_DIR)/prereq.mk
10 include $(INCLUDE_DIR)/kernel.mk
11 include $(INCLUDE_DIR)/host.mk
15 override MAKE:=$(SUBMAKE)
16 KDIR=$(KERNEL_BUILD_DIR)
18 IMG_PREFIX:=openwrt-$(BOARD)$(if $(SUBTARGET),-$(SUBTARGET))
20 MKFS_DEVTABLE_OPT := -D $(INCLUDE_DIR)/device_table.txt
22 ifneq ($(CONFIG_BIG_ENDIAN),)
23 JFFS2OPTS := --big-endian --squash-uids -v
25 JFFS2OPTS := --little-endian --squash-uids -v
28 ifeq ($(CONFIG_JFFS2_RTIME),y)
31 ifeq ($(CONFIG_JFFS2_ZLIB),y)
34 ifeq ($(CONFIG_JFFS2_LZMA),y)
35 JFFS2OPTS += -X lzma --compression-mode=size
37 ifneq ($(CONFIG_JFFS2_RTIME),y)
40 ifneq ($(CONFIG_JFFS2_ZLIB),y)
43 ifneq ($(CONFIG_JFFS2_LZMA),y)
47 JFFS2OPTS += $(MKFS_DEVTABLE_OPT)
49 SQUASHFS_BLOCKSIZE := 256k
50 SQUASHFSOPT := -b $(SQUASHFS_BLOCKSIZE)
51 SQUASHFSOPT += -p '/dev d 755 0 0' -p '/dev/console c 600 0 0 5 1'
53 LZMA_XZ_OPTIONS := -Xpreset 9 -Xe -Xlc 0 -Xlp 2 -Xpb 2
54 ifeq ($(CONFIG_SQUASHFS_LZMA),y)
55 SQUASHFSCOMP := lzma $(LZMA_XZ_OPTIONS)
57 ifeq ($(CONFIG_SQUASHFS_XZ),y)
58 ifneq ($(filter arm x86 powerpc sparc,$(LINUX_KARCH)),)
59 BCJ_FILTER:=-Xbcj $(LINUX_KARCH)
61 SQUASHFSCOMP := xz $(LZMA_XZ_OPTIONS) $(BCJ_FILTER)
64 JFFS2_BLOCKSIZE ?= 64k 128k
67 echo -ne '\xde\xad\xc0\xde' >> $(1)
71 $(shell echo $(1) | tr '[:lower:]' '[:upper:]')
74 # pad to 4k, 8k, 16k, 64k, 128k, 256k and add jffs2 end-of-filesystem mark
75 define prepare_generic_squashfs
76 $(STAGING_DIR_HOST)/bin/padjffs2 $(1) 4 8 16 64 128 256
79 ifneq ($(CONFIG_TARGET_ROOTFS_INITRAMFS),)
80 define Image/BuildKernel/Initramfs
81 cp $(KDIR)/vmlinux-initramfs.elf $(BIN_DIR)/$(IMG_PREFIX)-vmlinux-initramfs.elf
82 $(call Image/Build/Initramfs)
85 define Image/BuildKernel/Initramfs
89 define Image/BuildKernel/MkuImage
90 mkimage -A $(ARCH) -O linux -T kernel -C $(1) -a $(2) -e $(3) \
91 -n '$(call toupper,$(ARCH)) OpenWrt Linux-$(LINUX_VERSION)' -d $(4) $(5)
94 define Image/BuildKernel/MkFIT
95 $(TOPDIR)/scripts/mkits.sh \
96 -D $(1) -o $(KDIR)/fit-$(1).its -k $(2) -d $(3) -C $(4) -a $(5) -e $(6) \
97 -A $(ARCH) -v $(LINUX_VERSION)
98 PATH=$(LINUX_DIR)/scripts/dtc:$(PATH) mkimage -f $(KDIR)/fit-$(1).its $(KDIR)/fit-$(1)$(7).itb
101 define Image/mkfs/jffs2/sub
102 # FIXME: removing this line will cause strange behaviour in the foreach loop below
103 $(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 '/^.+$$$$/'
104 $(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 '/^.+$$$$/'
105 $(call add_jffs2_mark,$(KDIR)/root.jffs2-$(2))
106 $(call Image/Build,jffs2-$(2))
109 ifneq ($(CONFIG_TARGET_ROOTFS_JFFS2),)
110 define Image/mkfs/jffs2
111 $(foreach SZ,$(JFFS2_BLOCKSIZE),$(call Image/mkfs/jffs2/sub,$(SZ),$(SZ),$(JFFS2OPTS)))
115 ifneq ($(CONFIG_TARGET_ROOTFS_JFFS2_NAND),)
116 define Image/mkfs/jffs2_nand
117 $(foreach SZ,$(NAND_BLOCKSIZE), $(call Image/mkfs/jffs2/sub, \
118 $(word 2,$(subst :, ,$(SZ))),nand-$(subst :,-,$(SZ)), \
119 $(JFFS2OPTS) --no-cleanmarkers --pagesize=$(word 1,$(subst :, ,$(SZ)))) \
124 ifneq ($(CONFIG_TARGET_ROOTFS_SQUASHFS),)
125 define Image/mkfs/squashfs
126 @mkdir -p $(TARGET_DIR)/overlay
127 $(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)
128 $(call Image/Build,squashfs)
132 ifneq ($(CONFIG_TARGET_ROOTFS_UBIFS),)
133 define Image/mkfs/ubifs/generate
134 $(CP) ./ubinize$(1).cfg $(KDIR)
136 $(STAGING_DIR_HOST)/bin/ubinize \
137 $(if $($(PROFILE)_UBI_OPTS), \
138 $(shell echo $($(PROFILE)_UBI_OPTS)), \
139 $(shell echo $(UBI_OPTS)) \
141 -o $(KDIR)/root$(1).ubi \
146 define Image/mkfs/ubifs
148 ifneq ($($(PROFILE)_UBIFS_OPTS)$(UBIFS_OPTS),)
149 $(STAGING_DIR_HOST)/bin/mkfs.ubifs \
150 $(if $($(PROFILE)_UBIFS_OPTS), \
151 $(shell echo $($(PROFILE)_UBIFS_OPTS)), \
152 $(shell echo $(UBIFS_OPTS)) \
154 $(if $(CONFIG_TARGET_UBIFS_FREE_SPACE_FIXUP),--space-fixup) \
155 $(if $(CONFIG_TARGET_UBIFS_COMPRESSION_NONE),--force-compr=none) \
156 $(if $(CONFIG_TARGET_UBIFS_COMPRESSION_LZO),--force-compr=lzo) \
157 $(if $(CONFIG_TARGET_UBIFS_COMPRESSION_ZLIB),--force-compr=zlib) \
158 $(if $(shell echo $(CONFIG_TARGET_UBIFS_JOURNAL_SIZE)),--jrn-size=$(CONFIG_TARGET_UBIFS_JOURNAL_SIZE)) \
160 -o $(KDIR)/root.ubifs \
163 $(call Image/Build,ubifs)
165 ifneq ($($(PROFILE)_UBI_OPTS)$(UBI_OPTS),)
166 $(call Image/mkfs/ubifs/generate,)
167 $(if $(wildcard ./ubinize-overlay.cfg),$(call Image/mkfs/ubifs/generate,-overlay))
169 $(call Image/Build,ubi)
173 ifneq ($(CONFIG_TARGET_ROOTFS_CPIOGZ),)
174 define Image/mkfs/cpiogz
175 ( cd $(TARGET_DIR); find . | cpio -o -H newc | gzip -9 >$(BIN_DIR)/$(IMG_PREFIX)-rootfs.cpio.gz )
179 ifneq ($(CONFIG_TARGET_ROOTFS_TARGZ),)
180 define Image/mkfs/targz
181 # Preserve permissions (-p) when building as non-root user
182 $(TAR) -czpf $(BIN_DIR)/$(IMG_PREFIX)$(if $(PROFILE),-$(PROFILE))-rootfs.tar.gz --numeric-owner --owner=0 --group=0 -C $(TARGET_DIR)/ .
186 ifneq ($(CONFIG_TARGET_ROOTFS_EXT4FS),)
187 E2SIZE=$(shell echo $$(($(CONFIG_TARGET_ROOTFS_PARTSIZE)*1024)))
189 define Image/mkfs/ext4
190 # generate an ext2 fs
191 $(STAGING_DIR_HOST)/bin/genext2fs -U -b $(E2SIZE) -N $(CONFIG_TARGET_ROOTFS_MAXINODE) -d $(TARGET_DIR)/ $(KDIR)/root.ext4 -m $(CONFIG_TARGET_ROOTFS_RESERVED_PCT) $(MKFS_DEVTABLE_OPT)
193 $(STAGING_DIR_HOST)/bin/tune2fs -O extents,uninit_bg,dir_index $(KDIR)/root.ext4
195 $(STAGING_DIR_HOST)/bin/e2fsck -fy $(KDIR)/root.ext4
196 $(call Image/Build,ext4)
200 ifneq ($(CONFIG_TARGET_ROOTFS_ISO),)
201 define Image/mkfs/iso
202 $(call Image/Build,iso)
207 define Image/mkfs/prepare/default
208 # Use symbolic permissions to avoid clobbering SUID/SGID/sticky bits
209 - $(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
210 - $(FIND) $(TARGET_DIR) -type f -perm +0100 -print0 | $(XARGS) -0 chmod u+rwx,g+rx,o+rx
211 - $(FIND) $(TARGET_DIR) -type d -print0 | $(XARGS) -0 chmod u+rwx,g+rx,o+rx
212 $(INSTALL_DIR) $(TARGET_DIR)/tmp
213 chmod 1777 $(TARGET_DIR)/tmp
216 define Image/mkfs/prepare
217 $(call Image/mkfs/prepare/default)
221 define Image/Checksum
223 $(FIND) -maxdepth 1 -type f \! -name 'md5sums' -printf "%P\n" | sort | xargs \
224 md5sum --binary > md5sums \
235 compile: compile-targets FORCE
236 $(call Build/Compile)
242 install: compile install-targets FORCE
243 $(call Image/Prepare)
244 $(call Image/mkfs/prepare)
245 $(call Image/BuildKernel)
246 $(call Image/BuildKernel/Initramfs)
247 $(call Image/InstallKernel)
248 $(call Image/mkfs/cpiogz)
249 $(call Image/mkfs/targz)
250 $(call Image/mkfs/ext4)
251 $(call Image/mkfs/iso)
252 $(call Image/mkfs/jffs2)
253 $(call Image/mkfs/jffs2_nand)
254 $(call Image/mkfs/squashfs)
255 $(call Image/mkfs/ubifs)
256 $(call Image/Checksum)
258 install: compile install-targets
259 $(call Image/BuildKernel)
260 $(call Image/BuildKernel/Initramfs)
261 $(call Image/InstallKernel)
262 $(call Image/mkfs/cpiogz)
263 $(call Image/mkfs/targz)
264 $(call Image/mkfs/ext4)
265 $(call Image/mkfs/iso)
266 $(call Image/mkfs/jffs2)
267 $(call Image/mkfs/jffs2_nand)
268 $(call Image/mkfs/squashfs)
269 $(call Image/mkfs/ubifs)
270 $(call Image/Checksum)