blob: bb0253af0888b755fd255381d535b1045f751482 [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
9. "$(dirname "$0")/common.sh"
10
11get_default_board
12
13# Flags.
14DEFINE_string arch "x86" \
15 "The boot architecture: arm or x86. (Default: x86)"
16DEFINE_string to "/tmp/vmlinuz.image" \
17 "The path to the kernel image to be created. (Default: /tmp/vmlinuz.image)"
Louis Yung-Chieh Lo36020402010-07-05 13:23:34 +080018DEFINE_string hd_vblock "/tmp/vmlinuz_hd.vblock" \
19 "The path to the installed kernel's vblock (Default: /tmp/vmlinuz_hd.vblock)"
Will Drewry69563b72010-06-24 16:12:58 -050020DEFINE_string vmlinuz "vmlinuz" \
21 "The path to the kernel (Default: vmlinuz)"
22DEFINE_string working_dir "/tmp/vmlinuz.working" \
23 "Working directory for in-progress files. (Default: /tmp/vmlinuz.working)"
24DEFINE_boolean keep_work ${FLAGS_FALSE} \
25 "Keep temporary files (*.keyblock, *.vbpubk). (Default: false)"
26DEFINE_string keys_dir "${SRC_ROOT}/platform/vboot_reference/tests/testkeys" \
Bill Richardson2ace49e2010-07-01 10:23:27 -070027 "Directory with the RSA signing keys. (Defaults to test keys)"
Tan Gao843b70a2010-08-17 09:41:48 -070028DEFINE_boolean use_dev_keys ${FLAGS_FALSE} \
29 "Use developer keys for signing. (Default: false)"
Will Drewrybcbf1c42010-07-03 10:23:30 -050030# Note, to enable verified boot, the caller would manually pass:
Will Drewry69563b72010-06-24 16:12:58 -050031# --boot_args='dm="... /dev/sd%D%P /dev/sd%D%P ..." \
32# --root=/dev/dm-0
33DEFINE_string boot_args "noinitrd" \
34 "Additional boot arguments to pass to the commandline (Default: noinitrd)"
35DEFINE_string root "/dev/sd%D%P" \
36 "Expected device root (Default: root=/dev/sd%D%P)"
37
Will Drewrybcbf1c42010-07-03 10:23:30 -050038# If provided, will automatically add verified boot arguments.
39DEFINE_string rootfs_image "" \
40 "Optional path to the rootfs device or image.(Default: \"\")"
41DEFINE_string rootfs_hash "" \
42 "Optional path to output the rootfs hash to. (Default: \"\")"
Will Drewry1670d482010-07-09 13:08:38 -070043DEFINE_integer verity_error_behavior 2 \
Will Drewrybcbf1c42010-07-03 10:23:30 -050044 "Verified boot error behavior [0: I/O errors, 1: reboot, 2: nothing] \
45(Default: 2)"
Will Drewry1670d482010-07-09 13:08:38 -070046DEFINE_integer verity_tree_depth 1 \
Will Drewrybcbf1c42010-07-03 10:23:30 -050047 "Optional Verified boot hash tree depth. (Default: 1)"
Will Drewry1670d482010-07-09 13:08:38 -070048DEFINE_integer verity_max_ios 1024 \
Will Drewrybcbf1c42010-07-03 10:23:30 -050049 "Optional number of outstanding I/O operations. (Default: 1024)"
Will Drewry1670d482010-07-09 13:08:38 -070050DEFINE_string verity_hash_alg "sha1" \
51 "Cryptographic hash algorithm used for dm-verity. (Default: sha1)"
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
58set -e
59
Will Drewry1670d482010-07-09 13:08:38 -070060verity_args=
Will Drewrybcbf1c42010-07-03 10:23:30 -050061# Even with a rootfs_image, root= is not changed unless specified.
62if [[ -n "${FLAGS_rootfs_image}" && -n "${FLAGS_rootfs_hash}" ]]; then
Will Drewrybcbf1c42010-07-03 10:23:30 -050063 # Gets the number of blocks. 4096 byte blocks _are_ expected.
64 root_fs_blocks=$(sudo dumpe2fs "${FLAGS_rootfs_image}" 2> /dev/null |
65 grep "Block count" |
66 tr -d ' ' |
67 cut -f2 -d:)
Will Drewrybcbf1c42010-07-03 10:23:30 -050068 root_fs_block_sz=$(sudo dumpe2fs "${FLAGS_rootfs_image}" 2> /dev/null |
69 grep "Block size" |
70 tr -d ' ' |
71 cut -f2 -d:)
Will Drewry78992a32010-07-21 14:02:20 -050072 info "rootfs is ${root_fs_blocks} blocks of ${root_fs_block_sz} bytes"
Will Drewrybcbf1c42010-07-03 10:23:30 -050073 if [[ ${root_fs_block_sz} -ne 4096 ]]; then
74 error "Root file system blocks are not 4k!"
75 fi
76
77 info "Generating root fs hash tree."
78 # Runs as sudo in case the image is a block device.
Will Drewry1670d482010-07-09 13:08:38 -070079 table=$(sudo verity create ${FLAGS_verity_tree_depth} \
80 ${FLAGS_verity_hash_alg} \
Will Drewrybcbf1c42010-07-03 10:23:30 -050081 ${FLAGS_rootfs_image} \
82 ${root_fs_blocks} \
83 ${FLAGS_rootfs_hash})
Will Drewry821d07c2010-07-03 17:14:58 -070084 if [[ -f "${FLAGS_rootfs_hash}" ]]; then
85 sudo chmod a+r "${FLAGS_rootfs_hash}"
86 fi
Will Drewrybcbf1c42010-07-03 10:23:30 -050087 # Don't claim the root device unless the root= flag is pointed to
88 # the verified boot device. Doing so will claim /dev/sdDP out from
89 # under the system.
90 if [[ ${FLAGS_root} = "/dev/dm-0" ]]; then
Will Drewry78992a32010-07-21 14:02:20 -050091 table=${table//HASH_DEV//dev/sd%D%P}
92 table=${table//ROOT_DEV//dev/sd%D%P}
Will Drewrybcbf1c42010-07-03 10:23:30 -050093 fi
Will Drewry78992a32010-07-21 14:02:20 -050094 verity_args="dm=\"vroot none ro,${table}\""
Will Drewry1670d482010-07-09 13:08:38 -070095 info "dm-verity configuration: ${verity_args}"
Will Drewrybcbf1c42010-07-03 10:23:30 -050096fi
97
98mkdir -p "${FLAGS_working_dir}"
99cat <<EOF > "${FLAGS_working_dir}/boot.config"
100root=${FLAGS_root}
Will Drewry1670d482010-07-09 13:08:38 -0700101dm_verity.error_behavior=${FLAGS_verity_error_behavior}
102dm_verity.max_bios=${FLAGS_verity_max_ios}
103${verity_args}
Will Drewrybcbf1c42010-07-03 10:23:30 -0500104${FLAGS_boot_args}
105EOF
106
107WORK="${WORK} ${FLAGS_working_dir}/boot.config"
108info "Emitted cross-platform boot params to ${FLAGS_working_dir}/boot.config"
109
Will Drewry69563b72010-06-24 16:12:58 -0500110# FIXME: At the moment, we're working on signed images for x86 only. ARM will
111# support this before shipping, but at the moment they don't.
112if [[ "${FLAGS_arch}" = "x86" ]]; then
113
Will Drewrybcbf1c42010-07-03 10:23:30 -0500114 # Legacy BIOS will use the kernel in the rootfs (via syslinux), as will
115 # standard EFI BIOS (via grub, from the EFI System Partition). Chrome OS
116 # BIOS will use a separate signed kernel partition, which we'll create now.
117 # FIXME: remove serial output, debugging messages.
118 mkdir -p ${FLAGS_working_dir}
119 cat <<EOF | cat - "${FLAGS_working_dir}/boot.config" \
120 > "${FLAGS_working_dir}/config.txt"
Will Drewry69563b72010-06-24 16:12:58 -0500121earlyprintk=serial,ttyS0,115200
122console=ttyS0,115200
123init=/sbin/init
124add_efi_memmap
125boot=local
126rootwait
Will Drewry69563b72010-06-24 16:12:58 -0500127ro
128noresume
129noswap
130i915.modeset=1
131loglevel=7
132cros_secure
Bill Richardson8bfa4682010-07-23 17:24:15 -0700133kern_guid=%U
Will Drewry69563b72010-06-24 16:12:58 -0500134EOF
Will Drewrybcbf1c42010-07-03 10:23:30 -0500135 WORK="${WORK} ${FLAGS_working_dir}/config.txt"
Will Drewry69563b72010-06-24 16:12:58 -0500136
Louis Yung-Chieh Lo36020402010-07-05 13:23:34 +0800137 # We sign the image with the recovery_key, because this is what goes onto the
138 # USB key. We can only boot from the USB drive in recovery mode.
Tan Gao843b70a2010-08-17 09:41:48 -0700139 # For dev install shim, we need to use the installer keyblock instead of
140 # the recovery keyblock because of the difference in flags.
141 if [ ${FLAGS_use_dev_keys} -eq ${FLAGS_TRUE} ]; then
142 USB_KEYBLOCK=installer_kernel.keyblock
143 info "DEBUG: use dev install signing key"
144 else
145 USB_KEYBLOCK=recovery_kernel.keyblock
146 info "DEBUG: use recovery signing key"
147 fi
Bill Richardson2ace49e2010-07-01 10:23:27 -0700148
Will Drewrybcbf1c42010-07-03 10:23:30 -0500149 # Create and sign the kernel blob
150 vbutil_kernel \
151 --pack "${FLAGS_to}" \
Tan Gao843b70a2010-08-17 09:41:48 -0700152 --keyblock "${FLAGS_keys_dir}/${USB_KEYBLOCK}" \
Louis Yung-Chieh Lo36020402010-07-05 13:23:34 +0800153 --signprivate "${FLAGS_keys_dir}/recovery_kernel_data_key.vbprivk" \
Will Drewrybcbf1c42010-07-03 10:23:30 -0500154 --version 1 \
155 --config "${FLAGS_working_dir}/config.txt" \
156 --bootloader /lib64/bootstub/bootstub.efi \
157 --vmlinuz "${FLAGS_vmlinuz}"
Will Drewry69563b72010-06-24 16:12:58 -0500158
Will Drewrybcbf1c42010-07-03 10:23:30 -0500159 # And verify it.
160 vbutil_kernel \
161 --verify "${FLAGS_to}" \
Louis Yung-Chieh Lo36020402010-07-05 13:23:34 +0800162 --signpubkey "${FLAGS_keys_dir}/recovery_key.vbpubk"
163
164
165 # Now we re-sign the same image using the normal keys. This is the kernel
166 # image that is put on the hard disk by the installer. Note: To save space on
167 # the USB image, we're only emitting the new verfication block, and the
168 # installer just replaces that part of the hard disk's kernel partition.
169 vbutil_kernel \
170 --repack "${FLAGS_hd_vblock}" \
171 --vblockonly \
172 --keyblock "${FLAGS_keys_dir}/kernel.keyblock" \
173 --signprivate "${FLAGS_keys_dir}/kernel_data_key.vbprivk" \
174 --oldblob "${FLAGS_to}"
175
176
177 # To verify it, we have to replace the vblock from the original image.
178 tempfile=$(mktemp)
179 trap "rm -f $tempfile" EXIT
180 cat "${FLAGS_hd_vblock}" > $tempfile
181 dd if="${FLAGS_to}" bs=65536 skip=1 >> $tempfile
182
183 vbutil_kernel \
184 --verify $tempfile \
185 --signpubkey "${FLAGS_keys_dir}/kernel_subkey.vbpubk"
186
187 rm -f $tempfile
188 trap - EXIT
Will Drewry69563b72010-06-24 16:12:58 -0500189
Will Drewrybcbf1c42010-07-03 10:23:30 -0500190elif [[ "${FLAGS_arch}" = "arm" ]]; then
Will Drewry69563b72010-06-24 16:12:58 -0500191 # FIXME: For now, ARM just uses the unsigned kernel by itself.
192 cp -f "${FLAGS_vmlinuz}" "${FLAGS_to}"
Will Drewrybcbf1c42010-07-03 10:23:30 -0500193else
194 error "Unknown arch: ${FLAGS_arch}"
Will Drewry69563b72010-06-24 16:12:58 -0500195fi
196
197set +e # cleanup failure is a-ok
198
199if [[ ${FLAGS_keep_work} -eq ${FLAGS_FALSE} ]]; then
Will Drewrybcbf1c42010-07-03 10:23:30 -0500200 info "Cleaning up temporary files: ${WORK}"
Will Drewry69563b72010-06-24 16:12:58 -0500201 rm ${WORK}
202 rmdir ${FLAGS_working_dir}
203fi
204
Will Drewrybcbf1c42010-07-03 10:23:30 -0500205info "Kernel partition image emitted: ${FLAGS_to}"
206
207if [[ -f ${FLAGS_rootfs_hash} ]]; then
208 info "Root filesystem hash emitted: ${FLAGS_rootfs_hash}"
209fi