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 |
| 61 | info "Determining root fs block count." |
| 62 | # Gets the number of blocks. 4096 byte blocks _are_ expected. |
| 63 | root_fs_blocks=$(sudo dumpe2fs "${FLAGS_rootfs_image}" 2> /dev/null | |
| 64 | grep "Block count" | |
| 65 | tr -d ' ' | |
| 66 | cut -f2 -d:) |
| 67 | info "Checking root fs block size." |
| 68 | root_fs_block_sz=$(sudo dumpe2fs "${FLAGS_rootfs_image}" 2> /dev/null | |
| 69 | grep "Block size" | |
| 70 | tr -d ' ' | |
| 71 | cut -f2 -d:) |
| 72 | if [[ ${root_fs_block_sz} -ne 4096 ]]; then |
| 73 | error "Root file system blocks are not 4k!" |
| 74 | fi |
| 75 | |
| 76 | info "Generating root fs hash tree." |
| 77 | # Runs as sudo in case the image is a block device. |
Will Drewry | 1670d48 | 2010-07-09 13:08:38 -0700 | [diff] [blame^] | 78 | table=$(sudo verity create ${FLAGS_verity_tree_depth} \ |
| 79 | ${FLAGS_verity_hash_alg} \ |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 80 | ${FLAGS_rootfs_image} \ |
| 81 | ${root_fs_blocks} \ |
| 82 | ${FLAGS_rootfs_hash}) |
Will Drewry | 821d07c | 2010-07-03 17:14:58 -0700 | [diff] [blame] | 83 | if [[ -f "${FLAGS_rootfs_hash}" ]]; then |
| 84 | sudo chmod a+r "${FLAGS_rootfs_hash}" |
| 85 | fi |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 86 | # Don't claim the root device unless the root= flag is pointed to |
| 87 | # the verified boot device. Doing so will claim /dev/sdDP out from |
| 88 | # under the system. |
| 89 | if [[ ${FLAGS_root} = "/dev/dm-0" ]]; then |
| 90 | table=${table//HASH_DEV/\/dev\/sd%D%P} |
| 91 | table=${table//ROOT_DEV/\/dev\/sd%D%P} |
| 92 | fi |
Will Drewry | 1670d48 | 2010-07-09 13:08:38 -0700 | [diff] [blame^] | 93 | verity_args="dm=\"${table}\"" |
| 94 | info "dm-verity configuration: ${verity_args}" |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 95 | fi |
| 96 | |
| 97 | mkdir -p "${FLAGS_working_dir}" |
| 98 | cat <<EOF > "${FLAGS_working_dir}/boot.config" |
| 99 | root=${FLAGS_root} |
Will Drewry | 1670d48 | 2010-07-09 13:08:38 -0700 | [diff] [blame^] | 100 | dm_verity.error_behavior=${FLAGS_verity_error_behavior} |
| 101 | dm_verity.max_bios=${FLAGS_verity_max_ios} |
| 102 | ${verity_args} |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 103 | ${FLAGS_boot_args} |
| 104 | EOF |
| 105 | |
| 106 | WORK="${WORK} ${FLAGS_working_dir}/boot.config" |
| 107 | info "Emitted cross-platform boot params to ${FLAGS_working_dir}/boot.config" |
| 108 | |
Will Drewry | 69563b7 | 2010-06-24 16:12:58 -0500 | [diff] [blame] | 109 | # FIXME: At the moment, we're working on signed images for x86 only. ARM will |
| 110 | # support this before shipping, but at the moment they don't. |
| 111 | if [[ "${FLAGS_arch}" = "x86" ]]; then |
| 112 | |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 113 | # Legacy BIOS will use the kernel in the rootfs (via syslinux), as will |
| 114 | # standard EFI BIOS (via grub, from the EFI System Partition). Chrome OS |
| 115 | # BIOS will use a separate signed kernel partition, which we'll create now. |
| 116 | # FIXME: remove serial output, debugging messages. |
| 117 | mkdir -p ${FLAGS_working_dir} |
| 118 | cat <<EOF | cat - "${FLAGS_working_dir}/boot.config" \ |
| 119 | > "${FLAGS_working_dir}/config.txt" |
Will Drewry | 69563b7 | 2010-06-24 16:12:58 -0500 | [diff] [blame] | 120 | earlyprintk=serial,ttyS0,115200 |
| 121 | console=ttyS0,115200 |
| 122 | init=/sbin/init |
| 123 | add_efi_memmap |
| 124 | boot=local |
| 125 | rootwait |
Will Drewry | 69563b7 | 2010-06-24 16:12:58 -0500 | [diff] [blame] | 126 | ro |
| 127 | noresume |
| 128 | noswap |
| 129 | i915.modeset=1 |
| 130 | loglevel=7 |
| 131 | cros_secure |
Will Drewry | 69563b7 | 2010-06-24 16:12:58 -0500 | [diff] [blame] | 132 | EOF |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 133 | WORK="${WORK} ${FLAGS_working_dir}/config.txt" |
Will Drewry | 69563b7 | 2010-06-24 16:12:58 -0500 | [diff] [blame] | 134 | |
Louis Yung-Chieh Lo | 3602040 | 2010-07-05 13:23:34 +0800 | [diff] [blame] | 135 | # We sign the image with the recovery_key, because this is what goes onto the |
| 136 | # 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] | 137 | |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 138 | # Create and sign the kernel blob |
| 139 | vbutil_kernel \ |
| 140 | --pack "${FLAGS_to}" \ |
Louis Yung-Chieh Lo | 3602040 | 2010-07-05 13:23:34 +0800 | [diff] [blame] | 141 | --keyblock "${FLAGS_keys_dir}/recovery_kernel.keyblock" \ |
| 142 | --signprivate "${FLAGS_keys_dir}/recovery_kernel_data_key.vbprivk" \ |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 143 | --version 1 \ |
| 144 | --config "${FLAGS_working_dir}/config.txt" \ |
| 145 | --bootloader /lib64/bootstub/bootstub.efi \ |
| 146 | --vmlinuz "${FLAGS_vmlinuz}" |
Will Drewry | 69563b7 | 2010-06-24 16:12:58 -0500 | [diff] [blame] | 147 | |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 148 | # And verify it. |
| 149 | vbutil_kernel \ |
| 150 | --verify "${FLAGS_to}" \ |
Louis Yung-Chieh Lo | 3602040 | 2010-07-05 13:23:34 +0800 | [diff] [blame] | 151 | --signpubkey "${FLAGS_keys_dir}/recovery_key.vbpubk" |
| 152 | |
| 153 | |
| 154 | # Now we re-sign the same image using the normal keys. This is the kernel |
| 155 | # image that is put on the hard disk by the installer. Note: To save space on |
| 156 | # the USB image, we're only emitting the new verfication block, and the |
| 157 | # installer just replaces that part of the hard disk's kernel partition. |
| 158 | vbutil_kernel \ |
| 159 | --repack "${FLAGS_hd_vblock}" \ |
| 160 | --vblockonly \ |
| 161 | --keyblock "${FLAGS_keys_dir}/kernel.keyblock" \ |
| 162 | --signprivate "${FLAGS_keys_dir}/kernel_data_key.vbprivk" \ |
| 163 | --oldblob "${FLAGS_to}" |
| 164 | |
| 165 | |
| 166 | # To verify it, we have to replace the vblock from the original image. |
| 167 | tempfile=$(mktemp) |
| 168 | trap "rm -f $tempfile" EXIT |
| 169 | cat "${FLAGS_hd_vblock}" > $tempfile |
| 170 | dd if="${FLAGS_to}" bs=65536 skip=1 >> $tempfile |
| 171 | |
| 172 | vbutil_kernel \ |
| 173 | --verify $tempfile \ |
| 174 | --signpubkey "${FLAGS_keys_dir}/kernel_subkey.vbpubk" |
| 175 | |
| 176 | rm -f $tempfile |
| 177 | trap - EXIT |
Will Drewry | 69563b7 | 2010-06-24 16:12:58 -0500 | [diff] [blame] | 178 | |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 179 | elif [[ "${FLAGS_arch}" = "arm" ]]; then |
Will Drewry | 69563b7 | 2010-06-24 16:12:58 -0500 | [diff] [blame] | 180 | # FIXME: For now, ARM just uses the unsigned kernel by itself. |
| 181 | cp -f "${FLAGS_vmlinuz}" "${FLAGS_to}" |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 182 | else |
| 183 | error "Unknown arch: ${FLAGS_arch}" |
Will Drewry | 69563b7 | 2010-06-24 16:12:58 -0500 | [diff] [blame] | 184 | fi |
| 185 | |
| 186 | set +e # cleanup failure is a-ok |
| 187 | |
| 188 | if [[ ${FLAGS_keep_work} -eq ${FLAGS_FALSE} ]]; then |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 189 | info "Cleaning up temporary files: ${WORK}" |
Will Drewry | 69563b7 | 2010-06-24 16:12:58 -0500 | [diff] [blame] | 190 | rm ${WORK} |
| 191 | rmdir ${FLAGS_working_dir} |
| 192 | fi |
| 193 | |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 194 | info "Kernel partition image emitted: ${FLAGS_to}" |
| 195 | |
| 196 | if [[ -f ${FLAGS_rootfs_hash} ]]; then |
| 197 | info "Root filesystem hash emitted: ${FLAGS_rootfs_hash}" |
| 198 | fi |