Will Drewry | d3c938b | 2010-07-03 13:32:26 -0500 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
David James | 0103b59 | 2012-06-06 11:31:52 -0700 | [diff] [blame] | 3 | # Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Will Drewry | d3c938b | 2010-07-03 13:32:26 -0500 | [diff] [blame] | 4 | # 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 Harring | aa13ea4 | 2012-03-15 18:31:03 -0700 | [diff] [blame] | 10 | SCRIPT_ROOT=$(dirname $(readlink -f "$0")) |
David James | 359d3e1 | 2012-07-10 13:09:48 -0700 | [diff] [blame^] | 11 | . "${SCRIPT_ROOT}/common.sh" || exit 1 |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 12 | |
| 13 | # Need to be inside the chroot to load chromeos-common.sh |
| 14 | assert_inside_chroot |
| 15 | |
| 16 | # Load functions and constants for chromeos-install |
David James | 359d3e1 | 2012-07-10 13:09:48 -0700 | [diff] [blame^] | 17 | . /usr/lib/installer/chromeos-common.sh || exit 1 |
Will Drewry | d3c938b | 2010-07-03 13:32:26 -0500 | [diff] [blame] | 18 | |
| 19 | get_default_board |
| 20 | |
| 21 | # Flags. |
| 22 | DEFINE_string arch "x86" \ |
| 23 | "The boot architecture: arm or x86. (Default: x86)" |
| 24 | # TODO(wad) once extlinux is dead, we can remove this. |
| 25 | DEFINE_boolean install_syslinux ${FLAGS_FALSE} \ |
| 26 | "Controls whether syslinux is run on 'to'. (Default: false)" |
| 27 | DEFINE_string from "/tmp/boot" \ |
| 28 | "Path the legacy bootloader templates are copied from. (Default /tmp/boot)" |
| 29 | DEFINE_string to "/tmp/esp.img" \ |
Kenneth Waters | e3049de | 2010-09-30 14:20:34 -0700 | [diff] [blame] | 30 | "Path to esp image (Default: /tmp/esp.img)" |
Will Drewry | 721d94f | 2010-07-16 14:39:45 -0500 | [diff] [blame] | 31 | DEFINE_integer to_offset 0 \ |
| 32 | "Offset in bytes into 'to' if it is a file (Default: 0)" |
| 33 | DEFINE_integer to_size -1 \ |
| 34 | "Size in bytes of 'to' to use if it is a file. -1 is ignored. (Default: -1)" |
Will Drewry | d3c938b | 2010-07-03 13:32:26 -0500 | [diff] [blame] | 35 | DEFINE_string vmlinuz "/tmp/vmlinuz" \ |
| 36 | "Path to the vmlinuz file to use (Default: /tmp/vmlinuz)" |
| 37 | # The kernel_partition and the kernel_cmdline each are used to supply |
| 38 | # verified boot configuration: dm="". |
| 39 | DEFINE_string kernel_partition "/tmp/vmlinuz.image" \ |
| 40 | "Path to the signed kernel image. (Default: /tmp/vmlinuz.image)" |
| 41 | DEFINE_string kernel_cmdline "" \ |
| 42 | "Kernel commandline if no kernel_partition given. (Default: '')" |
| 43 | DEFINE_string kernel_partition_offset "0" \ |
| 44 | "Offset to the kernel partition [KERN-A] (Default: 0)" |
| 45 | DEFINE_string kernel_partition_sectors "0" \ |
| 46 | "Kernel partition sectors (Default: 0)" |
| 47 | DEFINE_string usb_disk /dev/sdb3 \ |
Kenneth Waters | e3049de | 2010-09-30 14:20:34 -0700 | [diff] [blame] | 48 | "Path syslinux should use to do a usb boot. Default: /dev/sdb3" |
Will Drewry | d3c938b | 2010-07-03 13:32:26 -0500 | [diff] [blame] | 49 | |
| 50 | # Parse flags |
| 51 | FLAGS "$@" || exit 1 |
| 52 | eval set -- "${FLAGS_ARGV}" |
Brian Harring | 7f175a5 | 2012-03-02 05:37:00 -0800 | [diff] [blame] | 53 | switch_to_strict_mode |
Will Drewry | d3c938b | 2010-07-03 13:32:26 -0500 | [diff] [blame] | 54 | |
Elly Jones | dfd3694 | 2011-08-10 15:59:36 -0400 | [diff] [blame] | 55 | part_index_to_uuid() { |
| 56 | local image="$1" |
| 57 | local index="$2" |
| 58 | cgpt show -i "$index" -u "$image" |
| 59 | } |
| 60 | |
Will Drewry | d3c938b | 2010-07-03 13:32:26 -0500 | [diff] [blame] | 61 | # If not provided by chromeos-common.sh, this will update all of the |
| 62 | # boot loader files (both A and B) with the data pulled |
| 63 | # from the kernel_partition. The default boot target should |
| 64 | # be set when the rootfs is stuffed. |
| 65 | if ! type -p update_x86_bootloaders; then |
| 66 | update_x86_bootloaders() { |
Will Drewry | b910de8 | 2011-02-23 13:26:50 -0600 | [diff] [blame] | 67 | local old_root="$1" # e.g., /dev/sd%D%P or %U+1 |
Will Drewry | d3c938b | 2010-07-03 13:32:26 -0500 | [diff] [blame] | 68 | local kernel_cmdline="$2" |
| 69 | local esp_fs_dir="$3" |
| 70 | local template_dir="$4" |
Elly Jones | dfd3694 | 2011-08-10 15:59:36 -0400 | [diff] [blame] | 71 | local to="$5" |
Will Drewry | d3c938b | 2010-07-03 13:32:26 -0500 | [diff] [blame] | 72 | |
| 73 | # Pull out the dm="" values |
Will Drewry | 82780e5 | 2010-07-03 18:27:10 -0700 | [diff] [blame] | 74 | dm_table= |
| 75 | if echo "$kernel_cmdline" | grep -q 'dm="'; then |
| 76 | dm_table=$(echo "$kernel_cmdline" | sed -s 's/.*dm="\([^"]*\)".*/\1/') |
| 77 | fi |
Will Drewry | d3c938b | 2010-07-03 13:32:26 -0500 | [diff] [blame] | 78 | |
Arkaitz Ruiz Alvarez | e2f33fb | 2011-07-08 12:21:22 -0700 | [diff] [blame] | 79 | # Maintain cros_debug flag in developer and test images. |
| 80 | cros_flags="cros_legacy" |
| 81 | if echo "$kernel_cmdline" | grep -q 'cros_debug'; then |
| 82 | cros_flags="cros_legacy cros_debug" |
| 83 | fi |
| 84 | |
Elly Jones | dfd3694 | 2011-08-10 15:59:36 -0400 | [diff] [blame] | 85 | root_a_uuid="PARTUUID=$(part_index_to_uuid "$to" 3)" |
| 86 | root_b_uuid="PARTUUID=$(part_index_to_uuid "$to" 5)" |
| 87 | |
Will Drewry | d3c938b | 2010-07-03 13:32:26 -0500 | [diff] [blame] | 88 | # Rewrite grub table |
Elly Jones | dfd3694 | 2011-08-10 15:59:36 -0400 | [diff] [blame] | 89 | grub_dm_table_a=${dm_table//${old_root}/${root_a_uuid}} |
| 90 | grub_dm_table_b=${dm_table//${old_root}/${root_b_uuid}} |
Will Drewry | d3c938b | 2010-07-03 13:32:26 -0500 | [diff] [blame] | 91 | sed -e "s|DMTABLEA|${grub_dm_table_a}|g" \ |
| 92 | -e "s|DMTABLEB|${grub_dm_table_b}|g" \ |
Arkaitz Ruiz Alvarez | e2f33fb | 2011-07-08 12:21:22 -0700 | [diff] [blame] | 93 | -e "s|cros_legacy|${cros_flags}|g" \ |
Will Drewry | d3c938b | 2010-07-03 13:32:26 -0500 | [diff] [blame] | 94 | "${template_dir}"/efi/boot/grub.cfg | |
| 95 | sudo dd of="${esp_fs_dir}"/efi/boot/grub.cfg |
Elly Jones | dfd3694 | 2011-08-10 15:59:36 -0400 | [diff] [blame] | 96 | sed -e "s|/dev/\\\$linuxpartA|${root_a_uuid}|g" \ |
| 97 | -e "s|/dev/\\\$linuxpartB|${root_b_uuid}|g" \ |
| 98 | "${template_dir}"/efi/boot/grub.cfg | |
| 99 | sudo dd of="${esp_fs_dir}"/efi/boot/grub.cfg |
Will Drewry | d3c938b | 2010-07-03 13:32:26 -0500 | [diff] [blame] | 100 | |
| 101 | # Rewrite syslinux DM_TABLE |
Elly Jones | dfd3694 | 2011-08-10 15:59:36 -0400 | [diff] [blame] | 102 | syslinux_dm_table_usb=${dm_table//${old_root}/${root_a_uuid}} |
Will Drewry | d3c938b | 2010-07-03 13:32:26 -0500 | [diff] [blame] | 103 | sed -e "s|DMTABLEA|${syslinux_dm_table_usb}|g" \ |
Arkaitz Ruiz Alvarez | e2f33fb | 2011-07-08 12:21:22 -0700 | [diff] [blame] | 104 | -e "s|cros_legacy|${cros_flags}|g" \ |
Will Drewry | d3c938b | 2010-07-03 13:32:26 -0500 | [diff] [blame] | 105 | "${template_dir}"/syslinux/usb.A.cfg | |
| 106 | sudo dd of="${esp_fs_dir}"/syslinux/usb.A.cfg |
| 107 | |
Will Drewry | b910de8 | 2011-02-23 13:26:50 -0600 | [diff] [blame] | 108 | syslinux_dm_table_a=${dm_table//${old_root}/HDROOTA} |
Will Drewry | d3c938b | 2010-07-03 13:32:26 -0500 | [diff] [blame] | 109 | sed -e "s|DMTABLEA|${syslinux_dm_table_a}|g" \ |
Arkaitz Ruiz Alvarez | e2f33fb | 2011-07-08 12:21:22 -0700 | [diff] [blame] | 110 | -e "s|cros_legacy|${cros_flags}|g" \ |
Will Drewry | d3c938b | 2010-07-03 13:32:26 -0500 | [diff] [blame] | 111 | "${template_dir}"/syslinux/root.A.cfg | |
| 112 | sudo dd of="${esp_fs_dir}"/syslinux/root.A.cfg |
| 113 | |
Will Drewry | b910de8 | 2011-02-23 13:26:50 -0600 | [diff] [blame] | 114 | syslinux_dm_table_b=${dm_table//${old_root}/HDROOTB} |
Will Drewry | 78992a3 | 2010-07-21 14:02:20 -0500 | [diff] [blame] | 115 | sed -e "s|DMTABLEB|${syslinux_dm_table_b}|g" \ |
Arkaitz Ruiz Alvarez | e2f33fb | 2011-07-08 12:21:22 -0700 | [diff] [blame] | 116 | -e "s|cros_legacy|${cros_flags}|g" \ |
Will Drewry | d3c938b | 2010-07-03 13:32:26 -0500 | [diff] [blame] | 117 | "${template_dir}"/syslinux/root.B.cfg | |
| 118 | sudo dd of="${esp_fs_dir}"/syslinux/root.B.cfg |
| 119 | |
| 120 | # Copy the vmlinuz's into place for syslinux |
| 121 | sudo cp -f "${template_dir}"/vmlinuz "${esp_fs_dir}"/syslinux/vmlinuz.A |
| 122 | sudo cp -f "${template_dir}"/vmlinuz "${esp_fs_dir}"/syslinux/vmlinuz.B |
| 123 | |
| 124 | # The only work left for the installer is to pick the correct defaults |
Will Drewry | b910de8 | 2011-02-23 13:26:50 -0600 | [diff] [blame] | 125 | # and replace HDROOTA and HDROOTB with the correct /dev/sd%D%P/%U+1 |
Will Drewry | d3c938b | 2010-07-03 13:32:26 -0500 | [diff] [blame] | 126 | } |
| 127 | fi |
| 128 | |
| 129 | ESP_DEV= |
| 130 | if [[ ! -e "${FLAGS_to}" ]]; then |
| 131 | error "The ESP doesn't exist" |
| 132 | # This shouldn't happen. |
| 133 | info "Creating a new esp image at ${FLAGS_to}" anyway. |
| 134 | # Create EFI System Partition to boot stock EFI BIOS (but not ChromeOS EFI |
Kenneth Waters | e3049de | 2010-09-30 14:20:34 -0700 | [diff] [blame] | 135 | # BIOS). ARM uses this space to determine which partition is bootable. |
Will Drewry | d3c938b | 2010-07-03 13:32:26 -0500 | [diff] [blame] | 136 | # NOTE: The size argument for mkfs.vfat is in 1024-byte blocks. |
| 137 | # We'll hard-code it to 16M for now. |
| 138 | ESP_BLOCKS=16384 |
| 139 | /usr/sbin/mkfs.vfat -C "${FLAGS_to}" ${ESP_BLOCKS} |
David James | 8b9643f | 2011-07-21 22:11:11 -0700 | [diff] [blame] | 140 | ESP_DEV=$(sudo losetup --show -f "${FLAGS_to}") |
Will Drewry | 721d94f | 2010-07-16 14:39:45 -0500 | [diff] [blame] | 141 | if [ -z "${ESP_DEV}" ]; then |
| 142 | die "No free loop devices." |
| 143 | fi |
Will Drewry | d3c938b | 2010-07-03 13:32:26 -0500 | [diff] [blame] | 144 | else |
| 145 | if [[ -f "${FLAGS_to}" ]]; then |
Will Drewry | 721d94f | 2010-07-16 14:39:45 -0500 | [diff] [blame] | 146 | esp_offset="--offset ${FLAGS_to_offset}" |
| 147 | esp_size="--sizelimit ${FLAGS_to_size}" |
| 148 | if [ ${FLAGS_to_size} -lt 0 ]; then |
| 149 | esp_size= |
| 150 | fi |
David James | 8b9643f | 2011-07-21 22:11:11 -0700 | [diff] [blame] | 151 | ESP_DEV=$(sudo losetup --show -f ${esp_offset} ${esp_size} "${FLAGS_to}") |
| 152 | if [ -z "${ESP_DEV}" ]; then |
| 153 | die "No free loop devices." |
| 154 | fi |
Will Drewry | d3c938b | 2010-07-03 13:32:26 -0500 | [diff] [blame] | 155 | else |
| 156 | # If it is a block device or something else, try to mount it anyway. |
| 157 | ESP_DEV="${FLAGS_to}" |
| 158 | fi |
| 159 | fi |
| 160 | |
| 161 | ESP_FS_DIR=$(mktemp -d /tmp/esp.XXXXXX) |
| 162 | cleanup() { |
David James | c9ca3db | 2012-07-09 08:12:26 -0700 | [diff] [blame] | 163 | set +e |
| 164 | if ! sudo umount "${ESP_FS_DIR}"; then |
| 165 | # There is a race condition possible on some ubuntu setups |
| 166 | # with mounting and unmounting a device very quickly |
| 167 | # Doing a quick sleep/retry as a temporary workaround |
| 168 | warn "Initial unmount failed. Possibly crosbug.com/23443. Retrying" |
| 169 | sleep 5 |
| 170 | sudo umount "${ESP_FS_DIR}" |
Jonathan Kliegman | ae6baa2 | 2011-11-25 11:03:16 -0500 | [diff] [blame] | 171 | fi |
Will Drewry | d3c938b | 2010-07-03 13:32:26 -0500 | [diff] [blame] | 172 | if [[ -n "${ESP_DEV}" && -z "${ESP_DEV//\/dev\/loop*}" ]]; then |
David James | c9ca3db | 2012-07-09 08:12:26 -0700 | [diff] [blame] | 173 | sudo losetup -d "${ESP_DEV}" |
Will Drewry | d3c938b | 2010-07-03 13:32:26 -0500 | [diff] [blame] | 174 | fi |
David James | c9ca3db | 2012-07-09 08:12:26 -0700 | [diff] [blame] | 175 | rm -rf "${ESP_FS_DIR}" |
Will Drewry | d3c938b | 2010-07-03 13:32:26 -0500 | [diff] [blame] | 176 | } |
| 177 | trap cleanup EXIT |
| 178 | sudo mount "${ESP_DEV}" "${ESP_FS_DIR}" |
| 179 | |
Sonny Rao | 3163cf0 | 2011-10-17 16:20:28 -0700 | [diff] [blame] | 180 | if [[ "${FLAGS_arch}" = "x86" || "${FLAGS_arch}" = "amd64" ]]; then |
Will Drewry | d3c938b | 2010-07-03 13:32:26 -0500 | [diff] [blame] | 181 | # Populate the EFI bootloader configuration |
| 182 | sudo mkdir -p "${ESP_FS_DIR}/efi/boot" |
| 183 | sudo cp "${FLAGS_from}"/efi/boot/bootx64.efi \ |
| 184 | "${ESP_FS_DIR}/efi/boot/bootx64.efi" |
| 185 | sudo cp "${FLAGS_from}/efi/boot/grub.cfg" \ |
| 186 | "${ESP_FS_DIR}/efi/boot/grub.cfg" |
| 187 | |
| 188 | # Prepopulate the syslinux directories too and update for verified boot values |
| 189 | # after the rootfs work is done. |
| 190 | sudo mkdir -p "${ESP_FS_DIR}"/syslinux |
| 191 | sudo cp -r "${FLAGS_from}"/syslinux/. "${ESP_FS_DIR}"/syslinux |
| 192 | |
| 193 | # Stage both kernels with the only one we built. |
| 194 | sudo cp -f "${FLAGS_vmlinuz}" "${ESP_FS_DIR}"/syslinux/vmlinuz.A |
| 195 | sudo cp -f "${FLAGS_vmlinuz}" "${ESP_FS_DIR}"/syslinux/vmlinuz.B |
| 196 | |
| 197 | # Extract kernel flags |
| 198 | kernel_cfg= |
Will Drewry | b910de8 | 2011-02-23 13:26:50 -0600 | [diff] [blame] | 199 | old_root="%U+1" |
Will Drewry | d3c938b | 2010-07-03 13:32:26 -0500 | [diff] [blame] | 200 | if [[ -n "${FLAGS_kernel_cmdline}" ]]; then |
| 201 | info "Using supplied kernel_cmdline to update templates." |
| 202 | kernel_cfg="${FLAGS_kernel_cmdline}" |
| 203 | elif [[ -n "${FLAGS_kernel_partition}" ]]; then |
| 204 | info "Extracting the kernel command line from ${FLAGS_kernel_partition}" |
| 205 | kernel_cfg=$(dump_kernel_config "${FLAGS_kernel_partition}") |
| 206 | fi |
| 207 | update_x86_bootloaders "${old_root}" \ |
| 208 | "${kernel_cfg}" \ |
| 209 | "${ESP_FS_DIR}" \ |
Elly Jones | dfd3694 | 2011-08-10 15:59:36 -0400 | [diff] [blame] | 210 | "${FLAGS_from}" \ |
| 211 | "${FLAGS_to}" |
Will Drewry | d3c938b | 2010-07-03 13:32:26 -0500 | [diff] [blame] | 212 | |
| 213 | # Install the syslinux loader on the ESP image (part 12) so it is ready when |
| 214 | # we cut over from rootfs booting (extlinux). |
| 215 | if [[ ${FLAGS_install_syslinux} -eq ${FLAGS_TRUE} ]]; then |
| 216 | sudo umount "${ESP_FS_DIR}" |
Will Drewry | 721d94f | 2010-07-16 14:39:45 -0500 | [diff] [blame] | 217 | sudo syslinux -d /syslinux "${ESP_DEV}" |
David James | c9ca3db | 2012-07-09 08:12:26 -0700 | [diff] [blame] | 218 | # mount again for cleanup to free resource gracefully |
| 219 | sudo mount -o ro "${ESP_DEV}" "${ESP_FS_DIR}" |
Will Drewry | d3c938b | 2010-07-03 13:32:26 -0500 | [diff] [blame] | 220 | fi |
| 221 | elif [[ "${FLAGS_arch}" = "arm" ]]; then |
Kenneth Waters | eca7646 | 2010-08-18 11:17:01 -0700 | [diff] [blame] | 222 | # Copy u-boot script to ESP partition |
Kenneth Waters | cb39f99 | 2010-08-18 14:35:22 -0700 | [diff] [blame] | 223 | if [ -r "${FLAGS_from}/boot-A.scr.uimg" ]; then |
| 224 | sudo mkdir -p "${ESP_FS_DIR}/u-boot" |
| 225 | sudo cp "${FLAGS_from}/boot-A.scr.uimg" \ |
| 226 | "${ESP_FS_DIR}/u-boot/boot.scr.uimg" |
Olof Johansson | 296adf0 | 2011-12-22 15:05:26 -0800 | [diff] [blame] | 227 | sudo cp -f "${FLAGS_from}"/vmlinuz "${ESP_FS_DIR}"/vmlinuz.uimg.A |
| 228 | sudo cp -f "${FLAGS_from}"/zImage "${ESP_FS_DIR}"/vmlinuz.A |
Kenneth Waters | cb39f99 | 2010-08-18 14:35:22 -0700 | [diff] [blame] | 229 | fi |
Will Drewry | d3c938b | 2010-07-03 13:32:26 -0500 | [diff] [blame] | 230 | fi |
| 231 | |
| 232 | set +e |