bcm63xx: nand: support Sercomm firmwares
[openwrt/openwrt.git] / target / linux / bcm63xx / image / Makefile
1 #
2 # Copyright (C) 2006-2015 OpenWrt.org
3 # Copyright (C) 2016 LEDE project
4 #
5 # This is free software, licensed under the GNU General Public License v2.
6 # See /LICENSE for more information.
7 #
8 include $(TOPDIR)/rules.mk
9 include $(INCLUDE_DIR)/image.mk
10
11 KERNEL_LOADADDR = 0x80010000 # RAM start + 64K
12 LOADER_ENTRY = 0x80a00000 # RAM start + 10M, for relocate
13 RAMSIZE = 0x02000000 # 32MB
14 LZMA_TEXT_START = 0x81800000 # 32MB - 8MB
15
16 LOADER_MAKEOPTS= \
17 KDIR=$(KDIR) \
18 LOADADDR=$(KERNEL_LOADADDR) \
19 RAMSIZE=$(RAMSIZE) \
20 LZMA_TEXT_START=$(LZMA_TEXT_START) \
21 CHIP_ID=$(CHIP_ID)
22
23 RELOCATE_MAKEOPTS= \
24 CACHELINE_SIZE=16 \
25 KERNEL_ADDR=$(KERNEL_LOADADDR) \
26 CROSS_COMPILE=$(TARGET_CROSS) \
27 LZMA_TEXT_START=$(LOADER_ENTRY)
28
29 define Build/Compile
30 rm -rf $(KDIR)/relocate
31 $(CP) ../../generic/image/relocate $(KDIR)
32 $(MAKE) -C $(KDIR)/relocate $(RELOCATE_MAKEOPTS)
33 endef
34
35 ### Kernel scripts ###
36 define Build/hcs-initramfs
37 $(STAGING_DIR_HOST)/bin/hcsmakeimage --magic_bytes=$(HCS_MAGIC_BYTES) \
38 --rev_maj=$(HCS_REV_MAJ) --rev_min=$(HCS_REV_MIN) --input_file=$@ \
39 --output_file=$@.hcs --ldaddress=$(KERNEL_LOADADDR)
40 mv $@.hcs $@
41 endef
42
43 define Build/loader-lzma
44 rm -rf $@.src
45 $(MAKE) -C lzma-loader \
46 $(LOADER_MAKEOPTS) \
47 PKG_BUILD_DIR="$@.src" \
48 TARGET_DIR="$(dir $@)" \
49 LOADER_DATA="$@" \
50 LOADER_NAME="$(notdir $@)" \
51 compile loader.$(1)
52 mv "$@.$(1)" "$@"
53 rm -rf $@.src
54 endef
55
56 define Build/lzma-cfe
57 # CFE is a LZMA nazi! It took me hours to find out the parameters!
58 # Also I think lzma has a bug cause it generates different output depending on
59 # if you use stdin / stdout or not. Use files instead of stdio here, cause
60 # otherwise CFE will complain and not boot the image.
61 $(call Build/lzma-no-dict,-d22 -fb64 -a1)
62 # Strip out the length, CFE doesn't like this
63 dd if=$@ of=$@.new bs=5 count=1
64 dd if=$@ of=$@.new ibs=13 obs=5 skip=1 seek=1 conv=notrunc
65 mv $@.new $@
66 endef
67
68 define Build/relocate-kernel
69 # CFE only allows ~4 MiB for the uncompressed kernels, but uncompressed
70 # kernel might get larger than that, so let CFE unpack and load at a
71 # higher address and make the kernel relocate itself to the expected
72 # location.
73 ( \
74 dd if=$(KDIR)/relocate/loader.bin bs=32 conv=sync && \
75 perl -e '@s = stat("$@"); print pack("N", @s[7])' && \
76 cat $@ \
77 ) > $@.relocate
78 mv $@.relocate $@
79 endef
80
81 ### Image scripts ###
82 define rootfspad/jffs2-128k
83 --align-rootfs
84 endef
85 define rootfspad/jffs2-64k
86 --align-rootfs
87 endef
88 define rootfspad/squashfs
89 endef
90
91 define Image/LimitName16
92 $(shell expr substr "$(1)" 1 16)
93 endef
94
95 define Image/FileSystemStrip
96 $(firstword $(subst +,$(space),$(subst root.,,$(notdir $(1)))))
97 endef
98
99 define Build/ad1018-jffs2-cferam
100 mv $@ $@.kernel
101
102 rm -rf $@-cferam
103 mkdir -p $@-cferam
104
105 # CFE RAM JFFS2 partition
106 cp $(KDIR)/bcm63xx-cfe/$(CFE_RAM_FILE) $@-cferam/$(CFE_RAM_JFFS2_NAME)
107 $(call Build/cfe-jffs2,$@-cferam)
108 mv $@ $@.cferam
109
110 # First block of CFE RAM 1 will be destroyed by MMAP
111 dd if=$@.cferam bs=$(BLOCKSIZE) count=1 > $@
112 dd if=$@.cferam >> $@
113 $(call Build/pad-to,896k)
114
115 # CFE RAM 2
116 dd if=$@.cferam >> $@
117
118 # Append kernel @ 4M
119 $(call Build/pad-to,4096k)
120 dd if=$@.kernel >> $@
121 rm -f $@.kernel
122 endef
123
124 define Build/cfe-bin
125 $(STAGING_DIR_HOST)/bin/imagetag -i $(IMAGE_KERNEL) -f $(IMAGE_ROOTFS) \
126 --output $@ --boardid $(CFE_BOARD_ID) --chipid $(CHIP_ID) \
127 --entry $(LOADER_ENTRY) --load-addr $(LOADER_ENTRY) \
128 --info1 "$(call Image/LimitName16,$(DEVICE_NAME))" \
129 --info2 "$(call Image/FileSystemStrip,$(IMAGE_ROOTFS))" \
130 $(call rootfspad/$(call Image/FileSystemStrip,$(IMAGE_ROOTFS))) \
131 $(CFE_EXTRAS) $(1)
132 endef
133
134 define Build/cfe-jffs2
135 $(STAGING_DIR_HOST)/bin/mkfs.jffs2 \
136 --big-endian \
137 --pad \
138 --no-cleanmarkers \
139 --eraseblock=$(patsubst %k,%KiB,$(BLOCKSIZE)) \
140 --root=$(1) \
141 --output=$@ \
142 --compression-mode=none
143
144 $(call Build/pad-to,$(BLOCKSIZE))
145 endef
146
147 define Build/cfe-jffs2-cferam
148 mv $@ $@.kernel
149
150 rm -rf $@-cferam
151 mkdir -p $@-cferam
152
153 # CFE ROM checks JFFS2 dirent version of cferam.
154 # If version is not > 0 it will ignore the fs entry.
155 # JFFS2 sets version 0 to the first fs entry and increments
156 # it on the following ones, so let's create a dummy file that
157 # will have version 0 and let cferam be the second (version 1).
158 touch $@-cferam/1-openwrt
159 # Add cferam as the last file in the JFFS2 partition
160 cp $(KDIR)/bcm63xx-cfe/$(CFE_RAM_FILE) $@-cferam/$(CFE_RAM_JFFS2_NAME)
161
162 # The JFFS2 partition creation should result in the following
163 # layout:
164 # 1) 1-openwrt (version 0, ino 2)
165 # 2) cferam.000 (version 1, ino 3)
166 $(call Build/cfe-jffs2,$@-cferam)
167
168 # Some devices need padding between CFE RAM and kernel
169 $(if $(CFE_RAM_JFFS2_PAD),$(call Build/pad-to,$(CFE_RAM_JFFS2_PAD)))
170
171 # Add CFE partition tag
172 $(if $(CFE_PART_ID),$(call Build/cfe-part-tag))
173
174 # Append kernel
175 dd if=$@.kernel >> $@
176 rm -f $@.kernel
177 endef
178
179 define Build/cfe-jffs2-kernel
180 rm -rf $@-kernel
181 mkdir -p $@-kernel
182
183 # CFE RAM checks JFFS2 dirent version of vmlinux.
184 # If version is not > 0 it will ignore the fs entry.
185 # JFFS2 sets version 0 to the first fs entry and increments
186 # it on the following ones, so let's create a dummy file that
187 # will have version 0 and let cferam be the second (version 1).
188 touch $@-kernel/1-openwrt
189 # vmlinux is located on a different JFFS2 partition, but CFE RAM
190 # ignores it, so let's create another dummy file that will match
191 # the JFFS2 ino of cferam entry on the first JFFS2 partition.
192 # CFE RAM won't be able to find vmlinux if cferam has the same
193 # ino as vmlinux.
194 touch $@-kernel/2-openwrt
195 # Add vmlinux as the last file in the JFFS2 partition
196 $(TOPDIR)/scripts/cfe-bin-header.py \
197 --input-file $@ \
198 --output-file $@-kernel/vmlinux.lz \
199 --load-addr $(LOADER_ENTRY) \
200 --entry-addr $(LOADER_ENTRY)
201
202 # The JFFS2 partition creation should result in the following
203 # layout:
204 # 1) 1-openwrt (version 0, ino 2)
205 # 2) 2-openwrt (version 1, ino 3)
206 # 3) vmlinux.lz (version 2, ino 4)
207 $(call Build/cfe-jffs2,$@-kernel)
208 endef
209
210 define Build/cfe-part-tag
211 mv $@ $@.part
212
213 $(TOPDIR)/scripts/cfe-partition-tag.py \
214 --input-file $@.part \
215 --output-file $@ \
216 --flags $(CFE_PART_FLAGS) \
217 --id $(CFE_PART_ID) \
218 --name $(VERSION_CODE) \
219 --version $(DEVICE_NAME)
220
221 $(call Build/pad-to,$(BLOCKSIZE))
222
223 dd if=$@.part >> $@
224 endef
225
226 define Build/cfe-sercomm-part
227 $(TOPDIR)/scripts/sercomm-partition-tag.py \
228 --input-file $@ \
229 --output-file $@.kernel_rootfs \
230 --part-name kernel_rootfs \
231 --part-version OpenWrt \
232 --rootfs-version $(SERCOMM_VERSION)
233
234 rm -rf $@-rootfs_lib
235 mkdir -p $@-rootfs_lib
236 echo $(SERCOMM_VERSION) > $@-rootfs_lib/lib_ver
237 $(call Build/cfe-jffs2,$@-rootfs_lib)
238 $(call Build/pad-to,$(BLOCKSIZE))
239 $(TOPDIR)/scripts/sercomm-partition-tag.py \
240 --input-file $@ \
241 --output-file $@.rootfs_lib \
242 --part-name rootfs_lib \
243 --part-version $(SERCOMM_VERSION)
244
245 rm -rf $@-bootloader
246 mkdir -p $@-bootloader
247 cp $(KDIR)/bcm63xx-cfe/$(CFE_RAM_FILE) $@-bootloader/$(CFE_RAM_JFFS2_NAME)
248 $(call Build/cfe-jffs2,$@-bootloader)
249 $(call Build/pad-to,$(BLOCKSIZE))
250 $(TOPDIR)/scripts/sercomm-partition-tag.py \
251 --input-file $@ \
252 --output-file $@.bootloader \
253 --part-name bootloader \
254 --part-version $(SERCOMM_VERSION)
255
256 mv $@.kernel_rootfs $@
257 dd if=$@.rootfs_lib >> $@
258 dd if=$@.bootloader >> $@
259 endef
260
261 define Build/cfe-sercomm-load
262 mv $@ $@.ser
263 echo $(SERCOMM_PID) | xxd -p -r > $@
264 sha256sum $@.ser | awk '{ print $1 }' | xxd -p -r >> $@
265 dd if=$@.ser >> $@
266 rm -f $@.ser
267 endef
268
269 define Build/cfe-sercomm-crypto
270 $(TOPDIR)/scripts/sercomm-crypto.py \
271 --input-file $@ \
272 --key-file $@.key \
273 --output-file $@.ser \
274 --version OpenWrt
275 $(STAGING_DIR_HOST)/bin/openssl enc -md md5 -aes-256-cbc \
276 -in $@ -out $@.enc \
277 -K `cat $@.key` \
278 -iv 00000000000000000000000000000000
279 dd if=$@.enc >> $@.ser
280 mv $@.ser $@
281 rm -f $@.enc $@.key
282 endef
283
284 define Build/cfe-old-bin
285 $(TOPDIR)/scripts/brcmImage.pl -t -p \
286 -o $@ -b $(CFE_BOARD_ID) -c $(CHIP_ID) \
287 -e $(LOADER_ENTRY) -a $(LOADER_ENTRY) \
288 -k $(IMAGE_KERNEL) -r $(IMAGE_ROOTFS) \
289 $(CFE_EXTRAS)
290 endef
291
292 define Build/cfe-spw303v-bin
293 $(STAGING_DIR_HOST)/bin/imagetag -i $(IMAGE_KERNEL) -f $(IMAGE_ROOTFS) \
294 --output $@ --boardid $(CFE_BOARD_ID) --chipid $(CHIP_ID) \
295 --entry $(LOADER_ENTRY) --load-addr $(LOADER_ENTRY) \
296 $(call rootfspad/$(call Image/FileSystemStrip,$(IMAGE_ROOTFS))) \
297 $(CFE_EXTRAS) $(1)
298 endef
299
300 define Build/cfe-wfi-tag
301 $(TOPDIR)/scripts/cfe-wfi-tag.py \
302 --input-file $@ \
303 --output-file $@.new \
304 --version $(if $(1),$(1),$(CFE_WFI_VERSION)) \
305 --chip-id $(CFE_WFI_CHIP_ID) \
306 --flash-type $(CFE_WFI_FLASH_TYPE) \
307 $(if $(CFE_WFI_FLAGS),--flags $(CFE_WFI_FLAGS))
308 mv $@.new $@
309 endef
310
311 define Build/spw303v-bin
312 $(STAGING_DIR_HOST)/bin/spw303v -i $@ -o $@.spw303v
313 mv $@.spw303v $@
314 endef
315
316 define Build/zyxel-bin
317 $(STAGING_DIR_HOST)/bin/zyxbcm -i $@ -o $@.zyxel
318 mv $@.zyxel $@
319 endef
320
321 define Build/redboot-bin
322 # Prepare kernel and rootfs
323 dd if=$(IMAGE_KERNEL) of=$(BIN_DIR)/$(REDBOOT_PREFIX)-vmlinux.gz bs=65536 conv=sync
324 dd if=$(IMAGE_ROOTFS) of=$(BIN_DIR)/$(REDBOOT_PREFIX)-$(notdir $(IMAGE_ROOTFS)) bs=64k conv=sync
325 echo -ne \\xDE\\xAD\\xC0\\xDE >> $(BIN_DIR)/$(REDBOOT_PREFIX)-$(notdir $(IMAGE_ROOTFS))
326 # Generate the scripted image
327 $(TOPDIR)/scripts/redboot-script.pl \
328 -k $(BIN_DIR)/$(REDBOOT_PREFIX)-vmlinux.gz \
329 -r $(BIN_DIR)/$(REDBOOT_PREFIX)-$(notdir $(IMAGE_ROOTFS)) \
330 -a $(strip $(LOADER_ENTRY)) -f 0xbe430000 -l 0x7c0000 \
331 -s 0x1000 -t 20 -o $@.redbootscript
332 dd if="$@.redbootscript" of="$@.redbootscript.padded" bs=4096 conv=sync
333 cat \
334 "$@.redbootscript.padded" \
335 "$(BIN_DIR)/$(REDBOOT_PREFIX)-vmlinux.gz" \
336 "$(BIN_DIR)/$(REDBOOT_PREFIX)-$(notdir $(IMAGE_ROOTFS))" \
337 > "$@"
338 endef
339
340 define Device/Default
341 PROFILES = Default $$(DEVICE_NAME)
342 KERNEL_DEPENDS = $$(wildcard ../dts/$$(DEVICE_DTS).dts)
343 KERNEL_INITRAMFS_SUFFIX := .elf
344 DEVICE_DTS_DIR := ../dts
345 CHIP_ID :=
346 SOC = bcm$$(CHIP_ID)
347 DEVICE_DTS = $$(SOC)-$(subst _,-,$(1))
348 endef
349 DEVICE_VARS += CHIP_ID
350
351 ATH5K_PACKAGES := kmod-ath5k wpad-basic
352 ATH9K_PACKAGES := kmod-ath9k wpad-basic
353 B43_PACKAGES := kmod-b43 wpad-basic
354 BRCMWL_PACKAGES := kmod-brcm-wl nas wlc
355 RT28_PACKAGES := kmod-rt2800-pci wpad-basic
356 RT61_PACKAGES := kmod-rt61-pci wpad-basic
357 USB1_PACKAGES := kmod-usb-ohci kmod-usb-ledtrig-usbport
358 USB2_PACKAGES := kmod-usb2 kmod-usb-ohci kmod-usb-ledtrig-usbport
359
360 include bcm63xx.mk
361
362 ifeq ($(SUBTARGET),smp)
363 include bcm63xx_nand.mk
364 endif
365
366 $(eval $(call BuildImage))