blob: 5628924eabcd794a80ca125ca109f2b8464dd1ab [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
11PARSED=$(getopt --options o:a:b:s:h --long address:,board:,bl22:,sensor:,out:,help --name "$0" -- "$@")
12
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)
30 if [[ "$2" =~ ^SENSOR=.* ]]; then
31 SENSOR_TYPE=${2#SENSOR=}
32 fi
33 shift 2
34 ;;
35 --bl22)
36 BL22_DIR=$2
37 shift 2
38 ;;
39 -o | --out)
40 INSTALL_DIR=$2
41 shift 2
42 ;;
43 -h | --help)
44 echo -e "\033[41;33m gen_fastboot.sh help info \033[0m"
45 echo "Usage: fastboot [OPTIONS]"
46 echo
47 echo "Options:"
48 echo " -a/--address et address to load (e.g., -a 0x2200000)"
49 echo " -b/--board Set board type (e.g., -b aw402_c302x)"
50 echo " -s/--sensor Set ipc sensor type (e.g., -s IMX290)"
51 echo " --b22 PATH Set BL22 path"
52 echo " -h Display this help information"
53 echo
54 echo "For more details, refer to the fastboot documentation."
55 exit 1
56 shift
57 ;;
58 --)
59 shift
60 break
61 ;;
62 *)
63 echo "Invalid option: $1" >&2
64 exit 1
65 ;;
66 esac
67done
68
69if [[ ! -d "$BL22_DIR" ]]; then
70 echo "The provided bl22 path is not a directory: $BL22_DIR"
71 exit 1
72fi
73
74#Retrieve the RTOS loading address; if none exists, set it to the default address.
75if [ -z $RTOS_TARGET_ADDRESS ]; then
76 case $BOARD_TYPE in
77 'aw409_c302x')
78 RTOS_TARGET_ADDRESS=0x5400000
79 ;;
80 'aw402_c302x')
81 RTOS_TARGET_ADDRESS=0x7600000
82 ;;
83 'aw402s_c302x')
84 RTOS_TARGET_ADDRESS=0x7600000
85 ;;
86 *)
87 RTOS_TARGET_ADDRESS=0x9000000
88 ;;
89 esac
90fi
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
141}
142
143function sensor_config_choose() {
144 sed -i '/CONFIG_SENSOR_SC401AI/d' $RTOS_BASE_DIR/boards/$ARCH/$BOARD/defconfig
145 if [ "${SENSOR_TYPE}" == "SC401AI" ]; then
146 sed -i '$ a CONFIG_SENSOR_SC401AI=y' $RTOS_BASE_DIR/boards/$ARCH/$BOARD/defconfig
147 fi
148}
149
150function lz4_rtos() {
151 pushd $RTOS_BASE_DIR/lib/utilities/lz4
152 cp $RTOS_IMAGE_1 .
153 #Get the rtos target address
154 ./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
155 cp ./self_decompress_firmware.bin $RTOS_IMAGE_1
156 rm ./self_decompress_firmware.bin ./rtos_1.bin
157 popd
158}
159
160function bl22_compile() {
161 pushd $BL22_DIR
162 if [ -f ./mk ]; then
163 ./mk c3 $BOARD_TYPE
164 if [ "$?" -ne 0 ]; then
165 echo "RTOS-SDK: BL22 compilation failed !!!"
166 exit 1
167 fi
168 fi
169 cp ./bl22.bin $RTOS_BUILD_DIR/bl22.bin
170 popd
171}
172
173function build_header() {
174 local bl2_2=$1
175 local rtos_1=$2
176 local rtos_2=$3
177
178 local size=0
179 # 4KB for header (reserve for sign)
180 local offset=4096
181 local rem=0
182
183 # build head for bl2-2.bin
184 size=$(stat -c %s ${bl2_2})
185 # insert uuid
186 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
187 # insert size
188 printf "%02x%02x%02x%02x" $(((size) & 0xff)) $((((size) >> 8) & 0xff)) \
189 $((((size) >> 16) & 0xff)) $((((size) >> 24) & 0xff)) | xxd -r -ps >>${RTOS_BUILD_DIR}/_tmp_hdr.bin
190 # insert offset
191 printf "%02x%02x%02x%02x" $(((offset) & 0xff)) $((((offset) >> 8) & 0xff)) \
192 $((((offset) >> 16) & 0xff)) $((((offset) >> 24) & 0xff)) | xxd -r -ps >>${RTOS_BUILD_DIR}/_tmp_hdr.bin
193 # insert 8 bytes padding
194 printf "\0\0\0\0\0\0\0\0" >>${RTOS_BUILD_DIR}/_tmp_hdr.bin
195 # insert sha256
196 openssl dgst -sha256 -binary ${bl2_2} >>${RTOS_BUILD_DIR}/_tmp_hdr.bin
197
198 # build header for RTOS_1
199 offset=$(expr ${offset} + ${size} + 4095)
200 rem=$(expr ${offset} % 4096)
201 offset=$(expr ${offset} - ${rem})
202 size=$(stat -c %s ${rtos_1})
203 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
204 printf "%02x%02x%02x%02x" $(((size) & 0xff)) $((((size) >> 8) & 0xff)) \
205 $((((size) >> 16) & 0xff)) $((((size) >> 24) & 0xff)) | xxd -r -ps >>${RTOS_BUILD_DIR}/_tmp_hdr.bin
206 printf "%02x%02x%02x%02x" $(((offset) & 0xff)) $((((offset) >> 8) & 0xff)) \
207 $((((offset) >> 16) & 0xff)) $((((offset) >> 24) & 0xff)) | xxd -r -ps >>${RTOS_BUILD_DIR}/_tmp_hdr.bin
208 printf "\0\0\0\0\0\0\0\0" >>${RTOS_BUILD_DIR}/_tmp_hdr.bin
209 openssl dgst -sha256 -binary ${rtos_1} >>${RTOS_BUILD_DIR}/_tmp_hdr.bin
210
211 # build header for RTOS_2
212 offset=$(expr ${offset} + ${size} + 4095)
213 rem=$(expr ${offset} % 4096)
214 offset=$(expr ${offset} - ${rem})
215 size=$(stat -c %s ${rtos_2})
216 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
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 printf "%02x%02x%02x%02x" $(((offset) & 0xff)) $((((offset) >> 8) & 0xff)) \
220 $((((offset) >> 16) & 0xff)) $((((offset) >> 24) & 0xff)) | xxd -r -ps >>${RTOS_BUILD_DIR}/_tmp_hdr.bin
221 printf "\0\0\0\0\0\0\0\0" >>${RTOS_BUILD_DIR}/_tmp_hdr.bin
222 openssl dgst -sha256 -binary ${rtos_2} >>${RTOS_BUILD_DIR}/_tmp_hdr.bin
223}
224
225function package_binary() {
226 local fw_size=
227 local rem_val=
228 local bin1=$1
229 local bin2=$2
230 local out_bin=$3
231
232 # align bin1 size up to 4KB and padding bin2 at the end of bin1
233 fw_size=$(stat -c %s ${bin1})
234 fw_size=$(expr ${fw_size} + 4095)
235 rem_val=$(expr ${fw_size} % 4096)
236 fw_size=$(expr ${fw_size} - ${rem_val})
237 dd if=/dev/zero of=${RTOS_BUILD_DIR}/_tmp.bin bs=1 count=${fw_size} &>/dev/null
238 dd if=${bin1} of=${RTOS_BUILD_DIR}/_tmp.bin bs=1 count=${fw_size} conv=notrunc &>/dev/null
239
240 cat ${bin2} >>${RTOS_BUILD_DIR}/_tmp.bin
241 mv ${RTOS_BUILD_DIR}/_tmp.bin ${RTOS_BUILD_DIR}/${out_bin}
242 echo ==== package ${bin1} ${bin2} to ${out_bin} ====
243}
244
245function package_fastboot() {
246 build_header $BL22_IMAGE $RTOS_IMAGE_1 $RTOS_IMAGE_2
247 package_binary ${RTOS_BUILD_DIR}/_tmp_hdr.bin ${RTOS_BUILD_DIR}/bl22.bin bl22.bin
248 package_binary ${RTOS_BUILD_DIR}/bl22.bin ${RTOS_BUILD_DIR}/rtos_1.bin bl22.bin
249 package_binary ${RTOS_BUILD_DIR}/bl22.bin ${RTOS_BUILD_DIR}/rtos_2.bin rtos_fastboot.bin
250if [[ -d "$INSTALL_DIR" ]]; then
251 cp ${RTOS_BUILD_DIR}/rtos_fastboot.bin ${INSTALL_DIR} -rf
252fi
253}
254
255function debug_info() {
256 echo "<============ Kconfig RTOS ============>"
257 cat $RTOS_BASE_DIR/Kconfig
258 echo "<============ CMakeLists RTOS ============>"
259 cat $RTOS_BASE_DIR/CMakeLists.txt
260 echo "<============ XML RTOS ============>"
261 cat $RTOS_BUILD_DIR/rtos_sdk_manifest.xml
262 echo "<============ XML OLD RTOS ============>"
263 cat $RTOS_BUILD_DIR/rtos_sdk_manifest_old.xml
264 echo "<============ JENKINS FOR RTOS ============>"
265}
266
267#Configure the RTOS environment
268rtos_config_prepare
269#choose sensor
270sensor_config_choose
271#Compile toolchain preparation
272toolchain_prepare
273#compile the rtos image
274cd $RTOS_BASE_DIR && make
275if [ "$?" -ne 0 ]; then
276 echo "RTOS-SDK: RTOS compilation failed !!!"
277 exit 1
278fi
279#lz4 compression
280lz4_rtos
281#compile the bl22 image
282bl22_compile
283#Packaging RTOS binary files
284package_fastboot
285#debug
286# debug_info