blob: cc6338894680c0cde757d414ec905943c887d8f8 [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.hecb647a72024-09-11 15:46:34 +080011RTOS_TARGET_ADDRESS=0x2200000
12BOARD_TYPE=aw402_c302x
13SENSOR_TYPE=SC301IOT
14DDR_SIZE=128m
15UBOOT_CFG=c3_aw402
16
yongbing.he9dab8742024-08-30 18:29:05 +080017while [[ $# -gt 0 ]]; do
shijie.xiong3da90db2024-06-27 17:13:42 +080018 case "$1" in
19 -a | --address)
20 RTOS_TARGET_ADDRESS=$2
21 shift 2
22 ;;
23 -b | --board)
24 BOARD_TYPE=$2
25 shift 2
26 ;;
27 -s | --sensor)
yongbing.hefc759372024-08-05 17:51:39 +080028 SENSOR_TYPE=$2
shijie.xiong3da90db2024-06-27 17:13:42 +080029 if [[ "$2" =~ ^SENSOR=.* ]]; then
30 SENSOR_TYPE=${2#SENSOR=}
31 fi
32 shift 2
33 ;;
34 --bl22)
35 BL22_DIR=$2
36 shift 2
37 ;;
yongbing.hefc759372024-08-05 17:51:39 +080038 --uboot)
39 UBOOT_DIR=$2
40 shift 2
41 ;;
42 --ipc-ddr-size)
43 DDR_SIZE=$2
44 shift 2
45 ;;
46 --ubootcfg)
47 UBOOT_CFG=$2
48 shift 2
49 ;;
shijie.xiong3da90db2024-06-27 17:13:42 +080050 -o | --out)
51 INSTALL_DIR=$2
52 shift 2
53 ;;
54 -h | --help)
55 echo -e "\033[41;33m gen_fastboot.sh help info \033[0m"
56 echo "Usage: fastboot [OPTIONS]"
57 echo
58 echo "Options:"
59 echo " -a/--address et address to load (e.g., -a 0x2200000)"
60 echo " -b/--board Set board type (e.g., -b aw402_c302x)"
61 echo " -s/--sensor Set ipc sensor type (e.g., -s IMX290)"
62 echo " --b22 PATH Set BL22 path"
63 echo " -h Display this help information"
64 echo
65 echo "For more details, refer to the fastboot documentation."
66 exit 1
67 shift
68 ;;
69 --)
70 shift
71 break
72 ;;
yongbing.he9dab8742024-08-30 18:29:05 +080073 -*|--*)
shijie.xiong3da90db2024-06-27 17:13:42 +080074 echo "Invalid option: $1" >&2
75 exit 1
76 ;;
yongbing.he9dab8742024-08-30 18:29:05 +080077 *)
78 echo "No more options, just arguments" >&2
79 exit 1
80 ;;
shijie.xiong3da90db2024-06-27 17:13:42 +080081 esac
82done
83
84if [[ ! -d "$BL22_DIR" ]]; then
85 echo "The provided bl22 path is not a directory: $BL22_DIR"
86 exit 1
87fi
88
89#Retrieve the RTOS loading address; if none exists, set it to the default address.
90if [ -z $RTOS_TARGET_ADDRESS ]; then
yongbing.hefc759372024-08-05 17:51:39 +080091 RTOS_TARGET_ADDRESS=0x02200000
shijie.xiong3da90db2024-06-27 17:13:42 +080092fi
93
94#Calculate the RTOS2 loading address
95let "RTOS2_TARGET_ADDRESS=$RTOS_TARGET_ADDRESS+0x100000"
96RTOS2_TARGET_ADDRESS=$(printf '0x%x\n' $RTOS2_TARGET_ADDRESS)
97
98#Clear cache files
99[ -d $RTOS_BASE_DIR/output ] && rm -rf $RTOS_BASE_DIR/output
100
101#Get the current project environment variables
102source $RTOS_BASE_DIR/scripts/env.sh arm64 c3 $BOARD_TYPE fastboot
103if [ "$?" -ne 0 ]; then
104 echo "Error: Incorrect board type selection!"
105 exit 1
106fi
107
108#IPC sensor model configuration.
109if [ -z $SENSOR_TYPE ]; then
110 case $BOARD_TYPE in
111 'aw402_c302x')
112 SENSOR_TYPE=SC301IOT
113 ;;
114 *)
115 SENSOR_TYPE=IMX290
116 ;;
117 esac
118fi
119
120#RTOS object file path
121RTOS_BUILD_DIR=$RTOS_BASE_DIR/output/$ARCH-$BOARD-$PRODUCT/freertos
122RTOS_IMAGE_1=$RTOS_BUILD_DIR/rtos_1.bin
123RTOS_IMAGE_2=$RTOS_BUILD_DIR/rtos_2.bin
124BL22_IMAGE=$RTOS_BUILD_DIR/bl22.bin
125
126function toolchain_prepare() {
127 echo "<============ TOOLCHAIN INFO RTOS ============>"
128 CROSSTOOL=$RTOS_BASE_DIR/arch/$ARCH/toolchain/$COMPILER*$TOOLCHAIN_KEYWORD
129 TOOLCHAIN_DIR=$RTOS_BASE_DIR/output/toolchains/$COMPILER-$TOOLCHAIN_KEYWORD
130 rm -rf $RTOS_BASE_DIR/output/toolchains
131 mkdir -p $TOOLCHAIN_DIR
132 tar -xf $CROSSTOOL.tar.xz -C $TOOLCHAIN_DIR --strip-components=1
133 ls -la $TOOLCHAIN_DIR/bin
134 $TOOLCHAIN_DIR/bin/aarch64-none-elf-gcc -v
135 echo "<============ TOOLCHAIN INFO RTOS ============>"
136}
137
138#Configure the compile-time address.
139function rtos_config_prepare() {
140 CONFIG_FILE=$RTOS_BASE_DIR/boards/$ARCH/$BOARD/lscript.h
141 sed -i '/.*#define configTEXT_BASE*/c\#define configTEXT_BASE '${RTOS_TARGET_ADDRESS}'' $CONFIG_FILE
142 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 +0800143 case $DDR_SIZE in
144 '128m')
145 sed -i '/.*#define configSRAM_START*/c\#define configSRAM_START '0x3000000'' $CONFIG_FILE
146 sed -i '/.*#define configSRAM_LEN*/c\#define configSRAM_LEN '0x1700000'' $CONFIG_FILE
147 sed -i '/.*#define configVENC_BASE*/c\#define configVENC_BASE '0x04700000'' $CONFIG_FILE
148 sed -i '/.*#define configVENC_LEN*/c\#define configVENC_LEN '0x0B00000'' $CONFIG_FILE
149 sed -i '/.*#define configISP_RGB_BASE*/c\#define configISP_RGB_BASE '0x6000000'' $CONFIG_FILE
150 ;;
151 '256m')
152 sed -i '/.*#define configSRAM_START*/c\#define configSRAM_START '0x9B00000'' $CONFIG_FILE
153 sed -i '/.*#define configSRAM_LEN*/c\#define configSRAM_LEN '0x3200000'' $CONFIG_FILE
154 sed -i '/.*#define configVENC_BASE*/c\#define configVENC_BASE '0xCD00000'' $CONFIG_FILE
155 sed -i '/.*#define configVENC_LEN*/c\#define configVENC_LEN '0x2A00000'' $CONFIG_FILE
156 sed -i '/.*#define configISP_RGB_BASE*/c\#define configISP_RGB_BASE '0x3700000'' $CONFIG_FILE
157 ;;
158 *)
159 sed -i '/.*#define configSRAM_START*/c\#define configSRAM_START '0x3000000'' $CONFIG_FILE
160 sed -i '/.*#define configSRAM_LEN*/c\#define configSRAM_LEN '0x1700000'' $CONFIG_FILE
161 sed -i '/.*#define configVENC_LEN*/c\#define configVENC_LEN '0x04700000'' $CONFIG_FILE
162 sed -i '/.*#define configISP_RGB_BASE*/c\#define configISP_RGB_BASE '0x6000000'' $CONFIG_FILE
163 ;;
164 esac
shijie.xiong3da90db2024-06-27 17:13:42 +0800165}
166
167function sensor_config_choose() {
yongbing.hefc759372024-08-05 17:51:39 +0800168 sed -i '/CONFIG_SENSOR_SC401AI/d' $RTOS_BASE_DIR/boards/$ARCH/$BOARD/defconfig
169 sed -i '/CONFIG_SENSOR_SC5336P/d' $RTOS_BASE_DIR/boards/$ARCH/$BOARD/defconfig
170 if [ "${SENSOR_TYPE}" == "SC401AI" ]; then
171 sed -i '$ a CONFIG_SENSOR_SC401AI=y' $RTOS_BASE_DIR/boards/$ARCH/$BOARD/defconfig
172 fi
173 if [ "${SENSOR_TYPE}" == "SC5336P" ]; then
174 sed -i '$ a CONFIG_SENSOR_SC5336P=y' $RTOS_BASE_DIR/boards/$ARCH/$BOARD/defconfig
175 fi
shijie.xiong3da90db2024-06-27 17:13:42 +0800176}
177
178function lz4_rtos() {
179 pushd $RTOS_BASE_DIR/lib/utilities/lz4
180 cp $RTOS_IMAGE_1 .
181 #Get the rtos target address
182 ./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
183 cp ./self_decompress_firmware.bin $RTOS_IMAGE_1
184 rm ./self_decompress_firmware.bin ./rtos_1.bin
185 popd
186}
187
188function bl22_compile() {
189 pushd $BL22_DIR
190 if [ -f ./mk ]; then
yongbing.hefc759372024-08-05 17:51:39 +0800191 echo "./mk c3 $BOARD_TYPE $SENSOR_TYPE"
192 ./mk c3 $BOARD_TYPE $SENSOR_TYPE
shijie.xiong3da90db2024-06-27 17:13:42 +0800193 if [ "$?" -ne 0 ]; then
194 echo "RTOS-SDK: BL22 compilation failed !!!"
195 exit 1
196 fi
197 fi
198 cp ./bl22.bin $RTOS_BUILD_DIR/bl22.bin
199 popd
200}
201
202function build_header() {
203 local bl2_2=$1
204 local rtos_1=$2
205 local rtos_2=$3
206
207 local size=0
208 # 4KB for header (reserve for sign)
209 local offset=4096
210 local rem=0
211
212 # build head for bl2-2.bin
213 size=$(stat -c %s ${bl2_2})
214 # insert uuid
215 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
216 # insert size
217 printf "%02x%02x%02x%02x" $(((size) & 0xff)) $((((size) >> 8) & 0xff)) \
218 $((((size) >> 16) & 0xff)) $((((size) >> 24) & 0xff)) | xxd -r -ps >>${RTOS_BUILD_DIR}/_tmp_hdr.bin
219 # insert offset
220 printf "%02x%02x%02x%02x" $(((offset) & 0xff)) $((((offset) >> 8) & 0xff)) \
221 $((((offset) >> 16) & 0xff)) $((((offset) >> 24) & 0xff)) | xxd -r -ps >>${RTOS_BUILD_DIR}/_tmp_hdr.bin
222 # insert 8 bytes padding
223 printf "\0\0\0\0\0\0\0\0" >>${RTOS_BUILD_DIR}/_tmp_hdr.bin
224 # insert sha256
225 openssl dgst -sha256 -binary ${bl2_2} >>${RTOS_BUILD_DIR}/_tmp_hdr.bin
226
227 # build header for RTOS_1
228 offset=$(expr ${offset} + ${size} + 4095)
229 rem=$(expr ${offset} % 4096)
230 offset=$(expr ${offset} - ${rem})
231 size=$(stat -c %s ${rtos_1})
232 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
233 printf "%02x%02x%02x%02x" $(((size) & 0xff)) $((((size) >> 8) & 0xff)) \
234 $((((size) >> 16) & 0xff)) $((((size) >> 24) & 0xff)) | xxd -r -ps >>${RTOS_BUILD_DIR}/_tmp_hdr.bin
235 printf "%02x%02x%02x%02x" $(((offset) & 0xff)) $((((offset) >> 8) & 0xff)) \
236 $((((offset) >> 16) & 0xff)) $((((offset) >> 24) & 0xff)) | xxd -r -ps >>${RTOS_BUILD_DIR}/_tmp_hdr.bin
237 printf "\0\0\0\0\0\0\0\0" >>${RTOS_BUILD_DIR}/_tmp_hdr.bin
238 openssl dgst -sha256 -binary ${rtos_1} >>${RTOS_BUILD_DIR}/_tmp_hdr.bin
239
240 # build header for RTOS_2
241 offset=$(expr ${offset} + ${size} + 4095)
242 rem=$(expr ${offset} % 4096)
243 offset=$(expr ${offset} - ${rem})
244 size=$(stat -c %s ${rtos_2})
245 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
246 printf "%02x%02x%02x%02x" $(((size) & 0xff)) $((((size) >> 8) & 0xff)) \
247 $((((size) >> 16) & 0xff)) $((((size) >> 24) & 0xff)) | xxd -r -ps >>${RTOS_BUILD_DIR}/_tmp_hdr.bin
248 printf "%02x%02x%02x%02x" $(((offset) & 0xff)) $((((offset) >> 8) & 0xff)) \
249 $((((offset) >> 16) & 0xff)) $((((offset) >> 24) & 0xff)) | xxd -r -ps >>${RTOS_BUILD_DIR}/_tmp_hdr.bin
250 printf "\0\0\0\0\0\0\0\0" >>${RTOS_BUILD_DIR}/_tmp_hdr.bin
251 openssl dgst -sha256 -binary ${rtos_2} >>${RTOS_BUILD_DIR}/_tmp_hdr.bin
252}
253
254function package_binary() {
255 local fw_size=
256 local rem_val=
257 local bin1=$1
258 local bin2=$2
259 local out_bin=$3
260
261 # align bin1 size up to 4KB and padding bin2 at the end of bin1
262 fw_size=$(stat -c %s ${bin1})
263 fw_size=$(expr ${fw_size} + 4095)
264 rem_val=$(expr ${fw_size} % 4096)
265 fw_size=$(expr ${fw_size} - ${rem_val})
266 dd if=/dev/zero of=${RTOS_BUILD_DIR}/_tmp.bin bs=1 count=${fw_size} &>/dev/null
267 dd if=${bin1} of=${RTOS_BUILD_DIR}/_tmp.bin bs=1 count=${fw_size} conv=notrunc &>/dev/null
268
269 cat ${bin2} >>${RTOS_BUILD_DIR}/_tmp.bin
270 mv ${RTOS_BUILD_DIR}/_tmp.bin ${RTOS_BUILD_DIR}/${out_bin}
271 echo ==== package ${bin1} ${bin2} to ${out_bin} ====
272}
273
274function package_fastboot() {
yongbing.hefc759372024-08-05 17:51:39 +0800275if [ -n $UBOOT_DIR ]; then
276 pushd $UBOOT_DIR
277 if [ -d ./fastboot ]; then
278 rm -rf ./fastboot
279 fi
280 mkdir -p ./fastboot
281 cp $RTOS_IMAGE_1 ./fastboot
282 cp $RTOS_IMAGE_2 ./fastboot
283 cp $BL22_IMAGE ./fastboot
284 echo "./mk $UBOOT_CFG --build-nogit --ipc-ddr-size $DDR_SIZE"
285 ./mk $UBOOT_CFG --build-nogit --ipc-ddr-size $DDR_SIZE
286 if [ "$?" -ne 0 ]; then
287 if [ -d ./fastboot ]; then
288 rm -rf ./fastboot
289 fi
290 echo "RTOS-SDK: Uboot compilation failed !!!"
291 exit 1
292 fi
293
294 if [ -d ./fastboot ]; then
295 rm -rf ./fastboot
296 fi
297 popd
298fi
shijie.xiong3da90db2024-06-27 17:13:42 +0800299 build_header $BL22_IMAGE $RTOS_IMAGE_1 $RTOS_IMAGE_2
300 package_binary ${RTOS_BUILD_DIR}/_tmp_hdr.bin ${RTOS_BUILD_DIR}/bl22.bin bl22.bin
301 package_binary ${RTOS_BUILD_DIR}/bl22.bin ${RTOS_BUILD_DIR}/rtos_1.bin bl22.bin
302 package_binary ${RTOS_BUILD_DIR}/bl22.bin ${RTOS_BUILD_DIR}/rtos_2.bin rtos_fastboot.bin
303if [[ -d "$INSTALL_DIR" ]]; then
304 cp ${RTOS_BUILD_DIR}/rtos_fastboot.bin ${INSTALL_DIR} -rf
305fi
306}
307
308function debug_info() {
309 echo "<============ Kconfig RTOS ============>"
310 cat $RTOS_BASE_DIR/Kconfig
311 echo "<============ CMakeLists RTOS ============>"
312 cat $RTOS_BASE_DIR/CMakeLists.txt
313 echo "<============ XML RTOS ============>"
314 cat $RTOS_BUILD_DIR/rtos_sdk_manifest.xml
315 echo "<============ XML OLD RTOS ============>"
316 cat $RTOS_BUILD_DIR/rtos_sdk_manifest_old.xml
317 echo "<============ JENKINS FOR RTOS ============>"
318}
319
320#Configure the RTOS environment
321rtos_config_prepare
322#choose sensor
323sensor_config_choose
324#Compile toolchain preparation
325toolchain_prepare
326#compile the rtos image
327cd $RTOS_BASE_DIR && make
328if [ "$?" -ne 0 ]; then
329 echo "RTOS-SDK: RTOS compilation failed !!!"
330 exit 1
331fi
332#lz4 compression
333lz4_rtos
334#compile the bl22 image
335bl22_compile
336#Packaging RTOS binary files
337package_fastboot
338#debug
339# debug_info