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 | |
Brian Harring | aa13ea4 | 2012-03-15 18:31:03 -0700 | [diff] [blame] | 9 | SCRIPT_ROOT=$(dirname $(readlink -f "$0")) |
David James | 359d3e1 | 2012-07-10 13:09:48 -0700 | [diff] [blame^] | 10 | . "${SCRIPT_ROOT}/common.sh" || exit 1 |
Will Drewry | 69563b7 | 2010-06-24 16:12:58 -0500 | [diff] [blame] | 11 | |
| 12 | get_default_board |
| 13 | |
| 14 | # Flags. |
| 15 | DEFINE_string arch "x86" \ |
Sonny Rao | 0679b36 | 2011-10-12 13:53:19 -0700 | [diff] [blame] | 16 | "The boot architecture: arm, x86, or amd64. (Default: x86)" |
Will Drewry | 69563b7 | 2010-06-24 16:12:58 -0500 | [diff] [blame] | 17 | DEFINE_string to "/tmp/vmlinuz.image" \ |
| 18 | "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] | 19 | DEFINE_string hd_vblock "/tmp/vmlinuz_hd.vblock" \ |
| 20 | "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] | 21 | DEFINE_string vmlinuz "vmlinuz" \ |
| 22 | "The path to the kernel (Default: vmlinuz)" |
| 23 | DEFINE_string working_dir "/tmp/vmlinuz.working" \ |
| 24 | "Working directory for in-progress files. (Default: /tmp/vmlinuz.working)" |
| 25 | DEFINE_boolean keep_work ${FLAGS_FALSE} \ |
| 26 | "Keep temporary files (*.keyblock, *.vbpubk). (Default: false)" |
| 27 | DEFINE_string keys_dir "${SRC_ROOT}/platform/vboot_reference/tests/testkeys" \ |
Bill Richardson | 2ace49e | 2010-07-01 10:23:27 -0700 | [diff] [blame] | 28 | "Directory with the RSA signing keys. (Defaults to test keys)" |
Tan Gao | 843b70a | 2010-08-17 09:41:48 -0700 | [diff] [blame] | 29 | DEFINE_boolean use_dev_keys ${FLAGS_FALSE} \ |
| 30 | "Use developer keys for signing. (Default: false)" |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 31 | # Note, to enable verified boot, the caller would manually pass: |
Will Drewry | b910de8 | 2011-02-23 13:26:50 -0600 | [diff] [blame] | 32 | # --boot_args='dm="... %U+1 %U+1 ..." \ |
Will Drewry | 69563b7 | 2010-06-24 16:12:58 -0500 | [diff] [blame] | 33 | # --root=/dev/dm-0 |
| 34 | DEFINE_string boot_args "noinitrd" \ |
| 35 | "Additional boot arguments to pass to the commandline (Default: noinitrd)" |
Will Drewry | b910de8 | 2011-02-23 13:26:50 -0600 | [diff] [blame] | 36 | # By default, we use a firmware enumerated value, but it isn't reliable for |
| 37 | # production use. If +%d can be added upstream, then we can use: |
| 38 | # root=PARTUID=uuid+1 |
Che-Liang Chiou | 93b6eff | 2011-05-18 17:21:13 +0800 | [diff] [blame] | 39 | DEFINE_string root "PARTUUID=%U/PARTNROFF=1" \ |
Will Drewry | b910de8 | 2011-02-23 13:26:50 -0600 | [diff] [blame] | 40 | "Expected device root partition" |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 41 | # If provided, will automatically add verified boot arguments. |
| 42 | DEFINE_string rootfs_image "" \ |
| 43 | "Optional path to the rootfs device or image.(Default: \"\")" |
| 44 | DEFINE_string rootfs_hash "" \ |
| 45 | "Optional path to output the rootfs hash to. (Default: \"\")" |
Will Drewry | 1670d48 | 2010-07-09 13:08:38 -0700 | [diff] [blame] | 46 | DEFINE_integer verity_error_behavior 2 \ |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 47 | "Verified boot error behavior [0: I/O errors, 1: reboot, 2: nothing] \ |
| 48 | (Default: 2)" |
Will Drewry | b910de8 | 2011-02-23 13:26:50 -0600 | [diff] [blame] | 49 | DEFINE_integer verity_max_ios -1 \ |
| 50 | "Optional number of outstanding I/O operations. (Default: -1)" |
Will Drewry | 1670d48 | 2010-07-09 13:08:38 -0700 | [diff] [blame] | 51 | DEFINE_string verity_hash_alg "sha1" \ |
| 52 | "Cryptographic hash algorithm used for dm-verity. (Default: sha1)" |
Elly Jones | 9ca3e4c | 2011-09-26 15:18:19 -0400 | [diff] [blame] | 53 | DEFINE_string verity_salt "" \ |
| 54 | "Salt to use for rootfs hash (Default: \"\")" |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 55 | |
Will Drewry | 69563b7 | 2010-06-24 16:12:58 -0500 | [diff] [blame] | 56 | # Parse flags |
| 57 | FLAGS "$@" || exit 1 |
| 58 | eval set -- "${FLAGS_ARGV}" |
| 59 | |
| 60 | # Die on error |
Brian Harring | 7f175a5 | 2012-03-02 05:37:00 -0800 | [diff] [blame] | 61 | switch_to_strict_mode |
Will Drewry | 69563b7 | 2010-06-24 16:12:58 -0500 | [diff] [blame] | 62 | |
Will Drewry | 1670d48 | 2010-07-09 13:08:38 -0700 | [diff] [blame] | 63 | verity_args= |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 64 | # Even with a rootfs_image, root= is not changed unless specified. |
| 65 | if [[ -n "${FLAGS_rootfs_image}" && -n "${FLAGS_rootfs_hash}" ]]; then |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 66 | # Gets the number of blocks. 4096 byte blocks _are_ expected. |
Da Zheng | 87465ea | 2011-07-30 16:25:28 -0700 | [diff] [blame] | 67 | if [ -f "${FLAGS_rootfs_image}" ]; then |
| 68 | root_fs_block_sz=4096 |
| 69 | root_fs_sz=$(stat -c '%s' ${FLAGS_rootfs_image}) |
| 70 | root_fs_blocks=$((root_fs_sz / ${root_fs_block_sz})) |
| 71 | else |
| 72 | root_fs_blocks=$(sudo dumpe2fs "${FLAGS_rootfs_image}" 2> /dev/null | |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 73 | grep "Block count" | |
| 74 | tr -d ' ' | |
| 75 | cut -f2 -d:) |
Da Zheng | 87465ea | 2011-07-30 16:25:28 -0700 | [diff] [blame] | 76 | root_fs_block_sz=$(sudo dumpe2fs "${FLAGS_rootfs_image}" 2> /dev/null | |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 77 | grep "Block size" | |
| 78 | tr -d ' ' | |
| 79 | cut -f2 -d:) |
Da Zheng | 87465ea | 2011-07-30 16:25:28 -0700 | [diff] [blame] | 80 | fi |
| 81 | |
Will Drewry | 78992a3 | 2010-07-21 14:02:20 -0500 | [diff] [blame] | 82 | 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] | 83 | if [[ ${root_fs_block_sz} -ne 4096 ]]; then |
| 84 | error "Root file system blocks are not 4k!" |
| 85 | fi |
| 86 | |
Elly Jones | 9ca3e4c | 2011-09-26 15:18:19 -0400 | [diff] [blame] | 87 | info "Generating root fs hash tree (salt '${FLAGS_verity_salt}')." |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 88 | # Runs as sudo in case the image is a block device. |
Mandeep Singh Baines | 118692a | 2011-04-28 13:50:33 -0700 | [diff] [blame] | 89 | # First argument to verity is reserved/unused and MUST be 0 |
Elly Jones | 5e7a430 | 2011-05-27 16:08:16 -0400 | [diff] [blame] | 90 | table=$(sudo verity mode=create \ |
| 91 | alg=${FLAGS_verity_hash_alg} \ |
| 92 | payload=${FLAGS_rootfs_image} \ |
| 93 | payload_blocks=${root_fs_blocks} \ |
Elly Jones | f4e4833 | 2011-09-02 13:25:04 -0400 | [diff] [blame] | 94 | hashtree=${FLAGS_rootfs_hash} \ |
Elly Jones | 9ca3e4c | 2011-09-26 15:18:19 -0400 | [diff] [blame] | 95 | salt=${FLAGS_verity_salt}) |
Will Drewry | 821d07c | 2010-07-03 17:14:58 -0700 | [diff] [blame] | 96 | if [[ -f "${FLAGS_rootfs_hash}" ]]; then |
| 97 | sudo chmod a+r "${FLAGS_rootfs_hash}" |
| 98 | fi |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 99 | # Don't claim the root device unless the root= flag is pointed to |
| 100 | # the verified boot device. Doing so will claim /dev/sdDP out from |
| 101 | # under the system. |
| 102 | if [[ ${FLAGS_root} = "/dev/dm-0" ]]; then |
Nick Sanders | ffd0ca6 | 2011-06-25 01:20:24 -0700 | [diff] [blame] | 103 | base_root='%U+1' # kern_guid + 1 |
Kenneth Waters | ed54d93 | 2010-09-21 10:29:54 -0700 | [diff] [blame] | 104 | table=${table//HASH_DEV/${base_root}} |
| 105 | table=${table//ROOT_DEV/${base_root}} |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 106 | fi |
Will Drewry | 78992a3 | 2010-07-21 14:02:20 -0500 | [diff] [blame] | 107 | verity_args="dm=\"vroot none ro,${table}\"" |
Will Drewry | 1670d48 | 2010-07-09 13:08:38 -0700 | [diff] [blame] | 108 | info "dm-verity configuration: ${verity_args}" |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 109 | fi |
| 110 | |
| 111 | mkdir -p "${FLAGS_working_dir}" |
Will Drewry | d6435d4 | 2010-10-20 15:37:46 -0500 | [diff] [blame] | 112 | |
| 113 | # Only let dm-verity block if rootfs verification is configured. |
| 114 | dev_wait=0 |
| 115 | if [[ ${FLAGS_root} = "/dev/dm-0" ]]; then |
| 116 | dev_wait=1 |
| 117 | fi |
| 118 | |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 119 | cat <<EOF > "${FLAGS_working_dir}/boot.config" |
| 120 | root=${FLAGS_root} |
Olof Johansson | e0b7551 | 2011-01-26 14:31:26 -0800 | [diff] [blame] | 121 | rootwait |
| 122 | ro |
Will Drewry | 1670d48 | 2010-07-09 13:08:38 -0700 | [diff] [blame] | 123 | dm_verity.error_behavior=${FLAGS_verity_error_behavior} |
| 124 | dm_verity.max_bios=${FLAGS_verity_max_ios} |
Will Drewry | d6435d4 | 2010-10-20 15:37:46 -0500 | [diff] [blame] | 125 | dm_verity.dev_wait=${dev_wait} |
Will Drewry | 1670d48 | 2010-07-09 13:08:38 -0700 | [diff] [blame] | 126 | ${verity_args} |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 127 | ${FLAGS_boot_args} |
Pawel Osciak | a2c2375 | 2011-11-15 15:09:45 -0800 | [diff] [blame] | 128 | vt.global_cursor_default=0 |
Doug Anderson | 2dd2e8e | 2012-01-06 09:32:24 -0800 | [diff] [blame] | 129 | kern_guid=%U |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 130 | EOF |
| 131 | |
| 132 | WORK="${WORK} ${FLAGS_working_dir}/boot.config" |
| 133 | info "Emitted cross-platform boot params to ${FLAGS_working_dir}/boot.config" |
| 134 | |
Sonny Rao | 3163cf0 | 2011-10-17 16:20:28 -0700 | [diff] [blame] | 135 | if [[ "${FLAGS_arch}" = "x86" || "${FLAGS_arch}" = "amd64" ]]; then |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 136 | # Legacy BIOS will use the kernel in the rootfs (via syslinux), as will |
| 137 | # standard EFI BIOS (via grub, from the EFI System Partition). Chrome OS |
| 138 | # BIOS will use a separate signed kernel partition, which we'll create now. |
| 139 | # FIXME: remove serial output, debugging messages. |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 140 | cat <<EOF | cat - "${FLAGS_working_dir}/boot.config" \ |
| 141 | > "${FLAGS_working_dir}/config.txt" |
Olof Johansson | 24e10cb | 2011-04-07 15:08:08 -0700 | [diff] [blame] | 142 | quiet |
Sean Paul | 078164e | 2012-05-08 10:34:24 -0400 | [diff] [blame] | 143 | loglevel=0 |
Nick Sanders | ada8a1f | 2010-10-26 02:56:51 -0700 | [diff] [blame] | 144 | console=tty2 |
Will Drewry | 69563b7 | 2010-06-24 16:12:58 -0500 | [diff] [blame] | 145 | init=/sbin/init |
| 146 | add_efi_memmap |
| 147 | boot=local |
Will Drewry | 69563b7 | 2010-06-24 16:12:58 -0500 | [diff] [blame] | 148 | noresume |
| 149 | noswap |
| 150 | i915.modeset=1 |
Will Drewry | 69563b7 | 2010-06-24 16:12:58 -0500 | [diff] [blame] | 151 | cros_secure |
Luigi Semenzato | 195aebe | 2010-10-20 17:35:00 -0700 | [diff] [blame] | 152 | tpm_tis.force=1 |
| 153 | tpm_tis.interrupts=0 |
Vadim Bendebury | 8e623f6 | 2011-02-28 10:28:31 -0800 | [diff] [blame] | 154 | nmi_watchdog=panic,lapic |
Will Drewry | 69563b7 | 2010-06-24 16:12:58 -0500 | [diff] [blame] | 155 | EOF |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 156 | WORK="${WORK} ${FLAGS_working_dir}/config.txt" |
Will Drewry | 69563b7 | 2010-06-24 16:12:58 -0500 | [diff] [blame] | 157 | |
Che-Liang Chiou | 75ac2be | 2011-02-24 12:00:16 +0800 | [diff] [blame] | 158 | bootloader_path="/lib64/bootstub/bootstub.efi" |
| 159 | kernel_image="${FLAGS_vmlinuz}" |
Che-Liang Chiou | 75ac2be | 2011-02-24 12:00:16 +0800 | [diff] [blame] | 160 | elif [[ "${FLAGS_arch}" = "arm" ]]; then |
Olof Johansson | 24e10cb | 2011-04-07 15:08:08 -0700 | [diff] [blame] | 161 | cat <<EOF | cat - "${FLAGS_working_dir}/boot.config" \ |
| 162 | > "${FLAGS_working_dir}/config.txt" |
| 163 | earlyprintk |
Katie Roberts-Hoffman | 8429b4e | 2011-09-27 13:34:59 -0700 | [diff] [blame] | 164 | vmalloc=234MB |
Olof Johansson | 24e10cb | 2011-04-07 15:08:08 -0700 | [diff] [blame] | 165 | EOF |
Che-Liang Chiou | 75ac2be | 2011-02-24 12:00:16 +0800 | [diff] [blame] | 166 | WORK="${WORK} ${FLAGS_working_dir}/config.txt" |
| 167 | |
Che-Liang Chiou | 69faa26 | 2011-06-01 11:52:21 +0800 | [diff] [blame] | 168 | # arm does not need/have a bootloader in kernel partition |
| 169 | dd if="/dev/zero" of="${FLAGS_working_dir}/bootloader.bin" bs=512 count=1 |
| 170 | WORK="${WORK} ${FLAGS_working_dir}/bootloader.bin" |
Che-Liang Chiou | 75ac2be | 2011-02-24 12:00:16 +0800 | [diff] [blame] | 171 | |
Che-Liang Chiou | 69faa26 | 2011-06-01 11:52:21 +0800 | [diff] [blame] | 172 | bootloader_path="${FLAGS_working_dir}/bootloader.bin" |
Che-Liang Chiou | 75ac2be | 2011-02-24 12:00:16 +0800 | [diff] [blame] | 173 | kernel_image="${FLAGS_vmlinuz/vmlinuz/vmlinux.uimg}" |
Che-Liang Chiou | 75ac2be | 2011-02-24 12:00:16 +0800 | [diff] [blame] | 174 | else |
| 175 | error "Unknown arch: ${FLAGS_arch}" |
| 176 | fi |
| 177 | |
Che-Liang Chiou | e51bdf2 | 2011-07-26 21:19:24 +0800 | [diff] [blame] | 178 | # We sign the image with the recovery_key, because this is what goes onto the |
| 179 | # USB key. We can only boot from the USB drive in recovery mode. |
| 180 | # For dev install shim, we need to use the installer keyblock instead of |
| 181 | # the recovery keyblock because of the difference in flags. |
| 182 | if [ ${FLAGS_use_dev_keys} -eq ${FLAGS_TRUE} ]; then |
| 183 | USB_KEYBLOCK=installer_kernel.keyblock |
| 184 | info "DEBUG: use dev install signing key" |
Che-Liang Chiou | 75ac2be | 2011-02-24 12:00:16 +0800 | [diff] [blame] | 185 | else |
Che-Liang Chiou | e51bdf2 | 2011-07-26 21:19:24 +0800 | [diff] [blame] | 186 | USB_KEYBLOCK=recovery_kernel.keyblock |
| 187 | info "DEBUG: use recovery signing key" |
Will Drewry | 69563b7 | 2010-06-24 16:12:58 -0500 | [diff] [blame] | 188 | fi |
| 189 | |
Che-Liang Chiou | e51bdf2 | 2011-07-26 21:19:24 +0800 | [diff] [blame] | 190 | # Create and sign the kernel blob |
| 191 | vbutil_kernel \ |
| 192 | --pack "${FLAGS_to}" \ |
| 193 | --keyblock "${FLAGS_keys_dir}/${USB_KEYBLOCK}" \ |
| 194 | --signprivate "${FLAGS_keys_dir}/recovery_kernel_data_key.vbprivk" \ |
| 195 | --version 1 \ |
| 196 | --config "${FLAGS_working_dir}/config.txt" \ |
| 197 | --bootloader "${bootloader_path}" \ |
| 198 | --vmlinuz "${kernel_image}" \ |
| 199 | --arch "${FLAGS_arch}" |
| 200 | |
| 201 | # And verify it. |
| 202 | vbutil_kernel \ |
| 203 | --verify "${FLAGS_to}" \ |
| 204 | --signpubkey "${FLAGS_keys_dir}/recovery_key.vbpubk" |
| 205 | |
| 206 | |
| 207 | # Now we re-sign the same image using the normal keys. This is the kernel |
| 208 | # image that is put on the hard disk by the installer. Note: To save space on |
| 209 | # the USB image, we're only emitting the new verfication block, and the |
| 210 | # installer just replaces that part of the hard disk's kernel partition. |
| 211 | vbutil_kernel \ |
| 212 | --repack "${FLAGS_hd_vblock}" \ |
| 213 | --vblockonly \ |
| 214 | --keyblock "${FLAGS_keys_dir}/kernel.keyblock" \ |
| 215 | --signprivate "${FLAGS_keys_dir}/kernel_data_key.vbprivk" \ |
| 216 | --oldblob "${FLAGS_to}" |
| 217 | |
| 218 | |
| 219 | # To verify it, we have to replace the vblock from the original image. |
| 220 | tempfile=$(mktemp) |
| 221 | trap "rm -f $tempfile" EXIT |
| 222 | cat "${FLAGS_hd_vblock}" > $tempfile |
| 223 | dd if="${FLAGS_to}" bs=65536 skip=1 >> $tempfile |
| 224 | |
| 225 | vbutil_kernel \ |
| 226 | --verify $tempfile \ |
| 227 | --signpubkey "${FLAGS_keys_dir}/kernel_subkey.vbpubk" |
| 228 | |
| 229 | rm -f $tempfile |
| 230 | trap - EXIT |
| 231 | |
Will Drewry | 69563b7 | 2010-06-24 16:12:58 -0500 | [diff] [blame] | 232 | set +e # cleanup failure is a-ok |
| 233 | |
| 234 | if [[ ${FLAGS_keep_work} -eq ${FLAGS_FALSE} ]]; then |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 235 | info "Cleaning up temporary files: ${WORK}" |
Will Drewry | 69563b7 | 2010-06-24 16:12:58 -0500 | [diff] [blame] | 236 | rm ${WORK} |
| 237 | rmdir ${FLAGS_working_dir} |
| 238 | fi |
| 239 | |
Will Drewry | bcbf1c4 | 2010-07-03 10:23:30 -0500 | [diff] [blame] | 240 | info "Kernel partition image emitted: ${FLAGS_to}" |
| 241 | |
| 242 | if [[ -f ${FLAGS_rootfs_hash} ]]; then |
| 243 | info "Root filesystem hash emitted: ${FLAGS_rootfs_hash}" |
| 244 | fi |