Debian Codegraph @

util/nvidia/cbootimage/samples/sign.sh

raw symbols SCIP
1 #!/bin/bash
2 #
3 # Copyright (c) 2015-2016, NVIDIA CORPORATION. All rights reserved.
4 #
5 # This program is free software; you can redistribute it and/or modify it
6 # under the terms and conditions of the GNU General Public License,
7 # version 2, as published by the Free Software Foundation.
8 #
9 # This program is distributed in the hope it will be useful, but WITHOUT
10 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 # more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 #
17 # See file CREDITS for list of people who contributed to this
18 # project.
19 #
20
21 Usage ()
22 {
23 cat << EOF
24 Usage: ./sign.sh <soc> <boot_image> <rsa_priv_key>
25 Where,
26 soc: tegra124, tegra210
27 boot_image: image generated by cbootimage,
28 priv_key: rsa key file in .pem format.
29 EOF
30 exit 1;
31 }
32
33 set -e
34
35 soc=$1 # tegra124, tegra210
36 if [[ "${soc}" = tegra124 ]]; then
37 bl_block_offset=16384; # emmc: 16384, spi_flash: 32768: default: emmc
38 bct_signed_offset=1712;
39 bct_signed_length=6480;
40 elif [[ "${soc}" = tegra210 ]]; then
41 bl_block_offset=32768; # emmc: 16384, spi_flash: 32768: default: spi
42 bct_signed_offset=1296;
43 bct_signed_length=8944;
44 else
45 echo "Error: Invalid target device: soc = $soc";
46 Usage;
47 fi;
48 bct_length=$(($bct_signed_offset + $bct_signed_length));
49
50 # more error check
51 if [ $# -lt 3 ]; then
52 echo "Error: Missing parameter(s)";
53 Usage;
54 fi;
55
56 #
57 # In case to add more parameters in the future, we keep the last two as
58 # IMAGE_FILE and KEY_FILE
59 #
60 argv=($@);
61 IMAGE_FILE=${argv[$#-2]};
62 KEY_FILE=${argv[$#-1]};
63 TARGET_IMAGE=$IMAGE_FILE
64 CONFIG_FILE=config.tmp
65
66 CBOOTIMAGE=../src/cbootimage
67 BCT_DUMP=../src/bct_dump
68 OBJCOPY=objcopy
69 OPENSSL=openssl
70 DD=dd
71 RM=rm
72 MV=mv
73 XXD=xxd
74 CUT=cut
75
76 echo "Sign ${soc} ${IMAGE_FILE} with key ${KEY_FILE}"
77
78 echo "Get bl length "
79 BL_LENGTH=`$BCT_DUMP $IMAGE_FILE | grep "Bootloader\[0\].Length"\
80 | awk -F ' ' '{print $4}' | awk -F ';' '{print $1}'`
81
82 echo "Extract bootloader to $IMAGE_FILE.bl.tosig, length $BL_LENGTH"
83 $DD bs=1 skip=${bl_block_offset} if=$IMAGE_FILE of=$IMAGE_FILE.bl.tosig \
84 count=$BL_LENGTH
85
86 echo "Calculate rsa signature for bootloader and save to $IMAGE_FILE.bl.sig"
87 $OPENSSL dgst -sha256 -sigopt rsa_padding_mode:pss -sigopt rsa_pss_saltlen:-1 \
88 -sign $KEY_FILE -out $IMAGE_FILE.bl.sig $IMAGE_FILE.bl.tosig
89
90 echo "Update bootloader's rsa signature, aes hash and bct's aes hash"
91 echo "RsaPssSigBlFile = $IMAGE_FILE.bl.sig;" > $CONFIG_FILE
92 echo "RehashBl;" >> $CONFIG_FILE
93 $CBOOTIMAGE -s ${soc} -u $CONFIG_FILE $IMAGE_FILE $IMAGE_FILE.tmp
94
95 echo "Extract the part of bct which needs to be rsa signed"
96 $DD bs=1 if=$IMAGE_FILE.tmp of=$IMAGE_FILE.bct.tosig skip=${bct_signed_offset} \
97 count=${bct_signed_length}
98
99 echo "Calculate rsa signature for bct and save to $IMAGE_FILE.bct.sig"
100 $OPENSSL dgst -sha256 -sigopt rsa_padding_mode:pss -sigopt rsa_pss_saltlen:-1 \
101 -sign $KEY_FILE -out $IMAGE_FILE.bct.sig $IMAGE_FILE.bct.tosig
102
103 echo "Create public key modulus from key file $KEY_FILE and save to $KEY_FILE.mod"
104 $OPENSSL rsa -in $KEY_FILE -noout -modulus -out $KEY_FILE.mod
105 # remove prefix
106 $CUT -d= -f2 < $KEY_FILE.mod > $KEY_FILE.mod.tmp
107
108 # convert from hexdecimal to binary
109 $XXD -r -p -l 256 $KEY_FILE.mod.tmp $KEY_FILE.mod.bin
110
111 echo "Update bct's rsa signature and modulus"
112 echo "RsaPssSigBctFile = $IMAGE_FILE.bct.sig;" > $CONFIG_FILE
113 echo "RsaKeyModulusFile = $KEY_FILE.mod.bin;" >> $CONFIG_FILE
114 echo ""
115 $CBOOTIMAGE -s ${soc} -u $CONFIG_FILE $IMAGE_FILE.tmp $TARGET_IMAGE
116
117 echo ""
118 $DD bs=1 if=$TARGET_IMAGE of=${soc}.bct count=${bct_length}
119 echo ""
120 echo "Signed bct ${soc}.bct has been successfully generated!";
121
122 #echo "Get rid of all temporary files: *.sig, *.tosig, *.tmp, *.mod, *.mod.bin"
123 $RM -f *.sig *.tosig *.tmp *.mod *.mod.bin