1 # SPDX-License-Identifier: GPL-2.0-or-later
3 include $(TOPDIR
)/rules.mk
4 include $(INCLUDE_DIR
)/image.mk
6 KERNEL_LOADADDR
:= 0x80010000 # RAM start + 64K
7 UBOOT_ENTRY
:= 0x81c00000
8 LOADER_ENTRY
:= 0x81000000 # RAM start + 16M, for relocate
9 LZMA_TEXT_START
:= 0x82000000 # RAM start + 32M
11 DEVICE_VARS
+= CFE_BOARD_ID CFE_EXTRAS
12 DEVICE_VARS
+= CFE_PART_FLAGS CFE_PART_ID
13 DEVICE_VARS
+= CFE_RAM_FILE
14 DEVICE_VARS
+= CFE_RAM_JFFS2_NAME CFE_RAM_JFFS2_PAD
15 DEVICE_VARS
+= CFE_WFI_CHIP_ID CFE_WFI_FLASH_TYPE
16 DEVICE_VARS
+= CFE_WFI_FLAGS CFE_WFI_VERSION
17 DEVICE_VARS
+= CHIP_ID DEVICE_LOADADDR
18 DEVICE_VARS
+= FLASH_MB IMAGE_OFFSET
19 DEVICE_VARS
+= SERCOMM_FSVER SERCOMM_HWVER SERCOMM_SWVER
22 rm -rf
$(KDIR
)/relocate
23 $(CP
) ..
/..
/generic
/image
/relocate
$(KDIR
)
24 $(MAKE
) -C
$(KDIR
)/relocate \
26 CROSS_COMPILE
=$(TARGET_CROSS
) \
27 KERNEL_ADDR
=$(KERNEL_LOADADDR
) \
28 LZMA_TEXT_START
=$(LOADER_ENTRY
)
31 ### Kernel scripts ###
32 define Build
/loader-lzma
34 $(MAKE
) -C lzma-loader \
36 KERNEL_ADDR
=$(KERNEL_LOADADDR
) \
38 LOADER_ADDR
=$(if
$(DEVICE_LOADADDR
),$(DEVICE_LOADADDR
),$(LOADER_ENTRY
)) \
40 LOADER_NAME
="$(notdir $@)" \
41 LZMA_TEXT_START
=$(LZMA_TEXT_START
) \
42 PKG_BUILD_DIR
="$@.src" \
43 TARGET_DIR
="$(dir $@)" \
50 # CFE is a LZMA nazi! It took me hours to find out the parameters!
51 # Also I think lzma has a bug cause it generates different output depending on
52 # if you use stdin / stdout or not. Use files instead of stdio here, cause
53 # otherwise CFE will complain and not boot the image.
54 $(call Build
/lzma-no-dict
,-d22
-fb64
-a1
)
55 # Strip out the length, CFE doesn't like this
56 dd if
=$@ of
=$@.new bs
=5 count
=1
57 dd if
=$@ of
=$@.new ibs
=13 obs
=5 skip
=1 seek
=1 conv
=notrunc
61 define Build
/relocate-kernel
62 # CFE only allows ~4 MiB for the uncompressed kernels, but uncompressed
63 # kernel might get larger than that, so let CFE unpack and load at a
64 # higher address and make the kernel relocate itself to the expected
67 dd if
=$(KDIR
)/relocate
/loader.bin bs
=32 conv
=sync
&& \
68 perl
-e
'@s = stat("$@"); print pack("N", @s[7])' && \
75 define rootfspad
/jffs2-128k
78 define rootfspad
/jffs2-64k
81 define rootfspad
/squashfs
84 define Image
/FileSystemStrip
85 $(firstword $(subst +,$(space
),$(subst root.
,,$(notdir $(1)))))
89 $(STAGING_DIR_HOST
)/bin
/imagetag
-i
$(IMAGE_KERNEL
) -f
$(IMAGE_ROOTFS
) \
90 --output
$@
--boardid
$(CFE_BOARD_ID
) --chipid
$(CHIP_ID
) \
91 --entry
$(LOADER_ENTRY
) --load-addr
$(LOADER_ENTRY
) \
92 --info1
"$(call ModelNameLimit16,$(DEVICE_NAME))" \
93 --info2
"$(call Image/FileSystemStrip,$(IMAGE_ROOTFS))" \
94 $(call rootfspad
/$(call Image
/FileSystemStrip
,$(IMAGE_ROOTFS
))) \
98 # Build a CFE image with just U-Boot
99 define Build
/cfe-bin-uboot
100 cp
$(STAGING_DIR_IMAGE
)/$(DEVICE_NAME
)-u-boot.bin
$@
103 echo
"dummy" > $@.dummyfs
104 $(STAGING_DIR_HOST
)/bin
/imagetag
-i
$@.uboot.lzma
-f
$@.dummyfs \
105 --output
$@
--boardid
$(CFE_BOARD_ID
) --chipid
$(CHIP_ID
) \
106 --entry
$(UBOOT_ENTRY
) --load-addr
$(UBOOT_ENTRY
) \
107 --info1
"$(call ModelNameLimit16,$(DEVICE_NAME))" \
113 define Build
/cfe-jffs2
114 $(STAGING_DIR_HOST
)/bin
/mkfs.jffs2 \
118 --eraseblock
=$(patsubst %k
,%KiB
,$(BLOCKSIZE
)) \
121 --compression-mode
=none
123 $(call Build
/pad-to
,$(BLOCKSIZE
))
126 define Build
/cfe-jffs2-cferam
132 # CFE ROM checks JFFS2 dirent version of cferam.
133 # If version is not > 0 it will ignore the fs entry.
134 # JFFS2 sets version 0 to the first fs entry and increments
135 # it on the following ones, so let's create a dummy file that
136 # will have version 0 and let cferam be the second (version 1).
137 touch
$@
-cferam
/1-openwrt
138 # Add cferam as the last file in the JFFS2 partition
139 cp
$(KDIR
)/bcm63xx-cfe
/$(CFE_RAM_FILE
) $@
-cferam
/$(CFE_RAM_JFFS2_NAME
)
141 # The JFFS2 partition creation should result in the following
143 # 1) 1-openwrt (version 0, ino 2)
144 # 2) cferam.000 (version 1, ino 3)
145 $(call Build
/cfe-jffs2
,$@
-cferam
)
147 # Some devices need padding between CFE RAM and kernel
148 $(if
$(CFE_RAM_JFFS2_PAD
),$(call Build
/pad-to
,$(CFE_RAM_JFFS2_PAD
)))
150 # Add CFE partition tag
151 $(if
$(CFE_PART_ID
),$(call Build
/cfe-part-tag
))
154 dd if
=$@.kernel
>> $@
158 define Build
/cfe-jffs2-kernel
162 # CFE RAM checks JFFS2 dirent version of vmlinux.
163 # If version is not > 0 it will ignore the fs entry.
164 # JFFS2 sets version 0 to the first fs entry and increments
165 # it on the following ones, so let's create a dummy file that
166 # will have version 0 and let cferam be the second (version 1).
167 touch
$@
-kernel
/1-openwrt
168 # vmlinux is located on a different JFFS2 partition, but CFE RAM
169 # ignores it, so let's create another dummy file that will match
170 # the JFFS2 ino of cferam entry on the first JFFS2 partition.
171 # CFE RAM won't be able to find vmlinux if cferam has the same
173 touch
$@
-kernel
/2-openwrt
174 # Add vmlinux as the last file in the JFFS2 partition
175 $(TOPDIR
)/scripts
/cfe-bin-header.py \
177 --output-file
$@
-kernel
/vmlinux.lz \
178 --load-addr
$(if
$(DEVICE_LOADADDR
),$(DEVICE_LOADADDR
),$(LOADER_ENTRY
)) \
179 --entry-addr
$(if
$(DEVICE_LOADADDR
),$(DEVICE_LOADADDR
),$(LOADER_ENTRY
))
181 # The JFFS2 partition creation should result in the following
183 # 1) 1-openwrt (version 0, ino 2)
184 # 2) 2-openwrt (version 1, ino 3)
185 # 3) vmlinux.lz (version 2, ino 4)
186 $(call Build
/cfe-jffs2
,$@
-kernel
)
189 define Build
/cfe-part-tag
192 $(TOPDIR
)/scripts
/cfe-partition-tag.py \
193 --input-file
$@.part \
195 --flags
$(CFE_PART_FLAGS
) \
196 --id
$(CFE_PART_ID
) \
197 --name
$(VERSION_CODE
) \
198 --version
$(DEVICE_NAME
)
200 $(call Build
/pad-to
,$(BLOCKSIZE
))
205 define Build
/cfe-sercomm-crypto
206 $(TOPDIR
)/scripts
/sercomm-crypto.py \
209 --output-file
$@.ser \
211 $(STAGING_DIR_HOST
)/bin
/openssl enc
-md md5
-aes-256-cbc \
214 -iv
00000000000000000000000000000000
215 dd if
=$@.enc
>> $@.ser
220 define Build
/cfe-sercomm-load
221 $(TOPDIR
)/scripts
/sercomm-pid.py \
222 --hw-version
$(SERCOMM_HWVER
) \
223 --sw-version
$(SERCOMM_SWVER
) \
224 --extra-padding-size
0x10 \
226 $(TOPDIR
)/scripts
/sercomm-payload.py \
228 --output-file
$@.new \
235 define Build
/cfe-sercomm-part
236 $(TOPDIR
)/scripts
/sercomm-partition-tag.py \
238 --output-file
$@.kernel_rootfs \
239 --part-name kernel_rootfs \
240 --part-version OpenWrt \
241 --rootfs-version
$(SERCOMM_FSVER
)
244 mkdir
-p
$@
-rootfs_lib
245 echo
$(SERCOMM_FSVER
) > $@
-rootfs_lib
/lib_ver
246 $(call Build
/cfe-jffs2
,$@
-rootfs_lib
)
247 $(call Build
/pad-to
,$(BLOCKSIZE
))
248 $(TOPDIR
)/scripts
/sercomm-partition-tag.py \
250 --output-file
$@.rootfs_lib \
251 --part-name rootfs_lib \
252 --part-version
$(SERCOMM_FSVER
)
254 mv
$@.kernel_rootfs
$@
255 dd if
=$@.rootfs_lib
>> $@
258 define Build
/cfe-wfi-tag
259 $(TOPDIR
)/scripts
/cfe-wfi-tag.py \
261 --output-file
$@.new \
262 --version
$(if
$(1),$(1),$(CFE_WFI_VERSION
)) \
263 --chip-id
$(CFE_WFI_CHIP_ID
) \
264 --flash-type
$(CFE_WFI_FLASH_TYPE
) \
265 $(if
$(CFE_WFI_FLAGS
),--flags
$(CFE_WFI_FLAGS
))
269 ### Device scripts ###
270 define Device
/Default
271 PROFILES
= Default
$$(DEVICE_NAME
)
272 KERNEL_DEPENDS
= $$(wildcard ..
/dts
/$$(DEVICE_DTS
).dts
)
273 DEVICE_DTS_DIR
:= ..
/dts
276 DEVICE_DTS
= $$(SOC
)-$(subst _
,-,$(1))
280 define Device
/bcm63xx-cfe
281 FILESYSTEMS
:= squashfs jffs2-64k jffs2-128k
282 KERNEL
:= kernel-bin | append-dtb | relocate-kernel | lzma
283 KERNEL_INITRAMFS
:= kernel-bin | append-dtb | lzma | loader-lzma elf
284 KERNEL_INITRAMFS_SUFFIX
:= .elf
285 IMAGES
:= cfe.bin sysupgrade.bin
287 cfe-bin
$$$$(if
$$$$(FLASH_MB
),--pad
$$$$(shell expr
$$$$(FLASH_MB
) / 2))
288 IMAGE
/sysupgrade.bin
:= cfe-bin | append-metadata
293 CFE_EXTRAS
= --block-size
$$(BLOCKSIZE
) \
294 --image-offset
$$(if
$$(IMAGE_OFFSET
),$$(IMAGE_OFFSET
),$$(BLOCKSIZE
))
297 # Legacy CFEs with specific LZMA parameters and no length
298 define Device
/bcm63xx-cfe-legacy
299 $(Device
/bcm63xx-cfe
)
300 KERNEL
:= kernel-bin | append-dtb | relocate-kernel | lzma-cfe
303 # CFE images with U-Boot in front of the kernel, these will execute
304 # U-Boot instead of the kernel and U-Boot will then proceed to load
305 # the kernel. The reason to do this is that CFE is sometimes unable to
306 # load big kernels even with the lzma loader tricks.
307 define Device
/bcm63xx-cfe-uboot
308 $(Device
/bcm63xx-cfe
)
309 KERNEL
:= kernel-bin | append-dtb | lzma | uImage lzma
310 IMAGE
/cfe.bin
:= cfe-bin-uboot | pad-to
$$$$$$$$(($$(BLOCKSIZE
))) | \
311 append-kernel | pad-to
$$$$$$$$(($$(BLOCKSIZE
))) | \
312 append-rootfs
$$$$(if
$$$$(FLASH_MB
),--pad
$$$$(shell expr
$$$$(FLASH_MB
) / 2))
313 IMAGE
/sysupgrade.bin
:= cfe-bin-uboot | pad-to
$$$$$$$$(($$(BLOCKSIZE
))) | \
314 append-kernel | pad-to
$$$$$$$$(($$(BLOCKSIZE
))) | \
315 append-rootfs | append-metadata
318 # CFE expects a single JFFS2 partition with cferam and kernel. However,
319 # it's possible to fool CFE into properly loading both cferam and kernel
320 # from two different JFFS2 partitions by adding dummy files (see
321 # cfe-jffs2-cferam and cfe-jffs2-kernel).
322 # Separate JFFS2 partitions allow upgrading openwrt without reflashing cferam
323 # JFFS2 partition, which is much safer in case anything goes wrong.
324 define Device
/bcm63xx-nand
325 FILESYSTEMS
:= squashfs ubifs
326 KERNEL
:= kernel-bin | append-dtb | relocate-kernel | lzma | cfe-jffs2-kernel
327 KERNEL_INITRAMFS
:= kernel-bin | append-dtb | lzma | loader-lzma elf
328 KERNEL_INITRAMFS_SUFFIX
:= .elf
329 IMAGES
:= cfe.bin sysupgrade.bin
330 IMAGE
/cfe.bin
:= append-kernel | pad-to
$$$$(KERNEL_SIZE
) |\
331 cfe-jffs2-cferam | append-ubi | cfe-wfi-tag
332 IMAGE
/sysupgrade.bin
:= sysupgrade-tar | append-metadata
337 CFE_RAM_JFFS2_NAME
:=
340 CFE_WFI_CHIP_ID
= 0x
$$(CHIP_ID
)
341 CFE_WFI_FLASH_TYPE
:=
344 DEVICE_PACKAGES
+= nand-utils
347 define Device
/bcm63xx-netgear
348 $(Device
/bcm63xx-cfe
)
349 DEVICE_VENDOR
:= NETGEAR
350 IMAGES
:= factory.chk sysupgrade.bin
351 IMAGE
/factory.chk
:= cfe-bin | netgear-chk
356 define Device
/sercomm-nand
357 $(Device
/bcm63xx-nand
)
358 IMAGES
:= factory.img sysupgrade.bin
359 IMAGE
/factory.img
:= append-kernel | pad-to
$$$$(KERNEL_SIZE
) | append-ubi |\
360 cfe-sercomm-part | gzip | cfe-sercomm-load | cfe-sercomm-crypto
366 ### Package helpers ###
367 ATH9K_PACKAGES
:= kmod-ath9k kmod-owl-loader wpad-basic-mbedtls
368 B43_PACKAGES
:= kmod-b43 wpad-basic-mbedtls
369 USB1_PACKAGES
:= kmod-usb-ohci kmod-usb-ledtrig-usbport
370 USB2_PACKAGES
:= $(USB1_PACKAGES
) kmod-usb2
372 include $(SUBTARGET
).mk
374 $(eval
$(call BuildImage
))