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