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 |
Joshua Emele | e606672 | 2017-03-07 16:36:55 -0800 | [diff] [blame] | 12 | . "${BUILD_LIBRARY_DIR}/disk_layout_util.sh" || exit 1 |
Greg Spencer | 798d75f | 2011-02-01 22:04:49 -0800 | [diff] [blame] | 13 | |
| 14 | # Need to be inside the chroot to load chromeos-common.sh |
| 15 | assert_inside_chroot |
| 16 | |
| 17 | # Load functions and constants for chromeos-install |
Gwendal Grignou | 0f61849 | 2014-04-07 20:05:58 +0000 | [diff] [blame] | 18 | . /usr/share/misc/chromeos-common.sh || exit 1 |
Will Drewry | d3c938b | 2010-07-03 13:32:26 -0500 | [diff] [blame] | 19 | |
Will Drewry | d3c938b | 2010-07-03 13:32:26 -0500 | [diff] [blame] | 20 | # Flags. |
| 21 | DEFINE_string arch "x86" \ |
Ben Chan | d4ec205 | 2014-07-17 18:18:31 -0700 | [diff] [blame] | 22 | "The boot architecture: arm, mips, x86, or amd64 (Default: x86)" |
Evan Green | 550e57c | 2019-03-22 17:03:55 -0700 | [diff] [blame] | 23 | DEFINE_string board "${DEFAULT_BOARD}" \ |
| 24 | "Board we're building for." |
Joshua Emele | e606672 | 2017-03-07 16:36:55 -0800 | [diff] [blame] | 25 | DEFINE_string image_type "usb" \ |
| 26 | "Type of image we're building for." |
Will Drewry | d3c938b | 2010-07-03 13:32:26 -0500 | [diff] [blame] | 27 | # TODO(wad) once extlinux is dead, we can remove this. |
| 28 | DEFINE_boolean install_syslinux ${FLAGS_FALSE} \ |
| 29 | "Controls whether syslinux is run on 'to'. (Default: false)" |
| 30 | DEFINE_string from "/tmp/boot" \ |
| 31 | "Path the legacy bootloader templates are copied from. (Default /tmp/boot)" |
| 32 | DEFINE_string to "/tmp/esp.img" \ |
Kenneth Waters | e3049de | 2010-09-30 14:20:34 -0700 | [diff] [blame] | 33 | "Path to esp image (Default: /tmp/esp.img)" |
LaMont Jones | 7074374 | 2019-04-26 10:14:33 -0600 | [diff] [blame] | 34 | # offset= interacts with dirty pages in the file in a very poor manner. See |
| 35 | # crbug.com/954188. Use device partitions on the loop device instead. |
| 36 | #DEFINE_integer to_offset 0 \ |
| 37 | # "Offset in bytes into 'to' if it is a file (Default: 0)" |
| 38 | #DEFINE_integer to_size -1 \ |
| 39 | # "Size in bytes of 'to' to use if it is a file. -1 is ignored. (Default: -1)" |
LaMont Jones | 0d2f049 | 2019-04-18 17:26:56 -0600 | [diff] [blame] | 40 | DEFINE_integer to_partition -1 \ |
| 41 | "Partition number to use on block device. -1 is ignored. (Default: -1)" |
Will Drewry | d3c938b | 2010-07-03 13:32:26 -0500 | [diff] [blame] | 42 | DEFINE_string vmlinuz "/tmp/vmlinuz" \ |
| 43 | "Path to the vmlinuz file to use (Default: /tmp/vmlinuz)" |
Georges Winkenbach | d9d48db | 2018-11-02 14:59:15 -0600 | [diff] [blame] | 44 | DEFINE_string zimage "/tmp/zImage" \ |
| 45 | "Path to the zimage file to use (Default: /tmp/zImage)" |
Will Drewry | d3c938b | 2010-07-03 13:32:26 -0500 | [diff] [blame] | 46 | # The kernel_partition and the kernel_cmdline each are used to supply |
| 47 | # verified boot configuration: dm="". |
| 48 | DEFINE_string kernel_partition "/tmp/vmlinuz.image" \ |
| 49 | "Path to the signed kernel image. (Default: /tmp/vmlinuz.image)" |
| 50 | DEFINE_string kernel_cmdline "" \ |
| 51 | "Kernel commandline if no kernel_partition given. (Default: '')" |
| 52 | DEFINE_string kernel_partition_offset "0" \ |
| 53 | "Offset to the kernel partition [KERN-A] (Default: 0)" |
| 54 | DEFINE_string kernel_partition_sectors "0" \ |
| 55 | "Kernel partition sectors (Default: 0)" |
Will Drewry | d3c938b | 2010-07-03 13:32:26 -0500 | [diff] [blame] | 56 | |
| 57 | # Parse flags |
| 58 | FLAGS "$@" || exit 1 |
| 59 | eval set -- "${FLAGS_ARGV}" |
Brian Harring | 7f175a5 | 2012-03-02 05:37:00 -0800 | [diff] [blame] | 60 | switch_to_strict_mode |
Will Drewry | d3c938b | 2010-07-03 13:32:26 -0500 | [diff] [blame] | 61 | |
Evan Green | 550e57c | 2019-03-22 17:03:55 -0700 | [diff] [blame] | 62 | . "${BUILD_LIBRARY_DIR}/board_options.sh" || exit 1 |
| 63 | load_board_specific_script "board_specific_setup.sh" |
| 64 | |
Elly Jones | dfd3694 | 2011-08-10 15:59:36 -0400 | [diff] [blame] | 65 | part_index_to_uuid() { |
| 66 | local image="$1" |
| 67 | local index="$2" |
| 68 | cgpt show -i "$index" -u "$image" |
| 69 | } |
| 70 | |
Will Drewry | d3c938b | 2010-07-03 13:32:26 -0500 | [diff] [blame] | 71 | # If not provided by chromeos-common.sh, this will update all of the |
| 72 | # boot loader files (both A and B) with the data pulled |
| 73 | # from the kernel_partition. The default boot target should |
| 74 | # be set when the rootfs is stuffed. |
| 75 | if ! type -p update_x86_bootloaders; then |
| 76 | update_x86_bootloaders() { |
Will Drewry | b910de8 | 2011-02-23 13:26:50 -0600 | [diff] [blame] | 77 | 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] | 78 | local kernel_cmdline="$2" |
| 79 | local esp_fs_dir="$3" |
| 80 | local template_dir="$4" |
Elly Jones | dfd3694 | 2011-08-10 15:59:36 -0400 | [diff] [blame] | 81 | local to="$5" |
Will Drewry | d3c938b | 2010-07-03 13:32:26 -0500 | [diff] [blame] | 82 | |
| 83 | # Pull out the dm="" values |
Will Drewry | 82780e5 | 2010-07-03 18:27:10 -0700 | [diff] [blame] | 84 | dm_table= |
| 85 | if echo "$kernel_cmdline" | grep -q 'dm="'; then |
| 86 | dm_table=$(echo "$kernel_cmdline" | sed -s 's/.*dm="\([^"]*\)".*/\1/') |
| 87 | fi |
Will Drewry | d3c938b | 2010-07-03 13:32:26 -0500 | [diff] [blame] | 88 | |
Joshua Emele | e606672 | 2017-03-07 16:36:55 -0800 | [diff] [blame] | 89 | # Discover last known partition numbers. |
| 90 | local partition_num_root_a="$(get_layout_partition_number \ |
| 91 | "${FLAGS_image_type}" ROOT-A)" |
| 92 | local partition_num_root_b="$(get_layout_partition_number \ |
| 93 | "${FLAGS_image_type}" ROOT-B)" |
| 94 | root_a_uuid="PARTUUID=$(part_index_to_uuid "$to" ${partition_num_root_a})" |
| 95 | root_b_uuid="PARTUUID=$(part_index_to_uuid "$to" ${partition_num_root_b})" |
Elly Jones | dfd3694 | 2011-08-10 15:59:36 -0400 | [diff] [blame] | 96 | |
Will Drewry | d3c938b | 2010-07-03 13:32:26 -0500 | [diff] [blame] | 97 | # Rewrite grub table |
Elly Jones | dfd3694 | 2011-08-10 15:59:36 -0400 | [diff] [blame] | 98 | grub_dm_table_a=${dm_table//${old_root}/${root_a_uuid}} |
| 99 | grub_dm_table_b=${dm_table//${old_root}/${root_b_uuid}} |
Will Drewry | d3c938b | 2010-07-03 13:32:26 -0500 | [diff] [blame] | 100 | sed -e "s|DMTABLEA|${grub_dm_table_a}|g" \ |
| 101 | -e "s|DMTABLEB|${grub_dm_table_b}|g" \ |
Hung-Te Lin | 368df43 | 2013-03-20 16:13:07 +0800 | [diff] [blame] | 102 | -e "s|/dev/\\\$linuxpartA|${root_a_uuid}|g" \ |
Elly Jones | dfd3694 | 2011-08-10 15:59:36 -0400 | [diff] [blame] | 103 | -e "s|/dev/\\\$linuxpartB|${root_b_uuid}|g" \ |
Prathmesh Prabhu | 0606a7c | 2014-10-23 09:55:03 -0700 | [diff] [blame] | 104 | -e "s|HDROOTUSB|${root_a_uuid}|g" \ |
Elly Jones | dfd3694 | 2011-08-10 15:59:36 -0400 | [diff] [blame] | 105 | "${template_dir}"/efi/boot/grub.cfg | |
Mike Frysinger | 79ae5b4 | 2013-01-08 11:00:11 -0500 | [diff] [blame] | 106 | sudo dd of="${esp_fs_dir}"/efi/boot/grub.cfg status=none |
Will Drewry | d3c938b | 2010-07-03 13:32:26 -0500 | [diff] [blame] | 107 | |
Prathmesh Prabhu | 0606a7c | 2014-10-23 09:55:03 -0700 | [diff] [blame] | 108 | # Rewrite syslinux DM_TABLE and HDROOTUSB for USB booting. |
Elly Jones | dfd3694 | 2011-08-10 15:59:36 -0400 | [diff] [blame] | 109 | syslinux_dm_table_usb=${dm_table//${old_root}/${root_a_uuid}} |
Will Drewry | d3c938b | 2010-07-03 13:32:26 -0500 | [diff] [blame] | 110 | sed -e "s|DMTABLEA|${syslinux_dm_table_usb}|g" \ |
Prathmesh Prabhu | 0606a7c | 2014-10-23 09:55:03 -0700 | [diff] [blame] | 111 | -e "s|HDROOTUSB|${root_a_uuid}|g" \ |
Will Drewry | d3c938b | 2010-07-03 13:32:26 -0500 | [diff] [blame] | 112 | "${template_dir}"/syslinux/usb.A.cfg | |
Mike Frysinger | 79ae5b4 | 2013-01-08 11:00:11 -0500 | [diff] [blame] | 113 | sudo dd of="${esp_fs_dir}"/syslinux/usb.A.cfg status=none |
Will Drewry | d3c938b | 2010-07-03 13:32:26 -0500 | [diff] [blame] | 114 | |
Hung-Te Lin | 6d7164a | 2013-03-21 16:59:12 +0800 | [diff] [blame] | 115 | # Note DMTABLE for root.A and root.B does not need to be updated because |
| 116 | # postinst will discard all changes in EFI partition and copy from |
| 117 | # rootfs:boot/syslinux/root.?.cfg again after installation or AU, because |
| 118 | # new rootfs will be apparently different. |
Will Drewry | d3c938b | 2010-07-03 13:32:26 -0500 | [diff] [blame] | 119 | |
| 120 | # Copy the vmlinuz's into place for syslinux |
Georges Winkenbach | d9d48db | 2018-11-02 14:59:15 -0600 | [diff] [blame] | 121 | sudo cp -f "${FLAGS_vmlinuz}" "${esp_fs_dir}"/syslinux/vmlinuz.A |
| 122 | sudo cp -f "${FLAGS_vmlinuz}" "${esp_fs_dir}"/syslinux/vmlinuz.B |
Will Drewry | d3c938b | 2010-07-03 13:32:26 -0500 | [diff] [blame] | 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 | |
LaMont Jones | 0d2f049 | 2019-04-18 17:26:56 -0600 | [diff] [blame] | 129 | ESP_DEV_OURS= |
Will Drewry | d3c938b | 2010-07-03 13:32:26 -0500 | [diff] [blame] | 130 | ESP_DEV= |
| 131 | if [[ ! -e "${FLAGS_to}" ]]; then |
| 132 | error "The ESP doesn't exist" |
| 133 | # This shouldn't happen. |
| 134 | info "Creating a new esp image at ${FLAGS_to}" anyway. |
| 135 | # 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] | 136 | # BIOS). ARM uses this space to determine which partition is bootable. |
Will Drewry | d3c938b | 2010-07-03 13:32:26 -0500 | [diff] [blame] | 137 | # NOTE: The size argument for mkfs.vfat is in 1024-byte blocks. |
| 138 | # We'll hard-code it to 16M for now. |
| 139 | ESP_BLOCKS=16384 |
| 140 | /usr/sbin/mkfs.vfat -C "${FLAGS_to}" ${ESP_BLOCKS} |
David James | 8b9643f | 2011-07-21 22:11:11 -0700 | [diff] [blame] | 141 | ESP_DEV=$(sudo losetup --show -f "${FLAGS_to}") |
LaMont Jones | 0d2f049 | 2019-04-18 17:26:56 -0600 | [diff] [blame] | 142 | ESP_DEV_OURS=y |
Will Drewry | 721d94f | 2010-07-16 14:39:45 -0500 | [diff] [blame] | 143 | if [ -z "${ESP_DEV}" ]; then |
| 144 | die "No free loop devices." |
| 145 | fi |
Will Drewry | d3c938b | 2010-07-03 13:32:26 -0500 | [diff] [blame] | 146 | else |
| 147 | if [[ -f "${FLAGS_to}" ]]; then |
LaMont Jones | 0d2f049 | 2019-04-18 17:26:56 -0600 | [diff] [blame] | 148 | ESP_DEV=$(sudo losetup --show -f "${FLAGS_to}") |
| 149 | ESP_DEV_OURS=y |
David James | 8b9643f | 2011-07-21 22:11:11 -0700 | [diff] [blame] | 150 | if [ -z "${ESP_DEV}" ]; then |
| 151 | die "No free loop devices." |
| 152 | fi |
Will Drewry | d3c938b | 2010-07-03 13:32:26 -0500 | [diff] [blame] | 153 | else |
| 154 | # If it is a block device or something else, try to mount it anyway. |
| 155 | ESP_DEV="${FLAGS_to}" |
LaMont Jones | 0d2f049 | 2019-04-18 17:26:56 -0600 | [diff] [blame] | 156 | if [ ${FLAGS_to_partition} -ge 0 ]; then |
| 157 | ESP_DEV="${ESP_DEV}"p"${FLAGS_to_partition}" |
| 158 | fi |
Will Drewry | d3c938b | 2010-07-03 13:32:26 -0500 | [diff] [blame] | 159 | fi |
| 160 | fi |
| 161 | |
| 162 | ESP_FS_DIR=$(mktemp -d /tmp/esp.XXXXXX) |
| 163 | cleanup() { |
David James | c9ca3db | 2012-07-09 08:12:26 -0700 | [diff] [blame] | 164 | set +e |
Brian Harring | ece65e0 | 2012-08-30 13:42:21 -0700 | [diff] [blame] | 165 | if ! safe_umount "${ESP_FS_DIR}"; then |
David James | c9ca3db | 2012-07-09 08:12:26 -0700 | [diff] [blame] | 166 | # There is a race condition possible on some ubuntu setups |
| 167 | # with mounting and unmounting a device very quickly |
| 168 | # Doing a quick sleep/retry as a temporary workaround |
| 169 | warn "Initial unmount failed. Possibly crosbug.com/23443. Retrying" |
| 170 | sleep 5 |
Brian Harring | ece65e0 | 2012-08-30 13:42:21 -0700 | [diff] [blame] | 171 | safe_umount "${ESP_FS_DIR}" |
Jonathan Kliegman | ae6baa2 | 2011-11-25 11:03:16 -0500 | [diff] [blame] | 172 | fi |
LaMont Jones | 0d2f049 | 2019-04-18 17:26:56 -0600 | [diff] [blame] | 173 | if [[ -n "${ESP_DEV_OURS}" && -z "${ESP_DEV//\/dev\/loop*}" ]]; then |
David James | c9ca3db | 2012-07-09 08:12:26 -0700 | [diff] [blame] | 174 | sudo losetup -d "${ESP_DEV}" |
Will Drewry | d3c938b | 2010-07-03 13:32:26 -0500 | [diff] [blame] | 175 | fi |
David James | c9ca3db | 2012-07-09 08:12:26 -0700 | [diff] [blame] | 176 | rm -rf "${ESP_FS_DIR}" |
Will Drewry | d3c938b | 2010-07-03 13:32:26 -0500 | [diff] [blame] | 177 | } |
| 178 | trap cleanup EXIT |
| 179 | sudo mount "${ESP_DEV}" "${ESP_FS_DIR}" |
| 180 | |
Sonny Rao | 3163cf0 | 2011-10-17 16:20:28 -0700 | [diff] [blame] | 181 | if [[ "${FLAGS_arch}" = "x86" || "${FLAGS_arch}" = "amd64" ]]; then |
Will Drewry | d3c938b | 2010-07-03 13:32:26 -0500 | [diff] [blame] | 182 | # Populate the EFI bootloader configuration |
| 183 | sudo mkdir -p "${ESP_FS_DIR}/efi/boot" |
Josh Triplett | f09c59b | 2013-04-25 11:08:13 -0700 | [diff] [blame] | 184 | sudo cp -r "${FLAGS_from}"/efi/boot/. "${ESP_FS_DIR}"/efi/boot |
Will Drewry | d3c938b | 2010-07-03 13:32:26 -0500 | [diff] [blame] | 185 | |
| 186 | # Prepopulate the syslinux directories too and update for verified boot values |
| 187 | # after the rootfs work is done. |
| 188 | sudo mkdir -p "${ESP_FS_DIR}"/syslinux |
| 189 | sudo cp -r "${FLAGS_from}"/syslinux/. "${ESP_FS_DIR}"/syslinux |
| 190 | |
| 191 | # Stage both kernels with the only one we built. |
| 192 | sudo cp -f "${FLAGS_vmlinuz}" "${ESP_FS_DIR}"/syslinux/vmlinuz.A |
| 193 | sudo cp -f "${FLAGS_vmlinuz}" "${ESP_FS_DIR}"/syslinux/vmlinuz.B |
| 194 | |
| 195 | # Extract kernel flags |
| 196 | kernel_cfg= |
Paul Taysom | a59a0b3 | 2013-07-24 15:56:18 -0700 | [diff] [blame] | 197 | old_root='PARTUUID=%U/PARTNROFF=1' |
Will Drewry | d3c938b | 2010-07-03 13:32:26 -0500 | [diff] [blame] | 198 | if [[ -n "${FLAGS_kernel_cmdline}" ]]; then |
| 199 | info "Using supplied kernel_cmdline to update templates." |
| 200 | kernel_cfg="${FLAGS_kernel_cmdline}" |
| 201 | elif [[ -n "${FLAGS_kernel_partition}" ]]; then |
| 202 | info "Extracting the kernel command line from ${FLAGS_kernel_partition}" |
| 203 | kernel_cfg=$(dump_kernel_config "${FLAGS_kernel_partition}") |
| 204 | fi |
| 205 | update_x86_bootloaders "${old_root}" \ |
| 206 | "${kernel_cfg}" \ |
| 207 | "${ESP_FS_DIR}" \ |
Elly Jones | dfd3694 | 2011-08-10 15:59:36 -0400 | [diff] [blame] | 208 | "${FLAGS_from}" \ |
| 209 | "${FLAGS_to}" |
Will Drewry | d3c938b | 2010-07-03 13:32:26 -0500 | [diff] [blame] | 210 | |
| 211 | # Install the syslinux loader on the ESP image (part 12) so it is ready when |
| 212 | # we cut over from rootfs booting (extlinux). |
| 213 | if [[ ${FLAGS_install_syslinux} -eq ${FLAGS_TRUE} ]]; then |
Brian Harring | ece65e0 | 2012-08-30 13:42:21 -0700 | [diff] [blame] | 214 | safe_umount "${ESP_FS_DIR}" |
Will Drewry | 721d94f | 2010-07-16 14:39:45 -0500 | [diff] [blame] | 215 | sudo syslinux -d /syslinux "${ESP_DEV}" |
David James | c9ca3db | 2012-07-09 08:12:26 -0700 | [diff] [blame] | 216 | # mount again for cleanup to free resource gracefully |
| 217 | sudo mount -o ro "${ESP_DEV}" "${ESP_FS_DIR}" |
Will Drewry | d3c938b | 2010-07-03 13:32:26 -0500 | [diff] [blame] | 218 | fi |
Ben Chan | d4ec205 | 2014-07-17 18:18:31 -0700 | [diff] [blame] | 219 | elif [[ "${FLAGS_arch}" = "arm" || "${FLAGS_arch}" = "mips" ]]; then |
Kenneth Waters | eca7646 | 2010-08-18 11:17:01 -0700 | [diff] [blame] | 220 | # Copy u-boot script to ESP partition |
Kenneth Waters | cb39f99 | 2010-08-18 14:35:22 -0700 | [diff] [blame] | 221 | if [ -r "${FLAGS_from}/boot-A.scr.uimg" ]; then |
| 222 | sudo mkdir -p "${ESP_FS_DIR}/u-boot" |
| 223 | sudo cp "${FLAGS_from}/boot-A.scr.uimg" \ |
| 224 | "${ESP_FS_DIR}/u-boot/boot.scr.uimg" |
Georges Winkenbach | d9d48db | 2018-11-02 14:59:15 -0600 | [diff] [blame] | 225 | sudo cp -f "${FLAGS_vmlinuz}" "${ESP_FS_DIR}"/vmlinuz.uimg.A |
| 226 | if [ -r "${FLAGS_zimage}" ]; then |
| 227 | sudo cp -f "${FLAGS_zimage}" "${ESP_FS_DIR}"/vmlinuz.A |
David Riley | 0ffab01 | 2014-07-03 15:31:43 -0700 | [diff] [blame] | 228 | fi |
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 |