1#!/usr/bin/env bash
2
3#1 is the Amlogic fip directory
4#2 is u-boot directory
5
6set -o errexit
7set -o pipefail
8set -o nounset
9
10set -o xtrace
11
12function fix_blx() {
13	#bl2 file size 41K, bl21 file size 3K (file size not equal runtime size)
14	#total 44K
15	#after encrypt process, bl2 add 4K header, cut off 4K tail
16
17	#bl30 limit 41K
18	#bl301 limit 12K
19	#bl2 limit 41K
20	#bl21 limit 3K, but encrypt tool need 48K bl2.bin, so fix to 7168byte.
21
22	declare blx_bin_limit=0
23	declare blx01_bin_limit=0
24	declare -i blx_size=0
25	declare -i zero_size=0
26
27	#$7:name flag
28	if [ "$7" = "bl30" ]; then
29		if [ -e ${FIPDIR}/lpddr3_1d.fw -a "$SOCFAMILY" = "g12a" ]; then
30			blx_bin_limit=47104   # VIM3L
31		else
32			blx_bin_limit=40960   # PD#132613 2016-10-31 update, 41984->40960
33		fi
34		blx01_bin_limit=13312 # PD#132613 2016-10-31 update, 12288->13312
35	elif [ "$7" = "bl2" ]; then
36	    if [ "$SOCFAMILY" = "g12a" -o "$SOCFAMILY" = "sm1" -o "$SOCFAMILY" = "g12b" ]; then
37		blx_bin_limit=57344
38		blx01_bin_limit=4096
39	    else
40		blx_bin_limit=41984
41		blx01_bin_limit=7168
42	    fi
43	else
44		echo "blx_fix name flag not supported!"
45		exit 1
46	fi
47
48	# blx_size: blx.bin size, zero_size: fill with zeros
49	blx_size=`du -b $1 | awk '{print int($1)}'`
50	zero_size=$blx_bin_limit-$blx_size
51	dd if=/dev/zero of=$2 bs=1 count=$zero_size
52	cat $1 $2 > $3
53	rm $2
54
55	blx_size=`du -b $4 | awk '{print int($1)}'`
56	zero_size=$blx01_bin_limit-$blx_size
57	dd if=/dev/zero of=$2 bs=1 count=$zero_size
58	cat $4 $2 > $5
59
60	cat $3 $5 > $6
61
62	rm $2
63}
64
65FIPDIR=${1}
66UBOOTBIN=${2:-u-boot.bin}
67
68source ${FIPDIR}/soc-var.sh
69
70TMP=$(mktemp -d)
71
72if [ "$SOCFAMILY" = "gxl" ]
73then
74
75    fix_blx ${FIPDIR}/bl30.bin ${TMP}/zero_tmp ${TMP}/bl30_zero.bin ${FIPDIR}/bl301.bin ${TMP}/bl301_zero.bin ${TMP}/bl30_new.bin bl30
76    /usr/bin/env python2 ${FIPDIR}/acs_tool.pyc ${FIPDIR}/bl2.bin ${TMP}/bl2_acs.bin ${FIPDIR}/acs.bin 0
77    fix_blx ${TMP}/bl2_acs.bin ${TMP}/zero_tmp ${TMP}/bl2_zero.bin ${FIPDIR}/bl21.bin ${TMP}/bl21_zero.bin ${TMP}/bl2_new.bin bl2
78    ${FIPDIR}/aml_encrypt --bl3enc --input ${TMP}/bl30_new.bin --output ${TMP}/bl30_new.bin.enc
79    ${FIPDIR}/aml_encrypt --bl3enc --input ${FIPDIR}/bl31.img --output ${TMP}/bl31.img.enc
80    ${FIPDIR}/aml_encrypt --bl3enc --input ${UBOOTBIN} --output ${TMP}/bl33.bin.enc
81    ${FIPDIR}/aml_encrypt --bl2sig --input ${TMP}/bl2_new.bin --output ${TMP}/bl2.n.bin.sig
82    ${FIPDIR}/aml_encrypt --bootmk --output ${TMP}/u-boot.bin \
83	     --bl2 ${TMP}/bl2.n.bin.sig \
84	     --bl30 ${TMP}/bl30_new.bin.enc \
85	     --bl31 ${TMP}/bl31.img.enc \
86	     --bl33 ${TMP}/bl33.bin.enc
87
88elif [ "$SOCFAMILY" = "axg" ]
89then
90    fix_blx ${FIPDIR}/bl30.bin ${TMP}/zero_tmp ${TMP}/bl30_zero.bin ${FIPDIR}/bl301.bin ${TMP}/bl301_zero.bin ${TMP}/bl30_new.bin bl30
91    /usr/bin/env python2 ${FIPDIR}/acs_tool.pyc ${FIPDIR}/bl2.bin ${TMP}/bl2_acs.bin ${FIPDIR}/acs.bin 0
92    fix_blx ${TMP}/bl2_acs.bin ${TMP}/zero_tmp ${TMP}/bl2_zero.bin ${FIPDIR}/bl21.bin ${TMP}/bl21_zero.bin ${TMP}/bl2_new.bin bl2
93    ${FIPDIR}/aml_encrypt --bl3sig --input ${TMP}/bl30_new.bin --output ${TMP}/bl30_new.bin.enc --level v3 --type bl30
94    ${FIPDIR}/aml_encrypt --bl3sig --input ${FIPDIR}/bl31.img --output ${TMP}/bl31.img.enc --level v3 --type bl31
95    ${FIPDIR}/aml_encrypt --bl3sig --input  ${UBOOTBIN} --output ${TMP}/bl33.bin.enc --level v3 --type bl33 --compress lz4
96    ${FIPDIR}/aml_encrypt --bl2sig --input ${TMP}/bl2_new.bin --output ${TMP}/bl2.n.bin.sig
97    ${FIPDIR}/aml_encrypt --bootmk --output ${TMP}/u-boot.bin \
98	     --bl2 ${TMP}/bl2.n.bin.sig \
99	     --bl30 ${TMP}/bl30_new.bin.enc \
100	     --bl31 ${TMP}/bl31.img.enc \
101	     --bl33 ${TMP}/bl33.bin.enc \
102	     --level v3
103
104elif [ "$SOCFAMILY" = "g12a" -o "$SOCFAMILY" = "sm1" -o "$SOCFAMILY" = "g12b" ]
105then
106    cp ${FIPDIR}/acs.bin ${TMP}/acs.bin
107    [ -e ${FIPDIR}/parse ] && ${FIPDIR}/parse ${TMP}/acs.bin
108    fix_blx ${FIPDIR}/bl30.bin ${TMP}/zero_tmp ${TMP}/bl30_zero.bin ${FIPDIR}/bl301.bin ${TMP}/bl301_zero.bin ${TMP}/bl30_new.bin bl30
109    fix_blx ${FIPDIR}/bl2.bin ${TMP}/zero_tmp ${TMP}/bl2_zero.bin ${TMP}/acs.bin ${TMP}/bl21_zero.bin ${TMP}/bl2_new.bin bl2
110    ${FIPDIR}/aml_encrypt --bl30sig --input ${TMP}/bl30_new.bin --output ${TMP}/bl30_new.bin.g12.enc --level v3
111    ${FIPDIR}/aml_encrypt --bl3sig  --input ${TMP}/bl30_new.bin.g12.enc --output ${TMP}/bl30_new.bin.enc --level v3 --type bl30
112    ${FIPDIR}/aml_encrypt --bl3sig  --input ${FIPDIR}/bl31.img --output ${TMP}/bl31.img.enc --level v3 --type bl31
113    ${FIPDIR}/aml_encrypt --bl3sig  --input ${UBOOTBIN} --compress lz4 --output ${TMP}/bl33.bin.enc --level v3 --type bl33
114    ${FIPDIR}/aml_encrypt --bl2sig  --input ${TMP}/bl2_new.bin --output ${TMP}/bl2.n.bin.sig
115    if [ -e ${FIPDIR}/lpddr3_1d.fw ]
116    then
117	    ${FIPDIR}/aml_encrypt --bootmk  --output ${TMP}/u-boot.bin \
118		     --bl2 ${TMP}/bl2.n.bin.sig \
119		     --bl30 ${TMP}/bl30_new.bin.enc \
120		     --bl31 ${TMP}/bl31.img.enc \
121		     --bl33 ${TMP}/bl33.bin.enc \
122		     --ddrfw1 ${FIPDIR}/ddr4_1d.fw \
123		     --ddrfw2 ${FIPDIR}/ddr4_2d.fw \
124		     --ddrfw3 ${FIPDIR}/ddr3_1d.fw \
125		     --ddrfw4 ${FIPDIR}/piei.fw \
126		     --ddrfw5 ${FIPDIR}/lpddr4_1d.fw \
127		     --ddrfw6 ${FIPDIR}/lpddr4_2d.fw \
128		     --ddrfw7 ${FIPDIR}/diag_lpddr4.fw \
129		     --ddrfw8 ${FIPDIR}/aml_ddr.fw \
130		     --ddrfw9 ${FIPDIR}/lpddr3_1d.fw \
131		     --level v3
132    else
133	    ${FIPDIR}/aml_encrypt --bootmk  --output ${TMP}/u-boot.bin \
134		     --bl2 ${TMP}/bl2.n.bin.sig \
135		     --bl30 ${TMP}/bl30_new.bin.enc \
136		     --bl31 ${TMP}/bl31.img.enc \
137		     --bl33 ${TMP}/bl33.bin.enc \
138		     --ddrfw1 ${FIPDIR}/ddr4_1d.fw \
139		     --ddrfw2 ${FIPDIR}/ddr4_2d.fw \
140		     --ddrfw3 ${FIPDIR}/ddr3_1d.fw \
141		     --ddrfw4 ${FIPDIR}/piei.fw \
142		     --ddrfw5 ${FIPDIR}/lpddr4_1d.fw \
143		     --ddrfw6 ${FIPDIR}/lpddr4_2d.fw \
144		     --ddrfw7 ${FIPDIR}/diag_lpddr4.fw \
145		     --ddrfw8 ${FIPDIR}/aml_ddr.fw \
146		     --level v3
147    fi
148else
149    echo "${SOCFAMILY} is not supported - should be [gxl, axg, g12a, sm1, g12b]"
150    exit 22
151fi
152
153TMP2="uboot-bins-$(date +%Y%m%d-%H%M%S)"
154mkdir $TMP2
155ln -sfn $TMP2 uboot-bins
156
157mv ${TMP}/u-boot.bin{,.sd.bin,.usb.bl2,.usb.tpl} ${TMP2}
158rm -r ${TMP}
159