| OLD | NEW |
| 1 #!/bin/bash | 1 #!/bin/bash |
| 2 # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 2 # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 # A simple script to run SAFT unit tests and possibly start the SAFT. | 6 # A simple script to run SAFT unit tests and possibly start the SAFT. |
| 7 # | 7 # |
| 8 # The only expected optional command line parameter is the name of the file | 8 # The only expected optional command line parameter is the name of the file |
| 9 # containing the firmware image to test. If the parameter is not provided, | 9 # containing the firmware image to test. If the parameter is not provided, |
| 10 # SAFT is not started, just the unit tests get to run. | 10 # SAFT is not started, just the unit tests get to run. |
| 11 | 11 |
| 12 rm -rf /tmp/tmp*/var/.fw_test 2> /dev/null | 12 rm -rf /tmp/tmp*/var/.fw_test 2> /dev/null |
| 13 umount -d /tmp/tmp* 2> /dev/null | 13 umount -d /tmp/tmp* 2> /dev/null |
| 14 | 14 |
| 15 TMPD_PATTERN='/tmp/tmp_saft.XXXX' | 15 TMPD_PATTERN='/tmp/tmp_saft.XXXX' |
| 16 DEFAULT_FLASH_DEVICE="${FLASH_DEVICE:=sdb}" | 16 DEFAULT_FLASH_DEVICE="${FLASH_DEVICE:=sdb}" |
| 17 set -e | 17 set -e |
| 18 this_prog=$(realpath $0) | 18 this_prog=$(realpath $0) |
| 19 if [ -n "$1" ]; then | 19 if [ -n "$1" ]; then |
| 20 new_firmware=$(realpath $1) | 20 new_firmware=$(realpath $1) |
| 21 else | 21 else |
| 22 new_firmware='' | 22 new_firmware='' |
| 23 fi | 23 fi |
| 24 | 24 |
| 25 # This hack has been borrowed from src/scripts/common.sh |
| 26 enable_rw_mount() { |
| 27 local rootfs="$1" |
| 28 local ro_compat_offset=$((0x464 + 3)) # Set 'highest' byte |
| 29 printf '\000' | |
| 30 sudo dd of="$rootfs" seek=${ro_compat_offset} \ |
| 31 conv=notrunc count=1 bs=1 |
| 32 } |
| 33 |
| 25 on_removable_device() { | 34 on_removable_device() { |
| 26 # Check if the code is running off a removable device or not. | 35 # Check if the code is running off a removable device or not. |
| 27 # Print '1' or '0' respectively on the console. | 36 # Print '1' or '0' respectively on the console. |
| 28 rootd=$(df "${this_prog}" | grep '^/dev' | awk '{ print $1 }') | 37 rootd=$(df "${this_prog}" | grep '^/dev' | awk '{ print $1 }') |
| 29 if [ "${rootd}" == '/dev/root' ]; then | 38 if [ "${rootd}" == '/dev/root' ]; then |
| 30 rootd=$(rootdev -s) | 39 rootd=$(rootdev -s) |
| 31 fi | 40 fi |
| 32 blockd=$(echo "${rootd}" | sed 's|.*/\([^/0-9]\+\)[0-9]\+|\1|') | 41 blockd=$(echo "${rootd}" | sed 's|.*/\([^/0-9]\+\)[0-9]\+|\1|') |
| 33 removable=$(cat /sys/block/"${blockd}"/removable) | 42 removable=$(cat /sys/block/"${blockd}"/removable) |
| 34 echo "${removable}" | 43 echo "${removable}" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 49 | 58 |
| 50 # Determine userland partition on the removable device. | 59 # Determine userland partition on the removable device. |
| 51 dev_num=$(cgpt show /dev/"${flash_device}" | \ | 60 dev_num=$(cgpt show /dev/"${flash_device}" | \ |
| 52 grep '"ROOT-A"' | awk '{ print $3 }') | 61 grep '"ROOT-A"' | awk '{ print $3 }') |
| 53 if [ -z "${dev_num}" ]; then | 62 if [ -z "${dev_num}" ]; then |
| 54 echo "/dev/${flash_device} does not contain a valid file system" | 63 echo "/dev/${flash_device} does not contain a valid file system" |
| 55 exit 1 | 64 exit 1 |
| 56 fi | 65 fi |
| 57 flash_root_partition="/dev/${flash_device}${dev_num}" | 66 flash_root_partition="/dev/${flash_device}${dev_num}" |
| 58 | 67 |
| 68 enable_rw_mount "${flash_root_partition}" |
| 69 |
| 59 # Find its mountpoint, or mount it if not yet mounted. | 70 # Find its mountpoint, or mount it if not yet mounted. |
| 60 mp=$(mount | grep "${flash_root_partition}" | awk '{print $3}') | 71 mp=$(mount | grep "${flash_root_partition}" | awk '{print $3}') |
| 61 if [ "${mp}" == "" ]; then | 72 if [ "${mp}" == "" ]; then |
| 62 mp=$(mktemp -d "${TMPD_PATTERN}") | 73 mp=$(mktemp -d "${TMPD_PATTERN}") |
| 63 mount "${flash_root_partition}" "${mp}" | 74 mount "${flash_root_partition}" "${mp}" |
| 64 else | 75 else |
| 65 mount -o remount,exec "${mp}" | 76 mount -o remount,exec "${mp}" |
| 66 fi | 77 fi |
| 67 | 78 |
| 68 # Copy the two directories SAFT test requires to the removable device to | 79 # Copy the two directories SAFT test requires to the removable device to |
| 69 # the same path we are in on the root device now. | 80 # the same path we are in on the root device now. |
| 70 my_root=$(realpath .) | 81 my_root=$(realpath .) |
| 71 dest="${mp}${my_root}" | 82 dest="${mp}${my_root}" |
| 72 if [ ! -d "${dest}" ]; then | 83 if [ ! -d "${dest}" ]; then |
| 73 mkdir -p "${dest}" | 84 mkdir -p "${dest}" |
| 74 fi | 85 fi |
| 75 cp -rp "${my_root}"/* "${dest}" | 86 cp -rp "${my_root}"/* "${dest}" |
| 76 | 87 |
| 77 # Start it running off the removable device. We don't expect to come back | 88 # Start it running off the removable device. We don't expect to come back |
| 78 # from this invocation in case this is a full mode SAFT. If this is a | 89 # from this invocation in case this is a full mode SAFT. If this is a |
| 79 # unittest run, some post processing can be added after unit tests return. | 90 # unittest run, some post processing can be added after unit tests return. |
| 80 echo "starting as ${mp}${this_prog} ${new_firmware}" | 91 echo "starting as ${mp}${this_prog} ${new_firmware}" |
| 81 "${mp}${this_prog}" "${new_firmware}" | 92 "${mp}${this_prog}" "${new_firmware}" |
| 82 } | 93 } |
| 83 | 94 |
| 84 configure_gpt_settings() { | 95 configure_gpt_settings() { |
| 85 # Let's keep it simple for now, partition 2 is the one to boot. | 96 # Let's keep it simple for now, partition 2 is the one to boot. |
| 86 cgpt add -i 2 -T 5 -P 9 /dev/sda | 97 cgpt add -i 2 -T 5 -P 9 -S 1 /dev/sda |
| 87 cgpt add -i 4 -T 5 -P 5 /dev/sda | 98 cgpt add -i 4 -T 5 -P 5 -S 1 /dev/sda |
| 88 } | 99 } |
| 89 | 100 |
| 90 run_tests() { | 101 run_tests() { |
| 91 ./test_chromeos_interface.py | 102 ./test_chromeos_interface.py |
| 92 ./test_flashrom_handler.py "${new_firmware}" | 103 ./test_flashrom_handler.py "${new_firmware}" |
| 93 ./test_saft_utility.py | 104 ./test_saft_utility.py |
| 94 ./test_kernel_handler.py | 105 ./test_kernel_handler.py |
| 95 ./test_cgpt_handler.py | 106 ./test_cgpt_handler.py |
| 107 ./test_tpm_handler.py |
| 96 | 108 |
| 97 if [ -n "${new_firmware}" ]; then | 109 if [ -n "${new_firmware}" ]; then |
| 98 configure_gpt_settings | 110 configure_gpt_settings |
| 99 ./saft_utility.py --ima="${new_firmware}" | 111 ./saft_utility.py --ima="${new_firmware}" |
| 100 fi | 112 fi |
| 101 } | 113 } |
| 102 | 114 |
| 103 check_and_set_saft_environment() { | 115 check_and_set_saft_environment() { |
| 104 # Does the other side contain a valid kernel? | 116 # Does the other side contain a valid kernel? |
| 105 tmpd=$(mktemp -d "${TMPD_PATTERN}") | 117 tmpd=$(mktemp -d "${TMPD_PATTERN}") |
| (...skipping 15 matching lines...) Expand all Loading... |
| 121 | 133 |
| 122 # Is flash device kernel configured to run with verified root fs? | 134 # Is flash device kernel configured to run with verified root fs? |
| 123 flash_kernel_dev="/dev/${DEFAULT_FLASH_DEVICE}2" | 135 flash_kernel_dev="/dev/${DEFAULT_FLASH_DEVICE}2" |
| 124 dd if="${flash_kernel_dev}" of="${other_kern_file}" | 136 dd if="${flash_kernel_dev}" of="${other_kern_file}" |
| 125 cmd_line=$(vbutil_kernel --verify "${other_kern_file}" --verbose | tail -1) | 137 cmd_line=$(vbutil_kernel --verify "${other_kern_file}" --verbose | tail -1) |
| 126 | 138 |
| 127 if echo $cmd_line | grep -q 'root=/dev/dm'; then | 139 if echo $cmd_line | grep -q 'root=/dev/dm'; then |
| 128 echo 'Disabling rootfs verification on the flash device kernel' | 140 echo 'Disabling rootfs verification on the flash device kernel' |
| 129 new_cmd_line_file="${tmpd}/cmdline" | 141 new_cmd_line_file="${tmpd}/cmdline" |
| 130 echo {$cmd_line} | sed ' | 142 echo {$cmd_line} | sed ' |
| 143 s/ ro / rw / |
| 131 s/dm_verity[^ ]\+//g | 144 s/dm_verity[^ ]\+//g |
| 132 s|verity /dev/sd%D%P /dev/sd%D%P || | 145 s|verity /dev/sd%D%P /dev/sd%D%P || |
| 133 s| root=/dev/dm-0 | root=/dev/sd%D%P | | 146 s| root=/dev/dm-0 | root=/dev/sd%D%P | |
| 134 s/dm="[^"]\+" //' > "${new_cmd_line_file}" | 147 s/dm="[^"]\+" //' > "${new_cmd_line_file}" |
| 135 vbutil_kernel --repack "${other_kern_file}.new" \ | 148 vbutil_kernel --repack "${other_kern_file}.new" \ |
| 136 --config "${new_cmd_line_file}" \ | 149 --config "${new_cmd_line_file}" \ |
| 137 --signprivate recovery_kernel_data_key.vbprivk \ | 150 --signprivate recovery_kernel_data_key.vbprivk \ |
| 138 --oldblob "${other_kern_file}" | 151 --oldblob "${other_kern_file}" |
| 139 dd if="${other_kern_file}.new" of="${flash_kernel_dev}" bs=4M | 152 dd if="${other_kern_file}.new" of="${flash_kernel_dev}" bs=4M |
| 140 fi | 153 fi |
| 141 } | 154 } |
| 142 | 155 |
| 143 cd $(dirname ${this_prog}) | 156 cd $(dirname ${this_prog}) |
| 144 if [ "$(on_removable_device)" == "0" ]; then | 157 if [ "$(on_removable_device)" == "0" ]; then |
| 145 check_and_set_saft_environment | 158 check_and_set_saft_environment |
| 146 move_to_removable_device | 159 move_to_removable_device |
| 147 exit 0 | 160 exit 0 |
| 148 fi | 161 fi |
| 149 | 162 |
| 150 run_tests | 163 run_tests |
| OLD | NEW |