+$(KDIR)/root.%: kernel_prepare
+ $(call Image/mkfs/$(word 1,$(subst +,$(space),$*)),$(subst +,$(space),$*))
+
+define Device/InitProfile
+ PROFILES := $(PROFILE)
+ DEVICE_TITLE :=
+ DEVICE_PACKAGES :=
+ DEVICE_DESCRIPTION = Build firmware images for $$(DEVICE_TITLE)
+endef
+
+define Device/Init
+ DEVICE_NAME := $(1)
+ KERNEL:=
+ KERNEL_INITRAMFS = $$(KERNEL)
+ KERNEL_SIZE:=
+ CMDLINE:=
+
+ IMAGE_PREFIX := $(IMG_PREFIX)-$(1)
+ IMAGE_NAME = $$(IMAGE_PREFIX)-$$(1)-$$(2)
+ KERNEL_PREFIX = $$(IMAGE_PREFIX)
+ KERNEL_SUFFIX := -kernel.bin
+ KERNEL_INITRAMFS_SUFFIX = $$(KERNEL_SUFFIX)
+ KERNEL_IMAGE = $$(KERNEL_PREFIX)$$(KERNEL_SUFFIX)
+ KERNEL_INITRAMFS_PREFIX = $$(IMAGE_PREFIX)-initramfs
+ KERNEL_INITRAMFS_IMAGE = $$(KERNEL_INITRAMFS_PREFIX)$$(KERNEL_INITRAMFS_SUFFIX)
+ KERNEL_INITRAMFS_NAME = $$(KERNEL_NAME)-initramfs
+ KERNEL_INSTALL :=
+ KERNEL_NAME := vmlinux
+ KERNEL_DEPENDS :=
+ KERNEL_SIZE :=
+
+ UBOOTENV_IN_UBI :=
+ KERNEL_IN_UBI :=
+ BLOCKSIZE :=
+ PAGESIZE :=
+ SUBPAGESIZE :=
+ UBINIZE_OPTS := -E 5
+ MKUBIFS_OPTS :=
+
+ FS_OPTIONS/ubifs = $$(MKUBIFS_OPTS)
+
+ DEVICE_DTS :=
+ DEVICE_DTS_DIR :=
+
+ FILESYSTEMS := $(TARGET_FILESYSTEMS)
+endef
+
+DEFAULT_DEVICE_VARS := \
+ DEVICE_NAME KERNEL KERNEL_INITRAMFS KERNEL_INITRAMFS_IMAGE \
+ DEVICE_DTS DEVICE_DTS_DIR \
+ UBOOTENV_IN_UBI KERNEL_IN_UBI \
+ BLOCKSIZE PAGESIZE SUBPAGESIZE \
+ UBINIZE_OPTS
+
+define Device/ExportVar
+ $(1) : $(2):=$$($(2))
+
+endef
+define Device/Export
+ $(foreach var,$(DEVICE_VARS) $(DEFAULT_DEVICE_VARS),$(call Device/ExportVar,$(1),$(var)))
+ $(1) : FILESYSTEM:=$(2)
+endef
+
+ifdef IB
+ DEVICE_CHECK_PROFILE = $(filter $(1),$(PROFILE))
+else
+ DEVICE_CHECK_PROFILE = $(CONFIG_TARGET_$(if $(CONFIG_TARGET_MULTI_PROFILE),DEVICE_)$(call target_conf,$(BOARD)$(if $(SUBTARGET),_$(SUBTARGET)))_$(1))
+endif
+
+define Device/Check
+ _PROFILE_SET = $$(strip $$(foreach profile,$$(PROFILES) DEVICE_$(1),$$(call DEVICE_CHECK_PROFILE,$$(profile))))
+ _TARGET := $$(if $$(_PROFILE_SET),install-images,install-disabled)
+ ifndef IB
+ _COMPILE_TARGET := $$(if $(CONFIG_IB)$$(_PROFILE_SET),compile,compile-disabled)
+ endif
+endef
+
+ifndef IB
+define Device/Build/initramfs
+ $(call Device/Export,$(KDIR)/tmp/$$(KERNEL_INITRAMFS_IMAGE),$(1))
+ $$(_TARGET): $$(if $$(KERNEL_INITRAMFS),$(BIN_DIR)/$$(KERNEL_INITRAMFS_IMAGE))
+
+ $(KDIR)/$$(KERNEL_INITRAMFS_NAME):: image_prepare
+ $(BIN_DIR)/$$(KERNEL_INITRAMFS_IMAGE): $(KDIR)/tmp/$$(KERNEL_INITRAMFS_IMAGE)
+ cp $$^ $$@
+
+ $(KDIR)/tmp/$$(KERNEL_INITRAMFS_IMAGE): $(KDIR)/$$(KERNEL_INITRAMFS_NAME) $(CURDIR)/Makefile $$(KERNEL_DEPENDS)
+ @rm -f $$@
+ $$(call concat_cmd,$$(KERNEL_INITRAMFS))
+endef
+endif
+
+define Device/Build/check_size
+ @[ $$(($(subst k,* 1024,$(subst m, * 1024k,$(1))))) -ge "$$(stat -c%s $@)" ] || { \
+ echo "WARNING: Image file $@ is too big" >&2; \
+ rm -f $@; \
+ }
+endef
+
+define Device/Build/compile
+ $$(_COMPILE_TARGET): $(KDIR)/$(1)
+ $(eval $(call Device/Export,$(KDIR)/$(1)))
+ $(KDIR)/$(1):
+ $$(call concat_cmd,$(COMPILE/$(1)))
+
+endef
+
+define Device/Build/kernel
+ _KERNEL_IMAGES += $(KDIR)/$$(KERNEL_NAME)
+ $(KDIR)/$$(KERNEL_NAME):: image_prepare
+ $$(_TARGET): $$(if $$(KERNEL_INSTALL),$(BIN_DIR)/$$(KERNEL_IMAGE))
+ $(call Device/Export,$(KDIR)/$$(KERNEL_IMAGE),$(1))
+ $(BIN_DIR)/$$(KERNEL_IMAGE): $(KDIR)/$$(KERNEL_IMAGE)
+ cp $$^ $$@
+ ifndef IB
+ ifdef CONFIG_IB
+ install: $(KDIR)/$$(KERNEL_IMAGE)
+ endif
+ $(KDIR)/$$(KERNEL_IMAGE): $(KDIR)/$$(KERNEL_NAME) $(CURDIR)/Makefile $$(KERNEL_DEPENDS)
+ @rm -f $$@
+ $$(call concat_cmd,$$(KERNEL))
+ $$(if $$(KERNEL_SIZE),$$(call Device/Build/check_size,$$(KERNEL_SIZE)))
+ endif
+endef
+
+define Device/Build/image
+ $$(_TARGET): $(BIN_DIR)/$(call IMAGE_NAME,$(1),$(2))
+ $(eval $(call Device/Export,$(KDIR)/tmp/$(call IMAGE_NAME,$(1),$(2)),$(1)))
+ $(KDIR)/tmp/$(call IMAGE_NAME,$(1),$(2)): $(KDIR)/$$(KERNEL_IMAGE) $(KDIR)/root.$(1)$$(if $$(FS_OPTIONS/$(1)),+fs=$$(call param_mangle,$$(FS_OPTIONS/$(1))))
+ @rm -f $$@
+ [ -f $$(word 1,$$^) -a -f $$(word 2,$$^) ]
+ $$(call concat_cmd,$(if $(IMAGE/$(2)/$(1)),$(IMAGE/$(2)/$(1)),$(IMAGE/$(2))))
+
+ .IGNORE: $(BIN_DIR)/$(call IMAGE_NAME,$(1),$(2))
+ $(BIN_DIR)/$(call IMAGE_NAME,$(1),$(2)): $(KDIR)/tmp/$(call IMAGE_NAME,$(1),$(2))
+ cp $$^ $$@
+
+endef
+
+define Device/Build
+ $(if $(CONFIG_TARGET_ROOTFS_INITRAMFS),$(call Device/Build/initramfs,$(1)))
+ $(call Device/Build/kernel,$(1))
+
+ $$(eval $$(foreach compile,$$(COMPILE), \
+ $$(call Device/Build/compile,$$(compile),$(1))))
+
+ $$(eval $$(foreach image,$$(IMAGES), \
+ $$(foreach fs,$$(filter $(TARGET_FILESYSTEMS),$$(FILESYSTEMS)), \
+ $$(call Device/Build/image,$$(fs),$$(image),$(1)))))
+endef
+
+define Device/DumpInfo
+Target-Profile: DEVICE_$(1)
+Target-Profile-Name: $(DEVICE_TITLE)
+Target-Profile-Packages: $(DEVICE_PACKAGES)
+Target-Profile-Description:
+$(DEVICE_DESCRIPTION)
+@@
+
+endef
+
+define Device/Dump
+$$(eval $$(if $$(DEVICE_TITLE),$$(info $$(call Device/DumpInfo,$(1)))))
+endef
+
+define Device
+ $(call Device/InitProfile,$(1))
+ $(call Device/Init,$(1))
+ $(call Device/Default,$(1))
+ $(call Device/$(1),$(1))
+ $(call Device/Check,$(1))
+ $(call Device/$(if $(DUMP),Dump,Build),$(1))