blob: b43b27d2474603d1ee6c1b84648a1704eb5e6df0 [file] [log] [blame]
shijie.xiong3da90db2024-06-27 17:13:42 +08001#!/bin/bash
2#
3# Copyright (c) 2021-2022 Amlogic, Inc. All rights reserved.
4#
5# SPDX-License-Identifier: MIT
6#
7
8#RTOS root directory
9RTOS_BASE_DIR=$(realpath $(dirname $(readlink -f ${BASH_SOURCE[0]:-$0}))/..)
10
yongbing.hefc759372024-08-05 17:51:39 +080011PARSED=$(getopt --options o:a:b:s:h --long address:,board:,bl22:,sensor:,out:,uboot:,ipc-ddr-size:,ubootcfg:,help --name "$0" -- "$@")
shijie.xiong3da90db2024-06-27 17:13:42 +080012
13if [[ $? -ne 0 ]]; then
14 exit 1
15fi
16
17eval set -- "$PARSED"
18
19while true; do
20 case "$1" in
21 -a | --address)
22 RTOS_TARGET_ADDRESS=$2
23 shift 2
24 ;;
25 -b | --board)
26 BOARD_TYPE=$2
27 shift 2
28 ;;
29 -s | --sensor)
yongbing.hefc759372024-08-05 17:51:39 +080030 SENSOR_TYPE=$2
shijie.xiong3da90db2024-06-27 17:13:42 +080031 if [[ "$2" =~ ^SENSOR=.* ]]; then
32 SENSOR_TYPE=${2#SENSOR=}
33 fi
34 shift 2
35 ;;
36 --bl22)
37 BL22_DIR=$2
38 shift 2
39 ;;
yongbing.hefc759372024-08-05 17:51:39 +080040 --uboot)
41 UBOOT_DIR=$2
42 shift 2
43 ;;
44 --ipc-ddr-size)
45 DDR_SIZE=$2
46 shift 2
47 ;;
48 --ubootcfg)
49 UBOOT_CFG=$2
50 shift 2
51 ;;
shijie.xiong3da90db2024-06-27 17:13:42 +080052 -o | --out)
53 INSTALL_DIR=$2
54 shift 2
55 ;;
56 -h | --help)
57 echo -e "\033[41;33m gen_fastboot.sh help info \033[0m"
58 echo "Usage: fastboot [OPTIONS]"
59 echo
60 echo "Options:"
61 echo " -a/--address et address to load (e.g., -a 0x2200000)"
62 echo " -b/--board Set board type (e.g., -b aw402_c302x)"
63 echo " -s/--sensor Set ipc sensor type (e.g., -s IMX290)"
64 echo " --b22 PATH Set BL22 path"
65 echo " -h Display this help information"
66 echo
67 echo "For more details, refer to the fastboot documentation."
68 exit 1
69 shift
70 ;;
71 --)
72 shift
73 break
74 ;;
75 *)
76 echo "Invalid option: $1" >&2
77 exit 1
78 ;;
79 esac
80done
81
82if [[ ! -d "$BL22_DIR" ]]; then
83 echo "The provided bl22 path is not a directory: $BL22_DIR"
84 exit 1
85fi
86
87#Retrieve the RTOS loading address; if none exists, set it to the default address.
88if [ -z $RTOS_TARGET_ADDRESS ]; then
yongbing.hefc759372024-08-05 17:51:39 +080089 RTOS_TARGET_ADDRESS=0x02200000
shijie.xiong3da90db2024-06-27 17:13:42 +080090fi
91
92#Calculate the RTOS2 loading address
93let "RTOS2_TARGET_ADDRESS=$RTOS_TARGET_ADDRESS+0x100000"
94RTOS2_TARGET_ADDRESS=$(printf '0x%x\n' $RTOS2_TARGET_ADDRESS)
95
96#Clear cache files
97[ -d $RTOS_BASE_DIR/output ] && rm -rf $RTOS_BASE_DIR/output
98
99#Get the current project environment variables
100source $RTOS_BASE_DIR/scripts/env.sh arm64 c3 $BOARD_TYPE fastboot
101if [ "$?" -ne 0 ]; then
102 echo "Error: Incorrect board type selection!"
103 exit 1
104fi
105
106#IPC sensor model configuration.
107if [ -z $SENSOR_TYPE ]; then
108 case $BOARD_TYPE in
109 'aw402_c302x')
110 SENSOR_TYPE=SC301IOT
111 ;;
112 *)
113 SENSOR_TYPE=IMX290
114 ;;
115 esac
116fi
117
118#RTOS object file path
119RTOS_BUILD_DIR=$RTOS_BASE_DIR/output/$ARCH-$BOARD-$PRODUCT/freertos
120RTOS_IMAGE_1=$RTOS_BUILD_DIR/rtos_1.bin
121RTOS_IMAGE_2=$RTOS_BUILD_DIR/rtos_2.bin
122BL22_IMAGE=$RTOS_BUILD_DIR/bl22.bin
123
124function toolchain_prepare() {
125 echo "<============ TOOLCHAIN INFO RTOS ============>"
126 CROSSTOOL=$RTOS_BASE_DIR/arch/$ARCH/toolchain/$COMPILER*$TOOLCHAIN_KEYWORD
127 TOOLCHAIN_DIR=$RTOS_BASE_DIR/output/toolchains/$COMPILER-$TOOLCHAIN_KEYWORD
128 rm -rf $RTOS_BASE_DIR/output/toolchains
129 mkdir -p $TOOLCHAIN_DIR
130 tar -xf $CROSSTOOL.tar.xz -C $TOOLCHAIN_DIR --strip-components=1
131 ls -la $TOOLCHAIN_DIR/bin
132 $TOOLCHAIN_DIR/bin/aarch64-none-elf-gcc -v
133 echo "<============ TOOLCHAIN INFO RTOS ============>"
134}
135
136#Configure the compile-time address.
137function rtos_config_prepare() {
138 CONFIG_FILE=$RTOS_BASE_DIR/boards/$ARCH/$BOARD/lscript.h
139 sed -i '/.*#define configTEXT_BASE*/c\#define configTEXT_BASE '${RTOS_TARGET_ADDRESS}'' $CONFIG_FILE
140 sed -i '/.*#define CONFIG_SCATTER_LOAD_ADDRESS*/c\#define CONFIG_SCATTER_LOAD_ADDRESS '${RTOS2_TARGET_ADDRESS}'' $CONFIG_FILE
yongbing.hefc759372024-08-05 17:51:39 +0800141 case $DDR_SIZE in
142 '128m')
143 sed -i '/.*#define configSRAM_START*/c\#define configSRAM_START '0x3000000'' $CONFIG_FILE
144 sed -i '/.*#define configSRAM_LEN*/c\#define configSRAM_LEN '0x1700000'' $CONFIG_FILE
145 sed -i '/.*#define configVENC_BASE*/c\#define configVENC_BASE '0x04700000'' $CONFIG_FILE
146 sed -i '/.*#define configVENC_LEN*/c\#define configVENC_LEN '0x0B00000'' $CONFIG_FILE
147 sed -i '/.*#define configISP_RGB_BASE*/c\#define configISP_RGB_BASE '0x6000000'' $CONFIG_FILE
148 ;;
149 '256m')
150 sed -i '/.*#define configSRAM_START*/c\#define configSRAM_START '0x9B00000'' $CONFIG_FILE
151 sed -i '/.*#define configSRAM_LEN*/c\#define configSRAM_LEN '0x3200000'' $CONFIG_FILE
152 sed -i '/.*#define configVENC_BASE*/c\#define configVENC_BASE '0xCD00000'' $CONFIG_FILE
153 sed -i '/.*#define configVENC_LEN*/c\#define configVENC_LEN '0x2A00000'' $CONFIG_FILE
154 sed -i '/.*#define configISP_RGB_BASE*/c\#define configISP_RGB_BASE '0x3700000'' $CONFIG_FILE
155 ;;
156 *)
157 sed -i '/.*#define configSRAM_START*/c\#define configSRAM_START '0x3000000'' $CONFIG_FILE
158 sed -i '/.*#define configSRAM_LEN*/c\#define configSRAM_LEN '0x1700000'' $CONFIG_FILE
159 sed -i '/.*#define configVENC_LEN*/c\#define configVENC_LEN '0x04700000'' $CONFIG_FILE
160 sed -i '/.*#define configISP_RGB_BASE*/c\#define configISP_RGB_BASE '0x6000000'' $CONFIG_FILE
161 ;;
162 esac
shijie.xiong3da90db2024-06-27 17:13:42 +0800163}
164
165function sensor_config_choose() {
yongbing.hefc759372024-08-05 17:51:39 +0800166 sed -i '/CONFIG_SENSOR_SC401AI/d' $RTOS_BASE_DIR/boards/$ARCH/$BOARD/defconfig
167 sed -i '/CONFIG_SENSOR_SC5336P/d' $RTOS_BASE_DIR/boards/$ARCH/$BOARD/defconfig
168 if [ "${SENSOR_TYPE}" == "SC401AI" ]; then
169 sed -i '$ a CONFIG_SENSOR_SC401AI=y' $RTOS_BASE_DIR/boards/$ARCH/$BOARD/defconfig
170 fi
171 if [ "${SENSOR_TYPE}" == "SC5336P" ]; then
172 sed -i '$ a CONFIG_SENSOR_SC5336P=y' $RTOS_BASE_DIR/boards/$ARCH/$BOARD/defconfig
173 fi
shijie.xiong3da90db2024-06-27 17:13:42 +0800174}
175
176function lz4_rtos() {
177 pushd $RTOS_BASE_DIR/lib/utilities/lz4
178 cp $RTOS_IMAGE_1 .
179 #Get the rtos target address
180 ./self_decompress_tool.sh -a ./self_decompress_head.bin -b ./rtos_1.bin -l 0x04c00000 -j $RTOS_TARGET_ADDRESS -d 0 -s $RTOS2_TARGET_ADDRESS
181 cp ./self_decompress_firmware.bin $RTOS_IMAGE_1
182 rm ./self_decompress_firmware.bin ./rtos_1.bin
183 popd
184}
185
186function bl22_compile() {
187 pushd $BL22_DIR
188 if [ -f ./mk ]; then
yongbing.hefc759372024-08-05 17:51:39 +0800189 echo "./mk c3 $BOARD_TYPE $SENSOR_TYPE"
190 ./mk c3 $BOARD_TYPE $SENSOR_TYPE
shijie.xiong3da90db2024-06-27 17:13:42 +0800191 if [ "$?" -ne 0 ]; then
192 echo "RTOS-SDK: BL22 compilation failed !!!"
193 exit 1
194 fi
195 fi
196 cp ./bl22.bin $RTOS_BUILD_DIR/bl22.bin
197 popd
198}
199
200function build_header() {
201 local bl2_2=$1
202 local rtos_1=$2
203 local rtos_2=$3
204
205 local size=0
206 # 4KB for header (reserve for sign)
207 local offset=4096
208 local rem=0
209
210 # build head for bl2-2.bin
211 size=$(stat -c %s ${bl2_2})
212 # insert uuid
213 echo -n -e "\x6b\xe6\x1e\x1f\xac\xc0\x45\x12\x97\x3b\x32\x5f\x2e\x17\xa7\x2d" >${RTOS_BUILD_DIR}/_tmp_hdr.bin
214 # insert size
215 printf "%02x%02x%02x%02x" $(((size) & 0xff)) $((((size) >> 8) & 0xff)) \
216 $((((size) >> 16) & 0xff)) $((((size) >> 24) & 0xff)) | xxd -r -ps >>${RTOS_BUILD_DIR}/_tmp_hdr.bin
217 # insert offset
218 printf "%02x%02x%02x%02x" $(((offset) & 0xff)) $((((offset) >> 8) & 0xff)) \
219 $((((offset) >> 16) & 0xff)) $((((offset) >> 24) & 0xff)) | xxd -r -ps >>${RTOS_BUILD_DIR}/_tmp_hdr.bin
220 # insert 8 bytes padding
221 printf "\0\0\0\0\0\0\0\0" >>${RTOS_BUILD_DIR}/_tmp_hdr.bin
222 # insert sha256
223 openssl dgst -sha256 -binary ${bl2_2} >>${RTOS_BUILD_DIR}/_tmp_hdr.bin
224
225 # build header for RTOS_1
226 offset=$(expr ${offset} + ${size} + 4095)
227 rem=$(expr ${offset} % 4096)
228 offset=$(expr ${offset} - ${rem})
229 size=$(stat -c %s ${rtos_1})
230 echo -n -e "\x31\x9f\xb3\x9e\x1f\x9f\x48\x98\x96\x38\xca\xfa\x7e\xa4\x2c\xe9" >>${RTOS_BUILD_DIR}/_tmp_hdr.bin
231 printf "%02x%02x%02x%02x" $(((size) & 0xff)) $((((size) >> 8) & 0xff)) \
232 $((((size) >> 16) & 0xff)) $((((size) >> 24) & 0xff)) | xxd -r -ps >>${RTOS_BUILD_DIR}/_tmp_hdr.bin
233 printf "%02x%02x%02x%02x" $(((offset) & 0xff)) $((((offset) >> 8) & 0xff)) \
234 $((((offset) >> 16) & 0xff)) $((((offset) >> 24) & 0xff)) | xxd -r -ps >>${RTOS_BUILD_DIR}/_tmp_hdr.bin
235 printf "\0\0\0\0\0\0\0\0" >>${RTOS_BUILD_DIR}/_tmp_hdr.bin
236 openssl dgst -sha256 -binary ${rtos_1} >>${RTOS_BUILD_DIR}/_tmp_hdr.bin
237
238 # build header for RTOS_2
239 offset=$(expr ${offset} + ${size} + 4095)
240 rem=$(expr ${offset} % 4096)
241 offset=$(expr ${offset} - ${rem})
242 size=$(stat -c %s ${rtos_2})
243 echo -n -e "\x33\x71\xb3\x2b\x17\xca\x43\xe2\xb4\xdb\x11\xe1\x3e\xc0\xa5\xf3" >>${RTOS_BUILD_DIR}/_tmp_hdr.bin
244 printf "%02x%02x%02x%02x" $(((size) & 0xff)) $((((size) >> 8) & 0xff)) \
245 $((((size) >> 16) & 0xff)) $((((size) >> 24) & 0xff)) | xxd -r -ps >>${RTOS_BUILD_DIR}/_tmp_hdr.bin
246 printf "%02x%02x%02x%02x" $(((offset) & 0xff)) $((((offset) >> 8) & 0xff)) \
247 $((((offset) >> 16) & 0xff)) $((((offset) >> 24) & 0xff)) | xxd -r -ps >>${RTOS_BUILD_DIR}/_tmp_hdr.bin
248 printf "\0\0\0\0\0\0\0\0" >>${RTOS_BUILD_DIR}/_tmp_hdr.bin
249 openssl dgst -sha256 -binary ${rtos_2} >>${RTOS_BUILD_DIR}/_tmp_hdr.bin
250}
251
252function package_binary() {
253 local fw_size=
254 local rem_val=
255 local bin1=$1
256 local bin2=$2
257 local out_bin=$3
258
259 # align bin1 size up to 4KB and padding bin2 at the end of bin1
260 fw_size=$(stat -c %s ${bin1})
261 fw_size=$(expr ${fw_size} + 4095)
262 rem_val=$(expr ${fw_size} % 4096)
263 fw_size=$(expr ${fw_size} - ${rem_val})
264 dd if=/dev/zero of=${RTOS_BUILD_DIR}/_tmp.bin bs=1 count=${fw_size} &>/dev/null
265 dd if=${bin1} of=${RTOS_BUILD_DIR}/_tmp.bin bs=1 count=${fw_size} conv=notrunc &>/dev/null
266
267 cat ${bin2} >>${RTOS_BUILD_DIR}/_tmp.bin
268 mv ${RTOS_BUILD_DIR}/_tmp.bin ${RTOS_BUILD_DIR}/${out_bin}
269 echo ==== package ${bin1} ${bin2} to ${out_bin} ====
270}
271
272function package_fastboot() {
yongbing.hefc759372024-08-05 17:51:39 +0800273if [ -n $UBOOT_DIR ]; then
274 pushd $UBOOT_DIR
275 if [ -d ./fastboot ]; then
276 rm -rf ./fastboot
277 fi
278 mkdir -p ./fastboot
279 cp $RTOS_IMAGE_1 ./fastboot
280 cp $RTOS_IMAGE_2 ./fastboot
281 cp $BL22_IMAGE ./fastboot
282 echo "./mk $UBOOT_CFG --build-nogit --ipc-ddr-size $DDR_SIZE"
283 ./mk $UBOOT_CFG --build-nogit --ipc-ddr-size $DDR_SIZE
284 if [ "$?" -ne 0 ]; then
285 if [ -d ./fastboot ]; then
286 rm -rf ./fastboot
287 fi
288 echo "RTOS-SDK: Uboot compilation failed !!!"
289 exit 1
290 fi
291
292 if [ -d ./fastboot ]; then
293 rm -rf ./fastboot
294 fi
295 popd
296fi
shijie.xiong3da90db2024-06-27 17:13:42 +0800297 build_header $BL22_IMAGE $RTOS_IMAGE_1 $RTOS_IMAGE_2
298 package_binary ${RTOS_BUILD_DIR}/_tmp_hdr.bin ${RTOS_BUILD_DIR}/bl22.bin bl22.bin
299 package_binary ${RTOS_BUILD_DIR}/bl22.bin ${RTOS_BUILD_DIR}/rtos_1.bin bl22.bin
300 package_binary ${RTOS_BUILD_DIR}/bl22.bin ${RTOS_BUILD_DIR}/rtos_2.bin rtos_fastboot.bin
301if [[ -d "$INSTALL_DIR" ]]; then
302 cp ${RTOS_BUILD_DIR}/rtos_fastboot.bin ${INSTALL_DIR} -rf
303fi
304}
305
306function debug_info() {
307 echo "<============ Kconfig RTOS ============>"
308 cat $RTOS_BASE_DIR/Kconfig
309 echo "<============ CMakeLists RTOS ============>"
310 cat $RTOS_BASE_DIR/CMakeLists.txt
311 echo "<============ XML RTOS ============>"
312 cat $RTOS_BUILD_DIR/rtos_sdk_manifest.xml
313 echo "<============ XML OLD RTOS ============>"
314 cat $RTOS_BUILD_DIR/rtos_sdk_manifest_old.xml
315 echo "<============ JENKINS FOR RTOS ============>"
316}
317
318#Configure the RTOS environment
319rtos_config_prepare
320#choose sensor
321sensor_config_choose
322#Compile toolchain preparation
323toolchain_prepare
324#compile the rtos image
325cd $RTOS_BASE_DIR && make
326if [ "$?" -ne 0 ]; then
327 echo "RTOS-SDK: RTOS compilation failed !!!"
328 exit 1
329fi
330#lz4 compression
331lz4_rtos
332#compile the bl22 image
333bl22_compile
334#Packaging RTOS binary files
335package_fastboot
336#debug
337# debug_info