blob: c5a5967c11d35c63973b4a27586dff2a555e7ffd [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
Steven 'Steve' Kendall57636ac2016-06-09 12:50:25 -040012. ${BUILD_LIBRARY_DIR}/disk_layout_util.sh
Greg Spencer798d75f2011-02-01 22:04:49 -080013
14# Need to be inside the chroot to load chromeos-common.sh
15assert_inside_chroot
16
17# Load functions and constants for chromeos-install
Gwendal Grignou0f618492014-04-07 20:05:58 +000018. /usr/share/misc/chromeos-common.sh || exit 1
Will Drewryd3c938b2010-07-03 13:32:26 -050019
Will Drewryd3c938b2010-07-03 13:32:26 -050020# Flags.
21DEFINE_string arch "x86" \
Ben Chand4ec2052014-07-17 18:18:31 -070022 "The boot architecture: arm, mips, x86, or amd64 (Default: x86)"
Will Drewryd3c938b2010-07-03 13:32:26 -050023# TODO(wad) once extlinux is dead, we can remove this.
24DEFINE_boolean install_syslinux ${FLAGS_FALSE} \
25 "Controls whether syslinux is run on 'to'. (Default: false)"
26DEFINE_string from "/tmp/boot" \
27 "Path the legacy bootloader templates are copied from. (Default /tmp/boot)"
28DEFINE_string to "/tmp/esp.img" \
Kenneth Waterse3049de2010-09-30 14:20:34 -070029 "Path to esp image (Default: /tmp/esp.img)"
Will Drewry721d94f2010-07-16 14:39:45 -050030DEFINE_integer to_offset 0 \
31 "Offset in bytes into 'to' if it is a file (Default: 0)"
32DEFINE_integer to_size -1 \
33 "Size in bytes of 'to' to use if it is a file. -1 is ignored. (Default: -1)"
Will Drewryd3c938b2010-07-03 13:32:26 -050034DEFINE_string vmlinuz "/tmp/vmlinuz" \
35 "Path to the vmlinuz file to use (Default: /tmp/vmlinuz)"
36# The kernel_partition and the kernel_cmdline each are used to supply
37# verified boot configuration: dm="".
38DEFINE_string kernel_partition "/tmp/vmlinuz.image" \
39 "Path to the signed kernel image. (Default: /tmp/vmlinuz.image)"
40DEFINE_string kernel_cmdline "" \
41 "Kernel commandline if no kernel_partition given. (Default: '')"
42DEFINE_string kernel_partition_offset "0" \
43 "Offset to the kernel partition [KERN-A] (Default: 0)"
44DEFINE_string kernel_partition_sectors "0" \
45 "Kernel partition sectors (Default: 0)"
Will Drewryd3c938b2010-07-03 13:32:26 -050046
47# Parse flags
48FLAGS "$@" || exit 1
49eval set -- "${FLAGS_ARGV}"
Brian Harring7f175a52012-03-02 05:37:00 -080050switch_to_strict_mode
Will Drewryd3c938b2010-07-03 13:32:26 -050051
Elly Jonesdfd36942011-08-10 15:59:36 -040052part_index_to_uuid() {
53 local image="$1"
54 local index="$2"
55 cgpt show -i "$index" -u "$image"
56}
57
Will Drewryd3c938b2010-07-03 13:32:26 -050058# If not provided by chromeos-common.sh, this will update all of the
59# boot loader files (both A and B) with the data pulled
60# from the kernel_partition. The default boot target should
61# be set when the rootfs is stuffed.
62if ! type -p update_x86_bootloaders; then
63 update_x86_bootloaders() {
Will Drewryb910de82011-02-23 13:26:50 -060064 local old_root="$1" # e.g., /dev/sd%D%P or %U+1
Will Drewryd3c938b2010-07-03 13:32:26 -050065 local kernel_cmdline="$2"
66 local esp_fs_dir="$3"
67 local template_dir="$4"
Elly Jonesdfd36942011-08-10 15:59:36 -040068 local to="$5"
Will Drewryd3c938b2010-07-03 13:32:26 -050069
70 # Pull out the dm="" values
Will Drewry82780e52010-07-03 18:27:10 -070071 dm_table=
72 if echo "$kernel_cmdline" | grep -q 'dm="'; then
73 dm_table=$(echo "$kernel_cmdline" | sed -s 's/.*dm="\([^"]*\)".*/\1/')
74 fi
Will Drewryd3c938b2010-07-03 13:32:26 -050075
Steven 'Steve' Kendall57636ac2016-06-09 12:50:25 -040076 root_a_uuid="PARTUUID=$(part_index_to_uuid "$to" ${PARTITION_NUM_ROOT_A})"
77 root_b_uuid="PARTUUID=$(part_index_to_uuid "$to" ${PARTITION_NUM_ROOT_B})"
Elly Jonesdfd36942011-08-10 15:59:36 -040078
Will Drewryd3c938b2010-07-03 13:32:26 -050079 # Rewrite grub table
Elly Jonesdfd36942011-08-10 15:59:36 -040080 grub_dm_table_a=${dm_table//${old_root}/${root_a_uuid}}
81 grub_dm_table_b=${dm_table//${old_root}/${root_b_uuid}}
Will Drewryd3c938b2010-07-03 13:32:26 -050082 sed -e "s|DMTABLEA|${grub_dm_table_a}|g" \
83 -e "s|DMTABLEB|${grub_dm_table_b}|g" \
Hung-Te Lin368df432013-03-20 16:13:07 +080084 -e "s|/dev/\\\$linuxpartA|${root_a_uuid}|g" \
Elly Jonesdfd36942011-08-10 15:59:36 -040085 -e "s|/dev/\\\$linuxpartB|${root_b_uuid}|g" \
Prathmesh Prabhu0606a7c2014-10-23 09:55:03 -070086 -e "s|HDROOTUSB|${root_a_uuid}|g" \
Elly Jonesdfd36942011-08-10 15:59:36 -040087 "${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
Prathmesh Prabhu0606a7c2014-10-23 09:55:03 -070090 # Rewrite syslinux DM_TABLE and HDROOTUSB 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" \
Prathmesh Prabhu0606a7c2014-10-23 09:55:03 -070093 -e "s|HDROOTUSB|${root_a_uuid}|g" \
Will Drewryd3c938b2010-07-03 13:32:26 -050094 "${template_dir}"/syslinux/usb.A.cfg |
Mike Frysinger79ae5b42013-01-08 11:00:11 -050095 sudo dd of="${esp_fs_dir}"/syslinux/usb.A.cfg status=none
Will Drewryd3c938b2010-07-03 13:32:26 -050096
Hung-Te Lin6d7164a2013-03-21 16:59:12 +080097 # Note DMTABLE for root.A and root.B does not need to be updated because
98 # postinst will discard all changes in EFI partition and copy from
99 # rootfs:boot/syslinux/root.?.cfg again after installation or AU, because
100 # new rootfs will be apparently different.
Will Drewryd3c938b2010-07-03 13:32:26 -0500101
102 # Copy the vmlinuz's into place for syslinux
103 sudo cp -f "${template_dir}"/vmlinuz "${esp_fs_dir}"/syslinux/vmlinuz.A
104 sudo cp -f "${template_dir}"/vmlinuz "${esp_fs_dir}"/syslinux/vmlinuz.B
105
106 # The only work left for the installer is to pick the correct defaults
Will Drewryb910de82011-02-23 13:26:50 -0600107 # and replace HDROOTA and HDROOTB with the correct /dev/sd%D%P/%U+1
Will Drewryd3c938b2010-07-03 13:32:26 -0500108 }
109fi
110
111ESP_DEV=
112if [[ ! -e "${FLAGS_to}" ]]; then
113 error "The ESP doesn't exist"
114 # This shouldn't happen.
115 info "Creating a new esp image at ${FLAGS_to}" anyway.
116 # Create EFI System Partition to boot stock EFI BIOS (but not ChromeOS EFI
Kenneth Waterse3049de2010-09-30 14:20:34 -0700117 # BIOS). ARM uses this space to determine which partition is bootable.
Will Drewryd3c938b2010-07-03 13:32:26 -0500118 # NOTE: The size argument for mkfs.vfat is in 1024-byte blocks.
119 # We'll hard-code it to 16M for now.
120 ESP_BLOCKS=16384
121 /usr/sbin/mkfs.vfat -C "${FLAGS_to}" ${ESP_BLOCKS}
David James8b9643f2011-07-21 22:11:11 -0700122 ESP_DEV=$(sudo losetup --show -f "${FLAGS_to}")
Will Drewry721d94f2010-07-16 14:39:45 -0500123 if [ -z "${ESP_DEV}" ]; then
124 die "No free loop devices."
125 fi
Will Drewryd3c938b2010-07-03 13:32:26 -0500126else
127 if [[ -f "${FLAGS_to}" ]]; then
Will Drewry721d94f2010-07-16 14:39:45 -0500128 esp_offset="--offset ${FLAGS_to_offset}"
129 esp_size="--sizelimit ${FLAGS_to_size}"
130 if [ ${FLAGS_to_size} -lt 0 ]; then
131 esp_size=
132 fi
David James8b9643f2011-07-21 22:11:11 -0700133 ESP_DEV=$(sudo losetup --show -f ${esp_offset} ${esp_size} "${FLAGS_to}")
134 if [ -z "${ESP_DEV}" ]; then
135 die "No free loop devices."
136 fi
Will Drewryd3c938b2010-07-03 13:32:26 -0500137 else
138 # If it is a block device or something else, try to mount it anyway.
139 ESP_DEV="${FLAGS_to}"
140 fi
141fi
142
143ESP_FS_DIR=$(mktemp -d /tmp/esp.XXXXXX)
144cleanup() {
David Jamesc9ca3db2012-07-09 08:12:26 -0700145 set +e
Brian Harringece65e02012-08-30 13:42:21 -0700146 if ! safe_umount "${ESP_FS_DIR}"; then
David Jamesc9ca3db2012-07-09 08:12:26 -0700147 # There is a race condition possible on some ubuntu setups
148 # with mounting and unmounting a device very quickly
149 # Doing a quick sleep/retry as a temporary workaround
150 warn "Initial unmount failed. Possibly crosbug.com/23443. Retrying"
151 sleep 5
Brian Harringece65e02012-08-30 13:42:21 -0700152 safe_umount "${ESP_FS_DIR}"
Jonathan Kliegmanae6baa22011-11-25 11:03:16 -0500153 fi
Will Drewryd3c938b2010-07-03 13:32:26 -0500154 if [[ -n "${ESP_DEV}" && -z "${ESP_DEV//\/dev\/loop*}" ]]; then
David Jamesc9ca3db2012-07-09 08:12:26 -0700155 sudo losetup -d "${ESP_DEV}"
Will Drewryd3c938b2010-07-03 13:32:26 -0500156 fi
David Jamesc9ca3db2012-07-09 08:12:26 -0700157 rm -rf "${ESP_FS_DIR}"
Will Drewryd3c938b2010-07-03 13:32:26 -0500158}
159trap cleanup EXIT
160sudo mount "${ESP_DEV}" "${ESP_FS_DIR}"
161
Sonny Rao3163cf02011-10-17 16:20:28 -0700162if [[ "${FLAGS_arch}" = "x86" || "${FLAGS_arch}" = "amd64" ]]; then
Will Drewryd3c938b2010-07-03 13:32:26 -0500163 # Populate the EFI bootloader configuration
164 sudo mkdir -p "${ESP_FS_DIR}/efi/boot"
Josh Triplettf09c59b2013-04-25 11:08:13 -0700165 sudo cp -r "${FLAGS_from}"/efi/boot/. "${ESP_FS_DIR}"/efi/boot
Will Drewryd3c938b2010-07-03 13:32:26 -0500166
167 # Prepopulate the syslinux directories too and update for verified boot values
168 # after the rootfs work is done.
169 sudo mkdir -p "${ESP_FS_DIR}"/syslinux
170 sudo cp -r "${FLAGS_from}"/syslinux/. "${ESP_FS_DIR}"/syslinux
171
172 # Stage both kernels with the only one we built.
173 sudo cp -f "${FLAGS_vmlinuz}" "${ESP_FS_DIR}"/syslinux/vmlinuz.A
174 sudo cp -f "${FLAGS_vmlinuz}" "${ESP_FS_DIR}"/syslinux/vmlinuz.B
175
176 # Extract kernel flags
177 kernel_cfg=
Paul Taysoma59a0b32013-07-24 15:56:18 -0700178 old_root='PARTUUID=%U/PARTNROFF=1'
Will Drewryd3c938b2010-07-03 13:32:26 -0500179 if [[ -n "${FLAGS_kernel_cmdline}" ]]; then
180 info "Using supplied kernel_cmdline to update templates."
181 kernel_cfg="${FLAGS_kernel_cmdline}"
182 elif [[ -n "${FLAGS_kernel_partition}" ]]; then
183 info "Extracting the kernel command line from ${FLAGS_kernel_partition}"
184 kernel_cfg=$(dump_kernel_config "${FLAGS_kernel_partition}")
185 fi
186 update_x86_bootloaders "${old_root}" \
187 "${kernel_cfg}" \
188 "${ESP_FS_DIR}" \
Elly Jonesdfd36942011-08-10 15:59:36 -0400189 "${FLAGS_from}" \
190 "${FLAGS_to}"
Will Drewryd3c938b2010-07-03 13:32:26 -0500191
192 # Install the syslinux loader on the ESP image (part 12) so it is ready when
193 # we cut over from rootfs booting (extlinux).
194 if [[ ${FLAGS_install_syslinux} -eq ${FLAGS_TRUE} ]]; then
Brian Harringece65e02012-08-30 13:42:21 -0700195 safe_umount "${ESP_FS_DIR}"
Will Drewry721d94f2010-07-16 14:39:45 -0500196 sudo syslinux -d /syslinux "${ESP_DEV}"
David Jamesc9ca3db2012-07-09 08:12:26 -0700197 # mount again for cleanup to free resource gracefully
198 sudo mount -o ro "${ESP_DEV}" "${ESP_FS_DIR}"
Will Drewryd3c938b2010-07-03 13:32:26 -0500199 fi
Ben Chand4ec2052014-07-17 18:18:31 -0700200elif [[ "${FLAGS_arch}" = "arm" || "${FLAGS_arch}" = "mips" ]]; then
Kenneth Waterseca76462010-08-18 11:17:01 -0700201 # Copy u-boot script to ESP partition
Kenneth Waterscb39f992010-08-18 14:35:22 -0700202 if [ -r "${FLAGS_from}/boot-A.scr.uimg" ]; then
203 sudo mkdir -p "${ESP_FS_DIR}/u-boot"
204 sudo cp "${FLAGS_from}/boot-A.scr.uimg" \
205 "${ESP_FS_DIR}/u-boot/boot.scr.uimg"
Olof Johansson296adf02011-12-22 15:05:26 -0800206 sudo cp -f "${FLAGS_from}"/vmlinuz "${ESP_FS_DIR}"/vmlinuz.uimg.A
David Riley0ffab012014-07-03 15:31:43 -0700207 if [ -r "${FLAGS_from}/zImage" ]; then
208 sudo cp -f "${FLAGS_from}"/zImage "${ESP_FS_DIR}"/vmlinuz.A
209 fi
Kenneth Waterscb39f992010-08-18 14:35:22 -0700210 fi
Will Drewryd3c938b2010-07-03 13:32:26 -0500211fi
212
213set +e