| OLD | NEW |
| (Empty) |
| 1 #!/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 . "/usr/lib/crosutils/common.sh" | |
| 11 . "/usr/lib/installer/chromeos-common.sh" # installer | |
| 12 | |
| 13 get_default_board | |
| 14 | |
| 15 # Flags. | |
| 16 DEFINE_string arch "x86" \ | |
| 17 "The boot architecture: arm or x86. (Default: x86)" | |
| 18 # TODO(wad) once extlinux is dead, we can remove this. | |
| 19 DEFINE_boolean install_syslinux ${FLAGS_FALSE} \ | |
| 20 "Controls whether syslinux is run on 'to'. (Default: false)" | |
| 21 DEFINE_string from "/tmp/boot" \ | |
| 22 "Path the legacy bootloader templates are copied from. (Default /tmp/boot)" | |
| 23 DEFINE_string to "/tmp/esp.img" \ | |
| 24 "Path to esp image or ARM output MBR (Default: /tmp/esp.img)" | |
| 25 DEFINE_integer to_offset 0 \ | |
| 26 "Offset in bytes into 'to' if it is a file (Default: 0)" | |
| 27 DEFINE_integer to_size -1 \ | |
| 28 "Size in bytes of 'to' to use if it is a file. -1 is ignored. (Default: -1)" | |
| 29 DEFINE_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="". | |
| 33 DEFINE_string kernel_partition "/tmp/vmlinuz.image" \ | |
| 34 "Path to the signed kernel image. (Default: /tmp/vmlinuz.image)" | |
| 35 DEFINE_string kernel_cmdline "" \ | |
| 36 "Kernel commandline if no kernel_partition given. (Default: '')" | |
| 37 DEFINE_string kernel_partition_offset "0" \ | |
| 38 "Offset to the kernel partition [KERN-A] (Default: 0)" | |
| 39 DEFINE_string kernel_partition_sectors "0" \ | |
| 40 "Kernel partition sectors (Default: 0)" | |
| 41 DEFINE_string usb_disk /dev/sdb3 \ | |
| 42 "Path syslinux should use to do a usb (or arm!) boot. Default: /dev/sdb3" | |
| 43 | |
| 44 # Parse flags | |
| 45 FLAGS "$@" || exit 1 | |
| 46 eval set -- "${FLAGS_ARGV}" | |
| 47 set -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. | |
| 53 if ! 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 | |
| 61 dm_table= | |
| 62 if echo "$kernel_cmdline" | grep -q 'dm="'; then | |
| 63 dm_table=$(echo "$kernel_cmdline" | sed -s 's/.*dm="\([^"]*\)".*/\1/') | |
| 64 fi | |
| 65 | |
| 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 syslinux_dm_table_usb=${dm_table//\/dev\/${old_root}/${FLAGS_usb_disk}} | |
| 76 sed -e "s|DMTABLEA|${syslinux_dm_table_usb}|g" \ | |
| 77 "${template_dir}"/syslinux/usb.A.cfg | | |
| 78 sudo dd of="${esp_fs_dir}"/syslinux/usb.A.cfg | |
| 79 | |
| 80 syslinux_dm_table_a=${dm_table//\/dev\/${old_root}/HDROOTA} | |
| 81 sed -e "s|DMTABLEA|${syslinux_dm_table_a}|g" \ | |
| 82 "${template_dir}"/syslinux/root.A.cfg | | |
| 83 sudo dd of="${esp_fs_dir}"/syslinux/root.A.cfg | |
| 84 | |
| 85 syslinux_dm_table_b=${dm_table//\/dev\/${old_root}/HDROOTB} | |
| 86 sed -e "s|DMTABLEB|${syslinux_dm_table_b}|g" \ | |
| 87 "${template_dir}"/syslinux/root.B.cfg | | |
| 88 sudo dd of="${esp_fs_dir}"/syslinux/root.B.cfg | |
| 89 | |
| 90 # Copy the vmlinuz's into place for syslinux | |
| 91 sudo cp -f "${template_dir}"/vmlinuz "${esp_fs_dir}"/syslinux/vmlinuz.A | |
| 92 sudo cp -f "${template_dir}"/vmlinuz "${esp_fs_dir}"/syslinux/vmlinuz.B | |
| 93 | |
| 94 # The only work left for the installer is to pick the correct defaults | |
| 95 # and replace HDROOTA and HDROOTB with the correct /dev/sd%D%P. | |
| 96 } | |
| 97 fi | |
| 98 | |
| 99 ESP_DEV= | |
| 100 if [[ ! -e "${FLAGS_to}" ]]; then | |
| 101 error "The ESP doesn't exist" | |
| 102 # This shouldn't happen. | |
| 103 info "Creating a new esp image at ${FLAGS_to}" anyway. | |
| 104 # Create EFI System Partition to boot stock EFI BIOS (but not ChromeOS EFI | |
| 105 # BIOS). We only need this for x86, but it's simpler and safer to keep the | |
| 106 # disk images the same for both x86 and ARM. | |
| 107 # NOTE: The size argument for mkfs.vfat is in 1024-byte blocks. | |
| 108 # We'll hard-code it to 16M for now. | |
| 109 ESP_BLOCKS=16384 | |
| 110 /usr/sbin/mkfs.vfat -C "${FLAGS_to}" ${ESP_BLOCKS} | |
| 111 ESP_DEV=$(sudo losetup -f) | |
| 112 if [ -z "${ESP_DEV}" ]; then | |
| 113 die "No free loop devices." | |
| 114 fi | |
| 115 sudo losetup "${ESP_DEV}" "${FLAGS_to}" | |
| 116 else | |
| 117 if [[ -f "${FLAGS_to}" ]]; then | |
| 118 ESP_DEV=$(sudo losetup -f) | |
| 119 if [ -z "${ESP_DEV}" ]; then | |
| 120 die "No free loop devices." | |
| 121 fi | |
| 122 | |
| 123 esp_offset="--offset ${FLAGS_to_offset}" | |
| 124 esp_size="--sizelimit ${FLAGS_to_size}" | |
| 125 if [ ${FLAGS_to_size} -lt 0 ]; then | |
| 126 esp_size= | |
| 127 fi | |
| 128 sudo losetup ${esp_offset} ${esp_size} "${ESP_DEV}" "${FLAGS_to}" | |
| 129 else | |
| 130 # If it is a block device or something else, try to mount it anyway. | |
| 131 ESP_DEV="${FLAGS_to}" | |
| 132 fi | |
| 133 fi | |
| 134 | |
| 135 ESP_FS_DIR=$(mktemp -d /tmp/esp.XXXXXX) | |
| 136 cleanup() { | |
| 137 set +e | |
| 138 sudo umount "${ESP_FS_DIR}" | |
| 139 if [[ -n "${ESP_DEV}" && -z "${ESP_DEV//\/dev\/loop*}" ]]; then | |
| 140 sudo losetup -d "${ESP_DEV}" | |
| 141 fi | |
| 142 rm -rf "${ESP_FS_DIR}" | |
| 143 } | |
| 144 trap cleanup EXIT | |
| 145 sudo mount "${ESP_DEV}" "${ESP_FS_DIR}" | |
| 146 | |
| 147 if [[ "${FLAGS_arch}" = "x86" ]]; then | |
| 148 # Populate the EFI bootloader configuration | |
| 149 sudo mkdir -p "${ESP_FS_DIR}/efi/boot" | |
| 150 sudo cp "${FLAGS_from}"/efi/boot/bootx64.efi \ | |
| 151 "${ESP_FS_DIR}/efi/boot/bootx64.efi" | |
| 152 sudo cp "${FLAGS_from}/efi/boot/grub.cfg" \ | |
| 153 "${ESP_FS_DIR}/efi/boot/grub.cfg" | |
| 154 | |
| 155 # Prepopulate the syslinux directories too and update for verified boot values | |
| 156 # after the rootfs work is done. | |
| 157 sudo mkdir -p "${ESP_FS_DIR}"/syslinux | |
| 158 sudo cp -r "${FLAGS_from}"/syslinux/. "${ESP_FS_DIR}"/syslinux | |
| 159 | |
| 160 # Stage both kernels with the only one we built. | |
| 161 sudo cp -f "${FLAGS_vmlinuz}" "${ESP_FS_DIR}"/syslinux/vmlinuz.A | |
| 162 sudo cp -f "${FLAGS_vmlinuz}" "${ESP_FS_DIR}"/syslinux/vmlinuz.B | |
| 163 | |
| 164 # Extract kernel flags | |
| 165 kernel_cfg= | |
| 166 old_root="sd%D%P" | |
| 167 if [[ -n "${FLAGS_kernel_cmdline}" ]]; then | |
| 168 info "Using supplied kernel_cmdline to update templates." | |
| 169 kernel_cfg="${FLAGS_kernel_cmdline}" | |
| 170 elif [[ -n "${FLAGS_kernel_partition}" ]]; then | |
| 171 info "Extracting the kernel command line from ${FLAGS_kernel_partition}" | |
| 172 kernel_cfg=$(dump_kernel_config "${FLAGS_kernel_partition}") | |
| 173 fi | |
| 174 update_x86_bootloaders "${old_root}" \ | |
| 175 "${kernel_cfg}" \ | |
| 176 "${ESP_FS_DIR}" \ | |
| 177 "${FLAGS_from}" | |
| 178 | |
| 179 # Install the syslinux loader on the ESP image (part 12) so it is ready when | |
| 180 # we cut over from rootfs booting (extlinux). | |
| 181 if [[ ${FLAGS_install_syslinux} -eq ${FLAGS_TRUE} ]]; then | |
| 182 sudo umount "${ESP_FS_DIR}" | |
| 183 sudo syslinux -d /syslinux "${ESP_DEV}" | |
| 184 fi | |
| 185 elif [[ "${FLAGS_arch}" = "arm" ]]; then | |
| 186 # Extract kernel flags | |
| 187 kernel_cfg= | |
| 188 old_root="sd%D%P" | |
| 189 if [[ -n "${FLAGS_kernel_cmdline}" ]]; then | |
| 190 info "Using supplied kernel_cmdline to update templates." | |
| 191 kernel_cfg="${FLAGS_kernel_cmdline}" | |
| 192 elif [[ -n "${FLAGS_kernel_partition}" ]]; then | |
| 193 info "Extracting the kernel command line from ${FLAGS_kernel_partition}" | |
| 194 kernel_cfg=$(dump_kernel_config "${kernel_partition}") | |
| 195 fi | |
| 196 dm_table= | |
| 197 if echo "$kernel_cfg" | grep -q 'dm="'; then | |
| 198 dm_table=$(echo "$kernel_cfg" | sed -s 's/.*dm="\([^"]*\)".*/\1/') | |
| 199 fi | |
| 200 # TODO(wad) assume usb_disk contains the arm boot location for now. | |
| 201 new_root="${FLAGS_usb_disk}" | |
| 202 info "Replacing dm slave devices with /dev/${new_root}" | |
| 203 dm_table="${dm_table//ROOT_DEV/\/dev\/${new_root}}" | |
| 204 dm_table="${dm_table//HASH_DEV/\/dev\/${new_root}}" | |
| 205 | |
| 206 warn "FIXME: cannot replace root= here for the arm bootloader yet." | |
| 207 dm_table="" # TODO(wad) Clear it until we can fix root=/dev/dm-0 | |
| 208 | |
| 209 # Copy u-boot script to ESP partition | |
| 210 if [ -r "${FLAGS_from}/boot-A.scr.uimg" ]; then | |
| 211 sudo mkdir -p "${ESP_FS_DIR}/u-boot" | |
| 212 sudo cp "${FLAGS_from}/boot-A.scr.uimg" \ | |
| 213 "${ESP_FS_DIR}/u-boot/boot.scr.uimg" | |
| 214 fi | |
| 215 fi | |
| 216 | |
| 217 set +e | |
| OLD | NEW |