+define ModelNameLimit16
+$(shell expr substr "$(word 2, $(subst _, ,$(1)))" 1 16)
+endef
+
+define rootfs_align
+$(patsubst %-256k,0x40000,$(patsubst %-128k,0x20000,$(patsubst %-64k,0x10000,$(patsubst squashfs%,0x4,$(patsubst root.%,%,$(1))))))
+endef
+
+
+define Build/append-dtb
+ cat $(KDIR)/image-$(firstword $(DEVICE_DTS)).dtb >> $@
+endef
+
+define Build/append-dtb-elf
+ $(TARGET_CROSS)objcopy \
+ --set-section-flags=.appended_dtb=alloc,contents \
+ --update-section \
+ .appended_dtb=$(KDIR)/image-$(firstword $(DEVICE_DTS)).dtb $@
+endef
+
+define Build/append-kernel
+ dd if=$(IMAGE_KERNEL) >> $@
+endef
+
+define Build/append-image
+ dd if=$(BIN_DIR)/$(DEVICE_IMG_PREFIX)-$(1) >> $@
+endef
+
+ifdef IB
+define Build/append-image-stage
+ dd if=$(STAGING_DIR_IMAGE)/$(BOARD)$(if $(SUBTARGET),-$(SUBTARGET))-$(DEVICE_NAME)-$(1) >> $@
+endef
+else
+define Build/append-image-stage
+ dd if=$(BIN_DIR)/$(DEVICE_IMG_PREFIX)-$(1) of=$(STAGING_DIR_IMAGE)/$(BOARD)$(if $(SUBTARGET),-$(SUBTARGET))-$(DEVICE_NAME)-$(1)
+ dd if=$(BIN_DIR)/$(DEVICE_IMG_PREFIX)-$(1) >> $@
+endef
+endif
+
+
+compat_version=$(if $(DEVICE_COMPAT_VERSION),$(DEVICE_COMPAT_VERSION),1.0)
+json_quote=$(subst ','\'',$(subst ",\",$(1)))
+#")')
+
+legacy_supported_message=$(SUPPORTED_DEVICES) - Image version mismatch: image $(compat_version), \
+ device 1.0. Please wipe config during upgrade (force required) or reinstall. \
+ $(if $(DEVICE_COMPAT_MESSAGE),Reason: $(DEVICE_COMPAT_MESSAGE),Please check documentation ...)
+
+metadata_devices=$(if $(1),$(subst "$(space)","$(comma)",$(strip $(foreach v,$(1),"$(call json_quote,$(v))"))))
+metadata_json = \
+ '{ $(if $(IMAGE_METADATA),$(IMAGE_METADATA)$(comma)) \
+ "metadata_version": "1.1", \
+ "compat_version": "$(call json_quote,$(compat_version))", \
+ $(if $(DEVICE_COMPAT_MESSAGE),"compat_message": "$(call json_quote,$(DEVICE_COMPAT_MESSAGE))"$(comma)) \
+ $(if $(filter-out 1.0,$(compat_version)),"new_supported_devices": \
+ [$(call metadata_devices,$(SUPPORTED_DEVICES))]$(comma) \
+ "supported_devices": ["$(call json_quote,$(legacy_supported_message))"]$(comma)) \
+ $(if $(filter 1.0,$(compat_version)),"supported_devices":[$(call metadata_devices,$(SUPPORTED_DEVICES))]$(comma)) \
+ "version": { \
+ "dist": "$(call json_quote,$(VERSION_DIST))", \
+ "version": "$(call json_quote,$(VERSION_NUMBER))", \
+ "revision": "$(call json_quote,$(REVISION))", \
+ "target": "$(call json_quote,$(TARGETID))", \
+ "board": "$(call json_quote,$(if $(BOARD_NAME),$(BOARD_NAME),$(DEVICE_NAME)))" \
+ } \
+ }'
+
+define Build/append-metadata
+ $(if $(SUPPORTED_DEVICES),-echo $(call metadata_json) | fwtool -I - $@)
+ [ ! -s "$(BUILD_KEY)" -o ! -s "$(BUILD_KEY).ucert" -o ! -s "$@" ] || { \
+ cp "$(BUILD_KEY).ucert" "$@.ucert" ;\
+ usign -S -m "$@" -s "$(BUILD_KEY)" -x "$@.sig" ;\
+ ucert -A -c "$@.ucert" -x "$@.sig" ;\
+ fwtool -S "$@.ucert" "$@" ;\
+ }
+endef
+
+define Build/append-rootfs
+ dd if=$(IMAGE_ROOTFS) >> $@
+endef
+
+define Build/append-squashfs-fakeroot-be
+ rm -rf $@.fakefs $@.fakesquashfs
+ mkdir $@.fakefs
+ $(STAGING_DIR_HOST)/bin/mksquashfs-lzma \
+ $@.fakefs $@.fakesquashfs \
+ -noappend -root-owned -be -nopad -b 65536 \
+ $(if $(SOURCE_DATE_EPOCH),-fixed-time $(SOURCE_DATE_EPOCH))
+ cat $@.fakesquashfs >> $@
+endef
+
+define Build/append-string
+ echo -n $(1) >> $@
+endef
+
+define Build/append-ubi
+ sh $(TOPDIR)/scripts/ubinize-image.sh \
+ $(if $(UBOOTENV_IN_UBI),--uboot-env) \
+ $(if $(KERNEL_IN_UBI),--kernel $(IMAGE_KERNEL)) \
+ $(foreach part,$(UBINIZE_PARTS),--part $(part)) \
+ $(IMAGE_ROOTFS) \
+ $@.tmp \
+ -p $(BLOCKSIZE:%k=%KiB) -m $(PAGESIZE) \
+ $(if $(SUBPAGESIZE),-s $(SUBPAGESIZE)) \
+ $(if $(VID_HDR_OFFSET),-O $(VID_HDR_OFFSET)) \
+ $(UBINIZE_OPTS)
+ cat $@.tmp >> $@
+ rm $@.tmp
+endef
+
+define Build/append-uboot
+ dd if=$(UBOOT_PATH) >> $@
+endef
+
+# append a fake/empty uImage header, to fool bootloaders rootfs integrity check
+# for example
+define Build/append-uImage-fakehdr
+ $(eval type=$(word 1,$(1)))
+ $(eval magic=$(word 2,$(1)))
+ touch $@.fakehdr
+ $(STAGING_DIR_HOST)/bin/mkimage \
+ -A $(LINUX_KARCH) -O linux -T $(type) -C none \
+ -n '$(VERSION_DIST) fake $(type)' \
+ $(if $(magic),-M $(magic)) \
+ -d $@.fakehdr \
+ -s \
+ $@.fakehdr
+ cat $@.fakehdr >> $@
+endef
+
+define Build/buffalo-dhp-image
+ $(STAGING_DIR_HOST)/bin/mkdhpimg $@ $@.new