2 # Copyright (C) 2008-2011 OpenWrt.org
4 # This is free software, licensed under the GNU General Public License v2.
5 # See /LICENSE for more information.
7 include $(TOPDIR)/rules.mk
8 include $(INCLUDE_DIR)/image.mk
11 $(BIN_DIR)/$(IMG_PREFIX)-$(2)-$(1)
15 $(call imgname,$(1),$(2))-sysupgrade.bin
18 VMLINUX:=$(IMG_PREFIX)-vmlinux
19 UIMAGE:=$(IMG_PREFIX)-uImage
21 define Image/Build/Initramfs
22 $(call Image/Build/Profile/$(PROFILE),initramfs)
25 DEVICE_VARS += DTS IMAGE_SIZE KERNEL_SIZE
27 loadaddr-y := 0x80000000
28 loadaddr-$(CONFIG_TARGET_ramips_rt288x) := 0x88000000
29 loadaddr-$(CONFIG_TARGET_ramips_mt7621) := 0x80001000
31 KERNEL_LOADADDR := $(loadaddr-y)
33 KERNEL_DTB = kernel-bin | patch-dtb | lzma
35 PROFILES = Default $$(DTS)
36 KERNEL_DEPENDS = $$(wildcard ../dts/$$(DTS).dts)
37 KERNEL := $(KERNEL_DTB) | uImage lzma
38 IMAGES := sysupgrade.bin
39 IMAGE_SIZE := $(ralink_default_fw_size_8M)
40 IMAGE/sysupgrade.bin := append-kernel | append-rootfs | pad-rootfs | check-size $$$$(IMAGE_SIZE)
43 define Build/patch-dtb
44 $(call Image/BuildDTB,../dts/$(DTS).dts,$@.dtb)
45 $(STAGING_DIR_HOST)/bin/patch-dtb $@ $@.dtb
49 $(STAGING_DIR_HOST)/bin/trx \
53 -a 4 -f $(IMAGE_ROOTFS)
56 define Build/relocate-kernel
58 dd if=$(KDIR)/loader.bin bs=32 conv=sync && \
59 perl -e '@s = stat("$@"); print pack("V", @s[7])' && \
65 define MkCombineduImage
66 $(call PatchKernelLzma,$(2),$(3))
67 if [ `stat -c%s "$(KDIR)/vmlinux-$(2).bin.lzma"` -gt `expr $(4) - 64` ]; then \
68 echo "Warning: $(KDIR)/vmlinux-$(2).bin.lzma is too big" >&2; \
69 else if [ `stat -c%s "$(KDIR)/root.$(1)"` -gt $(5) ]; then \
70 echo "Warning: $(KDIR)/root.$(1) is too big" >&2; \
72 ( dd if=$(KDIR)/vmlinux-$(2).bin.lzma bs=`expr $(4) - 64` conv=sync ; dd if=$(KDIR)/root.$(1) ) > $(KDIR)/vmlinux-$(2).bin.lzma.combined ; \
74 $(call MkImage,lzma,$(KDIR)/vmlinux-$(2).bin.lzma.combined,$(call sysupname,$(1),$(2)),$(6))
77 define Build/umedia-header
78 fix-u-media-header -T 0x46 -B $(1) -i $@ -o $@.new && mv $@.new $@
82 # The real magic happens inside these templates
84 # $(1), compression method
85 # $(2), filename of image data
86 # $(3), output filename
88 $(eval imagename=$(if $(4),$(4),MIPS OpenWrt Linux-$(LINUX_VERSION)))
89 -mkimage -A mips -O linux -T kernel -C $(1) -a $(loadaddr-y) -e $(loadaddr-y) \
95 $(STAGING_DIR_HOST)/bin/lzma e $(1) -lc1 -lp2 -pb2 $(2)
98 define MkImageSysupgrade/squashfs
99 $(eval output_name=$(IMG_PREFIX)-$(2)-$(1)-$(if $(4),$(4),sysupgrade).bin)
100 cat $(KDIR)/vmlinux-$(2).uImage $(KDIR)/root.$(1) > $(KDIR)/$(output_name)
101 $(call prepare_generic_squashfs,$(KDIR)/$(output_name))
102 if [ `stat -c%s "$(KDIR)/$(output_name)"` -gt $(3) ]; then \
103 echo "Warning: $(KDIR)/$(output_name) is too big" >&2; \
105 $(CP) $(KDIR)/$(output_name) $(BIN_DIR)/$(output_name); \
109 # $(1), lowercase board name like "mt7620a_v22sg"
110 # $(2), DTS filename without .dts extension
111 # $(3), optional filename suffix, e.g. "-initramfs"
112 define PatchKernelLzmaDtb
113 cp $(KDIR)/vmlinux$(3) $(KDIR)/vmlinux-$(1)$(3)
114 $(call Image/BuildDTB,../dts/$(2).dts,$(KDIR)/$(2).dtb)
115 $(STAGING_DIR_HOST)/bin/patch-dtb $(KDIR)/vmlinux-$(1)$(3) $(KDIR)/$(2).dtb
116 $(call CompressLzma,$(KDIR)/vmlinux-$(1)$(3),$(KDIR)/vmlinux-$(1)$(3).bin.lzma)
119 # $(1), lowercase board name
120 # $(2), DTS filename without .dts extension
121 # $(3), ih_name field of uImage header
122 # $(4), optional filename suffix, e.g. "-initramfs"
123 define MkImageLzmaDtb
124 $(call PatchKernelLzmaDtb,$(1),$(2),$(4))
125 $(call MkImage,lzma,$(KDIR)/vmlinux-$(1)$(4).bin.lzma,$(KDIR)/vmlinux-$(1)$(4).uImage,$(3))
128 # $(1), Rootfs type, e.g. squashfs
129 # $(2), lowercase board name
130 # $(3), DTS filename without .dts extension
131 # $(4), maximum size of sysupgrade image
132 # $(5), uImage header's ih_name field
133 define BuildFirmware/OF
134 $(call MkImageLzmaDtb,$(2),$(3),$(5))
135 $(call MkImageSysupgrade/$(1),$(1),$(2),$(4),$(6))
138 # $(1), squashfs/initramfs
139 # $(2), lowercase board name
140 # $(3), DTS filename without .dts extension
141 # $(4), ih_name field of uImage header
142 define BuildFirmware/OF/initramfs
143 $(call MkImageLzmaDtb,$(2),$(3),$(4),-initramfs)
144 $(CP) $(KDIR)/vmlinux-$(2)-initramfs.uImage $(call imgname,$(1),$(2))-uImage.bin
147 # Build images for default ralink layout for 4MB flash
148 # kernel + roots = 0x3b0000
149 # $(1) = squashfs/initramfs
150 # $(2) = lowercase board name
152 ralink_default_fw_size_4M=3866624
153 BuildFirmware/Default4M/squashfs=$(call BuildFirmware/OF,$(1),$(2),$(3),$(ralink_default_fw_size_4M),$(4))
154 BuildFirmware/Default4M/initramfs=$(call BuildFirmware/OF/initramfs,$(1),$(2),$(3),$(4))
156 # Build images for default ralink layout for 8MB flash
157 # kernel + roots = 0x7b0000
158 # $(1) = squashfs/initramfs
159 # $(2) = lowercase board name
161 # $(4) = uImage header name field
162 ralink_default_fw_size_8M=8060928
163 BuildFirmware/Default8M/squashfs=$(call BuildFirmware/OF,$(1),$(2),$(3),$(ralink_default_fw_size_8M),$(4))
164 BuildFirmware/Default8M/initramfs=$(call BuildFirmware/OF/initramfs,$(1),$(2),$(3),$(4))
166 ralink_default_fw_size_16M=16121856
167 BuildFirmware/Default16M/squashfs=$(call BuildFirmware/OF,$(1),$(2),$(3),$(ralink_default_fw_size_16M),$(4))
168 BuildFirmware/Default16M/initramfs=$(call BuildFirmware/OF/initramfs,$(1),$(2),$(3),$(4))
170 ralink_default_fw_size_32M=33226752
171 BuildFirmware/Default32M/squashfs=$(call BuildFirmware/OF,$(1),$(2),$(3),$(ralink_default_fw_size_32M),$(4))
172 BuildFirmware/Default32M/initramfs=$(call BuildFirmware/OF/initramfs,$(1),$(2),$(3),$(4))
174 # Build images for a custom sized flash layout
175 # $(1) = squashfs/initramfs
176 # $(2) = lowercase board name
178 # $(4) = kernel + rootfs size
179 BuildFirmware/CustomFlash/squashfs=$(call BuildFirmware/OF,$(1),$(2),$(3),$(4),$(5),$(6))
180 BuildFirmware/CustomFlash/initramfs=$(call BuildFirmware/OF/initramfs,$(1),$(2),$(3))
182 # Some boards need a special header inside the uImage to make them bootable
183 define BuildFirmware/CustomFlashFactory/squashfs
184 $(call BuildFirmware/CustomFlash/$(1),$(1),$(2),$(3),$(4))
185 $(call BuildFirmware/CustomFlash/$(1),$(1),$(2),$(3),$(4),$(5),$(6))
187 BuildFirmware/CustomFlashFactory/initramfs=$(call BuildFirmware/OF/initramfs,$(1),$(2),$(3))
189 # wrappers for boards that have 4MB and 8MB versions
190 define BuildFirmware/DefaultDualSize/squashfs
191 $(call BuildFirmware/Default4M/$(1),$(1),$(2)-4M,$(3)-4M)
192 $(call BuildFirmware/Default8M/$(1),$(1),$(2)-8M,$(3)-8M)
194 define BuildFirmware/DefaultDualSize/initramfs
195 $(call BuildFirmware/OF/initramfs,$(1),$(2)-4M,$(3)-4M)
196 $(call BuildFirmware/OF/initramfs,$(1),$(2)-8M,$(3)-8M)
199 # build Seama header images
200 define BuildFirmware/Seama/squashfs
201 $(call MkImageLzmaDtb,$(2),$(3),$(5))
202 $(eval output_name=$(IMG_PREFIX)-$(2)-$(1)-sysupgrade.bin)
203 cat $(KDIR)/vmlinux-$(2).bin.lzma $(KDIR)/root.$(1) > $(KDIR)/img_$(2).$(1).tmp
204 if [ `stat -c%s "$(KDIR)/img_$(2).$(1).tmp"` -gt $(5) ]; then \
205 echo "Warning: $(KDIR)/img_$(2).$(1).tmp is too big" >&2; \
207 dd if=$(KDIR)/vmlinux-$(2).bin.lzma of=$(KDIR)/vmlinux-$(2).bin.lzma.padded bs=64k conv=sync; \
209 dd if=$(KDIR)/vmlinux-$(2).bin.lzma.padded bs=1 count=`expr \`stat -c%s $(KDIR)/vmlinux-$(2).bin.lzma.padded\` - 64`; \
210 dd if=$(KDIR)/root.$(1) bs=64k conv=sync; \
211 ) > $(KDIR)/vmlinux-$(2).tmp; \
212 $(STAGING_DIR_HOST)/bin/seama \
213 -i $(KDIR)/vmlinux-$(2).tmp \
214 -m "dev=/dev/mtdblock/2" -m "type=firmware"; \
215 $(STAGING_DIR_HOST)/bin/seama \
216 -s $(call imgname,$(1),$(2))-factory.bin \
217 -m "signature=$(4)" \
218 -i $(KDIR)/vmlinux-$(2).tmp.seama; \
219 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; \
220 $(STAGING_DIR_HOST)/bin/seama \
221 -i $(KDIR)/vmlinux-$(2)-sysupgrade.tmp \
222 -m "dev=/dev/mtdblock/2" -m "type=firmware"; \
224 dd if=$(KDIR)/vmlinux-$(2)-sysupgrade.tmp.seama; \
225 dd if=$(KDIR)/root.$(1) bs=64k conv=sync; \
226 ) > $(BIN_DIR)/$(output_name); \
229 BuildFirmware/Seama/initramfs=$(call BuildFirmware/OF/initramfs,$(1),$(2),$(3))
231 define BuildFirmware/PorayDualSize/squashfs
232 $(call BuildFirmware/DefaultDualSize/$(1),$(1),$(2),$(3))
233 if [ -e "$(call sysupname,$(1),$(2)-4M)" ]; then \
234 mkporayfw -B $(3) -F 4M \
235 -f $(call sysupname,$(1),$(2)-4M) \
236 -o $(call imgname,$(1),$(2)-4M)-factory.bin; \
238 if [ -e "$(call sysupname,$(1),$(2)-8M)" ]; then \
239 mkporayfw -B $(3) -F 8M \
240 -f $(call sysupname,$(1),$(2)-8M) \
241 -o $(call imgname,$(1),$(2)-8M)-factory.bin; \
244 BuildFirmware/PorayDualSize/initramfs=$(call BuildFirmware/DefaultDualSize/initramfs,$(1),$(2),$(3))
247 ifeq ($(SUBTARGET),rt288x)
251 ifeq ($(SUBTARGET),rt305x)
253 include rt305x-legacy.mk
256 ifeq ($(SUBTARGET),rt3883)
260 ifeq ($(SUBTARGET),mt7620)
264 ifeq ($(SUBTARGET),mt7621)
268 ifeq ($(SUBTARGET),mt7628)
272 ifeq ($(SUBTARGET),mt7688)
278 # Generic Targets - only needed for legacy image building code
280 define Image/BuildKernel
281 cp $(KDIR)/vmlinux.elf $(BIN_DIR)/$(VMLINUX).elf
282 cp $(KDIR)/vmlinux $(BIN_DIR)/$(VMLINUX).bin
283 $(call CompressLzma,$(KDIR)/vmlinux,$(KDIR)/vmlinux.bin.lzma)
284 $(call MkImage,lzma,$(KDIR)/vmlinux.bin.lzma,$(KDIR)/uImage.lzma)
285 cp $(KDIR)/uImage.lzma $(BIN_DIR)/$(UIMAGE).bin
288 define Image/BuildKernel/Initramfs
289 cp $(KDIR)/vmlinux-initramfs.elf $(BIN_DIR)/$(VMLINUX)-initramfs.elf
290 cp $(KDIR)/vmlinux-initramfs $(BIN_DIR)/$(VMLINUX)-initramfs.bin
291 $(call CompressLzma,$(KDIR)/vmlinux-initramfs,$(KDIR)/vmlinux-initramfs.bin.lzma)
292 $(call MkImage,lzma,$(KDIR)/vmlinux-initramfs.bin.lzma,$(KDIR)/uImage-initramfs.lzma)
293 cp $(KDIR)/uImage-initramfs.lzma $(BIN_DIR)/$(UIMAGE)-initramfs.bin
294 $(call Image/Build/Initramfs)
298 $(call Image/Build/$(1))
299 dd if=$(KDIR)/root.$(1) of=$(BIN_DIR)/$(IMG_PREFIX)-root.$(1) bs=128k conv=sync
302 define Image/Build/Profile
303 $(call Image/Build/Profile/$(1),$(2))
308 $(eval $(call BuildImage))