openwrt/target/linux/ramips/image/Makefile
2016-07-31 12:25:25 +02:00

301 lines
9.9 KiB
Makefile

#
# Copyright (C) 2008-2011 OpenWrt.org
#
# This is free software, licensed under the GNU General Public License v2.
# See /LICENSE for more information.
#
include $(TOPDIR)/rules.mk
include $(INCLUDE_DIR)/image.mk
define imgname
$(BIN_DIR)/$(IMG_PREFIX)-$(2)-$(1)
endef
define sysupname
$(call imgname,$(1),$(2))-sysupgrade.bin
endef
VMLINUX:=$(IMG_PREFIX)-vmlinux
UIMAGE:=$(IMG_PREFIX)-uImage
define Image/Build/Initramfs
$(call Image/Build/Profile/$(PROFILE),initramfs)
endef
DEVICE_VARS += DTS IMAGE_SIZE KERNEL_SIZE
loadaddr-y := 0x80000000
loadaddr-$(CONFIG_TARGET_ramips_rt288x) := 0x88000000
loadaddr-$(CONFIG_TARGET_ramips_mt7621) := 0x80001000
KERNEL_LOADADDR := $(loadaddr-y)
KERNEL_DTB = kernel-bin | patch-dtb | lzma
define Device/Default
PROFILES = Default $$(DTS)
KERNEL_DEPENDS = $$(wildcard ../dts/$$(DTS).dts)
KERNEL := $(KERNEL_DTB) | uImage lzma
IMAGES := sysupgrade.bin
IMAGE_SIZE := $(ralink_default_fw_size_8M)
IMAGE/sysupgrade.bin := append-kernel | append-rootfs | pad-rootfs | check-size $$$$(IMAGE_SIZE)
endef
define Build/patch-dtb
$(call Image/BuildDTB,../dts/$(DTS).dts,$@.dtb)
$(STAGING_DIR_HOST)/bin/patch-dtb $@ $@.dtb
endef
define Build/trx
$(STAGING_DIR_HOST)/bin/trx \
-o $@ \
-m $(IMAGE_SIZE) \
-f $(IMAGE_KERNEL) \
-a 4 -f $(IMAGE_ROOTFS)
endef
define Build/relocate-kernel
( \
dd if=$(KDIR)/loader.bin bs=32 conv=sync && \
perl -e '@s = stat("$@"); print pack("V", @s[7])' && \
cat $@ \
) > $@.new
mv $@.new $@
endef
define MkCombineduImage
$(call PatchKernelLzma,$(2),$(3))
if [ `stat -c%s "$(KDIR)/vmlinux-$(2).bin.lzma"` -gt `expr $(4) - 64` ]; then \
echo "Warning: $(KDIR)/vmlinux-$(2).bin.lzma is too big" >&2; \
else if [ `stat -c%s "$(KDIR)/root.$(1)"` -gt $(5) ]; then \
echo "Warning: $(KDIR)/root.$(1) is too big" >&2; \
else \
( dd if=$(KDIR)/vmlinux-$(2).bin.lzma bs=`expr $(4) - 64` conv=sync ; dd if=$(KDIR)/root.$(1) ) > $(KDIR)/vmlinux-$(2).bin.lzma.combined ; \
fi ; fi
$(call MkImage,lzma,$(KDIR)/vmlinux-$(2).bin.lzma.combined,$(call sysupname,$(1),$(2)),$(6))
endef
#
# The real magic happens inside these templates
#
# $(1), compression method
# $(2), filename of image data
# $(3), output filename
define MkImage
$(eval imagename=$(if $(4),$(4),MIPS OpenWrt Linux-$(LINUX_VERSION)))
-mkimage -A mips -O linux -T kernel -C $(1) -a $(loadaddr-y) -e $(loadaddr-y) \
-n "$(imagename)" \
-d $(2) $(3)
endef
define CompressLzma
$(STAGING_DIR_HOST)/bin/lzma e $(1) -lc1 -lp2 -pb2 $(2)
endef
define MkImageSysupgrade/squashfs
$(eval output_name=$(IMG_PREFIX)-$(2)-$(1)-$(if $(4),$(4),sysupgrade).bin)
cat $(KDIR)/vmlinux-$(2).uImage $(KDIR)/root.$(1) > $(KDIR)/$(output_name)
$(call prepare_generic_squashfs,$(KDIR)/$(output_name))
if [ `stat -c%s "$(KDIR)/$(output_name)"` -gt $(3) ]; then \
echo "Warning: $(KDIR)/$(output_name) is too big" >&2; \
else \
$(CP) $(KDIR)/$(output_name) $(BIN_DIR)/$(output_name); \
fi
endef
# $(1), lowercase board name like "mt7620a_v22sg"
# $(2), DTS filename without .dts extension
# $(3), optional filename suffix, e.g. "-initramfs"
define PatchKernelLzmaDtb
cp $(KDIR)/vmlinux$(3) $(KDIR)/vmlinux-$(1)$(3)
$(call Image/BuildDTB,../dts/$(2).dts,$(KDIR)/$(2).dtb)
$(STAGING_DIR_HOST)/bin/patch-dtb $(KDIR)/vmlinux-$(1)$(3) $(KDIR)/$(2).dtb
$(call CompressLzma,$(KDIR)/vmlinux-$(1)$(3),$(KDIR)/vmlinux-$(1)$(3).bin.lzma)
endef
# $(1), lowercase board name
# $(2), DTS filename without .dts extension
# $(3), ih_name field of uImage header
# $(4), optional filename suffix, e.g. "-initramfs"
define MkImageLzmaDtb
$(call PatchKernelLzmaDtb,$(1),$(2),$(4))
$(call MkImage,lzma,$(KDIR)/vmlinux-$(1)$(4).bin.lzma,$(KDIR)/vmlinux-$(1)$(4).uImage,$(3))
endef
# $(1), Rootfs type, e.g. squashfs
# $(2), lowercase board name
# $(3), DTS filename without .dts extension
# $(4), maximum size of sysupgrade image
# $(5), uImage header's ih_name field
define BuildFirmware/OF
$(call MkImageLzmaDtb,$(2),$(3),$(5))
$(call MkImageSysupgrade/$(1),$(1),$(2),$(4),$(6))
endef
# $(1), squashfs/initramfs
# $(2), lowercase board name
# $(3), DTS filename without .dts extension
# $(4), ih_name field of uImage header
define BuildFirmware/OF/initramfs
$(call MkImageLzmaDtb,$(2),$(3),$(4),-initramfs)
$(CP) $(KDIR)/vmlinux-$(2)-initramfs.uImage $(call imgname,$(1),$(2))-uImage.bin
endef
# Build images for default ralink layout for 4MB flash
# kernel + roots = 0x3b0000
# $(1) = squashfs/initramfs
# $(2) = lowercase board name
# $(3) = dts file
ralink_default_fw_size_4M=3866624
BuildFirmware/Default4M/squashfs=$(call BuildFirmware/OF,$(1),$(2),$(3),$(ralink_default_fw_size_4M),$(4))
BuildFirmware/Default4M/initramfs=$(call BuildFirmware/OF/initramfs,$(1),$(2),$(3),$(4))
# Build images for default ralink layout for 8MB flash
# kernel + roots = 0x7b0000
# $(1) = squashfs/initramfs
# $(2) = lowercase board name
# $(3) = dts file
# $(4) = uImage header name field
ralink_default_fw_size_8M=8060928
BuildFirmware/Default8M/squashfs=$(call BuildFirmware/OF,$(1),$(2),$(3),$(ralink_default_fw_size_8M),$(4))
BuildFirmware/Default8M/initramfs=$(call BuildFirmware/OF/initramfs,$(1),$(2),$(3),$(4))
ralink_default_fw_size_16M=16121856
BuildFirmware/Default16M/squashfs=$(call BuildFirmware/OF,$(1),$(2),$(3),$(ralink_default_fw_size_16M),$(4))
BuildFirmware/Default16M/initramfs=$(call BuildFirmware/OF/initramfs,$(1),$(2),$(3),$(4))
ralink_default_fw_size_32M=33226752
BuildFirmware/Default32M/squashfs=$(call BuildFirmware/OF,$(1),$(2),$(3),$(ralink_default_fw_size_32M),$(4))
BuildFirmware/Default32M/initramfs=$(call BuildFirmware/OF/initramfs,$(1),$(2),$(3),$(4))
# Build images for a custom sized flash layout
# $(1) = squashfs/initramfs
# $(2) = lowercase board name
# $(3) = dts file
# $(4) = kernel + rootfs size
BuildFirmware/CustomFlash/squashfs=$(call BuildFirmware/OF,$(1),$(2),$(3),$(4),$(5),$(6))
BuildFirmware/CustomFlash/initramfs=$(call BuildFirmware/OF/initramfs,$(1),$(2),$(3))
# Some boards need a special header inside the uImage to make them bootable
define BuildFirmware/CustomFlashFactory/squashfs
$(call BuildFirmware/CustomFlash/$(1),$(1),$(2),$(3),$(4))
$(call BuildFirmware/CustomFlash/$(1),$(1),$(2),$(3),$(4),$(5),$(6))
endef
BuildFirmware/CustomFlashFactory/initramfs=$(call BuildFirmware/OF/initramfs,$(1),$(2),$(3))
# wrappers for boards that have 4MB and 8MB versions
define BuildFirmware/DefaultDualSize/squashfs
$(call BuildFirmware/Default4M/$(1),$(1),$(2)-4M,$(3)-4M)
$(call BuildFirmware/Default8M/$(1),$(1),$(2)-8M,$(3)-8M)
endef
define BuildFirmware/DefaultDualSize/initramfs
$(call BuildFirmware/OF/initramfs,$(1),$(2)-4M,$(3)-4M)
$(call BuildFirmware/OF/initramfs,$(1),$(2)-8M,$(3)-8M)
endef
# build Seama header images
define BuildFirmware/Seama/squashfs
$(call MkImageLzmaDtb,$(2),$(3),$(5))
$(eval output_name=$(IMG_PREFIX)-$(2)-$(1)-sysupgrade.bin)
cat $(KDIR)/vmlinux-$(2).bin.lzma $(KDIR)/root.$(1) > $(KDIR)/img_$(2).$(1).tmp
if [ `stat -c%s "$(KDIR)/img_$(2).$(1).tmp"` -gt $$$$(($(5) - 64)) ]; then \
echo "Warning: $(KDIR)/img_$(2).$(1).tmp is too big" >&2; \
else \
dd if=$(KDIR)/vmlinux-$(2).bin.lzma of=$(KDIR)/vmlinux-$(2).bin.lzma.padded bs=64k conv=sync; \
( \
dd if=$(KDIR)/vmlinux-$(2).bin.lzma.padded bs=1 count=`expr \`stat -c%s $(KDIR)/vmlinux-$(2).bin.lzma.padded\` - 64`; \
dd if=$(KDIR)/root.$(1) bs=64k conv=sync; \
) > $(KDIR)/vmlinux-$(2).tmp; \
$(STAGING_DIR_HOST)/bin/seama \
-i $(KDIR)/vmlinux-$(2).tmp \
-m "dev=/dev/mtdblock/2" -m "type=firmware"; \
$(STAGING_DIR_HOST)/bin/seama \
-s $(call imgname,$(1),$(2))-factory.bin \
-m "signature=$(4)" \
-i $(KDIR)/vmlinux-$(2).tmp.seama; \
dd if=$(KDIR)/vmlinux-$(2).bin.lzma.padded bs=1 count=`expr \`stat -c%s $(KDIR)/vmlinux-$(2).bin.lzma.padded\` - 64` of=$(KDIR)/vmlinux-$(2)-sysupgrade.tmp; \
$(STAGING_DIR_HOST)/bin/seama \
-i $(KDIR)/vmlinux-$(2)-sysupgrade.tmp \
-m "dev=/dev/mtdblock/2" -m "type=firmware"; \
( \
dd if=$(KDIR)/vmlinux-$(2)-sysupgrade.tmp.seama; \
dd if=$(KDIR)/root.$(1) bs=64k conv=sync; \
) > $(BIN_DIR)/$(output_name); \
fi
endef
BuildFirmware/Seama/initramfs=$(call BuildFirmware/OF/initramfs,$(1),$(2),$(3))
define BuildFirmware/PorayDualSize/squashfs
$(call BuildFirmware/DefaultDualSize/$(1),$(1),$(2),$(3))
if [ -e "$(call sysupname,$(1),$(2)-4M)" ]; then \
mkporayfw -B $(3) -F 4M \
-f $(call sysupname,$(1),$(2)-4M) \
-o $(call imgname,$(1),$(2)-4M)-factory.bin; \
fi
if [ -e "$(call sysupname,$(1),$(2)-8M)" ]; then \
mkporayfw -B $(3) -F 8M \
-f $(call sysupname,$(1),$(2)-8M) \
-o $(call imgname,$(1),$(2)-8M)-factory.bin; \
fi
endef
BuildFirmware/PorayDualSize/initramfs=$(call BuildFirmware/DefaultDualSize/initramfs,$(1),$(2),$(3))
ifeq ($(SUBTARGET),rt288x)
include rt288x.mk
endif
ifeq ($(SUBTARGET),rt305x)
include rt305x.mk
endif
ifeq ($(SUBTARGET),rt3883)
include rt3883.mk
endif
ifeq ($(SUBTARGET),mt7620)
include mt7620.mk
endif
ifeq ($(SUBTARGET),mt7621)
include mt7621.mk
endif
ifeq ($(SUBTARGET),mt7628)
include mt7628.mk
endif
ifeq ($(SUBTARGET),mt7688)
include mt7688.mk
endif
ifndef TARGET_DEVICES
#
# Generic Targets - only needed for legacy image building code
#
define Image/BuildKernel
cp $(KDIR)/vmlinux.elf $(BIN_DIR)/$(VMLINUX).elf
cp $(KDIR)/vmlinux $(BIN_DIR)/$(VMLINUX).bin
$(call CompressLzma,$(KDIR)/vmlinux,$(KDIR)/vmlinux.bin.lzma)
$(call MkImage,lzma,$(KDIR)/vmlinux.bin.lzma,$(KDIR)/uImage.lzma)
cp $(KDIR)/uImage.lzma $(BIN_DIR)/$(UIMAGE).bin
endef
define Image/BuildKernel/Initramfs
cp $(KDIR)/vmlinux-initramfs.elf $(BIN_DIR)/$(VMLINUX)-initramfs.elf
cp $(KDIR)/vmlinux-initramfs $(BIN_DIR)/$(VMLINUX)-initramfs.bin
$(call CompressLzma,$(KDIR)/vmlinux-initramfs,$(KDIR)/vmlinux-initramfs.bin.lzma)
$(call MkImage,lzma,$(KDIR)/vmlinux-initramfs.bin.lzma,$(KDIR)/uImage-initramfs.lzma)
cp $(KDIR)/uImage-initramfs.lzma $(BIN_DIR)/$(UIMAGE)-initramfs.bin
$(call Image/Build/Initramfs)
endef
define Image/Build
$(call Image/Build/$(1))
dd if=$(KDIR)/root.$(1) of=$(BIN_DIR)/$(IMG_PREFIX)-root.$(1) bs=128k conv=sync
$(call Image/Build/Profile/$(PROFILE),$(1))
endef
endif
$(eval $(call BuildImage))