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
8override TARGET_BUILD=
9include $(INCLUDE_DIR)/prereq.mk
10include $(INCLUDE_DIR)/kernel.mk
11include $(INCLUDE_DIR)/host.mk
12
13.NOTPARALLEL:
14override MAKEFLAGS=
15override MAKE:=$(SUBMAKE)
16KDIR=$(KERNEL_BUILD_DIR)
17
18IMG_PREFIX:=openwrt-$(BOARD)$(if $(SUBTARGET),-$(SUBTARGET))
19
20ifneq ($(CONFIG_BIG_ENDIAN),)
21  JFFS2OPTS     :=  --pad --big-endian --squash -v
22else
23  JFFS2OPTS     :=  --pad --little-endian --squash -v
24endif
25
26ifeq ($(CONFIG_JFFS2_RTIME),y)
27  JFFS2OPTS += -X rtime
28endif
29ifeq ($(CONFIG_JFFS2_ZLIB),y) 
30  JFFS2OPTS += -X zlib
31endif
32ifeq ($(CONFIG_JFFS2_LZMA),y)
33  JFFS2OPTS += -X lzma --compression-mode=size
34endif
35ifneq ($(CONFIG_JFFS2_RTIME),y)
36  JFFS2OPTS +=  -x rtime
37endif
38ifneq ($(CONFIG_JFFS2_ZLIB),y)
39  JFFS2OPTS += -x zlib
40endif
41ifneq ($(CONFIG_JFFS2_LZMA),y)
42  JFFS2OPTS += -x lzma
43endif
44
45SQUASHFS_BLOCKSIZE := 256k
46SQUASHFSOPT := -b $(SQUASHFS_BLOCKSIZE)
47SQUASHFSCOMP := gzip
48LZMA_XZ_OPTIONS := -Xpreset 9 -Xe -Xlc 0 -Xlp 2 -Xpb 2
49ifeq ($(CONFIG_SQUASHFS_LZMA),y)
50  SQUASHFSCOMP := lzma $(LZMA_XZ_OPTIONS)
51endif
52ifeq ($(CONFIG_SQUASHFS_XZ),y)
53  SQUASHFSCOMP := xz $(LZMA_XZ_OPTIONS)
54endif
55
56JFFS2_BLOCKSIZE ?= 64k 128k
57
58define add_jffs2_mark
59	echo -ne '\xde\xad\xc0\xde' >> $(1)
60endef
61
62# pad to 4k, 8k, 64k, 128k 256k and add jffs2 end-of-filesystem mark
63define prepare_generic_squashfs
64	$(STAGING_DIR_HOST)/bin/padjffs2 $(1) 4 8 64 128 256
65endef
66
67
68ifneq ($(CONFIG_TARGET_ROOTFS_INITRAMFS),)
69
70  define Image/BuildKernel
71		cp $(KDIR)/vmlinux.elf $(BIN_DIR)/$(IMG_PREFIX)-vmlinux.elf
72		$(call Image/Build/Initramfs)
73  endef
74
75else
76
77  ifneq ($(CONFIG_TARGET_ROOTFS_JFFS2),)
78    define Image/mkfs/jffs2/sub
79		# FIXME: removing this line will cause strange behaviour in the foreach loop below
80		$(STAGING_DIR_HOST)/bin/mkfs.jffs2 $(JFFS2OPTS) -e $(patsubst %k,%KiB,$(1)) -o $(KDIR)/root.jffs2-$(1) -d $(TARGET_DIR) -v 2>&1 1>/dev/null | awk '/^.+$$$$/'
81		$(call add_jffs2_mark,$(KDIR)/root.jffs2-$(1))
82		$(call Image/Build,jffs2-$(1))
83    endef
84    define Image/mkfs/jffs2
85		$(foreach SZ,$(JFFS2_BLOCKSIZE),$(call Image/mkfs/jffs2/sub,$(SZ)))
86    endef
87  endif
88
89  ifneq ($(CONFIG_TARGET_ROOTFS_SQUASHFS),)
90    define Image/mkfs/squashfs
91		@mkdir -p $(TARGET_DIR)/overlay
92		$(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)
93		$(call Image/Build,squashfs)
94    endef
95  endif
96
97  ifneq ($(CONFIG_TARGET_ROOTFS_UBIFS),)
98    define Image/mkfs/ubifs
99		$(CP) ./ubinize.cfg $(KDIR)
100		$(STAGING_DIR_HOST)/bin/mkfs.ubifs $(UBIFS_OPTS) -o $(KDIR)/root.ubifs -d $(TARGET_DIR)
101		(cd $(KDIR); \
102		$(STAGING_DIR_HOST)/bin/ubinize $(UBINIZE_OPTS) -o $(KDIR)/root.ubi ubinize.cfg)
103		$(call Image/Build,ubi)
104    endef
105  endif
106
107endif
108
109ifneq ($(CONFIG_TARGET_ROOTFS_CPIOGZ),)
110  define Image/mkfs/cpiogz
111		( cd $(TARGET_DIR); find . | cpio -o -H newc | gzip -9 >$(BIN_DIR)/$(IMG_PREFIX)-rootfs.cpio.gz )
112  endef
113endif
114
115ifneq ($(CONFIG_TARGET_ROOTFS_TARGZ),)
116  define Image/mkfs/targz
117		# Preserve permissions (-p) when building as non-root user
118		$(TAR) -czpf $(BIN_DIR)/$(IMG_PREFIX)-rootfs.tar.gz --numeric-owner --owner=0 --group=0 -C $(TARGET_DIR)/ .
119  endef
120endif
121
122ifneq ($(CONFIG_TARGET_ROOTFS_EXT4FS),)
123  E2SIZE=$(shell echo $$(($(CONFIG_TARGET_ROOTFS_PARTSIZE)*1024)))
124
125  define Image/mkfs/ext4
126# generate an ext2 fs
127	$(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)
128# convert it to ext4
129	$(STAGING_DIR_HOST)/bin/tune2fs -O extents,uninit_bg,dir_index $(KDIR)/root.ext4
130# fix it up
131	$(STAGING_DIR_HOST)/bin/e2fsck -fy $(KDIR)/root.ext4
132	$(call Image/Build,ext4)
133  endef
134endif
135
136ifneq ($(CONFIG_TARGET_ROOTFS_ISO),)
137  define Image/mkfs/iso
138		$(call Image/Build,iso)
139  endef
140endif
141
142
143define Image/mkfs/prepare/default
144	# Use symbolic permissions to avoid clobbering SUID/SGID/sticky bits
145	- $(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
146	- $(FIND) $(TARGET_DIR) -type f -perm +0100 -print0 | $(XARGS) -0 chmod u+rwx,g+rx,o+rx
147	- $(FIND) $(TARGET_DIR) -type d -print0 | $(XARGS) -0 chmod u+rwx,g+rx,o+rx
148	$(INSTALL_DIR) $(TARGET_DIR)/tmp
149	chmod 1777 $(TARGET_DIR)/tmp
150endef
151
152define Image/mkfs/prepare
153	$(call Image/mkfs/prepare/default)
154endef
155
156
157define Image/Checksum
158	( cd ${BIN_DIR} ; \
159		$(FIND) -maxdepth 1 -type f \! -name 'md5sums'  -printf "%P\n" | sort | xargs \
160		md5sum --binary > md5sums \
161	)
162endef
163
164
165define BuildImage
166
167  download:
168  prepare:
169
170  ifeq ($(IB),)
171    compile: compile-targets FORCE
172		$(call Build/Compile)
173  else
174    compile:
175  endif
176
177  ifeq ($(IB),)
178    install: compile install-targets FORCE
179		$(call Image/Prepare)
180		$(call Image/mkfs/prepare)
181		$(call Image/BuildKernel)
182		$(call Image/mkfs/cpiogz)
183		$(call Image/mkfs/targz)
184		$(call Image/mkfs/ext4)
185		$(call Image/mkfs/iso)
186		$(call Image/mkfs/jffs2)
187		$(call Image/mkfs/squashfs)
188		$(call Image/mkfs/ubifs)
189		$(call Image/Checksum)
190  else
191    install: compile install-targets
192		$(call Image/BuildKernel)
193		$(call Image/mkfs/cpiogz)
194		$(call Image/mkfs/targz)
195		$(call Image/mkfs/ext4)
196		$(call Image/mkfs/iso)
197		$(call Image/mkfs/jffs2)
198		$(call Image/mkfs/squashfs)
199		$(call Image/mkfs/ubifs)
200		$(call Image/Checksum)
201  endif
202
203  ifeq ($(IB),)
204    clean: clean-targets
205		$(call Build/Clean)
206  else
207    clean:
208  endif
209
210  compile-targets:
211  install-targets:
212  clean-targets:
213
214endef
215