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