blob: 167e1991df79dfb1f88ab94af6eec14f270afb73 [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)"
Will Drewrybcbf1c42010-07-03 10:23:30 -050052
Will Drewry69563b72010-06-24 16:12:58 -050053# Parse flags
54FLAGS "$@" || exit 1
55eval set -- "${FLAGS_ARGV}"
56
57# Die on error
Brian Harring7f175a52012-03-02 05:37:00 -080058switch_to_strict_mode
Will Drewry69563b72010-06-24 16:12:58 -050059
Paul Taysom00df6f62012-11-20 12:35:40 -080060rootdigest() {
61 local digest=${table#*root_hexdigest=}
62 echo ${digest% salt*}
63}
64
65salt() {
66 local salt=${table#*salt=}
67 echo ${salt%}
68}
69
70hashstart() {
71 local hash=${table#*hashstart=}
72 echo ${hash% alg*}
73}
74
75# Estimate of sectors used by verity
76# (num blocks) * 32 (bytes per hash) * 2 (overhead) / 512 (bytes per sector)
77veritysize() {
78 echo $((root_fs_blocks * 32 * 2 / 512))
79}
80
81device_mapper_args=
Will Drewrybcbf1c42010-07-03 10:23:30 -050082# Even with a rootfs_image, root= is not changed unless specified.
83if [[ -n "${FLAGS_rootfs_image}" && -n "${FLAGS_rootfs_hash}" ]]; then
Will Drewrybcbf1c42010-07-03 10:23:30 -050084 # Gets the number of blocks. 4096 byte blocks _are_ expected.
Da Zheng87465ea2011-07-30 16:25:28 -070085 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 Drewrybcbf1c42010-07-03 10:23:30 -050091 grep "Block count" |
92 tr -d ' ' |
93 cut -f2 -d:)
Da Zheng87465ea2011-07-30 16:25:28 -070094 root_fs_block_sz=$(sudo dumpe2fs "${FLAGS_rootfs_image}" 2> /dev/null |
Will Drewrybcbf1c42010-07-03 10:23:30 -050095 grep "Block size" |
96 tr -d ' ' |
97 cut -f2 -d:)
Da Zheng87465ea2011-07-30 16:25:28 -070098 fi
99
Will Drewry78992a32010-07-21 14:02:20 -0500100 info "rootfs is ${root_fs_blocks} blocks of ${root_fs_block_sz} bytes"
Will Drewrybcbf1c42010-07-03 10:23:30 -0500101 if [[ ${root_fs_block_sz} -ne 4096 ]]; then
102 error "Root file system blocks are not 4k!"
103 fi
104
Elly Jones9ca3e4c2011-09-26 15:18:19 -0400105 info "Generating root fs hash tree (salt '${FLAGS_verity_salt}')."
Will Drewrybcbf1c42010-07-03 10:23:30 -0500106 # Runs as sudo in case the image is a block device.
Mandeep Singh Baines118692a2011-04-28 13:50:33 -0700107 # First argument to verity is reserved/unused and MUST be 0
Elly Jones5e7a4302011-05-27 16:08:16 -0400108 table=$(sudo verity mode=create \
109 alg=${FLAGS_verity_hash_alg} \
110 payload=${FLAGS_rootfs_image} \
111 payload_blocks=${root_fs_blocks} \
Elly Jonesf4e48332011-09-02 13:25:04 -0400112 hashtree=${FLAGS_rootfs_hash} \
Elly Jones9ca3e4c2011-09-26 15:18:19 -0400113 salt=${FLAGS_verity_salt})
Will Drewry821d07c2010-07-03 17:14:58 -0700114 if [[ -f "${FLAGS_rootfs_hash}" ]]; then
115 sudo chmod a+r "${FLAGS_rootfs_hash}"
116 fi
Paul Taysom5b2c7e92012-09-05 10:13:03 -0700117 # Don't claim the root device unless verity is enabled.
118 # Doing so will claim /dev/sdDP out from under the system.
119 if [[ ${FLAGS_enable_rootfs_verification} -eq ${FLAGS_TRUE} ]]; then
Paul Taysom00df6f62012-11-20 12:35:40 -0800120 if [[ ${FLAGS_enable_bootcache} -eq ${FLAGS_TRUE} ]]; then
121 base_root='254:0' # major:minor numbers for /dev/dm-0
122 else
123 base_root='%U+1' # kern_guid + 1
124 fi
Kenneth Watersed54d932010-09-21 10:29:54 -0700125 table=${table//HASH_DEV/${base_root}}
126 table=${table//ROOT_DEV/${base_root}}
Will Drewrybcbf1c42010-07-03 10:23:30 -0500127 fi
Paul Taysom00df6f62012-11-20 12:35:40 -0800128 verity_dev="vroot none ro 1,${table}"
129 if [[ ${FLAGS_enable_bootcache} -eq ${FLAGS_TRUE} ]]; then
130 signature=$(rootdigest)
131 cachestart=$(($(hashstart) + $(veritysize)))
132 size_limit=512
133 max_trace=20000
134 max_pages=100000
135 bootcache_args="%U+1 ${cachestart} ${signature} ${size_limit}"
136 bootcache_args="${bootcache_args} ${max_trace} ${max_pages}"
137 bootcache_dev="vboot none ro 1,0 ${cachestart} bootcache ${bootcache_args}"
138 device_mapper_args="dm=\"2 ${bootcache_dev}, ${verity_dev}\""
139 else
140 device_mapper_args="dm=\"1 ${verity_dev}\""
141 fi
142 info "device mapper configuration: ${device_mapper_args}"
Will Drewrybcbf1c42010-07-03 10:23:30 -0500143fi
144
145mkdir -p "${FLAGS_working_dir}"
Will Drewryd6435d42010-10-20 15:37:46 -0500146
147# Only let dm-verity block if rootfs verification is configured.
Paul Taysom5b2c7e92012-09-05 10:13:03 -0700148# By default, we use a firmware enumerated value, but it isn't reliable for
149# production use. If +%d can be added upstream, then we can use:
150# root_dev=PARTUID=uuid+1
Will Drewryd6435d42010-10-20 15:37:46 -0500151dev_wait=0
Paul Taysom5b2c7e92012-09-05 10:13:03 -0700152root_dev="PARTUUID=%U/PARTNROFF=1"
153if [[ ${FLAGS_enable_rootfs_verification} -eq ${FLAGS_TRUE} ]]; then
Will Drewryd6435d42010-10-20 15:37:46 -0500154 dev_wait=1
Paul Taysom00df6f62012-11-20 12:35:40 -0800155 if [[ ${FLAGS_enable_bootcache} -eq ${FLAGS_TRUE} ]]; then
156 root_dev=/dev/dm-1
157 else
158 root_dev=/dev/dm-0
159 fi
160else
161 if [[ ${FLAGS_enable_bootcache} -eq ${FLAGS_TRUE} ]]; then
162 die "Having bootcache without verity is not supported"
163 fi
Will Drewryd6435d42010-10-20 15:37:46 -0500164fi
165
Will Drewrybcbf1c42010-07-03 10:23:30 -0500166cat <<EOF > "${FLAGS_working_dir}/boot.config"
Paul Taysom5b2c7e92012-09-05 10:13:03 -0700167root=${root_dev}
Olof Johanssone0b75512011-01-26 14:31:26 -0800168rootwait
169ro
Will Drewry1670d482010-07-09 13:08:38 -0700170dm_verity.error_behavior=${FLAGS_verity_error_behavior}
171dm_verity.max_bios=${FLAGS_verity_max_ios}
Will Drewryd6435d42010-10-20 15:37:46 -0500172dm_verity.dev_wait=${dev_wait}
Paul Taysom00df6f62012-11-20 12:35:40 -0800173${device_mapper_args}
Will Drewrybcbf1c42010-07-03 10:23:30 -0500174${FLAGS_boot_args}
Pawel Osciaka2c23752011-11-15 15:09:45 -0800175vt.global_cursor_default=0
Doug Anderson2dd2e8e2012-01-06 09:32:24 -0800176kern_guid=%U
Will Drewrybcbf1c42010-07-03 10:23:30 -0500177EOF
178
179WORK="${WORK} ${FLAGS_working_dir}/boot.config"
180info "Emitted cross-platform boot params to ${FLAGS_working_dir}/boot.config"
181
Olof Johansson1eafb5a2012-08-01 00:04:30 -0700182# Add common boot options first.
183cat <<EOF | cat - "${FLAGS_working_dir}/boot.config" \
184 > "${FLAGS_working_dir}/config.txt"
Sameer Nanda542cb532012-08-22 16:11:12 -0700185loglevel=7
186console=
Will Drewry69563b72010-06-24 16:12:58 -0500187init=/sbin/init
Olof Johansson1eafb5a2012-08-01 00:04:30 -0700188cros_secure
189EOF
190
191if [[ "${FLAGS_arch}" = "x86" || "${FLAGS_arch}" = "amd64" ]]; then
192 # Legacy BIOS will use the kernel in the rootfs (via syslinux), as will
193 # standard EFI BIOS (via grub, from the EFI System Partition). Chrome OS
194 # BIOS will use a separate signed kernel partition, which we'll create now.
Olof Johanssond7a8aaf2012-08-02 20:35:47 -0700195 cat <<EOF >> "${FLAGS_working_dir}/config.txt"
Will Drewry69563b72010-06-24 16:12:58 -0500196add_efi_memmap
197boot=local
Will Drewry69563b72010-06-24 16:12:58 -0500198noresume
199noswap
200i915.modeset=1
Luigi Semenzato195aebe2010-10-20 17:35:00 -0700201tpm_tis.force=1
202tpm_tis.interrupts=0
Vadim Bendebury8e623f62011-02-28 10:28:31 -0800203nmi_watchdog=panic,lapic
Will Drewry69563b72010-06-24 16:12:58 -0500204EOF
Will Drewrybcbf1c42010-07-03 10:23:30 -0500205 WORK="${WORK} ${FLAGS_working_dir}/config.txt"
Will Drewry69563b72010-06-24 16:12:58 -0500206
Che-Liang Chiou75ac2be2011-02-24 12:00:16 +0800207 bootloader_path="/lib64/bootstub/bootstub.efi"
208 kernel_image="${FLAGS_vmlinuz}"
Che-Liang Chiou75ac2be2011-02-24 12:00:16 +0800209elif [[ "${FLAGS_arch}" = "arm" ]]; then
Che-Liang Chiou75ac2be2011-02-24 12:00:16 +0800210 WORK="${WORK} ${FLAGS_working_dir}/config.txt"
211
Che-Liang Chiou69faa262011-06-01 11:52:21 +0800212 # arm does not need/have a bootloader in kernel partition
213 dd if="/dev/zero" of="${FLAGS_working_dir}/bootloader.bin" bs=512 count=1
214 WORK="${WORK} ${FLAGS_working_dir}/bootloader.bin"
Che-Liang Chiou75ac2be2011-02-24 12:00:16 +0800215
Che-Liang Chiou69faa262011-06-01 11:52:21 +0800216 bootloader_path="${FLAGS_working_dir}/bootloader.bin"
Che-Liang Chiou75ac2be2011-02-24 12:00:16 +0800217 kernel_image="${FLAGS_vmlinuz/vmlinuz/vmlinux.uimg}"
Che-Liang Chiou75ac2be2011-02-24 12:00:16 +0800218else
219 error "Unknown arch: ${FLAGS_arch}"
220fi
221
Che-Liang Chioue51bdf22011-07-26 21:19:24 +0800222# We sign the image with the recovery_key, because this is what goes onto the
223# USB key. We can only boot from the USB drive in recovery mode.
224# For dev install shim, we need to use the installer keyblock instead of
225# the recovery keyblock because of the difference in flags.
226if [ ${FLAGS_use_dev_keys} -eq ${FLAGS_TRUE} ]; then
227 USB_KEYBLOCK=installer_kernel.keyblock
228 info "DEBUG: use dev install signing key"
Che-Liang Chiou75ac2be2011-02-24 12:00:16 +0800229else
Che-Liang Chioue51bdf22011-07-26 21:19:24 +0800230 USB_KEYBLOCK=recovery_kernel.keyblock
231 info "DEBUG: use recovery signing key"
Will Drewry69563b72010-06-24 16:12:58 -0500232fi
233
Che-Liang Chioue51bdf22011-07-26 21:19:24 +0800234# Create and sign the kernel blob
235vbutil_kernel \
236 --pack "${FLAGS_to}" \
237 --keyblock "${FLAGS_keys_dir}/${USB_KEYBLOCK}" \
238 --signprivate "${FLAGS_keys_dir}/recovery_kernel_data_key.vbprivk" \
239 --version 1 \
240 --config "${FLAGS_working_dir}/config.txt" \
241 --bootloader "${bootloader_path}" \
242 --vmlinuz "${kernel_image}" \
243 --arch "${FLAGS_arch}"
244
245# And verify it.
246vbutil_kernel \
247 --verify "${FLAGS_to}" \
248 --signpubkey "${FLAGS_keys_dir}/recovery_key.vbpubk"
249
250
251# Now we re-sign the same image using the normal keys. This is the kernel
252# image that is put on the hard disk by the installer. Note: To save space on
253# the USB image, we're only emitting the new verfication block, and the
254# installer just replaces that part of the hard disk's kernel partition.
255vbutil_kernel \
256 --repack "${FLAGS_hd_vblock}" \
257 --vblockonly \
258 --keyblock "${FLAGS_keys_dir}/kernel.keyblock" \
259 --signprivate "${FLAGS_keys_dir}/kernel_data_key.vbprivk" \
260 --oldblob "${FLAGS_to}"
261
262
263# To verify it, we have to replace the vblock from the original image.
264tempfile=$(mktemp)
265trap "rm -f $tempfile" EXIT
266cat "${FLAGS_hd_vblock}" > $tempfile
267dd if="${FLAGS_to}" bs=65536 skip=1 >> $tempfile
268
269vbutil_kernel \
270 --verify $tempfile \
271 --signpubkey "${FLAGS_keys_dir}/kernel_subkey.vbpubk"
272
273rm -f $tempfile
274trap - EXIT
275
Will Drewry69563b72010-06-24 16:12:58 -0500276set +e # cleanup failure is a-ok
277
278if [[ ${FLAGS_keep_work} -eq ${FLAGS_FALSE} ]]; then
Will Drewrybcbf1c42010-07-03 10:23:30 -0500279 info "Cleaning up temporary files: ${WORK}"
Will Drewry69563b72010-06-24 16:12:58 -0500280 rm ${WORK}
281 rmdir ${FLAGS_working_dir}
282fi
283
Will Drewrybcbf1c42010-07-03 10:23:30 -0500284info "Kernel partition image emitted: ${FLAGS_to}"
285
286if [[ -f ${FLAGS_rootfs_hash} ]]; then
287 info "Root filesystem hash emitted: ${FLAGS_rootfs_hash}"
288fi