1#!/bin/sh 2# Copyright 2018 The Chromium OS Authors. All rights reserved. 3# Use of this source code is governed by a BSD-style license that can be 4# found in the LICENSE file. 5 6# gen_test_images.sh BOARD IMAGE 7# Generate test images from any premp/mp signed image. 8 9set -e 10 11BOARD=$1 12IMAGE=$(readlink -f $2) 13KEY=~/trunk/src/platform/ec/board/${BOARD}/dev_key.pem 14# Increment to different rollback versions 15ROLLBACK0=00000000 16ROLLBACK1=01000000 17ROLLBACK9=09000000 18 19rm -rf images 20mkdir images 21cd images 22 23# Use original image for some tests. 24cp $IMAGE ${BOARD}.bin 25 26# Generate dev key set 27futility create --desc="${BOARD} dev key" $KEY key 28 29# Pick up RO and RW version (only take up to 27 bytes, to leave an extra 30# 4 bytes for .dev/.rbX tag, and terminating \0. 31ro_version_offset=`dump_fmap $IMAGE RO_FRID | sed -n 's/area_offset: *//p'` 32ro_version=`dd if=$IMAGE bs=1 skip=$((ro_version_offset)) count=27` 33rw_version_offset=`dump_fmap $IMAGE RW_FWID | sed -n 's/area_offset: *//p'` 34rw_version=`dd if=$IMAGE bs=1 skip=$((rw_version_offset)) count=27` 35 36# Hack the version string 37cp $IMAGE ${BOARD}.dev 38printf "${ro_version}.dev" | dd of=${BOARD}.dev bs=1 seek=$((ro_version_offset)) count=32 conv=notrunc 39printf "${rw_version}.dev" | dd of=${BOARD}.dev bs=1 seek=$((rw_version_offset)) count=32 conv=notrunc 40 41# Resign the image with dev key 42echo "Generating image signed with dev keys:" 43~/trunk/src/platform/vboot_reference/scripts/image_signing/sign_official_build.sh accessory_rwsig ${BOARD}.dev . ${BOARD}.dev.out 44mv ${BOARD}.dev.out ${BOARD}.dev 45 46# Show signature 47futility show ${BOARD}.dev 48 49echo "Generating image with rollback = 0:" 50 51printf "Current rollback version: " 52rb_offset=`dump_fmap ${BOARD}.dev RW_RBVER | sed -n 's/area_offset: *//p'` 53dd if=${BOARD}.dev bs=1 skip=$((rb_offset)) count=4 2>/dev/null | xxd -l 4 -p 54 55cp ${BOARD}.dev ${BOARD}.dev.rb0 56# Decrement rollback to 0 57echo $ROLLBACK0 | xxd -g 4 -p -r | dd of=${BOARD}.dev.rb0 bs=1 seek=$((rb_offset)) count=4 conv=notrunc 58# Hack the version string 59printf "${rw_version}.rb0" | dd of=${BOARD}.dev.rb0 bs=1 seek=$((rw_version_offset)) count=32 conv=notrunc 60# Resign the image with dev key 61~/trunk/src/platform/vboot_reference/scripts/image_signing/sign_official_build.sh accessory_rwsig ${BOARD}.dev.rb0 . ${BOARD}.dev.rb0.out 62mv ${BOARD}.dev.rb0.out ${BOARD}.dev.rb0 63 64 65echo "Generating image with rollback = 1:" 66 67printf "Current rollback version: " 68rb_offset=`dump_fmap ${BOARD}.dev RW_RBVER | sed -n 's/area_offset: *//p'` 69dd if=${BOARD}.dev bs=1 skip=$((rb_offset)) count=4 2>/dev/null | xxd -l 4 -p 70 71cp ${BOARD}.dev ${BOARD}.dev.rb1 72# Increment rollback to 1 73echo $ROLLBACK1 | xxd -g 4 -p -r | dd of=${BOARD}.dev.rb1 bs=1 seek=$((rb_offset)) count=4 conv=notrunc 74# Hack the version string 75printf "${rw_version}.rb1" | dd of=${BOARD}.dev.rb1 bs=1 seek=$((rw_version_offset)) count=32 conv=notrunc 76# Resign the image with dev key 77~/trunk/src/platform/vboot_reference/scripts/image_signing/sign_official_build.sh accessory_rwsig ${BOARD}.dev.rb1 . ${BOARD}.dev.rb1.out 78mv ${BOARD}.dev.rb1.out ${BOARD}.dev.rb1 79 80echo "Generating image with rollback = 9:" 81 82printf "Current rollback version: " 83rb_offset=`dump_fmap ${BOARD}.dev RW_RBVER | sed -n 's/area_offset: *//p'` 84dd if=${BOARD}.dev bs=1 skip=$((rb_offset)) count=4 2>/dev/null | xxd -l 4 -p 85 86cp ${BOARD}.dev ${BOARD}.dev.rb9 87# Increment rollback to 9 88echo $ROLLBACK9 | xxd -g 4 -p -r | dd of=${BOARD}.dev.rb9 bs=1 seek=$((rb_offset)) count=4 conv=notrunc 89# Hack the version string 90printf "${rw_version}.rb9" | dd of=${BOARD}.dev.rb9 bs=1 seek=$((rw_version_offset)) count=32 conv=notrunc 91# Resign the image with dev key 92~/trunk/src/platform/vboot_reference/scripts/image_signing/sign_official_build.sh accessory_rwsig ${BOARD}.dev.rb9 . ${BOARD}.dev.rb9.out 93mv ${BOARD}.dev.rb9.out ${BOARD}.dev.rb9 94 95 96echo "Generating image with bits corrupted at start of image:" 97cp $IMAGE ${BOARD}_corrupt_first_byte.bin 98offset=`dump_fmap ${BOARD}_corrupt_first_byte.bin EC_RW | sed -n 's/area_offset: *//p'` 99dd if=/dev/random of=${BOARD}_corrupt_first_byte.bin bs=1 seek=$((offset+100)) count=1 conv=notrunc 100 101echo "Generating image with bits corrupted at end of image:" 102cp $IMAGE ${BOARD}_corrupt_last_byte.bin 103offset=`dump_fmap ${BOARD}_corrupt_last_byte.bin SIG_RW | sed -n 's/area_offset: *//p'` 104dd if=/dev/zero of=${BOARD}_corrupt_last_byte.bin bs=1 seek=$((offset-100)) count=1 conv=notrunc 105 106# hexdumps are always nice to have to do diffs 107for image in ${BOARD}.bin ${BOARD}_corrupt_first_byte.bin ${BOARD}_corrupt_last_byte.bin \ 108 ${BOARD}.dev ${BOARD}.dev.rb0 ${BOARD}.dev.rb1 ${BOARD}.dev.rb9; do 109 hexdump -C $image > $image.hex 110done 111 112