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) $(if $(3),-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 -9n >$(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)))
236 define Image/mkfs/ext4
237 $(STAGING_DIR_HOST)/bin/make_ext4fs \
238 -l $(E2SIZE) -b $(CONFIG_TARGET_EXT4_BLOCKSIZE) \
239 -i $(CONFIG_TARGET_EXT4_MAXINODE) \
240 -m $(CONFIG_TARGET_EXT4_RESERVED_PCT) \
241 $(if $(CONFIG_TARGET_EXT4_JOURNAL),,-J) \
242 $(KDIR)/root.ext4 $(TARGET_DIR)/
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 $1 > $2 \
265 define BuildImage/mkfs
268 mkfs-$(1): mkfs_prepare
270 $(call Build/mkfs/default,$(1))
271 $(call Build/mkfs/$(1),$(1))
272 $(KDIR)/root.$(1): mkfs-$(1)
276 # Build commands that can be called from Device/* templates
278 mkimage -A $(LINUX_KARCH) \
280 -C $(1) -a $(KERNEL_LOADADDR) -e $(if $(KERNEL_ENTRY),$(KERNEL_ENTRY),$(KERNEL_LOADADDR)) \
281 -n '$(call toupper,$(LINUX_KARCH)) OpenWrt Linux-$(LINUX_VERSION)' -d $@ $@.new
286 $(STAGING_DIR_HOST)/bin/lzma e $@ -lc1 -lp2 -pb2 $(1) $@.new
290 define Build/kernel-bin
295 define Build/patch-cmdline
296 $(STAGING_DIR_HOST)/bin/patch-cmdline $@ '$(CMDLINE)'
299 define Build/append-kernel
300 dd if=$(word 1,$^) $(if $(1),bs=$(1) conv=sync) >> $@
303 define Build/append-rootfs
304 dd if=$(word 2,$^) $(if $(1),bs=$(1) conv=sync) >> $@
308 dd if=$@ of=$@.new bs=$(1) conv=sync
312 define Build/pad-rootfs
313 $(call prepare_generic_squashfs,$@ $(1))
316 define Build/pad-offset
318 size="$$(stat -c%s $@)" \
319 pad="$(word 1, $(1))" \
320 offset="$(word 2, $(1))" \
321 pad="(pad - ((size + offset) % pad)) % pad" \
322 newsize='size + pad'; \
323 dd if=$@ of=$@.new bs=$$newsize count=1 conv=sync
327 define Build/check-size
328 @[ $$(($(subst k,* 1024,$(subst m, * 1024k,$(1))))) -gt "$$(stat -c%s $@)" ] || { \
329 echo "WARNING: Image file $@ is too big" >&2; \
334 define Build/combined-image
335 -sh $(TOPDIR)/scripts/combined-image.sh \
343 PROFILES := $(PROFILE)
345 KERNEL_INITRAMFS = $$(KERNEL)
349 IMAGE_PREFIX := $(IMG_PREFIX)-$(1)
350 IMAGE_NAME = $$(IMAGE_PREFIX)-$$(1)-$$(2)
352 KERNEL_SUFFIX := -kernel.bin
353 KERNEL_IMAGE = $$(KERNEL_PREFIX)$$(KERNEL_SUFFIX)
354 KERNEL_INITRAMFS_PREFIX = $$(IMAGE_PREFIX)-initramfs
355 KERNEL_INITRAMFS_IMAGE = $$(KERNEL_INITRAMFS_PREFIX)$$(KERNEL_SUFFIX)
357 KERNEL_NAME := vmlinux
360 FILESYSTEMS := $(TARGET_FILESYSTEMS)
363 define Device/ExportVar
364 $(1) : $(2):=$$($(2))
368 $(foreach var,$(DEVICE_VARS) KERNEL KERNEL_INITRAMFS,$(call Device/ExportVar,$(1),$(var)))
369 $(1) : FILESYSTEM:=$(2)
373 _TARGET = $$(if $$(filter $(PROFILE),$$(PROFILES)),install,install-disabled)
374 _COMPILE_TARGET = $$(if $(if $(IB),,$(CONFIG_IB)$$(filter $(PROFILE),$$(PROFILES))),compile,compile-disabled)
378 define Device/Build/initramfs
379 $$(_TARGET): $(BIN_DIR)/$$(KERNEL_INITRAMFS_IMAGE)
381 $(KDIR)/$$(KERNEL_NAME)-initramfs: image_prepare
382 $(BIN_DIR)/$$(KERNEL_INITRAMFS_IMAGE): $(KDIR)/tmp/$$(KERNEL_INITRAMFS_IMAGE)
385 $(KDIR)/tmp/$$(KERNEL_INITRAMFS_IMAGE): $(KDIR)/$$(KERNEL_NAME)-initramfs
387 $$(call concat_cmd,$$(KERNEL_INITRAMFS))
391 define Device/Build/check_size
392 @[ $$(($(subst k,* 1024,$(subst m, * 1024k,$(1))))) -gt "$$(stat -c%s $@)" ] || { \
393 echo "WARNING: Image file $@ is too big" >&2; \
398 define Device/Build/compile
399 $$(_COMPILE_TARGET): $(KDIR)/$(1)
400 $(eval $(call Device/Export,$(KDIR)/$(1)))
402 $$(call concat_cmd,$(COMPILE/$(1)))
406 define Device/Build/kernel
407 _KERNEL_IMAGES += $(KDIR)/$$(KERNEL_NAME)
408 $(KDIR)/$$(KERNEL_NAME): image_prepare
409 $$(_TARGET): $$(if $$(KERNEL_INSTALL),$(BIN_DIR)/$$(KERNEL_IMAGE))
410 $(BIN_DIR)/$$(KERNEL_IMAGE): $(KDIR)/$$(KERNEL_IMAGE)
414 install: $(KDIR)/$$(KERNEL_IMAGE)
416 $(KDIR)/$$(KERNEL_IMAGE): $(KDIR)/$$(KERNEL_NAME)
418 $$(call concat_cmd,$$(KERNEL))
419 $$(if $$(KERNEL_SIZE),$$(call Device/Build/check_size,$$(KERNEL_SIZE)))
423 define Device/Build/image
424 $$(_TARGET): $(BIN_DIR)/$(call IMAGE_NAME,$(1),$(2))
425 $(eval $(call Device/Export,$(KDIR)/$(KERNEL_IMAGE),$(1)))
426 $(eval $(call Device/Export,$(KDIR)/tmp/$(KERNEL_INITRAMFS_IMAGE),$(1)))
427 $(eval $(call Device/Export,$(KDIR)/tmp/$(call IMAGE_NAME,$(1),$(2)),$(1)))
428 $(KDIR)/tmp/$(call IMAGE_NAME,$(1),$(2)): $(KDIR)/$$(KERNEL_IMAGE) $(KDIR)/root.$(1)
430 [ -f $$(word 1,$$^) -a -f $$(word 2,$$^) ]
431 $$(call concat_cmd,$(if $(IMAGE/$(2)/$(1)),$(IMAGE/$(2)/$(1)),$(IMAGE/$(2))))
433 .IGNORE: $(BIN_DIR)/$(call IMAGE_NAME,$(1),$(2))
434 $(BIN_DIR)/$(call IMAGE_NAME,$(1),$(2)): $(KDIR)/tmp/$(call IMAGE_NAME,$(1),$(2))
440 $(if $(CONFIG_TARGET_ROOTFS_INITRAMFS),$(call Device/Build/initramfs,$(1)))
441 $(call Device/Build/kernel,$(1))
443 $$(eval $$(foreach compile,$$(COMPILE), \
444 $$(call Device/Build/compile,$$(compile),$(1))))
446 $$(eval $$(foreach image,$$(IMAGES), \
447 $$(foreach fs,$$(filter $(TARGET_FILESYSTEMS),$$(FILESYSTEMS)), \
448 $$(call Device/Build/image,$$(fs),$$(image),$(1)))))
452 $(call Device/Init,$(1))
453 $(call Device/Default,$(1))
454 $(call Device/Check,$(1))
455 $(call Device/$(1),$(1))
456 $(call Device/Build,$(1))
469 .PHONY: download prepare compile clean image_prepare mkfs_prepare kernel_prepare install
471 $(call Build/Compile)
476 image_prepare: compile
478 $(call Image/Prepare)
484 mkfs_prepare: image_prepare
485 $(call Image/mkfs/prepare)
487 kernel_prepare: mkfs_prepare
488 $(call Image/BuildKernel)
489 $(if $(CONFIG_TARGET_ROOTFS_INITRAMFS),$(if $(IB),,$(call Image/BuildKernel/Initramfs)))
490 $(call Image/InstallKernel)
492 $(foreach device,$(TARGET_DEVICES),$(call Device,$(device)))
493 $(foreach fs,$(TARGET_FILESYSTEMS) $(fs-subtypes-y),$(call BuildImage/mkfs,$(fs)))
495 $$(sort $$(_KERNEL_IMAGES)):
498 install: kernel_prepare
499 $(foreach fs,$(TARGET_FILESYSTEMS),
500 $(call Image/Build,$(fs))
502 $(call Image/mkfs/ubifs)
503 $(call Image/Checksum,md5sum --binary,md5sums)
504 $(call Image/Checksum,openssl dgst -sha256,sha256sums)