blob: 80b17b316579ebb99a0f2bf8077b808cc7170142 [file] [log] [blame]
Will Drewry69563b72010-06-24 16:12:58 -05001#!/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 Harringaa13ea42012-03-15 18:31:03 -07009SCRIPT_ROOT=$(dirname $(readlink -f "$0"))
David James359d3e12012-07-10 13:09:48 -070010. "${SCRIPT_ROOT}/common.sh" || exit 1
Will Drewry69563b72010-06-24 16:12:58 -050011
12get_default_board
13
14# Flags.
15DEFINE_string arch "x86" \
Sonny Rao0679b362011-10-12 13:53:19 -070016 "The boot architecture: arm, x86, or amd64. (Default: x86)"
Will Drewry69563b72010-06-24 16:12:58 -050017DEFINE_string to "/tmp/vmlinuz.image" \
18 "The path to the kernel image to be created. (Default: /tmp/vmlinuz.image)"
Louis Yung-Chieh Lo36020402010-07-05 13:23:34 +080019DEFINE_string hd_vblock "/tmp/vmlinuz_hd.vblock" \
20 "The path to the installed kernel's vblock (Default: /tmp/vmlinuz_hd.vblock)"
Will Drewry69563b72010-06-24 16:12:58 -050021DEFINE_string vmlinuz "vmlinuz" \
22 "The path to the kernel (Default: vmlinuz)"
23DEFINE_string working_dir "/tmp/vmlinuz.working" \
24 "Working directory for in-progress files. (Default: /tmp/vmlinuz.working)"
25DEFINE_boolean keep_work ${FLAGS_FALSE} \
26 "Keep temporary files (*.keyblock, *.vbpubk). (Default: false)"
27DEFINE_string keys_dir "${SRC_ROOT}/platform/vboot_reference/tests/testkeys" \
Bill Richardson2ace49e2010-07-01 10:23:27 -070028 "Directory with the RSA signing keys. (Defaults to test keys)"
Tan Gao843b70a2010-08-17 09:41:48 -070029DEFINE_boolean use_dev_keys ${FLAGS_FALSE} \
30 "Use developer keys for signing. (Default: false)"
Will Drewrybcbf1c42010-07-03 10:23:30 -050031# Note, to enable verified boot, the caller would manually pass:
Will Drewryb910de82011-02-23 13:26:50 -060032# --boot_args='dm="... %U+1 %U+1 ..." \
Will Drewry69563b72010-06-24 16:12:58 -050033# --root=/dev/dm-0
34DEFINE_string boot_args "noinitrd" \
35 "Additional boot arguments to pass to the commandline (Default: noinitrd)"
Will Drewryb910de82011-02-23 13:26:50 -060036# 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 Chiou93b6eff2011-05-18 17:21:13 +080039DEFINE_string root "PARTUUID=%U/PARTNROFF=1" \
Will Drewryb910de82011-02-23 13:26:50 -060040 "Expected device root partition"
Will Drewrybcbf1c42010-07-03 10:23:30 -050041# If provided, will automatically add verified boot arguments.
42DEFINE_string rootfs_image "" \
43 "Optional path to the rootfs device or image.(Default: \"\")"
44DEFINE_string rootfs_hash "" \
45 "Optional path to output the rootfs hash to. (Default: \"\")"
Will Drewry1670d482010-07-09 13:08:38 -070046DEFINE_integer verity_error_behavior 2 \
Will Drewrybcbf1c42010-07-03 10:23:30 -050047 "Verified boot error behavior [0: I/O errors, 1: reboot, 2: nothing] \
48(Default: 2)"
Will Drewryb910de82011-02-23 13:26:50 -060049DEFINE_integer verity_max_ios -1 \
50 "Optional number of outstanding I/O operations. (Default: -1)"
Will Drewry1670d482010-07-09 13:08:38 -070051DEFINE_string verity_hash_alg "sha1" \
52 "Cryptographic hash algorithm used for dm-verity. (Default: sha1)"
Elly Jones9ca3e4c2011-09-26 15:18:19 -040053DEFINE_string verity_salt "" \
54 "Salt to use for rootfs hash (Default: \"\")"
Will Drewrybcbf1c42010-07-03 10:23:30 -050055
Will Drewry69563b72010-06-24 16:12:58 -050056# Parse flags
57FLAGS "$@" || exit 1
58eval set -- "${FLAGS_ARGV}"
59
60# Die on error
Brian Harring7f175a52012-03-02 05:37:00 -080061switch_to_strict_mode
Will Drewry69563b72010-06-24 16:12:58 -050062
Will Drewry1670d482010-07-09 13:08:38 -070063verity_args=
Will Drewrybcbf1c42010-07-03 10:23:30 -050064# Even with a rootfs_image, root= is not changed unless specified.
65if [[ -n "${FLAGS_rootfs_image}" && -n "${FLAGS_rootfs_hash}" ]]; then
Will Drewrybcbf1c42010-07-03 10:23:30 -050066 # Gets the number of blocks. 4096 byte blocks _are_ expected.
Da Zheng87465ea2011-07-30 16:25:28 -070067 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 Drewrybcbf1c42010-07-03 10:23:30 -050073 grep "Block count" |
74 tr -d ' ' |
75 cut -f2 -d:)
Da Zheng87465ea2011-07-30 16:25:28 -070076 root_fs_block_sz=$(sudo dumpe2fs "${FLAGS_rootfs_image}" 2> /dev/null |
Will Drewrybcbf1c42010-07-03 10:23:30 -050077 grep "Block size" |
78 tr -d ' ' |
79 cut -f2 -d:)
Da Zheng87465ea2011-07-30 16:25:28 -070080 fi
81
Will Drewry78992a32010-07-21 14:02:20 -050082 info "rootfs is ${root_fs_blocks} blocks of ${root_fs_block_sz} bytes"
Will Drewrybcbf1c42010-07-03 10:23:30 -050083 if [[ ${root_fs_block_sz} -ne 4096 ]]; then
84 error "Root file system blocks are not 4k!"
85 fi
86
Elly Jones9ca3e4c2011-09-26 15:18:19 -040087 info "Generating root fs hash tree (salt '${FLAGS_verity_salt}')."
Will Drewrybcbf1c42010-07-03 10:23:30 -050088 # Runs as sudo in case the image is a block device.
Mandeep Singh Baines118692a2011-04-28 13:50:33 -070089 # First argument to verity is reserved/unused and MUST be 0
Elly Jones5e7a4302011-05-27 16:08:16 -040090 table=$(sudo verity mode=create \
91 alg=${FLAGS_verity_hash_alg} \
92 payload=${FLAGS_rootfs_image} \
93 payload_blocks=${root_fs_blocks} \
Elly Jonesf4e48332011-09-02 13:25:04 -040094 hashtree=${FLAGS_rootfs_hash} \
Elly Jones9ca3e4c2011-09-26 15:18:19 -040095 salt=${FLAGS_verity_salt})
Will Drewry821d07c2010-07-03 17:14:58 -070096 if [[ -f "${FLAGS_rootfs_hash}" ]]; then
97 sudo chmod a+r "${FLAGS_rootfs_hash}"
98 fi
Will Drewrybcbf1c42010-07-03 10:23:30 -050099 # 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 Sandersffd0ca62011-06-25 01:20:24 -0700103 base_root='%U+1' # kern_guid + 1
Kenneth Watersed54d932010-09-21 10:29:54 -0700104 table=${table//HASH_DEV/${base_root}}
105 table=${table//ROOT_DEV/${base_root}}
Will Drewrybcbf1c42010-07-03 10:23:30 -0500106 fi
Will Drewry78992a32010-07-21 14:02:20 -0500107 verity_args="dm=\"vroot none ro,${table}\""
Will Drewry1670d482010-07-09 13:08:38 -0700108 info "dm-verity configuration: ${verity_args}"
Will Drewrybcbf1c42010-07-03 10:23:30 -0500109fi
110
111mkdir -p "${FLAGS_working_dir}"
Will Drewryd6435d42010-10-20 15:37:46 -0500112
113# Only let dm-verity block if rootfs verification is configured.
114dev_wait=0
115if [[ ${FLAGS_root} = "/dev/dm-0" ]]; then
116 dev_wait=1
117fi
118
Will Drewrybcbf1c42010-07-03 10:23:30 -0500119cat <<EOF > "${FLAGS_working_dir}/boot.config"
120root=${FLAGS_root}
Olof Johanssone0b75512011-01-26 14:31:26 -0800121rootwait
122ro
Will Drewry1670d482010-07-09 13:08:38 -0700123dm_verity.error_behavior=${FLAGS_verity_error_behavior}
124dm_verity.max_bios=${FLAGS_verity_max_ios}
Will Drewryd6435d42010-10-20 15:37:46 -0500125dm_verity.dev_wait=${dev_wait}
Will Drewry1670d482010-07-09 13:08:38 -0700126${verity_args}
Will Drewrybcbf1c42010-07-03 10:23:30 -0500127${FLAGS_boot_args}
Pawel Osciaka2c23752011-11-15 15:09:45 -0800128vt.global_cursor_default=0
Doug Anderson2dd2e8e2012-01-06 09:32:24 -0800129kern_guid=%U
Will Drewrybcbf1c42010-07-03 10:23:30 -0500130EOF
131
132WORK="${WORK} ${FLAGS_working_dir}/boot.config"
133info "Emitted cross-platform boot params to ${FLAGS_working_dir}/boot.config"
134
Olof Johansson1eafb5a2012-08-01 00:04:30 -0700135# Add common boot options first.
136cat <<EOF | cat - "${FLAGS_working_dir}/boot.config" \
137 > "${FLAGS_working_dir}/config.txt"
Olof Johansson24e10cb2011-04-07 15:08:08 -0700138quiet
Sean Paul078164e2012-05-08 10:34:24 -0400139loglevel=0
Nick Sandersada8a1f2010-10-26 02:56:51 -0700140console=tty2
Will Drewry69563b72010-06-24 16:12:58 -0500141init=/sbin/init
Olof Johansson1eafb5a2012-08-01 00:04:30 -0700142cros_secure
143EOF
144
145if [[ "${FLAGS_arch}" = "x86" || "${FLAGS_arch}" = "amd64" ]]; then
146 # Legacy BIOS will use the kernel in the rootfs (via syslinux), as will
147 # standard EFI BIOS (via grub, from the EFI System Partition). Chrome OS
148 # BIOS will use a separate signed kernel partition, which we'll create now.
149 cat <<EOF | cat - "${FLAGS_working_dir}/boot.config" \
150 >> "${FLAGS_working_dir}/config.txt"
Will Drewry69563b72010-06-24 16:12:58 -0500151add_efi_memmap
152boot=local
Will Drewry69563b72010-06-24 16:12:58 -0500153noresume
154noswap
155i915.modeset=1
Luigi Semenzato195aebe2010-10-20 17:35:00 -0700156tpm_tis.force=1
157tpm_tis.interrupts=0
Vadim Bendebury8e623f62011-02-28 10:28:31 -0800158nmi_watchdog=panic,lapic
Will Drewry69563b72010-06-24 16:12:58 -0500159EOF
Will Drewrybcbf1c42010-07-03 10:23:30 -0500160 WORK="${WORK} ${FLAGS_working_dir}/config.txt"
Will Drewry69563b72010-06-24 16:12:58 -0500161
Che-Liang Chiou75ac2be2011-02-24 12:00:16 +0800162 bootloader_path="/lib64/bootstub/bootstub.efi"
163 kernel_image="${FLAGS_vmlinuz}"
Che-Liang Chiou75ac2be2011-02-24 12:00:16 +0800164elif [[ "${FLAGS_arch}" = "arm" ]]; then
Che-Liang Chiou75ac2be2011-02-24 12:00:16 +0800165 WORK="${WORK} ${FLAGS_working_dir}/config.txt"
166
Che-Liang Chiou69faa262011-06-01 11:52:21 +0800167 # arm does not need/have a bootloader in kernel partition
168 dd if="/dev/zero" of="${FLAGS_working_dir}/bootloader.bin" bs=512 count=1
169 WORK="${WORK} ${FLAGS_working_dir}/bootloader.bin"
Che-Liang Chiou75ac2be2011-02-24 12:00:16 +0800170
Che-Liang Chiou69faa262011-06-01 11:52:21 +0800171 bootloader_path="${FLAGS_working_dir}/bootloader.bin"
Che-Liang Chiou75ac2be2011-02-24 12:00:16 +0800172 kernel_image="${FLAGS_vmlinuz/vmlinuz/vmlinux.uimg}"
Che-Liang Chiou75ac2be2011-02-24 12:00:16 +0800173else
174 error "Unknown arch: ${FLAGS_arch}"
175fi
176
Che-Liang Chioue51bdf22011-07-26 21:19:24 +0800177# We sign the image with the recovery_key, because this is what goes onto the
178# USB key. We can only boot from the USB drive in recovery mode.
179# For dev install shim, we need to use the installer keyblock instead of
180# the recovery keyblock because of the difference in flags.
181if [ ${FLAGS_use_dev_keys} -eq ${FLAGS_TRUE} ]; then
182 USB_KEYBLOCK=installer_kernel.keyblock
183 info "DEBUG: use dev install signing key"
Che-Liang Chiou75ac2be2011-02-24 12:00:16 +0800184else
Che-Liang Chioue51bdf22011-07-26 21:19:24 +0800185 USB_KEYBLOCK=recovery_kernel.keyblock
186 info "DEBUG: use recovery signing key"
Will Drewry69563b72010-06-24 16:12:58 -0500187fi
188
Che-Liang Chioue51bdf22011-07-26 21:19:24 +0800189# Create and sign the kernel blob
190vbutil_kernel \
191 --pack "${FLAGS_to}" \
192 --keyblock "${FLAGS_keys_dir}/${USB_KEYBLOCK}" \
193 --signprivate "${FLAGS_keys_dir}/recovery_kernel_data_key.vbprivk" \
194 --version 1 \
195 --config "${FLAGS_working_dir}/config.txt" \
196 --bootloader "${bootloader_path}" \
197 --vmlinuz "${kernel_image}" \
198 --arch "${FLAGS_arch}"
199
200# And verify it.
201vbutil_kernel \
202 --verify "${FLAGS_to}" \
203 --signpubkey "${FLAGS_keys_dir}/recovery_key.vbpubk"
204
205
206# Now we re-sign the same image using the normal keys. This is the kernel
207# image that is put on the hard disk by the installer. Note: To save space on
208# the USB image, we're only emitting the new verfication block, and the
209# installer just replaces that part of the hard disk's kernel partition.
210vbutil_kernel \
211 --repack "${FLAGS_hd_vblock}" \
212 --vblockonly \
213 --keyblock "${FLAGS_keys_dir}/kernel.keyblock" \
214 --signprivate "${FLAGS_keys_dir}/kernel_data_key.vbprivk" \
215 --oldblob "${FLAGS_to}"
216
217
218# To verify it, we have to replace the vblock from the original image.
219tempfile=$(mktemp)
220trap "rm -f $tempfile" EXIT
221cat "${FLAGS_hd_vblock}" > $tempfile
222dd if="${FLAGS_to}" bs=65536 skip=1 >> $tempfile
223
224vbutil_kernel \
225 --verify $tempfile \
226 --signpubkey "${FLAGS_keys_dir}/kernel_subkey.vbpubk"
227
228rm -f $tempfile
229trap - EXIT
230
Will Drewry69563b72010-06-24 16:12:58 -0500231set +e # cleanup failure is a-ok
232
233if [[ ${FLAGS_keep_work} -eq ${FLAGS_FALSE} ]]; then
Will Drewrybcbf1c42010-07-03 10:23:30 -0500234 info "Cleaning up temporary files: ${WORK}"
Will Drewry69563b72010-06-24 16:12:58 -0500235 rm ${WORK}
236 rmdir ${FLAGS_working_dir}
237fi
238
Will Drewrybcbf1c42010-07-03 10:23:30 -0500239info "Kernel partition image emitted: ${FLAGS_to}"
240
241if [[ -f ${FLAGS_rootfs_hash} ]]; then
242 info "Root filesystem hash emitted: ${FLAGS_rootfs_hash}"
243fi