255aa8c5e64124f58b288c439598e4bfb3600673
[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 SUPPORTED_DEVICES = $(1)
41 sysupgrade_bin := append-kernel | append-rootfs | pad-rootfs
42 IMAGE/sysupgrade.bin := append-kernel | append-rootfs | pad-rootfs | append-metadata | check-size $$$$(IMAGE_SIZE)
43 endef
44
45 define Build/patch-dtb
46 $(call Image/BuildDTB,../dts/$(DTS).dts,$@.dtb)
47 $(STAGING_DIR_HOST)/bin/patch-dtb $@ $@.dtb
48 endef
49
50 define Build/trx
51 $(STAGING_DIR_HOST)/bin/trx \
52 -o $@ \
53 -m $(IMAGE_SIZE) \
54 -f $(IMAGE_KERNEL) \
55 -a 4 -f $(IMAGE_ROOTFS)
56 endef
57
58 define Build/relocate-kernel
59 ( \
60 dd if=$(KDIR)/loader.bin bs=32 conv=sync && \
61 perl -e '@s = stat("$@"); print pack("V", @s[7])' && \
62 cat $@ \
63 ) > $@.new
64 mv $@.new $@
65 endef
66
67 define MkCombineduImage
68 $(call PatchKernelLzma,$(2),$(3))
69 if [ `stat -c%s "$(KDIR)/vmlinux-$(2).bin.lzma"` -gt `expr $(4) - 64` ]; then \
70 echo "Warning: $(KDIR)/vmlinux-$(2).bin.lzma is too big" >&2; \
71 else if [ `stat -c%s "$(KDIR)/root.$(1)"` -gt $(5) ]; then \
72 echo "Warning: $(KDIR)/root.$(1) is too big" >&2; \
73 else \
74 ( dd if=$(KDIR)/vmlinux-$(2).bin.lzma bs=`expr $(4) - 64` conv=sync ; dd if=$(KDIR)/root.$(1) ) > $(KDIR)/vmlinux-$(2).bin.lzma.combined ; \
75 fi ; fi
76 $(call MkImage,lzma,$(KDIR)/vmlinux-$(2).bin.lzma.combined,$(call sysupname,$(1),$(2)),$(6))
77 endef
78
79 define Build/umedia-header
80 fix-u-media-header -T 0x46 -B $(1) -i $@ -o $@.new && mv $@.new $@
81 endef
82
83 define Build/edimax-header
84 $(STAGING_DIR_HOST)/bin/mkedimaximg -i $@ -o $@.new $(1)
85 @mv $@.new $@
86 endef
87
88 define Build/poray-header
89 $(STAGING_DIR_HOST)/bin/mkporayfw $(1) -f $@ -o $@.new
90 mv $@.new $@
91 endef
92
93 define Build/senao-header
94 $(STAGING_DIR_HOST)/bin/mksenaofw $(1) -e $@ -o $@.new
95 mv $@.new $@
96 endef
97
98 define Build/seama
99 $(STAGING_DIR_HOST)/bin/seama -i $@ $(1)
100 mv $@.seama $@
101 endef
102
103 define Build/seama-seal
104 $(call Build/seama,-s $@.seama $(1))
105 endef
106
107 define Build/wrg-header
108 mkwrgimg -i $@ -d "/dev/mtdblock/2" -s $(1) -o $@.new
109 mv $@.new $@
110 endef
111
112 #
113 # The real magic happens inside these templates
114 #
115 # $(1), compression method
116 # $(2), filename of image data
117 # $(3), output filename
118 define MkImage
119 $(eval imagename=$(if $(4),$(4),MIPS OpenWrt Linux-$(LINUX_VERSION)))
120 -mkimage -A mips -O linux -T kernel -C $(1) -a $(loadaddr-y) -e $(loadaddr-y) \
121 -n "$(imagename)" \
122 -d $(2) $(3)
123 endef
124
125 define CompressLzma
126 $(STAGING_DIR_HOST)/bin/lzma e $(1) -lc1 -lp2 -pb2 $(2)
127 endef
128
129 define MkImageSysupgrade/squashfs
130 $(eval output_name=$(IMG_PREFIX)-$(2)-$(1)-$(if $(4),$(4),sysupgrade).bin)
131 cat $(KDIR)/vmlinux-$(2).uImage $(KDIR)/root.$(1) > $(KDIR)/$(output_name)
132 $(call prepare_generic_squashfs,$(KDIR)/$(output_name))
133 if [ `stat -c%s "$(KDIR)/$(output_name)"` -gt $(3) ]; then \
134 echo "Warning: $(KDIR)/$(output_name) is too big" >&2; \
135 else \
136 $(CP) $(KDIR)/$(output_name) $(BIN_DIR)/$(output_name); \
137 fi
138 endef
139
140 # $(1), lowercase board name like "mt7620a_v22sg"
141 # $(2), DTS filename without .dts extension
142 # $(3), optional filename suffix, e.g. "-initramfs"
143 define PatchKernelLzmaDtb
144 cp $(KDIR)/vmlinux$(3) $(KDIR)/vmlinux-$(1)$(3)
145 $(call Image/BuildDTB,../dts/$(2).dts,$(KDIR)/$(2).dtb)
146 $(STAGING_DIR_HOST)/bin/patch-dtb $(KDIR)/vmlinux-$(1)$(3) $(KDIR)/$(2).dtb
147 $(call CompressLzma,$(KDIR)/vmlinux-$(1)$(3),$(KDIR)/vmlinux-$(1)$(3).bin.lzma)
148 endef
149
150 # $(1), lowercase board name
151 # $(2), DTS filename without .dts extension
152 # $(3), ih_name field of uImage header
153 # $(4), optional filename suffix, e.g. "-initramfs"
154 define MkImageLzmaDtb
155 $(call PatchKernelLzmaDtb,$(1),$(2),$(4))
156 $(call MkImage,lzma,$(KDIR)/vmlinux-$(1)$(4).bin.lzma,$(KDIR)/vmlinux-$(1)$(4).uImage,$(3))
157 endef
158
159 # $(1), Rootfs type, e.g. squashfs
160 # $(2), lowercase board name
161 # $(3), DTS filename without .dts extension
162 # $(4), maximum size of sysupgrade image
163 # $(5), uImage header's ih_name field
164 define BuildFirmware/OF
165 $(call MkImageLzmaDtb,$(2),$(3),$(5))
166 $(call MkImageSysupgrade/$(1),$(1),$(2),$(4),$(6))
167 endef
168
169 # $(1), squashfs/initramfs
170 # $(2), lowercase board name
171 # $(3), DTS filename without .dts extension
172 # $(4), ih_name field of uImage header
173 define BuildFirmware/OF/initramfs
174 $(call MkImageLzmaDtb,$(2),$(3),$(4),-initramfs)
175 $(CP) $(KDIR)/vmlinux-$(2)-initramfs.uImage $(call imgname,$(1),$(2))-uImage.bin
176 endef
177
178 # Build images for default ralink layout for 4MB flash
179 # kernel + roots = 0x3b0000
180 # $(1) = squashfs/initramfs
181 # $(2) = lowercase board name
182 # $(3) = dts file
183 ralink_default_fw_size_4M=3866624
184 BuildFirmware/Default4M/squashfs=$(call BuildFirmware/OF,$(1),$(2),$(3),$(ralink_default_fw_size_4M),$(4))
185 BuildFirmware/Default4M/initramfs=$(call BuildFirmware/OF/initramfs,$(1),$(2),$(3),$(4))
186
187 ralink_default_fw_size_8M=8060928
188 ralink_default_fw_size_16M=16121856
189 ralink_default_fw_size_32M=33226752
190
191
192 ifeq ($(SUBTARGET),rt288x)
193 include rt288x.mk
194 endif
195
196 ifeq ($(SUBTARGET),rt305x)
197 include rt305x.mk
198 include rt305x-legacy.mk
199 endif
200
201 ifeq ($(SUBTARGET),rt3883)
202 include rt3883.mk
203 endif
204
205 ifeq ($(SUBTARGET),mt7620)
206 include mt7620.mk
207 endif
208
209 ifeq ($(SUBTARGET),mt7621)
210 include mt7621.mk
211 endif
212
213 ifeq ($(SUBTARGET),mt7628)
214 include mt7628.mk
215 endif
216
217 ifeq ($(SUBTARGET),mt7688)
218 include mt7688.mk
219 endif
220
221 ifdef LEGACY_DEVICES
222 #
223 # Generic Targets - only needed for legacy image building code
224 #
225 define Image/BuildKernel
226 cp $(KDIR)/vmlinux.elf $(BIN_DIR)/$(VMLINUX).elf
227 cp $(KDIR)/vmlinux $(BIN_DIR)/$(VMLINUX).bin
228 $(call CompressLzma,$(KDIR)/vmlinux,$(KDIR)/vmlinux.bin.lzma)
229 $(call MkImage,lzma,$(KDIR)/vmlinux.bin.lzma,$(KDIR)/uImage.lzma)
230 cp $(KDIR)/uImage.lzma $(BIN_DIR)/$(UIMAGE).bin
231 endef
232
233 define Image/BuildKernel/Initramfs
234 cp $(KDIR)/vmlinux-initramfs.elf $(BIN_DIR)/$(VMLINUX)-initramfs.elf
235 cp $(KDIR)/vmlinux-initramfs $(BIN_DIR)/$(VMLINUX)-initramfs.bin
236 $(call CompressLzma,$(KDIR)/vmlinux-initramfs,$(KDIR)/vmlinux-initramfs.bin.lzma)
237 $(call MkImage,lzma,$(KDIR)/vmlinux-initramfs.bin.lzma,$(KDIR)/uImage-initramfs.lzma)
238 cp $(KDIR)/uImage-initramfs.lzma $(BIN_DIR)/$(UIMAGE)-initramfs.bin
239 $(call Image/Build/Initramfs)
240 endef
241
242 define Image/Build
243 $(call Image/Build/$(1))
244 dd if=$(KDIR)/root.$(1) of=$(BIN_DIR)/$(IMG_PREFIX)-root.$(1) bs=128k conv=sync
245 endef
246
247 define Image/Build/Profile
248 $(if $(CONFIG_TARGET_ROOTFS_INITRAMFS),$(call Image/Build/Profile/$(1),initramfs))
249 $(call Image/Build/Profile/$(1),$(2))
250 endef
251
252 endif
253
254 $(eval $(call BuildImage))