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