blob: 98230e41e6d077658d330ceaf7ec3680d8c7c78d [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
Kenneth Watersed54d932010-09-21 10:29:54 -070091 if [[ "${FLAGS_arch}" = "x86" ]]; then
92 base_root='/dev/sd%D%P'
93 elif [[ "${FLAGS_arch}" = "arm" ]]; then
94 base_root='/dev/${devname}${rootpart}'
95 fi
96 table=${table//HASH_DEV/${base_root}}
97 table=${table//ROOT_DEV/${base_root}}
Will Drewrybcbf1c42010-07-03 10:23:30 -050098 fi
Will Drewry78992a32010-07-21 14:02:20 -050099 verity_args="dm=\"vroot none ro,${table}\""
Will Drewry1670d482010-07-09 13:08:38 -0700100 info "dm-verity configuration: ${verity_args}"
Will Drewrybcbf1c42010-07-03 10:23:30 -0500101fi
102
103mkdir -p "${FLAGS_working_dir}"
Will Drewryd6435d42010-10-20 15:37:46 -0500104
105# Only let dm-verity block if rootfs verification is configured.
106dev_wait=0
107if [[ ${FLAGS_root} = "/dev/dm-0" ]]; then
108 dev_wait=1
109fi
110
Will Drewrybcbf1c42010-07-03 10:23:30 -0500111cat <<EOF > "${FLAGS_working_dir}/boot.config"
112root=${FLAGS_root}
Will Drewry1670d482010-07-09 13:08:38 -0700113dm_verity.error_behavior=${FLAGS_verity_error_behavior}
114dm_verity.max_bios=${FLAGS_verity_max_ios}
Will Drewryd6435d42010-10-20 15:37:46 -0500115dm_verity.dev_wait=${dev_wait}
Will Drewry1670d482010-07-09 13:08:38 -0700116${verity_args}
Will Drewrybcbf1c42010-07-03 10:23:30 -0500117${FLAGS_boot_args}
118EOF
119
120WORK="${WORK} ${FLAGS_working_dir}/boot.config"
121info "Emitted cross-platform boot params to ${FLAGS_working_dir}/boot.config"
122
Will Drewry69563b72010-06-24 16:12:58 -0500123# FIXME: At the moment, we're working on signed images for x86 only. ARM will
124# support this before shipping, but at the moment they don't.
125if [[ "${FLAGS_arch}" = "x86" ]]; then
126
Will Drewrybcbf1c42010-07-03 10:23:30 -0500127 # Legacy BIOS will use the kernel in the rootfs (via syslinux), as will
128 # standard EFI BIOS (via grub, from the EFI System Partition). Chrome OS
129 # BIOS will use a separate signed kernel partition, which we'll create now.
130 # FIXME: remove serial output, debugging messages.
131 mkdir -p ${FLAGS_working_dir}
132 cat <<EOF | cat - "${FLAGS_working_dir}/boot.config" \
133 > "${FLAGS_working_dir}/config.txt"
Nick Sandersada8a1f2010-10-26 02:56:51 -0700134quiet
135console=tty2
Will Drewry69563b72010-06-24 16:12:58 -0500136init=/sbin/init
137add_efi_memmap
138boot=local
139rootwait
Will Drewry69563b72010-06-24 16:12:58 -0500140ro
141noresume
142noswap
143i915.modeset=1
Nick Sandersada8a1f2010-10-26 02:56:51 -0700144loglevel=1
Will Drewry69563b72010-06-24 16:12:58 -0500145cros_secure
Bill Richardson8bfa4682010-07-23 17:24:15 -0700146kern_guid=%U
Luigi Semenzato195aebe2010-10-20 17:35:00 -0700147tpm_tis.force=1
148tpm_tis.interrupts=0
Will Drewry69563b72010-06-24 16:12:58 -0500149EOF
Will Drewrybcbf1c42010-07-03 10:23:30 -0500150 WORK="${WORK} ${FLAGS_working_dir}/config.txt"
Will Drewry69563b72010-06-24 16:12:58 -0500151
Louis Yung-Chieh Lo36020402010-07-05 13:23:34 +0800152 # We sign the image with the recovery_key, because this is what goes onto the
153 # USB key. We can only boot from the USB drive in recovery mode.
Tan Gao843b70a2010-08-17 09:41:48 -0700154 # For dev install shim, we need to use the installer keyblock instead of
155 # the recovery keyblock because of the difference in flags.
156 if [ ${FLAGS_use_dev_keys} -eq ${FLAGS_TRUE} ]; then
157 USB_KEYBLOCK=installer_kernel.keyblock
158 info "DEBUG: use dev install signing key"
159 else
160 USB_KEYBLOCK=recovery_kernel.keyblock
161 info "DEBUG: use recovery signing key"
162 fi
Bill Richardson2ace49e2010-07-01 10:23:27 -0700163
Will Drewrybcbf1c42010-07-03 10:23:30 -0500164 # Create and sign the kernel blob
165 vbutil_kernel \
166 --pack "${FLAGS_to}" \
Tan Gao843b70a2010-08-17 09:41:48 -0700167 --keyblock "${FLAGS_keys_dir}/${USB_KEYBLOCK}" \
Louis Yung-Chieh Lo36020402010-07-05 13:23:34 +0800168 --signprivate "${FLAGS_keys_dir}/recovery_kernel_data_key.vbprivk" \
Will Drewrybcbf1c42010-07-03 10:23:30 -0500169 --version 1 \
170 --config "${FLAGS_working_dir}/config.txt" \
171 --bootloader /lib64/bootstub/bootstub.efi \
172 --vmlinuz "${FLAGS_vmlinuz}"
Will Drewry69563b72010-06-24 16:12:58 -0500173
Will Drewrybcbf1c42010-07-03 10:23:30 -0500174 # And verify it.
175 vbutil_kernel \
176 --verify "${FLAGS_to}" \
Louis Yung-Chieh Lo36020402010-07-05 13:23:34 +0800177 --signpubkey "${FLAGS_keys_dir}/recovery_key.vbpubk"
178
179
180 # Now we re-sign the same image using the normal keys. This is the kernel
181 # image that is put on the hard disk by the installer. Note: To save space on
182 # the USB image, we're only emitting the new verfication block, and the
183 # installer just replaces that part of the hard disk's kernel partition.
184 vbutil_kernel \
185 --repack "${FLAGS_hd_vblock}" \
186 --vblockonly \
187 --keyblock "${FLAGS_keys_dir}/kernel.keyblock" \
188 --signprivate "${FLAGS_keys_dir}/kernel_data_key.vbprivk" \
189 --oldblob "${FLAGS_to}"
190
191
192 # To verify it, we have to replace the vblock from the original image.
193 tempfile=$(mktemp)
194 trap "rm -f $tempfile" EXIT
195 cat "${FLAGS_hd_vblock}" > $tempfile
196 dd if="${FLAGS_to}" bs=65536 skip=1 >> $tempfile
197
198 vbutil_kernel \
199 --verify $tempfile \
200 --signpubkey "${FLAGS_keys_dir}/kernel_subkey.vbpubk"
201
202 rm -f $tempfile
203 trap - EXIT
Will Drewry69563b72010-06-24 16:12:58 -0500204
Will Drewrybcbf1c42010-07-03 10:23:30 -0500205elif [[ "${FLAGS_arch}" = "arm" ]]; then
Kenneth Watersed54d932010-09-21 10:29:54 -0700206 # FIXME: This stuff is unsigned, and will likely change with vboot_reference
207 # but it doesn't technically have to.
208
209 kernel_script="${FLAGS_working_dir}/kernel.scr"
210 kernel_script_img="${FLAGS_working_dir}/kernel.scr.uimg"
211 # HACK: !! Kernel image construction requires some stuff from portage, not
212 # sure how to get that information here cleanly !!
213 kernel_image="${FLAGS_vmlinuz/vmlinuz/vmlinux.uimg}"
214 WORK="${WORK} ${kernel_script} ${kernel_script_img}"
215
216 kernel_size=$((($(stat -c %s "${kernel_image}") + 511) / 512))
217 script_size=16
218
219 # Build boot script image
220 echo -n 'setenv bootargs ${bootargs} ' > "${kernel_script}"
221 tr '\n' ' ' <"${FLAGS_working_dir}/boot.config" >> "${kernel_script}"
222 echo >> "${kernel_script}"
223 printf 'read ${devtype} 0:${kernelpart} ${loadaddr} %x %x\n' \
224 ${script_size} ${kernel_size} >> "${kernel_script}"
225 echo 'bootm ${loadaddr}' >> ${kernel_script}
226 mkimage -A arm -O linux -T script -C none -a 0 -e 0 \
227 -n kernel_script -d "${kernel_script}" "${kernel_script_img}"
228
229 if [ $(stat -c %s "${kernel_script_img}") -gt $((512 * ${script_size})) ]
230 then
231 echo 'Kernel script too large for reserved space.'
232 exit 1
233 fi
234
235 # Assemble image
236 rm -f "${FLAGS_to}"
237 dd if="${kernel_script_img}" of="${FLAGS_to}" bs=512 count="${script_size}"
238 dd if="${kernel_image}" of="${FLAGS_to}" bs=512 seek="${script_size}"
Kenneth Waters9bc78b32010-09-22 13:54:44 -0700239
240 # TODO: HACK: Until the kernel partition contains a signed image, create a
241 # phony hd.vblock to keep chromeos-install and cros_generate_update_payload
242 # working.
243 dd if="${FLAGS_to}" of="${FLAGS_hd_vblock}" bs=64K count=1
Will Drewrybcbf1c42010-07-03 10:23:30 -0500244else
245 error "Unknown arch: ${FLAGS_arch}"
Will Drewry69563b72010-06-24 16:12:58 -0500246fi
247
248set +e # cleanup failure is a-ok
249
250if [[ ${FLAGS_keep_work} -eq ${FLAGS_FALSE} ]]; then
Will Drewrybcbf1c42010-07-03 10:23:30 -0500251 info "Cleaning up temporary files: ${WORK}"
Will Drewry69563b72010-06-24 16:12:58 -0500252 rm ${WORK}
253 rmdir ${FLAGS_working_dir}
254fi
255
Will Drewrybcbf1c42010-07-03 10:23:30 -0500256info "Kernel partition image emitted: ${FLAGS_to}"
257
258if [[ -f ${FLAGS_rootfs_hash} ]]; then
259 info "Root filesystem hash emitted: ${FLAGS_rootfs_hash}"
260fi