blob: 4be100c0f126d14e06dee9d1015783d1912f0c07 [file] [log] [blame]
Bill Richardson4364a2e2010-03-30 14:17:34 -07001#!/bin/bash
2#
3# Copyright (c) 2010 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.
Greg Spencer798d75f2011-02-01 22:04:49 -08006
7# --- BEGIN COMMON.SH BOILERPLATE ---
8# Load common CrOS utilities. Inside the chroot this file is installed in
9# /usr/lib/crosutils. Outside the chroot we find it relative to the script's
10# location.
11find_common_sh() {
12 local common_paths=(/usr/lib/crosutils $(dirname "$(readlink -f "$0")"))
13 local path
14
15 SCRIPT_ROOT=
16 for path in "${common_paths[@]}"; do
17 if [ -r "${path}/common.sh" ]; then
18 SCRIPT_ROOT=${path}
19 break
20 fi
21 done
22}
23
24find_common_sh
25. "${SCRIPT_ROOT}/common.sh" || (echo "Unable to load common.sh" && exit 1)
26# --- END COMMON.SH BOILERPLATE ---
Bill Richardson4364a2e2010-03-30 14:17:34 -070027
28# Load functions and constants for chromeos-install
Greg Spencer798d75f2011-02-01 22:04:49 -080029[ -f /usr/lib/installer/chromeos-common.sh ] && \
30 INSTALLER_ROOT=/usr/lib/installer || \
31 INSTALLER_ROOT=$(dirname "$(readlink -f "$0")")
32
33. "${INSTALLER_ROOT}/chromeos-common.sh" || \
34 die "Unable to load chromeos-common.sh"
Bill Richardson4364a2e2010-03-30 14:17:34 -070035
36# Script must be run inside the chroot.
Greg Spencer798d75f2011-02-01 22:04:49 -080037restart_in_chroot_if_needed "$@"
Bill Richardson4364a2e2010-03-30 14:17:34 -070038
39get_default_board
40
41# Flags.
42DEFINE_string arch "" \
43 "The target architecture (\"arm\" or \"x86\")."
44DEFINE_string board "$DEFAULT_BOARD" \
45 "The board to build an image for."
Zelidrag Hornung1d12c1a2010-06-02 10:20:29 -070046DEFINE_integer rootfs_partition_size 1024 \
47 "rootfs parition size in MBs."
Bill Richardson4364a2e2010-03-30 14:17:34 -070048
49# Usage.
50FLAGS_HELP=$(cat <<EOF
Bill Richardson8b3bd102010-04-06 15:00:10 -070051
Bill Richardson4364a2e2010-03-30 14:17:34 -070052Usage: $(basename $0) [flags] IMAGEDIR OUTDEV
53
54This takes the image components in IMAGEDIR and creates a bootable,
55GPT-formatted image in OUTDEV. OUTDEV can be a file or block device.
Bill Richardson8b3bd102010-04-06 15:00:10 -070056
Bill Richardson4364a2e2010-03-30 14:17:34 -070057EOF
58)
59
60# Parse command line.
61FLAGS "$@" || exit 1
62eval set -- "${FLAGS_ARGV}"
63
64if [[ -z "$FLAGS_board" ]] ; then
65 error "--board is required."
66 exit 1
67fi
68
69if [[ -z "$1" || -z "$2" ]] ; then
70 flags_help
71 exit 1
72fi
73IMAGEDIR="$1"
74OUTDEV="$2"
75
76if [[ -n "$FLAGS_arch" ]]; then
77 ARCH=${FLAGS_arch}
78else
79 # Figure out ARCH from the given toolchain.
80 # TODO: Move to common.sh as a function after scripts are switched over.
81 TC_ARCH=$(echo "$CHOST" | awk -F'-' '{ print $1 }')
82 case "$TC_ARCH" in
83 arm*)
84 ARCH="arm"
85 ;;
86 *86)
87 ARCH="x86"
88 ;;
89 *)
90 error "Unable to determine ARCH from toolchain: $CHOST"
91 exit 1
92 esac
93fi
94
Bill Richardson4364a2e2010-03-30 14:17:34 -070095# Only now can we die on error. shflags functions leak non-zero error codes,
96# so will die prematurely if 'set -e' is specified before now.
97set -e
98# Die on uninitialized variables.
99set -u
100
101# Check for missing parts.
102ROOTFS_IMG="${IMAGEDIR}/rootfs.image"
103if [[ ! -s ${ROOTFS_IMG} ]]; then
104 error "Can't find ${ROOTFS_IMG}"
105 exit 1
106fi
107
108KERNEL_IMG="${IMAGEDIR}/vmlinuz.image"
109if [[ ! -s ${KERNEL_IMG} ]]; then
110 error "Can't find ${KERNEL_IMG}"
111 exit 1
112fi
113
114STATEFUL_IMG="${IMAGEDIR}/stateful_partition.image"
Tan Gao40551cd2010-06-24 16:33:32 -0700115if [ ! -s ${STATEFUL_IMG} ]; then
Bill Richardson4364a2e2010-03-30 14:17:34 -0700116 error "Can't find ${STATEFUL_IMG}"
117 exit 1
118fi
119
Bill Richardson8b3bd102010-04-06 15:00:10 -0700120ESP_IMG="${IMAGEDIR}/esp.image"
Tan Gao40551cd2010-06-24 16:33:32 -0700121if [ ! -s ${ESP_IMG} ]; then
Bill Richardson8b3bd102010-04-06 15:00:10 -0700122 error "Can't find ${ESP_IMG}"
123 exit 1
124fi
125
Kenneth Waterse3049de2010-09-30 14:20:34 -0700126# We'll need some code to put in the PMBR, for booting on legacy BIOS.
Bill Richardson4364a2e2010-03-30 14:17:34 -0700127if [[ "$ARCH" = "arm" ]]; then
Jie Sun5ade3312010-04-12 17:04:36 -0700128 PMBRCODE=/dev/zero
Bill Richardson4364a2e2010-03-30 14:17:34 -0700129else
130 PMBRCODE=$(readlink -f /usr/share/syslinux/gptmbr.bin)
131fi
132
133# Create the GPT. This has the side-effect of setting some global vars
134# describing the partition table entries (see the comments in the source).
Tan Gao0d5f9482010-08-06 08:28:20 -0700135install_gpt $OUTDEV $(numsectors $ROOTFS_IMG) $(numsectors $STATEFUL_IMG) \
136 $PMBRCODE $(numsectors $ESP_IMG) false $FLAGS_rootfs_partition_size
Bill Richardson4364a2e2010-03-30 14:17:34 -0700137
Bill Richardson6dcea162010-03-31 19:20:24 -0700138# Emit helpful scripts for testers, etc.
139${SCRIPTS_DIR}/emit_gpt_scripts.sh "${OUTDEV}" "${IMAGEDIR}"
140
Antoine Laboure9e585f2010-04-01 15:57:57 -0700141sudo=
142if [ ! -w "$OUTDEV" ] ; then
143 # use sudo when writing to a block device.
144 sudo=sudo
145fi
146
Bill Richardson4364a2e2010-03-30 14:17:34 -0700147# Now populate the partitions.
148echo "Copying stateful partition..."
Antoine Laboure9e585f2010-04-01 15:57:57 -0700149$sudo dd if=${STATEFUL_IMG} of=${OUTDEV} conv=notrunc bs=512 \
150 seek=${START_STATEFUL}
Bill Richardson4364a2e2010-03-30 14:17:34 -0700151
152echo "Copying kernel..."
Antoine Laboure9e585f2010-04-01 15:57:57 -0700153$sudo dd if=${KERNEL_IMG} of=${OUTDEV} conv=notrunc bs=512 seek=${START_KERN_A}
154
Bill Richardson4364a2e2010-03-30 14:17:34 -0700155echo "Copying rootfs..."
Tan Gao6c84bea2010-05-20 13:46:34 -0700156$sudo dd if=${ROOTFS_IMG} of=${OUTDEV} conv=notrunc bs=512 \
157 seek=${START_ROOTFS_A}
158
Bill Richardson8b3bd102010-04-06 15:00:10 -0700159echo "Copying EFI system partition..."
Jie Sun67958672010-04-07 14:34:04 -0700160$sudo dd if=${ESP_IMG} of=${OUTDEV} conv=notrunc bs=512 seek=${START_ESP}
Bill Richardson8b3bd102010-04-06 15:00:10 -0700161
Bill Richardson6dcea162010-03-31 19:20:24 -0700162# Clean up temporary files.
163if [[ -n "${MBR_IMG:-}" ]]; then
164 rm "${MBR_IMG}"
165fi