ramips: move different subtarget image generation to separate makefiles to improve...
[openwrt/openwrt.git] / target / linux / ramips / image / Makefile
1 #
2 # Copyright (C) 2008-2011 OpenWrt.org
3 #
4 # This is free software, licensed under the GNU General Public License v2.
5 # See /LICENSE for more information.
6 #
7 include $(TOPDIR)/rules.mk
8 include $(INCLUDE_DIR)/image.mk
9
10 define imgname
11 $(BIN_DIR)/$(IMG_PREFIX)-$(2)-$(1)
12 endef
13
14 define sysupname
15 $(call imgname,$(1),$(2))-sysupgrade.bin
16 endef
17
18 VMLINUX:=$(IMG_PREFIX)-vmlinux
19 UIMAGE:=$(IMG_PREFIX)-uImage
20
21 define Image/Build/Initramfs
22 $(call Image/Build/Profile/$(PROFILE),initramfs)
23 endef
24
25 DEVICE_VARS += DTS IMAGE_SIZE
26
27 loadaddr-y := 0x80000000
28 loadaddr-$(CONFIG_TARGET_ramips_rt288x) := 0x88000000
29 loadaddr-$(CONFIG_TARGET_ramips_mt7621) := 0x80001000
30
31 KERNEL_LOADADDR := $(loadaddr-y)
32
33 KERNEL_DTB = kernel-bin | patch-dtb | lzma
34 define Device/Default
35 KERNEL_DEPENDS = $$(wildcard ../dts/$$(DTS).dts)
36 KERNEL := $(KERNEL_DTB) | uImage lzma
37 IMAGES := sysupgrade.bin
38 IMAGE_SIZE := $(ralink_default_fw_size_8M)
39 IMAGE/sysupgrade.bin := append-kernel | append-rootfs | pad-rootfs | check-size $$$$(IMAGE_SIZE)
40 endef
41
42 define Build/patch-dtb
43 $(call Image/BuildDTB,../dts/$(DTS).dts,$@.dtb)
44 $(STAGING_DIR_HOST)/bin/patch-dtb $@ $@.dtb
45 endef
46
47 define Build/trx
48 $(STAGING_DIR_HOST)/bin/trx \
49 -o $@ \
50 -m $(IMAGE_SIZE) \
51 -f $(word 1,$^) \
52 -a 4 -f $(word 2,$^)
53 endef
54
55 define Build/relocate-kernel
56 ( \
57 dd if=$(KDIR)/loader.bin bs=32 conv=sync && \
58 perl -e '@s = stat("$@"); print pack("V", @s[7])' && \
59 cat $@ \
60 ) > $@.new
61 mv $@.new $@
62 endef
63
64 define MkCombineduImage
65 $(call PatchKernelLzma,$(2),$(3))
66 if [ `stat -c%s "$(KDIR)/vmlinux-$(2).bin.lzma"` -gt `expr $(4) - 64` ]; then \
67 echo "Warning: $(KDIR)/vmlinux-$(2).bin.lzma is too big" >&2; \
68 else if [ `stat -c%s "$(KDIR)/root.$(1)"` -gt $(5) ]; then \
69 echo "Warning: $(KDIR)/root.$(1) is too big" >&2; \
70 else \
71 ( dd if=$(KDIR)/vmlinux-$(2).bin.lzma bs=`expr $(4) - 64` conv=sync ; dd if=$(KDIR)/root.$(1) ) > $(KDIR)/vmlinux-$(2).bin.lzma.combined ; \
72 fi ; fi
73 $(call MkImage,lzma,$(KDIR)/vmlinux-$(2).bin.lzma.combined,$(call sysupname,$(1),$(2)),$(6))
74 endef
75
76 #
77 # The real magic happens inside these templates
78 #
79 # $(1), compression method
80 # $(2), filename of image data
81 # $(3), output filename
82 define MkImage
83 $(eval imagename=$(if $(4),$(4),MIPS OpenWrt Linux-$(LINUX_VERSION)))
84 -mkimage -A mips -O linux -T kernel -C $(1) -a $(loadaddr-y) -e $(loadaddr-y) \
85 -n "$(imagename)" \
86 -d $(2) $(3)
87 endef
88
89 define CompressLzma
90 $(STAGING_DIR_HOST)/bin/lzma e $(1) -lc1 -lp2 -pb2 $(2)
91 endef
92
93 define MkImageSysupgrade/squashfs
94 $(eval output_name=$(IMG_PREFIX)-$(2)-$(1)-$(if $(4),$(4),sysupgrade).bin)
95 cat $(KDIR)/vmlinux-$(2).uImage $(KDIR)/root.$(1) > $(KDIR)/$(output_name)
96 $(call prepare_generic_squashfs,$(KDIR)/$(output_name))
97 if [ `stat -c%s "$(KDIR)/$(output_name)"` -gt $(3) ]; then \
98 echo "Warning: $(KDIR)/$(output_name) is too big" >&2; \
99 else \
100 $(CP) $(KDIR)/$(output_name) $(BIN_DIR)/$(output_name); \
101 fi
102 endef
103
104 # $(1), lowercase board name like "mt7620a_v22sg"
105 # $(2), DTS filename without .dts extension
106 # $(3), optional filename suffix, e.g. "-initramfs"
107 define PatchKernelLzmaDtb
108 cp $(KDIR)/vmlinux$(3) $(KDIR)/vmlinux-$(1)$(3)
109 $(LINUX_DIR)/scripts/dtc/dtc -O dtb -o $(KDIR)/$(2).dtb ../dts/$(2).dts
110 $(STAGING_DIR_HOST)/bin/patch-dtb $(KDIR)/vmlinux-$(1)$(3) $(KDIR)/$(2).dtb
111 $(call CompressLzma,$(KDIR)/vmlinux-$(1)$(3),$(KDIR)/vmlinux-$(1)$(3).bin.lzma)
112 endef
113
114 # $(1), lowercase board name
115 # $(2), DTS filename without .dts extension
116 # $(3), ih_name field of uImage header
117 # $(4), optional filename suffix, e.g. "-initramfs"
118 define MkImageLzmaDtb
119 $(call PatchKernelLzmaDtb,$(1),$(2),$(4))
120 $(call MkImage,lzma,$(KDIR)/vmlinux-$(1)$(4).bin.lzma,$(KDIR)/vmlinux-$(1)$(4).uImage,$(3))
121 endef
122
123 # $(1), Rootfs type, e.g. squashfs
124 # $(2), lowercase board name
125 # $(3), DTS filename without .dts extension
126 # $(4), maximum size of sysupgrade image
127 # $(5), uImage header's ih_name field
128 define BuildFirmware/OF
129 $(call MkImageLzmaDtb,$(2),$(3),$(5))
130 $(call MkImageSysupgrade/$(1),$(1),$(2),$(4),$(6))
131 endef
132
133 # $(1), squashfs/initramfs
134 # $(2), lowercase board name
135 # $(3), DTS filename without .dts extension
136 # $(4), ih_name field of uImage header
137 define BuildFirmware/OF/initramfs
138 $(call MkImageLzmaDtb,$(2),$(3),$(4),-initramfs)
139 $(CP) $(KDIR)/vmlinux-$(2)-initramfs.uImage $(call imgname,$(1),$(2))-uImage.bin
140 endef
141
142 # Build images for default ralink layout for 4MB flash
143 # kernel + roots = 0x3b0000
144 # $(1) = squashfs/initramfs
145 # $(2) = lowercase board name
146 # $(3) = dts file
147 ralink_default_fw_size_4M=3866624
148 BuildFirmware/Default4M/squashfs=$(call BuildFirmware/OF,$(1),$(2),$(3),$(ralink_default_fw_size_4M),$(4))
149 BuildFirmware/Default4M/initramfs=$(call BuildFirmware/OF/initramfs,$(1),$(2),$(3),$(4))
150
151 # Build images for default ralink layout for 8MB flash
152 # kernel + roots = 0x7b0000
153 # $(1) = squashfs/initramfs
154 # $(2) = lowercase board name
155 # $(3) = dts file
156 # $(4) = uImage header name field
157 ralink_default_fw_size_8M=8060928
158 BuildFirmware/Default8M/squashfs=$(call BuildFirmware/OF,$(1),$(2),$(3),$(ralink_default_fw_size_8M),$(4))
159 BuildFirmware/Default8M/initramfs=$(call BuildFirmware/OF/initramfs,$(1),$(2),$(3),$(4))
160
161 ralink_default_fw_size_16M=16121856
162 BuildFirmware/Default16M/squashfs=$(call BuildFirmware/OF,$(1),$(2),$(3),$(ralink_default_fw_size_16M),$(4))
163 BuildFirmware/Default16M/initramfs=$(call BuildFirmware/OF/initramfs,$(1),$(2),$(3),$(4))
164
165 ralink_default_fw_size_32M=33226752
166 BuildFirmware/Default32M/squashfs=$(call BuildFirmware/OF,$(1),$(2),$(3),$(ralink_default_fw_size_32M),$(4))
167 BuildFirmware/Default32M/initramfs=$(call BuildFirmware/OF/initramfs,$(1),$(2),$(3),$(4))
168
169 # Build images for a custom sized flash layout
170 # $(1) = squashfs/initramfs
171 # $(2) = lowercase board name
172 # $(3) = dts file
173 # $(4) = kernel + rootfs size
174 BuildFirmware/CustomFlash/squashfs=$(call BuildFirmware/OF,$(1),$(2),$(3),$(4),$(5),$(6))
175 BuildFirmware/CustomFlash/initramfs=$(call BuildFirmware/OF/initramfs,$(1),$(2),$(3))
176
177 # Some boards need a special header inside the uImage to make them bootable
178 define BuildFirmware/CustomFlashFactory/squashfs
179 $(call BuildFirmware/CustomFlash/$(1),$(1),$(2),$(3),$(4))
180 $(call BuildFirmware/CustomFlash/$(1),$(1),$(2),$(3),$(4),$(5),$(6))
181 endef
182 BuildFirmware/CustomFlashFactory/initramfs=$(call BuildFirmware/OF/initramfs,$(1),$(2),$(3))
183
184 # wrappers for boards that have 4MB and 8MB versions
185 define BuildFirmware/DefaultDualSize/squashfs
186 $(call BuildFirmware/Default4M/$(1),$(1),$(2)-4M,$(3)-4M)
187 $(call BuildFirmware/Default8M/$(1),$(1),$(2)-8M,$(3)-8M)
188 endef
189 define BuildFirmware/DefaultDualSize/initramfs
190 $(call BuildFirmware/OF/initramfs,$(1),$(2)-4M,$(3)-4M)
191 $(call BuildFirmware/OF/initramfs,$(1),$(2)-8M,$(3)-8M)
192 endef
193
194 # build Seama header images
195 define BuildFirmware/Seama/squashfs
196 $(call MkImageLzmaDtb,$(2),$(3),$(5))
197 $(eval output_name=$(IMG_PREFIX)-$(2)-$(1)-sysupgrade.bin)
198 cat $(KDIR)/vmlinux-$(2).bin.lzma $(KDIR)/root.$(1) > $(KDIR)/img_$(2).$(1).tmp
199 if [ `stat -c%s "$(KDIR)/img_$(2).$(1).tmp"` -gt $$$$(($(5) - 64)) ]; then \
200 echo "Warning: $(KDIR)/img_$(2).$(1).tmp is too big" >&2; \
201 else \
202 dd if=$(KDIR)/vmlinux-$(2).bin.lzma of=$(KDIR)/vmlinux-$(2).bin.lzma.padded bs=64k conv=sync; \
203 ( \
204 dd if=$(KDIR)/vmlinux-$(2).bin.lzma.padded bs=1 count=`expr \`stat -c%s $(KDIR)/vmlinux-$(2).bin.lzma.padded\` - 64`; \
205 dd if=$(KDIR)/root.$(1) bs=64k conv=sync; \
206 ) > $(KDIR)/vmlinux-$(2).tmp; \
207 $(STAGING_DIR_HOST)/bin/seama \
208 -i $(KDIR)/vmlinux-$(2).tmp \
209 -m "dev=/dev/mtdblock/2" -m "type=firmware"; \
210 $(STAGING_DIR_HOST)/bin/seama \
211 -s $(call imgname,$(1),$(2))-factory.bin \
212 -m "signature=$(4)" \
213 -i $(KDIR)/vmlinux-$(2).tmp.seama; \
214 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; \
215 $(STAGING_DIR_HOST)/bin/seama \
216 -i $(KDIR)/vmlinux-$(2)-sysupgrade.tmp \
217 -m "dev=/dev/mtdblock/2" -m "type=firmware"; \
218 ( \
219 dd if=$(KDIR)/vmlinux-$(2)-sysupgrade.tmp.seama; \
220 dd if=$(KDIR)/root.$(1) bs=64k conv=sync; \
221 ) > $(BIN_DIR)/$(output_name); \
222 fi
223 endef
224 BuildFirmware/Seama/initramfs=$(call BuildFirmware/OF/initramfs,$(1),$(2),$(3))
225
226 define BuildFirmware/PorayDualSize/squashfs
227 $(call BuildFirmware/DefaultDualSize/$(1),$(1),$(2),$(3))
228 if [ -e "$(call sysupname,$(1),$(2)-4M)" ]; then \
229 mkporayfw -B $(3) -F 4M \
230 -f $(call sysupname,$(1),$(2)-4M) \
231 -o $(call imgname,$(1),$(2)-4M)-factory.bin; \
232 fi
233 if [ -e "$(call sysupname,$(1),$(2)-8M)" ]; then \
234 mkporayfw -B $(3) -F 8M \
235 -f $(call sysupname,$(1),$(2)-8M) \
236 -o $(call imgname,$(1),$(2)-8M)-factory.bin; \
237 fi
238 endef
239 BuildFirmware/PorayDualSize/initramfs=$(call BuildFirmware/DefaultDualSize/initramfs,$(1),$(2),$(3))
240
241
242 ifeq ($(SUBTARGET),rt288x)
243 include rt288x.mk
244 endif
245
246 ifeq ($(SUBTARGET),rt305x)
247 include rt305x.mk
248 endif
249
250 ifeq ($(SUBTARGET),rt3883)
251 include rt3883.mk
252 endif
253
254 ifeq ($(SUBTARGET),mt7620)
255 include mt7620.mk
256 endif
257
258 ifeq ($(SUBTARGET),mt7621)
259 include mt7621.mk
260 endif
261
262 ifeq ($(SUBTARGET),mt7628)
263 include mt7628.mk
264 endif
265
266 ifeq ($(SUBTARGET),mt7688)
267 include mt7688.mk
268 endif
269
270
271 ifndef TARGET_DEVICES
272 #
273 # Generic Targets - only needed for legacy image building code
274 #
275 define Image/BuildKernel
276 cp $(KDIR)/vmlinux.elf $(BIN_DIR)/$(VMLINUX).elf
277 cp $(KDIR)/vmlinux $(BIN_DIR)/$(VMLINUX).bin
278 $(call CompressLzma,$(KDIR)/vmlinux,$(KDIR)/vmlinux.bin.lzma)
279 $(call MkImage,lzma,$(KDIR)/vmlinux.bin.lzma,$(KDIR)/uImage.lzma)
280 cp $(KDIR)/uImage.lzma $(BIN_DIR)/$(UIMAGE).bin
281 endef
282
283 define Image/BuildKernel/Initramfs
284 cp $(KDIR)/vmlinux-initramfs.elf $(BIN_DIR)/$(VMLINUX)-initramfs.elf
285 cp $(KDIR)/vmlinux-initramfs $(BIN_DIR)/$(VMLINUX)-initramfs.bin
286 $(call CompressLzma,$(KDIR)/vmlinux-initramfs,$(KDIR)/vmlinux-initramfs.bin.lzma)
287 $(call MkImage,lzma,$(KDIR)/vmlinux-initramfs.bin.lzma,$(KDIR)/uImage-initramfs.lzma)
288 cp $(KDIR)/uImage-initramfs.lzma $(BIN_DIR)/$(UIMAGE)-initramfs.bin
289 $(call Image/Build/Initramfs)
290 endef
291
292 define Image/Build
293 $(call Image/Build/$(1))
294 dd if=$(KDIR)/root.$(1) of=$(BIN_DIR)/$(IMG_PREFIX)-root.$(1) bs=128k conv=sync
295 $(call Image/Build/Profile/$(PROFILE),$(1))
296 endef
297 endif
298
299 $(eval $(call BuildImage))