blob: 3e2856ed1ff92c2198cea4d24ab54e8fcf6853ab [file] [log] [blame]
Simon Glass041bca52013-06-13 15:10:11 -07001#!/bin/sh
2#
3# Copyright (c) 2013, Google Inc.
4#
5# Simple Verified Boot Test Script
6#
Wolfgang Denk1a459662013-07-08 09:37:19 +02007# SPDX-License-Identifier: GPL-2.0+
Simon Glass041bca52013-06-13 15:10:11 -07008
9set -e
10
11# Run U-Boot and report the result
12# Args:
13# $1: Test message
14run_uboot() {
15 echo -n "Test Verified Boot Run: $1: "
16 ${uboot} -d sandbox-u-boot.dtb >${tmp} -c '
17sb load host 0 100 test.fit;
18fdt addr 100;
19bootm 100;
20reset'
21 if ! grep -q "$2" ${tmp}; then
22 echo
23 echo "Verified boot key check failed, output follows:"
24 cat ${tmp}
25 false
26 else
27 echo "OK"
28 fi
29}
30
31echo "Simple Verified Boot Test"
32echo "========================="
33echo
34echo "Please see doc/uImage.FIT/verified-boot.txt for more information"
35echo
36
37err=0
38tmp=/tmp/vboot_test.$$
39
40dir=$(dirname $0)
41
42if [ -z ${O} ]; then
43 O=.
44fi
45O=$(readlink -f ${O})
46
47dtc="-I dts -O dtb -p 2000"
48uboot="${O}/u-boot"
49mkimage="${O}/tools/mkimage"
50keys="${dir}/dev-keys"
51echo ${mkimage} -D "${dtc}"
52
53echo "Build keys"
54mkdir -p ${keys}
55
56# Create an RSA key pair
57openssl genrsa -F4 -out ${keys}/dev.key 2048 2>/dev/null
58
59# Create a certificate containing the public key
60openssl req -batch -new -x509 -key ${keys}/dev.key -out ${keys}/dev.crt
61
62pushd ${dir} >/dev/null
63
Heiko Schocher646257d2014-03-03 12:19:26 +010064function do_test {
65 echo do $sha test
66 # Compile our device tree files for kernel and U-Boot
67 dtc -p 0x1000 sandbox-kernel.dts -O dtb -o sandbox-kernel.dtb
68 dtc -p 0x1000 sandbox-u-boot.dts -O dtb -o sandbox-u-boot.dtb
Simon Glass041bca52013-06-13 15:10:11 -070069
Heiko Schocher646257d2014-03-03 12:19:26 +010070 # Create a number kernel image with zeroes
71 head -c 5000 /dev/zero >test-kernel.bin
Simon Glass041bca52013-06-13 15:10:11 -070072
Heiko Schocher646257d2014-03-03 12:19:26 +010073 # Build the FIT, but don't sign anything yet
74 echo Build FIT with signed images
75 ${mkimage} -D "${dtc}" -f sign-images-$sha.its test.fit >${tmp}
Simon Glass041bca52013-06-13 15:10:11 -070076
Heiko Schocher646257d2014-03-03 12:19:26 +010077 run_uboot "unsigned signatures:" "dev-"
Simon Glass041bca52013-06-13 15:10:11 -070078
Heiko Schocher646257d2014-03-03 12:19:26 +010079 # Sign images with our dev keys
80 echo Sign images
81 ${mkimage} -D "${dtc}" -F -k dev-keys -K sandbox-u-boot.dtb \
82 -r test.fit >${tmp}
Simon Glass041bca52013-06-13 15:10:11 -070083
Heiko Schocher646257d2014-03-03 12:19:26 +010084 run_uboot "signed images" "dev+"
Simon Glass041bca52013-06-13 15:10:11 -070085
86
Heiko Schocher646257d2014-03-03 12:19:26 +010087 # Create a fresh .dtb without the public keys
88 dtc -p 0x1000 sandbox-u-boot.dts -O dtb -o sandbox-u-boot.dtb
Simon Glass041bca52013-06-13 15:10:11 -070089
Heiko Schocher646257d2014-03-03 12:19:26 +010090 echo Build FIT with signed configuration
91 ${mkimage} -D "${dtc}" -f sign-configs-$sha.its test.fit >${tmp}
Simon Glass041bca52013-06-13 15:10:11 -070092
Heiko Schocher646257d2014-03-03 12:19:26 +010093 run_uboot "unsigned config" $sha"+ OK"
Simon Glass041bca52013-06-13 15:10:11 -070094
Heiko Schocher646257d2014-03-03 12:19:26 +010095 # Sign images with our dev keys
96 echo Sign images
97 ${mkimage} -D "${dtc}" -F -k dev-keys -K sandbox-u-boot.dtb \
98 -r test.fit >${tmp}
Simon Glass041bca52013-06-13 15:10:11 -070099
Heiko Schocher646257d2014-03-03 12:19:26 +0100100 run_uboot "signed config" "dev+"
Simon Glass041bca52013-06-13 15:10:11 -0700101
Heiko Schocher646257d2014-03-03 12:19:26 +0100102 # Increment the first byte of the signature, which should cause failure
103 sig=$(fdtget -t bx test.fit /configurations/conf@1/signature@1 value)
104 newbyte=$(printf %x $((0x${sig:0:2} + 1)))
105 sig="${newbyte} ${sig:2}"
106 fdtput -t bx test.fit /configurations/conf@1/signature@1 value ${sig}
Simon Glass041bca52013-06-13 15:10:11 -0700107
Heiko Schocher646257d2014-03-03 12:19:26 +0100108 run_uboot "signed config with bad hash" "Bad Data Hash"
109}
110
111sha=sha1
112do_test
113sha=sha256
114do_test
Simon Glass041bca52013-06-13 15:10:11 -0700115
116popd >/dev/null
117
118echo
119if ${ok}; then
120 echo "Test passed"
121else
122 echo "Test failed"
123fi