generic: backport accepted and pending patches for mtk_eth_soc
[openwrt/staging/dangole.git] / include / image-commands.mk
index 1f6ba1c15a47e983692a0a100721e4420476818a..83ecf7c5209f9d4311c5afec89be6ccf71a611a1 100644 (file)
@@ -46,14 +46,15 @@ endef
 
 ifdef IB
 define Build/append-image-stage
-       dd if=$(STAGING_DIR_IMAGE)/$(BOARD)$(if $(SUBTARGET),-$(SUBTARGET))-$(DEVICE_NAME)-$(1) >> $@
+       dd if=$(STAGING_DIR_IMAGE)/$(BOARD)-$(SUBTARGET)-$(DEVICE_NAME)-$(1) >> $@
 endef
 else
 define Build/append-image-stage
        cp "$(BIN_DIR)/$(DEVICE_IMG_PREFIX)-$(1)" "$@.stripmeta"
        fwtool -s /dev/null -t "$@.stripmeta" || :
        fwtool -i /dev/null -t "$@.stripmeta" || :
-       dd if="$@.stripmeta" of="$(STAGING_DIR_IMAGE)/$(BOARD)$(if $(SUBTARGET),-$(SUBTARGET))-$(DEVICE_NAME)-$(1)"
+       mkdir -p "$(STAGING_DIR_IMAGE)"
+       dd if="$@.stripmeta" of="$(STAGING_DIR_IMAGE)/$(BOARD)-$(SUBTARGET)-$(DEVICE_NAME)-$(1)"
        dd if="$@.stripmeta" >> "$@"
        rm "$@.stripmeta"
 endef
@@ -105,7 +106,7 @@ endef
 define Build/append-squashfs-fakeroot-be
        rm -rf $@.fakefs $@.fakesquashfs
        mkdir $@.fakefs
-       $(STAGING_DIR_HOST)/bin/mksquashfs-lzma \
+       $(STAGING_DIR_HOST)/bin/mksquashfs3-lzma \
                $@.fakefs $@.fakesquashfs \
                -noappend -root-owned -be -nopad -b 65536 \
                $(if $(SOURCE_DATE_EPOCH),-fixed-time $(SOURCE_DATE_EPOCH))
@@ -125,6 +126,15 @@ define Build/append-string
        echo -n $(1) >> $@
 endef
 
+define Build/append-md5sum-ascii-salted
+       cp $@ $@.salted
+       echo -ne $(1) >> $@.salted
+       $(STAGING_DIR_HOST)/bin/mkhash md5 $@.salted | head -c32 >> $@
+       rm $@.salted
+endef
+
+UBI_NAND_SIZE_LIMIT = $(IMAGE_SIZE) - ($(NAND_SIZE)*20/1024 + 4*$(BLOCKSIZE))
+
 define Build/append-ubi
        sh $(TOPDIR)/scripts/ubinize-image.sh \
                $(if $(UBOOTENV_IN_UBI),--uboot-env) \
@@ -138,6 +148,22 @@ define Build/append-ubi
                $(UBINIZE_OPTS)
        cat $@.tmp >> $@
        rm $@.tmp
+       $(if $(and $(IMAGE_SIZE),$(NAND_SIZE)),\
+               $(call Build/check-size,$(UBI_NAND_SIZE_LIMIT)))
+endef
+
+define Build/ubinize-image
+       sh $(TOPDIR)/scripts/ubinize-image.sh \
+               $(if $(UBOOTENV_IN_UBI),--uboot-env) \
+               $(foreach part,$(UBINIZE_PARTS),--part $(part)) \
+               --part $(word 1,$(1))="$(BIN_DIR)/$(DEVICE_IMG_PREFIX)-$(word 2,$(1))" \
+               $@.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/ubinize-kernel
@@ -207,7 +233,7 @@ endef
 
 define Build/check-size
        @imagesize="$$(stat -c%s $@)"; \
-       limitsize="$$(($(subst k,* 1024,$(subst m, * 1024k,$(if $(1),$(1),$(IMAGE_SIZE))))))"; \
+       limitsize="$$(($(call exp_units,$(if $(1),$(1),$(IMAGE_SIZE)))))"; \
        [ $$limitsize -ge $$imagesize ] || { \
                $(call ERROR_MESSAGE,    WARNING: Image file $@ is too big: $$imagesize > $$limitsize); \
                rm -f $@; \
@@ -218,6 +244,48 @@ define Build/copy-file
        cat "$(1)" > "$@"
 endef
 
+# Create a header for a D-Link AI series recovery image and add it at the beginning of the image
+# Currently supported: AQUILA M30, EAGLE M32 and R32
+# Arguments:
+# 1: Start string of the header
+# 2: Firmware version
+# 3: Block start address
+# 4: Block length
+# 5: Device FMID
+define Build/dlink-ai-recovery-header
+       $(eval header_start=$(word 1,$(1)))
+       $(eval firmware_version=$(word 2,$(1)))
+       $(eval block_start=$(word 3,$(1)))
+       $(eval block_length=$(word 4,$(1)))
+       $(eval device_fmid=$(word 5,$(1)))
+# create $@.header without the checksum
+       echo -en "$(header_start)\x00\x00" > "$@.header"
+# Calculate checksum over data area ($@) and append it to the header.
+# The checksum is the 2byte-sum over the whole data area.
+# Every overflow during the checksum calculation must increment the current checksum value by 1.
+       od -v -w2 -tu2 -An --endian little "$@" | awk '{ s+=$$1; } END { s%=65535; printf "%c%c",s%256,s/256; }' >> "$@.header"
+       echo -en "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00" >> "$@.header"
+       echo -en "$(firmware_version)" >> "$@.header"
+# Only one block supported: Erase start/length is identical to data start/length
+       echo -en "$(block_start)$(block_length)$(block_start)$(block_length)" >> "$@.header"
+# Only zeros
+       echo -en "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" >> "$@.header"
+# Last 16 bytes, but without checksum
+       echo -en "\x42\x48\x02\x00\x00\x00\x08\x00\x00\x00\x00\x00" >> "$@.header"
+       echo -en "$(device_fmid)" >> "$@.header"
+# Calculate and append checksum: The checksum must be set so that the 2byte-sum of the whole header is 0.
+# Every overflow during the checksum calculation must increment the current checksum value by 1.
+       od -v -w2 -tu2 -An --endian little "$@.header" | awk '{s+=65535-$$1;}END{s%=65535;printf "%c%c",s%256,s/256;}' >> "$@.header"
+       cat "$@.header" "$@" > "$@.new"
+       mv "$@.new" "$@"
+       rm "$@.header"
+endef
+
+define Build/dlink-sge-image
+       $(STAGING_DIR_HOST)/bin/dlink-sge-image $(1) $@ $@.enc
+       mv $@.enc $@
+endef
+
 define Build/edimax-header
        $(STAGING_DIR_HOST)/bin/mkedimaximg -i $@ -o $@.new $(1)
        @mv $@.new $@
@@ -276,7 +344,9 @@ endef
 define Build/initrd_compression
        $(if $(CONFIG_TARGET_INITRAMFS_COMPRESSION_BZIP2),.bzip2) \
        $(if $(CONFIG_TARGET_INITRAMFS_COMPRESSION_GZIP),.gzip) \
+       $(if $(CONFIG_TARGET_INITRAMFS_COMPRESSION_LZ4),.lz4) \
        $(if $(CONFIG_TARGET_INITRAMFS_COMPRESSION_LZMA),.lzma) \
+       $(if $(CONFIG_TARGET_INITRAMFS_COMPRESSION_LZO),.lzo) \
        $(if $(CONFIG_TARGET_INITRAMFS_COMPRESSION_XZ),.xz) \
        $(if $(CONFIG_TARGET_INITRAMFS_COMPRESSION_ZSTD),.zstd)
 endef
@@ -297,7 +367,7 @@ define Build/fit
                $(if $(DEVICE_FDT_NUM),-n $(DEVICE_FDT_NUM)) \
                $(if $(DEVICE_DTS_DELIMITER),-l $(DEVICE_DTS_DELIMITER)) \
                $(if $(DEVICE_DTS_LOADADDR),-s $(DEVICE_DTS_LOADADDR)) \
-               $(if $(DEVICE_DTS_OVERLAY),$(foreach dtso,$(DEVICE_DTS_OVERLAY), -O $(dtso):$(KERNEL_BUILD_DIR)/image-$(dtso).dtb)) \
+               $(if $(DEVICE_DTS_OVERLAY),$(foreach dtso,$(DEVICE_DTS_OVERLAY), -O $(dtso):$(KERNEL_BUILD_DIR)/image-$(dtso).dtbo)) \
                -c $(if $(DEVICE_DTS_CONFIG),$(DEVICE_DTS_CONFIG),"config-1") \
                -A $(LINUX_KARCH) -v $(LINUX_VERSION)
        PATH=$(LINUX_DIR)/scripts/dtc:$(PATH) mkimage $(if $(findstring external,$(word 3,$(1))),\
@@ -305,11 +375,25 @@ define Build/fit
        @mv $@.new $@
 endef
 
+define Build/libdeflate-gzip
+       $(STAGING_DIR_HOST)/bin/libdeflate-gzip -f -12 -c $@ $(1) > $@.new
+       @mv $@.new $@
+endef
+
 define Build/gzip
-       gzip -f -9n -c $@ $(1) > $@.new
+       $(STAGING_DIR_HOST)/bin/gzip -f -9n -c $@ $(1) > $@.new
        @mv $@.new $@
 endef
 
+define Build/gzip-filename
+       @mkdir -p $@.tmp
+       @cp $@ $@.tmp/$(word 1,$(1))
+       $(if $(SOURCE_DATE_EPOCH),touch -hcd "@$(SOURCE_DATE_EPOCH)" $@.tmp/$(word 1,$(1)) $(word 2,$(1)))
+       $(STAGING_DIR_HOST)/bin/gzip -f -9 -N -c $@.tmp/$(word 1,$(1)) $(word 2,$(1)) > $@.new
+       @mv $@.new $@
+       @rm -rf $@.tmp
+endef
+
 define Build/install-dtb
        $(call locked, \
                $(foreach dts,$(DEVICE_DTS), \
@@ -360,10 +444,17 @@ define Build/kernel-bin
 endef
 
 define Build/linksys-image
-       $(TOPDIR)/scripts/linksys-image.sh \
+       let \
+               size="$$(stat -c%s $@)" \
+               pad="$(call exp_units,$(PAGESIZE))" \
+               offset="256" \
+               pad="(pad - ((size + offset) % pad)) % pad"; \
+               dd if=/dev/zero bs=$$pad count=1 | tr '\000' '\377' >> $@
+       printf ".LINKSYS.01000409%-15s%08X%-8s%-16s" \
                "$(call param_get_default,type,$(1),$(DEVICE_NAME))" \
-               $@ $@.new
-               mv $@.new $@
+               "$$(cksum $@ | cut -d ' ' -f1)" \
+               "0" "K0000000F0246434" >> $@
+       dd if=/dev/zero bs=192 count=1 >> $@
 endef
 
 define Build/lzma
@@ -375,6 +466,15 @@ define Build/lzma-no-dict
        @mv $@.new $@
 endef
 
+define Build/moxa-encode-fw
+       $(TOPDIR)/scripts/moxa-encode-fw.py \
+               --input $@ \
+               --output $@ \
+               --magic $(MOXA_MAGIC) \
+               --hwid $(MOXA_HWID) \
+               --buildid 00000000
+endef
+
 define Build/netgear-chk
        $(STAGING_DIR_HOST)/bin/mkchkimg \
                -o $@.new \
@@ -386,7 +486,7 @@ endef
 
 define Build/netgear-dni
        $(STAGING_DIR_HOST)/bin/mkdniimg \
-               -B $(NETGEAR_BOARD_ID) -v $(VERSION_DIST).$(firstword $(subst -, ,$(REVISION))) \
+               -B $(NETGEAR_BOARD_ID) -v $(shell cat $(VERSION_DIST)| sed -e 's/[[:space:]]/-/g').$(firstword $(subst -, ,$(REVISION))) \
                $(if $(NETGEAR_HW_ID),-H $(NETGEAR_HW_ID)) \
                -r "$(1)" \
                -i $@ -o $@.new
@@ -399,7 +499,9 @@ define Build/netgear-encrypted-factory
                --output-file $@ \
                --model $(NETGEAR_ENC_MODEL) \
                --region $(NETGEAR_ENC_REGION) \
-               --version V1.0.0.0.$(VERSION_DIST).$(firstword $(subst -, ,$(REVISION))) \
+               $(if $(NETGEAR_ENC_HW_ID_LIST),--hw-id-list "$(NETGEAR_ENC_HW_ID_LIST)") \
+               $(if $(NETGEAR_ENC_MODEL_LIST),--model-list "$(NETGEAR_ENC_MODEL_LIST)") \
+               --version V1.0.0.0.$(shell cat $(VERSION_DIST)| sed -e 's/[[:space:]]/-/g').$(firstword $(subst -, ,$(REVISION))) \
                --encryption-block-size 0x20000 \
                --openssl-bin "$(STAGING_DIR_HOST)/bin/openssl" \
                --key 6865392d342b4d212964363d6d7e7765312c7132613364316e26322a5a5e2538 \
@@ -426,8 +528,8 @@ endef
 define Build/pad-offset
        let \
                size="$$(stat -c%s $@)" \
-               pad="$(subst k,* 1024,$(word 1, $(1)))" \
-               offset="$(subst k,* 1024,$(word 2, $(1)))" \
+               pad="$(call exp_units,$(word 1, $(1)))" \
+               offset="$(call exp_units,$(word 2, $(1)))" \
                pad="(pad - ((size + offset) % pad)) % pad" \
                newsize='size + pad'; \
                dd if=$@ of=$@.new bs=$$newsize count=1 conv=sync
@@ -557,6 +659,7 @@ define Build/tplink-v2-image
 endef
 
 define Build/uImage
+       $(if $(UIMAGE_TIME),SOURCE_DATE_EPOCH="$(UIMAGE_TIME)") \
        mkimage \
                -A $(LINUX_KARCH) \
                -O linux \
@@ -588,7 +691,7 @@ endef
 
 define Build/zyxel-ras-image
        let \
-               newsize="$(subst k,* 1024,$(RAS_ROOTFS_SIZE))"; \
+               newsize="$(call exp_units,$(RAS_ROOTFS_SIZE))"; \
                $(STAGING_DIR_HOST)/bin/mkrasimage \
                        -b $(RAS_BOARD) \
                        -v $(RAS_VERSION) \