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)" |
Tan Gao | 843b70a | 2010-08-17 09:41:48 -0700 | [diff] [blame] | 28 | DEFINE_boolean use_dev_keys ${FLAGS_FALSE} \ |
| 29 | "Use developer keys for signing. (Default: false)" |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 30 | # Note, to enable verified boot, the caller would manually pass: |
Will Drewry | 69563b7 | 2010-06-24 16:12:58 -0500 | [diff] [blame] | 31 | # --boot_args='dm="... /dev/sd%D%P /dev/sd%D%P ..." \ |
| 32 | # --root=/dev/dm-0 |
| 33 | DEFINE_string boot_args "noinitrd" \ |
| 34 | "Additional boot arguments to pass to the commandline (Default: noinitrd)" |
| 35 | DEFINE_string root "/dev/sd%D%P" \ |
| 36 | "Expected device root (Default: root=/dev/sd%D%P)" |
| 37 | |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 38 | # If provided, will automatically add verified boot arguments. |
| 39 | DEFINE_string rootfs_image "" \ |
| 40 | "Optional path to the rootfs device or image.(Default: \"\")" |
| 41 | DEFINE_string rootfs_hash "" \ |
| 42 | "Optional path to output the rootfs hash to. (Default: \"\")" |
Will Drewry | 1670d48 | 2010-07-09 13:08:38 -0700 | [diff] [blame] | 43 | DEFINE_integer verity_error_behavior 2 \ |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 44 | "Verified boot error behavior [0: I/O errors, 1: reboot, 2: nothing] \ |
| 45 | (Default: 2)" |
Will Drewry | 1670d48 | 2010-07-09 13:08:38 -0700 | [diff] [blame] | 46 | DEFINE_integer verity_tree_depth 1 \ |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 47 | "Optional Verified boot hash tree depth. (Default: 1)" |
Will Drewry | 1670d48 | 2010-07-09 13:08:38 -0700 | [diff] [blame] | 48 | DEFINE_integer verity_max_ios 1024 \ |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 49 | "Optional number of outstanding I/O operations. (Default: 1024)" |
Will Drewry | 1670d48 | 2010-07-09 13:08:38 -0700 | [diff] [blame] | 50 | DEFINE_string verity_hash_alg "sha1" \ |
| 51 | "Cryptographic hash algorithm used for dm-verity. (Default: sha1)" |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 52 | |
Will Drewry | 69563b7 | 2010-06-24 16:12:58 -0500 | [diff] [blame] | 53 | # Parse flags |
| 54 | FLAGS "$@" || exit 1 |
| 55 | eval set -- "${FLAGS_ARGV}" |
| 56 | |
| 57 | # Die on error |
| 58 | set -e |
| 59 | |
Will Drewry | 1670d48 | 2010-07-09 13:08:38 -0700 | [diff] [blame] | 60 | verity_args= |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 61 | # Even with a rootfs_image, root= is not changed unless specified. |
| 62 | if [[ -n "${FLAGS_rootfs_image}" && -n "${FLAGS_rootfs_hash}" ]]; then |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 63 | # Gets the number of blocks. 4096 byte blocks _are_ expected. |
| 64 | root_fs_blocks=$(sudo dumpe2fs "${FLAGS_rootfs_image}" 2> /dev/null | |
| 65 | grep "Block count" | |
| 66 | tr -d ' ' | |
| 67 | cut -f2 -d:) |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 68 | root_fs_block_sz=$(sudo dumpe2fs "${FLAGS_rootfs_image}" 2> /dev/null | |
| 69 | grep "Block size" | |
| 70 | tr -d ' ' | |
| 71 | cut -f2 -d:) |
Will Drewry | 78992a3 | 2010-07-21 14:02:20 -0500 | [diff] [blame] | 72 | 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] | 73 | if [[ ${root_fs_block_sz} -ne 4096 ]]; then |
| 74 | error "Root file system blocks are not 4k!" |
| 75 | fi |
| 76 | |
| 77 | info "Generating root fs hash tree." |
| 78 | # Runs as sudo in case the image is a block device. |
Will Drewry | 1670d48 | 2010-07-09 13:08:38 -0700 | [diff] [blame] | 79 | table=$(sudo verity create ${FLAGS_verity_tree_depth} \ |
| 80 | ${FLAGS_verity_hash_alg} \ |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 81 | ${FLAGS_rootfs_image} \ |
| 82 | ${root_fs_blocks} \ |
| 83 | ${FLAGS_rootfs_hash}) |
Will Drewry | 821d07c | 2010-07-03 17:14:58 -0700 | [diff] [blame] | 84 | if [[ -f "${FLAGS_rootfs_hash}" ]]; then |
| 85 | sudo chmod a+r "${FLAGS_rootfs_hash}" |
| 86 | fi |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 87 | # Don't claim the root device unless the root= flag is pointed to |
| 88 | # the verified boot device. Doing so will claim /dev/sdDP out from |
| 89 | # under the system. |
| 90 | if [[ ${FLAGS_root} = "/dev/dm-0" ]]; then |
Kenneth Waters | ed54d93 | 2010-09-21 10:29:54 -0700 | [diff] [blame] | 91 | if [[ "${FLAGS_arch}" = "x86" ]]; then |
| 92 | base_root='/dev/sd%D%P' |
| 93 | elif [[ "${FLAGS_arch}" = "arm" ]]; then |
| 94 | base_root='/dev/${devname}${rootpart}' |
| 95 | fi |
| 96 | table=${table//HASH_DEV/${base_root}} |
| 97 | table=${table//ROOT_DEV/${base_root}} |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 98 | fi |
Will Drewry | 78992a3 | 2010-07-21 14:02:20 -0500 | [diff] [blame] | 99 | verity_args="dm=\"vroot none ro,${table}\"" |
Will Drewry | 1670d48 | 2010-07-09 13:08:38 -0700 | [diff] [blame] | 100 | info "dm-verity configuration: ${verity_args}" |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 101 | fi |
| 102 | |
| 103 | mkdir -p "${FLAGS_working_dir}" |
Will Drewry | d6435d4 | 2010-10-20 15:37:46 -0500 | [diff] [blame^] | 104 | |
| 105 | # Only let dm-verity block if rootfs verification is configured. |
| 106 | dev_wait=0 |
| 107 | if [[ ${FLAGS_root} = "/dev/dm-0" ]]; then |
| 108 | dev_wait=1 |
| 109 | fi |
| 110 | |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 111 | cat <<EOF > "${FLAGS_working_dir}/boot.config" |
| 112 | root=${FLAGS_root} |
Will Drewry | 1670d48 | 2010-07-09 13:08:38 -0700 | [diff] [blame] | 113 | dm_verity.error_behavior=${FLAGS_verity_error_behavior} |
| 114 | dm_verity.max_bios=${FLAGS_verity_max_ios} |
Will Drewry | d6435d4 | 2010-10-20 15:37:46 -0500 | [diff] [blame^] | 115 | dm_verity.dev_wait=${dev_wait} |
Will Drewry | 1670d48 | 2010-07-09 13:08:38 -0700 | [diff] [blame] | 116 | ${verity_args} |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 117 | ${FLAGS_boot_args} |
| 118 | EOF |
| 119 | |
| 120 | WORK="${WORK} ${FLAGS_working_dir}/boot.config" |
| 121 | info "Emitted cross-platform boot params to ${FLAGS_working_dir}/boot.config" |
| 122 | |
Will Drewry | 69563b7 | 2010-06-24 16:12:58 -0500 | [diff] [blame] | 123 | # FIXME: At the moment, we're working on signed images for x86 only. ARM will |
| 124 | # support this before shipping, but at the moment they don't. |
| 125 | if [[ "${FLAGS_arch}" = "x86" ]]; then |
| 126 | |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 127 | # Legacy BIOS will use the kernel in the rootfs (via syslinux), as will |
| 128 | # standard EFI BIOS (via grub, from the EFI System Partition). Chrome OS |
| 129 | # BIOS will use a separate signed kernel partition, which we'll create now. |
| 130 | # FIXME: remove serial output, debugging messages. |
| 131 | mkdir -p ${FLAGS_working_dir} |
| 132 | cat <<EOF | cat - "${FLAGS_working_dir}/boot.config" \ |
| 133 | > "${FLAGS_working_dir}/config.txt" |
Will Drewry | 69563b7 | 2010-06-24 16:12:58 -0500 | [diff] [blame] | 134 | earlyprintk=serial,ttyS0,115200 |
| 135 | console=ttyS0,115200 |
| 136 | init=/sbin/init |
| 137 | add_efi_memmap |
| 138 | boot=local |
| 139 | rootwait |
Will Drewry | 69563b7 | 2010-06-24 16:12:58 -0500 | [diff] [blame] | 140 | ro |
| 141 | noresume |
| 142 | noswap |
| 143 | i915.modeset=1 |
| 144 | loglevel=7 |
| 145 | cros_secure |
Bill Richardson | 8bfa468 | 2010-07-23 17:24:15 -0700 | [diff] [blame] | 146 | kern_guid=%U |
Will Drewry | 69563b7 | 2010-06-24 16:12:58 -0500 | [diff] [blame] | 147 | EOF |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 148 | WORK="${WORK} ${FLAGS_working_dir}/config.txt" |
Will Drewry | 69563b7 | 2010-06-24 16:12:58 -0500 | [diff] [blame] | 149 | |
Louis Yung-Chieh Lo | 3602040 | 2010-07-05 13:23:34 +0800 | [diff] [blame] | 150 | # We sign the image with the recovery_key, because this is what goes onto the |
| 151 | # USB key. We can only boot from the USB drive in recovery mode. |
Tan Gao | 843b70a | 2010-08-17 09:41:48 -0700 | [diff] [blame] | 152 | # For dev install shim, we need to use the installer keyblock instead of |
| 153 | # the recovery keyblock because of the difference in flags. |
| 154 | if [ ${FLAGS_use_dev_keys} -eq ${FLAGS_TRUE} ]; then |
| 155 | USB_KEYBLOCK=installer_kernel.keyblock |
| 156 | info "DEBUG: use dev install signing key" |
| 157 | else |
| 158 | USB_KEYBLOCK=recovery_kernel.keyblock |
| 159 | info "DEBUG: use recovery signing key" |
| 160 | fi |
Bill Richardson | 2ace49e | 2010-07-01 10:23:27 -0700 | [diff] [blame] | 161 | |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 162 | # Create and sign the kernel blob |
| 163 | vbutil_kernel \ |
| 164 | --pack "${FLAGS_to}" \ |
Tan Gao | 843b70a | 2010-08-17 09:41:48 -0700 | [diff] [blame] | 165 | --keyblock "${FLAGS_keys_dir}/${USB_KEYBLOCK}" \ |
Louis Yung-Chieh Lo | 3602040 | 2010-07-05 13:23:34 +0800 | [diff] [blame] | 166 | --signprivate "${FLAGS_keys_dir}/recovery_kernel_data_key.vbprivk" \ |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 167 | --version 1 \ |
| 168 | --config "${FLAGS_working_dir}/config.txt" \ |
| 169 | --bootloader /lib64/bootstub/bootstub.efi \ |
| 170 | --vmlinuz "${FLAGS_vmlinuz}" |
Will Drewry | 69563b7 | 2010-06-24 16:12:58 -0500 | [diff] [blame] | 171 | |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 172 | # And verify it. |
| 173 | vbutil_kernel \ |
| 174 | --verify "${FLAGS_to}" \ |
Louis Yung-Chieh Lo | 3602040 | 2010-07-05 13:23:34 +0800 | [diff] [blame] | 175 | --signpubkey "${FLAGS_keys_dir}/recovery_key.vbpubk" |
| 176 | |
| 177 | |
| 178 | # Now we re-sign the same image using the normal keys. This is the kernel |
| 179 | # image that is put on the hard disk by the installer. Note: To save space on |
| 180 | # the USB image, we're only emitting the new verfication block, and the |
| 181 | # installer just replaces that part of the hard disk's kernel partition. |
| 182 | vbutil_kernel \ |
| 183 | --repack "${FLAGS_hd_vblock}" \ |
| 184 | --vblockonly \ |
| 185 | --keyblock "${FLAGS_keys_dir}/kernel.keyblock" \ |
| 186 | --signprivate "${FLAGS_keys_dir}/kernel_data_key.vbprivk" \ |
| 187 | --oldblob "${FLAGS_to}" |
| 188 | |
| 189 | |
| 190 | # To verify it, we have to replace the vblock from the original image. |
| 191 | tempfile=$(mktemp) |
| 192 | trap "rm -f $tempfile" EXIT |
| 193 | cat "${FLAGS_hd_vblock}" > $tempfile |
| 194 | dd if="${FLAGS_to}" bs=65536 skip=1 >> $tempfile |
| 195 | |
| 196 | vbutil_kernel \ |
| 197 | --verify $tempfile \ |
| 198 | --signpubkey "${FLAGS_keys_dir}/kernel_subkey.vbpubk" |
| 199 | |
| 200 | rm -f $tempfile |
| 201 | trap - EXIT |
Will Drewry | 69563b7 | 2010-06-24 16:12:58 -0500 | [diff] [blame] | 202 | |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 203 | elif [[ "${FLAGS_arch}" = "arm" ]]; then |
Kenneth Waters | ed54d93 | 2010-09-21 10:29:54 -0700 | [diff] [blame] | 204 | # FIXME: This stuff is unsigned, and will likely change with vboot_reference |
| 205 | # but it doesn't technically have to. |
| 206 | |
| 207 | kernel_script="${FLAGS_working_dir}/kernel.scr" |
| 208 | kernel_script_img="${FLAGS_working_dir}/kernel.scr.uimg" |
| 209 | # HACK: !! Kernel image construction requires some stuff from portage, not |
| 210 | # sure how to get that information here cleanly !! |
| 211 | kernel_image="${FLAGS_vmlinuz/vmlinuz/vmlinux.uimg}" |
| 212 | WORK="${WORK} ${kernel_script} ${kernel_script_img}" |
| 213 | |
| 214 | kernel_size=$((($(stat -c %s "${kernel_image}") + 511) / 512)) |
| 215 | script_size=16 |
| 216 | |
| 217 | # Build boot script image |
| 218 | echo -n 'setenv bootargs ${bootargs} ' > "${kernel_script}" |
| 219 | tr '\n' ' ' <"${FLAGS_working_dir}/boot.config" >> "${kernel_script}" |
| 220 | echo >> "${kernel_script}" |
| 221 | printf 'read ${devtype} 0:${kernelpart} ${loadaddr} %x %x\n' \ |
| 222 | ${script_size} ${kernel_size} >> "${kernel_script}" |
| 223 | echo 'bootm ${loadaddr}' >> ${kernel_script} |
| 224 | mkimage -A arm -O linux -T script -C none -a 0 -e 0 \ |
| 225 | -n kernel_script -d "${kernel_script}" "${kernel_script_img}" |
| 226 | |
| 227 | if [ $(stat -c %s "${kernel_script_img}") -gt $((512 * ${script_size})) ] |
| 228 | then |
| 229 | echo 'Kernel script too large for reserved space.' |
| 230 | exit 1 |
| 231 | fi |
| 232 | |
| 233 | # Assemble image |
| 234 | rm -f "${FLAGS_to}" |
| 235 | dd if="${kernel_script_img}" of="${FLAGS_to}" bs=512 count="${script_size}" |
| 236 | dd if="${kernel_image}" of="${FLAGS_to}" bs=512 seek="${script_size}" |
Kenneth Waters | 9bc78b3 | 2010-09-22 13:54:44 -0700 | [diff] [blame] | 237 | |
| 238 | # TODO: HACK: Until the kernel partition contains a signed image, create a |
| 239 | # phony hd.vblock to keep chromeos-install and cros_generate_update_payload |
| 240 | # working. |
| 241 | dd if="${FLAGS_to}" of="${FLAGS_hd_vblock}" bs=64K count=1 |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 242 | else |
| 243 | error "Unknown arch: ${FLAGS_arch}" |
Will Drewry | 69563b7 | 2010-06-24 16:12:58 -0500 | [diff] [blame] | 244 | fi |
| 245 | |
| 246 | set +e # cleanup failure is a-ok |
| 247 | |
| 248 | if [[ ${FLAGS_keep_work} -eq ${FLAGS_FALSE} ]]; then |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 249 | info "Cleaning up temporary files: ${WORK}" |
Will Drewry | 69563b7 | 2010-06-24 16:12:58 -0500 | [diff] [blame] | 250 | rm ${WORK} |
| 251 | rmdir ${FLAGS_working_dir} |
| 252 | fi |
| 253 | |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 254 | info "Kernel partition image emitted: ${FLAGS_to}" |
| 255 | |
| 256 | if [[ -f ${FLAGS_rootfs_hash} ]]; then |
| 257 | info "Root filesystem hash emitted: ${FLAGS_rootfs_hash}" |
| 258 | fi |