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 usable by factory.
8
9# TODO(gauravsh): crosbug.com/14790 (Merge this with
10#                 convert_recovery_to_ssd.sh)
11
12# Load common constants and variables.
13. "$(dirname "$0")/common_minimal.sh"
14
15usage() {
16  cat <<EOF
17Usage: $PROG <signed_recovery_image> <original_image_zip> <output_ssd_image>
18
19Converts <signed_recovery_image> into a full SSD image usable by factory. Uses
20stateful partition from SSD image <original_image_zip>.
21EOF
22}
23
24if [ $# -ne 3 ]; then
25  usage
26  exit 1
27fi
28
29type -P cgpt &>/dev/null ||
30  { echo "cgpt tool must be in the path"; exit 1; }
31
32# Abort on errors.
33set -e
34
35RECOVERY_IMAGE=$1
36IMAGE_ZIP=$2
37SSD_IMAGE=$3
38
39work_dir=$(make_temp_dir)
40
41echo "Extracting original SSD image."
42unzip -o $IMAGE_ZIP chromiumos_base_image.bin -d ${work_dir}
43
44mv ${work_dir}/chromiumos_base_image.bin ${SSD_IMAGE}
45
46kerna_offset=$(partoffset ${RECOVERY_IMAGE} 2)
47kernb_offset=$(partoffset ${RECOVERY_IMAGE} 4)
48# Kernel partition sizes should be the same.
49kern_size=$(partsize ${RECOVERY_IMAGE} 2)
50
51rootfs=$(make_temp_file)
52echo "Replacing RootFS on the SSD with that of the RECOVERY image"
53extract_image_partition ${RECOVERY_IMAGE} 3 ${rootfs}
54replace_image_partition ${SSD_IMAGE} 3 ${rootfs}
55
56kerna=$(make_temp_file)
57echo "Replacing KernelA on the SSD with that of the RECOVERY image"
58extract_image_partition ${RECOVERY_IMAGE} 4 ${kerna}
59replace_image_partition ${SSD_IMAGE} 2 ${kerna}
60
61# Overwrite the kernel vblock on the created SSD image.
62stateful_dir=$(make_temp_dir)
63tmp_vblock=$(make_temp_file)
64mount_image_partition_ro ${RECOVERY_IMAGE} 1 ${stateful_dir}
65sudo cp ${stateful_dir}/vmlinuz_hd.vblock ${tmp_vblock}
66echo "Overwriting kernel vblock with SSD kernel vblock"
67sudo dd if=${tmp_vblock} of=${SSD_IMAGE} seek=${kerna_offset} bs=512 \
68  conv=notrunc
69sudo umount ${stateful_dir}
70
71# Zero out Kernel B partition.
72echo "Zeroing out Kernel partition B"
73sudo dd if=/dev/zero of=${SSD_IMAGE} seek=${kernb_offset} bs=512 \
74  count=${kern_size} conv=notrunc
75echo "${RECOVERY_IMAGE} was converted to a factory SSD image: ${SSD_IMAGE}"
76