blob: 9611c6dc1d5bf8671f16ae815ab076e85f4fc1ac [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
Will Drewry69563b72010-06-24 16:12:58 -050012# Flags.
13DEFINE_string arch "x86" \
Sonny Rao0679b362011-10-12 13:53:19 -070014 "The boot architecture: arm, x86, or amd64. (Default: x86)"
Will Drewry69563b72010-06-24 16:12:58 -050015DEFINE_string to "/tmp/vmlinuz.image" \
16 "The path to the kernel image to be created. (Default: /tmp/vmlinuz.image)"
Louis Yung-Chieh Lo36020402010-07-05 13:23:34 +080017DEFINE_string hd_vblock "/tmp/vmlinuz_hd.vblock" \
18 "The path to the installed kernel's vblock (Default: /tmp/vmlinuz_hd.vblock)"
Will Drewry69563b72010-06-24 16:12:58 -050019DEFINE_string vmlinuz "vmlinuz" \
20 "The path to the kernel (Default: vmlinuz)"
21DEFINE_string working_dir "/tmp/vmlinuz.working" \
22 "Working directory for in-progress files. (Default: /tmp/vmlinuz.working)"
23DEFINE_boolean keep_work ${FLAGS_FALSE} \
24 "Keep temporary files (*.keyblock, *.vbpubk). (Default: false)"
25DEFINE_string keys_dir "${SRC_ROOT}/platform/vboot_reference/tests/testkeys" \
Bill Richardson2ace49e2010-07-01 10:23:27 -070026 "Directory with the RSA signing keys. (Defaults to test keys)"
Tan Gao843b70a2010-08-17 09:41:48 -070027DEFINE_boolean use_dev_keys ${FLAGS_FALSE} \
28 "Use developer keys for signing. (Default: false)"
Will Drewrybcbf1c42010-07-03 10:23:30 -050029# Note, to enable verified boot, the caller would manually pass:
Will Drewryb910de82011-02-23 13:26:50 -060030# --boot_args='dm="... %U+1 %U+1 ..." \
Will Drewry69563b72010-06-24 16:12:58 -050031# --root=/dev/dm-0
32DEFINE_string boot_args "noinitrd" \
33 "Additional boot arguments to pass to the commandline (Default: noinitrd)"
Will Drewrybcbf1c42010-07-03 10:23:30 -050034# If provided, will automatically add verified boot arguments.
35DEFINE_string rootfs_image "" \
36 "Optional path to the rootfs device or image.(Default: \"\")"
37DEFINE_string rootfs_hash "" \
38 "Optional path to output the rootfs hash to. (Default: \"\")"
Liam McLoughline81a2322012-09-24 10:37:34 +000039DEFINE_integer verity_error_behavior 3 \
Will Drewrybcbf1c42010-07-03 10:23:30 -050040 "Verified boot error behavior [0: I/O errors, 1: reboot, 2: nothing] \
Liam McLoughline81a2322012-09-24 10:37:34 +000041(Default: 3)"
Will Drewryb910de82011-02-23 13:26:50 -060042DEFINE_integer verity_max_ios -1 \
43 "Optional number of outstanding I/O operations. (Default: -1)"
Will Drewry1670d482010-07-09 13:08:38 -070044DEFINE_string verity_hash_alg "sha1" \
45 "Cryptographic hash algorithm used for dm-verity. (Default: sha1)"
Elly Jones9ca3e4c2011-09-26 15:18:19 -040046DEFINE_string verity_salt "" \
47 "Salt to use for rootfs hash (Default: \"\")"
Paul Taysom5b2c7e92012-09-05 10:13:03 -070048DEFINE_boolean enable_rootfs_verification ${FLAGS_TRUE} \
49 "Enable kernel-based root fs integrity checking. (Default: true)"
Paul Taysom00df6f62012-11-20 12:35:40 -080050DEFINE_boolean enable_bootcache ${FLAGS_FALSE} \
51 "Enable boot cache to accelerate booting. (Default: false)"
Paul Taysom5e39bb62013-01-18 07:39:25 -080052DEFINE_string enable_serial "" \
53 "Enable serial port for printks. Example values: ttyS0"
Will Drewrybcbf1c42010-07-03 10:23:30 -050054
Will Drewry69563b72010-06-24 16:12:58 -050055# Parse flags
56FLAGS "$@" || exit 1
57eval set -- "${FLAGS_ARGV}"
58
59# Die on error
Brian Harring7f175a52012-03-02 05:37:00 -080060switch_to_strict_mode
Will Drewry69563b72010-06-24 16:12:58 -050061
Paul Taysom00df6f62012-11-20 12:35:40 -080062rootdigest() {
63 local digest=${table#*root_hexdigest=}
64 echo ${digest% salt*}
65}
66
67salt() {
68 local salt=${table#*salt=}
69 echo ${salt%}
70}
71
72hashstart() {
73 local hash=${table#*hashstart=}
74 echo ${hash% alg*}
75}
76
77# Estimate of sectors used by verity
78# (num blocks) * 32 (bytes per hash) * 2 (overhead) / 512 (bytes per sector)
79veritysize() {
80 echo $((root_fs_blocks * 32 * 2 / 512))
81}
82
83device_mapper_args=
Will Drewrybcbf1c42010-07-03 10:23:30 -050084# Even with a rootfs_image, root= is not changed unless specified.
85if [[ -n "${FLAGS_rootfs_image}" && -n "${FLAGS_rootfs_hash}" ]]; then
Will Drewrybcbf1c42010-07-03 10:23:30 -050086 # Gets the number of blocks. 4096 byte blocks _are_ expected.
Da Zheng87465ea2011-07-30 16:25:28 -070087 if [ -f "${FLAGS_rootfs_image}" ]; then
88 root_fs_block_sz=4096
89 root_fs_sz=$(stat -c '%s' ${FLAGS_rootfs_image})
90 root_fs_blocks=$((root_fs_sz / ${root_fs_block_sz}))
91 else
92 root_fs_blocks=$(sudo dumpe2fs "${FLAGS_rootfs_image}" 2> /dev/null |
Will Drewrybcbf1c42010-07-03 10:23:30 -050093 grep "Block count" |
94 tr -d ' ' |
95 cut -f2 -d:)
Da Zheng87465ea2011-07-30 16:25:28 -070096 root_fs_block_sz=$(sudo dumpe2fs "${FLAGS_rootfs_image}" 2> /dev/null |
Will Drewrybcbf1c42010-07-03 10:23:30 -050097 grep "Block size" |
98 tr -d ' ' |
99 cut -f2 -d:)
Da Zheng87465ea2011-07-30 16:25:28 -0700100 fi
101
Will Drewry78992a32010-07-21 14:02:20 -0500102 info "rootfs is ${root_fs_blocks} blocks of ${root_fs_block_sz} bytes"
Will Drewrybcbf1c42010-07-03 10:23:30 -0500103 if [[ ${root_fs_block_sz} -ne 4096 ]]; then
104 error "Root file system blocks are not 4k!"
105 fi
106
Elly Jones9ca3e4c2011-09-26 15:18:19 -0400107 info "Generating root fs hash tree (salt '${FLAGS_verity_salt}')."
Will Drewrybcbf1c42010-07-03 10:23:30 -0500108 # Runs as sudo in case the image is a block device.
Mandeep Singh Baines118692a2011-04-28 13:50:33 -0700109 # First argument to verity is reserved/unused and MUST be 0
Elly Jones5e7a4302011-05-27 16:08:16 -0400110 table=$(sudo verity mode=create \
111 alg=${FLAGS_verity_hash_alg} \
112 payload=${FLAGS_rootfs_image} \
113 payload_blocks=${root_fs_blocks} \
Elly Jonesf4e48332011-09-02 13:25:04 -0400114 hashtree=${FLAGS_rootfs_hash} \
Elly Jones9ca3e4c2011-09-26 15:18:19 -0400115 salt=${FLAGS_verity_salt})
Will Drewry821d07c2010-07-03 17:14:58 -0700116 if [[ -f "${FLAGS_rootfs_hash}" ]]; then
117 sudo chmod a+r "${FLAGS_rootfs_hash}"
118 fi
Paul Taysom5b2c7e92012-09-05 10:13:03 -0700119 # Don't claim the root device unless verity is enabled.
120 # Doing so will claim /dev/sdDP out from under the system.
121 if [[ ${FLAGS_enable_rootfs_verification} -eq ${FLAGS_TRUE} ]]; then
Paul Taysom00df6f62012-11-20 12:35:40 -0800122 if [[ ${FLAGS_enable_bootcache} -eq ${FLAGS_TRUE} ]]; then
123 base_root='254:0' # major:minor numbers for /dev/dm-0
124 else
Paul Taysomb6cf37f2013-06-06 09:35:07 -0700125 base_root='PARTUUID=%U/PARTNROFF=1' # kern_guid + 1
Paul Taysom00df6f62012-11-20 12:35:40 -0800126 fi
Kenneth Watersed54d932010-09-21 10:29:54 -0700127 table=${table//HASH_DEV/${base_root}}
128 table=${table//ROOT_DEV/${base_root}}
Will Drewrybcbf1c42010-07-03 10:23:30 -0500129 fi
Paul Taysom00df6f62012-11-20 12:35:40 -0800130 verity_dev="vroot none ro 1,${table}"
131 if [[ ${FLAGS_enable_bootcache} -eq ${FLAGS_TRUE} ]]; then
132 signature=$(rootdigest)
133 cachestart=$(($(hashstart) + $(veritysize)))
134 size_limit=512
135 max_trace=20000
136 max_pages=100000
Paul Taysomb6cf37f2013-06-06 09:35:07 -0700137 bootcache_args="PARTUUID=%U/PARTNROFF=1"
138 bootcache_args+=" ${cachestart} ${signature} ${size_limit}"
139 bootcache_args+=" ${max_trace} ${max_pages}"
Paul Taysom00df6f62012-11-20 12:35:40 -0800140 bootcache_dev="vboot none ro 1,0 ${cachestart} bootcache ${bootcache_args}"
141 device_mapper_args="dm=\"2 ${bootcache_dev}, ${verity_dev}\""
142 else
143 device_mapper_args="dm=\"1 ${verity_dev}\""
144 fi
145 info "device mapper configuration: ${device_mapper_args}"
Will Drewrybcbf1c42010-07-03 10:23:30 -0500146fi
147
148mkdir -p "${FLAGS_working_dir}"
Will Drewryd6435d42010-10-20 15:37:46 -0500149
150# Only let dm-verity block if rootfs verification is configured.
Paul Taysom5b2c7e92012-09-05 10:13:03 -0700151# By default, we use a firmware enumerated value, but it isn't reliable for
152# production use. If +%d can be added upstream, then we can use:
153# root_dev=PARTUID=uuid+1
Will Drewryd6435d42010-10-20 15:37:46 -0500154dev_wait=0
Paul Taysom5b2c7e92012-09-05 10:13:03 -0700155root_dev="PARTUUID=%U/PARTNROFF=1"
156if [[ ${FLAGS_enable_rootfs_verification} -eq ${FLAGS_TRUE} ]]; then
Will Drewryd6435d42010-10-20 15:37:46 -0500157 dev_wait=1
Paul Taysom00df6f62012-11-20 12:35:40 -0800158 if [[ ${FLAGS_enable_bootcache} -eq ${FLAGS_TRUE} ]]; then
159 root_dev=/dev/dm-1
160 else
161 root_dev=/dev/dm-0
162 fi
163else
164 if [[ ${FLAGS_enable_bootcache} -eq ${FLAGS_TRUE} ]]; then
165 die "Having bootcache without verity is not supported"
166 fi
Will Drewryd6435d42010-10-20 15:37:46 -0500167fi
168
Paul Taysomb6cf37f2013-06-06 09:35:07 -0700169# kern_guid should eventually be changed to use PARTUUID
Will Drewrybcbf1c42010-07-03 10:23:30 -0500170cat <<EOF > "${FLAGS_working_dir}/boot.config"
Paul Taysom5b2c7e92012-09-05 10:13:03 -0700171root=${root_dev}
Olof Johanssone0b75512011-01-26 14:31:26 -0800172rootwait
173ro
Will Drewry1670d482010-07-09 13:08:38 -0700174dm_verity.error_behavior=${FLAGS_verity_error_behavior}
175dm_verity.max_bios=${FLAGS_verity_max_ios}
Will Drewryd6435d42010-10-20 15:37:46 -0500176dm_verity.dev_wait=${dev_wait}
Paul Taysom00df6f62012-11-20 12:35:40 -0800177${device_mapper_args}
Will Drewrybcbf1c42010-07-03 10:23:30 -0500178${FLAGS_boot_args}
Pawel Osciaka2c23752011-11-15 15:09:45 -0800179vt.global_cursor_default=0
Doug Anderson2dd2e8e2012-01-06 09:32:24 -0800180kern_guid=%U
Will Drewrybcbf1c42010-07-03 10:23:30 -0500181EOF
182
183WORK="${WORK} ${FLAGS_working_dir}/boot.config"
184info "Emitted cross-platform boot params to ${FLAGS_working_dir}/boot.config"
185
Olof Johansson1eafb5a2012-08-01 00:04:30 -0700186# Add common boot options first.
Paul Taysom5e39bb62013-01-18 07:39:25 -0800187config="${FLAGS_working_dir}/config.txt"
188if [[ -n ${FLAGS_enable_serial} ]]; then
189 console=${FLAGS_enable_serial}
190 if [[ ${console} != *,* ]]; then
191 console+=",115200n8"
192 fi
193 cat <<EOF > "${config}"
194console=${console}
195earlyprintk=${console}
196console=tty1
197EOF
198else
199 cat <<EOF > "${config}"
Sameer Nanda542cb532012-08-22 16:11:12 -0700200console=
Paul Taysom5e39bb62013-01-18 07:39:25 -0800201EOF
202fi
203
204cat <<EOF - "${FLAGS_working_dir}/boot.config" >> "${config}"
205loglevel=7
Will Drewry69563b72010-06-24 16:12:58 -0500206init=/sbin/init
Olof Johansson1eafb5a2012-08-01 00:04:30 -0700207cros_secure
Paul Taysom984acd72013-04-02 09:41:26 -0700208oops=panic
209panic=-1
Olof Johansson1eafb5a2012-08-01 00:04:30 -0700210EOF
211
212if [[ "${FLAGS_arch}" = "x86" || "${FLAGS_arch}" = "amd64" ]]; then
213 # Legacy BIOS will use the kernel in the rootfs (via syslinux), as will
214 # standard EFI BIOS (via grub, from the EFI System Partition). Chrome OS
215 # BIOS will use a separate signed kernel partition, which we'll create now.
Olof Johanssond7a8aaf2012-08-02 20:35:47 -0700216 cat <<EOF >> "${FLAGS_working_dir}/config.txt"
Will Drewry69563b72010-06-24 16:12:58 -0500217add_efi_memmap
218boot=local
Will Drewry69563b72010-06-24 16:12:58 -0500219noresume
220noswap
221i915.modeset=1
Luigi Semenzato195aebe2010-10-20 17:35:00 -0700222tpm_tis.force=1
223tpm_tis.interrupts=0
Vadim Bendebury8e623f62011-02-28 10:28:31 -0800224nmi_watchdog=panic,lapic
Will Drewry69563b72010-06-24 16:12:58 -0500225EOF
Will Drewrybcbf1c42010-07-03 10:23:30 -0500226 WORK="${WORK} ${FLAGS_working_dir}/config.txt"
Will Drewry69563b72010-06-24 16:12:58 -0500227
Che-Liang Chiou75ac2be2011-02-24 12:00:16 +0800228 bootloader_path="/lib64/bootstub/bootstub.efi"
229 kernel_image="${FLAGS_vmlinuz}"
Che-Liang Chiou75ac2be2011-02-24 12:00:16 +0800230elif [[ "${FLAGS_arch}" = "arm" ]]; then
Che-Liang Chiou75ac2be2011-02-24 12:00:16 +0800231 WORK="${WORK} ${FLAGS_working_dir}/config.txt"
232
Che-Liang Chiou69faa262011-06-01 11:52:21 +0800233 # arm does not need/have a bootloader in kernel partition
234 dd if="/dev/zero" of="${FLAGS_working_dir}/bootloader.bin" bs=512 count=1
235 WORK="${WORK} ${FLAGS_working_dir}/bootloader.bin"
Che-Liang Chiou75ac2be2011-02-24 12:00:16 +0800236
Che-Liang Chiou69faa262011-06-01 11:52:21 +0800237 bootloader_path="${FLAGS_working_dir}/bootloader.bin"
Che-Liang Chiou75ac2be2011-02-24 12:00:16 +0800238 kernel_image="${FLAGS_vmlinuz/vmlinuz/vmlinux.uimg}"
Che-Liang Chiou75ac2be2011-02-24 12:00:16 +0800239else
240 error "Unknown arch: ${FLAGS_arch}"
241fi
242
Che-Liang Chioue51bdf22011-07-26 21:19:24 +0800243# We sign the image with the recovery_key, because this is what goes onto the
244# USB key. We can only boot from the USB drive in recovery mode.
245# For dev install shim, we need to use the installer keyblock instead of
246# the recovery keyblock because of the difference in flags.
247if [ ${FLAGS_use_dev_keys} -eq ${FLAGS_TRUE} ]; then
248 USB_KEYBLOCK=installer_kernel.keyblock
249 info "DEBUG: use dev install signing key"
Che-Liang Chiou75ac2be2011-02-24 12:00:16 +0800250else
Che-Liang Chioue51bdf22011-07-26 21:19:24 +0800251 USB_KEYBLOCK=recovery_kernel.keyblock
252 info "DEBUG: use recovery signing key"
Will Drewry69563b72010-06-24 16:12:58 -0500253fi
254
Che-Liang Chioue51bdf22011-07-26 21:19:24 +0800255# Create and sign the kernel blob
256vbutil_kernel \
257 --pack "${FLAGS_to}" \
258 --keyblock "${FLAGS_keys_dir}/${USB_KEYBLOCK}" \
259 --signprivate "${FLAGS_keys_dir}/recovery_kernel_data_key.vbprivk" \
260 --version 1 \
261 --config "${FLAGS_working_dir}/config.txt" \
262 --bootloader "${bootloader_path}" \
263 --vmlinuz "${kernel_image}" \
264 --arch "${FLAGS_arch}"
265
266# And verify it.
267vbutil_kernel \
268 --verify "${FLAGS_to}" \
269 --signpubkey "${FLAGS_keys_dir}/recovery_key.vbpubk"
270
271
272# Now we re-sign the same image using the normal keys. This is the kernel
273# image that is put on the hard disk by the installer. Note: To save space on
274# the USB image, we're only emitting the new verfication block, and the
275# installer just replaces that part of the hard disk's kernel partition.
276vbutil_kernel \
277 --repack "${FLAGS_hd_vblock}" \
278 --vblockonly \
279 --keyblock "${FLAGS_keys_dir}/kernel.keyblock" \
280 --signprivate "${FLAGS_keys_dir}/kernel_data_key.vbprivk" \
281 --oldblob "${FLAGS_to}"
282
283
284# To verify it, we have to replace the vblock from the original image.
285tempfile=$(mktemp)
286trap "rm -f $tempfile" EXIT
287cat "${FLAGS_hd_vblock}" > $tempfile
288dd if="${FLAGS_to}" bs=65536 skip=1 >> $tempfile
289
290vbutil_kernel \
291 --verify $tempfile \
292 --signpubkey "${FLAGS_keys_dir}/kernel_subkey.vbpubk"
293
294rm -f $tempfile
295trap - EXIT
296
Will Drewry69563b72010-06-24 16:12:58 -0500297set +e # cleanup failure is a-ok
298
299if [[ ${FLAGS_keep_work} -eq ${FLAGS_FALSE} ]]; then
Will Drewrybcbf1c42010-07-03 10:23:30 -0500300 info "Cleaning up temporary files: ${WORK}"
Will Drewry69563b72010-06-24 16:12:58 -0500301 rm ${WORK}
302 rmdir ${FLAGS_working_dir}
303fi
304
Will Drewrybcbf1c42010-07-03 10:23:30 -0500305info "Kernel partition image emitted: ${FLAGS_to}"
306
307if [[ -f ${FLAGS_rootfs_hash} ]]; then
308 info "Root filesystem hash emitted: ${FLAGS_rootfs_hash}"
309fi