Files

235 lines
5.9 KiB
Bash
Executable File

#
# Copyright (C) 2025 Amlogic, Inc. All rights reserved.
#
#!/bin/bash
set -e
BL22=
RTOS1=
RTOS2=
OUT_FILE="blob-rtos.bin"
FIP_BUILD_FOLDER="fip/_tmp/"
RTOS1_RUN_ADDR=0x2200000
RTOS2_RUN_ADDR=0x2300000
function align_4k() {
local file=$1
local raw_size=
local file_size=
local rem_val=
raw_size=`stat -c %s ${file}`
file_size=`expr ${raw_size} + 4095`
rem_val=`expr ${file_size} % 4096`
file_size=`expr ${file_size} - ${rem_val}`
#echo ==== align file ${file} from ${raw_size} to ${file_size} ====
dd if=/dev/zero of=${file}_tmp.bin bs=1 count=${file_size} &> /dev/null
dd if=${file} of=${file}_tmp.bin bs=1 count=${file_size} conv=notrunc &> /dev/null
mv ${file}_tmp.bin ${file}
}
function amfc_compress() {
local file=$1
local run_addr=$2
amfc_zstd_hdr=amfc_zstd_hdr.bin
./fip/tools/aml_zstd ${file} -19 -o ${file}.tmp
bin_org_size=`stat -c %s ${file}`
bin_zstd_size=`stat -c %s ${file}.tmp`
printf "%s" "@ZST" > $amfc_zstd_hdr
printf "%02x%02x%02x%02x" $[(bin_org_size) & 0xff] \
$[((bin_org_size) >> 8) & 0xff] $[((bin_org_size) >> 16) & 0xff] \
$[((bin_org_size) >> 24) & 0xff] | xxd -r -ps >> $amfc_zstd_hdr
printf "%02x%02x%02x%02x" $[(bin_zstd_size) & 0xff] \
$[((bin_zstd_size) >> 8) & 0xff] $[((bin_zstd_size) >> 16) & 0xff] \
$[((bin_zstd_size) >> 24) & 0xff] | xxd -r -ps >> $amfc_zstd_hdr
printf "%02x%02x%02x%02x" $(((run_addr) & 0xff)) \
$((((run_addr) >> 8) & 0xff)) $((((run_addr) >> 16) & 0xff)) \
$((((run_addr) >> 24) & 0xff)) | xxd -r -ps >> $amfc_zstd_hdr
cat $amfc_zstd_hdr ${file}.tmp > ${file}.zstd
rm $amfc_zstd_hdr ${file}.tmp -f
}
function package_binary()
{
local fw_size=
local rem_val=
local bin1=$1
local bin2=$2
local out_bin=$3
# align bin1 size up to 4KB and padding bin2 at the end of bin1
fw_size=`stat -c %s ${bin1}`
fw_size=`expr ${fw_size} + 4095`
rem_val=`expr ${fw_size} % 4096`
fw_size=`expr ${fw_size} - ${rem_val}`
dd if=/dev/zero of=${bin1}_tmp.bin bs=1 count=${fw_size} &> /dev/null
dd if=${bin1} of=${bin1}_tmp.bin bs=1 count=${fw_size} conv=notrunc &> /dev/null
cat ${bin2} >> ${bin1}_tmp.bin
mv ${bin1}_tmp.bin ${out_bin}
echo ==== package ${bin1} ${bin2} to ${out_bin} ====
}
function usage() {
cat << EOF
Usage:
c4_create_rtos.sh --help
create boot blobs for c4, input payload will be compressed before create bootblob
c4_create_rtos.sh --bl22 [bl22.bin] --rtos1 [rtos1.bin] --rtos2 [rtos2.bin] --out rtos.bin
EOF
exit 1
}
function parser() {
local i=0
local argv=()
for arg in "$@" ; do
argv[$i]="$arg"
i=$((i + 1))
done
i=0
while [ $i -lt $# ]; do
arg="${argv[$i]}"
i=$((i + 1)) # must place here
case "$arg" in
--h|--help)
usage
exit ;;
--bl22)
BL22="${argv[$i]}"
i=$((i + 1))
continue ;;
--rtos1)
RTOS1="${argv[$i]}"
i=$((i + 1))
continue ;;
--rtos1_addr)
RTOS1_RUN_ADDR="${argv[$i]}"
i=$((i + 1))
continue ;;
--rtos2)
RTOS2="${argv[$i]}"
i=$((i + 1))
continue ;;
--rtos2_addr)
RTOS2_RUN_ADDR="${argv[$i]}"
i=$((i + 1))
continue ;;
--out)
OUT_FILE="${argv[$i]}"
i=$((i + 1))
continue ;;
*)
echo "unsupported args: ${arg}"
usage
exit ;;
esac
done
}
# steps:
# 1, compress for rtos1/rtos2
# 2, align compressed file size to 4K
# 3, call acpu image tools to generate blobs
# 4, package blobs to final output
function create_blob() {
local bl22_file_size
local rtos1_file_size
local rtos2_file_size
# process bl22
cp -rf ${BL22} ${FIP_BUILD_FOLDER}/bl22.bin
align_4k ${FIP_BUILD_FOLDER}/bl22.bin
bl22_file_size=`stat -c %s ${FIP_BUILD_FOLDER}/bl22.bin`
echo bl22 size: ${bl22_file_size}
if [ ${bl22_file_size} -gt 28672 ] || [ ${bl22_file_size} -lt 8192 ]; then
echo bl22 size ${bl22_file_size} wrong
exit -1
fi
# process rtos1
cp -rf ${RTOS1} ${FIP_BUILD_FOLDER}/rtos1.bin
amfc_compress ${FIP_BUILD_FOLDER}/rtos1.bin $RTOS1_RUN_ADDR
align_4k ${FIP_BUILD_FOLDER}/rtos1.bin.zstd
rtos1_file_size=`stat -c %s ${FIP_BUILD_FOLDER}/rtos1.bin.zstd`
echo rtos1 size: ${rtos1_file_size}
if [ ${rtos1_file_size} -gt 5242880 ] || [ ${rtos1_file_size} -lt 16384 ]; then
echo rtos1 size ${rtos1_file_size} wrong
exit -1
fi
# process rtos2
if [ -s "${RTOS2}" ]; then
cp -rf ${RTOS2} ${FIP_BUILD_FOLDER}/rtos2.bin
amfc_compress ${FIP_BUILD_FOLDER}/rtos2.bin $RTOS2_RUN_ADDR
align_4k ${FIP_BUILD_FOLDER}/rtos2.bin.zstd
rtos2_file_size=`stat -c %s ${FIP_BUILD_FOLDER}/rtos2.bin.zstd`
echo rtos2 size: ${rtos2_file_size}
if [ ${rtos2_file_size} -gt 20971520 ] || [ ${rtos2_file_size} -lt 65536 ]; then
echo rtos2 size ${rtos2_file_size} wrong
exit -1
fi
fip/c4/binary-tool/acpu-imagetool create-boot-blobs --scs-family=c4 \
--infile-bl2-2-payload=${FIP_BUILD_FOLDER}/bl22.bin \
--size-payload-bl2-2=${bl22_file_size} \
--infile-rtos-1-payload=${FIP_BUILD_FOLDER}/rtos1.bin.zstd \
--size-payload-rtos-1=${rtos1_file_size} \
--infile-rtos-2-payload=${FIP_BUILD_FOLDER}/rtos2.bin.zstd \
--size-payload-rtos-2=${rtos2_file_size} \
--outfile-blob-bl2-rtos=${FIP_BUILD_FOLDER}/rtos-blob-final.bin
else
fip/c4/binary-tool/acpu-imagetool create-boot-blobs --scs-family=c4 \
--infile-bl2-2-payload=${FIP_BUILD_FOLDER}/bl22.bin \
--size-payload-bl2-2=${bl22_file_size} \
--infile-rtos-1-payload=${FIP_BUILD_FOLDER}/rtos1.bin.zstd \
--size-payload-rtos-1=${rtos1_file_size} \
--size-payload-rtos-2=0 \
--outfile-blob-bl2-rtos=${FIP_BUILD_FOLDER}/rtos-blob-final.bin
fi
cp ${FIP_BUILD_FOLDER}/rtos-blob-final.bin ${OUT_FILE}
# clean up
#rm -rf ${BL22}.temp_size
#rm -rf ${BL22}.tmp
#rm -rf ${BL22}-rtos1.bin
#rm -rf ${BL22}-blob.bin
#rm -rf ${RTOS1}.tmp
#rm -rf ${RTOS1}.zstd
#rm -rf ${RTOS1}-blob.bin
#rm -rf ${RTOS2}.tmp
#rm -rf ${RTOS2}.zstd
#rm -rf ${RTOS2}-blob.bin
}
parser $@
if [ -z ${BL22} ]; then
echo "ERROR: NO bl22"
usage
exit -1
fi
if [ -z ${RTOS1} ]; then
echo "ERROR:NO rtos1"
usage
exit -1
fi
if [ -z ${RTOS2} ]; then
echo "NO rtos2"
fi
create_blob ${BL22} ${RTOS1} ${RTOS2} ${OUT_FILE}