blob: e3f4182127711444e053cd8fe365d789cff4c5b1 [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
Greg Spencer798d75f2011-02-01 22:04:49 -08009# --- BEGIN COMMON.SH BOILERPLATE ---
10# Load common CrOS utilities. Inside the chroot this file is installed in
11# /usr/lib/crosutils. Outside the chroot we find it relative to the script's
12# location.
13find_common_sh() {
14 local common_paths=(/usr/lib/crosutils $(dirname "$(readlink -f "$0")"))
15 local path
16
17 SCRIPT_ROOT=
18 for path in "${common_paths[@]}"; do
19 if [ -r "${path}/common.sh" ]; then
20 SCRIPT_ROOT=${path}
21 break
22 fi
23 done
24}
25
26find_common_sh
Brian Harringd5d5dbf2011-07-11 16:36:21 -070027. "${SCRIPT_ROOT}/common.sh" || { echo "Unable to load common.sh"; exit 1; }
Greg Spencer798d75f2011-02-01 22:04:49 -080028# --- END COMMON.SH BOILERPLATE ---
Will Drewry69563b72010-06-24 16:12:58 -050029
30get_default_board
31
32# Flags.
33DEFINE_string arch "x86" \
Sonny Rao0679b362011-10-12 13:53:19 -070034 "The boot architecture: arm, x86, or amd64. (Default: x86)"
Will Drewry69563b72010-06-24 16:12:58 -050035DEFINE_string to "/tmp/vmlinuz.image" \
36 "The path to the kernel image to be created. (Default: /tmp/vmlinuz.image)"
Louis Yung-Chieh Lo36020402010-07-05 13:23:34 +080037DEFINE_string hd_vblock "/tmp/vmlinuz_hd.vblock" \
38 "The path to the installed kernel's vblock (Default: /tmp/vmlinuz_hd.vblock)"
Will Drewry69563b72010-06-24 16:12:58 -050039DEFINE_string vmlinuz "vmlinuz" \
40 "The path to the kernel (Default: vmlinuz)"
41DEFINE_string working_dir "/tmp/vmlinuz.working" \
42 "Working directory for in-progress files. (Default: /tmp/vmlinuz.working)"
43DEFINE_boolean keep_work ${FLAGS_FALSE} \
44 "Keep temporary files (*.keyblock, *.vbpubk). (Default: false)"
45DEFINE_string keys_dir "${SRC_ROOT}/platform/vboot_reference/tests/testkeys" \
Bill Richardson2ace49e2010-07-01 10:23:27 -070046 "Directory with the RSA signing keys. (Defaults to test keys)"
Tan Gao843b70a2010-08-17 09:41:48 -070047DEFINE_boolean use_dev_keys ${FLAGS_FALSE} \
48 "Use developer keys for signing. (Default: false)"
Will Drewrybcbf1c42010-07-03 10:23:30 -050049# Note, to enable verified boot, the caller would manually pass:
Will Drewryb910de82011-02-23 13:26:50 -060050# --boot_args='dm="... %U+1 %U+1 ..." \
Will Drewry69563b72010-06-24 16:12:58 -050051# --root=/dev/dm-0
52DEFINE_string boot_args "noinitrd" \
53 "Additional boot arguments to pass to the commandline (Default: noinitrd)"
Will Drewryb910de82011-02-23 13:26:50 -060054# By default, we use a firmware enumerated value, but it isn't reliable for
55# production use. If +%d can be added upstream, then we can use:
56# root=PARTUID=uuid+1
Che-Liang Chiou93b6eff2011-05-18 17:21:13 +080057DEFINE_string root "PARTUUID=%U/PARTNROFF=1" \
Will Drewryb910de82011-02-23 13:26:50 -060058 "Expected device root partition"
Will Drewrybcbf1c42010-07-03 10:23:30 -050059# If provided, will automatically add verified boot arguments.
60DEFINE_string rootfs_image "" \
61 "Optional path to the rootfs device or image.(Default: \"\")"
62DEFINE_string rootfs_hash "" \
63 "Optional path to output the rootfs hash to. (Default: \"\")"
Will Drewry1670d482010-07-09 13:08:38 -070064DEFINE_integer verity_error_behavior 2 \
Will Drewrybcbf1c42010-07-03 10:23:30 -050065 "Verified boot error behavior [0: I/O errors, 1: reboot, 2: nothing] \
66(Default: 2)"
Will Drewryb910de82011-02-23 13:26:50 -060067DEFINE_integer verity_max_ios -1 \
68 "Optional number of outstanding I/O operations. (Default: -1)"
Will Drewry1670d482010-07-09 13:08:38 -070069DEFINE_string verity_hash_alg "sha1" \
70 "Cryptographic hash algorithm used for dm-verity. (Default: sha1)"
Elly Jones9ca3e4c2011-09-26 15:18:19 -040071DEFINE_string verity_salt "" \
72 "Salt to use for rootfs hash (Default: \"\")"
Will Drewrybcbf1c42010-07-03 10:23:30 -050073
Will Drewry69563b72010-06-24 16:12:58 -050074# Parse flags
75FLAGS "$@" || exit 1
76eval set -- "${FLAGS_ARGV}"
77
78# Die on error
79set -e
80
Will Drewry1670d482010-07-09 13:08:38 -070081verity_args=
Will Drewrybcbf1c42010-07-03 10:23:30 -050082# Even with a rootfs_image, root= is not changed unless specified.
83if [[ -n "${FLAGS_rootfs_image}" && -n "${FLAGS_rootfs_hash}" ]]; then
Will Drewrybcbf1c42010-07-03 10:23:30 -050084 # Gets the number of blocks. 4096 byte blocks _are_ expected.
Da Zheng87465ea2011-07-30 16:25:28 -070085 if [ -f "${FLAGS_rootfs_image}" ]; then
86 root_fs_block_sz=4096
87 root_fs_sz=$(stat -c '%s' ${FLAGS_rootfs_image})
88 root_fs_blocks=$((root_fs_sz / ${root_fs_block_sz}))
89 else
90 root_fs_blocks=$(sudo dumpe2fs "${FLAGS_rootfs_image}" 2> /dev/null |
Will Drewrybcbf1c42010-07-03 10:23:30 -050091 grep "Block count" |
92 tr -d ' ' |
93 cut -f2 -d:)
Da Zheng87465ea2011-07-30 16:25:28 -070094 root_fs_block_sz=$(sudo dumpe2fs "${FLAGS_rootfs_image}" 2> /dev/null |
Will Drewrybcbf1c42010-07-03 10:23:30 -050095 grep "Block size" |
96 tr -d ' ' |
97 cut -f2 -d:)
Da Zheng87465ea2011-07-30 16:25:28 -070098 fi
99
Will Drewry78992a32010-07-21 14:02:20 -0500100 info "rootfs is ${root_fs_blocks} blocks of ${root_fs_block_sz} bytes"
Will Drewrybcbf1c42010-07-03 10:23:30 -0500101 if [[ ${root_fs_block_sz} -ne 4096 ]]; then
102 error "Root file system blocks are not 4k!"
103 fi
104
Elly Jones9ca3e4c2011-09-26 15:18:19 -0400105 info "Generating root fs hash tree (salt '${FLAGS_verity_salt}')."
Will Drewrybcbf1c42010-07-03 10:23:30 -0500106 # Runs as sudo in case the image is a block device.
Mandeep Singh Baines118692a2011-04-28 13:50:33 -0700107 # First argument to verity is reserved/unused and MUST be 0
Elly Jones5e7a4302011-05-27 16:08:16 -0400108 table=$(sudo verity mode=create \
109 alg=${FLAGS_verity_hash_alg} \
110 payload=${FLAGS_rootfs_image} \
111 payload_blocks=${root_fs_blocks} \
Elly Jonesf4e48332011-09-02 13:25:04 -0400112 hashtree=${FLAGS_rootfs_hash} \
Elly Jones9ca3e4c2011-09-26 15:18:19 -0400113 salt=${FLAGS_verity_salt})
Will Drewry821d07c2010-07-03 17:14:58 -0700114 if [[ -f "${FLAGS_rootfs_hash}" ]]; then
115 sudo chmod a+r "${FLAGS_rootfs_hash}"
116 fi
Will Drewrybcbf1c42010-07-03 10:23:30 -0500117 # Don't claim the root device unless the root= flag is pointed to
118 # the verified boot device. Doing so will claim /dev/sdDP out from
119 # under the system.
120 if [[ ${FLAGS_root} = "/dev/dm-0" ]]; then
Nick Sandersffd0ca62011-06-25 01:20:24 -0700121 base_root='%U+1' # kern_guid + 1
Kenneth Watersed54d932010-09-21 10:29:54 -0700122 table=${table//HASH_DEV/${base_root}}
123 table=${table//ROOT_DEV/${base_root}}
Will Drewrybcbf1c42010-07-03 10:23:30 -0500124 fi
Will Drewry78992a32010-07-21 14:02:20 -0500125 verity_args="dm=\"vroot none ro,${table}\""
Will Drewry1670d482010-07-09 13:08:38 -0700126 info "dm-verity configuration: ${verity_args}"
Will Drewrybcbf1c42010-07-03 10:23:30 -0500127fi
128
129mkdir -p "${FLAGS_working_dir}"
Will Drewryd6435d42010-10-20 15:37:46 -0500130
131# Only let dm-verity block if rootfs verification is configured.
132dev_wait=0
133if [[ ${FLAGS_root} = "/dev/dm-0" ]]; then
134 dev_wait=1
135fi
136
Will Drewrybcbf1c42010-07-03 10:23:30 -0500137cat <<EOF > "${FLAGS_working_dir}/boot.config"
138root=${FLAGS_root}
Olof Johanssone0b75512011-01-26 14:31:26 -0800139rootwait
140ro
Will Drewry1670d482010-07-09 13:08:38 -0700141dm_verity.error_behavior=${FLAGS_verity_error_behavior}
142dm_verity.max_bios=${FLAGS_verity_max_ios}
Will Drewryd6435d42010-10-20 15:37:46 -0500143dm_verity.dev_wait=${dev_wait}
Will Drewry1670d482010-07-09 13:08:38 -0700144${verity_args}
Will Drewrybcbf1c42010-07-03 10:23:30 -0500145${FLAGS_boot_args}
Pawel Osciaka2c23752011-11-15 15:09:45 -0800146vt.global_cursor_default=0
Will Drewrybcbf1c42010-07-03 10:23:30 -0500147EOF
148
149WORK="${WORK} ${FLAGS_working_dir}/boot.config"
150info "Emitted cross-platform boot params to ${FLAGS_working_dir}/boot.config"
151
Sonny Rao3163cf02011-10-17 16:20:28 -0700152if [[ "${FLAGS_arch}" = "x86" || "${FLAGS_arch}" = "amd64" ]]; then
Will Drewrybcbf1c42010-07-03 10:23:30 -0500153 # Legacy BIOS will use the kernel in the rootfs (via syslinux), as will
154 # standard EFI BIOS (via grub, from the EFI System Partition). Chrome OS
155 # BIOS will use a separate signed kernel partition, which we'll create now.
156 # FIXME: remove serial output, debugging messages.
Will Drewrybcbf1c42010-07-03 10:23:30 -0500157 cat <<EOF | cat - "${FLAGS_working_dir}/boot.config" \
158 > "${FLAGS_working_dir}/config.txt"
Olof Johansson24e10cb2011-04-07 15:08:08 -0700159quiet
160loglevel=1
Nick Sandersada8a1f2010-10-26 02:56:51 -0700161console=tty2
Will Drewry69563b72010-06-24 16:12:58 -0500162init=/sbin/init
163add_efi_memmap
164boot=local
Will Drewry69563b72010-06-24 16:12:58 -0500165noresume
166noswap
167i915.modeset=1
Will Drewry69563b72010-06-24 16:12:58 -0500168cros_secure
Bill Richardson8bfa4682010-07-23 17:24:15 -0700169kern_guid=%U
Luigi Semenzato195aebe2010-10-20 17:35:00 -0700170tpm_tis.force=1
171tpm_tis.interrupts=0
Vadim Bendebury8e623f62011-02-28 10:28:31 -0800172nmi_watchdog=panic,lapic
Will Drewry69563b72010-06-24 16:12:58 -0500173EOF
Will Drewrybcbf1c42010-07-03 10:23:30 -0500174 WORK="${WORK} ${FLAGS_working_dir}/config.txt"
Will Drewry69563b72010-06-24 16:12:58 -0500175
Che-Liang Chiou75ac2be2011-02-24 12:00:16 +0800176 bootloader_path="/lib64/bootstub/bootstub.efi"
177 kernel_image="${FLAGS_vmlinuz}"
Che-Liang Chiou75ac2be2011-02-24 12:00:16 +0800178elif [[ "${FLAGS_arch}" = "arm" ]]; then
Olof Johansson24e10cb2011-04-07 15:08:08 -0700179 cat <<EOF | cat - "${FLAGS_working_dir}/boot.config" \
180 > "${FLAGS_working_dir}/config.txt"
181earlyprintk
Nick Sandersf9fa9f52011-05-24 11:48:23 -0700182kern_guid=%U
Katie Roberts-Hoffman8429b4e2011-09-27 13:34:59 -0700183vmalloc=234MB
Olof Johansson24e10cb2011-04-07 15:08:08 -0700184EOF
Che-Liang Chiou75ac2be2011-02-24 12:00:16 +0800185 WORK="${WORK} ${FLAGS_working_dir}/config.txt"
186
Che-Liang Chiou69faa262011-06-01 11:52:21 +0800187 # arm does not need/have a bootloader in kernel partition
188 dd if="/dev/zero" of="${FLAGS_working_dir}/bootloader.bin" bs=512 count=1
189 WORK="${WORK} ${FLAGS_working_dir}/bootloader.bin"
Che-Liang Chiou75ac2be2011-02-24 12:00:16 +0800190
Che-Liang Chiou69faa262011-06-01 11:52:21 +0800191 bootloader_path="${FLAGS_working_dir}/bootloader.bin"
Che-Liang Chiou75ac2be2011-02-24 12:00:16 +0800192 kernel_image="${FLAGS_vmlinuz/vmlinuz/vmlinux.uimg}"
Che-Liang Chiou75ac2be2011-02-24 12:00:16 +0800193else
194 error "Unknown arch: ${FLAGS_arch}"
195fi
196
Che-Liang Chioue51bdf22011-07-26 21:19:24 +0800197# We sign the image with the recovery_key, because this is what goes onto the
198# USB key. We can only boot from the USB drive in recovery mode.
199# For dev install shim, we need to use the installer keyblock instead of
200# the recovery keyblock because of the difference in flags.
201if [ ${FLAGS_use_dev_keys} -eq ${FLAGS_TRUE} ]; then
202 USB_KEYBLOCK=installer_kernel.keyblock
203 info "DEBUG: use dev install signing key"
Che-Liang Chiou75ac2be2011-02-24 12:00:16 +0800204else
Che-Liang Chioue51bdf22011-07-26 21:19:24 +0800205 USB_KEYBLOCK=recovery_kernel.keyblock
206 info "DEBUG: use recovery signing key"
Will Drewry69563b72010-06-24 16:12:58 -0500207fi
208
Che-Liang Chioue51bdf22011-07-26 21:19:24 +0800209# Create and sign the kernel blob
210vbutil_kernel \
211 --pack "${FLAGS_to}" \
212 --keyblock "${FLAGS_keys_dir}/${USB_KEYBLOCK}" \
213 --signprivate "${FLAGS_keys_dir}/recovery_kernel_data_key.vbprivk" \
214 --version 1 \
215 --config "${FLAGS_working_dir}/config.txt" \
216 --bootloader "${bootloader_path}" \
217 --vmlinuz "${kernel_image}" \
218 --arch "${FLAGS_arch}"
219
220# And verify it.
221vbutil_kernel \
222 --verify "${FLAGS_to}" \
223 --signpubkey "${FLAGS_keys_dir}/recovery_key.vbpubk"
224
225
226# Now we re-sign the same image using the normal keys. This is the kernel
227# image that is put on the hard disk by the installer. Note: To save space on
228# the USB image, we're only emitting the new verfication block, and the
229# installer just replaces that part of the hard disk's kernel partition.
230vbutil_kernel \
231 --repack "${FLAGS_hd_vblock}" \
232 --vblockonly \
233 --keyblock "${FLAGS_keys_dir}/kernel.keyblock" \
234 --signprivate "${FLAGS_keys_dir}/kernel_data_key.vbprivk" \
235 --oldblob "${FLAGS_to}"
236
237
238# To verify it, we have to replace the vblock from the original image.
239tempfile=$(mktemp)
240trap "rm -f $tempfile" EXIT
241cat "${FLAGS_hd_vblock}" > $tempfile
242dd if="${FLAGS_to}" bs=65536 skip=1 >> $tempfile
243
244vbutil_kernel \
245 --verify $tempfile \
246 --signpubkey "${FLAGS_keys_dir}/kernel_subkey.vbpubk"
247
248rm -f $tempfile
249trap - EXIT
250
Will Drewry69563b72010-06-24 16:12:58 -0500251set +e # cleanup failure is a-ok
252
253if [[ ${FLAGS_keep_work} -eq ${FLAGS_FALSE} ]]; then
Will Drewrybcbf1c42010-07-03 10:23:30 -0500254 info "Cleaning up temporary files: ${WORK}"
Will Drewry69563b72010-06-24 16:12:58 -0500255 rm ${WORK}
256 rmdir ${FLAGS_working_dir}
257fi
258
Will Drewrybcbf1c42010-07-03 10:23:30 -0500259info "Kernel partition image emitted: ${FLAGS_to}"
260
261if [[ -f ${FLAGS_rootfs_hash} ]]; then
262 info "Root filesystem hash emitted: ${FLAGS_rootfs_hash}"
263fi