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