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. --uboot-env and/or --kernel kernelimage)
146 # $(3) rootfstype (e.g. squashfs or ubifs)
147 # $(4) options to pass-through to ubinize (i.e. $($(PROFILE)_UBI_OPTS)))
148 define Image/Build/UbinizeImage
149 sh $(TOPDIR)/scripts/ubinize-image.sh $(2) \
150 "$(KDIR)/root.$(3)" \
151 "$(BIN_DIR)/$(IMG_PREFIX)-$(1)-$(3)-ubinized.bin" \
157 ifneq ($(CONFIG_TARGET_ROOTFS_UBIFS),)
158 define Image/mkfs/ubifs/generate
159 $(CP) ./ubinize$(1).cfg $(KDIR)
161 $(STAGING_DIR_HOST)/bin/ubinize \
162 $(if $($(PROFILE)_UBI_OPTS), \
163 $(shell echo $($(PROFILE)_UBI_OPTS)), \
164 $(shell echo $(UBI_OPTS)) \
166 -o $(KDIR)/root$(1).ubi \
171 define Image/mkfs/ubifs
173 ifneq ($($(PROFILE)_UBIFS_OPTS)$(UBIFS_OPTS),)
174 $(STAGING_DIR_HOST)/bin/mkfs.ubifs \
175 $(if $($(PROFILE)_UBIFS_OPTS), \
176 $(shell echo $($(PROFILE)_UBIFS_OPTS)), \
177 $(shell echo $(UBIFS_OPTS)) \
179 $(if $(CONFIG_TARGET_UBIFS_FREE_SPACE_FIXUP),--space-fixup) \
180 $(if $(CONFIG_TARGET_UBIFS_COMPRESSION_NONE),--force-compr=none) \
181 $(if $(CONFIG_TARGET_UBIFS_COMPRESSION_LZO),--force-compr=lzo) \
182 $(if $(CONFIG_TARGET_UBIFS_COMPRESSION_ZLIB),--force-compr=zlib) \
183 $(if $(shell echo $(CONFIG_TARGET_UBIFS_JOURNAL_SIZE)),--jrn-size=$(CONFIG_TARGET_UBIFS_JOURNAL_SIZE)) \
185 -o $(KDIR)/root.ubifs \
188 $(call Image/Build,ubifs)
190 ifneq ($($(PROFILE)_UBI_OPTS)$(UBI_OPTS),)
191 $(call Image/mkfs/ubifs/generate,)
192 $(if $(wildcard ./ubinize-overlay.cfg),$(call Image/mkfs/ubifs/generate,-overlay))
194 $(call Image/Build,ubi)
198 ifneq ($(CONFIG_TARGET_ROOTFS_CPIOGZ),)
199 define Image/mkfs/cpiogz
200 ( cd $(TARGET_DIR); find . | cpio -o -H newc | gzip -9 >$(BIN_DIR)/$(IMG_PREFIX)-rootfs.cpio.gz )
204 ifneq ($(CONFIG_TARGET_ROOTFS_TARGZ),)
205 define Image/mkfs/targz
206 # Preserve permissions (-p) when building as non-root user
207 $(TAR) -czpf $(BIN_DIR)/$(IMG_PREFIX)$(if $(PROFILE),-$(PROFILE))-rootfs.tar.gz --numeric-owner --owner=0 --group=0 -C $(TARGET_DIR)/ .
211 ifneq ($(CONFIG_TARGET_ROOTFS_EXT4FS),)
212 E2SIZE=$(shell echo $$(($(CONFIG_TARGET_ROOTFS_PARTSIZE)*1024*1024/$(CONFIG_TARGET_EXT4_BLOCKSIZE))))
214 define Image/mkfs/ext4
215 # generate an ext2 fs
216 $(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)
218 $(STAGING_DIR_HOST)/bin/tune2fs $(if $(CONFIG_TARGET_EXT4_JOURNAL),-j) -O extents,uninit_bg,dir_index $(KDIR)/root.ext4
220 $(STAGING_DIR_HOST)/bin/e2fsck -fy $(KDIR)/root.ext4
221 $(call Image/Build,ext4)
225 ifneq ($(CONFIG_TARGET_ROOTFS_ISO),)
226 define Image/mkfs/iso
227 $(call Image/Build,iso)
232 define Image/mkfs/prepare/default
233 # Use symbolic permissions to avoid clobbering SUID/SGID/sticky bits
234 - $(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
235 - $(FIND) $(TARGET_DIR) -type f -perm +0100 -print0 | $(XARGS) -0 chmod u+rwx,g+rx,o+rx
236 - $(FIND) $(TARGET_DIR) -type d -print0 | $(XARGS) -0 chmod u+rwx,g+rx,o+rx
237 $(INSTALL_DIR) $(TARGET_DIR)/tmp
238 chmod 1777 $(TARGET_DIR)/tmp
241 define Image/mkfs/prepare
242 $(call Image/mkfs/prepare/default)
246 define Image/Checksum
248 $(FIND) -maxdepth 1 -type f \! -name 'md5sums' -printf "%P\n" | sort | xargs \
249 md5sum --binary > md5sums \
260 compile: compile-targets FORCE
261 $(call Build/Compile)
267 install: compile install-targets FORCE
268 $(call Image/Prepare)
269 $(call Image/mkfs/prepare)
270 $(call Image/BuildKernel)
271 $(call Image/BuildKernel/Initramfs)
272 $(call Image/InstallKernel)
273 $(call Image/mkfs/cpiogz)
274 $(call Image/mkfs/targz)
275 $(call Image/mkfs/ext4)
276 $(call Image/mkfs/iso)
277 $(call Image/mkfs/jffs2)
278 $(call Image/mkfs/jffs2_nand)
279 $(call Image/mkfs/squashfs)
280 $(call Image/mkfs/ubifs)
281 $(call Image/Checksum)
283 install: compile install-targets
284 $(call Image/BuildKernel)
285 $(call Image/BuildKernel/Initramfs)
286 $(call Image/InstallKernel)
287 $(call Image/mkfs/cpiogz)
288 $(call Image/mkfs/targz)
289 $(call Image/mkfs/ext4)
290 $(call Image/mkfs/iso)
291 $(call Image/mkfs/jffs2)
292 $(call Image/mkfs/jffs2_nand)
293 $(call Image/mkfs/squashfs)
294 $(call Image/mkfs/ubifs)
295 $(call Image/Checksum)