blob: 0f517af7aea428aa640db0773ec2286552c30418 [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)"
Gabe Blacke41371c2014-07-29 15:48:40 -070019DEFINE_string hd_vblock "" \
20 "The path to the installed kernel's 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)"
Gabe Blacke41371c2014-07-29 15:48:40 -070029DEFINE_string keyblock "kernel.keyblock" \
30 "The keyblock to use. (Defaults to kernel.keyblock)"
31DEFINE_string private "kernel_data_key.vbprivk" \
32 "The private key to sign the kernel (Defaults to kernel_data_key.vbprivk)"
33DEFINE_string public "kernel_subkey.vbpubk" \
34 "The public key to verify the kernel (Defaults to kernel_subkey.vbpubk)"
Will Drewrybcbf1c42010-07-03 10:23:30 -050035# Note, to enable verified boot, the caller would manually pass:
Will Drewryb910de82011-02-23 13:26:50 -060036# --boot_args='dm="... %U+1 %U+1 ..." \
Will Drewry69563b72010-06-24 16:12:58 -050037# --root=/dev/dm-0
38DEFINE_string boot_args "noinitrd" \
39 "Additional boot arguments to pass to the commandline (Default: noinitrd)"
Will Drewrybcbf1c42010-07-03 10:23:30 -050040# If provided, will automatically add verified boot arguments.
41DEFINE_string rootfs_image "" \
42 "Optional path to the rootfs device or image.(Default: \"\")"
43DEFINE_string rootfs_hash "" \
44 "Optional path to output the rootfs hash to. (Default: \"\")"
Liam McLoughline81a2322012-09-24 10:37:34 +000045DEFINE_integer verity_error_behavior 3 \
Will Drewrybcbf1c42010-07-03 10:23:30 -050046 "Verified boot error behavior [0: I/O errors, 1: reboot, 2: nothing] \
Liam McLoughline81a2322012-09-24 10:37:34 +000047(Default: 3)"
Will Drewryb910de82011-02-23 13:26:50 -060048DEFINE_integer verity_max_ios -1 \
49 "Optional number of outstanding I/O operations. (Default: -1)"
Will Drewry1670d482010-07-09 13:08:38 -070050DEFINE_string verity_hash_alg "sha1" \
51 "Cryptographic hash algorithm used for dm-verity. (Default: sha1)"
Elly Jones9ca3e4c2011-09-26 15:18:19 -040052DEFINE_string verity_salt "" \
53 "Salt to use for rootfs hash (Default: \"\")"
Paul Taysom5b2c7e92012-09-05 10:13:03 -070054DEFINE_boolean enable_rootfs_verification ${FLAGS_TRUE} \
55 "Enable kernel-based root fs integrity checking. (Default: true)"
Paul Taysom00df6f62012-11-20 12:35:40 -080056DEFINE_boolean enable_bootcache ${FLAGS_FALSE} \
57 "Enable boot cache to accelerate booting. (Default: false)"
Paul Taysom5e39bb62013-01-18 07:39:25 -080058DEFINE_string enable_serial "" \
59 "Enable serial port for printks. Example values: ttyS0"
Zach Reiznerf09d3d12014-09-24 10:45:19 -070060DEFINE_integer loglevel 7 \
61 "The loglevel to add to the kernel command line."
Will Drewrybcbf1c42010-07-03 10:23:30 -050062
Will Drewry69563b72010-06-24 16:12:58 -050063# Parse flags
64FLAGS "$@" || exit 1
65eval set -- "${FLAGS_ARGV}"
66
67# Die on error
Brian Harring7f175a52012-03-02 05:37:00 -080068switch_to_strict_mode
Will Drewry69563b72010-06-24 16:12:58 -050069
Mike Frysinger94a82df2013-11-26 14:51:50 -050070# N.B. Ordering matters for some of the libraries below, because
71# some of the files contain initialization used by later files.
72. "${BUILD_LIBRARY_DIR}/board_options.sh" || exit 1
73
Paul Taysom00df6f62012-11-20 12:35:40 -080074rootdigest() {
75 local digest=${table#*root_hexdigest=}
76 echo ${digest% salt*}
77}
78
79salt() {
80 local salt=${table#*salt=}
81 echo ${salt%}
82}
83
84hashstart() {
85 local hash=${table#*hashstart=}
86 echo ${hash% alg*}
87}
88
89# Estimate of sectors used by verity
90# (num blocks) * 32 (bytes per hash) * 2 (overhead) / 512 (bytes per sector)
91veritysize() {
92 echo $((root_fs_blocks * 32 * 2 / 512))
93}
94
Chris Masone175a2692014-01-21 16:06:44 -080095# Munge the kernel command line.
96# Intended to be overridden by boards that wish to add to the command line.
97# $1 - Configuration file containing boot args.
98modify_kernel_command_line() {
99 :
100}
101
Mike Frysinger94a82df2013-11-26 14:51:50 -0500102load_board_specific_script "${BOARD}" "build_kernel_image.sh"
Albert Chaulk5d190f72013-07-12 11:42:18 -0700103
Paul Taysom00df6f62012-11-20 12:35:40 -0800104device_mapper_args=
Will Drewrybcbf1c42010-07-03 10:23:30 -0500105# Even with a rootfs_image, root= is not changed unless specified.
106if [[ -n "${FLAGS_rootfs_image}" && -n "${FLAGS_rootfs_hash}" ]]; then
Will Drewrybcbf1c42010-07-03 10:23:30 -0500107 # Gets the number of blocks. 4096 byte blocks _are_ expected.
Da Zheng87465ea2011-07-30 16:25:28 -0700108 if [ -f "${FLAGS_rootfs_image}" ]; then
109 root_fs_block_sz=4096
110 root_fs_sz=$(stat -c '%s' ${FLAGS_rootfs_image})
111 root_fs_blocks=$((root_fs_sz / ${root_fs_block_sz}))
112 else
113 root_fs_blocks=$(sudo dumpe2fs "${FLAGS_rootfs_image}" 2> /dev/null |
Will Drewrybcbf1c42010-07-03 10:23:30 -0500114 grep "Block count" |
115 tr -d ' ' |
116 cut -f2 -d:)
Da Zheng87465ea2011-07-30 16:25:28 -0700117 root_fs_block_sz=$(sudo dumpe2fs "${FLAGS_rootfs_image}" 2> /dev/null |
Will Drewrybcbf1c42010-07-03 10:23:30 -0500118 grep "Block size" |
119 tr -d ' ' |
120 cut -f2 -d:)
Da Zheng87465ea2011-07-30 16:25:28 -0700121 fi
122
Will Drewry78992a32010-07-21 14:02:20 -0500123 info "rootfs is ${root_fs_blocks} blocks of ${root_fs_block_sz} bytes"
Will Drewrybcbf1c42010-07-03 10:23:30 -0500124 if [[ ${root_fs_block_sz} -ne 4096 ]]; then
125 error "Root file system blocks are not 4k!"
126 fi
127
Elly Jones9ca3e4c2011-09-26 15:18:19 -0400128 info "Generating root fs hash tree (salt '${FLAGS_verity_salt}')."
Will Drewrybcbf1c42010-07-03 10:23:30 -0500129 # Runs as sudo in case the image is a block device.
Mandeep Singh Baines118692a2011-04-28 13:50:33 -0700130 # First argument to verity is reserved/unused and MUST be 0
Elly Jones5e7a4302011-05-27 16:08:16 -0400131 table=$(sudo verity mode=create \
132 alg=${FLAGS_verity_hash_alg} \
133 payload=${FLAGS_rootfs_image} \
134 payload_blocks=${root_fs_blocks} \
Elly Jonesf4e48332011-09-02 13:25:04 -0400135 hashtree=${FLAGS_rootfs_hash} \
Elly Jones9ca3e4c2011-09-26 15:18:19 -0400136 salt=${FLAGS_verity_salt})
Will Drewry821d07c2010-07-03 17:14:58 -0700137 if [[ -f "${FLAGS_rootfs_hash}" ]]; then
138 sudo chmod a+r "${FLAGS_rootfs_hash}"
139 fi
Paul Taysom5b2c7e92012-09-05 10:13:03 -0700140 # Don't claim the root device unless verity is enabled.
141 # Doing so will claim /dev/sdDP out from under the system.
142 if [[ ${FLAGS_enable_rootfs_verification} -eq ${FLAGS_TRUE} ]]; then
Paul Taysom00df6f62012-11-20 12:35:40 -0800143 if [[ ${FLAGS_enable_bootcache} -eq ${FLAGS_TRUE} ]]; then
144 base_root='254:0' # major:minor numbers for /dev/dm-0
145 else
Paul Taysoma59a0b32013-07-24 15:56:18 -0700146 base_root='PARTUUID=%U/PARTNROFF=1' # kern_guid + 1
Paul Taysom00df6f62012-11-20 12:35:40 -0800147 fi
Kenneth Watersed54d932010-09-21 10:29:54 -0700148 table=${table//HASH_DEV/${base_root}}
149 table=${table//ROOT_DEV/${base_root}}
Will Drewrybcbf1c42010-07-03 10:23:30 -0500150 fi
Paul Taysom00df6f62012-11-20 12:35:40 -0800151 verity_dev="vroot none ro 1,${table}"
152 if [[ ${FLAGS_enable_bootcache} -eq ${FLAGS_TRUE} ]]; then
153 signature=$(rootdigest)
154 cachestart=$(($(hashstart) + $(veritysize)))
155 size_limit=512
156 max_trace=20000
157 max_pages=100000
Paul Taysoma59a0b32013-07-24 15:56:18 -0700158 bootcache_args="PARTUUID=%U/PARTNROFF=1"
159 bootcache_args+=" ${cachestart} ${signature} ${size_limit}"
160 bootcache_args+=" ${max_trace} ${max_pages}"
Paul Taysom00df6f62012-11-20 12:35:40 -0800161 bootcache_dev="vboot none ro 1,0 ${cachestart} bootcache ${bootcache_args}"
162 device_mapper_args="dm=\"2 ${bootcache_dev}, ${verity_dev}\""
163 else
164 device_mapper_args="dm=\"1 ${verity_dev}\""
165 fi
166 info "device mapper configuration: ${device_mapper_args}"
Will Drewrybcbf1c42010-07-03 10:23:30 -0500167fi
168
169mkdir -p "${FLAGS_working_dir}"
Will Drewryd6435d42010-10-20 15:37:46 -0500170
171# Only let dm-verity block if rootfs verification is configured.
Paul Taysom5b2c7e92012-09-05 10:13:03 -0700172# By default, we use a firmware enumerated value, but it isn't reliable for
173# production use. If +%d can be added upstream, then we can use:
174# root_dev=PARTUID=uuid+1
Will Drewryd6435d42010-10-20 15:37:46 -0500175dev_wait=0
Paul Taysom5b2c7e92012-09-05 10:13:03 -0700176root_dev="PARTUUID=%U/PARTNROFF=1"
177if [[ ${FLAGS_enable_rootfs_verification} -eq ${FLAGS_TRUE} ]]; then
Will Drewryd6435d42010-10-20 15:37:46 -0500178 dev_wait=1
Paul Taysom00df6f62012-11-20 12:35:40 -0800179 if [[ ${FLAGS_enable_bootcache} -eq ${FLAGS_TRUE} ]]; then
180 root_dev=/dev/dm-1
181 else
182 root_dev=/dev/dm-0
183 fi
184else
185 if [[ ${FLAGS_enable_bootcache} -eq ${FLAGS_TRUE} ]]; then
186 die "Having bootcache without verity is not supported"
187 fi
Will Drewryd6435d42010-10-20 15:37:46 -0500188fi
189
Paul Taysoma59a0b32013-07-24 15:56:18 -0700190# kern_guid should eventually be changed to use PARTUUID
Will Drewrybcbf1c42010-07-03 10:23:30 -0500191cat <<EOF > "${FLAGS_working_dir}/boot.config"
Paul Taysom5b2c7e92012-09-05 10:13:03 -0700192root=${root_dev}
Olof Johanssone0b75512011-01-26 14:31:26 -0800193rootwait
194ro
Will Drewry1670d482010-07-09 13:08:38 -0700195dm_verity.error_behavior=${FLAGS_verity_error_behavior}
196dm_verity.max_bios=${FLAGS_verity_max_ios}
Will Drewryd6435d42010-10-20 15:37:46 -0500197dm_verity.dev_wait=${dev_wait}
Paul Taysom00df6f62012-11-20 12:35:40 -0800198${device_mapper_args}
Will Drewrybcbf1c42010-07-03 10:23:30 -0500199${FLAGS_boot_args}
Pawel Osciaka2c23752011-11-15 15:09:45 -0800200vt.global_cursor_default=0
Doug Anderson2dd2e8e2012-01-06 09:32:24 -0800201kern_guid=%U
Will Drewrybcbf1c42010-07-03 10:23:30 -0500202EOF
203
204WORK="${WORK} ${FLAGS_working_dir}/boot.config"
205info "Emitted cross-platform boot params to ${FLAGS_working_dir}/boot.config"
206
Olof Johansson1eafb5a2012-08-01 00:04:30 -0700207# Add common boot options first.
Paul Taysom5e39bb62013-01-18 07:39:25 -0800208config="${FLAGS_working_dir}/config.txt"
209if [[ -n ${FLAGS_enable_serial} ]]; then
210 console=${FLAGS_enable_serial}
211 if [[ ${console} != *,* ]]; then
212 console+=",115200n8"
213 fi
214 cat <<EOF > "${config}"
Paul Taysom5e39bb62013-01-18 07:39:25 -0800215earlyprintk=${console}
216console=tty1
Doug Andersond65edf42014-07-09 15:00:53 -0700217console=${console}
Paul Taysom5e39bb62013-01-18 07:39:25 -0800218EOF
219else
220 cat <<EOF > "${config}"
Sameer Nanda542cb532012-08-22 16:11:12 -0700221console=
Paul Taysom5e39bb62013-01-18 07:39:25 -0800222EOF
223fi
224
225cat <<EOF - "${FLAGS_working_dir}/boot.config" >> "${config}"
Zach Reiznerf09d3d12014-09-24 10:45:19 -0700226loglevel=${FLAGS_loglevel}
Will Drewry69563b72010-06-24 16:12:58 -0500227init=/sbin/init
Olof Johansson1eafb5a2012-08-01 00:04:30 -0700228cros_secure
Paul Taysom984acd72013-04-02 09:41:26 -0700229oops=panic
230panic=-1
Olof Johansson1eafb5a2012-08-01 00:04:30 -0700231EOF
232
233if [[ "${FLAGS_arch}" = "x86" || "${FLAGS_arch}" = "amd64" ]]; then
234 # Legacy BIOS will use the kernel in the rootfs (via syslinux), as will
235 # standard EFI BIOS (via grub, from the EFI System Partition). Chrome OS
236 # BIOS will use a separate signed kernel partition, which we'll create now.
Olof Johanssond7a8aaf2012-08-02 20:35:47 -0700237 cat <<EOF >> "${FLAGS_working_dir}/config.txt"
Will Drewry69563b72010-06-24 16:12:58 -0500238add_efi_memmap
239boot=local
Will Drewry69563b72010-06-24 16:12:58 -0500240noresume
241noswap
242i915.modeset=1
Luigi Semenzato195aebe2010-10-20 17:35:00 -0700243tpm_tis.force=1
244tpm_tis.interrupts=0
Vadim Bendebury8e623f62011-02-28 10:28:31 -0800245nmi_watchdog=panic,lapic
Will Drewry69563b72010-06-24 16:12:58 -0500246EOF
Will Drewrybcbf1c42010-07-03 10:23:30 -0500247 WORK="${WORK} ${FLAGS_working_dir}/config.txt"
Will Drewry69563b72010-06-24 16:12:58 -0500248
Che-Liang Chiou75ac2be2011-02-24 12:00:16 +0800249 bootloader_path="/lib64/bootstub/bootstub.efi"
250 kernel_image="${FLAGS_vmlinuz}"
Gaurav Shahae5d4d22014-05-14 17:10:05 -0700251elif [[ "${FLAGS_arch}" = "arm" || "${FLAGS_arch}" = "mips" ]]; then
Che-Liang Chiou75ac2be2011-02-24 12:00:16 +0800252 WORK="${WORK} ${FLAGS_working_dir}/config.txt"
253
Che-Liang Chiou69faa262011-06-01 11:52:21 +0800254 # arm does not need/have a bootloader in kernel partition
255 dd if="/dev/zero" of="${FLAGS_working_dir}/bootloader.bin" bs=512 count=1
256 WORK="${WORK} ${FLAGS_working_dir}/bootloader.bin"
Che-Liang Chiou75ac2be2011-02-24 12:00:16 +0800257
Che-Liang Chiou69faa262011-06-01 11:52:21 +0800258 bootloader_path="${FLAGS_working_dir}/bootloader.bin"
Che-Liang Chiou75ac2be2011-02-24 12:00:16 +0800259 kernel_image="${FLAGS_vmlinuz/vmlinuz/vmlinux.uimg}"
Che-Liang Chiou75ac2be2011-02-24 12:00:16 +0800260else
261 error "Unknown arch: ${FLAGS_arch}"
262fi
263
Gabe Blacke41371c2014-07-29 15:48:40 -0700264config_file="${FLAGS_working_dir}/config.txt"
265modify_kernel_command_line "${config_file}"
266# Create and sign the kernel blob
267vbutil_kernel \
268 --pack "${FLAGS_to}" \
269 --keyblock "${FLAGS_keys_dir}/${FLAGS_keyblock}" \
270 --signprivate "${FLAGS_keys_dir}/${FLAGS_private}" \
271 --version 1 \
272 --config "${config_file}" \
273 --bootloader "${bootloader_path}" \
274 --vmlinuz "${kernel_image}" \
275 --arch "${FLAGS_arch}"
276
277# And verify it.
278vbutil_kernel \
279 --verify "${FLAGS_to}" \
280 --signpubkey "${FLAGS_keys_dir}/${FLAGS_public}"
281
282if [[ -n "${FLAGS_hd_vblock}" ]]; then
283 dd if="${FLAGS_to}" bs=65536 count=1 of="${FLAGS_hd_vblock}"
Will Drewry69563b72010-06-24 16:12:58 -0500284fi
285
286set +e # cleanup failure is a-ok
287
288if [[ ${FLAGS_keep_work} -eq ${FLAGS_FALSE} ]]; then
Will Drewrybcbf1c42010-07-03 10:23:30 -0500289 info "Cleaning up temporary files: ${WORK}"
Will Drewry69563b72010-06-24 16:12:58 -0500290 rm ${WORK}
291 rmdir ${FLAGS_working_dir}
292fi
293
Will Drewrybcbf1c42010-07-03 10:23:30 -0500294info "Kernel partition image emitted: ${FLAGS_to}"
295
296if [[ -f ${FLAGS_rootfs_hash} ]]; then
297 info "Root filesystem hash emitted: ${FLAGS_rootfs_hash}"
298fi