cat << 'EOTEOT' > generate-secureboot.sh
#!/bin/sh
# Script that generate almost everything to get secure boot HAB working
# Aurelien BOUIN
VERSION="1.0.0"
#1.0.0 : Initial version
[ -z "$CERTIFICATE_SERIAL_NUMBERS" ] && CERTIFICATE_SERIAL_NUMBERS='49630111'
[ -z "$PASS_PHRASE" ] && PASS_PHRASE='!CAPTINA!captina'
if [ $(echo $CERTIFICATE_SERIAL_NUMBERS | wc -c) -ne 9 ]
then
echo "$CERTIFICATE_SERIAL_NUMBERS must contains 8-digit"
exit 1
fi
[ -z "$BASE_DIR" ] && BASE_DIR=$PWD
if [ ! -f "$BASE_DIR/cst-3.3.0.tgz" ] ; then
echo "You are missing the tgz package cst-3.3.0.tgz at $BASE_DIR"
exit 1
fi
if [ ! -x "/usr/bin/hexdump" ] ; then
echo "You are missing hexdump in /usr/bin/"
exit 1
fi
cd $BASE_DIR
rm -rf release
tar xzf cst-3.3.0.tgz
cd $BASE_DIR/release/keys
# Create serial file with an 8-digit content : OpenSSL uses the contents of this file for the certificate serial numbers
echo "$CERTIFICATE_SERIAL_NUMBERS" > serial
# Create key_pass.txt file that contains your pass phrase that will protect the HAB code signing private keys
echo "$PASS_PHRASE" > key_pass.txt
echo "$PASS_PHRASE" >> key_pass.txt
# Create the signature keys
./hab4_pki_tree.sh -existing-ca n -use-ecc n -kl 4096 -duration 20 -num-srk 4 -srk-ca y
# Create the fuse table and binary to be flashed
cd $BASE_DIR/release/crts
../linux64/bin/srktool -h 4 -t SRK_1_2_3_4_table.bin -e SRK_1_2_3_4_fuse.bin -d sha256 -c ./SRK1_sha256_4096_65537_v3_ca_crt.pem,./SRK2_sha256_4096_65537_v3_ca_crt.pem,./SRK3_sha256_4096_65537_v3_ca_crt.pem,./SRK4_sha256_4096_65537_v3_ca_crt.pem -f 1
# The fuse table generated in the previous section is what needs to be flashed to the device.
cd $BASE_DIR/release/crts
OUTPUT_FUSE_COMMANDS="$BASE_DIR/release/crts/fuse_uboot_commands.sh"
rm -f ${OUTPUT_FUSE_COMMANDS}
echo "# This is what need to be executed on U-Boot prompt :"
LIST_OF_FUSES=$(hexdump -e '/4 "0x"' -e '/4 "%X""\n"' < SRK_1_2_3_4_fuse.bin)
PAGE=6
TABLE=0
for one in $LIST_OF_FUSES
do
echo "fuse prog -y $PAGE $TABLE $one" | tee -a ${OUTPUT_FUSE_COMMANDS}
TABLE=$(( TABLE + 1 ))
if [ $TABLE -eq 4 ]; then
TABLE=0
PAGE=7
fi
done
echo "# If No HAB Events Found!" | tee -a ${OUTPUT_FUSE_COMMANDS}
echo "#Close the device with :" | tee -a ${OUTPUT_FUSE_COMMANDS}
echo "#fuse prog 1 3 0x02000000" | tee -a ${OUTPUT_FUSE_COMMANDS}
[ -z "$OUTPUT_DIR" ] && OUTPUT_DIR=${BASE_DIR}/output
rm -rf ${OUTPUT_DIR}
cd $BASE_DIR/release/
mkdir -p ${OUTPUT_DIR}/crts
cp crts/fuse_uboot_commands.sh ${OUTPUT_DIR}/crts
cp crts/*.pem ${OUTPUT_DIR}/crts
cp crts/SRK_1_2_3_4* ${OUTPUT_DIR}/crts
mkdir -p ${OUTPUT_DIR}/keys
cp keys/serial ${OUTPUT_DIR}/keys
cp keys/key_pass.txt ${OUTPUT_DIR}/keys
cp keys/*.pem ${OUTPUT_DIR}/keys
mkdir -p ${OUTPUT_DIR}/tool
cp linux64/bin/cst ${OUTPUT_DIR}/tool/
echo "Output files here : ${OUTPUT_DIR}"
tree ${OUTPUT_DIR}
EOTEOT
chmod +x generate-secureboot.sh
# Using docker :
docker run --rm -it -v ${PWD}:/root ubuntu:14.04 bash