blob: e0e282490bb682de33bf8c788c19103da7ea8b21 [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)"
Albert Chaulk5d190f72013-07-12 11:42:18 -070015DEFINE_string board "${DEFAULT_BOARD}" \
16 "Board we're building for."
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 Drewrybcbf1c42010-07-03 10:23:30 -050036# If provided, will automatically add verified boot arguments.
37DEFINE_string rootfs_image "" \
38 "Optional path to the rootfs device or image.(Default: \"\")"
39DEFINE_string rootfs_hash "" \
40 "Optional path to output the rootfs hash to. (Default: \"\")"
Liam McLoughline81a2322012-09-24 10:37:34 +000041DEFINE_integer verity_error_behavior 3 \
Will Drewrybcbf1c42010-07-03 10:23:30 -050042 "Verified boot error behavior [0: I/O errors, 1: reboot, 2: nothing] \
Liam McLoughline81a2322012-09-24 10:37:34 +000043(Default: 3)"
Will Drewryb910de82011-02-23 13:26:50 -060044DEFINE_integer verity_max_ios -1 \
45 "Optional number of outstanding I/O operations. (Default: -1)"
Will Drewry1670d482010-07-09 13:08:38 -070046DEFINE_string verity_hash_alg "sha1" \
47 "Cryptographic hash algorithm used for dm-verity. (Default: sha1)"
Elly Jones9ca3e4c2011-09-26 15:18:19 -040048DEFINE_string verity_salt "" \
49 "Salt to use for rootfs hash (Default: \"\")"
Paul Taysom5b2c7e92012-09-05 10:13:03 -070050DEFINE_boolean enable_rootfs_verification ${FLAGS_TRUE} \
51 "Enable kernel-based root fs integrity checking. (Default: true)"
Paul Taysom00df6f62012-11-20 12:35:40 -080052DEFINE_boolean enable_bootcache ${FLAGS_FALSE} \
53 "Enable boot cache to accelerate booting. (Default: false)"
Paul Taysom5e39bb62013-01-18 07:39:25 -080054DEFINE_string enable_serial "" \
55 "Enable serial port for printks. Example values: ttyS0"
Will Drewrybcbf1c42010-07-03 10:23:30 -050056
Will Drewry69563b72010-06-24 16:12:58 -050057# Parse flags
58FLAGS "$@" || exit 1
59eval set -- "${FLAGS_ARGV}"
60
61# Die on error
Brian Harring7f175a52012-03-02 05:37:00 -080062switch_to_strict_mode
Will Drewry69563b72010-06-24 16:12:58 -050063
Mike Frysinger94a82df2013-11-26 14:51:50 -050064# N.B. Ordering matters for some of the libraries below, because
65# some of the files contain initialization used by later files.
66. "${BUILD_LIBRARY_DIR}/board_options.sh" || exit 1
67
Paul Taysom00df6f62012-11-20 12:35:40 -080068rootdigest() {
69 local digest=${table#*root_hexdigest=}
70 echo ${digest% salt*}
71}
72
73salt() {
74 local salt=${table#*salt=}
75 echo ${salt%}
76}
77
78hashstart() {
79 local hash=${table#*hashstart=}
80 echo ${hash% alg*}
81}
82
83# Estimate of sectors used by verity
84# (num blocks) * 32 (bytes per hash) * 2 (overhead) / 512 (bytes per sector)
85veritysize() {
86 echo $((root_fs_blocks * 32 * 2 / 512))
87}
88
Chris Masone175a2692014-01-21 16:06:44 -080089# Munge the kernel command line.
90# Intended to be overridden by boards that wish to add to the command line.
91# $1 - Configuration file containing boot args.
92modify_kernel_command_line() {
93 :
94}
95
Albert Chaulk5d190f72013-07-12 11:42:18 -070096# Construct a kernel image that is verifiable in some way - vboot, signed, etc.
97# $1 - Destination file.
98# $2 - Kernel binary.
99# $3 - Bootloader binary.
100# $4 - Directory where the signing keys are stored.
101# $5 - Keyblock file for vboot, installer of recovery.
102# $6 - Output vblock file for the kernel.
103# $7 - Configuration file containing boot args.
104# $8 - Architecture we are building on (x86, arm, etc).
105build_verified_kernel() {
106 local dst=$1 kernel=$2 bootloader_path=$3 keydir=$4 keyblock=$5 vblock=$6
107 local configfile=$7 arch=$8
108 # Create and sign the kernel blob
109 vbutil_kernel \
110 --pack "${dst}" \
111 --keyblock "${keydir}/${keyblock}" \
112 --signprivate "${keydir}/recovery_kernel_data_key.vbprivk" \
113 --version 1 \
114 --config "${configfile}" \
115 --bootloader "${bootloader_path}" \
116 --vmlinuz "${kernel}" \
117 --arch "${arch}"
118
119 # And verify it.
120 vbutil_kernel \
121 --verify "${dst}" \
122 --signpubkey "${keydir}/recovery_key.vbpubk"
123
124 # Now we re-sign the same image using the normal keys. This is the kernel
125 # image that is put on the hard disk by the installer. Note: To save space on
126 # the USB image, we're only emitting the new verfication block, and the
127 # installer just replaces that part of the hard disk's kernel partition.
128 vbutil_kernel \
129 --repack "${vblock}" \
130 --vblockonly \
131 --keyblock "${keydir}/kernel.keyblock" \
132 --signprivate "${keydir}/kernel_data_key.vbprivk" \
133 --oldblob "${dst}"
134
135 # To verify it, we have to replace the vblock from the original image.
136 local tempfile=$(mktemp)
137 trap "rm -f '${tempfile}'" EXIT
138 cp "${vblock}" "${tempfile}"
139 dd if="${dst}" bs=65536 skip=1 >> "${tempfile}"
140
141 vbutil_kernel \
142 --verify $tempfile \
143 --signpubkey "${keydir}/kernel_subkey.vbpubk"
144
145 rm -f "${tempfile}"
146 trap - EXIT
147}
148
Mike Frysinger94a82df2013-11-26 14:51:50 -0500149load_board_specific_script "${BOARD}" "build_kernel_image.sh"
Albert Chaulk5d190f72013-07-12 11:42:18 -0700150
Paul Taysom00df6f62012-11-20 12:35:40 -0800151device_mapper_args=
Will Drewrybcbf1c42010-07-03 10:23:30 -0500152# Even with a rootfs_image, root= is not changed unless specified.
153if [[ -n "${FLAGS_rootfs_image}" && -n "${FLAGS_rootfs_hash}" ]]; then
Will Drewrybcbf1c42010-07-03 10:23:30 -0500154 # Gets the number of blocks. 4096 byte blocks _are_ expected.
Da Zheng87465ea2011-07-30 16:25:28 -0700155 if [ -f "${FLAGS_rootfs_image}" ]; then
156 root_fs_block_sz=4096
157 root_fs_sz=$(stat -c '%s' ${FLAGS_rootfs_image})
158 root_fs_blocks=$((root_fs_sz / ${root_fs_block_sz}))
159 else
160 root_fs_blocks=$(sudo dumpe2fs "${FLAGS_rootfs_image}" 2> /dev/null |
Will Drewrybcbf1c42010-07-03 10:23:30 -0500161 grep "Block count" |
162 tr -d ' ' |
163 cut -f2 -d:)
Da Zheng87465ea2011-07-30 16:25:28 -0700164 root_fs_block_sz=$(sudo dumpe2fs "${FLAGS_rootfs_image}" 2> /dev/null |
Will Drewrybcbf1c42010-07-03 10:23:30 -0500165 grep "Block size" |
166 tr -d ' ' |
167 cut -f2 -d:)
Da Zheng87465ea2011-07-30 16:25:28 -0700168 fi
169
Will Drewry78992a32010-07-21 14:02:20 -0500170 info "rootfs is ${root_fs_blocks} blocks of ${root_fs_block_sz} bytes"
Will Drewrybcbf1c42010-07-03 10:23:30 -0500171 if [[ ${root_fs_block_sz} -ne 4096 ]]; then
172 error "Root file system blocks are not 4k!"
173 fi
174
Elly Jones9ca3e4c2011-09-26 15:18:19 -0400175 info "Generating root fs hash tree (salt '${FLAGS_verity_salt}')."
Will Drewrybcbf1c42010-07-03 10:23:30 -0500176 # Runs as sudo in case the image is a block device.
Mandeep Singh Baines118692a2011-04-28 13:50:33 -0700177 # First argument to verity is reserved/unused and MUST be 0
Elly Jones5e7a4302011-05-27 16:08:16 -0400178 table=$(sudo verity mode=create \
179 alg=${FLAGS_verity_hash_alg} \
180 payload=${FLAGS_rootfs_image} \
181 payload_blocks=${root_fs_blocks} \
Elly Jonesf4e48332011-09-02 13:25:04 -0400182 hashtree=${FLAGS_rootfs_hash} \
Elly Jones9ca3e4c2011-09-26 15:18:19 -0400183 salt=${FLAGS_verity_salt})
Will Drewry821d07c2010-07-03 17:14:58 -0700184 if [[ -f "${FLAGS_rootfs_hash}" ]]; then
185 sudo chmod a+r "${FLAGS_rootfs_hash}"
186 fi
Paul Taysom5b2c7e92012-09-05 10:13:03 -0700187 # Don't claim the root device unless verity is enabled.
188 # Doing so will claim /dev/sdDP out from under the system.
189 if [[ ${FLAGS_enable_rootfs_verification} -eq ${FLAGS_TRUE} ]]; then
Paul Taysom00df6f62012-11-20 12:35:40 -0800190 if [[ ${FLAGS_enable_bootcache} -eq ${FLAGS_TRUE} ]]; then
191 base_root='254:0' # major:minor numbers for /dev/dm-0
192 else
Paul Taysoma59a0b32013-07-24 15:56:18 -0700193 base_root='PARTUUID=%U/PARTNROFF=1' # kern_guid + 1
Paul Taysom00df6f62012-11-20 12:35:40 -0800194 fi
Kenneth Watersed54d932010-09-21 10:29:54 -0700195 table=${table//HASH_DEV/${base_root}}
196 table=${table//ROOT_DEV/${base_root}}
Will Drewrybcbf1c42010-07-03 10:23:30 -0500197 fi
Paul Taysom00df6f62012-11-20 12:35:40 -0800198 verity_dev="vroot none ro 1,${table}"
199 if [[ ${FLAGS_enable_bootcache} -eq ${FLAGS_TRUE} ]]; then
200 signature=$(rootdigest)
201 cachestart=$(($(hashstart) + $(veritysize)))
202 size_limit=512
203 max_trace=20000
204 max_pages=100000
Paul Taysoma59a0b32013-07-24 15:56:18 -0700205 bootcache_args="PARTUUID=%U/PARTNROFF=1"
206 bootcache_args+=" ${cachestart} ${signature} ${size_limit}"
207 bootcache_args+=" ${max_trace} ${max_pages}"
Paul Taysom00df6f62012-11-20 12:35:40 -0800208 bootcache_dev="vboot none ro 1,0 ${cachestart} bootcache ${bootcache_args}"
209 device_mapper_args="dm=\"2 ${bootcache_dev}, ${verity_dev}\""
210 else
211 device_mapper_args="dm=\"1 ${verity_dev}\""
212 fi
213 info "device mapper configuration: ${device_mapper_args}"
Will Drewrybcbf1c42010-07-03 10:23:30 -0500214fi
215
216mkdir -p "${FLAGS_working_dir}"
Will Drewryd6435d42010-10-20 15:37:46 -0500217
218# Only let dm-verity block if rootfs verification is configured.
Paul Taysom5b2c7e92012-09-05 10:13:03 -0700219# By default, we use a firmware enumerated value, but it isn't reliable for
220# production use. If +%d can be added upstream, then we can use:
221# root_dev=PARTUID=uuid+1
Will Drewryd6435d42010-10-20 15:37:46 -0500222dev_wait=0
Paul Taysom5b2c7e92012-09-05 10:13:03 -0700223root_dev="PARTUUID=%U/PARTNROFF=1"
224if [[ ${FLAGS_enable_rootfs_verification} -eq ${FLAGS_TRUE} ]]; then
Will Drewryd6435d42010-10-20 15:37:46 -0500225 dev_wait=1
Paul Taysom00df6f62012-11-20 12:35:40 -0800226 if [[ ${FLAGS_enable_bootcache} -eq ${FLAGS_TRUE} ]]; then
227 root_dev=/dev/dm-1
228 else
229 root_dev=/dev/dm-0
230 fi
231else
232 if [[ ${FLAGS_enable_bootcache} -eq ${FLAGS_TRUE} ]]; then
233 die "Having bootcache without verity is not supported"
234 fi
Will Drewryd6435d42010-10-20 15:37:46 -0500235fi
236
Paul Taysoma59a0b32013-07-24 15:56:18 -0700237# kern_guid should eventually be changed to use PARTUUID
Will Drewrybcbf1c42010-07-03 10:23:30 -0500238cat <<EOF > "${FLAGS_working_dir}/boot.config"
Paul Taysom5b2c7e92012-09-05 10:13:03 -0700239root=${root_dev}
Olof Johanssone0b75512011-01-26 14:31:26 -0800240rootwait
241ro
Will Drewry1670d482010-07-09 13:08:38 -0700242dm_verity.error_behavior=${FLAGS_verity_error_behavior}
243dm_verity.max_bios=${FLAGS_verity_max_ios}
Will Drewryd6435d42010-10-20 15:37:46 -0500244dm_verity.dev_wait=${dev_wait}
Paul Taysom00df6f62012-11-20 12:35:40 -0800245${device_mapper_args}
Will Drewrybcbf1c42010-07-03 10:23:30 -0500246${FLAGS_boot_args}
Pawel Osciaka2c23752011-11-15 15:09:45 -0800247vt.global_cursor_default=0
Doug Anderson2dd2e8e2012-01-06 09:32:24 -0800248kern_guid=%U
Will Drewrybcbf1c42010-07-03 10:23:30 -0500249EOF
250
251WORK="${WORK} ${FLAGS_working_dir}/boot.config"
252info "Emitted cross-platform boot params to ${FLAGS_working_dir}/boot.config"
253
Olof Johansson1eafb5a2012-08-01 00:04:30 -0700254# Add common boot options first.
Paul Taysom5e39bb62013-01-18 07:39:25 -0800255config="${FLAGS_working_dir}/config.txt"
256if [[ -n ${FLAGS_enable_serial} ]]; then
257 console=${FLAGS_enable_serial}
258 if [[ ${console} != *,* ]]; then
259 console+=",115200n8"
260 fi
261 cat <<EOF > "${config}"
262console=${console}
263earlyprintk=${console}
264console=tty1
Vadim Bendebury90b6ec42013-07-17 10:53:46 -0700265keep_bootcon
Paul Taysom5e39bb62013-01-18 07:39:25 -0800266EOF
267else
268 cat <<EOF > "${config}"
Sameer Nanda542cb532012-08-22 16:11:12 -0700269console=
Paul Taysom5e39bb62013-01-18 07:39:25 -0800270EOF
271fi
272
273cat <<EOF - "${FLAGS_working_dir}/boot.config" >> "${config}"
274loglevel=7
Will Drewry69563b72010-06-24 16:12:58 -0500275init=/sbin/init
Olof Johansson1eafb5a2012-08-01 00:04:30 -0700276cros_secure
Paul Taysom984acd72013-04-02 09:41:26 -0700277oops=panic
278panic=-1
Olof Johansson1eafb5a2012-08-01 00:04:30 -0700279EOF
280
281if [[ "${FLAGS_arch}" = "x86" || "${FLAGS_arch}" = "amd64" ]]; then
282 # Legacy BIOS will use the kernel in the rootfs (via syslinux), as will
283 # standard EFI BIOS (via grub, from the EFI System Partition). Chrome OS
284 # BIOS will use a separate signed kernel partition, which we'll create now.
Olof Johanssond7a8aaf2012-08-02 20:35:47 -0700285 cat <<EOF >> "${FLAGS_working_dir}/config.txt"
Will Drewry69563b72010-06-24 16:12:58 -0500286add_efi_memmap
287boot=local
Will Drewry69563b72010-06-24 16:12:58 -0500288noresume
289noswap
290i915.modeset=1
Luigi Semenzato195aebe2010-10-20 17:35:00 -0700291tpm_tis.force=1
292tpm_tis.interrupts=0
Vadim Bendebury8e623f62011-02-28 10:28:31 -0800293nmi_watchdog=panic,lapic
Will Drewry69563b72010-06-24 16:12:58 -0500294EOF
Will Drewrybcbf1c42010-07-03 10:23:30 -0500295 WORK="${WORK} ${FLAGS_working_dir}/config.txt"
Will Drewry69563b72010-06-24 16:12:58 -0500296
Che-Liang Chiou75ac2be2011-02-24 12:00:16 +0800297 bootloader_path="/lib64/bootstub/bootstub.efi"
298 kernel_image="${FLAGS_vmlinuz}"
Che-Liang Chiou75ac2be2011-02-24 12:00:16 +0800299elif [[ "${FLAGS_arch}" = "arm" ]]; then
Che-Liang Chiou75ac2be2011-02-24 12:00:16 +0800300 WORK="${WORK} ${FLAGS_working_dir}/config.txt"
301
Che-Liang Chiou69faa262011-06-01 11:52:21 +0800302 # arm does not need/have a bootloader in kernel partition
303 dd if="/dev/zero" of="${FLAGS_working_dir}/bootloader.bin" bs=512 count=1
304 WORK="${WORK} ${FLAGS_working_dir}/bootloader.bin"
Che-Liang Chiou75ac2be2011-02-24 12:00:16 +0800305
Che-Liang Chiou69faa262011-06-01 11:52:21 +0800306 bootloader_path="${FLAGS_working_dir}/bootloader.bin"
Che-Liang Chiou75ac2be2011-02-24 12:00:16 +0800307 kernel_image="${FLAGS_vmlinuz/vmlinuz/vmlinux.uimg}"
Che-Liang Chiou75ac2be2011-02-24 12:00:16 +0800308else
309 error "Unknown arch: ${FLAGS_arch}"
310fi
311
Che-Liang Chioue51bdf22011-07-26 21:19:24 +0800312# We sign the image with the recovery_key, because this is what goes onto the
313# USB key. We can only boot from the USB drive in recovery mode.
314# For dev install shim, we need to use the installer keyblock instead of
315# the recovery keyblock because of the difference in flags.
316if [ ${FLAGS_use_dev_keys} -eq ${FLAGS_TRUE} ]; then
317 USB_KEYBLOCK=installer_kernel.keyblock
318 info "DEBUG: use dev install signing key"
Che-Liang Chiou75ac2be2011-02-24 12:00:16 +0800319else
Che-Liang Chioue51bdf22011-07-26 21:19:24 +0800320 USB_KEYBLOCK=recovery_kernel.keyblock
321 info "DEBUG: use recovery signing key"
Will Drewry69563b72010-06-24 16:12:58 -0500322fi
323
Chris Masone175a2692014-01-21 16:06:44 -0800324modify_kernel_command_line "${FLAGS_working_dir}/config.txt"
Che-Liang Chioue51bdf22011-07-26 21:19:24 +0800325
Albert Chaulk5d190f72013-07-12 11:42:18 -0700326build_verified_kernel "${FLAGS_to}" "${kernel_image}" "${bootloader_path}" \
327 "${FLAGS_keys_dir}" "${USB_KEYBLOCK}" "${FLAGS_hd_vblock}" \
328 "${FLAGS_working_dir}/config.txt" "${FLAGS_arch}"
Che-Liang Chioue51bdf22011-07-26 21:19:24 +0800329
Will Drewry69563b72010-06-24 16:12:58 -0500330set +e # cleanup failure is a-ok
331
332if [[ ${FLAGS_keep_work} -eq ${FLAGS_FALSE} ]]; then
Will Drewrybcbf1c42010-07-03 10:23:30 -0500333 info "Cleaning up temporary files: ${WORK}"
Will Drewry69563b72010-06-24 16:12:58 -0500334 rm ${WORK}
335 rmdir ${FLAGS_working_dir}
336fi
337
Will Drewrybcbf1c42010-07-03 10:23:30 -0500338info "Kernel partition image emitted: ${FLAGS_to}"
339
340if [[ -f ${FLAGS_rootfs_hash} ]]; then
341 info "Root filesystem hash emitted: ${FLAGS_rootfs_hash}"
342fi