blob: 8919559a8f63e662a13fc7670a6b0fa2bc2acb30 [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
57 dm_table=$(echo "$kernel_cmdline" | sed -s 's/.*dm="\([^"]*\)".*/\1/')
58
59 # Rewrite grub table
60 grub_dm_table_a=${dm_table//${old_root}/\$linuxpartA}
61 grub_dm_table_b=${dm_table//${old_root}/\$linuxpartB}
62 sed -e "s|DMTABLEA|${grub_dm_table_a}|g" \
63 -e "s|DMTABLEB|${grub_dm_table_b}|g" \
64 "${template_dir}"/efi/boot/grub.cfg |
65 sudo dd of="${esp_fs_dir}"/efi/boot/grub.cfg
66
67 # Rewrite syslinux DM_TABLE
68 usb_target="${FLAGS_usb_disk//\//\\\/}"
69 syslinux_dm_table_usb=${dm_table//\/dev\/${old_root}/${usb_target}}
70 sed -e "s|DMTABLEA|${syslinux_dm_table_usb}|g" \
71 "${template_dir}"/syslinux/usb.A.cfg |
72 sudo dd of="${esp_fs_dir}"/syslinux/usb.A.cfg
73
74 syslinux_dm_table_a=${dm_table//\/dev\/${old_root}/HDROOTA}
75 sed -e "s|DMTABLEA|${syslinux_dm_table_a}|g" \
76 "${template_dir}"/syslinux/root.A.cfg |
77 sudo dd of="${esp_fs_dir}"/syslinux/root.A.cfg
78
79 syslinux_dm_table_b=${dm_table//\/dev\/${old_root}/HDROOTB}
80 sed -e "s|DMTABLEA|${syslinux_dm_table_a}|g" \
81 "${template_dir}"/syslinux/root.B.cfg |
82 sudo dd of="${esp_fs_dir}"/syslinux/root.B.cfg
83
84 # Copy the vmlinuz's into place for syslinux
85 sudo cp -f "${template_dir}"/vmlinuz "${esp_fs_dir}"/syslinux/vmlinuz.A
86 sudo cp -f "${template_dir}"/vmlinuz "${esp_fs_dir}"/syslinux/vmlinuz.B
87
88 # The only work left for the installer is to pick the correct defaults
89 # and replace HDROOTA and HDROOTB with the correct /dev/sd%D%P.
90 }
91fi
92
93ESP_DEV=
94if [[ ! -e "${FLAGS_to}" ]]; then
95 error "The ESP doesn't exist"
96 # This shouldn't happen.
97 info "Creating a new esp image at ${FLAGS_to}" anyway.
98 # Create EFI System Partition to boot stock EFI BIOS (but not ChromeOS EFI
99 # BIOS). We only need this for x86, but it's simpler and safer to keep the
100 # disk images the same for both x86 and ARM.
101 # NOTE: The size argument for mkfs.vfat is in 1024-byte blocks.
102 # We'll hard-code it to 16M for now.
103 ESP_BLOCKS=16384
104 /usr/sbin/mkfs.vfat -C "${FLAGS_to}" ${ESP_BLOCKS}
105 ESP_DEV=$(sudo losetup -f)
106 test -z "${ESP_DEV}" && error "No free loop devices."
107 sudo losetup "${ESP_DEV}" "${FLAGS_to}"
108else
109 if [[ -f "${FLAGS_to}" ]]; then
110 ESP_DEV=$(sudo losetup -f)
111 test -z "${ESP_DEV}" && error "No free loop devices."
112 sudo losetup "${ESP_DEV}" "${FLAGS_to}"
113 else
114 # If it is a block device or something else, try to mount it anyway.
115 ESP_DEV="${FLAGS_to}"
116 fi
117fi
118
119ESP_FS_DIR=$(mktemp -d /tmp/esp.XXXXXX)
120cleanup() {
121 set +e
122 sudo umount "${ESP_FS_DIR}"
123 if [[ -n "${ESP_DEV}" && -z "${ESP_DEV//\/dev\/loop*}" ]]; then
124 sudo losetup -d "${ESP_DEV}"
125 fi
126 rm -rf "${ESP_FS_DIR}"
127}
128trap cleanup EXIT
129sudo mount "${ESP_DEV}" "${ESP_FS_DIR}"
130
131if [[ "${FLAGS_arch}" = "x86" ]]; then
132 # Populate the EFI bootloader configuration
133 sudo mkdir -p "${ESP_FS_DIR}/efi/boot"
134 sudo cp "${FLAGS_from}"/efi/boot/bootx64.efi \
135 "${ESP_FS_DIR}/efi/boot/bootx64.efi"
136 sudo cp "${FLAGS_from}/efi/boot/grub.cfg" \
137 "${ESP_FS_DIR}/efi/boot/grub.cfg"
138
139 # Prepopulate the syslinux directories too and update for verified boot values
140 # after the rootfs work is done.
141 sudo mkdir -p "${ESP_FS_DIR}"/syslinux
142 sudo cp -r "${FLAGS_from}"/syslinux/. "${ESP_FS_DIR}"/syslinux
143
144 # Stage both kernels with the only one we built.
145 sudo cp -f "${FLAGS_vmlinuz}" "${ESP_FS_DIR}"/syslinux/vmlinuz.A
146 sudo cp -f "${FLAGS_vmlinuz}" "${ESP_FS_DIR}"/syslinux/vmlinuz.B
147
148 # Extract kernel flags
149 kernel_cfg=
150 old_root="sd%D%P"
151 if [[ -n "${FLAGS_kernel_cmdline}" ]]; then
152 info "Using supplied kernel_cmdline to update templates."
153 kernel_cfg="${FLAGS_kernel_cmdline}"
154 elif [[ -n "${FLAGS_kernel_partition}" ]]; then
155 info "Extracting the kernel command line from ${FLAGS_kernel_partition}"
156 kernel_cfg=$(dump_kernel_config "${FLAGS_kernel_partition}")
157 fi
158 update_x86_bootloaders "${old_root}" \
159 "${kernel_cfg}" \
160 "${ESP_FS_DIR}" \
161 "${FLAGS_from}"
162
163 # Install the syslinux loader on the ESP image (part 12) so it is ready when
164 # we cut over from rootfs booting (extlinux).
165 if [[ ${FLAGS_install_syslinux} -eq ${FLAGS_TRUE} ]]; then
166 sudo umount "${ESP_FS_DIR}"
167 sudo syslinux -d /syslinux "${FLAGS_to}"
168 fi
169elif [[ "${FLAGS_arch}" = "arm" ]]; then
170 # Extract kernel flags
171 kernel_cfg=
172 old_root="sd%D%P"
173 if [[ -n "${FLAGS_kernel_cmdline}" ]]; then
174 info "Using supplied kernel_cmdline to update templates."
175 kernel_cfg="${FLAGS_kernel_cmdline}"
176 elif [[ -n "${FLAGS_kernel_partition}" ]]; then
177 info "Extracting the kernel command line from ${FLAGS_kernel_partition}"
178 kernel_cfg=$(dump_kernel_config "${kernel_partition}")
179 fi
180 dm_table=$(echo "$kernel_cfg" | sed -s 's/.*dm="\([^"]*\)".*/\1/')
181 # TODO(wad) assume usb_disk contains the arm boot location for now.
182 new_root="${FLAGS_usb_disk}"
183 info "Replacing dm slave devices with /dev/${new_root}"
184 dm_table="${dm_table//ROOT_DEV/\/dev\/${new_root}}"
185 dm_table="${dm_table//HASH_DEV/\/dev\/${new_root}}"
186
187 warn "FIXME: cannot replace root= here for the arm bootloader yet."
188 dm_table="" # TODO(wad) Clear it until we can fix root=/dev/dm-0
189
190 local device=1
191 local MBR_SCRIPT_UIMG=$(make_arm_mbr \
192 ${FLAGS_kernel_partition_offset} \
193 ${FLAGS_kernel_partition_sectors} \
194 ${device} \
195 "'dm=\"${dm_table}\"'")
196 sudo dd bs=1 count=`stat --printf="%s" ${MBR_SCRIPT_UIMG}` \
197 if="$MBR_SCRIPT_UIMG" of=${FLAGS_to} conv=notrunc
198 info "Emitted new ARM MBR to ${FLAGS_to}"
199fi
200
201set +e