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 := $(CONFIG_TARGET_SQUASHFS_BLOCK_SIZE)k
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)
136 ifneq ($(CONFIG_NAND_SUPPORT),)
137 define Image/Build/SysupgradeNAND
138 mkdir -p $(KDIR_TMP)/sysupgrade-$(1)/
139 echo "BOARD=$(1)" > $(KDIR_TMP)/sysupgrade-$(1)/CONTROL
140 [ -z "$(2)" ] || $(CP) $(2) $(KDIR_TMP)/sysupgrade-$(1)/kernel
141 [ -z "$(3)" ] || $(CP) $(3) $(KDIR_TMP)/sysupgrade-$(1)/root
142 [ -z "$(4)" ] || $(CP) $(4) $(KDIR_TMP)/sysupgrade-$(1)/ubi
143 (cd $(KDIR_TMP); $(TAR) cvf \
144 $(BIN_DIR)/$(IMG_PREFIX)-$(1)-ubi-sysupgrade.tar sysupgrade-$(1))
148 ifneq ($(CONFIG_TARGET_ROOTFS_UBIFS),)
149 define Image/mkfs/ubifs/generate
150 $(CP) ./ubinize$(1).cfg $(KDIR)
152 $(STAGING_DIR_HOST)/bin/ubinize \
153 $(if $($(PROFILE)_UBI_OPTS), \
154 $(shell echo $($(PROFILE)_UBI_OPTS)), \
155 $(shell echo $(UBI_OPTS)) \
157 -o $(KDIR)/root$(1).ubi \
162 define Image/mkfs/ubifs
164 ifneq ($($(PROFILE)_UBIFS_OPTS)$(UBIFS_OPTS),)
165 $(STAGING_DIR_HOST)/bin/mkfs.ubifs \
166 $(if $($(PROFILE)_UBIFS_OPTS), \
167 $(shell echo $($(PROFILE)_UBIFS_OPTS)), \
168 $(shell echo $(UBIFS_OPTS)) \
170 $(if $(CONFIG_TARGET_UBIFS_FREE_SPACE_FIXUP),--space-fixup) \
171 $(if $(CONFIG_TARGET_UBIFS_COMPRESSION_NONE),--force-compr=none) \
172 $(if $(CONFIG_TARGET_UBIFS_COMPRESSION_LZO),--force-compr=lzo) \
173 $(if $(CONFIG_TARGET_UBIFS_COMPRESSION_ZLIB),--force-compr=zlib) \
174 $(if $(shell echo $(CONFIG_TARGET_UBIFS_JOURNAL_SIZE)),--jrn-size=$(CONFIG_TARGET_UBIFS_JOURNAL_SIZE)) \
176 -o $(KDIR)/root.ubifs \
179 $(call Image/Build,ubifs)
181 ifneq ($($(PROFILE)_UBI_OPTS)$(UBI_OPTS),)
182 $(call Image/mkfs/ubifs/generate,)
183 $(if $(wildcard ./ubinize-overlay.cfg),$(call Image/mkfs/ubifs/generate,-overlay))
185 $(call Image/Build,ubi)
189 ifneq ($(CONFIG_TARGET_ROOTFS_CPIOGZ),)
190 define Image/mkfs/cpiogz
191 ( cd $(TARGET_DIR); find . | cpio -o -H newc | gzip -9 >$(BIN_DIR)/$(IMG_PREFIX)-rootfs.cpio.gz )
195 ifneq ($(CONFIG_TARGET_ROOTFS_TARGZ),)
196 define Image/mkfs/targz
197 # Preserve permissions (-p) when building as non-root user
198 $(TAR) -czpf $(BIN_DIR)/$(IMG_PREFIX)$(if $(PROFILE),-$(PROFILE))-rootfs.tar.gz --numeric-owner --owner=0 --group=0 -C $(TARGET_DIR)/ .
202 ifneq ($(CONFIG_TARGET_ROOTFS_EXT4FS),)
203 E2SIZE=$(shell echo $$(($(CONFIG_TARGET_ROOTFS_PARTSIZE)*1024*1024/$(CONFIG_TARGET_EXT4_BLOCKSIZE))))
205 define Image/mkfs/ext4
206 # generate an ext2 fs
207 $(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)
209 $(STAGING_DIR_HOST)/bin/tune2fs $(if $(CONFIG_TARGET_EXT4_JOURNAL),-j) -O extents,uninit_bg,dir_index $(KDIR)/root.ext4
211 $(STAGING_DIR_HOST)/bin/e2fsck -fy $(KDIR)/root.ext4
212 $(call Image/Build,ext4)
216 ifneq ($(CONFIG_TARGET_ROOTFS_ISO),)
217 define Image/mkfs/iso
218 $(call Image/Build,iso)
223 define Image/mkfs/prepare/default
224 # Use symbolic permissions to avoid clobbering SUID/SGID/sticky bits
225 - $(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
226 - $(FIND) $(TARGET_DIR) -type f -perm +0100 -print0 | $(XARGS) -0 chmod u+rwx,g+rx,o+rx
227 - $(FIND) $(TARGET_DIR) -type d -print0 | $(XARGS) -0 chmod u+rwx,g+rx,o+rx
228 $(INSTALL_DIR) $(TARGET_DIR)/tmp
229 chmod 1777 $(TARGET_DIR)/tmp
232 define Image/mkfs/prepare
233 $(call Image/mkfs/prepare/default)
237 define Image/Checksum
239 $(FIND) -maxdepth 1 -type f \! -name 'md5sums' -printf "%P\n" | sort | xargs \
240 md5sum --binary > md5sums \
251 compile: compile-targets FORCE
252 $(call Build/Compile)
258 install: compile install-targets FORCE
259 $(call Image/Prepare)
260 $(call Image/mkfs/prepare)
261 $(call Image/BuildKernel)
262 $(call Image/BuildKernel/Initramfs)
263 $(call Image/InstallKernel)
264 $(call Image/mkfs/cpiogz)
265 $(call Image/mkfs/targz)
266 $(call Image/mkfs/ext4)
267 $(call Image/mkfs/iso)
268 $(call Image/mkfs/jffs2)
269 $(call Image/mkfs/jffs2_nand)
270 $(call Image/mkfs/squashfs)
271 $(call Image/mkfs/ubifs)
272 $(call Image/Checksum)
274 install: compile install-targets
275 $(call Image/BuildKernel)
276 $(call Image/BuildKernel/Initramfs)
277 $(call Image/InstallKernel)
278 $(call Image/mkfs/cpiogz)
279 $(call Image/mkfs/targz)
280 $(call Image/mkfs/ext4)
281 $(call Image/mkfs/iso)
282 $(call Image/mkfs/jffs2)
283 $(call Image/mkfs/jffs2_nand)
284 $(call Image/mkfs/squashfs)
285 $(call Image/mkfs/ubifs)
286 $(call Image/Checksum)