Makefile 7.0 KB

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