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