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