Makefile 9.9 KB

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