Simon Glass | 041bca5 | 2013-06-13 15:10:11 -0700 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | # |
| 3 | # Copyright (c) 2013, Google Inc. |
| 4 | # |
| 5 | # Simple Verified Boot Test Script |
| 6 | # |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 7 | # SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 041bca5 | 2013-06-13 15:10:11 -0700 | [diff] [blame] | 8 | |
| 9 | set -e |
| 10 | |
| 11 | # Run U-Boot and report the result |
| 12 | # Args: |
| 13 | # $1: Test message |
| 14 | run_uboot() { |
| 15 | echo -n "Test Verified Boot Run: $1: " |
| 16 | ${uboot} -d sandbox-u-boot.dtb >${tmp} -c ' |
| 17 | sb load host 0 100 test.fit; |
| 18 | fdt addr 100; |
| 19 | bootm 100; |
| 20 | reset' |
| 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 | |
| 31 | echo "Simple Verified Boot Test" |
| 32 | echo "=========================" |
| 33 | echo |
| 34 | echo "Please see doc/uImage.FIT/verified-boot.txt for more information" |
| 35 | echo |
| 36 | |
| 37 | err=0 |
| 38 | tmp=/tmp/vboot_test.$$ |
| 39 | |
| 40 | dir=$(dirname $0) |
| 41 | |
| 42 | if [ -z ${O} ]; then |
| 43 | O=. |
| 44 | fi |
| 45 | O=$(readlink -f ${O}) |
| 46 | |
| 47 | dtc="-I dts -O dtb -p 2000" |
| 48 | uboot="${O}/u-boot" |
| 49 | mkimage="${O}/tools/mkimage" |
Heiko Schocher | 29a23f9 | 2014-03-03 12:19:30 +0100 | [diff] [blame] | 50 | fit_check_sign="${O}/tools/fit_check_sign" |
Simon Glass | 041bca5 | 2013-06-13 15:10:11 -0700 | [diff] [blame] | 51 | keys="${dir}/dev-keys" |
| 52 | echo ${mkimage} -D "${dtc}" |
| 53 | |
| 54 | echo "Build keys" |
| 55 | mkdir -p ${keys} |
| 56 | |
| 57 | # Create an RSA key pair |
| 58 | openssl genrsa -F4 -out ${keys}/dev.key 2048 2>/dev/null |
| 59 | |
| 60 | # Create a certificate containing the public key |
| 61 | openssl req -batch -new -x509 -key ${keys}/dev.key -out ${keys}/dev.crt |
| 62 | |
| 63 | pushd ${dir} >/dev/null |
| 64 | |
Heiko Schocher | 646257d | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 65 | function do_test { |
| 66 | echo do $sha test |
| 67 | # Compile our device tree files for kernel and U-Boot |
| 68 | dtc -p 0x1000 sandbox-kernel.dts -O dtb -o sandbox-kernel.dtb |
| 69 | dtc -p 0x1000 sandbox-u-boot.dts -O dtb -o sandbox-u-boot.dtb |
Simon Glass | 041bca5 | 2013-06-13 15:10:11 -0700 | [diff] [blame] | 70 | |
Heiko Schocher | 646257d | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 71 | # Create a number kernel image with zeroes |
| 72 | head -c 5000 /dev/zero >test-kernel.bin |
Simon Glass | 041bca5 | 2013-06-13 15:10:11 -0700 | [diff] [blame] | 73 | |
Heiko Schocher | 646257d | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 74 | # Build the FIT, but don't sign anything yet |
| 75 | echo Build FIT with signed images |
| 76 | ${mkimage} -D "${dtc}" -f sign-images-$sha.its test.fit >${tmp} |
Simon Glass | 041bca5 | 2013-06-13 15:10:11 -0700 | [diff] [blame] | 77 | |
Heiko Schocher | 646257d | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 78 | run_uboot "unsigned signatures:" "dev-" |
Simon Glass | 041bca5 | 2013-06-13 15:10:11 -0700 | [diff] [blame] | 79 | |
Heiko Schocher | 646257d | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 80 | # Sign images with our dev keys |
| 81 | echo Sign images |
| 82 | ${mkimage} -D "${dtc}" -F -k dev-keys -K sandbox-u-boot.dtb \ |
| 83 | -r test.fit >${tmp} |
Simon Glass | 041bca5 | 2013-06-13 15:10:11 -0700 | [diff] [blame] | 84 | |
Heiko Schocher | 646257d | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 85 | run_uboot "signed images" "dev+" |
Simon Glass | 041bca5 | 2013-06-13 15:10:11 -0700 | [diff] [blame] | 86 | |
| 87 | |
Heiko Schocher | 646257d | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 88 | # Create a fresh .dtb without the public keys |
| 89 | dtc -p 0x1000 sandbox-u-boot.dts -O dtb -o sandbox-u-boot.dtb |
Simon Glass | 041bca5 | 2013-06-13 15:10:11 -0700 | [diff] [blame] | 90 | |
Heiko Schocher | 646257d | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 91 | echo Build FIT with signed configuration |
| 92 | ${mkimage} -D "${dtc}" -f sign-configs-$sha.its test.fit >${tmp} |
Simon Glass | 041bca5 | 2013-06-13 15:10:11 -0700 | [diff] [blame] | 93 | |
Heiko Schocher | 646257d | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 94 | run_uboot "unsigned config" $sha"+ OK" |
Simon Glass | 041bca5 | 2013-06-13 15:10:11 -0700 | [diff] [blame] | 95 | |
Heiko Schocher | 646257d | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 96 | # Sign images with our dev keys |
| 97 | echo Sign images |
| 98 | ${mkimage} -D "${dtc}" -F -k dev-keys -K sandbox-u-boot.dtb \ |
| 99 | -r test.fit >${tmp} |
Simon Glass | 041bca5 | 2013-06-13 15:10:11 -0700 | [diff] [blame] | 100 | |
Heiko Schocher | 646257d | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 101 | run_uboot "signed config" "dev+" |
Simon Glass | 041bca5 | 2013-06-13 15:10:11 -0700 | [diff] [blame] | 102 | |
Heiko Schocher | 29a23f9 | 2014-03-03 12:19:30 +0100 | [diff] [blame] | 103 | echo check signed config on the host |
| 104 | if ! ${fit_check_sign} -f test.fit -k sandbox-u-boot.dtb >${tmp}; then |
| 105 | echo |
| 106 | echo "Verified boot key check on host failed, output follows:" |
| 107 | cat ${tmp} |
| 108 | false |
| 109 | else |
| 110 | if ! grep -q "dev+" ${tmp}; then |
| 111 | echo |
| 112 | echo "Verified boot key check failed, output follows:" |
| 113 | cat ${tmp} |
| 114 | false |
| 115 | else |
| 116 | echo "OK" |
| 117 | fi |
| 118 | fi |
| 119 | |
| 120 | run_uboot "signed config" "dev+" |
| 121 | |
Heiko Schocher | 646257d | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 122 | # Increment the first byte of the signature, which should cause failure |
| 123 | sig=$(fdtget -t bx test.fit /configurations/conf@1/signature@1 value) |
| 124 | newbyte=$(printf %x $((0x${sig:0:2} + 1))) |
| 125 | sig="${newbyte} ${sig:2}" |
| 126 | fdtput -t bx test.fit /configurations/conf@1/signature@1 value ${sig} |
Simon Glass | 041bca5 | 2013-06-13 15:10:11 -0700 | [diff] [blame] | 127 | |
Heiko Schocher | 646257d | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 128 | run_uboot "signed config with bad hash" "Bad Data Hash" |
| 129 | } |
| 130 | |
| 131 | sha=sha1 |
| 132 | do_test |
| 133 | sha=sha256 |
| 134 | do_test |
Simon Glass | 041bca5 | 2013-06-13 15:10:11 -0700 | [diff] [blame] | 135 | |
| 136 | popd >/dev/null |
| 137 | |
| 138 | echo |
| 139 | if ${ok}; then |
| 140 | echo "Test passed" |
| 141 | else |
| 142 | echo "Test failed" |
| 143 | fi |