Bill Richardson | 4364a2e | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 1 | #!/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 Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 6 | |
| 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. |
| 11 | find_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 | |
| 24 | find_common_sh |
| 25 | . "${SCRIPT_ROOT}/common.sh" || (echo "Unable to load common.sh" && exit 1) |
| 26 | # --- END COMMON.SH BOILERPLATE --- |
Bill Richardson | 4364a2e | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 27 | |
| 28 | # Load functions and constants for chromeos-install |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 29 | [ -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 Richardson | 4364a2e | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 35 | |
| 36 | # Script must be run inside the chroot. |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 37 | restart_in_chroot_if_needed "$@" |
Bill Richardson | 4364a2e | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 38 | |
| 39 | get_default_board |
| 40 | |
| 41 | # Flags. |
| 42 | DEFINE_string arch "" \ |
| 43 | "The target architecture (\"arm\" or \"x86\")." |
| 44 | DEFINE_string board "$DEFAULT_BOARD" \ |
| 45 | "The board to build an image for." |
Zelidrag Hornung | 1d12c1a | 2010-06-02 10:20:29 -0700 | [diff] [blame] | 46 | DEFINE_integer rootfs_partition_size 1024 \ |
| 47 | "rootfs parition size in MBs." |
Bill Richardson | 4364a2e | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 48 | |
| 49 | # Usage. |
| 50 | FLAGS_HELP=$(cat <<EOF |
Bill Richardson | 8b3bd10 | 2010-04-06 15:00:10 -0700 | [diff] [blame] | 51 | |
Bill Richardson | 4364a2e | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 52 | Usage: $(basename $0) [flags] IMAGEDIR OUTDEV |
| 53 | |
| 54 | This takes the image components in IMAGEDIR and creates a bootable, |
| 55 | GPT-formatted image in OUTDEV. OUTDEV can be a file or block device. |
Bill Richardson | 8b3bd10 | 2010-04-06 15:00:10 -0700 | [diff] [blame] | 56 | |
Bill Richardson | 4364a2e | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 57 | EOF |
| 58 | ) |
| 59 | |
| 60 | # Parse command line. |
| 61 | FLAGS "$@" || exit 1 |
| 62 | eval set -- "${FLAGS_ARGV}" |
| 63 | |
| 64 | if [[ -z "$FLAGS_board" ]] ; then |
| 65 | error "--board is required." |
| 66 | exit 1 |
| 67 | fi |
| 68 | |
| 69 | if [[ -z "$1" || -z "$2" ]] ; then |
| 70 | flags_help |
| 71 | exit 1 |
| 72 | fi |
| 73 | IMAGEDIR="$1" |
| 74 | OUTDEV="$2" |
| 75 | |
| 76 | if [[ -n "$FLAGS_arch" ]]; then |
| 77 | ARCH=${FLAGS_arch} |
| 78 | else |
| 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 |
| 93 | fi |
| 94 | |
Bill Richardson | 4364a2e | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 95 | # 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. |
| 97 | set -e |
| 98 | # Die on uninitialized variables. |
| 99 | set -u |
| 100 | |
| 101 | # Check for missing parts. |
| 102 | ROOTFS_IMG="${IMAGEDIR}/rootfs.image" |
| 103 | if [[ ! -s ${ROOTFS_IMG} ]]; then |
| 104 | error "Can't find ${ROOTFS_IMG}" |
| 105 | exit 1 |
| 106 | fi |
| 107 | |
| 108 | KERNEL_IMG="${IMAGEDIR}/vmlinuz.image" |
| 109 | if [[ ! -s ${KERNEL_IMG} ]]; then |
| 110 | error "Can't find ${KERNEL_IMG}" |
| 111 | exit 1 |
| 112 | fi |
| 113 | |
| 114 | STATEFUL_IMG="${IMAGEDIR}/stateful_partition.image" |
Tan Gao | 40551cd | 2010-06-24 16:33:32 -0700 | [diff] [blame] | 115 | if [ ! -s ${STATEFUL_IMG} ]; then |
Bill Richardson | 4364a2e | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 116 | error "Can't find ${STATEFUL_IMG}" |
| 117 | exit 1 |
| 118 | fi |
| 119 | |
Bill Richardson | 8b3bd10 | 2010-04-06 15:00:10 -0700 | [diff] [blame] | 120 | ESP_IMG="${IMAGEDIR}/esp.image" |
Tan Gao | 40551cd | 2010-06-24 16:33:32 -0700 | [diff] [blame] | 121 | if [ ! -s ${ESP_IMG} ]; then |
Bill Richardson | 8b3bd10 | 2010-04-06 15:00:10 -0700 | [diff] [blame] | 122 | error "Can't find ${ESP_IMG}" |
| 123 | exit 1 |
| 124 | fi |
| 125 | |
Kenneth Waters | e3049de | 2010-09-30 14:20:34 -0700 | [diff] [blame] | 126 | # We'll need some code to put in the PMBR, for booting on legacy BIOS. |
Bill Richardson | 4364a2e | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 127 | if [[ "$ARCH" = "arm" ]]; then |
Jie Sun | 5ade331 | 2010-04-12 17:04:36 -0700 | [diff] [blame] | 128 | PMBRCODE=/dev/zero |
Bill Richardson | 4364a2e | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 129 | else |
| 130 | PMBRCODE=$(readlink -f /usr/share/syslinux/gptmbr.bin) |
| 131 | fi |
| 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 Gao | 0d5f948 | 2010-08-06 08:28:20 -0700 | [diff] [blame] | 135 | install_gpt $OUTDEV $(numsectors $ROOTFS_IMG) $(numsectors $STATEFUL_IMG) \ |
| 136 | $PMBRCODE $(numsectors $ESP_IMG) false $FLAGS_rootfs_partition_size |
Bill Richardson | 4364a2e | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 137 | |
Bill Richardson | 6dcea16 | 2010-03-31 19:20:24 -0700 | [diff] [blame] | 138 | # Emit helpful scripts for testers, etc. |
| 139 | ${SCRIPTS_DIR}/emit_gpt_scripts.sh "${OUTDEV}" "${IMAGEDIR}" |
| 140 | |
Antoine Labour | e9e585f | 2010-04-01 15:57:57 -0700 | [diff] [blame] | 141 | sudo= |
| 142 | if [ ! -w "$OUTDEV" ] ; then |
| 143 | # use sudo when writing to a block device. |
| 144 | sudo=sudo |
| 145 | fi |
| 146 | |
Bill Richardson | 4364a2e | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 147 | # Now populate the partitions. |
| 148 | echo "Copying stateful partition..." |
Antoine Labour | e9e585f | 2010-04-01 15:57:57 -0700 | [diff] [blame] | 149 | $sudo dd if=${STATEFUL_IMG} of=${OUTDEV} conv=notrunc bs=512 \ |
| 150 | seek=${START_STATEFUL} |
Bill Richardson | 4364a2e | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 151 | |
| 152 | echo "Copying kernel..." |
Antoine Labour | e9e585f | 2010-04-01 15:57:57 -0700 | [diff] [blame] | 153 | $sudo dd if=${KERNEL_IMG} of=${OUTDEV} conv=notrunc bs=512 seek=${START_KERN_A} |
| 154 | |
Bill Richardson | 4364a2e | 2010-03-30 14:17:34 -0700 | [diff] [blame] | 155 | echo "Copying rootfs..." |
Tan Gao | 6c84bea | 2010-05-20 13:46:34 -0700 | [diff] [blame] | 156 | $sudo dd if=${ROOTFS_IMG} of=${OUTDEV} conv=notrunc bs=512 \ |
| 157 | seek=${START_ROOTFS_A} |
| 158 | |
Bill Richardson | 8b3bd10 | 2010-04-06 15:00:10 -0700 | [diff] [blame] | 159 | echo "Copying EFI system partition..." |
Jie Sun | 6795867 | 2010-04-07 14:34:04 -0700 | [diff] [blame] | 160 | $sudo dd if=${ESP_IMG} of=${OUTDEV} conv=notrunc bs=512 seek=${START_ESP} |
Bill Richardson | 8b3bd10 | 2010-04-06 15:00:10 -0700 | [diff] [blame] | 161 | |
Bill Richardson | 6dcea16 | 2010-03-31 19:20:24 -0700 | [diff] [blame] | 162 | # Clean up temporary files. |
| 163 | if [[ -n "${MBR_IMG:-}" ]]; then |
| 164 | rm "${MBR_IMG}" |
| 165 | fi |