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