86ab68d2443be5687f5a36b417d3c5adaee2c919
[openwrt/staging/ldir.git] / target / linux / bmips / image / Makefile
1 # SPDX-License-Identifier: GPL-2.0-or-later
2
3 include $(TOPDIR)/rules.mk
4 include $(INCLUDE_DIR)/image.mk
5
6 KERNEL_LOADADDR := 0x80010000 # RAM start + 64K
7 LOADER_ENTRY := 0x80a00000 # RAM start + 10M, for relocate
8 RAMSIZE := 0x02000000 # 32MB
9 LZMA_TEXT_START := 0x81800000 # 32MB - 8MB
10
11 DEVICE_VARS += CHIP_ID DEVICE_LOADADDR
12
13 define Build/Compile
14 rm -rf $(KDIR)/relocate
15 $(CP) ../../generic/image/relocate $(KDIR)
16 $(MAKE) -C $(KDIR)/relocate \
17 CACHELINE_SIZE=16 \
18 CROSS_COMPILE=$(TARGET_CROSS) \
19 KERNEL_ADDR=$(KERNEL_LOADADDR) \
20 LZMA_TEXT_START=$(LOADER_ENTRY)
21 endef
22
23 ### Kernel scripts ###
24 define Build/loader-lzma
25 @rm -rf $@.src
26 $(MAKE) -C lzma-loader \
27 CHIP_ID=$(CHIP_ID) \
28 KERNEL_ADDR=$(KERNEL_LOADADDR) \
29 KDIR=$(KDIR) \
30 LOADER_ADDR=$(if $(DEVICE_LOADADDR),$(DEVICE_LOADADDR),$(LOADER_ENTRY)) \
31 LOADER_DATA="$@" \
32 LOADER_NAME="$(notdir $@)" \
33 LZMA_TEXT_START=$(LZMA_TEXT_START) \
34 PKG_BUILD_DIR="$@.src" \
35 RAMSIZE=$(RAMSIZE) \
36 TARGET_DIR="$(dir $@)" \
37 compile loader.$(1)
38 @mv "$@.$(1)" "$@"
39 @rm -rf $@.src
40 endef
41
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=$@ of=$@.new bs=5 count=1
50 dd if=$@ of=$@.new ibs=13 obs=5 skip=1 seek=1 conv=notrunc
51 @mv $@.new $@
52 endef
53
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 ) > $@.relocate
64 @mv $@.relocate $@
65 endef
66
67 ### Image scripts ###
68 define rootfspad/jffs2-128k
69 --align-rootfs
70 endef
71 define rootfspad/jffs2-64k
72 --align-rootfs
73 endef
74 define rootfspad/squashfs
75 endef
76
77 define Image/FileSystemStrip
78 $(firstword $(subst +,$(space),$(subst root.,,$(notdir $(1)))))
79 endef
80
81 define Build/cfe-bin
82 $(STAGING_DIR_HOST)/bin/imagetag -i $(IMAGE_KERNEL) -f $(IMAGE_ROOTFS) \
83 --output $@ --boardid $(CFE_BOARD_ID) --chipid $(CHIP_ID) \
84 --entry $(LOADER_ENTRY) --load-addr $(LOADER_ENTRY) \
85 --info1 "$(call ModelNameLimit16,$(DEVICE_NAME))" \
86 --info2 "$(call Image/FileSystemStrip,$(IMAGE_ROOTFS))" \
87 $(call rootfspad/$(call Image/FileSystemStrip,$(IMAGE_ROOTFS))) \
88 $(CFE_EXTRAS) $(1)
89 endef
90
91 define Build/cfe-jffs2
92 $(STAGING_DIR_HOST)/bin/mkfs.jffs2 \
93 --big-endian \
94 --pad \
95 --no-cleanmarkers \
96 --eraseblock=$(patsubst %k,%KiB,$(BLOCKSIZE)) \
97 --root=$(1) \
98 --output=$@ \
99 --compression-mode=none
100
101 $(call Build/pad-to,$(BLOCKSIZE))
102 endef
103
104 define Build/cfe-jffs2-cferam
105 mv $@ $@.kernel
106
107 rm -rf $@-cferam
108 mkdir -p $@-cferam
109
110 # CFE ROM checks JFFS2 dirent version of cferam.
111 # If version is not > 0 it will ignore the fs entry.
112 # JFFS2 sets version 0 to the first fs entry and increments
113 # it on the following ones, so let's create a dummy file that
114 # will have version 0 and let cferam be the second (version 1).
115 touch $@-cferam/1-openwrt
116 # Add cferam as the last file in the JFFS2 partition
117 cp $(KDIR)/bcm63xx-cfe/$(CFE_RAM_FILE) $@-cferam/$(CFE_RAM_JFFS2_NAME)
118
119 # The JFFS2 partition creation should result in the following
120 # layout:
121 # 1) 1-openwrt (version 0, ino 2)
122 # 2) cferam.000 (version 1, ino 3)
123 $(call Build/cfe-jffs2,$@-cferam)
124
125 # Some devices need padding between CFE RAM and kernel
126 $(if $(CFE_RAM_JFFS2_PAD),$(call Build/pad-to,$(CFE_RAM_JFFS2_PAD)))
127
128 # Add CFE partition tag
129 $(if $(CFE_PART_ID),$(call Build/cfe-part-tag))
130
131 # Append kernel
132 dd if=$@.kernel >> $@
133 rm -f $@.kernel
134 endef
135
136 define Build/cfe-jffs2-kernel
137 rm -rf $@-kernel
138 mkdir -p $@-kernel
139
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
159 # The JFFS2 partition creation should result in the following
160 # layout:
161 # 1) 1-openwrt (version 0, ino 2)
162 # 2) 2-openwrt (version 1, ino 3)
163 # 3) vmlinux.lz (version 2, ino 4)
164 $(call Build/cfe-jffs2,$@-kernel)
165 endef
166
167 define Build/cfe-part-tag
168 mv $@ $@.part
169
170 $(TOPDIR)/scripts/cfe-partition-tag.py \
171 --input-file $@.part \
172 --output-file $@ \
173 --flags $(CFE_PART_FLAGS) \
174 --id $(CFE_PART_ID) \
175 --name $(VERSION_CODE) \
176 --version $(DEVICE_NAME)
177
178 $(call Build/pad-to,$(BLOCKSIZE))
179
180 dd if=$@.part >> $@
181 endef
182
183 define Build/cfe-sercomm-crypto
184 $(TOPDIR)/scripts/sercomm-crypto.py \
185 --input-file $@ \
186 --key-file $@.key \
187 --output-file $@.ser \
188 --version OpenWrt
189 $(STAGING_DIR_HOST)/bin/openssl enc -md md5 -aes-256-cbc \
190 -in $@ -out $@.enc \
191 -K `cat $@.key` \
192 -iv 00000000000000000000000000000000
193 dd if=$@.enc >> $@.ser
194 mv $@.ser $@
195 rm -f $@.enc $@.key
196 endef
197
198 define Build/cfe-sercomm-load
199 $(TOPDIR)/scripts/sercomm-payload.py \
200 --input-file $@ \
201 --output-file $@.new \
202 --pid "$(SERCOMM_PID)"
203
204 mv $@.new $@
205 endef
206
207 define Build/cfe-sercomm-part
208 $(TOPDIR)/scripts/sercomm-partition-tag.py \
209 --input-file $@ \
210 --output-file $@.kernel_rootfs \
211 --part-name kernel_rootfs \
212 --part-version OpenWrt \
213 --rootfs-version $(SERCOMM_VERSION)
214
215 rm -rf $@-rootfs_lib
216 mkdir -p $@-rootfs_lib
217 echo $(SERCOMM_VERSION) > $@-rootfs_lib/lib_ver
218 $(call Build/cfe-jffs2,$@-rootfs_lib)
219 $(call Build/pad-to,$(BLOCKSIZE))
220 $(TOPDIR)/scripts/sercomm-partition-tag.py \
221 --input-file $@ \
222 --output-file $@.rootfs_lib \
223 --part-name rootfs_lib \
224 --part-version $(SERCOMM_VERSION)
225
226 mv $@.kernel_rootfs $@
227 dd if=$@.rootfs_lib >> $@
228 endef
229
230 define Build/cfe-wfi-tag
231 $(TOPDIR)/scripts/cfe-wfi-tag.py \
232 --input-file $@ \
233 --output-file $@.new \
234 --version $(if $(1),$(1),$(CFE_WFI_VERSION)) \
235 --chip-id $(CFE_WFI_CHIP_ID) \
236 --flash-type $(CFE_WFI_FLASH_TYPE) \
237 $(if $(CFE_WFI_FLAGS),--flags $(CFE_WFI_FLAGS))
238 mv $@.new $@
239 endef
240
241 ### Device scripts ###
242 define Device/Default
243 PROFILES = Default $$(DEVICE_NAME)
244 KERNEL_DEPENDS = $$(wildcard ../dts/$$(DEVICE_DTS).dts)
245 DEVICE_DTS_DIR := ../dts
246 CHIP_ID :=
247 SOC = bcm$$(CHIP_ID)
248 DEVICE_DTS = $$(SOC)-$(subst _,-,$(1))
249 DEVICE_LOADADDR :=
250 endef
251
252 ATH9K_PACKAGES := kmod-ath9k wpad-basic-mbedtls
253 B43_PACKAGES := kmod-b43 wpad-basic-mbedtls
254 USB1_PACKAGES := kmod-usb-ohci kmod-ledtrig-usbdev
255 USB2_PACKAGES := $(USB1_PACKAGES) kmod-usb2
256
257 include bcm63xx_$(SUBTARGET).mk
258
259 $(eval $(call BuildImage))