ramips: move umedia build step to Makefile
[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 KERNEL_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 $(IMAGE_KERNEL) \
53 -a 4 -f $(IMAGE_ROOTFS)
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 define Build/umedia-header
78 fix-u-media-header -T 0x46 -B $(1) -i $@ -o $@.new && mv $@.new $@
79 endef
80
81 #
82 # The real magic happens inside these templates
83 #
84 # $(1), compression method
85 # $(2), filename of image data
86 # $(3), output filename
87 define MkImage
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) \
90 -n "$(imagename)" \
91 -d $(2) $(3)
92 endef
93
94 define CompressLzma
95 $(STAGING_DIR_HOST)/bin/lzma e $(1) -lc1 -lp2 -pb2 $(2)
96 endef
97
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; \
104 else \
105 $(CP) $(KDIR)/$(output_name) $(BIN_DIR)/$(output_name); \
106 fi
107 endef
108
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)
117 endef
118
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))
126 endef
127
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))
136 endef
137
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
145 endef
146
147 # Build images for default ralink layout for 4MB flash
148 # kernel + roots = 0x3b0000
149 # $(1) = squashfs/initramfs
150 # $(2) = lowercase board name
151 # $(3) = dts file
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))
155
156 # Build images for default ralink layout for 8MB flash
157 # kernel + roots = 0x7b0000
158 # $(1) = squashfs/initramfs
159 # $(2) = lowercase board name
160 # $(3) = dts file
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))
165
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))
169
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))
173
174 # Build images for a custom sized flash layout
175 # $(1) = squashfs/initramfs
176 # $(2) = lowercase board name
177 # $(3) = dts file
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))
181
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))
186 endef
187 BuildFirmware/CustomFlashFactory/initramfs=$(call BuildFirmware/OF/initramfs,$(1),$(2),$(3))
188
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)
193 endef
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)
197 endef
198
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; \
206 else \
207 dd if=$(KDIR)/vmlinux-$(2).bin.lzma of=$(KDIR)/vmlinux-$(2).bin.lzma.padded bs=64k conv=sync; \
208 ( \
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"; \
223 ( \
224 dd if=$(KDIR)/vmlinux-$(2)-sysupgrade.tmp.seama; \
225 dd if=$(KDIR)/root.$(1) bs=64k conv=sync; \
226 ) > $(BIN_DIR)/$(output_name); \
227 fi
228 endef
229 BuildFirmware/Seama/initramfs=$(call BuildFirmware/OF/initramfs,$(1),$(2),$(3))
230
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; \
237 fi
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; \
242 fi
243 endef
244 BuildFirmware/PorayDualSize/initramfs=$(call BuildFirmware/DefaultDualSize/initramfs,$(1),$(2),$(3))
245
246
247 ifeq ($(SUBTARGET),rt288x)
248 include rt288x.mk
249 endif
250
251 ifeq ($(SUBTARGET),rt305x)
252 include rt305x.mk
253 include rt305x-legacy.mk
254 endif
255
256 ifeq ($(SUBTARGET),rt3883)
257 include rt3883.mk
258 endif
259
260 ifeq ($(SUBTARGET),mt7620)
261 include mt7620.mk
262 endif
263
264 ifeq ($(SUBTARGET),mt7621)
265 include mt7621.mk
266 endif
267
268 ifeq ($(SUBTARGET),mt7628)
269 include mt7628.mk
270 endif
271
272 ifeq ($(SUBTARGET),mt7688)
273 include mt7688.mk
274 endif
275
276 ifdef LEGACY_DEVICES
277 #
278 # Generic Targets - only needed for legacy image building code
279 #
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
286 endef
287
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)
295 endef
296
297 define Image/Build
298 $(call Image/Build/$(1))
299 dd if=$(KDIR)/root.$(1) of=$(BIN_DIR)/$(IMG_PREFIX)-root.$(1) bs=128k conv=sync
300 endef
301
302 define Image/Build/Profile
303 $(call Image/Build/Profile/$(1),$(2))
304 endef
305
306 endif
307
308 $(eval $(call BuildImage))