blob: 8f0bfa9725b0791881ccef9777f3a6f470ad60b1 [file] [log] [blame]
fugui.zhang42666572023-09-27 16:33:20 +08001#! /bin/bash
2#
3# Copyright (c) 2021-2022 Amlogic, Inc. All rights reserved.
4#
5# SPDX-License-Identifier: MIT
6#
7
8MAIN_DIR=$(realpath $(dirname $(readlink -f ${BASH_SOURCE[0]:-$0}))/..)
9
10#$1: bl2.bin
11#$2: bl2_tmp.bin
12#$3: bl2_fixed.bin
13#$4: acs.bin
14#$5: acs_fixed.bin
15#$6: bl2_new.bin
16#This function fix bl2.bin to bl2_fixed.bin, acs.bin to acs_fixed.bin,
17#and then combine bl2_fixed.bin and acs_fixed.bin to bl2_new.bin
18function fix_bl2() {
19
20 declare -i blx_bin_limit=57344
21 declare -i blx_acs_limit=4096
22 declare -i blx_size=0
23 declare -i remain_size=0
24
25 blx_size=$((`stat -c %s $1`))
26 if [ $blx_size -gt $blx_bin_limit ]; then
27 echo "Error: ($1) too big. $blx_size > $blx_bin_limit"
28 exit 1
29 fi
30
31 #fix bl2 to 56KB with zero
32 remain_size=$((blx_bin_limit - blx_size))
33 dd if=/dev/zero of=$2 bs=1 count=$remain_size
34 cat $1 $2 > $3 2> /dev/zero && rm $2
35
36 blx_size=$((`stat -c %s $4`))
37 if [ "$blx_size" -gt "$blx_acs_limit" ]; then
38 echo "Error: ($4) too big. $blx_size > $blx_acs_limit"
39 exit 1
40 fi
41 #fix acs to 4KB with zero
42 remain_size=$((blx_acs_limit - blx_size))
43 dd if=/dev/zero of=$2 bs=1 count=$remain_size
44 cat $4 $2 > $5 2> /dev/zero && rm $2
45
46 #combine bl2.bin and acs.bin to bl2_new.bin
47 cat $3 $5 > $6 2> /dev/zero && rm $3 $5
48
49}
50
51function encrypt() {
52 local ret=0
53 $ENCRYPT_TOOL $@
54 ret=$?
55 if [ 0 -ne "$ret" ]; then
56 echo "Encrypt error: $ret"
57 exit 1
58 fi
59}
60#$1 mcuboot output directory
61function encrypt_bootloader() {
62 encrypt --bl3sig --input $BL31_IMG --output $BL31_IMG.enc --level v3 --type bl31
63 encrypt --bl3sig --input $BL33_BIN --output $BL33_BIN.enc --level v3 --type bl33
64 encrypt --bl2sig --input $BL2_NEW_BIN --output $BL2_ENCRYPT_BIN && rm $BL2_NEW_BIN
65 encrypt --bootmk --output $1/mcuboot.bin \
66 --bl2 $BL2_ENCRYPT_BIN \
67 --bl31 $BL31_IMG.enc \
68 --bl33 $BL33_BIN.enc --level v3 \
69 --ddrfw1 $DDRFW_1 \
70 --ddrfw2 $DDRFW_2 \
71 --ddrfw3 $DDRFW_3 \
72 --ddrfw4 $DDRFW_4 \
73 --ddrfw5 $DDRFW_5 \
74 && rm $BL2_ENCRYPT_BIN $BL31_IMG.enc $BL33_BIN.enc
75}
76
77#$1 soc
78#$2 product
79#$3 input bl33.bin's and output mcuboot.bin's directory: output/arch-board-product/images/
80function package_mcuboot() {
81 #define firmware parameters
82 FIRMWARE_DIR=$MAIN_DIR/products/$2/build/$1
83
84 ACS=$FIRMWARE_DIR/acs.bin
85 BL2_BIN=$FIRMWARE_DIR/bl2.bin
86 BL2_NEW_BIN=$FIRMWARE_DIR/bl2_new.bin
87 BL2_ENCRYPT_BIN=$FIRMWARE_DIR/bl2.n.bin.sig
88 BL31_BIN=$FIRMWARE_DIR/bl31.bin
89 BL31_IMG=$FIRMWARE_DIR/bl31.img
90 BL33_BIN=$3/bl33.bin
91 DDRFW_1=$FIRMWARE_DIR/ddr4_1d.fw
92 DDRFW_2=$FIRMWARE_DIR/ddr4_2d.fw
93 DDRFW_3=$FIRMWARE_DIR/ddr3_1d.fw
94 DDRFW_4=$FIRMWARE_DIR/piei.fw
95 DDRFW_5=$FIRMWARE_DIR/aml_ddr.fw
96 ENCRYPT_TOOL=$FIRMWARE_DIR/aml_encrypt_a1
97
98 fix_bl2 \
99 $BL2_BIN \
100 $FIRMWARE_DIR/bl2_tmp.bin \
101 $FIRMWARE_DIR/bl2_fixed.bin \
102 $ACS \
103 $FIRMWARE_DIR/acs_fix.bin \
104 $BL2_NEW_BIN
105 encrypt_bootloader $3
106}
107
108#$1 arch
109#$2 soc
110#$3 board
111#$4 product
112function compile_mcuboot() {
113 # target file path
114 export BUILD_MCUBOOT=1
115 MCUBOOT_ARCH=$1
116 MCUBOOT_SOC=$2
117 MCUBOOT_BOARD=$3
118 MCUBOOT_PRODUCT=$4
119 IMAGE_STORAGE_PATH=$5
120
121 MCUBOOT_OUTPUT_PATH=${MAIN_DIR}/output/$MCUBOOT_ARCH-$MCUBOOT_BOARD-$MCUBOOT_PRODUCT
122
123 MCUBOOT_IMAGE_PATH=${MCUBOOT_OUTPUT_PATH}/images
124 ORIGINAL_BINARY_FILE=${MCUBOOT_IMAGE_PATH}/bl33.bin
125 DEBUG_FILE_PREFIX=${MCUBOOT_OUTPUT_PATH}/${KERNEL}/${KERNEL}
126
127 # Clean up mcuboot compilation intermediate files
128 rm -rf $MCUBOOT_OUTPUT_PATH
129
130 # start compile flow
131 pushd $MAIN_DIR
132
133 source scripts/env.sh $MCUBOOT_ARCH $MCUBOOT_SOC $MCUBOOT_BOARD $MCUBOOT_PRODUCT
134
135 if [ "$BACKTRACE_ENABLE" = "1" ]; then
136 make backtrace
137 else
138 make
139 fi
140
141 if [ $? -ne 0 ]; then
142 echo "Compile MCUBoot failed:$?"
143 popd
144 exit 1
145 fi
146
147 if [ -f "$MCUBOOT_IMAGE_PATH/$KERNEL-signed.bin" ]; then
148 mv $MCUBOOT_IMAGE_PATH/$KERNEL-signed.bin $ORIGINAL_BINARY_FILE
149 else
150 echo "$MCUBOOT_OUTPUT_PATH/$KERNEL/$KERNEL.bin does not exist!"
151 popd
152 exit 1
153 fi
154
155 #package mcuboot
156 package_mcuboot $MCUBOOT_SOC $MCUBOOT_PRODUCT $MCUBOOT_IMAGE_PATH
157 test -f $MCUBOOT_IMAGE_PATH/mcuboot.bin && \
158 cp -av $MCUBOOT_IMAGE_PATH/mcuboot.* $IMAGE_STORAGE_PATH/
159 popd
160}