2 # SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
4 # Script to add K3 specific x509 cetificate to a binary.
9 TEMP_X509=x509-temp.cert
17 gen_degen_template() {
18 cat << 'EOF' > degen-template.txt
24 modulus=INTEGER:0xDEGEN_MODULUS
31 coeff=INTEGER:0xDEGEN_COEFF
35 # Generate x509 Template
37 cat << 'EOF' > x509-template.txt
39 distinguished_name = req_distinguished_name
40 x509_extensions = v3_ca
42 dirstring_type = nobmp
44 [ req_distinguished_name ]
48 O = Texas Instruments Incorporated
54 basicConstraints = CA:true
55 1.3.6.1.4.1.294.1.1 = ASN1:SEQUENCE:boot_seq
56 1.3.6.1.4.1.294.1.2 = ASN1:SEQUENCE:image_integrity
57 1.3.6.1.4.1.294.1.3 = ASN1:SEQUENCE:swrv
58 # 1.3.6.1.4.1.294.1.4 = ASN1:SEQUENCE:encryption
59 1.3.6.1.4.1.294.1.8 = ASN1:SEQUENCE:debug
62 certType = INTEGER:TEST_CERT_TYPE
63 bootCore = INTEGER:TEST_BOOT_CORE
64 bootCoreOpts = INTEGER:TEST_BOOT_CORE_OPTS
65 destAddr = FORMAT:HEX,OCT:TEST_BOOT_ADDR
66 imageSize = INTEGER:TEST_IMAGE_LENGTH
69 shaType = OID:2.16.840.1.101.3.4.2.3
70 shaValue = FORMAT:HEX,OCT:TEST_IMAGE_SHA_VAL
76 # initalVector = FORMAT:HEX,OCT:TEST_IMAGE_ENC_IV
77 # randomString = FORMAT:HEX,OCT:TEST_IMAGE_ENC_RS
78 # iterationCnt = INTEGER:TEST_IMAGE_KEY_DERIVE_INDEX
79 # salt = FORMAT:HEX,OCT:TEST_IMAGE_KEY_DERIVE_SALT
82 debugUID = FORMAT:HEX,OCT:0000000000000000000000000000000000000000000000000000000000000000
83 debugType = INTEGER:TEST_DEBUG_TYPE
85 coreDbgSecEn = INTEGER:0
90 sed '/\ \ \ \ /s/://g' key.txt | awk '!/\ \ \ \ / {printf("\n%s\n", $0)}; /\ \ \ \ / {printf("%s", $0)}' | sed 's/\ \ \ \ //g' | awk "/$1:/{getline; print}"
94 # Generate a 4096 bit RSA Key
95 openssl genrsa -out key.pem 1024 >>/dev/null 2>&1
96 openssl rsa -in key.pem -text -out key.txt >>/dev/null 2>&1
97 DEGEN_MODULUS=$( parse_key 'modulus' )
98 DEGEN_P=$( parse_key 'prime1' )
99 DEGEN_Q=$( parse_key 'prime2' )
100 DEGEN_COEFF=$( parse_key 'coefficient' )
103 sed -e "s/DEGEN_MODULUS/$DEGEN_MODULUS/"\
104 -e "s/DEGEN_P/$DEGEN_P/" \
105 -e "s/DEGEN_Q/$DEGEN_Q/" \
106 -e "s/DEGEN_COEFF/$DEGEN_COEFF/" \
107 degen-template.txt > degenerateKey.txt
109 openssl asn1parse -genconf degenerateKey.txt -out degenerateKey.der >>/dev/null 2>&1
110 openssl rsa -in degenerateKey.der -inform DER -outform PEM -out $RAND_KEY >>/dev/null 2>&1
112 rm key.pem key.txt degen-template.txt degenerateKey.txt degenerateKey.der
115 declare -A options_help
121 for option in "${!options_help[@]}"
123 arg=`echo ${options_help[$option]}|cut -d ':' -f1`
124 if [ -n "$arg" ]; then
127 echo -n "[-$option$arg] "
131 for option in "${!options_help[@]}"
133 arg=`echo ${options_help[$option]}|cut -d ':' -f1`
134 txt=`echo ${options_help[$option]}|cut -d ':' -f2`
136 if [ -n "$arg" ]; then
140 echo -e " -$option$arg:$tb$txt"
143 echo "Examples of usage:-"
144 echo "# Example of signing the SYSFW binary with rsa degenerate key"
145 echo " $0 -c 0 -b ti-sci-firmware-am6x.bin -o sysfw.bin -l 0x40000"
146 echo "# Example of signing the SPL binary with rsa degenerate key"
147 echo " $0 -c 16 -b spl/u-boot-spl.bin -o tiboot3.bin -l 0x41c00000"
150 options_help[b]="bin_file:Bin file that needs to be signed"
151 options_help[k]="key_file:file with key inside it. If not provided script generates a rsa degenerate key."
152 options_help[o]="output_file:Name of the final output file. default to $OUTPUT"
153 options_help[c]="core_id:target core id on which the image would be running. Default to $BOOTCORE"
154 options_help[l]="loadaddr: Target load address of the binary in hex. Default to $LOADADDR"
155 options_help[d]="debug_type: Debug type, set to 4 to enable early JTAG. Default to $DEBUG_TYPE"
157 while getopts "b:k:o:c:l:d:h" opt
183 usage "Invalid Option '-$OPTARG'"
187 usage "Option '-$OPTARG' Needs an argument."
193 if [ "$#" -eq 0 ]; then
194 usage "Arguments missing"
198 if [ -z "$BIN" ]; then
199 usage "Bin file missing in arguments"
203 # Generate rsa degenerate key if user doesn't provide a key
204 if [ -z "$KEY" ]; then
208 if [ $BOOTCORE == 0 ]; then # BOOTCORE M3, loaded by ROM
210 elif [ $BOOTCORE == 16 ]; then # BOOTCORE R5, loaded by ROM
212 else # Non BOOTCORE, loaded by SYSFW
213 BOOTCORE_OPTS_VER=$(printf "%01x" 1)
214 # Add input args option for SET and CLR flags.
215 BOOTCORE_OPTS_SETFLAG=$(printf "%08x" 0)
216 BOOTCORE_OPTS_CLRFLAG=$(printf "%08x" 0x100) # Clear FLAG_ARMV8_AARCH32
217 BOOTCORE_OPTS="0x$BOOTCORE_OPTS_VER$BOOTCORE_OPTS_SETFLAG$BOOTCORE_OPTS_CLRFLAG"
218 # Set the cert type to zero.
219 # We are not using public/private key store now
220 CERTTYPE=$(printf "0x%08x" 0)
223 SHA_VAL=`openssl dgst -sha512 -hex $BIN | sed -e "s/^.*= //g"`
224 BIN_SIZE=`cat $BIN | wc -c`
225 ADDR=`printf "%08x" $LOADADDR`
228 #echo "Certificate being generated :"
229 #echo " LOADADDR = 0x$ADDR"
230 #echo " IMAGE_SIZE = $BIN_SIZE"
231 #echo " CERT_TYPE = $CERTTYPE"
232 #echo " DEBUG_TYPE = $DEBUG_TYPE"
233 sed -e "s/TEST_IMAGE_LENGTH/$BIN_SIZE/" \
234 -e "s/TEST_IMAGE_SHA_VAL/$SHA_VAL/" \
235 -e "s/TEST_CERT_TYPE/$CERTTYPE/" \
236 -e "s/TEST_BOOT_CORE_OPTS/$BOOTCORE_OPTS/" \
237 -e "s/TEST_BOOT_CORE/$BOOTCORE/" \
238 -e "s/TEST_BOOT_ADDR/$ADDR/" \
239 -e "s/TEST_DEBUG_TYPE/$DEBUG_TYPE/" \
240 x509-template.txt > $TEMP_X509
241 openssl req -new -x509 -key $KEY -nodes -outform DER -out $CERT -config $TEMP_X509 -sha512
246 cat $CERT $BIN > $OUTPUT
248 # Remove all intermediate files
249 rm $TEMP_X509 $CERT x509-template.txt
250 if [ "$KEY" == "$RAND_KEY" ]; then