blob: d78b150b6568b01ed72b02a5e77d35cbc2106789 [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
61 info "Determining root fs block count."
62 # Gets the number of blocks. 4096 byte blocks _are_ expected.
63 root_fs_blocks=$(sudo dumpe2fs "${FLAGS_rootfs_image}" 2> /dev/null |
64 grep "Block count" |
65 tr -d ' ' |
66 cut -f2 -d:)
67 info "Checking root fs block size."
68 root_fs_block_sz=$(sudo dumpe2fs "${FLAGS_rootfs_image}" 2> /dev/null |
69 grep "Block size" |
70 tr -d ' ' |
71 cut -f2 -d:)
72 if [[ ${root_fs_block_sz} -ne 4096 ]]; then
73 error "Root file system blocks are not 4k!"
74 fi
75
76 info "Generating root fs hash tree."
77 # Runs as sudo in case the image is a block device.
Will Drewry1670d482010-07-09 13:08:38 -070078 table=$(sudo verity create ${FLAGS_verity_tree_depth} \
79 ${FLAGS_verity_hash_alg} \
Will Drewrybcbf1c42010-07-03 10:23:30 -050080 ${FLAGS_rootfs_image} \
81 ${root_fs_blocks} \
82 ${FLAGS_rootfs_hash})
Will Drewry821d07c2010-07-03 17:14:58 -070083 if [[ -f "${FLAGS_rootfs_hash}" ]]; then
84 sudo chmod a+r "${FLAGS_rootfs_hash}"
85 fi
Will Drewrybcbf1c42010-07-03 10:23:30 -050086 # Don't claim the root device unless the root= flag is pointed to
87 # the verified boot device. Doing so will claim /dev/sdDP out from
88 # under the system.
89 if [[ ${FLAGS_root} = "/dev/dm-0" ]]; then
90 table=${table//HASH_DEV/\/dev\/sd%D%P}
91 table=${table//ROOT_DEV/\/dev\/sd%D%P}
92 fi
Will Drewry1670d482010-07-09 13:08:38 -070093 verity_args="dm=\"${table}\""
94 info "dm-verity configuration: ${verity_args}"
Will Drewrybcbf1c42010-07-03 10:23:30 -050095fi
96
97mkdir -p "${FLAGS_working_dir}"
98cat <<EOF > "${FLAGS_working_dir}/boot.config"
99root=${FLAGS_root}
Will Drewry1670d482010-07-09 13:08:38 -0700100dm_verity.error_behavior=${FLAGS_verity_error_behavior}
101dm_verity.max_bios=${FLAGS_verity_max_ios}
102${verity_args}
Will Drewrybcbf1c42010-07-03 10:23:30 -0500103${FLAGS_boot_args}
104EOF
105
106WORK="${WORK} ${FLAGS_working_dir}/boot.config"
107info "Emitted cross-platform boot params to ${FLAGS_working_dir}/boot.config"
108
Will Drewry69563b72010-06-24 16:12:58 -0500109# FIXME: At the moment, we're working on signed images for x86 only. ARM will
110# support this before shipping, but at the moment they don't.
111if [[ "${FLAGS_arch}" = "x86" ]]; then
112
Will Drewrybcbf1c42010-07-03 10:23:30 -0500113 # Legacy BIOS will use the kernel in the rootfs (via syslinux), as will
114 # standard EFI BIOS (via grub, from the EFI System Partition). Chrome OS
115 # BIOS will use a separate signed kernel partition, which we'll create now.
116 # FIXME: remove serial output, debugging messages.
117 mkdir -p ${FLAGS_working_dir}
118 cat <<EOF | cat - "${FLAGS_working_dir}/boot.config" \
119 > "${FLAGS_working_dir}/config.txt"
Will Drewry69563b72010-06-24 16:12:58 -0500120earlyprintk=serial,ttyS0,115200
121console=ttyS0,115200
122init=/sbin/init
123add_efi_memmap
124boot=local
125rootwait
Will Drewry69563b72010-06-24 16:12:58 -0500126ro
127noresume
128noswap
129i915.modeset=1
130loglevel=7
131cros_secure
Will Drewry69563b72010-06-24 16:12:58 -0500132EOF
Will Drewrybcbf1c42010-07-03 10:23:30 -0500133 WORK="${WORK} ${FLAGS_working_dir}/config.txt"
Will Drewry69563b72010-06-24 16:12:58 -0500134
Louis Yung-Chieh Lo36020402010-07-05 13:23:34 +0800135 # We sign the image with the recovery_key, because this is what goes onto the
136 # USB key. We can only boot from the USB drive in recovery mode.
Bill Richardson2ace49e2010-07-01 10:23:27 -0700137
Will Drewrybcbf1c42010-07-03 10:23:30 -0500138 # Create and sign the kernel blob
139 vbutil_kernel \
140 --pack "${FLAGS_to}" \
Louis Yung-Chieh Lo36020402010-07-05 13:23:34 +0800141 --keyblock "${FLAGS_keys_dir}/recovery_kernel.keyblock" \
142 --signprivate "${FLAGS_keys_dir}/recovery_kernel_data_key.vbprivk" \
Will Drewrybcbf1c42010-07-03 10:23:30 -0500143 --version 1 \
144 --config "${FLAGS_working_dir}/config.txt" \
145 --bootloader /lib64/bootstub/bootstub.efi \
146 --vmlinuz "${FLAGS_vmlinuz}"
Will Drewry69563b72010-06-24 16:12:58 -0500147
Will Drewrybcbf1c42010-07-03 10:23:30 -0500148 # And verify it.
149 vbutil_kernel \
150 --verify "${FLAGS_to}" \
Louis Yung-Chieh Lo36020402010-07-05 13:23:34 +0800151 --signpubkey "${FLAGS_keys_dir}/recovery_key.vbpubk"
152
153
154 # Now we re-sign the same image using the normal keys. This is the kernel
155 # image that is put on the hard disk by the installer. Note: To save space on
156 # the USB image, we're only emitting the new verfication block, and the
157 # installer just replaces that part of the hard disk's kernel partition.
158 vbutil_kernel \
159 --repack "${FLAGS_hd_vblock}" \
160 --vblockonly \
161 --keyblock "${FLAGS_keys_dir}/kernel.keyblock" \
162 --signprivate "${FLAGS_keys_dir}/kernel_data_key.vbprivk" \
163 --oldblob "${FLAGS_to}"
164
165
166 # To verify it, we have to replace the vblock from the original image.
167 tempfile=$(mktemp)
168 trap "rm -f $tempfile" EXIT
169 cat "${FLAGS_hd_vblock}" > $tempfile
170 dd if="${FLAGS_to}" bs=65536 skip=1 >> $tempfile
171
172 vbutil_kernel \
173 --verify $tempfile \
174 --signpubkey "${FLAGS_keys_dir}/kernel_subkey.vbpubk"
175
176 rm -f $tempfile
177 trap - EXIT
Will Drewry69563b72010-06-24 16:12:58 -0500178
Will Drewrybcbf1c42010-07-03 10:23:30 -0500179elif [[ "${FLAGS_arch}" = "arm" ]]; then
Will Drewry69563b72010-06-24 16:12:58 -0500180 # FIXME: For now, ARM just uses the unsigned kernel by itself.
181 cp -f "${FLAGS_vmlinuz}" "${FLAGS_to}"
Will Drewrybcbf1c42010-07-03 10:23:30 -0500182else
183 error "Unknown arch: ${FLAGS_arch}"
Will Drewry69563b72010-06-24 16:12:58 -0500184fi
185
186set +e # cleanup failure is a-ok
187
188if [[ ${FLAGS_keep_work} -eq ${FLAGS_FALSE} ]]; then
Will Drewrybcbf1c42010-07-03 10:23:30 -0500189 info "Cleaning up temporary files: ${WORK}"
Will Drewry69563b72010-06-24 16:12:58 -0500190 rm ${WORK}
191 rmdir ${FLAGS_working_dir}
192fi
193
Will Drewrybcbf1c42010-07-03 10:23:30 -0500194info "Kernel partition image emitted: ${FLAGS_to}"
195
196if [[ -f ${FLAGS_rootfs_hash} ]]; then
197 info "Root filesystem hash emitted: ${FLAGS_rootfs_hash}"
198fi