blob: fe90297a5221724c9350c65a763ddd827a240f2e [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)"
Will Drewrybcbf1c42010-07-03 10:23:30 -050028# Note, to enable verified boot, the caller would manually pass:
Will Drewry69563b72010-06-24 16:12:58 -050029# --boot_args='dm="... /dev/sd%D%P /dev/sd%D%P ..." \
30# --root=/dev/dm-0
31DEFINE_string boot_args "noinitrd" \
32 "Additional boot arguments to pass to the commandline (Default: noinitrd)"
33DEFINE_string root "/dev/sd%D%P" \
34 "Expected device root (Default: root=/dev/sd%D%P)"
35
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: \"\")"
Will Drewry1670d482010-07-09 13:08:38 -070041DEFINE_integer verity_error_behavior 2 \
Will Drewrybcbf1c42010-07-03 10:23:30 -050042 "Verified boot error behavior [0: I/O errors, 1: reboot, 2: nothing] \
43(Default: 2)"
Will Drewry1670d482010-07-09 13:08:38 -070044DEFINE_integer verity_tree_depth 1 \
Will Drewrybcbf1c42010-07-03 10:23:30 -050045 "Optional Verified boot hash tree depth. (Default: 1)"
Will Drewry1670d482010-07-09 13:08:38 -070046DEFINE_integer verity_max_ios 1024 \
Will Drewrybcbf1c42010-07-03 10:23:30 -050047 "Optional number of outstanding I/O operations. (Default: 1024)"
Will Drewry1670d482010-07-09 13:08:38 -070048DEFINE_string verity_hash_alg "sha1" \
49 "Cryptographic hash algorithm used for dm-verity. (Default: sha1)"
Will Drewrybcbf1c42010-07-03 10:23:30 -050050
Will Drewry69563b72010-06-24 16:12:58 -050051# Parse flags
52FLAGS "$@" || exit 1
53eval set -- "${FLAGS_ARGV}"
54
55# Die on error
56set -e
57
Will Drewry1670d482010-07-09 13:08:38 -070058verity_args=
Will Drewrybcbf1c42010-07-03 10:23:30 -050059# Even with a rootfs_image, root= is not changed unless specified.
60if [[ -n "${FLAGS_rootfs_image}" && -n "${FLAGS_rootfs_hash}" ]]; then
Will Drewrybcbf1c42010-07-03 10:23:30 -050061 # Gets the number of blocks. 4096 byte blocks _are_ expected.
62 root_fs_blocks=$(sudo dumpe2fs "${FLAGS_rootfs_image}" 2> /dev/null |
63 grep "Block count" |
64 tr -d ' ' |
65 cut -f2 -d:)
Will Drewrybcbf1c42010-07-03 10:23:30 -050066 root_fs_block_sz=$(sudo dumpe2fs "${FLAGS_rootfs_image}" 2> /dev/null |
67 grep "Block size" |
68 tr -d ' ' |
69 cut -f2 -d:)
Will Drewry78992a32010-07-21 14:02:20 -050070 info "rootfs is ${root_fs_blocks} blocks of ${root_fs_block_sz} bytes"
Will Drewrybcbf1c42010-07-03 10:23:30 -050071 if [[ ${root_fs_block_sz} -ne 4096 ]]; then
72 error "Root file system blocks are not 4k!"
73 fi
74
75 info "Generating root fs hash tree."
76 # Runs as sudo in case the image is a block device.
Will Drewry1670d482010-07-09 13:08:38 -070077 table=$(sudo verity create ${FLAGS_verity_tree_depth} \
78 ${FLAGS_verity_hash_alg} \
Will Drewrybcbf1c42010-07-03 10:23:30 -050079 ${FLAGS_rootfs_image} \
80 ${root_fs_blocks} \
81 ${FLAGS_rootfs_hash})
Will Drewry821d07c2010-07-03 17:14:58 -070082 if [[ -f "${FLAGS_rootfs_hash}" ]]; then
83 sudo chmod a+r "${FLAGS_rootfs_hash}"
84 fi
Will Drewrybcbf1c42010-07-03 10:23:30 -050085 # Don't claim the root device unless the root= flag is pointed to
86 # the verified boot device. Doing so will claim /dev/sdDP out from
87 # under the system.
88 if [[ ${FLAGS_root} = "/dev/dm-0" ]]; then
Will Drewry78992a32010-07-21 14:02:20 -050089 table=${table//HASH_DEV//dev/sd%D%P}
90 table=${table//ROOT_DEV//dev/sd%D%P}
Will Drewrybcbf1c42010-07-03 10:23:30 -050091 fi
Will Drewry78992a32010-07-21 14:02:20 -050092 verity_args="dm=\"vroot none ro,${table}\""
Will Drewry1670d482010-07-09 13:08:38 -070093 info "dm-verity configuration: ${verity_args}"
Will Drewrybcbf1c42010-07-03 10:23:30 -050094fi
95
96mkdir -p "${FLAGS_working_dir}"
97cat <<EOF > "${FLAGS_working_dir}/boot.config"
98root=${FLAGS_root}
Will Drewry1670d482010-07-09 13:08:38 -070099dm_verity.error_behavior=${FLAGS_verity_error_behavior}
100dm_verity.max_bios=${FLAGS_verity_max_ios}
101${verity_args}
Will Drewrybcbf1c42010-07-03 10:23:30 -0500102${FLAGS_boot_args}
103EOF
104
105WORK="${WORK} ${FLAGS_working_dir}/boot.config"
106info "Emitted cross-platform boot params to ${FLAGS_working_dir}/boot.config"
107
Will Drewry69563b72010-06-24 16:12:58 -0500108# FIXME: At the moment, we're working on signed images for x86 only. ARM will
109# support this before shipping, but at the moment they don't.
110if [[ "${FLAGS_arch}" = "x86" ]]; then
111
Will Drewrybcbf1c42010-07-03 10:23:30 -0500112 # Legacy BIOS will use the kernel in the rootfs (via syslinux), as will
113 # standard EFI BIOS (via grub, from the EFI System Partition). Chrome OS
114 # BIOS will use a separate signed kernel partition, which we'll create now.
115 # FIXME: remove serial output, debugging messages.
116 mkdir -p ${FLAGS_working_dir}
117 cat <<EOF | cat - "${FLAGS_working_dir}/boot.config" \
118 > "${FLAGS_working_dir}/config.txt"
Will Drewry69563b72010-06-24 16:12:58 -0500119earlyprintk=serial,ttyS0,115200
120console=ttyS0,115200
121init=/sbin/init
122add_efi_memmap
123boot=local
124rootwait
Will Drewry69563b72010-06-24 16:12:58 -0500125ro
126noresume
127noswap
128i915.modeset=1
129loglevel=7
130cros_secure
Will Drewry69563b72010-06-24 16:12:58 -0500131EOF
Will Drewrybcbf1c42010-07-03 10:23:30 -0500132 WORK="${WORK} ${FLAGS_working_dir}/config.txt"
Will Drewry69563b72010-06-24 16:12:58 -0500133
Louis Yung-Chieh Lo36020402010-07-05 13:23:34 +0800134 # We sign the image with the recovery_key, because this is what goes onto the
135 # USB key. We can only boot from the USB drive in recovery mode.
Bill Richardson2ace49e2010-07-01 10:23:27 -0700136
Will Drewrybcbf1c42010-07-03 10:23:30 -0500137 # Create and sign the kernel blob
138 vbutil_kernel \
139 --pack "${FLAGS_to}" \
Louis Yung-Chieh Lo36020402010-07-05 13:23:34 +0800140 --keyblock "${FLAGS_keys_dir}/recovery_kernel.keyblock" \
141 --signprivate "${FLAGS_keys_dir}/recovery_kernel_data_key.vbprivk" \
Will Drewrybcbf1c42010-07-03 10:23:30 -0500142 --version 1 \
143 --config "${FLAGS_working_dir}/config.txt" \
144 --bootloader /lib64/bootstub/bootstub.efi \
145 --vmlinuz "${FLAGS_vmlinuz}"
Will Drewry69563b72010-06-24 16:12:58 -0500146
Will Drewrybcbf1c42010-07-03 10:23:30 -0500147 # And verify it.
148 vbutil_kernel \
149 --verify "${FLAGS_to}" \
Louis Yung-Chieh Lo36020402010-07-05 13:23:34 +0800150 --signpubkey "${FLAGS_keys_dir}/recovery_key.vbpubk"
151
152
153 # Now we re-sign the same image using the normal keys. This is the kernel
154 # image that is put on the hard disk by the installer. Note: To save space on
155 # the USB image, we're only emitting the new verfication block, and the
156 # installer just replaces that part of the hard disk's kernel partition.
157 vbutil_kernel \
158 --repack "${FLAGS_hd_vblock}" \
159 --vblockonly \
160 --keyblock "${FLAGS_keys_dir}/kernel.keyblock" \
161 --signprivate "${FLAGS_keys_dir}/kernel_data_key.vbprivk" \
162 --oldblob "${FLAGS_to}"
163
164
165 # To verify it, we have to replace the vblock from the original image.
166 tempfile=$(mktemp)
167 trap "rm -f $tempfile" EXIT
168 cat "${FLAGS_hd_vblock}" > $tempfile
169 dd if="${FLAGS_to}" bs=65536 skip=1 >> $tempfile
170
171 vbutil_kernel \
172 --verify $tempfile \
173 --signpubkey "${FLAGS_keys_dir}/kernel_subkey.vbpubk"
174
175 rm -f $tempfile
176 trap - EXIT
Will Drewry69563b72010-06-24 16:12:58 -0500177
Will Drewrybcbf1c42010-07-03 10:23:30 -0500178elif [[ "${FLAGS_arch}" = "arm" ]]; then
Will Drewry69563b72010-06-24 16:12:58 -0500179 # FIXME: For now, ARM just uses the unsigned kernel by itself.
180 cp -f "${FLAGS_vmlinuz}" "${FLAGS_to}"
Will Drewrybcbf1c42010-07-03 10:23:30 -0500181else
182 error "Unknown arch: ${FLAGS_arch}"
Will Drewry69563b72010-06-24 16:12:58 -0500183fi
184
185set +e # cleanup failure is a-ok
186
187if [[ ${FLAGS_keep_work} -eq ${FLAGS_FALSE} ]]; then
Will Drewrybcbf1c42010-07-03 10:23:30 -0500188 info "Cleaning up temporary files: ${WORK}"
Will Drewry69563b72010-06-24 16:12:58 -0500189 rm ${WORK}
190 rmdir ${FLAGS_working_dir}
191fi
192
Will Drewrybcbf1c42010-07-03 10:23:30 -0500193info "Kernel partition image emitted: ${FLAGS_to}"
194
195if [[ -f ${FLAGS_rootfs_hash} ]]; then
196 info "Root filesystem hash emitted: ${FLAGS_rootfs_hash}"
197fi