blob: 5aa9b9326b183d1eb69324d9fa3cc5568574a0a7 [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"))
Brian Harringd5d5dbf2011-07-11 16:36:21 -070011. "${SCRIPT_ROOT}/common.sh" || { echo "Unable to load 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
17. "/usr/lib/installer/chromeos-common.sh" || \
18 die "Unable to load /usr/lib/installer/chromeos-common.sh"
Will Drewryd3c938b2010-07-03 13:32:26 -050019
20get_default_board
21
22# Flags.
23DEFINE_string arch "x86" \
24 "The boot architecture: arm or x86. (Default: x86)"
25# TODO(wad) once extlinux is dead, we can remove this.
26DEFINE_boolean install_syslinux ${FLAGS_FALSE} \
27 "Controls whether syslinux is run on 'to'. (Default: false)"
28DEFINE_string from "/tmp/boot" \
29 "Path the legacy bootloader templates are copied from. (Default /tmp/boot)"
30DEFINE_string to "/tmp/esp.img" \
Kenneth Waterse3049de2010-09-30 14:20:34 -070031 "Path to esp image (Default: /tmp/esp.img)"
Will Drewry721d94f2010-07-16 14:39:45 -050032DEFINE_integer to_offset 0 \
33 "Offset in bytes into 'to' if it is a file (Default: 0)"
34DEFINE_integer to_size -1 \
35 "Size in bytes of 'to' to use if it is a file. -1 is ignored. (Default: -1)"
Will Drewryd3c938b2010-07-03 13:32:26 -050036DEFINE_string vmlinuz "/tmp/vmlinuz" \
37 "Path to the vmlinuz file to use (Default: /tmp/vmlinuz)"
38# The kernel_partition and the kernel_cmdline each are used to supply
39# verified boot configuration: dm="".
40DEFINE_string kernel_partition "/tmp/vmlinuz.image" \
41 "Path to the signed kernel image. (Default: /tmp/vmlinuz.image)"
42DEFINE_string kernel_cmdline "" \
43 "Kernel commandline if no kernel_partition given. (Default: '')"
44DEFINE_string kernel_partition_offset "0" \
45 "Offset to the kernel partition [KERN-A] (Default: 0)"
46DEFINE_string kernel_partition_sectors "0" \
47 "Kernel partition sectors (Default: 0)"
48DEFINE_string usb_disk /dev/sdb3 \
Kenneth Waterse3049de2010-09-30 14:20:34 -070049 "Path syslinux should use to do a usb boot. Default: /dev/sdb3"
Will Drewryd3c938b2010-07-03 13:32:26 -050050
51# Parse flags
52FLAGS "$@" || exit 1
53eval set -- "${FLAGS_ARGV}"
Brian Harring7f175a52012-03-02 05:37:00 -080054switch_to_strict_mode
Will Drewryd3c938b2010-07-03 13:32:26 -050055
Elly Jonesdfd36942011-08-10 15:59:36 -040056part_index_to_uuid() {
57 local image="$1"
58 local index="$2"
59 cgpt show -i "$index" -u "$image"
60}
61
Will Drewryd3c938b2010-07-03 13:32:26 -050062# If not provided by chromeos-common.sh, this will update all of the
63# boot loader files (both A and B) with the data pulled
64# from the kernel_partition. The default boot target should
65# be set when the rootfs is stuffed.
66if ! type -p update_x86_bootloaders; then
67 update_x86_bootloaders() {
Will Drewryb910de82011-02-23 13:26:50 -060068 local old_root="$1" # e.g., /dev/sd%D%P or %U+1
Will Drewryd3c938b2010-07-03 13:32:26 -050069 local kernel_cmdline="$2"
70 local esp_fs_dir="$3"
71 local template_dir="$4"
Elly Jonesdfd36942011-08-10 15:59:36 -040072 local to="$5"
Will Drewryd3c938b2010-07-03 13:32:26 -050073
74 # Pull out the dm="" values
Will Drewry82780e52010-07-03 18:27:10 -070075 dm_table=
76 if echo "$kernel_cmdline" | grep -q 'dm="'; then
77 dm_table=$(echo "$kernel_cmdline" | sed -s 's/.*dm="\([^"]*\)".*/\1/')
78 fi
Will Drewryd3c938b2010-07-03 13:32:26 -050079
Arkaitz Ruiz Alvareze2f33fb2011-07-08 12:21:22 -070080 # Maintain cros_debug flag in developer and test images.
81 cros_flags="cros_legacy"
82 if echo "$kernel_cmdline" | grep -q 'cros_debug'; then
83 cros_flags="cros_legacy cros_debug"
84 fi
85
Elly Jonesdfd36942011-08-10 15:59:36 -040086 root_a_uuid="PARTUUID=$(part_index_to_uuid "$to" 3)"
87 root_b_uuid="PARTUUID=$(part_index_to_uuid "$to" 5)"
88
Will Drewryd3c938b2010-07-03 13:32:26 -050089 # Rewrite grub table
Elly Jonesdfd36942011-08-10 15:59:36 -040090 grub_dm_table_a=${dm_table//${old_root}/${root_a_uuid}}
91 grub_dm_table_b=${dm_table//${old_root}/${root_b_uuid}}
Will Drewryd3c938b2010-07-03 13:32:26 -050092 sed -e "s|DMTABLEA|${grub_dm_table_a}|g" \
93 -e "s|DMTABLEB|${grub_dm_table_b}|g" \
Arkaitz Ruiz Alvareze2f33fb2011-07-08 12:21:22 -070094 -e "s|cros_legacy|${cros_flags}|g" \
Will Drewryd3c938b2010-07-03 13:32:26 -050095 "${template_dir}"/efi/boot/grub.cfg |
96 sudo dd of="${esp_fs_dir}"/efi/boot/grub.cfg
Elly Jonesdfd36942011-08-10 15:59:36 -040097 sed -e "s|/dev/\\\$linuxpartA|${root_a_uuid}|g" \
98 -e "s|/dev/\\\$linuxpartB|${root_b_uuid}|g" \
99 "${template_dir}"/efi/boot/grub.cfg |
100 sudo dd of="${esp_fs_dir}"/efi/boot/grub.cfg
Will Drewryd3c938b2010-07-03 13:32:26 -0500101
102 # Rewrite syslinux DM_TABLE
Elly Jonesdfd36942011-08-10 15:59:36 -0400103 syslinux_dm_table_usb=${dm_table//${old_root}/${root_a_uuid}}
Will Drewryd3c938b2010-07-03 13:32:26 -0500104 sed -e "s|DMTABLEA|${syslinux_dm_table_usb}|g" \
Arkaitz Ruiz Alvareze2f33fb2011-07-08 12:21:22 -0700105 -e "s|cros_legacy|${cros_flags}|g" \
Will Drewryd3c938b2010-07-03 13:32:26 -0500106 "${template_dir}"/syslinux/usb.A.cfg |
107 sudo dd of="${esp_fs_dir}"/syslinux/usb.A.cfg
108
Will Drewryb910de82011-02-23 13:26:50 -0600109 syslinux_dm_table_a=${dm_table//${old_root}/HDROOTA}
Will Drewryd3c938b2010-07-03 13:32:26 -0500110 sed -e "s|DMTABLEA|${syslinux_dm_table_a}|g" \
Arkaitz Ruiz Alvareze2f33fb2011-07-08 12:21:22 -0700111 -e "s|cros_legacy|${cros_flags}|g" \
Will Drewryd3c938b2010-07-03 13:32:26 -0500112 "${template_dir}"/syslinux/root.A.cfg |
113 sudo dd of="${esp_fs_dir}"/syslinux/root.A.cfg
114
Will Drewryb910de82011-02-23 13:26:50 -0600115 syslinux_dm_table_b=${dm_table//${old_root}/HDROOTB}
Will Drewry78992a32010-07-21 14:02:20 -0500116 sed -e "s|DMTABLEB|${syslinux_dm_table_b}|g" \
Arkaitz Ruiz Alvareze2f33fb2011-07-08 12:21:22 -0700117 -e "s|cros_legacy|${cros_flags}|g" \
Will Drewryd3c938b2010-07-03 13:32:26 -0500118 "${template_dir}"/syslinux/root.B.cfg |
119 sudo dd of="${esp_fs_dir}"/syslinux/root.B.cfg
120
121 # Copy the vmlinuz's into place for syslinux
122 sudo cp -f "${template_dir}"/vmlinuz "${esp_fs_dir}"/syslinux/vmlinuz.A
123 sudo cp -f "${template_dir}"/vmlinuz "${esp_fs_dir}"/syslinux/vmlinuz.B
124
125 # The only work left for the installer is to pick the correct defaults
Will Drewryb910de82011-02-23 13:26:50 -0600126 # and replace HDROOTA and HDROOTB with the correct /dev/sd%D%P/%U+1
Will Drewryd3c938b2010-07-03 13:32:26 -0500127 }
128fi
129
130ESP_DEV=
131if [[ ! -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 Waterse3049de2010-09-30 14:20:34 -0700136 # BIOS). ARM uses this space to determine which partition is bootable.
Will Drewryd3c938b2010-07-03 13:32:26 -0500137 # 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 James8b9643f2011-07-21 22:11:11 -0700141 ESP_DEV=$(sudo losetup --show -f "${FLAGS_to}")
Will Drewry721d94f2010-07-16 14:39:45 -0500142 if [ -z "${ESP_DEV}" ]; then
143 die "No free loop devices."
144 fi
Will Drewryd3c938b2010-07-03 13:32:26 -0500145else
146 if [[ -f "${FLAGS_to}" ]]; then
Will Drewry721d94f2010-07-16 14:39:45 -0500147 esp_offset="--offset ${FLAGS_to_offset}"
148 esp_size="--sizelimit ${FLAGS_to_size}"
149 if [ ${FLAGS_to_size} -lt 0 ]; then
150 esp_size=
151 fi
David James8b9643f2011-07-21 22:11:11 -0700152 ESP_DEV=$(sudo losetup --show -f ${esp_offset} ${esp_size} "${FLAGS_to}")
153 if [ -z "${ESP_DEV}" ]; then
154 die "No free loop devices."
155 fi
Will Drewryd3c938b2010-07-03 13:32:26 -0500156 else
157 # If it is a block device or something else, try to mount it anyway.
158 ESP_DEV="${FLAGS_to}"
159 fi
160fi
161
162ESP_FS_DIR=$(mktemp -d /tmp/esp.XXXXXX)
163cleanup() {
David James0103b592012-06-06 11:31:52 -0700164 local failed=()
165 if [[ -n "${ESP_FS_DIR}" ]]; then
166 sudo umount "${ESP_FS_DIR}" && rm -rf "${ESP_FS_DIR}" || failed+=( umount )
Jonathan Kliegmanae6baa22011-11-25 11:03:16 -0500167 fi
Will Drewryd3c938b2010-07-03 13:32:26 -0500168 if [[ -n "${ESP_DEV}" && -z "${ESP_DEV//\/dev\/loop*}" ]]; then
David James0103b592012-06-06 11:31:52 -0700169 sudo losetup -d "${ESP_DEV}" || failed+=( losetup )
Will Drewryd3c938b2010-07-03 13:32:26 -0500170 fi
David James0103b592012-06-06 11:31:52 -0700171 if [[ -n "${failed[*]}" ]]; then
172 die "Cleanup failed in ${failed[*]}"
173 fi
Will Drewryd3c938b2010-07-03 13:32:26 -0500174}
175trap cleanup EXIT
176sudo mount "${ESP_DEV}" "${ESP_FS_DIR}"
177
Sonny Rao3163cf02011-10-17 16:20:28 -0700178if [[ "${FLAGS_arch}" = "x86" || "${FLAGS_arch}" = "amd64" ]]; then
Will Drewryd3c938b2010-07-03 13:32:26 -0500179 # Populate the EFI bootloader configuration
180 sudo mkdir -p "${ESP_FS_DIR}/efi/boot"
181 sudo cp "${FLAGS_from}"/efi/boot/bootx64.efi \
182 "${ESP_FS_DIR}/efi/boot/bootx64.efi"
183 sudo cp "${FLAGS_from}/efi/boot/grub.cfg" \
184 "${ESP_FS_DIR}/efi/boot/grub.cfg"
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=
Will Drewryb910de82011-02-23 13:26:50 -0600197 old_root="%U+1"
Will Drewryd3c938b2010-07-03 13:32:26 -0500198 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 Jonesdfd36942011-08-10 15:59:36 -0400208 "${FLAGS_from}" \
209 "${FLAGS_to}"
Will Drewryd3c938b2010-07-03 13:32:26 -0500210
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
214 sudo umount "${ESP_FS_DIR}"
David James0103b592012-06-06 11:31:52 -0700215 rm -rf "${ESP_FS_DIR}"
216 ESP_FS_DIR=
Will Drewry721d94f2010-07-16 14:39:45 -0500217 sudo syslinux -d /syslinux "${ESP_DEV}"
Will Drewryd3c938b2010-07-03 13:32:26 -0500218 fi
219elif [[ "${FLAGS_arch}" = "arm" ]]; then
Kenneth Waterseca76462010-08-18 11:17:01 -0700220 # Copy u-boot script to ESP partition
Kenneth Waterscb39f992010-08-18 14:35:22 -0700221 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"
Olof Johansson296adf02011-12-22 15:05:26 -0800225 sudo cp -f "${FLAGS_from}"/vmlinuz "${ESP_FS_DIR}"/vmlinuz.uimg.A
226 sudo cp -f "${FLAGS_from}"/zImage "${ESP_FS_DIR}"/vmlinuz.A
Kenneth Waterscb39f992010-08-18 14:35:22 -0700227 fi
Will Drewryd3c938b2010-07-03 13:32:26 -0500228fi
229
230set +e