blob: 0b34172dcc3bb1305bfbd96f2879fe83db953e32 [file] [log] [blame]
Will Drewryd3c938b2010-07-03 13:32:26 -05001#!/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 Spencer798d75f2011-02-01 22:04:49 -080010# --- 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.
14find_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
27find_common_sh
28. "${SCRIPT_ROOT}/common.sh" || (echo "Unable to load common.sh" && exit 1)
29# --- END COMMON.SH BOILERPLATE ---
30
31# Need to be inside the chroot to load chromeos-common.sh
32assert_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 Drewryd3c938b2010-07-03 13:32:26 -050037
38get_default_board
39
40# Flags.
41DEFINE_string arch "x86" \
42 "The boot architecture: arm or x86. (Default: x86)"
43# TODO(wad) once extlinux is dead, we can remove this.
44DEFINE_boolean install_syslinux ${FLAGS_FALSE} \
45 "Controls whether syslinux is run on 'to'. (Default: false)"
46DEFINE_string from "/tmp/boot" \
47 "Path the legacy bootloader templates are copied from. (Default /tmp/boot)"
48DEFINE_string to "/tmp/esp.img" \
Kenneth Waterse3049de2010-09-30 14:20:34 -070049 "Path to esp image (Default: /tmp/esp.img)"
Will Drewry721d94f2010-07-16 14:39:45 -050050DEFINE_integer to_offset 0 \
51 "Offset in bytes into 'to' if it is a file (Default: 0)"
52DEFINE_integer to_size -1 \
53 "Size in bytes of 'to' to use if it is a file. -1 is ignored. (Default: -1)"
Will Drewryd3c938b2010-07-03 13:32:26 -050054DEFINE_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="".
58DEFINE_string kernel_partition "/tmp/vmlinuz.image" \
59 "Path to the signed kernel image. (Default: /tmp/vmlinuz.image)"
60DEFINE_string kernel_cmdline "" \
61 "Kernel commandline if no kernel_partition given. (Default: '')"
62DEFINE_string kernel_partition_offset "0" \
63 "Offset to the kernel partition [KERN-A] (Default: 0)"
64DEFINE_string kernel_partition_sectors "0" \
65 "Kernel partition sectors (Default: 0)"
66DEFINE_string usb_disk /dev/sdb3 \
Kenneth Waterse3049de2010-09-30 14:20:34 -070067 "Path syslinux should use to do a usb boot. Default: /dev/sdb3"
Will Drewryd3c938b2010-07-03 13:32:26 -050068
69# Parse flags
70FLAGS "$@" || exit 1
71eval set -- "${FLAGS_ARGV}"
72set -e
73
74# If not provided by chromeos-common.sh, this will update all of the
75# boot loader files (both A and B) with the data pulled
76# from the kernel_partition. The default boot target should
77# be set when the rootfs is stuffed.
78if ! type -p update_x86_bootloaders; then
79 update_x86_bootloaders() {
80 local old_root="$1" # e.g., sd%D%P
81 local kernel_cmdline="$2"
82 local esp_fs_dir="$3"
83 local template_dir="$4"
84
85 # Pull out the dm="" values
Will Drewry82780e52010-07-03 18:27:10 -070086 dm_table=
87 if echo "$kernel_cmdline" | grep -q 'dm="'; then
88 dm_table=$(echo "$kernel_cmdline" | sed -s 's/.*dm="\([^"]*\)".*/\1/')
89 fi
Will Drewryd3c938b2010-07-03 13:32:26 -050090
91 # Rewrite grub table
92 grub_dm_table_a=${dm_table//${old_root}/\$linuxpartA}
93 grub_dm_table_b=${dm_table//${old_root}/\$linuxpartB}
94 sed -e "s|DMTABLEA|${grub_dm_table_a}|g" \
95 -e "s|DMTABLEB|${grub_dm_table_b}|g" \
96 "${template_dir}"/efi/boot/grub.cfg |
97 sudo dd of="${esp_fs_dir}"/efi/boot/grub.cfg
98
99 # Rewrite syslinux DM_TABLE
Will Drewry78992a32010-07-21 14:02:20 -0500100 syslinux_dm_table_usb=${dm_table//\/dev\/${old_root}/${FLAGS_usb_disk}}
Will Drewryd3c938b2010-07-03 13:32:26 -0500101 sed -e "s|DMTABLEA|${syslinux_dm_table_usb}|g" \
102 "${template_dir}"/syslinux/usb.A.cfg |
103 sudo dd of="${esp_fs_dir}"/syslinux/usb.A.cfg
104
105 syslinux_dm_table_a=${dm_table//\/dev\/${old_root}/HDROOTA}
106 sed -e "s|DMTABLEA|${syslinux_dm_table_a}|g" \
107 "${template_dir}"/syslinux/root.A.cfg |
108 sudo dd of="${esp_fs_dir}"/syslinux/root.A.cfg
109
110 syslinux_dm_table_b=${dm_table//\/dev\/${old_root}/HDROOTB}
Will Drewry78992a32010-07-21 14:02:20 -0500111 sed -e "s|DMTABLEB|${syslinux_dm_table_b}|g" \
Will Drewryd3c938b2010-07-03 13:32:26 -0500112 "${template_dir}"/syslinux/root.B.cfg |
113 sudo dd of="${esp_fs_dir}"/syslinux/root.B.cfg
114
115 # Copy the vmlinuz's into place for syslinux
116 sudo cp -f "${template_dir}"/vmlinuz "${esp_fs_dir}"/syslinux/vmlinuz.A
117 sudo cp -f "${template_dir}"/vmlinuz "${esp_fs_dir}"/syslinux/vmlinuz.B
118
119 # The only work left for the installer is to pick the correct defaults
120 # and replace HDROOTA and HDROOTB with the correct /dev/sd%D%P.
121 }
122fi
123
124ESP_DEV=
125if [[ ! -e "${FLAGS_to}" ]]; then
126 error "The ESP doesn't exist"
127 # This shouldn't happen.
128 info "Creating a new esp image at ${FLAGS_to}" anyway.
129 # Create EFI System Partition to boot stock EFI BIOS (but not ChromeOS EFI
Kenneth Waterse3049de2010-09-30 14:20:34 -0700130 # BIOS). ARM uses this space to determine which partition is bootable.
Will Drewryd3c938b2010-07-03 13:32:26 -0500131 # NOTE: The size argument for mkfs.vfat is in 1024-byte blocks.
132 # We'll hard-code it to 16M for now.
133 ESP_BLOCKS=16384
134 /usr/sbin/mkfs.vfat -C "${FLAGS_to}" ${ESP_BLOCKS}
135 ESP_DEV=$(sudo losetup -f)
Will Drewry721d94f2010-07-16 14:39:45 -0500136 if [ -z "${ESP_DEV}" ]; then
137 die "No free loop devices."
138 fi
Will Drewryd3c938b2010-07-03 13:32:26 -0500139 sudo losetup "${ESP_DEV}" "${FLAGS_to}"
140else
141 if [[ -f "${FLAGS_to}" ]]; then
142 ESP_DEV=$(sudo losetup -f)
Will Drewry721d94f2010-07-16 14:39:45 -0500143 if [ -z "${ESP_DEV}" ]; then
144 die "No free loop devices."
145 fi
146
147 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
152 sudo losetup ${esp_offset} ${esp_size} "${ESP_DEV}" "${FLAGS_to}"
Will Drewryd3c938b2010-07-03 13:32:26 -0500153 else
154 # If it is a block device or something else, try to mount it anyway.
155 ESP_DEV="${FLAGS_to}"
156 fi
157fi
158
159ESP_FS_DIR=$(mktemp -d /tmp/esp.XXXXXX)
160cleanup() {
161 set +e
162 sudo umount "${ESP_FS_DIR}"
163 if [[ -n "${ESP_DEV}" && -z "${ESP_DEV//\/dev\/loop*}" ]]; then
164 sudo losetup -d "${ESP_DEV}"
165 fi
166 rm -rf "${ESP_FS_DIR}"
167}
168trap cleanup EXIT
169sudo mount "${ESP_DEV}" "${ESP_FS_DIR}"
170
171if [[ "${FLAGS_arch}" = "x86" ]]; then
172 # Populate the EFI bootloader configuration
173 sudo mkdir -p "${ESP_FS_DIR}/efi/boot"
174 sudo cp "${FLAGS_from}"/efi/boot/bootx64.efi \
175 "${ESP_FS_DIR}/efi/boot/bootx64.efi"
176 sudo cp "${FLAGS_from}/efi/boot/grub.cfg" \
177 "${ESP_FS_DIR}/efi/boot/grub.cfg"
178
179 # Prepopulate the syslinux directories too and update for verified boot values
180 # after the rootfs work is done.
181 sudo mkdir -p "${ESP_FS_DIR}"/syslinux
182 sudo cp -r "${FLAGS_from}"/syslinux/. "${ESP_FS_DIR}"/syslinux
183
184 # Stage both kernels with the only one we built.
185 sudo cp -f "${FLAGS_vmlinuz}" "${ESP_FS_DIR}"/syslinux/vmlinuz.A
186 sudo cp -f "${FLAGS_vmlinuz}" "${ESP_FS_DIR}"/syslinux/vmlinuz.B
187
188 # Extract kernel flags
189 kernel_cfg=
190 old_root="sd%D%P"
191 if [[ -n "${FLAGS_kernel_cmdline}" ]]; then
192 info "Using supplied kernel_cmdline to update templates."
193 kernel_cfg="${FLAGS_kernel_cmdline}"
194 elif [[ -n "${FLAGS_kernel_partition}" ]]; then
195 info "Extracting the kernel command line from ${FLAGS_kernel_partition}"
196 kernel_cfg=$(dump_kernel_config "${FLAGS_kernel_partition}")
197 fi
198 update_x86_bootloaders "${old_root}" \
199 "${kernel_cfg}" \
200 "${ESP_FS_DIR}" \
201 "${FLAGS_from}"
202
203 # Install the syslinux loader on the ESP image (part 12) so it is ready when
204 # we cut over from rootfs booting (extlinux).
205 if [[ ${FLAGS_install_syslinux} -eq ${FLAGS_TRUE} ]]; then
206 sudo umount "${ESP_FS_DIR}"
Will Drewry721d94f2010-07-16 14:39:45 -0500207 sudo syslinux -d /syslinux "${ESP_DEV}"
Hung-Te Lindf4fc212010-11-18 17:27:17 +0800208 # mount again for cleanup to free resource gracefully
209 sudo mount -o ro "${ESP_DEV}" "${ESP_FS_DIR}"
Will Drewryd3c938b2010-07-03 13:32:26 -0500210 fi
211elif [[ "${FLAGS_arch}" = "arm" ]]; then
Kenneth Waterseca76462010-08-18 11:17:01 -0700212 # Copy u-boot script to ESP partition
Kenneth Waterscb39f992010-08-18 14:35:22 -0700213 if [ -r "${FLAGS_from}/boot-A.scr.uimg" ]; then
214 sudo mkdir -p "${ESP_FS_DIR}/u-boot"
215 sudo cp "${FLAGS_from}/boot-A.scr.uimg" \
216 "${ESP_FS_DIR}/u-boot/boot.scr.uimg"
217 fi
Will Drewryd3c938b2010-07-03 13:32:26 -0500218fi
219
220set +e