Makefile 7.9 KB

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