Will Drewry | 69563b7 | 2010-06-24 16:12:58 -0500 | [diff] [blame] | 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 signed kernel image |
| 8 | |
| 9 | . "$(dirname "$0")/common.sh" |
| 10 | |
| 11 | get_default_board |
| 12 | |
| 13 | # Flags. |
| 14 | DEFINE_string arch "x86" \ |
| 15 | "The boot architecture: arm or x86. (Default: x86)" |
| 16 | DEFINE_string to "/tmp/vmlinuz.image" \ |
| 17 | "The path to the kernel image to be created. (Default: /tmp/vmlinuz.image)" |
Louis Yung-Chieh Lo | 3602040 | 2010-07-05 13:23:34 +0800 | [diff] [blame] | 18 | DEFINE_string hd_vblock "/tmp/vmlinuz_hd.vblock" \ |
| 19 | "The path to the installed kernel's vblock (Default: /tmp/vmlinuz_hd.vblock)" |
Will Drewry | 69563b7 | 2010-06-24 16:12:58 -0500 | [diff] [blame] | 20 | DEFINE_string vmlinuz "vmlinuz" \ |
| 21 | "The path to the kernel (Default: vmlinuz)" |
| 22 | DEFINE_string working_dir "/tmp/vmlinuz.working" \ |
| 23 | "Working directory for in-progress files. (Default: /tmp/vmlinuz.working)" |
| 24 | DEFINE_boolean keep_work ${FLAGS_FALSE} \ |
| 25 | "Keep temporary files (*.keyblock, *.vbpubk). (Default: false)" |
| 26 | DEFINE_string keys_dir "${SRC_ROOT}/platform/vboot_reference/tests/testkeys" \ |
Bill Richardson | 2ace49e | 2010-07-01 10:23:27 -0700 | [diff] [blame] | 27 | "Directory with the RSA signing keys. (Defaults to test keys)" |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 28 | # Note, to enable verified boot, the caller would manually pass: |
Will Drewry | 69563b7 | 2010-06-24 16:12:58 -0500 | [diff] [blame] | 29 | # --boot_args='dm="... /dev/sd%D%P /dev/sd%D%P ..." \ |
| 30 | # --root=/dev/dm-0 |
| 31 | DEFINE_string boot_args "noinitrd" \ |
| 32 | "Additional boot arguments to pass to the commandline (Default: noinitrd)" |
| 33 | DEFINE_string root "/dev/sd%D%P" \ |
| 34 | "Expected device root (Default: root=/dev/sd%D%P)" |
| 35 | |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 36 | # If provided, will automatically add verified boot arguments. |
| 37 | DEFINE_string rootfs_image "" \ |
| 38 | "Optional path to the rootfs device or image.(Default: \"\")" |
| 39 | DEFINE_string rootfs_hash "" \ |
| 40 | "Optional path to output the rootfs hash to. (Default: \"\")" |
Will Drewry | 1670d48 | 2010-07-09 13:08:38 -0700 | [diff] [blame] | 41 | DEFINE_integer verity_error_behavior 2 \ |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 42 | "Verified boot error behavior [0: I/O errors, 1: reboot, 2: nothing] \ |
| 43 | (Default: 2)" |
Will Drewry | 1670d48 | 2010-07-09 13:08:38 -0700 | [diff] [blame] | 44 | DEFINE_integer verity_tree_depth 1 \ |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 45 | "Optional Verified boot hash tree depth. (Default: 1)" |
Will Drewry | 1670d48 | 2010-07-09 13:08:38 -0700 | [diff] [blame] | 46 | DEFINE_integer verity_max_ios 1024 \ |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 47 | "Optional number of outstanding I/O operations. (Default: 1024)" |
Will Drewry | 1670d48 | 2010-07-09 13:08:38 -0700 | [diff] [blame] | 48 | DEFINE_string verity_hash_alg "sha1" \ |
| 49 | "Cryptographic hash algorithm used for dm-verity. (Default: sha1)" |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 50 | |
Will Drewry | 69563b7 | 2010-06-24 16:12:58 -0500 | [diff] [blame] | 51 | # Parse flags |
| 52 | FLAGS "$@" || exit 1 |
| 53 | eval set -- "${FLAGS_ARGV}" |
| 54 | |
| 55 | # Die on error |
| 56 | set -e |
| 57 | |
Will Drewry | 1670d48 | 2010-07-09 13:08:38 -0700 | [diff] [blame] | 58 | verity_args= |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 59 | # Even with a rootfs_image, root= is not changed unless specified. |
| 60 | if [[ -n "${FLAGS_rootfs_image}" && -n "${FLAGS_rootfs_hash}" ]]; then |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 61 | # Gets the number of blocks. 4096 byte blocks _are_ expected. |
| 62 | root_fs_blocks=$(sudo dumpe2fs "${FLAGS_rootfs_image}" 2> /dev/null | |
| 63 | grep "Block count" | |
| 64 | tr -d ' ' | |
| 65 | cut -f2 -d:) |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 66 | root_fs_block_sz=$(sudo dumpe2fs "${FLAGS_rootfs_image}" 2> /dev/null | |
| 67 | grep "Block size" | |
| 68 | tr -d ' ' | |
| 69 | cut -f2 -d:) |
Will Drewry | 78992a3 | 2010-07-21 14:02:20 -0500 | [diff] [blame^] | 70 | info "rootfs is ${root_fs_blocks} blocks of ${root_fs_block_sz} bytes" |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 71 | if [[ ${root_fs_block_sz} -ne 4096 ]]; then |
| 72 | error "Root file system blocks are not 4k!" |
| 73 | fi |
| 74 | |
| 75 | info "Generating root fs hash tree." |
| 76 | # Runs as sudo in case the image is a block device. |
Will Drewry | 1670d48 | 2010-07-09 13:08:38 -0700 | [diff] [blame] | 77 | table=$(sudo verity create ${FLAGS_verity_tree_depth} \ |
| 78 | ${FLAGS_verity_hash_alg} \ |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 79 | ${FLAGS_rootfs_image} \ |
| 80 | ${root_fs_blocks} \ |
| 81 | ${FLAGS_rootfs_hash}) |
Will Drewry | 821d07c | 2010-07-03 17:14:58 -0700 | [diff] [blame] | 82 | if [[ -f "${FLAGS_rootfs_hash}" ]]; then |
| 83 | sudo chmod a+r "${FLAGS_rootfs_hash}" |
| 84 | fi |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 85 | # Don't claim the root device unless the root= flag is pointed to |
| 86 | # the verified boot device. Doing so will claim /dev/sdDP out from |
| 87 | # under the system. |
| 88 | if [[ ${FLAGS_root} = "/dev/dm-0" ]]; then |
Will Drewry | 78992a3 | 2010-07-21 14:02:20 -0500 | [diff] [blame^] | 89 | table=${table//HASH_DEV//dev/sd%D%P} |
| 90 | table=${table//ROOT_DEV//dev/sd%D%P} |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 91 | fi |
Will Drewry | 78992a3 | 2010-07-21 14:02:20 -0500 | [diff] [blame^] | 92 | verity_args="dm=\"vroot none ro,${table}\"" |
Will Drewry | 1670d48 | 2010-07-09 13:08:38 -0700 | [diff] [blame] | 93 | info "dm-verity configuration: ${verity_args}" |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 94 | fi |
| 95 | |
| 96 | mkdir -p "${FLAGS_working_dir}" |
| 97 | cat <<EOF > "${FLAGS_working_dir}/boot.config" |
| 98 | root=${FLAGS_root} |
Will Drewry | 1670d48 | 2010-07-09 13:08:38 -0700 | [diff] [blame] | 99 | dm_verity.error_behavior=${FLAGS_verity_error_behavior} |
| 100 | dm_verity.max_bios=${FLAGS_verity_max_ios} |
| 101 | ${verity_args} |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 102 | ${FLAGS_boot_args} |
| 103 | EOF |
| 104 | |
| 105 | WORK="${WORK} ${FLAGS_working_dir}/boot.config" |
| 106 | info "Emitted cross-platform boot params to ${FLAGS_working_dir}/boot.config" |
| 107 | |
Will Drewry | 69563b7 | 2010-06-24 16:12:58 -0500 | [diff] [blame] | 108 | # FIXME: At the moment, we're working on signed images for x86 only. ARM will |
| 109 | # support this before shipping, but at the moment they don't. |
| 110 | if [[ "${FLAGS_arch}" = "x86" ]]; then |
| 111 | |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 112 | # Legacy BIOS will use the kernel in the rootfs (via syslinux), as will |
| 113 | # standard EFI BIOS (via grub, from the EFI System Partition). Chrome OS |
| 114 | # BIOS will use a separate signed kernel partition, which we'll create now. |
| 115 | # FIXME: remove serial output, debugging messages. |
| 116 | mkdir -p ${FLAGS_working_dir} |
| 117 | cat <<EOF | cat - "${FLAGS_working_dir}/boot.config" \ |
| 118 | > "${FLAGS_working_dir}/config.txt" |
Will Drewry | 69563b7 | 2010-06-24 16:12:58 -0500 | [diff] [blame] | 119 | earlyprintk=serial,ttyS0,115200 |
| 120 | console=ttyS0,115200 |
| 121 | init=/sbin/init |
| 122 | add_efi_memmap |
| 123 | boot=local |
| 124 | rootwait |
Will Drewry | 69563b7 | 2010-06-24 16:12:58 -0500 | [diff] [blame] | 125 | ro |
| 126 | noresume |
| 127 | noswap |
| 128 | i915.modeset=1 |
| 129 | loglevel=7 |
| 130 | cros_secure |
Will Drewry | 69563b7 | 2010-06-24 16:12:58 -0500 | [diff] [blame] | 131 | EOF |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 132 | WORK="${WORK} ${FLAGS_working_dir}/config.txt" |
Will Drewry | 69563b7 | 2010-06-24 16:12:58 -0500 | [diff] [blame] | 133 | |
Louis Yung-Chieh Lo | 3602040 | 2010-07-05 13:23:34 +0800 | [diff] [blame] | 134 | # We sign the image with the recovery_key, because this is what goes onto the |
| 135 | # USB key. We can only boot from the USB drive in recovery mode. |
Bill Richardson | 2ace49e | 2010-07-01 10:23:27 -0700 | [diff] [blame] | 136 | |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 137 | # Create and sign the kernel blob |
| 138 | vbutil_kernel \ |
| 139 | --pack "${FLAGS_to}" \ |
Louis Yung-Chieh Lo | 3602040 | 2010-07-05 13:23:34 +0800 | [diff] [blame] | 140 | --keyblock "${FLAGS_keys_dir}/recovery_kernel.keyblock" \ |
| 141 | --signprivate "${FLAGS_keys_dir}/recovery_kernel_data_key.vbprivk" \ |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 142 | --version 1 \ |
| 143 | --config "${FLAGS_working_dir}/config.txt" \ |
| 144 | --bootloader /lib64/bootstub/bootstub.efi \ |
| 145 | --vmlinuz "${FLAGS_vmlinuz}" |
Will Drewry | 69563b7 | 2010-06-24 16:12:58 -0500 | [diff] [blame] | 146 | |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 147 | # And verify it. |
| 148 | vbutil_kernel \ |
| 149 | --verify "${FLAGS_to}" \ |
Louis Yung-Chieh Lo | 3602040 | 2010-07-05 13:23:34 +0800 | [diff] [blame] | 150 | --signpubkey "${FLAGS_keys_dir}/recovery_key.vbpubk" |
| 151 | |
| 152 | |
| 153 | # Now we re-sign the same image using the normal keys. This is the kernel |
| 154 | # image that is put on the hard disk by the installer. Note: To save space on |
| 155 | # the USB image, we're only emitting the new verfication block, and the |
| 156 | # installer just replaces that part of the hard disk's kernel partition. |
| 157 | vbutil_kernel \ |
| 158 | --repack "${FLAGS_hd_vblock}" \ |
| 159 | --vblockonly \ |
| 160 | --keyblock "${FLAGS_keys_dir}/kernel.keyblock" \ |
| 161 | --signprivate "${FLAGS_keys_dir}/kernel_data_key.vbprivk" \ |
| 162 | --oldblob "${FLAGS_to}" |
| 163 | |
| 164 | |
| 165 | # To verify it, we have to replace the vblock from the original image. |
| 166 | tempfile=$(mktemp) |
| 167 | trap "rm -f $tempfile" EXIT |
| 168 | cat "${FLAGS_hd_vblock}" > $tempfile |
| 169 | dd if="${FLAGS_to}" bs=65536 skip=1 >> $tempfile |
| 170 | |
| 171 | vbutil_kernel \ |
| 172 | --verify $tempfile \ |
| 173 | --signpubkey "${FLAGS_keys_dir}/kernel_subkey.vbpubk" |
| 174 | |
| 175 | rm -f $tempfile |
| 176 | trap - EXIT |
Will Drewry | 69563b7 | 2010-06-24 16:12:58 -0500 | [diff] [blame] | 177 | |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 178 | elif [[ "${FLAGS_arch}" = "arm" ]]; then |
Will Drewry | 69563b7 | 2010-06-24 16:12:58 -0500 | [diff] [blame] | 179 | # FIXME: For now, ARM just uses the unsigned kernel by itself. |
| 180 | cp -f "${FLAGS_vmlinuz}" "${FLAGS_to}" |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 181 | else |
| 182 | error "Unknown arch: ${FLAGS_arch}" |
Will Drewry | 69563b7 | 2010-06-24 16:12:58 -0500 | [diff] [blame] | 183 | fi |
| 184 | |
| 185 | set +e # cleanup failure is a-ok |
| 186 | |
| 187 | if [[ ${FLAGS_keep_work} -eq ${FLAGS_FALSE} ]]; then |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 188 | info "Cleaning up temporary files: ${WORK}" |
Will Drewry | 69563b7 | 2010-06-24 16:12:58 -0500 | [diff] [blame] | 189 | rm ${WORK} |
| 190 | rmdir ${FLAGS_working_dir} |
| 191 | fi |
| 192 | |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 193 | info "Kernel partition image emitted: ${FLAGS_to}" |
| 194 | |
| 195 | if [[ -f ${FLAGS_rootfs_hash} ]]; then |
| 196 | info "Root filesystem hash emitted: ${FLAGS_rootfs_hash}" |
| 197 | fi |