shijie.xiong | 3da90db | 2024-06-27 17:13:42 +0800 | [diff] [blame] | 1 | #!/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 |
| 9 | RTOS_BASE_DIR=$(realpath $(dirname $(readlink -f ${BASH_SOURCE[0]:-$0}))/..) |
| 10 | |
yongbing.he | fc75937 | 2024-08-05 17:51:39 +0800 | [diff] [blame^] | 11 | PARSED=$(getopt --options o:a:b:s:h --long address:,board:,bl22:,sensor:,out:,uboot:,ipc-ddr-size:,ubootcfg:,help --name "$0" -- "$@") |
shijie.xiong | 3da90db | 2024-06-27 17:13:42 +0800 | [diff] [blame] | 12 | |
| 13 | if [[ $? -ne 0 ]]; then |
| 14 | exit 1 |
| 15 | fi |
| 16 | |
| 17 | eval set -- "$PARSED" |
| 18 | |
| 19 | while 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.he | fc75937 | 2024-08-05 17:51:39 +0800 | [diff] [blame^] | 30 | SENSOR_TYPE=$2 |
shijie.xiong | 3da90db | 2024-06-27 17:13:42 +0800 | [diff] [blame] | 31 | 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.he | fc75937 | 2024-08-05 17:51:39 +0800 | [diff] [blame^] | 40 | --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.xiong | 3da90db | 2024-06-27 17:13:42 +0800 | [diff] [blame] | 52 | -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 |
| 80 | done |
| 81 | |
| 82 | if [[ ! -d "$BL22_DIR" ]]; then |
| 83 | echo "The provided bl22 path is not a directory: $BL22_DIR" |
| 84 | exit 1 |
| 85 | fi |
| 86 | |
| 87 | #Retrieve the RTOS loading address; if none exists, set it to the default address. |
| 88 | if [ -z $RTOS_TARGET_ADDRESS ]; then |
yongbing.he | fc75937 | 2024-08-05 17:51:39 +0800 | [diff] [blame^] | 89 | RTOS_TARGET_ADDRESS=0x02200000 |
shijie.xiong | 3da90db | 2024-06-27 17:13:42 +0800 | [diff] [blame] | 90 | fi |
| 91 | |
| 92 | #Calculate the RTOS2 loading address |
| 93 | let "RTOS2_TARGET_ADDRESS=$RTOS_TARGET_ADDRESS+0x100000" |
| 94 | RTOS2_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 |
| 100 | source $RTOS_BASE_DIR/scripts/env.sh arm64 c3 $BOARD_TYPE fastboot |
| 101 | if [ "$?" -ne 0 ]; then |
| 102 | echo "Error: Incorrect board type selection!" |
| 103 | exit 1 |
| 104 | fi |
| 105 | |
| 106 | #IPC sensor model configuration. |
| 107 | if [ -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 |
| 116 | fi |
| 117 | |
| 118 | #RTOS object file path |
| 119 | RTOS_BUILD_DIR=$RTOS_BASE_DIR/output/$ARCH-$BOARD-$PRODUCT/freertos |
| 120 | RTOS_IMAGE_1=$RTOS_BUILD_DIR/rtos_1.bin |
| 121 | RTOS_IMAGE_2=$RTOS_BUILD_DIR/rtos_2.bin |
| 122 | BL22_IMAGE=$RTOS_BUILD_DIR/bl22.bin |
| 123 | |
| 124 | function 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. |
| 137 | function 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.he | fc75937 | 2024-08-05 17:51:39 +0800 | [diff] [blame^] | 141 | 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.xiong | 3da90db | 2024-06-27 17:13:42 +0800 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | function sensor_config_choose() { |
yongbing.he | fc75937 | 2024-08-05 17:51:39 +0800 | [diff] [blame^] | 166 | 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.xiong | 3da90db | 2024-06-27 17:13:42 +0800 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | function 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 | |
| 186 | function bl22_compile() { |
| 187 | pushd $BL22_DIR |
| 188 | if [ -f ./mk ]; then |
yongbing.he | fc75937 | 2024-08-05 17:51:39 +0800 | [diff] [blame^] | 189 | echo "./mk c3 $BOARD_TYPE $SENSOR_TYPE" |
| 190 | ./mk c3 $BOARD_TYPE $SENSOR_TYPE |
shijie.xiong | 3da90db | 2024-06-27 17:13:42 +0800 | [diff] [blame] | 191 | 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 | |
| 200 | function 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 | |
| 252 | function 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 | |
| 272 | function package_fastboot() { |
yongbing.he | fc75937 | 2024-08-05 17:51:39 +0800 | [diff] [blame^] | 273 | if [ -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 |
| 296 | fi |
shijie.xiong | 3da90db | 2024-06-27 17:13:42 +0800 | [diff] [blame] | 297 | 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 |
| 301 | if [[ -d "$INSTALL_DIR" ]]; then |
| 302 | cp ${RTOS_BUILD_DIR}/rtos_fastboot.bin ${INSTALL_DIR} -rf |
| 303 | fi |
| 304 | } |
| 305 | |
| 306 | function 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 |
| 319 | rtos_config_prepare |
| 320 | #choose sensor |
| 321 | sensor_config_choose |
| 322 | #Compile toolchain preparation |
| 323 | toolchain_prepare |
| 324 | #compile the rtos image |
| 325 | cd $RTOS_BASE_DIR && make |
| 326 | if [ "$?" -ne 0 ]; then |
| 327 | echo "RTOS-SDK: RTOS compilation failed !!!" |
| 328 | exit 1 |
| 329 | fi |
| 330 | #lz4 compression |
| 331 | lz4_rtos |
| 332 | #compile the bl22 image |
| 333 | bl22_compile |
| 334 | #Packaging RTOS binary files |
| 335 | package_fastboot |
| 336 | #debug |
| 337 | # debug_info |