Home | History | Annotate | Download | only in image_signing
      1 #!/bin/bash 
      2 
      3 # Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
      4 # Use of this source code is governed by a BSD-style license that can be
      5 # found in the LICENSE file.
      6 
      7 # Script to convert a recovery image into an SSD image. Changes are made in-
      8 # place.
      9 
     10 usage() {
     11   cat <<EOF
     12 Usage: $PROG <image> [--force] [--cgpt=/path/to/cgpt]
     13 
     14 In-place converts recovery <image> into an SSD image. With --force, does not
     15 ask for confirmation from the user. Use --cgpt= to specify cgpt binary location.
     16 
     17 EOF
     18   exit 1
     19 }
     20 
     21 if [ $# -lt 1 ] || [ $# -gt 3 ]; then
     22   usage
     23 else
     24   IMAGE=$1
     25   shift
     26 fi
     27 
     28 for arg in $*; do
     29   case "$arg" in
     30   --force)
     31     IS_FORCE=$arg
     32     ;;
     33   --cgpt=*)
     34     GPT=${arg#--cgpt=}
     35     ;;
     36   *)
     37     usage
     38     ;;
     39   esac
     40 done
     41 
     42 # Load common constants (and use GPT if set above) and variables.
     43 . "$(dirname "$0")/common_minimal.sh"
     44 
     45 type -P $GPT &>/dev/null ||
     46   { echo "cgpt tool must be in the path or specified via --cgpt"; exit 1; }
     47 
     48 # Abort on errors.
     49 set -e
     50 
     51 if [ "${IS_FORCE}" != "--force" ]; then
     52   echo "This will modify ${IMAGE} in-place and convert it into an SSD image."
     53   read -p "Are you sure you want to continue (y/N)?" SURE
     54   SURE="${SURE:0:1}"
     55   [ "${SURE}" != "y" ] && exit 1
     56 fi
     57 
     58 kerna_offset=$(partoffset ${IMAGE} 2)
     59 kernb_offset=$(partoffset ${IMAGE} 4)
     60 # Kernel partition sizes should be the same.
     61 kern_size=$(partsize ${IMAGE} 2)
     62 
     63 # Move Kernel B to Kernel A.
     64 kernb=$(make_temp_file)
     65 echo "Replacing Kernel partition A with Kernel partition B"
     66 extract_image_partition ${IMAGE} 4 ${kernb}
     67 replace_image_partition ${IMAGE} 2 ${kernb}
     68 
     69 # Overwrite the vblock.
     70 stateful_dir=$(make_temp_dir)
     71 tmp_vblock=$(make_temp_file)
     72 mount_image_partition_ro ${IMAGE} 1 ${stateful_dir}
     73 sudo cp ${stateful_dir}/vmlinuz_hd.vblock ${tmp_vblock}
     74 # Unmount before overwriting image to avoid sync issues.
     75 sudo umount ${stateful_dir}
     76 echo "Overwriting kernel partition A vblock with SSD vblock"
     77 sudo dd if=${tmp_vblock} of=${IMAGE} seek=${kerna_offset} bs=512 conv=notrunc
     78 
     79 # Zero out Kernel B partition.
     80 echo "Zeroing out Kernel partition B"
     81 sudo dd if=/dev/zero of=${IMAGE} seek=${kernb_offset} bs=512 count=${kern_size} conv=notrunc
     82 echo "${IMAGE} was converted to an SSD image."
     83