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)
135 ifneq ($(CONFIG_NAND_SUPPORT),)
136 define Image/Build/SysupgradeNAND
137 mkdir -p "$(KDIR_TMP)/sysupgrade-$(1)/"
138 echo "BOARD=$(1)" > "$(KDIR_TMP)/sysupgrade-$(1)/CONTROL"
139 [ -z "$(2)" ] || $(CP) "$(KDIR)/root.$(2)" "$(KDIR_TMP)/sysupgrade-$(1)/root"
140 [ -z "$(3)" ] || $(CP) "$(3)" "$(KDIR_TMP)/sysupgrade-$(1)/kernel"
141 (cd "$(KDIR_TMP)"; $(TAR) cvf \
142 "$(BIN_DIR)/$(IMG_PREFIX)-$(1)-$(2)-sysupgrade.tar" sysupgrade-$(1))
145 # $(2) ubinize-image options (e.g. --no-kernel and/or --uboot-env)
146 # $(3) rootfstype (e.g. squashfs or ubifs)
147 # $(4) kernel image file
148 # $(5) options to pass-through to ubinize (i.e. $($(PROFILE)_UBI_OPTS)))
149 define Image/Build/UbinizeImage
150 sh $(TOPDIR)/scripts/ubinize-image.sh $(2) \
151 "$(KDIR)/root.$(3)" "$(4)" \
152 "$(BIN_DIR)/$(IMG_PREFIX)-$(1)-$(3)-ubinized.bin" \
158 ifneq ($(CONFIG_TARGET_ROOTFS_UBIFS),)
159 define Image/mkfs/ubifs/generate
160 $(CP) ./ubinize$(1).cfg $(KDIR)
162 $(STAGING_DIR_HOST)/bin/ubinize \
163 $(if $($(PROFILE)_UBI_OPTS), \
164 $(shell echo $($(PROFILE)_UBI_OPTS)), \
165 $(shell echo $(UBI_OPTS)) \
167 -o $(KDIR)/root$(1).ubi \
172 define Image/mkfs/ubifs
174 ifneq ($($(PROFILE)_UBIFS_OPTS)$(UBIFS_OPTS),)
175 $(STAGING_DIR_HOST)/bin/mkfs.ubifs \
176 $(if $($(PROFILE)_UBIFS_OPTS), \
177 $(shell echo $($(PROFILE)_UBIFS_OPTS)), \
178 $(shell echo $(UBIFS_OPTS)) \
180 $(if $(CONFIG_TARGET_UBIFS_FREE_SPACE_FIXUP),--space-fixup) \
181 $(if $(CONFIG_TARGET_UBIFS_COMPRESSION_NONE),--force-compr=none) \
182 $(if $(CONFIG_TARGET_UBIFS_COMPRESSION_LZO),--force-compr=lzo) \
183 $(if $(CONFIG_TARGET_UBIFS_COMPRESSION_ZLIB),--force-compr=zlib) \
184 $(if $(shell echo $(CONFIG_TARGET_UBIFS_JOURNAL_SIZE)),--jrn-size=$(CONFIG_TARGET_UBIFS_JOURNAL_SIZE)) \
186 -o $(KDIR)/root.ubifs \
189 $(call Image/Build,ubifs)
191 ifneq ($($(PROFILE)_UBI_OPTS)$(UBI_OPTS),)
192 $(call Image/mkfs/ubifs/generate,)
193 $(if $(wildcard ./ubinize-overlay.cfg),$(call Image/mkfs/ubifs/generate,-overlay))
195 $(call Image/Build,ubi)
199 ifneq ($(CONFIG_TARGET_ROOTFS_CPIOGZ),)
200 define Image/mkfs/cpiogz
201 ( cd $(TARGET_DIR); find . | cpio -o -H newc | gzip -9 >$(BIN_DIR)/$(IMG_PREFIX)-rootfs.cpio.gz )
205 ifneq ($(CONFIG_TARGET_ROOTFS_TARGZ),)
206 define Image/mkfs/targz
207 # Preserve permissions (-p) when building as non-root user
208 $(TAR) -czpf $(BIN_DIR)/$(IMG_PREFIX)$(if $(PROFILE),-$(PROFILE))-rootfs.tar.gz --numeric-owner --owner=0 --group=0 -C $(TARGET_DIR)/ .
212 ifneq ($(CONFIG_TARGET_ROOTFS_EXT4FS),)
213 E2SIZE=$(shell echo $$(($(CONFIG_TARGET_ROOTFS_PARTSIZE)*1024*1024/$(CONFIG_TARGET_EXT4_BLOCKSIZE))))
215 define Image/mkfs/ext4
216 # generate an ext2 fs
217 $(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)
219 $(STAGING_DIR_HOST)/bin/tune2fs $(if $(CONFIG_TARGET_EXT4_JOURNAL),-j) -O extents,uninit_bg,dir_index $(KDIR)/root.ext4
221 $(STAGING_DIR_HOST)/bin/e2fsck -fy $(KDIR)/root.ext4
222 $(call Image/Build,ext4)
226 ifneq ($(CONFIG_TARGET_ROOTFS_ISO),)
227 define Image/mkfs/iso
228 $(call Image/Build,iso)
233 define Image/mkfs/prepare/default
234 # Use symbolic permissions to avoid clobbering SUID/SGID/sticky bits
235 - $(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
236 - $(FIND) $(TARGET_DIR) -type f -perm +0100 -print0 | $(XARGS) -0 chmod u+rwx,g+rx,o+rx
237 - $(FIND) $(TARGET_DIR) -type d -print0 | $(XARGS) -0 chmod u+rwx,g+rx,o+rx
238 $(INSTALL_DIR) $(TARGET_DIR)/tmp
239 chmod 1777 $(TARGET_DIR)/tmp
242 define Image/mkfs/prepare
243 $(call Image/mkfs/prepare/default)
247 define Image/Checksum
249 $(FIND) -maxdepth 1 -type f \! -name 'md5sums' -printf "%P\n" | sort | xargs \
250 md5sum --binary > md5sums \
261 compile: compile-targets FORCE
262 $(call Build/Compile)
268 install: compile install-targets FORCE
269 $(call Image/Prepare)
270 $(call Image/mkfs/prepare)
271 $(call Image/BuildKernel)
272 $(call Image/BuildKernel/Initramfs)
273 $(call Image/InstallKernel)
274 $(call Image/mkfs/cpiogz)
275 $(call Image/mkfs/targz)
276 $(call Image/mkfs/ext4)
277 $(call Image/mkfs/iso)
278 $(call Image/mkfs/jffs2)
279 $(call Image/mkfs/jffs2_nand)
280 $(call Image/mkfs/squashfs)
281 $(call Image/mkfs/ubifs)
282 $(call Image/Checksum)
284 install: compile install-targets
285 $(call Image/BuildKernel)
286 $(call Image/BuildKernel/Initramfs)
287 $(call Image/InstallKernel)
288 $(call Image/mkfs/cpiogz)
289 $(call Image/mkfs/targz)
290 $(call Image/mkfs/ext4)
291 $(call Image/mkfs/iso)
292 $(call Image/mkfs/jffs2)
293 $(call Image/mkfs/jffs2_nand)
294 $(call Image/mkfs/squashfs)
295 $(call Image/mkfs/ubifs)
296 $(call Image/Checksum)