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