blob: 5b216b7c56f01468daf3a8c11aea90396d327a61 [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
10. "$(dirname "$0")/common.sh"
11. "$(dirname "$0")/chromeos-common.sh" # installer
12
13get_default_board
14
15# Flags.
16DEFINE_string arch "x86" \
17 "The boot architecture: arm or x86. (Default: x86)"
18# TODO(wad) once extlinux is dead, we can remove this.
19DEFINE_boolean install_syslinux ${FLAGS_FALSE} \
20 "Controls whether syslinux is run on 'to'. (Default: false)"
21DEFINE_string from "/tmp/boot" \
22 "Path the legacy bootloader templates are copied from. (Default /tmp/boot)"
23DEFINE_string to "/tmp/esp.img" \
24 "Path to esp image or ARM output MBR (Default: /tmp/esp.img)"
25DEFINE_string vmlinuz "/tmp/vmlinuz" \
26 "Path to the vmlinuz file to use (Default: /tmp/vmlinuz)"
27# The kernel_partition and the kernel_cmdline each are used to supply
28# verified boot configuration: dm="".
29DEFINE_string kernel_partition "/tmp/vmlinuz.image" \
30 "Path to the signed kernel image. (Default: /tmp/vmlinuz.image)"
31DEFINE_string kernel_cmdline "" \
32 "Kernel commandline if no kernel_partition given. (Default: '')"
33DEFINE_string kernel_partition_offset "0" \
34 "Offset to the kernel partition [KERN-A] (Default: 0)"
35DEFINE_string kernel_partition_sectors "0" \
36 "Kernel partition sectors (Default: 0)"
37DEFINE_string usb_disk /dev/sdb3 \
38 "Path syslinux should use to do a usb (or arm!) boot. Default: /dev/sdb3"
39
40# Parse flags
41FLAGS "$@" || exit 1
42eval set -- "${FLAGS_ARGV}"
43set -e
44
45# If not provided by chromeos-common.sh, this will update all of the
46# boot loader files (both A and B) with the data pulled
47# from the kernel_partition. The default boot target should
48# be set when the rootfs is stuffed.
49if ! type -p update_x86_bootloaders; then
50 update_x86_bootloaders() {
51 local old_root="$1" # e.g., sd%D%P
52 local kernel_cmdline="$2"
53 local esp_fs_dir="$3"
54 local template_dir="$4"
55
56 # Pull out the dm="" values
Will Drewry82780e52010-07-03 18:27:10 -070057 dm_table=
58 if echo "$kernel_cmdline" | grep -q 'dm="'; then
59 dm_table=$(echo "$kernel_cmdline" | sed -s 's/.*dm="\([^"]*\)".*/\1/')
60 fi
Will Drewryd3c938b2010-07-03 13:32:26 -050061
62 # Rewrite grub table
63 grub_dm_table_a=${dm_table//${old_root}/\$linuxpartA}
64 grub_dm_table_b=${dm_table//${old_root}/\$linuxpartB}
65 sed -e "s|DMTABLEA|${grub_dm_table_a}|g" \
66 -e "s|DMTABLEB|${grub_dm_table_b}|g" \
67 "${template_dir}"/efi/boot/grub.cfg |
68 sudo dd of="${esp_fs_dir}"/efi/boot/grub.cfg
69
70 # Rewrite syslinux DM_TABLE
71 usb_target="${FLAGS_usb_disk//\//\\\/}"
72 syslinux_dm_table_usb=${dm_table//\/dev\/${old_root}/${usb_target}}
73 sed -e "s|DMTABLEA|${syslinux_dm_table_usb}|g" \
74 "${template_dir}"/syslinux/usb.A.cfg |
75 sudo dd of="${esp_fs_dir}"/syslinux/usb.A.cfg
76
77 syslinux_dm_table_a=${dm_table//\/dev\/${old_root}/HDROOTA}
78 sed -e "s|DMTABLEA|${syslinux_dm_table_a}|g" \
79 "${template_dir}"/syslinux/root.A.cfg |
80 sudo dd of="${esp_fs_dir}"/syslinux/root.A.cfg
81
82 syslinux_dm_table_b=${dm_table//\/dev\/${old_root}/HDROOTB}
83 sed -e "s|DMTABLEA|${syslinux_dm_table_a}|g" \
84 "${template_dir}"/syslinux/root.B.cfg |
85 sudo dd of="${esp_fs_dir}"/syslinux/root.B.cfg
86
87 # Copy the vmlinuz's into place for syslinux
88 sudo cp -f "${template_dir}"/vmlinuz "${esp_fs_dir}"/syslinux/vmlinuz.A
89 sudo cp -f "${template_dir}"/vmlinuz "${esp_fs_dir}"/syslinux/vmlinuz.B
90
91 # The only work left for the installer is to pick the correct defaults
92 # and replace HDROOTA and HDROOTB with the correct /dev/sd%D%P.
93 }
94fi
95
96ESP_DEV=
97if [[ ! -e "${FLAGS_to}" ]]; then
98 error "The ESP doesn't exist"
99 # This shouldn't happen.
100 info "Creating a new esp image at ${FLAGS_to}" anyway.
101 # Create EFI System Partition to boot stock EFI BIOS (but not ChromeOS EFI
102 # BIOS). We only need this for x86, but it's simpler and safer to keep the
103 # disk images the same for both x86 and ARM.
104 # NOTE: The size argument for mkfs.vfat is in 1024-byte blocks.
105 # We'll hard-code it to 16M for now.
106 ESP_BLOCKS=16384
107 /usr/sbin/mkfs.vfat -C "${FLAGS_to}" ${ESP_BLOCKS}
108 ESP_DEV=$(sudo losetup -f)
109 test -z "${ESP_DEV}" && error "No free loop devices."
110 sudo losetup "${ESP_DEV}" "${FLAGS_to}"
111else
112 if [[ -f "${FLAGS_to}" ]]; then
113 ESP_DEV=$(sudo losetup -f)
114 test -z "${ESP_DEV}" && error "No free loop devices."
115 sudo losetup "${ESP_DEV}" "${FLAGS_to}"
116 else
117 # If it is a block device or something else, try to mount it anyway.
118 ESP_DEV="${FLAGS_to}"
119 fi
120fi
121
122ESP_FS_DIR=$(mktemp -d /tmp/esp.XXXXXX)
123cleanup() {
124 set +e
125 sudo umount "${ESP_FS_DIR}"
126 if [[ -n "${ESP_DEV}" && -z "${ESP_DEV//\/dev\/loop*}" ]]; then
127 sudo losetup -d "${ESP_DEV}"
128 fi
129 rm -rf "${ESP_FS_DIR}"
130}
131trap cleanup EXIT
132sudo mount "${ESP_DEV}" "${ESP_FS_DIR}"
133
134if [[ "${FLAGS_arch}" = "x86" ]]; then
135 # Populate the EFI bootloader configuration
136 sudo mkdir -p "${ESP_FS_DIR}/efi/boot"
137 sudo cp "${FLAGS_from}"/efi/boot/bootx64.efi \
138 "${ESP_FS_DIR}/efi/boot/bootx64.efi"
139 sudo cp "${FLAGS_from}/efi/boot/grub.cfg" \
140 "${ESP_FS_DIR}/efi/boot/grub.cfg"
141
142 # Prepopulate the syslinux directories too and update for verified boot values
143 # after the rootfs work is done.
144 sudo mkdir -p "${ESP_FS_DIR}"/syslinux
145 sudo cp -r "${FLAGS_from}"/syslinux/. "${ESP_FS_DIR}"/syslinux
146
147 # Stage both kernels with the only one we built.
148 sudo cp -f "${FLAGS_vmlinuz}" "${ESP_FS_DIR}"/syslinux/vmlinuz.A
149 sudo cp -f "${FLAGS_vmlinuz}" "${ESP_FS_DIR}"/syslinux/vmlinuz.B
150
151 # Extract kernel flags
152 kernel_cfg=
153 old_root="sd%D%P"
154 if [[ -n "${FLAGS_kernel_cmdline}" ]]; then
155 info "Using supplied kernel_cmdline to update templates."
156 kernel_cfg="${FLAGS_kernel_cmdline}"
157 elif [[ -n "${FLAGS_kernel_partition}" ]]; then
158 info "Extracting the kernel command line from ${FLAGS_kernel_partition}"
159 kernel_cfg=$(dump_kernel_config "${FLAGS_kernel_partition}")
160 fi
161 update_x86_bootloaders "${old_root}" \
162 "${kernel_cfg}" \
163 "${ESP_FS_DIR}" \
164 "${FLAGS_from}"
165
166 # Install the syslinux loader on the ESP image (part 12) so it is ready when
167 # we cut over from rootfs booting (extlinux).
168 if [[ ${FLAGS_install_syslinux} -eq ${FLAGS_TRUE} ]]; then
169 sudo umount "${ESP_FS_DIR}"
170 sudo syslinux -d /syslinux "${FLAGS_to}"
171 fi
172elif [[ "${FLAGS_arch}" = "arm" ]]; then
173 # Extract kernel flags
174 kernel_cfg=
175 old_root="sd%D%P"
176 if [[ -n "${FLAGS_kernel_cmdline}" ]]; then
177 info "Using supplied kernel_cmdline to update templates."
178 kernel_cfg="${FLAGS_kernel_cmdline}"
179 elif [[ -n "${FLAGS_kernel_partition}" ]]; then
180 info "Extracting the kernel command line from ${FLAGS_kernel_partition}"
181 kernel_cfg=$(dump_kernel_config "${kernel_partition}")
182 fi
Will Drewry82780e52010-07-03 18:27:10 -0700183 dm_table=
184 if echo "$kernel_cfg" | grep -q 'dm="'; then
185 dm_table=$(echo "$kernel_cfg" | sed -s 's/.*dm="\([^"]*\)".*/\1/')
186 fi
Will Drewryd3c938b2010-07-03 13:32:26 -0500187 # TODO(wad) assume usb_disk contains the arm boot location for now.
188 new_root="${FLAGS_usb_disk}"
189 info "Replacing dm slave devices with /dev/${new_root}"
190 dm_table="${dm_table//ROOT_DEV/\/dev\/${new_root}}"
191 dm_table="${dm_table//HASH_DEV/\/dev\/${new_root}}"
192
193 warn "FIXME: cannot replace root= here for the arm bootloader yet."
194 dm_table="" # TODO(wad) Clear it until we can fix root=/dev/dm-0
195
196 local device=1
197 local MBR_SCRIPT_UIMG=$(make_arm_mbr \
198 ${FLAGS_kernel_partition_offset} \
199 ${FLAGS_kernel_partition_sectors} \
200 ${device} \
201 "'dm=\"${dm_table}\"'")
202 sudo dd bs=1 count=`stat --printf="%s" ${MBR_SCRIPT_UIMG}` \
Will Drewry82780e52010-07-03 18:27:10 -0700203 if="$MBR_SCRIPT_UIMG" of=${FLAGS_to}
Will Drewryd3c938b2010-07-03 13:32:26 -0500204 info "Emitted new ARM MBR to ${FLAGS_to}"
205fi
206
207set +e