Makefile 11 KB

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