Makefile 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. # SPDX-License-Identifier: GPL-2.0-or-later
  2. include $(TOPDIR)/rules.mk
  3. include $(INCLUDE_DIR)/image.mk
  4. KERNEL_LOADADDR := 0x80010000 # RAM start + 64K
  5. LOADER_ENTRY := 0x81000000 # RAM start + 16M, for relocate
  6. LZMA_TEXT_START := 0x82000000 # RAM start + 32M
  7. DEVICE_VARS += CFE_BOARD_ID CFE_EXTRAS
  8. DEVICE_VARS += CFE_PART_FLAGS CFE_PART_ID
  9. DEVICE_VARS += CFE_RAM_FILE
  10. DEVICE_VARS += CFE_RAM_JFFS2_NAME CFE_RAM_JFFS2_PAD
  11. DEVICE_VARS += CFE_WFI_CHIP_ID CFE_WFI_FLASH_TYPE
  12. DEVICE_VARS += CFE_WFI_FLAGS CFE_WFI_VERSION
  13. DEVICE_VARS += CHIP_ID DEVICE_LOADADDR
  14. DEVICE_VARS += FLASH_MB IMAGE_OFFSET
  15. DEVICE_VARS += SERCOMM_FSVER SERCOMM_HWVER SERCOMM_SWVER
  16. define Build/Compile
  17. rm -rf $(KDIR)/relocate
  18. $(CP) ../../generic/image/relocate $(KDIR)
  19. $(MAKE) -C $(KDIR)/relocate \
  20. CACHELINE_SIZE=16 \
  21. CROSS_COMPILE=$(TARGET_CROSS) \
  22. KERNEL_ADDR=$(KERNEL_LOADADDR) \
  23. LZMA_TEXT_START=$(LOADER_ENTRY)
  24. endef
  25. ### Kernel scripts ###
  26. define Build/loader-lzma
  27. @rm -rf [email protected]
  28. $(MAKE) -C lzma-loader \
  29. CHIP_ID=$(CHIP_ID) \
  30. KERNEL_ADDR=$(KERNEL_LOADADDR) \
  31. KDIR=$(KDIR) \
  32. LOADER_ADDR=$(if $(DEVICE_LOADADDR),$(DEVICE_LOADADDR),$(LOADER_ENTRY)) \
  33. LOADER_DATA="$@" \
  34. LOADER_NAME="$(notdir $@)" \
  35. LZMA_TEXT_START=$(LZMA_TEXT_START) \
  36. PKG_BUILD_DIR="[email protected]" \
  37. TARGET_DIR="$(dir $@)" \
  38. compile loader.$(1)
  39. @mv "$@.$(1)" "$@"
  40. @rm -rf [email protected]
  41. endef
  42. define Build/lzma-cfe
  43. # CFE is a LZMA nazi! It took me hours to find out the parameters!
  44. # Also I think lzma has a bug cause it generates different output depending on
  45. # if you use stdin / stdout or not. Use files instead of stdio here, cause
  46. # otherwise CFE will complain and not boot the image.
  47. $(call Build/lzma-no-dict,-d22 -fb64 -a1)
  48. # Strip out the length, CFE doesn't like this
  49. dd if=$@ [email protected] bs=5 count=1
  50. dd if=$@ [email protected] ibs=13 obs=5 skip=1 seek=1 conv=notrunc
  51. @mv [email protected] $@
  52. endef
  53. define Build/relocate-kernel
  54. # CFE only allows ~4 MiB for the uncompressed kernels, but uncompressed
  55. # kernel might get larger than that, so let CFE unpack and load at a
  56. # higher address and make the kernel relocate itself to the expected
  57. # location.
  58. ( \
  59. dd if=$(KDIR)/relocate/loader.bin bs=32 conv=sync && \
  60. perl -e '@s = stat("$@"); print pack("N", @s[7])' && \
  61. cat $@ \
  62. ) > [email protected]
  63. @mv [email protected] $@
  64. endef
  65. ### Image scripts ###
  66. define rootfspad/jffs2-128k
  67. --align-rootfs
  68. endef
  69. define rootfspad/jffs2-64k
  70. --align-rootfs
  71. endef
  72. define rootfspad/squashfs
  73. endef
  74. define Image/FileSystemStrip
  75. $(firstword $(subst +,$(space),$(subst root.,,$(notdir $(1)))))
  76. endef
  77. define Build/cfe-bin
  78. $(STAGING_DIR_HOST)/bin/imagetag -i $(IMAGE_KERNEL) -f $(IMAGE_ROOTFS) \
  79. --output $@ --boardid $(CFE_BOARD_ID) --chipid $(CHIP_ID) \
  80. --entry $(LOADER_ENTRY) --load-addr $(LOADER_ENTRY) \
  81. --info1 "$(call ModelNameLimit16,$(DEVICE_NAME))" \
  82. --info2 "$(call Image/FileSystemStrip,$(IMAGE_ROOTFS))" \
  83. $(call rootfspad/$(call Image/FileSystemStrip,$(IMAGE_ROOTFS))) \
  84. $(CFE_EXTRAS) $(1)
  85. endef
  86. define Build/cfe-jffs2
  87. $(STAGING_DIR_HOST)/bin/mkfs.jffs2 \
  88. --big-endian \
  89. --pad \
  90. --no-cleanmarkers \
  91. --eraseblock=$(patsubst %k,%KiB,$(BLOCKSIZE)) \
  92. --root=$(1) \
  93. --output=$@ \
  94. --compression-mode=none
  95. $(call Build/pad-to,$(BLOCKSIZE))
  96. endef
  97. define Build/cfe-jffs2-cferam
  98. mv $@ [email protected]
  99. rm -rf $@-cferam
  100. mkdir -p $@-cferam
  101. # CFE ROM checks JFFS2 dirent version of cferam.
  102. # If version is not > 0 it will ignore the fs entry.
  103. # JFFS2 sets version 0 to the first fs entry and increments
  104. # it on the following ones, so let's create a dummy file that
  105. # will have version 0 and let cferam be the second (version 1).
  106. touch $@-cferam/1-openwrt
  107. # Add cferam as the last file in the JFFS2 partition
  108. cp $(KDIR)/bcm63xx-cfe/$(CFE_RAM_FILE) $@-cferam/$(CFE_RAM_JFFS2_NAME)
  109. # The JFFS2 partition creation should result in the following
  110. # layout:
  111. # 1) 1-openwrt (version 0, ino 2)
  112. # 2) cferam.000 (version 1, ino 3)
  113. $(call Build/cfe-jffs2,$@-cferam)
  114. # Some devices need padding between CFE RAM and kernel
  115. $(if $(CFE_RAM_JFFS2_PAD),$(call Build/pad-to,$(CFE_RAM_JFFS2_PAD)))
  116. # Add CFE partition tag
  117. $(if $(CFE_PART_ID),$(call Build/cfe-part-tag))
  118. # Append kernel
  119. dd [email protected] >> $@
  120. rm -f [email protected]
  121. endef
  122. define Build/cfe-jffs2-kernel
  123. rm -rf $@-kernel
  124. mkdir -p $@-kernel
  125. # CFE RAM checks JFFS2 dirent version of vmlinux.
  126. # If version is not > 0 it will ignore the fs entry.
  127. # JFFS2 sets version 0 to the first fs entry and increments
  128. # it on the following ones, so let's create a dummy file that
  129. # will have version 0 and let cferam be the second (version 1).
  130. touch $@-kernel/1-openwrt
  131. # vmlinux is located on a different JFFS2 partition, but CFE RAM
  132. # ignores it, so let's create another dummy file that will match
  133. # the JFFS2 ino of cferam entry on the first JFFS2 partition.
  134. # CFE RAM won't be able to find vmlinux if cferam has the same
  135. # ino as vmlinux.
  136. touch $@-kernel/2-openwrt
  137. # Add vmlinux as the last file in the JFFS2 partition
  138. $(TOPDIR)/scripts/cfe-bin-header.py \
  139. --input-file $@ \
  140. --output-file $@-kernel/vmlinux.lz \
  141. --load-addr $(if $(DEVICE_LOADADDR),$(DEVICE_LOADADDR),$(LOADER_ENTRY)) \
  142. --entry-addr $(if $(DEVICE_LOADADDR),$(DEVICE_LOADADDR),$(LOADER_ENTRY))
  143. # The JFFS2 partition creation should result in the following
  144. # layout:
  145. # 1) 1-openwrt (version 0, ino 2)
  146. # 2) 2-openwrt (version 1, ino 3)
  147. # 3) vmlinux.lz (version 2, ino 4)
  148. $(call Build/cfe-jffs2,$@-kernel)
  149. endef
  150. define Build/cfe-part-tag
  151. mv $@ [email protected]
  152. $(TOPDIR)/scripts/cfe-partition-tag.py \
  153. --input-file [email protected] \
  154. --output-file $@ \
  155. --flags $(CFE_PART_FLAGS) \
  156. --id $(CFE_PART_ID) \
  157. --name $(VERSION_CODE) \
  158. --version $(DEVICE_NAME)
  159. $(call Build/pad-to,$(BLOCKSIZE))
  160. dd [email protected] >> $@
  161. endef
  162. define Build/cfe-sercomm-crypto
  163. $(TOPDIR)/scripts/sercomm-crypto.py \
  164. --input-file $@ \
  165. --key-file [email protected] \
  166. --output-file [email protected] \
  167. --version OpenWrt
  168. $(STAGING_DIR_HOST)/bin/openssl enc -md md5 -aes-256-cbc \
  169. -in $@ -out [email protected] \
  170. -K `cat [email protected]` \
  171. -iv 00000000000000000000000000000000
  172. dd [email protected] >> [email protected]
  173. mv [email protected] $@
  174. rm -f [email protected] [email protected]
  175. endef
  176. define Build/cfe-sercomm-load
  177. $(TOPDIR)/scripts/sercomm-pid.py \
  178. --hw-version $(SERCOMM_HWVER) \
  179. --sw-version $(SERCOMM_SWVER) \
  180. --extra-padding-size 0x10 \
  181. --pid-file [email protected]
  182. $(TOPDIR)/scripts/sercomm-payload.py \
  183. --input-file $@ \
  184. --output-file [email protected] \
  185. --pid-file [email protected]
  186. mv [email protected] $@
  187. rm -f [email protected]
  188. endef
  189. define Build/cfe-sercomm-part
  190. $(TOPDIR)/scripts/sercomm-partition-tag.py \
  191. --input-file $@ \
  192. --output-file [email protected]_rootfs \
  193. --part-name kernel_rootfs \
  194. --part-version OpenWrt \
  195. --rootfs-version $(SERCOMM_FSVER)
  196. rm -rf $@-rootfs_lib
  197. mkdir -p $@-rootfs_lib
  198. echo $(SERCOMM_FSVER) > $@-rootfs_lib/lib_ver
  199. $(call Build/cfe-jffs2,$@-rootfs_lib)
  200. $(call Build/pad-to,$(BLOCKSIZE))
  201. $(TOPDIR)/scripts/sercomm-partition-tag.py \
  202. --input-file $@ \
  203. --output-file [email protected]_lib \
  204. --part-name rootfs_lib \
  205. --part-version $(SERCOMM_FSVER)
  206. mv [email protected]_rootfs $@
  207. dd [email protected]_lib >> $@
  208. endef
  209. define Build/cfe-wfi-tag
  210. $(TOPDIR)/scripts/cfe-wfi-tag.py \
  211. --input-file $@ \
  212. --output-file [email protected] \
  213. --version $(if $(1),$(1),$(CFE_WFI_VERSION)) \
  214. --chip-id $(CFE_WFI_CHIP_ID) \
  215. --flash-type $(CFE_WFI_FLASH_TYPE) \
  216. $(if $(CFE_WFI_FLAGS),--flags $(CFE_WFI_FLAGS))
  217. mv [email protected] $@
  218. endef
  219. ### Device scripts ###
  220. define Device/Default
  221. PROFILES = Default $$(DEVICE_NAME)
  222. KERNEL_DEPENDS = $$(wildcard ../dts/$$(DEVICE_DTS).dts)
  223. DEVICE_DTS_DIR := ../dts
  224. CHIP_ID :=
  225. SOC = bcm$$(CHIP_ID)
  226. DEVICE_DTS = $$(SOC)-$(subst _,-,$(1))
  227. DEVICE_LOADADDR :=
  228. endef
  229. define Device/bcm63xx-cfe
  230. FILESYSTEMS := squashfs jffs2-64k jffs2-128k
  231. KERNEL := kernel-bin | append-dtb | relocate-kernel | lzma
  232. KERNEL_INITRAMFS := kernel-bin | append-dtb | lzma | loader-lzma elf
  233. KERNEL_INITRAMFS_SUFFIX := .elf
  234. IMAGES := cfe.bin sysupgrade.bin
  235. IMAGE/cfe.bin := \
  236. cfe-bin $$$$(if $$$$(FLASH_MB),--pad $$$$(shell expr $$$$(FLASH_MB) / 2))
  237. IMAGE/sysupgrade.bin := cfe-bin | append-metadata
  238. BLOCKSIZE := 0x10000
  239. IMAGE_OFFSET :=
  240. FLASH_MB :=
  241. CFE_BOARD_ID :=
  242. CFE_EXTRAS = --block-size $$(BLOCKSIZE) \
  243. --image-offset $$(if $$(IMAGE_OFFSET),$$(IMAGE_OFFSET),$$(BLOCKSIZE))
  244. endef
  245. # Legacy CFEs with specific LZMA parameters and no length
  246. define Device/bcm63xx-cfe-legacy
  247. $(Device/bcm63xx-cfe)
  248. KERNEL := kernel-bin | append-dtb | relocate-kernel | lzma-cfe
  249. endef
  250. # CFE expects a single JFFS2 partition with cferam and kernel. However,
  251. # it's possible to fool CFE into properly loading both cferam and kernel
  252. # from two different JFFS2 partitions by adding dummy files (see
  253. # cfe-jffs2-cferam and cfe-jffs2-kernel).
  254. # Separate JFFS2 partitions allow upgrading openwrt without reflashing cferam
  255. # JFFS2 partition, which is much safer in case anything goes wrong.
  256. define Device/bcm63xx-nand
  257. FILESYSTEMS := squashfs ubifs
  258. KERNEL := kernel-bin | append-dtb | relocate-kernel | lzma | cfe-jffs2-kernel
  259. KERNEL_INITRAMFS := kernel-bin | append-dtb | lzma | loader-lzma elf
  260. KERNEL_INITRAMFS_SUFFIX := .elf
  261. IMAGES := cfe.bin sysupgrade.bin
  262. IMAGE/cfe.bin := append-kernel | pad-to $$$$(KERNEL_SIZE) |\
  263. cfe-jffs2-cferam | append-ubi | cfe-wfi-tag
  264. IMAGE/sysupgrade.bin := sysupgrade-tar | append-metadata
  265. KERNEL_SIZE := 5120k
  266. CFE_PART_FLAGS :=
  267. CFE_PART_ID :=
  268. CFE_RAM_FILE :=
  269. CFE_RAM_JFFS2_NAME :=
  270. CFE_RAM_JFFS2_PAD :=
  271. CFE_WFI_VERSION :=
  272. CFE_WFI_CHIP_ID = 0x$$(CHIP_ID)
  273. CFE_WFI_FLASH_TYPE :=
  274. CFE_WFI_FLAGS :=
  275. UBINIZE_OPTS := -E 5
  276. DEVICE_PACKAGES += nand-utils
  277. endef
  278. define Device/bcm63xx-netgear
  279. $(Device/bcm63xx-cfe)
  280. DEVICE_VENDOR := NETGEAR
  281. IMAGES := factory.chk sysupgrade.bin
  282. IMAGE/factory.chk := cfe-bin | netgear-chk
  283. NETGEAR_BOARD_ID :=
  284. NETGEAR_REGION :=
  285. endef
  286. define Device/sercomm-nand
  287. $(Device/bcm63xx-nand)
  288. IMAGES := factory.img sysupgrade.bin
  289. IMAGE/factory.img := append-kernel | pad-to $$$$(KERNEL_SIZE) | append-ubi |\
  290. cfe-sercomm-part | gzip | cfe-sercomm-load | cfe-sercomm-crypto
  291. SERCOMM_FSVER :=
  292. SERCOMM_HWVER :=
  293. SERCOMM_SWVER :=
  294. endef
  295. ### Package helpers ###
  296. ATH9K_PACKAGES := kmod-ath9k kmod-owl-loader wpad-basic-mbedtls
  297. B43_PACKAGES := kmod-b43 wpad-basic-mbedtls
  298. USB1_PACKAGES := kmod-usb-ohci kmod-usb-ledtrig-usbport
  299. USB2_PACKAGES := $(USB1_PACKAGES) kmod-usb2
  300. include $(SUBTARGET).mk
  301. $(eval $(call BuildImage))