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