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