blob: 77387257d01d054a91fda6344cd4b3524ad75bfb [file] [log] [blame]
Will Drewryd3c938b2010-07-03 13:32:26 -05001#!/bin/bash
2
David James0103b592012-06-06 11:31:52 -07003# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Will Drewryd3c938b2010-07-03 13:32:26 -05004# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7# Helper script that generates the legacy/efi bootloader partitions.
8# It does not populate the templates, but can update a loop device.
9
Brian Harringaa13ea42012-03-15 18:31:03 -070010SCRIPT_ROOT=$(dirname $(readlink -f "$0"))
David James359d3e12012-07-10 13:09:48 -070011. "${SCRIPT_ROOT}/common.sh" || exit 1
Greg Spencer798d75f2011-02-01 22:04:49 -080012
13# Need to be inside the chroot to load chromeos-common.sh
14assert_inside_chroot
15
16# Load functions and constants for chromeos-install
David James359d3e12012-07-10 13:09:48 -070017. /usr/lib/installer/chromeos-common.sh || exit 1
Will Drewryd3c938b2010-07-03 13:32:26 -050018
Will Drewryd3c938b2010-07-03 13:32:26 -050019# Flags.
20DEFINE_string arch "x86" \
21 "The boot architecture: arm or x86. (Default: x86)"
22# TODO(wad) once extlinux is dead, we can remove this.
23DEFINE_boolean install_syslinux ${FLAGS_FALSE} \
24 "Controls whether syslinux is run on 'to'. (Default: false)"
25DEFINE_string from "/tmp/boot" \
26 "Path the legacy bootloader templates are copied from. (Default /tmp/boot)"
27DEFINE_string to "/tmp/esp.img" \
Kenneth Waterse3049de2010-09-30 14:20:34 -070028 "Path to esp image (Default: /tmp/esp.img)"
Will Drewry721d94f2010-07-16 14:39:45 -050029DEFINE_integer to_offset 0 \
30 "Offset in bytes into 'to' if it is a file (Default: 0)"
31DEFINE_integer to_size -1 \
32 "Size in bytes of 'to' to use if it is a file. -1 is ignored. (Default: -1)"
Will Drewryd3c938b2010-07-03 13:32:26 -050033DEFINE_string vmlinuz "/tmp/vmlinuz" \
34 "Path to the vmlinuz file to use (Default: /tmp/vmlinuz)"
35# The kernel_partition and the kernel_cmdline each are used to supply
36# verified boot configuration: dm="".
37DEFINE_string kernel_partition "/tmp/vmlinuz.image" \
38 "Path to the signed kernel image. (Default: /tmp/vmlinuz.image)"
39DEFINE_string kernel_cmdline "" \
40 "Kernel commandline if no kernel_partition given. (Default: '')"
41DEFINE_string kernel_partition_offset "0" \
42 "Offset to the kernel partition [KERN-A] (Default: 0)"
43DEFINE_string kernel_partition_sectors "0" \
44 "Kernel partition sectors (Default: 0)"
45DEFINE_string usb_disk /dev/sdb3 \
Kenneth Waterse3049de2010-09-30 14:20:34 -070046 "Path syslinux should use to do a usb boot. Default: /dev/sdb3"
Will Drewryd3c938b2010-07-03 13:32:26 -050047
48# Parse flags
49FLAGS "$@" || exit 1
50eval set -- "${FLAGS_ARGV}"
Brian Harring7f175a52012-03-02 05:37:00 -080051switch_to_strict_mode
Will Drewryd3c938b2010-07-03 13:32:26 -050052
Elly Jonesdfd36942011-08-10 15:59:36 -040053part_index_to_uuid() {
54 local image="$1"
55 local index="$2"
56 cgpt show -i "$index" -u "$image"
57}
58
Will Drewryd3c938b2010-07-03 13:32:26 -050059# If not provided by chromeos-common.sh, this will update all of the
60# boot loader files (both A and B) with the data pulled
61# from the kernel_partition. The default boot target should
62# be set when the rootfs is stuffed.
63if ! type -p update_x86_bootloaders; then
64 update_x86_bootloaders() {
Will Drewryb910de82011-02-23 13:26:50 -060065 local old_root="$1" # e.g., /dev/sd%D%P or %U+1
Will Drewryd3c938b2010-07-03 13:32:26 -050066 local kernel_cmdline="$2"
67 local esp_fs_dir="$3"
68 local template_dir="$4"
Elly Jonesdfd36942011-08-10 15:59:36 -040069 local to="$5"
Will Drewryd3c938b2010-07-03 13:32:26 -050070
71 # Pull out the dm="" values
Will Drewry82780e52010-07-03 18:27:10 -070072 dm_table=
73 if echo "$kernel_cmdline" | grep -q 'dm="'; then
74 dm_table=$(echo "$kernel_cmdline" | sed -s 's/.*dm="\([^"]*\)".*/\1/')
75 fi
Will Drewryd3c938b2010-07-03 13:32:26 -050076
Elly Jonesdfd36942011-08-10 15:59:36 -040077 root_a_uuid="PARTUUID=$(part_index_to_uuid "$to" 3)"
78 root_b_uuid="PARTUUID=$(part_index_to_uuid "$to" 5)"
79
Will Drewryd3c938b2010-07-03 13:32:26 -050080 # Rewrite grub table
Elly Jonesdfd36942011-08-10 15:59:36 -040081 grub_dm_table_a=${dm_table//${old_root}/${root_a_uuid}}
82 grub_dm_table_b=${dm_table//${old_root}/${root_b_uuid}}
Will Drewryd3c938b2010-07-03 13:32:26 -050083 sed -e "s|DMTABLEA|${grub_dm_table_a}|g" \
84 -e "s|DMTABLEB|${grub_dm_table_b}|g" \
Hung-Te Lin368df432013-03-20 16:13:07 +080085 -e "s|/dev/\\\$linuxpartA|${root_a_uuid}|g" \
Elly Jonesdfd36942011-08-10 15:59:36 -040086 -e "s|/dev/\\\$linuxpartB|${root_b_uuid}|g" \
87 "${template_dir}"/efi/boot/grub.cfg |
Mike Frysinger79ae5b42013-01-08 11:00:11 -050088 sudo dd of="${esp_fs_dir}"/efi/boot/grub.cfg status=none
Will Drewryd3c938b2010-07-03 13:32:26 -050089
Hung-Te Lin6d7164a2013-03-21 16:59:12 +080090 # Rewrite syslinux DM_TABLE for USB booting.
Elly Jonesdfd36942011-08-10 15:59:36 -040091 syslinux_dm_table_usb=${dm_table//${old_root}/${root_a_uuid}}
Will Drewryd3c938b2010-07-03 13:32:26 -050092 sed -e "s|DMTABLEA|${syslinux_dm_table_usb}|g" \
93 "${template_dir}"/syslinux/usb.A.cfg |
Mike Frysinger79ae5b42013-01-08 11:00:11 -050094 sudo dd of="${esp_fs_dir}"/syslinux/usb.A.cfg status=none
Will Drewryd3c938b2010-07-03 13:32:26 -050095
Hung-Te Lin6d7164a2013-03-21 16:59:12 +080096 # Note DMTABLE for root.A and root.B does not need to be updated because
97 # postinst will discard all changes in EFI partition and copy from
98 # rootfs:boot/syslinux/root.?.cfg again after installation or AU, because
99 # new rootfs will be apparently different.
Will Drewryd3c938b2010-07-03 13:32:26 -0500100
101 # Copy the vmlinuz's into place for syslinux
102 sudo cp -f "${template_dir}"/vmlinuz "${esp_fs_dir}"/syslinux/vmlinuz.A
103 sudo cp -f "${template_dir}"/vmlinuz "${esp_fs_dir}"/syslinux/vmlinuz.B
104
105 # The only work left for the installer is to pick the correct defaults
Will Drewryb910de82011-02-23 13:26:50 -0600106 # and replace HDROOTA and HDROOTB with the correct /dev/sd%D%P/%U+1
Will Drewryd3c938b2010-07-03 13:32:26 -0500107 }
108fi
109
110ESP_DEV=
111if [[ ! -e "${FLAGS_to}" ]]; then
112 error "The ESP doesn't exist"
113 # This shouldn't happen.
114 info "Creating a new esp image at ${FLAGS_to}" anyway.
115 # Create EFI System Partition to boot stock EFI BIOS (but not ChromeOS EFI
Kenneth Waterse3049de2010-09-30 14:20:34 -0700116 # BIOS). ARM uses this space to determine which partition is bootable.
Will Drewryd3c938b2010-07-03 13:32:26 -0500117 # NOTE: The size argument for mkfs.vfat is in 1024-byte blocks.
118 # We'll hard-code it to 16M for now.
119 ESP_BLOCKS=16384
120 /usr/sbin/mkfs.vfat -C "${FLAGS_to}" ${ESP_BLOCKS}
David James8b9643f2011-07-21 22:11:11 -0700121 ESP_DEV=$(sudo losetup --show -f "${FLAGS_to}")
Will Drewry721d94f2010-07-16 14:39:45 -0500122 if [ -z "${ESP_DEV}" ]; then
123 die "No free loop devices."
124 fi
Will Drewryd3c938b2010-07-03 13:32:26 -0500125else
126 if [[ -f "${FLAGS_to}" ]]; then
Will Drewry721d94f2010-07-16 14:39:45 -0500127 esp_offset="--offset ${FLAGS_to_offset}"
128 esp_size="--sizelimit ${FLAGS_to_size}"
129 if [ ${FLAGS_to_size} -lt 0 ]; then
130 esp_size=
131 fi
David James8b9643f2011-07-21 22:11:11 -0700132 ESP_DEV=$(sudo losetup --show -f ${esp_offset} ${esp_size} "${FLAGS_to}")
133 if [ -z "${ESP_DEV}" ]; then
134 die "No free loop devices."
135 fi
Will Drewryd3c938b2010-07-03 13:32:26 -0500136 else
137 # If it is a block device or something else, try to mount it anyway.
138 ESP_DEV="${FLAGS_to}"
139 fi
140fi
141
142ESP_FS_DIR=$(mktemp -d /tmp/esp.XXXXXX)
143cleanup() {
David Jamesc9ca3db2012-07-09 08:12:26 -0700144 set +e
Brian Harringece65e02012-08-30 13:42:21 -0700145 if ! safe_umount "${ESP_FS_DIR}"; then
David Jamesc9ca3db2012-07-09 08:12:26 -0700146 # There is a race condition possible on some ubuntu setups
147 # with mounting and unmounting a device very quickly
148 # Doing a quick sleep/retry as a temporary workaround
149 warn "Initial unmount failed. Possibly crosbug.com/23443. Retrying"
150 sleep 5
Brian Harringece65e02012-08-30 13:42:21 -0700151 safe_umount "${ESP_FS_DIR}"
Jonathan Kliegmanae6baa22011-11-25 11:03:16 -0500152 fi
Will Drewryd3c938b2010-07-03 13:32:26 -0500153 if [[ -n "${ESP_DEV}" && -z "${ESP_DEV//\/dev\/loop*}" ]]; then
David Jamesc9ca3db2012-07-09 08:12:26 -0700154 sudo losetup -d "${ESP_DEV}"
Will Drewryd3c938b2010-07-03 13:32:26 -0500155 fi
David Jamesc9ca3db2012-07-09 08:12:26 -0700156 rm -rf "${ESP_FS_DIR}"
Will Drewryd3c938b2010-07-03 13:32:26 -0500157}
158trap cleanup EXIT
159sudo mount "${ESP_DEV}" "${ESP_FS_DIR}"
160
Sonny Rao3163cf02011-10-17 16:20:28 -0700161if [[ "${FLAGS_arch}" = "x86" || "${FLAGS_arch}" = "amd64" ]]; then
Will Drewryd3c938b2010-07-03 13:32:26 -0500162 # Populate the EFI bootloader configuration
163 sudo mkdir -p "${ESP_FS_DIR}/efi/boot"
Josh Triplettf09c59b2013-04-25 11:08:13 -0700164 sudo cp -r "${FLAGS_from}"/efi/boot/. "${ESP_FS_DIR}"/efi/boot
Will Drewryd3c938b2010-07-03 13:32:26 -0500165
166 # Prepopulate the syslinux directories too and update for verified boot values
167 # after the rootfs work is done.
168 sudo mkdir -p "${ESP_FS_DIR}"/syslinux
169 sudo cp -r "${FLAGS_from}"/syslinux/. "${ESP_FS_DIR}"/syslinux
170
171 # Stage both kernels with the only one we built.
172 sudo cp -f "${FLAGS_vmlinuz}" "${ESP_FS_DIR}"/syslinux/vmlinuz.A
173 sudo cp -f "${FLAGS_vmlinuz}" "${ESP_FS_DIR}"/syslinux/vmlinuz.B
174
175 # Extract kernel flags
176 kernel_cfg=
Paul Taysom18673a82013-06-28 13:33:39 -0700177 old_root="%U+1"
Will Drewryd3c938b2010-07-03 13:32:26 -0500178 if [[ -n "${FLAGS_kernel_cmdline}" ]]; then
179 info "Using supplied kernel_cmdline to update templates."
180 kernel_cfg="${FLAGS_kernel_cmdline}"
181 elif [[ -n "${FLAGS_kernel_partition}" ]]; then
182 info "Extracting the kernel command line from ${FLAGS_kernel_partition}"
183 kernel_cfg=$(dump_kernel_config "${FLAGS_kernel_partition}")
184 fi
185 update_x86_bootloaders "${old_root}" \
186 "${kernel_cfg}" \
187 "${ESP_FS_DIR}" \
Elly Jonesdfd36942011-08-10 15:59:36 -0400188 "${FLAGS_from}" \
189 "${FLAGS_to}"
Will Drewryd3c938b2010-07-03 13:32:26 -0500190
191 # Install the syslinux loader on the ESP image (part 12) so it is ready when
192 # we cut over from rootfs booting (extlinux).
193 if [[ ${FLAGS_install_syslinux} -eq ${FLAGS_TRUE} ]]; then
Brian Harringece65e02012-08-30 13:42:21 -0700194 safe_umount "${ESP_FS_DIR}"
Will Drewry721d94f2010-07-16 14:39:45 -0500195 sudo syslinux -d /syslinux "${ESP_DEV}"
David Jamesc9ca3db2012-07-09 08:12:26 -0700196 # mount again for cleanup to free resource gracefully
197 sudo mount -o ro "${ESP_DEV}" "${ESP_FS_DIR}"
Will Drewryd3c938b2010-07-03 13:32:26 -0500198 fi
199elif [[ "${FLAGS_arch}" = "arm" ]]; then
Kenneth Waterseca76462010-08-18 11:17:01 -0700200 # Copy u-boot script to ESP partition
Kenneth Waterscb39f992010-08-18 14:35:22 -0700201 if [ -r "${FLAGS_from}/boot-A.scr.uimg" ]; then
202 sudo mkdir -p "${ESP_FS_DIR}/u-boot"
203 sudo cp "${FLAGS_from}/boot-A.scr.uimg" \
204 "${ESP_FS_DIR}/u-boot/boot.scr.uimg"
Olof Johansson296adf02011-12-22 15:05:26 -0800205 sudo cp -f "${FLAGS_from}"/vmlinuz "${ESP_FS_DIR}"/vmlinuz.uimg.A
206 sudo cp -f "${FLAGS_from}"/zImage "${ESP_FS_DIR}"/vmlinuz.A
Kenneth Waterscb39f992010-08-18 14:35:22 -0700207 fi
Will Drewryd3c938b2010-07-03 13:32:26 -0500208fi
209
210set +e