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