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
12 include $(INCLUDE_DIR)/version.mk
14 override MAKE:=$(_SINGLE)$(SUBMAKE)
15 override NO_TRACE_MAKE:=$(_SINGLE)$(NO_TRACE_MAKE)
17 KDIR=$(KERNEL_BUILD_DIR)
18 DTS_DIR:=$(LINUX_DIR)/arch/$(ARCH)/boot/dts/
20 IMG_PREFIX:=openwrt-$(if $(CONFIG_VERSION_FILENAMES),$(VERSION_NUMBER)-)$(BOARD)$(if $(SUBTARGET),-$(SUBTARGET))
22 MKFS_DEVTABLE_OPT := -D $(INCLUDE_DIR)/device_table.txt
24 ifneq ($(CONFIG_BIG_ENDIAN),)
25 JFFS2OPTS := --big-endian --squash-uids -v
27 JFFS2OPTS := --little-endian --squash-uids -v
30 ifeq ($(CONFIG_JFFS2_RTIME),y)
33 ifeq ($(CONFIG_JFFS2_ZLIB),y)
36 ifeq ($(CONFIG_JFFS2_LZMA),y)
37 JFFS2OPTS += -X lzma --compression-mode=size
39 ifneq ($(CONFIG_JFFS2_RTIME),y)
42 ifneq ($(CONFIG_JFFS2_ZLIB),y)
45 ifneq ($(CONFIG_JFFS2_LZMA),y)
49 JFFS2OPTS += $(MKFS_DEVTABLE_OPT)
51 SQUASHFS_BLOCKSIZE := $(CONFIG_TARGET_SQUASHFS_BLOCK_SIZE)k
52 SQUASHFSOPT := -b $(SQUASHFS_BLOCKSIZE)
53 SQUASHFSOPT += -p '/dev d 755 0 0' -p '/dev/console c 600 0 0 5 1'
55 LZMA_XZ_OPTIONS := -Xpreset 9 -Xe -Xlc 0 -Xlp 2 -Xpb 2
56 ifeq ($(CONFIG_SQUASHFS_LZMA),y)
57 SQUASHFSCOMP := lzma $(LZMA_XZ_OPTIONS)
59 ifeq ($(CONFIG_SQUASHFS_XZ),y)
60 ifneq ($(filter arm x86 powerpc sparc,$(LINUX_KARCH)),)
61 BCJ_FILTER:=-Xbcj $(LINUX_KARCH)
63 SQUASHFSCOMP := xz $(LZMA_XZ_OPTIONS) $(BCJ_FILTER)
66 JFFS2_BLOCKSIZE ?= 64k 128k
68 fs-types-$(CONFIG_TARGET_ROOTFS_SQUASHFS) += squashfs
69 fs-types-$(CONFIG_TARGET_ROOTFS_JFFS2) += $(addprefix jffs2-,$(JFFS2_BLOCKSIZE))
70 fs-types-$(CONFIG_TARGET_ROOTFS_JFFS2_NAND) += $(addprefix jffs2-nand-,$(NAND_BLOCKSIZE))
71 fs-types-$(CONFIG_TARGET_ROOTFS_EXT4FS) += ext4
72 fs-types-$(CONFIG_TARGET_ROOTFS_ISO) += iso
73 fs-subtypes-$(CONFIG_TARGET_ROOTFS_JFFS2) += $(addsuffix -raw,$(addprefix jffs2-,$(JFFS2_BLOCKSIZE)))
74 fs-subtypes-$(CONFIG_TARGET_ROOTFS_CPIOGZ) += cpiogz
75 fs-subtypes-$(CONFIG_TARGET_ROOTFS_TARGZ) += targz
77 TARGET_FILESYSTEMS := $(fs-types-y)
79 FS_64K := $(filter-out jffs2-%,$(TARGET_FILESYSTEMS)) jffs2-64k
80 FS_128K := $(filter-out jffs2-%,$(TARGET_FILESYSTEMS)) jffs2-128k
81 FS_256K := $(filter-out jffs2-%,$(TARGET_FILESYSTEMS)) jffs2-256k
84 echo -ne '\xde\xad\xc0\xde' >> $(1)
88 $(shell echo $(1) | tr '[:lower:]' '[:upper:]')
94 $(subst $(space),^,$(1))), \
95 $(call $(2),$(strip $(subst ^,$(space),$(data)))))
99 $(if $(Build/$(word 1,$(1))),,$(error Missing Build/$(word 1,$(1))))
100 $(call Build/$(word 1,$(1)),$(wordlist 2,$(words $(1)),$(1)))
105 $(call split_args,$(1),build_cmd)
108 # pad to 4k, 8k, 16k, 64k, 128k, 256k and add jffs2 end-of-filesystem mark
109 define prepare_generic_squashfs
110 $(STAGING_DIR_HOST)/bin/padjffs2 $(1) 4 8 16 64 128 256
113 define Image/BuildKernel/Initramfs
114 $(call Image/Build/Initramfs)
117 define Image/BuildKernel/MkuImage
118 mkimage -A $(ARCH) -O linux -T kernel -C $(1) -a $(2) -e $(3) \
119 -n '$(call toupper,$(ARCH)) OpenWrt Linux-$(LINUX_VERSION)' -d $(4) $(5)
122 define Image/BuildKernel/MkFIT
123 $(TOPDIR)/scripts/mkits.sh \
124 -D $(1) -o $(KDIR)/fit-$(1).its -k $(2) -d $(3) -C $(4) -a $(5) -e $(6) \
125 -A $(ARCH) -v $(LINUX_VERSION)
126 PATH=$(LINUX_DIR)/scripts/dtc:$(PATH) mkimage -f $(KDIR)/fit-$(1).its $(KDIR)/fit-$(1)$(7).itb
129 define Image/mkfs/jffs2/sub
130 $(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 '/^.+$$$$/'
131 $(call add_jffs2_mark,$(KDIR)/root.jffs2-$(2))
134 define Image/mkfs/jffs2/sub-raw
135 $(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 '/^.+$$$$/'
138 define Image/mkfs/jffs2/template
139 Image/mkfs/jffs2-$(1) = $$(call Image/mkfs/jffs2/sub,$(1),$(1),$(JFFS2OPTS))
140 Image/mkfs/jffs2-$(1)-raw = $$(call Image/mkfs/jffs2/sub-raw,$(1),$(1),$(JFFS2OPTS))
144 define Image/mkfs/jffs2-nand/template
145 Image/mkfs/jffs2-nand-$(1) = \
146 $$(call Image/mkfs/jffs2/sub, \
147 $(word 2,$(subst -, ,$(1))),nand-$(1), \
148 $(JFFS2OPTS) --no-cleanmarkers --pagesize=$(word 1,$(subst -, ,$(1))))
152 $(eval $(foreach S,$(JFFS2_BLOCKSIZE),$(call Image/mkfs/jffs2/template,$(S))))
153 $(eval $(foreach S,$(NAND_BLOCKSIZE),$(call Image/mkfs/jffs2-nand/template,$(S))))
155 define Image/mkfs/squashfs
156 $(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)
162 ifneq ($(CONFIG_NAND_SUPPORT),)
163 define Image/Build/SysupgradeNAND
164 mkdir -p "$(KDIR_TMP)/sysupgrade-$(1)/"
165 echo "BOARD=$(1)" > "$(KDIR_TMP)/sysupgrade-$(1)/CONTROL"
166 [ -z "$(2)" ] || $(CP) "$(KDIR)/root.$(2)" "$(KDIR_TMP)/sysupgrade-$(1)/root"
167 [ -z "$(3)" ] || $(CP) "$(3)" "$(KDIR_TMP)/sysupgrade-$(1)/kernel"
168 (cd "$(KDIR_TMP)"; $(TAR) cvf \
169 "$(BIN_DIR)/$(IMG_PREFIX)-$(1)-$(2)-sysupgrade.tar" sysupgrade-$(1))
173 # $(2) ubinize-image options (e.g. --uboot-env and/or --kernel kernelimage)
174 # $(3) rootfstype (e.g. squashfs or ubifs)
175 # $(4) options to pass-through to ubinize (i.e. $($(PROFILE)_UBI_OPTS)))
176 define Image/Build/UbinizeImage
177 sh $(TOPDIR)/scripts/ubinize-image.sh $(2) \
178 "$(KDIR)/root.$(3)" \
179 "$(KDIR)/$(IMG_PREFIX)-$(1)-$(3)-ubinized.bin" \
185 ifneq ($(CONFIG_TARGET_ROOTFS_UBIFS),)
186 define Image/mkfs/ubifs/generate
187 $(CP) ./ubinize$(1).cfg $(KDIR)
189 $(STAGING_DIR_HOST)/bin/ubinize \
190 $(if $($(PROFILE)_UBI_OPTS), \
191 $(shell echo $($(PROFILE)_UBI_OPTS)), \
192 $(shell echo $(UBI_OPTS)) \
194 -o $(KDIR)/root$(1).ubi \
199 define Image/mkfs/ubifs
201 ifneq ($($(PROFILE)_UBIFS_OPTS)$(UBIFS_OPTS),)
202 $(STAGING_DIR_HOST)/bin/mkfs.ubifs \
203 $(if $($(PROFILE)_UBIFS_OPTS), \
204 $(shell echo $($(PROFILE)_UBIFS_OPTS)), \
205 $(shell echo $(UBIFS_OPTS)) \
207 $(if $(CONFIG_TARGET_UBIFS_FREE_SPACE_FIXUP),--space-fixup) \
208 $(if $(CONFIG_TARGET_UBIFS_COMPRESSION_NONE),--force-compr=none) \
209 $(if $(CONFIG_TARGET_UBIFS_COMPRESSION_LZO),--force-compr=lzo) \
210 $(if $(CONFIG_TARGET_UBIFS_COMPRESSION_ZLIB),--force-compr=zlib) \
211 $(if $(shell echo $(CONFIG_TARGET_UBIFS_JOURNAL_SIZE)),--jrn-size=$(CONFIG_TARGET_UBIFS_JOURNAL_SIZE)) \
213 -o $(KDIR)/root.ubifs \
216 $(call Image/Build,ubifs)
218 ifneq ($($(PROFILE)_UBI_OPTS)$(UBI_OPTS),)
219 $(if $(wildcard ./ubinize.cfg),$(call Image/mkfs/ubifs/generate,))
220 $(if $(wildcard ./ubinize-overlay.cfg),$(call Image/mkfs/ubifs/generate,-overlay))
222 $(if $(wildcard ./ubinize.cfg),$(call Image/Build,ubi))
226 define Image/mkfs/cpiogz
227 ( cd $(TARGET_DIR); find . | cpio -o -H newc | gzip -9 >$(BIN_DIR)/$(IMG_PREFIX)-rootfs.cpio.gz )
230 define Image/mkfs/targz
231 $(TAR) -czpf $(BIN_DIR)/$(IMG_PREFIX)$(if $(PROFILE),-$(PROFILE))-rootfs.tar.gz --numeric-owner --owner=0 --group=0 -C $(TARGET_DIR)/ .
234 E2SIZE=$(shell echo $$(($(CONFIG_TARGET_ROOTFS_PARTSIZE)*1024*1024/$(CONFIG_TARGET_EXT4_BLOCKSIZE))))
236 define Image/mkfs/ext4
237 # generate an ext2 fs
238 $(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)
240 $(STAGING_DIR_HOST)/bin/tune2fs $(if $(CONFIG_TARGET_EXT4_JOURNAL),-j) -O extents,uninit_bg,dir_index $(KDIR)/root.ext4
242 $(STAGING_DIR_HOST)/bin/e2fsck -fy $(KDIR)/root.ext4
245 define Image/mkfs/prepare/default
246 # Use symbolic permissions to avoid clobbering SUID/SGID/sticky bits
247 - $(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
248 - $(FIND) $(TARGET_DIR) -type f -perm +0100 -print0 | $(XARGS) -0 chmod u+rwx,g+rx,o+rx
249 - $(FIND) $(TARGET_DIR) -type d -print0 | $(XARGS) -0 chmod u+rwx,g+rx,o+rx
250 $(INSTALL_DIR) $(TARGET_DIR)/tmp $(TARGET_DIR)/overlay
251 chmod 1777 $(TARGET_DIR)/tmp
254 define Image/mkfs/prepare
255 $(call Image/mkfs/prepare/default)
259 define Image/Checksum
261 $(FIND) -maxdepth 1 -type f \! -name 'md5sums' -printf "%P\n" | sort | xargs \
262 md5sum --binary > md5sums \
266 define BuildImage/mkfs
269 mkfs-$(1): mkfs_prepare
271 $(call Build/mkfs/default,$(1))
272 $(call Build/mkfs/$(1),$(1))
273 $(KDIR)/root.$(1): mkfs-$(1)
277 # Build commands that can be called from Device/* templates
279 mkimage -A $(LINUX_KARCH) \
281 -C $(1) -a $(KERNEL_LOADADDR) -e $(if $(KERNEL_ENTRY),$(KERNEL_ENTRY),$(KERNEL_LOADADDR)) \
282 -n '$(call toupper,$(LINUX_KARCH)) OpenWrt Linux-$(LINUX_VERSION)' -d $@ $@.new
287 $(STAGING_DIR_HOST)/bin/lzma e $@ -lc1 -lp2 -pb2 $(1) $@.new
291 define Build/kernel-bin
296 define Build/patch-cmdline
297 $(STAGING_DIR_HOST)/bin/patch-cmdline $@ '$(CMDLINE)'
300 define Build/append-kernel
301 dd if=$(word 1,$^) $(if $(1),bs=$(1) conv=sync) >> $@
304 define Build/append-rootfs
305 dd if=$(word 2,$^) $(if $(1),bs=$(1) conv=sync) >> $@
308 define Build/pad-rootfs
309 $(call prepare_generic_squashfs,$@ $(1))
312 define Build/pad-offset
314 size="$$(stat -c%s $@)" \
315 pad="$(word 1, $(1))" \
316 offset="$(word 2, $(1))" \
317 pad="(pad - ((size + offset) % pad)) % pad" \
318 newsize='size + pad'; \
319 dd if=$@ of=$@.new bs=$$newsize count=1 conv=sync
323 define Build/check-size
324 @[ $$(($(subst k,* 1024,$(subst m, * 1024k,$(1))))) -gt "$$(stat -c%s $@)" ] || { \
325 echo "WARNING: Image file $@ is too big"; \
332 PROFILES := $(PROFILE)
334 KERNEL_INITRAMFS = $$(KERNEL)
338 IMAGE_PREFIX := $(IMG_PREFIX)-$(1)
339 IMAGE_NAME = $$(IMAGE_PREFIX)-$$(1)-$$(2)
341 KERNEL_SUFFIX := -kernel.bin
342 KERNEL_IMAGE = $$(KERNEL_PREFIX)$$(KERNEL_SUFFIX)
343 KERNEL_INITRAMFS_PREFIX = $$(IMAGE_PREFIX)-initramfs
344 KERNEL_INITRAMFS_IMAGE = $$(KERNEL_INITRAMFS_PREFIX)$$(KERNEL_SUFFIX)
346 KERNEL_NAME := vmlinux
349 FILESYSTEMS := $(TARGET_FILESYSTEMS)
352 define Device/ExportVar
353 $(1) : $(2):=$$($(2))
356 Device/Export = $(foreach var,$(DEVICE_VARS) KERNEL,$(call Device/ExportVar,$(1),$(var)))
359 _TARGET = $$(if $$(filter $(PROFILE),$$(PROFILES)),install,install-disabled)
362 define Device/Build/initramfs
363 $$(_TARGET): $(BIN_DIR)/$$(KERNEL_INITRAMFS_IMAGE)
365 $(BIN_DIR)/$$(KERNEL_INITRAMFS_IMAGE): $(KDIR)/$$(KERNEL_INITRAMFS_IMAGE)
368 $(KDIR)/$$(KERNEL_INITRAMFS_IMAGE): $(KDIR)/$$(KERNEL_NAME)-initramfs
370 $$(call concat_cmd,$$(KERNEL_INITRAMFS))
373 define Device/Build/check_size
374 @[ $$(($(subst k,* 1024,$(subst m, * 1024k,$(1))))) -gt "$$(stat -c%s $@)" ] || { \
375 echo "WARNING: Image file $@ is too big"; \
380 define Device/Build/kernel
381 $$(_TARGET): $$(if $$(KERNEL_INSTALL),$(BIN_DIR)/$$(KERNEL_IMAGE))
382 $(BIN_DIR)/$$(KERNEL_IMAGE): $(KDIR)/$$(KERNEL_IMAGE)
384 $(KDIR)/$$(KERNEL_IMAGE): $(KDIR)/$$(KERNEL_NAME)
386 $$(call concat_cmd,$$(KERNEL))
387 $$(if $$(KERNEL_SIZE),$$(call Device/Build/check_size,$$(KERNEL_SIZE)))
390 define Device/Build/image
392 $$(_TARGET): $(BIN_DIR)/$(call IMAGE_NAME,$(1),$(2))
393 $(eval $(call Device/Export,$(KDIR)/$(KERNEL_IMAGE)))
394 $(eval $(call Device/Export,$(KDIR)/$(KERNEL_INITRAMFS_IMAGE)))
395 $(eval $(call Device/Export,$(KDIR)/$(call IMAGE_NAME,$(1),$(2))))
396 $(KDIR)/$(call IMAGE_NAME,$(1),$(2)): $(KDIR)/$$(KERNEL_IMAGE) $(KDIR)/root.$(1)
398 [ -f $$(word 1,$$^) -a -f $$(word 2,$$^) ]
399 $$(call concat_cmd,$(if $(IMAGE/$(2)/$(1)),$(IMAGE/$(2)/$(1)),$(IMAGE/$(2))))
401 .IGNORE: $(BIN_DIR)/$(call IMAGE_NAME,$(1),$(2))
402 $(BIN_DIR)/$(call IMAGE_NAME,$(1),$(2)): $(KDIR)/$(call IMAGE_NAME,$(1),$(2))
408 $(if $(CONFIG_TARGET_ROOTFS_INITRAMFS),$(call Device/Build/initramfs,$(1)))
409 $(call Device/Build/kernel,$(1))
411 $$(eval $$(foreach image,$$(IMAGES), \
412 $$(foreach fs,$$(filter $(TARGET_FILESYSTEMS),$$(FILESYSTEMS)), \
413 $$(call Device/Build/image,$$(fs),$$(image),$(1)))))
417 $(call Device/Init,$(1))
418 $(call Device/Default,$(1))
419 $(call Device/Check,$(1))
420 $(call Device/$(1),$(1))
421 $(call Device/Build,$(1))
434 .PHONY: download prepare compile clean image_prepare mkfs_prepare kernel_prepare install
436 $(call Build/Compile)
441 image_prepare: compile
442 $(call Image/Prepare)
445 mkfs_prepare: image_prepare
446 $(call Image/mkfs/prepare)
448 kernel_prepare: mkfs_prepare
449 $(call Image/BuildKernel)
450 $(if $(CONFIG_TARGET_ROOTFS_INITRAMFS),$(call Image/BuildKernel/Initramfs))
451 $(call Image/InstallKernel)
453 $(foreach device,$(TARGET_DEVICES),$(call Device,$(device)))
454 $(foreach fs,$(TARGET_FILESYSTEMS) $(fs-subtypes-y),$(call BuildImage/mkfs,$(fs)))
456 install: kernel_prepare
457 $(foreach fs,$(TARGET_FILESYSTEMS),
458 $(call Image/Build,$(fs))
460 $(call Image/mkfs/ubifs)
461 $(call Image/Checksum)